Skip to content

Commit

Permalink
Try to remove nightly features
Browse files Browse the repository at this point in the history
  • Loading branch information
jhorstmann committed Oct 24, 2024
1 parent a40cdd3 commit 0e90746
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 97 deletions.
12 changes: 7 additions & 5 deletions src/error_strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -560,12 +560,14 @@ impl<'input, Ctx: ParserNodeType<'input>> BailErrorStrategy<'input, Ctx> {
e: &ANTLRError,
) -> ANTLRError {
let mut ctx = recognizer.get_parser_rule_context().clone();
let _: Option<()> = try {
loop {
ctx.set_exception(e.clone());
ctx = ctx.get_parent()?
loop {
ctx.set_exception(e.clone());
let parent = ctx.get_parent();
if parent.is_none() {
break;
}
};
ctx = parent.unwrap();
}
return ANTLRError::FallThrough(Rc::new(ParseCancelledError(e.clone())));
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/lexer_atn_simulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ impl ILexerATNSimulator for LexerATNSimulator {
fn consume<T: IntStream + ?Sized>(&self, _input: &mut T) {
let ch = _input.la(1);
if ch == '\n' as isize {
self.current_pos.line.update(|x| x + 1);
self.current_pos.line.set(self.current_pos.line.get() + 1);
self.current_pos.char_position_in_line.set(0);
} else {
self.current_pos.char_position_in_line.update(|x| x + 1);
self.current_pos.char_position_in_line.set(self.current_pos.char_position_in_line.get() + 1);
}
_input.consume();
}
Expand Down
9 changes: 0 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
#![crate_type = "lib"]
#![feature(try_blocks)]
//#![feature(nll)]
#![feature(is_sorted)]
#![feature(cell_update)]
#![feature(get_mut_unchecked)]
#![feature(specialization)]
#![feature(coerce_unsized)]
#![feature(associated_type_defaults)]
#![feature(generic_associated_types)]
// #![feature(generic_associated_types)]
#![warn(rust_2018_idioms)]
#![warn(missing_docs)] // warn if there is missing docs
#![warn(trivial_numeric_casts)]
Expand Down
2 changes: 1 addition & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ where
offending_token: Option<isize>,
err: Option<&ANTLRError>,
) {
self._syntax_errors.update(|it| it + 1);
self._syntax_errors.set(self._syntax_errors.get() + 1);
let offending_token: Option<&_> = match offending_token {
None => Some(self.get_current_token().borrow()),
Some(x) => Some(self.input.get(x).borrow()),
Expand Down
7 changes: 3 additions & 4 deletions src/parser_rule_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ pub fn cast_mut<'a, T: ParserRuleContext<'a> + 'a + ?Sized, Result: 'a>(
// if Rc::strong_count(ctx) != 1 { panic!("cant mutate Rc with multiple strong ref count"); }
// is it safe because parser does not save/move mutable references anywhere.
// they are only used to write data immediately in the corresponding expression
unsafe { &mut *(Rc::get_mut_unchecked(ctx) as *mut T as *mut Result) }
unsafe { &mut *(Rc::get_mut(ctx).unwrap_unchecked() as *mut T as *mut Result) }
}

// workaround newtype for cycle in trait definition
Expand Down Expand Up @@ -240,7 +240,7 @@ pub struct BaseParserRuleContext<'input, Ctx: CustomRuleContext<'input>> {
}

impl<'input, Ctx: CustomRuleContext<'input>> Debug for BaseParserRuleContext<'input, Ctx> {
default fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
f.write_str(type_name::<Self>())
}
}
Expand Down Expand Up @@ -410,8 +410,7 @@ impl<'input, Ctx: CustomRuleContext<'input> + TidAble<'input>> ParseTree<'input>
b: self.stop().get_token_index(),
}
}

default fn get_text(&self) -> String {
fn get_text(&self) -> String {
let children = self.get_children();
let mut result = String::new();

Expand Down
45 changes: 2 additions & 43 deletions src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,20 @@ use std::marker::PhantomData;
use std::ops::{CoerceUnsized, Deref};
use std::rc::Rc;

use crate::atn::INVALID_ALT;
use crate::char_stream::InputData;
use crate::int_stream::EOF;
use crate::interval_set::Interval;
use crate::parser::ParserNodeType;
use crate::parser_rule_context::{ParserRuleContext, RuleContextExt};
use crate::recognizer::Recognizer;
use crate::rule_context::{CustomRuleContext, RuleContext};
use crate::token::Token;
use crate::token_factory::TokenFactory;
use crate::{interval_set, trees};
use crate::interval_set;
use better_any::{Tid, TidAble};

//todo try to make in more generic
#[allow(missing_docs)]
pub trait Tree<'input>: NodeText + RuleContext<'input> {
pub trait Tree<'input>: RuleContext<'input> {
fn get_parent(&self) -> Option<Rc<<Self::Ctx as ParserNodeType<'input>>::Type>> { None }
fn has_parent(&self) -> bool { false }
fn get_payload(&self) -> Box<dyn Any> { unimplemented!() }
Expand Down Expand Up @@ -69,39 +67,6 @@ pub trait ParseTree<'input>: Tree<'input> {
/// method.
fn get_text(&self) -> String { String::new() }

/// Print out a whole tree, not just a node, in LISP format
/// (root child1 .. childN). Print just a node if this is a leaf.
/// We have to know the recognizer so we can get rule names.
fn to_string_tree(
&self,
r: &dyn Recognizer<'input, TF = Self::TF, Node = Self::Ctx>,
) -> String {
trees::string_tree(self, r.get_rule_names())
}
}

/// text of the node.
/// Already implemented for all rule contexts
pub trait NodeText {
/// Returns text representation of current node type,
/// rule name for context nodes and token text for terminal nodes
fn get_node_text(&self, rule_names: &[&str]) -> String;
}

impl<T> NodeText for T {
default fn get_node_text(&self, _rule_names: &[&str]) -> String { "<unknown>".to_owned() }
}

impl<'input, T: CustomRuleContext<'input>> NodeText for T {
default fn get_node_text(&self, rule_names: &[&str]) -> String {
let rule_index = self.get_rule_index();
let rule_name = rule_names[rule_index];
let alt_number = self.get_alt_number();
if alt_number != INVALID_ALT {
return format!("{}:{}", rule_name, alt_number);
}
return rule_name.to_owned();
}
}

#[doc(hidden)]
Expand Down Expand Up @@ -141,12 +106,6 @@ impl<'input, Node: ParserNodeType<'input>, T: 'static> RuleContext<'input>
{
}

impl<'input, Node: ParserNodeType<'input>, T: 'static> NodeText for LeafNode<'input, Node, T> {
fn get_node_text(&self, _rule_names: &[&str]) -> String {
self.symbol.borrow().get_text().to_display()
}
}

impl<'input, Node: ParserNodeType<'input>, T: 'static> ParseTree<'input>
for LeafNode<'input, Node, T>
{
Expand Down
33 changes: 0 additions & 33 deletions src/trees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,6 @@
A set of utility routines useful for all kinds of ANTLR trees.
*/

use std::ops::Deref;

use crate::tree::Tree;
use crate::utils;

/// Print out a whole tree, not just a node, in LISP format
/// {@code (root child1 .. childN)}. Print just a node if this is a leaf.
pub fn string_tree<'a, T: Tree<'a> + ?Sized>(tree: &T, rule_names: &[&str]) -> String {
let s = utils::escape_whitespaces(get_node_text(tree, rule_names), false);
if tree.get_child_count() == 0 {
return s;
}
let mut result = String::new();
result.push('(');
result.extend(s.chars());
result = tree
.get_children()
// .iter()
.map(|child| string_tree(child.deref(), rule_names))
.fold(result, |mut acc, text| {
acc.push(' ');
acc.extend(text.chars());
acc
});
result.push(')');
result
}

/// Print out tree node text representation (rule name or token text)
pub fn get_node_text<'a>(t: &(impl Tree<'a> + ?Sized), rule_names: &[&str]) -> String {
t.get_node_text(rule_names)
}

//pub fn get_children(t: impl Tree) -> Vec<Rc<dyn Tree>> { unimplemented!() }
//
//pub fn get_ancestors(t: impl Tree) -> Vec<Rc<dyn Tree>> { unimplemented!() }
Expand Down

0 comments on commit 0e90746

Please sign in to comment.