diff --git a/src/error_strategy.rs b/src/error_strategy.rs index ab3449a..b0f78e0 100644 --- a/src/error_strategy.rs +++ b/src/error_strategy.rs @@ -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()))); } } diff --git a/src/lexer_atn_simulator.rs b/src/lexer_atn_simulator.rs index 4394bc3..f122279 100644 --- a/src/lexer_atn_simulator.rs +++ b/src/lexer_atn_simulator.rs @@ -114,10 +114,10 @@ impl ILexerATNSimulator for LexerATNSimulator { fn consume(&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(); } diff --git a/src/lib.rs b/src/lib.rs index d3fcce4..f224cf7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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)] diff --git a/src/parser.rs b/src/parser.rs index e3c3caf..d8f855d 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -301,7 +301,7 @@ where offending_token: Option, 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()), diff --git a/src/parser_rule_context.rs b/src/parser_rule_context.rs index 4926ad8..2ac651e 100644 --- a/src/parser_rule_context.rs +++ b/src/parser_rule_context.rs @@ -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 @@ -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::()) } } @@ -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(); diff --git a/src/tree.rs b/src/tree.rs index f4ff24d..d1efd24 100644 --- a/src/tree.rs +++ b/src/tree.rs @@ -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>::Type>> { None } fn has_parent(&self) -> bool { false } fn get_payload(&self) -> Box { unimplemented!() } @@ -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 NodeText for T { - default fn get_node_text(&self, _rule_names: &[&str]) -> String { "".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)] @@ -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> { diff --git a/src/trees.rs b/src/trees.rs index 67602ea..5d6be75 100644 --- a/src/trees.rs +++ b/src/trees.rs @@ -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> { unimplemented!() } // //pub fn get_ancestors(t: impl Tree) -> Vec> { unimplemented!() }