From 4779134a583a3bf20ae68b4377ade94d32cee69c Mon Sep 17 00:00:00 2001 From: Konstantin Date: Fri, 22 Jul 2022 16:59:01 +0300 Subject: [PATCH 1/7] v0.3 prerelease (#29) --- .github/workflows/antlr.yml | 17 +- Cargo.toml | 5 +- README.md | 37 +- build.rs | 12 +- grammars/VisitorBasic.g4 | 7 + grammars/VisitorCalc.g4 | 18 + rustfmt.toml | 4 +- src/atn.rs | 4 +- src/atn_config.rs | 36 +- src/atn_config_set.rs | 56 +- src/atn_deserialization_options.rs | 4 +- src/atn_simulator.rs | 12 +- src/atn_state.rs | 40 +- src/char_stream.rs | 16 +- src/common_token_stream.rs | 32 +- src/dfa.rs | 12 +- src/dfa_state.rs | 8 +- src/error_listener.rs | 4 +- src/error_strategy.rs | 51 +- src/errors.rs | 4 +- src/input_stream.rs | 53 +- src/interval_set.rs | 32 +- src/lexer.rs | 81 ++- src/lexer_action_executor.rs | 4 +- src/lexer_atn_simulator.rs | 41 +- src/lib.rs | 89 ++- src/ll1_analyzer.rs | 4 +- src/parser.rs | 116 +++- src/parser_atn_simulator.rs | 28 +- src/parser_rule_context.rs | 119 +++- src/prediction_context.rs | 15 +- src/prediction_context/test.rs | 8 +- src/recognizer.rs | 32 +- src/rule_context.rs | 63 +- src/token.rs | 68 +- src/token_factory.rs | 39 +- src/token_source.rs | 32 +- src/token_stream.rs | 31 +- src/transition.rs | 160 +++-- src/tree.rs | 237 +++++-- src/utils.rs | 11 + src/vocabulary.rs | 20 +- templates/BaseRustTest.java | 7 +- templates/Rust.stg | 94 ++- tests/gen/csvlexer.rs | 53 +- tests/gen/csvlistener.rs | 5 +- tests/gen/csvparser.rs | 139 ++-- tests/gen/csvvisitor.rs | 79 ++- tests/gen/labelslexer.rs | 53 +- tests/gen/labelslistener.rs | 9 +- tests/gen/labelsparser.rs | 206 ++++-- tests/gen/referencetoatnlexer.rs | 53 +- tests/gen/referencetoatnlistener.rs | 2 + tests/gen/referencetoatnparser.rs | 81 ++- tests/gen/simplelrlexer.rs | 53 +- tests/gen/simplelrlistener.rs | 3 +- tests/gen/simplelrparser.rs | 98 ++- tests/gen/visitorbasiclexer.rs | 191 +++++ tests/gen/visitorbasiclistener.rs | 21 + tests/gen/visitorbasicparser.rs | 367 ++++++++++ tests/gen/visitorbasicvisitor.rs | 42 ++ tests/gen/visitorcalclexer.rs | 224 ++++++ tests/gen/visitorcalclistener.rs | 57 ++ tests/gen/visitorcalcparser.rs | 927 +++++++++++++++++++++++++ tests/gen/visitorcalcvisitor.rs | 111 +++ tests/gen/xmllexer.rs | 53 +- tests/{my_test.rs => general_tests.rs} | 64 +- tests/perf.rs | 90 +-- tests/visitors_tests.rs | 385 ++++++++++ 69 files changed, 4320 insertions(+), 809 deletions(-) create mode 100644 grammars/VisitorBasic.g4 create mode 100644 grammars/VisitorCalc.g4 create mode 100644 tests/gen/visitorbasiclexer.rs create mode 100644 tests/gen/visitorbasiclistener.rs create mode 100644 tests/gen/visitorbasicparser.rs create mode 100644 tests/gen/visitorbasicvisitor.rs create mode 100644 tests/gen/visitorcalclexer.rs create mode 100644 tests/gen/visitorcalclistener.rs create mode 100644 tests/gen/visitorcalcparser.rs create mode 100644 tests/gen/visitorcalcvisitor.rs rename tests/{my_test.rs => general_tests.rs} (86%) create mode 100644 tests/visitors_tests.rs diff --git a/.github/workflows/antlr.yml b/.github/workflows/antlr.yml index 03607bc..d0d4cb5 100644 --- a/.github/workflows/antlr.yml +++ b/.github/workflows/antlr.yml @@ -4,15 +4,10 @@ on: push: branches: [ master ] pull_request: - branches: - - master - - v0.3 - - workflow_dispatch: + branches: [ master ] env: CARGO_TERM_COLOR: always - RUST_VERSION: nightly-2021-11-30 jobs: tests-antlr: @@ -35,7 +30,7 @@ jobs: - name: Install Rust uses: actions-rs/toolchain@v1 with: - toolchain: ${{ env.RUST_VERSION }} + toolchain: stable default: true - name: Cache local Maven repository uses: actions/cache@v2 @@ -64,12 +59,15 @@ jobs: runs-on: ubuntu-latest outputs: new_version: ${{ steps.check_ver.outputs.new_version }} + strategy: + matrix: + rust_version: [ 1.52 , stable] steps: - uses: actions/checkout@v2 - name: Install Rust uses: actions-rs/toolchain@v1 with: - toolchain: ${{ env.RUST_VERSION }} + toolchain: ${{ matrix.rust_version }} default: true components: rustfmt - name: Build @@ -81,7 +79,6 @@ jobs: - uses: actions/checkout@v2 if: ${{ github.event_name == 'push' }} with: - # todo change to `fetch-depth: 2` and HEAD^ ref: ${{ github.event.before }} path: base_branch - name: check version bump @@ -110,7 +107,7 @@ jobs: - name: Install Rust uses: actions-rs/toolchain@v1 with: - toolchain: ${{ env.RUST_VERSION }} + toolchain: stable default: true - name: Cargo publish check run: | diff --git a/Cargo.toml b/Cargo.toml index b785371..5977dac 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "antlr-rust" -version = "0.2.2" +version = "0.3.0-beta" authors = ["Konstantin Anisimov "] homepage = "https://github.com/rrevenantt/antlr4rust" repository = "https://github.com/rrevenantt/antlr4rust" @@ -23,7 +23,8 @@ bit-set = "=0.5.*" once_cell = "^1.2" #backtrace = "=0.3" typed-arena = "^2.0" -better_any = "=0.1" +better_any = "0.2.0-dev.1" +#better_any = "=0.1" parking_lot = "0.11" #qcell = { path="../qcell" } diff --git a/README.md b/README.md index 971fefd..e441c10 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # antlr4rust -[![Crate](https://flat.badgen.net/crates/v/antlr-rust)](https://crates.io/crates/antlr_rust/0.2.0) -[![docs](https://flat.badgen.net/badge/docs.rs/v0.2.0)](https://docs.rs/antlr-rust/0.2.0) +[![Crate](https://flat.badgen.net/crates/v/antlr-rust)](https://crates.io/crates/antlr_rust/0.3.0-beta) +[![docs](https://flat.badgen.net/badge/docs.rs/v0.3.0-beta)](https://docs.rs/antlr-rust/0.3.0-beta) ![ANTLR4 testsuite](https://github.com/rrevenantt/antlr4rust/workflows/ANTLR4%20testsuite/badge.svg?event=push) ![cargo test](https://github.com/rrevenantt/antlr4rust/workflows/cargo%20test/badge.svg) [![](https://tokei.rs/b1/github/rrevenantt/antlr4rust)](https://github.com/rrevenantt/antlr4rust) @@ -27,29 +27,10 @@ But if you want to build or change generator yourself: For now development is going on in this repository but eventually it will be merged to main ANTLR4 repo -Currently, requires nightly version of rust. -This likely will be the case until `coerce_unsized` or some kind of coercion trait is stabilized. -There are other unstable features in use but only `CoerceUnsized` is essential. - -Remaining things before merge: - - API stabilization - - [ ] Rust api guidelines compliance - - [ ] more tests for API because it is quite different from Java - -Can be done after merge: - - Documentation - - [ ] Some things are already documented but still far from perfect, also more links needed. - - cfg to not build potentially unnecessary parts - (no Lexer if custom token stream, no ParserATNSimulator if LL(1) grammar) - - run rustfmt on generated parser -###### Long term improvements - - generate enum for labeled alternatives without redundant `Error` option - - option to generate fields instead of getters by default and make visiting based on fields - - make tree generic over pointer type and allow tree nodes to arena. - (requires GAT, otherwise it would be a problem for users that want ownership for parse tree) - - support stable rust - - support no_std(although alloc would still be required) - +Since version `0.3` works on stable rust. +Previous versions are not maintained any more +so in case of nightly breakage you should migrate to the latest version. + ### Usage You should use the ANTLR4 "tool" to generate a parser, that will use the ANTLR @@ -67,9 +48,8 @@ Then add following to `Cargo.toml` of the crate from which generated parser is going to be used: ```toml [dependencies] -antlr-rust = "=0.2" +antlr-rust = "0.3" ``` -and `#![feature(try_blocks)]` in your project root module. ### Parse Tree structure @@ -117,9 +97,11 @@ Also while structs used by generated lexer and parser were customized to track a internals of the lexer cannot be customized enough yet and still track quite a lot of data that might not be used in particular case. So there is still room for improvement. ```text +lexers: large/large_xmlparser time: [1.8598 ms 1.8607 ms 1.8619 ms] large/large_quick_xml time: [1.4623 ms 1.4645 ms 1.4675 ms] large/large_antlr_xml_lexer time: [5.7866 ms 5.7877 ms 5.7891 ms] +parsers: large/large_xmlrs time: [16.734 ms 16.748 ms 16.766 ms] large/large_minidom time: [7.0639 ms 7.0792 ms 7.0975 ms] large/large_roxmltree time: [4.9341 ms 4.9360 ms 4.9380 ms] @@ -140,3 +122,4 @@ BSD 3-clause. Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this project by you shall be licensed as above, without any additional terms or conditions. + diff --git a/build.rs b/build.rs index 60efa3a..5cf97ab 100644 --- a/build.rs +++ b/build.rs @@ -9,6 +9,8 @@ use std::process::Command; fn main() { let grammars = vec![ + "VisitorBasic", + "VisitorCalc", "CSV", "ReferenceToATN", "XMLLexer", @@ -16,7 +18,15 @@ fn main() { "Labels", "FHIRPath", ]; - let additional_args = vec![Some("-visitor"), None, None, None, None]; + let additional_args = vec![ + Some("-visitor"), + Some("-visitor"), + Some("-visitor"), + None, + None, + None, + None, + ]; let antlr_path = "/home/rrevenantt/dev/antlr4/tool/target/antlr4-4.8-2-SNAPSHOT-complete.jar"; for (grammar, arg) in grammars.into_iter().zip(additional_args) { diff --git a/grammars/VisitorBasic.g4 b/grammars/VisitorBasic.g4 new file mode 100644 index 0000000..b1e67b3 --- /dev/null +++ b/grammars/VisitorBasic.g4 @@ -0,0 +1,7 @@ +grammar VisitorBasic; + +s + : 'A' EOF + ; + +A : 'A'; diff --git a/grammars/VisitorCalc.g4 b/grammars/VisitorCalc.g4 new file mode 100644 index 0000000..6ca1c2e --- /dev/null +++ b/grammars/VisitorCalc.g4 @@ -0,0 +1,18 @@ +grammar VisitorCalc; + +s + : expr EOF + ; + +expr + : INT # number + | expr (MUL | DIV) expr # multiply + | expr (ADD | SUB) expr # add + ; + +INT : [0-9]+; +MUL : '*'; +DIV : '/'; +ADD : '+'; +SUB : '-'; +WS : [ \t]+ -> channel(HIDDEN); diff --git a/rustfmt.toml b/rustfmt.toml index 67806f7..d3fc0f1 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -1,3 +1,3 @@ edition = "2018" -fn_single_line = true -fn_params_layout = "Compressed" \ No newline at end of file +#fn_single_line = true +#fn_args_layout = "Compressed" \ No newline at end of file diff --git a/src/atn.rs b/src/atn.rs index be9b25c..30c8c56 100644 --- a/src/atn.rs +++ b/src/atn.rs @@ -55,7 +55,7 @@ impl Debug for ATN { } impl ATN { - crate fn new_atn(grammar_type: ATNType, max_token_type: isize) -> ATN { + pub(crate) fn new_atn(grammar_type: ATNType, max_token_type: isize) -> ATN { ATN { decision_to_state: Vec::new(), grammar_type, @@ -93,7 +93,7 @@ impl ATN { analyzer.look::(s, None, _ctx) } - crate fn add_state(&mut self, state: Box) { + pub(crate) fn add_state(&mut self, state: Box) { debug_assert_eq!(state.get_state_number(), self.states.len()); self.states.push(state) } diff --git a/src/atn_config.rs b/src/atn_config.rs index f7d7d83..c3b25f7 100644 --- a/src/atn_config.rs +++ b/src/atn_config.rs @@ -32,8 +32,8 @@ impl PartialEq for ATNConfig { fn eq(&self, other: &Self) -> bool { self.get_state() == other.get_state() && self.get_alt() == other.get_alt() - && (Arc::ptr_eq(self.get_context().unwrap(), other.get_context().unwrap()) - || self.get_context() == other.get_context()) + // Arc is optimized to not do a deep equalitiy if arc pointers are equal so that's enough + && self.context == other.context && self.get_type() == other.get_type() && self.semantic_context == other.semantic_context && self.precedence_filter_suppressed == other.precedence_filter_suppressed @@ -202,25 +202,41 @@ impl ATNConfig { new } - pub fn get_state(&self) -> ATNStateRef { self.state } + pub fn get_state(&self) -> ATNStateRef { + self.state + } - pub fn get_alt(&self) -> isize { self.alt } + pub fn get_alt(&self) -> isize { + self.alt + } - pub(crate) fn get_type(&self) -> &ATNConfigType { &self.config_type } + pub(crate) fn get_type(&self) -> &ATNConfigType { + &self.config_type + } - pub fn get_context(&self) -> Option<&Arc> { self.context.as_ref() } + pub fn get_context(&self) -> Option<&Arc> { + self.context.as_ref() + } - pub fn take_context(&mut self) -> Arc { self.context.take().unwrap() } + pub fn take_context(&mut self) -> Arc { + self.context.take().unwrap() + } - pub fn set_context(&mut self, _v: Arc) { self.context = Some(_v); } + pub fn set_context(&mut self, _v: Arc) { + self.context = Some(_v); + } - pub fn get_reaches_into_outer_context(&self) -> isize { self.reaches_into_outer_context } + pub fn get_reaches_into_outer_context(&self) -> isize { + self.reaches_into_outer_context + } pub fn set_reaches_into_outer_context(&mut self, _v: isize) { self.reaches_into_outer_context = _v } - pub fn is_precedence_filter_suppressed(&self) -> bool { self.precedence_filter_suppressed } + pub fn is_precedence_filter_suppressed(&self) -> bool { + self.precedence_filter_suppressed + } pub fn set_precedence_filter_suppressed(&mut self, _v: bool) { self.precedence_filter_suppressed = _v; diff --git a/src/atn_config_set.rs b/src/atn_config_set.rs index c44bcc0..d4eea7c 100644 --- a/src/atn_config_set.rs +++ b/src/atn_config_set.rs @@ -86,7 +86,9 @@ impl PartialEq for ATNConfigSet { impl Eq for ATNConfigSet {} impl Hash for ATNConfigSet { - fn hash(&self, state: &mut H) { self.configs.hash(state) } + fn hash(&self, state: &mut H) { + self.configs.hash(state) + } } impl ATNConfigSet { @@ -113,7 +115,9 @@ impl ATNConfigSet { a } - fn full_hash_key(config: &ATNConfig) -> Key { Key::Full(config.clone()) } + fn full_hash_key(config: &ATNConfig) -> Key { + Key::Full(config.clone()) + } fn local_hash_key(config: &ATNConfig) -> Key { let mut hasher = MurmurHasher::default(); @@ -176,7 +180,9 @@ impl ATNConfigSet { true } - pub fn add(&mut self, config: Box) -> bool { self.add_cached(config, None) } + pub fn add(&mut self, config: Box) -> bool { + self.add_cached(config, None) + } pub fn get_items(&self) -> impl Iterator { self.configs.iter().map(|c| c.as_ref()) @@ -197,19 +203,33 @@ impl ATNConfigSet { } } - pub fn length(&self) -> usize { self.configs.len() } + pub fn length(&self) -> usize { + self.configs.len() + } - pub fn is_empty(&self) -> bool { self.configs.is_empty() } + pub fn is_empty(&self) -> bool { + self.configs.is_empty() + } - pub fn has_semantic_context(&self) -> bool { self.has_semantic_context } + pub fn has_semantic_context(&self) -> bool { + self.has_semantic_context + } - pub fn set_has_semantic_context(&mut self, _v: bool) { self.has_semantic_context = _v; } + pub fn set_has_semantic_context(&mut self, _v: bool) { + self.has_semantic_context = _v; + } - pub fn read_only(&self) -> bool { self.read_only } + pub fn read_only(&self) -> bool { + self.read_only + } - pub fn set_read_only(&mut self, _read_only: bool) { self.read_only = _read_only; } + pub fn set_read_only(&mut self, _read_only: bool) { + self.read_only = _read_only; + } - pub fn full_context(&self) -> bool { self.full_ctx } + pub fn full_context(&self) -> bool { + self.full_ctx + } //duplicate of the self.conflicting_alts??? pub fn get_alts(&self) -> BitSet { @@ -219,11 +239,19 @@ impl ATNConfigSet { }) } - pub fn get_unique_alt(&self) -> isize { self.unique_alt } + pub fn get_unique_alt(&self) -> isize { + self.unique_alt + } - pub fn set_unique_alt(&mut self, _v: isize) { self.unique_alt = _v } + pub fn set_unique_alt(&mut self, _v: isize) { + self.unique_alt = _v + } - pub fn get_dips_into_outer_context(&self) -> bool { self.dips_into_outer_context } + pub fn get_dips_into_outer_context(&self) -> bool { + self.dips_into_outer_context + } - pub fn set_dips_into_outer_context(&mut self, _v: bool) { self.dips_into_outer_context = _v } + pub fn set_dips_into_outer_context(&mut self, _v: bool) { + self.dips_into_outer_context = _v + } } diff --git a/src/atn_deserialization_options.rs b/src/atn_deserialization_options.rs index 783380e..8031d3a 100644 --- a/src/atn_deserialization_options.rs +++ b/src/atn_deserialization_options.rs @@ -6,7 +6,9 @@ pub struct ATNDeserializationOptions { } impl ATNDeserializationOptions { - pub fn is_verify(&self) -> bool { self.verify_atn } + pub fn is_verify(&self) -> bool { + self.verify_atn + } } impl Default for ATNDeserializationOptions { diff --git a/src/atn_simulator.rs b/src/atn_simulator.rs index 87d79ad..5f888f2 100644 --- a/src/atn_simulator.rs +++ b/src/atn_simulator.rs @@ -40,9 +40,15 @@ impl BaseATNSimulator { } impl IATNSimulator for BaseATNSimulator { - fn shared_context_cache(&self) -> &PredictionContextCache { self.shared_context_cache.deref() } + fn shared_context_cache(&self) -> &PredictionContextCache { + self.shared_context_cache.deref() + } - fn atn(&self) -> &ATN { self.atn.as_ref() } + fn atn(&self) -> &ATN { + self.atn.as_ref() + } - fn decision_to_dfa(&self) -> &Vec> { self.decision_to_dfa.as_ref() } + fn decision_to_dfa(&self) -> &Vec> { + self.decision_to_dfa.as_ref() + } } diff --git a/src/atn_state.rs b/src/atn_state.rs index 1c2a93e..10ce836 100644 --- a/src/atn_state.rs +++ b/src/atn_state.rs @@ -122,28 +122,48 @@ impl BaseATNState { } impl ATNState for BaseATNState { - fn has_epsilon_only_transitions(&self) -> bool { self.epsilon_only_transitions } - fn get_rule_index(&self) -> usize { self.rule_index } + fn has_epsilon_only_transitions(&self) -> bool { + self.epsilon_only_transitions + } + fn get_rule_index(&self) -> usize { + self.rule_index + } - fn set_rule_index(&self, _v: usize) { unimplemented!() } + fn set_rule_index(&self, _v: usize) { + unimplemented!() + } fn get_next_tokens_within_rule(&self) -> &OnceCell { &self.next_tokens_within_rule } - fn get_state_type(&self) -> &ATNStateType { &self.state_type } + fn get_state_type(&self) -> &ATNStateType { + &self.state_type + } - fn get_state_type_mut(&mut self) -> &mut ATNStateType { &mut self.state_type } + fn get_state_type_mut(&mut self) -> &mut ATNStateType { + &mut self.state_type + } - fn get_state_type_id(&self) -> isize { self.state_type_id } + fn get_state_type_id(&self) -> isize { + self.state_type_id + } - fn get_state_number(&self) -> usize { self.state_number } + fn get_state_number(&self) -> usize { + self.state_number + } - fn set_state_number(&self, _state_number: isize) { unimplemented!() } + fn set_state_number(&self, _state_number: isize) { + unimplemented!() + } - fn get_transitions(&self) -> &Vec> { &self.transitions } + fn get_transitions(&self) -> &Vec> { + &self.transitions + } - fn set_transitions(&self, _t: Vec>) { unimplemented!() } + fn set_transitions(&self, _t: Vec>) { + unimplemented!() + } fn add_transition(&mut self, trans: Box) { if self.transitions.is_empty() { diff --git a/src/char_stream.rs b/src/char_stream.rs index e19ef53..d93fc38 100644 --- a/src/char_stream.rs +++ b/src/char_stream.rs @@ -72,7 +72,9 @@ where } #[inline] - fn len(&self) -> usize { self.len() } + fn len(&self) -> usize { + self.len() + } #[inline] fn from_text(text: &str) -> Self::Owned { @@ -143,13 +145,19 @@ impl InputData for str { } #[inline] - fn len(&self) -> usize { self.len() } + fn len(&self) -> usize { + self.len() + } - fn from_text(text: &str) -> Self::Owned { text.to_owned() } + fn from_text(text: &str) -> Self::Owned { + text.to_owned() + } // #[inline] // fn from_text(text: &str) -> Self::Owned { text.to_owned() } #[inline] - fn to_display(&self) -> String { self.to_string() } + fn to_display(&self) -> String { + self.to_string() + } } diff --git a/src/common_token_stream.rs b/src/common_token_stream.rs index 545fee4..a56ddc7 100644 --- a/src/common_token_stream.rs +++ b/src/common_token_stream.rs @@ -9,12 +9,14 @@ use crate::token_stream::{TokenStream, UnbufferedTokenStream}; use better_any::{Tid, TidAble}; /// Default token stream that skips token that not correspond to current channel. -#[derive(Tid, Debug)] +#[derive(Debug)] pub struct CommonTokenStream<'input, T: TokenSource<'input>> { base: UnbufferedTokenStream<'input, T>, channel: isize, } +better_any::tid! { impl<'input,T> TidAble<'input> for CommonTokenStream<'input, T> where T: TokenSource<'input>} + impl<'input, T: TokenSource<'input>> IntStream for CommonTokenStream<'input, T> { #[inline] fn consume(&mut self) { @@ -34,21 +36,31 @@ impl<'input, T: TokenSource<'input>> IntStream for CommonTokenStream<'input, T> } #[inline(always)] - fn mark(&mut self) -> isize { 0 } + fn mark(&mut self) -> isize { + 0 + } #[inline(always)] fn release(&mut self, _marker: isize) {} #[inline(always)] - fn index(&self) -> isize { self.base.index() } + fn index(&self) -> isize { + self.base.index() + } #[inline(always)] - fn seek(&mut self, index: isize) { self.base.seek(index); } + fn seek(&mut self, index: isize) { + self.base.seek(index); + } #[inline(always)] - fn size(&self) -> isize { self.base.size() } + fn size(&self) -> isize { + self.base.size() + } - fn get_source_name(&self) -> String { self.base.get_source_name() } + fn get_source_name(&self) -> String { + self.base.get_source_name() + } } impl<'input, T: TokenSource<'input>> TokenStream<'input> for CommonTokenStream<'input, T> { @@ -69,7 +81,9 @@ impl<'input, T: TokenSource<'input>> TokenStream<'input> for CommonTokenStream<' } #[inline] - fn get(&self, index: isize) -> &>::Tok { self.base.get(index) } + fn get(&self, index: isize) -> &>::Tok { + self.base.get(index) + } fn get_token_source(&self) -> &dyn TokenSource<'input, TF = Self::TF> { self.base.get_token_source() @@ -118,7 +132,9 @@ impl<'input, T: TokenSource<'input>> CommonTokenStream<'input, T> { } /// Creates iterator over this token stream - pub fn iter(&mut self) -> IterWrapper<'_, Self> { IterWrapper(self) } + pub fn iter(&mut self) -> IterWrapper<'_, Self> { + IterWrapper(self) + } fn sync(&mut self, i: isize) -> bool { let need = i - self.size() + 1; diff --git a/src/dfa.rs b/src/dfa.rs index 87f06cd..79b1dcc 100644 --- a/src/dfa.rs +++ b/src/dfa.rs @@ -12,8 +12,12 @@ use crate::vocabulary::Vocabulary; ///Helper trait for scope management and temporary values not living long enough pub(crate) trait ScopeExt: Sized { - fn convert_with T>(self, f: F) -> T { f(self) } - fn run T>(&self, f: F) -> T { f(self) } + fn convert_with T>(self, f: F) -> T { + f(self) + } + fn run T>(&self, f: F) -> T { + f(self) + } //apply fn modify_with(mut self, f: F) -> Self { @@ -128,7 +132,9 @@ impl DFA { } } - pub fn is_precedence_dfa(&self) -> bool { self.is_precedence_dfa } + pub fn is_precedence_dfa(&self) -> bool { + self.is_precedence_dfa + } pub fn set_precedence_dfa(&mut self, precedence_dfa: bool) { self.is_precedence_dfa = precedence_dfa diff --git a/src/dfa_state.rs b/src/dfa_state.rs index 8935d90..21b1560 100644 --- a/src/dfa_state.rs +++ b/src/dfa_state.rs @@ -40,11 +40,15 @@ pub struct DFAState { } impl PartialEq for DFAState { - fn eq(&self, other: &Self) -> bool { self.configs == other.configs } + fn eq(&self, other: &Self) -> bool { + self.configs == other.configs + } } impl Hash for DFAState { - fn hash(&self, state: &mut H) { self.configs.hash(state); } + fn hash(&self, state: &mut H) { + self.configs.hash(state); + } } impl DFAState { diff --git a/src/error_listener.rs b/src/error_listener.rs index 679b25d..f214079 100644 --- a/src/error_listener.rs +++ b/src/error_listener.rs @@ -202,7 +202,9 @@ pub struct DiagnosticErrorListener { impl DiagnosticErrorListener { /// When `exact_only` is true, only exactly known ambiguities are reported. - pub fn new(exact_only: bool) -> Self { Self { exact_only } } + pub fn new(exact_only: bool) -> Self { + Self { exact_only } + } fn get_decision_description<'a, T: Parser<'a>>(&self, recog: &T, dfa: &DFA) -> String { let decision = dfa.decision; diff --git a/src/error_strategy.rs b/src/error_strategy.rs index ab3449a..04025ad 100644 --- a/src/error_strategy.rs +++ b/src/error_strategy.rs @@ -20,7 +20,7 @@ use crate::token_factory::TokenFactory; use crate::transition::RuleTransition; use crate::tree::Tree; use crate::utils::escape_whitespaces; -use better_any::{impl_tid, Tid, TidAble}; +use better_any::{Tid, TidAble}; /// The interface for defining strategies to deal with syntax errors encountered /// during a parse by ANTLR-generated parsers. We distinguish between three @@ -94,12 +94,14 @@ pub trait ErrorStrategy<'a, T: Parser<'a>>: Tid<'a> { // /// Supports downcasting. // pub type DynHandler<'a, T> = Box + 'a>; -#[impl_tid] -impl<'a, T: Parser<'a> + TidAble<'a>> TidAble<'a> for Box + 'a> {} +// impl<'a, T: Parser<'a> + TidAble<'a>> TidAble<'a> for Box + 'a> {} +better_any::tid! { impl<'a, T> TidAble<'a> for Box + 'a> where T: Parser<'a>} impl<'a, T: Parser<'a> + TidAble<'a>> ErrorStrategy<'a, T> for Box + 'a> { #[inline(always)] - fn reset(&mut self, recognizer: &mut T) { self.deref_mut().reset(recognizer) } + fn reset(&mut self, recognizer: &mut T) { + self.deref_mut().reset(recognizer) + } #[inline(always)] fn recover_inline( @@ -130,12 +132,14 @@ impl<'a, T: Parser<'a> + TidAble<'a>> ErrorStrategy<'a, T> for Box> { error_recovery_mode: bool, last_error_index: isize, @@ -144,8 +148,12 @@ pub struct DefaultErrorStrategy<'input, Ctx: ParserNodeType<'input>> { next_tokens_ctx: Option>, } +better_any::tid! { impl<'i,Ctx> TidAble<'i> for DefaultErrorStrategy<'i,Ctx> where Ctx: ParserNodeType<'i>} + impl<'input, Ctx: ParserNodeType<'input>> Default for DefaultErrorStrategy<'input, Ctx> { - fn default() -> Self { Self::new() } + fn default() -> Self { + Self::new() + } } impl<'input, Ctx: ParserNodeType<'input>> DefaultErrorStrategy<'input, Ctx> { @@ -391,7 +399,9 @@ impl<'input, Ctx: ParserNodeType<'input>> DefaultErrorStrategy<'input, Ctx> { } impl<'a, T: Parser<'a>> ErrorStrategy<'a, T> for DefaultErrorStrategy<'a, T::Node> { - fn reset(&mut self, recognizer: &mut T) { self.end_error_condition(recognizer) } + fn reset(&mut self, recognizer: &mut T) { + self.end_error_condition(recognizer) + } fn recover_inline( &mut self, @@ -494,7 +504,9 @@ impl<'a, T: Parser<'a>> ErrorStrategy<'a, T> for DefaultErrorStrategy<'a, T::Nod Ok(()) } - fn in_error_recovery_mode(&mut self, _recognizer: &mut T) -> bool { self.error_recovery_mode } + fn in_error_recovery_mode(&mut self, _recognizer: &mut T) -> bool { + self.error_recovery_mode + } fn report_error(&mut self, recognizer: &mut T, e: &ANTLRError) { if self.in_error_recovery_mode(recognizer) { @@ -545,14 +557,18 @@ impl<'a, T: Parser<'a>> ErrorStrategy<'a, T> for DefaultErrorStrategy<'a, T::Nod /// /// [`ParserRuleContext.exception`]: todo /// */ -#[derive(Default, Debug, Tid)] +#[derive(Default, Debug)] pub struct BailErrorStrategy<'input, Ctx: ParserNodeType<'input>>( DefaultErrorStrategy<'input, Ctx>, ); +better_any::tid! {impl<'i,Ctx> TidAble<'i> for BailErrorStrategy<'i,Ctx> where Ctx:ParserNodeType<'i> } + impl<'input, Ctx: ParserNodeType<'input>> BailErrorStrategy<'input, Ctx> { /// Creates new instance of `BailErrorStrategy` - pub fn new() -> Self { Self(DefaultErrorStrategy::new()) } + pub fn new() -> Self { + Self(DefaultErrorStrategy::new()) + } fn process_error>( &self, @@ -560,12 +576,13 @@ impl<'input, Ctx: ParserNodeType<'input>> BailErrorStrategy<'input, Ctx> { e: &ANTLRError, ) -> ANTLRError { let mut ctx = recognizer.get_parser_rule_context().clone(); - let _: Option<()> = try { + let _: Option<()> = (|| { loop { ctx.set_exception(e.clone()); ctx = ctx.get_parent()? } - }; + Some(()) + })(); return ANTLRError::FallThrough(Rc::new(ParseCancelledError(e.clone()))); } } @@ -575,7 +592,9 @@ impl<'input, Ctx: ParserNodeType<'input>> BailErrorStrategy<'input, Ctx> { pub struct ParseCancelledError(ANTLRError); impl Error for ParseCancelledError { - fn source(&self) -> Option<&(dyn Error + 'static)> { Some(&self.0) } + fn source(&self) -> Option<&(dyn Error + 'static)> { + Some(&self.0) + } } impl Display for ParseCancelledError { @@ -587,7 +606,9 @@ impl Display for ParseCancelledError { impl<'a, T: Parser<'a>> ErrorStrategy<'a, T> for BailErrorStrategy<'a, T::Node> { #[inline(always)] - fn reset(&mut self, recognizer: &mut T) { self.0.reset(recognizer) } + fn reset(&mut self, recognizer: &mut T) { + self.0.reset(recognizer) + } #[cold] fn recover_inline( diff --git a/src/errors.rs b/src/errors.rs index 725e5bb..2f1d3ae 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -77,7 +77,9 @@ pub enum ANTLRError { // } impl Display for ANTLRError { - fn fmt(&self, _f: &mut Formatter<'_>) -> fmt::Result { ::fmt(self, _f) } + fn fmt(&self, _f: &mut Formatter<'_>) -> fmt::Result { + ::fmt(self, _f) + } } impl Error for ANTLRError { diff --git a/src/input_stream.rs b/src/input_stream.rs index 4c17299..2373a9d 100644 --- a/src/input_stream.rs +++ b/src/input_stream.rs @@ -5,7 +5,7 @@ use crate::char_stream::{CharStream, InputData}; use crate::int_stream::IntStream; use std::ops::Deref; -use better_any::{impl_tid, TidAble}; +use better_any::TidAble; /// Default rust target input stream. /// @@ -21,20 +21,25 @@ pub struct InputStream { index: isize, } -#[impl_tid] -impl<'a, T: ?Sized + 'static> TidAble<'a> for InputStream<&'a T> {} - -#[impl_tid] -impl<'a, T: ?Sized + 'static> TidAble<'a> for InputStream> {} +// #[impl_tid] +// impl<'a, T: ?Sized + 'static> TidAble<'a> for InputStream> {} +// #[impl_tid] +// impl<'a, T: ?Sized + 'static> TidAble<'a> for InputStream<&'a T> {} +better_any::tid! {impl<'a, T: 'static> TidAble<'a> for InputStream<&'a T> where T: ?Sized} +better_any::tid! {impl<'a, T: 'static> TidAble<'a> for InputStream> where T: ?Sized} impl<'a, T: From<&'a str>> CharStream for InputStream<&'a str> { #[inline] - fn get_text(&self, start: isize, stop: isize) -> T { self.get_text_inner(start, stop).into() } + fn get_text(&self, start: isize, stop: isize) -> T { + self.get_text_inner(start, stop).into() + } } impl, D: ?Sized + InputData> CharStream for InputStream> { #[inline] - fn get_text(&self, start: isize, stop: isize) -> T { self.get_text_owned(start, stop).into() } + fn get_text(&self, start: isize, stop: isize) -> T { + self.get_text_owned(start, stop).into() + } } /// `InputStream` over byte slice pub type ByteStream<'a> = InputStream<&'a [u8]>; @@ -59,7 +64,9 @@ impl<'a, T> CharStream for InputStream<&'a [T]> where [T]: InputData, { - fn get_text(&self, a: isize, b: isize) -> String { self.get_text_inner(a, b).to_display() } + fn get_text(&self, a: isize, b: isize) -> String { + self.get_text_inner(a, b).to_display() + } } impl<'a, 'b, T> CharStream> for InputStream<&'a [T]> @@ -77,7 +84,9 @@ where [T]: InputData, { #[inline] - fn get_text(&self, a: isize, b: isize) -> &'a [T] { self.get_text_inner(a, b) } + fn get_text(&self, a: isize, b: isize) -> &'a [T] { + self.get_text_inner(a, b) + } } impl InputStream> { @@ -140,7 +149,9 @@ where { /// Resets input stream to start from the beginning of this slice #[inline] - pub fn reset(&mut self) { self.index = 0 } + pub fn reset(&mut self) { + self.index = 0 + } } impl<'a, Data: Deref> IntStream for InputStream @@ -180,21 +191,31 @@ where } #[inline] - fn mark(&mut self) -> isize { -1 } + fn mark(&mut self) -> isize { + -1 + } #[inline] fn release(&mut self, _marker: isize) {} #[inline] - fn index(&self) -> isize { self.index } + fn index(&self) -> isize { + self.index + } #[inline] - fn seek(&mut self, index: isize) { self.index = index } + fn seek(&mut self, index: isize) { + self.index = index + } #[inline] - fn size(&self) -> isize { self.data_raw.len() as isize } + fn size(&self) -> isize { + self.data_raw.len() as isize + } - fn get_source_name(&self) -> String { self.name.clone() } + fn get_source_name(&self) -> String { + self.name.clone() + } } #[cfg(test)] diff --git a/src/interval_set.rs b/src/interval_set.rs index 227ccd4..9707c1f 100644 --- a/src/interval_set.rs +++ b/src/interval_set.rs @@ -18,11 +18,15 @@ pub(crate) const INVALID: Interval = Interval { a: -1, b: -2 }; impl Interval { /* stop is not included! */ - fn new(a: isize, b: isize) -> Interval { Interval { a, b } } + fn new(a: isize, b: isize) -> Interval { + Interval { a, b } + } // fn contains(&self, _item: isize) -> bool { unimplemented!() } - fn length(&self) -> isize { self.b - self.a } + fn length(&self) -> isize { + self.b - self.a + } fn union(&self, another: &Interval) -> Interval { Interval { @@ -42,10 +46,14 @@ impl Interval { } /** Does self.a start after other.b? May or may not be disjoint */ - pub fn starts_after(&self, other: &Interval) -> bool { return self.a > other.a; } + pub fn starts_after(&self, other: &Interval) -> bool { + return self.a > other.a; + } /** Does self start completely after other? Disjoint */ - pub fn starts_after_disjoint(&self, other: &Interval) -> bool { return self.a > other.b; } + pub fn starts_after_disjoint(&self, other: &Interval) -> bool { + return self.a > other.b; + } /** Does self start after other? NonDisjoint */ pub fn starts_after_non_disjoint(&self, other: &Interval) -> bool { @@ -97,11 +105,17 @@ impl IntervalSet { } } - pub fn get_min(&self) -> Option { self.intervals.first().map(|x| x.a) } + pub fn get_min(&self) -> Option { + self.intervals.first().map(|x| x.a) + } - pub fn add_one(&mut self, _v: isize) { self.add_range(_v, _v) } + pub fn add_one(&mut self, _v: isize) { + self.add_range(_v, _v) + } - pub fn add_range(&mut self, l: isize, h: isize) { self.add_interval(Interval { a: l, b: h }) } + pub fn add_range(&mut self, l: isize, h: isize) { + self.add_interval(Interval { a: l, b: h }) + } pub fn add_interval(&mut self, added: Interval) { if added.length() < 0 { @@ -287,7 +301,9 @@ impl IntervalSet { // unimplemented!() // } // - pub fn to_index_string(&self) -> String { self.to_token_string(&DUMMY_VOCAB) } + pub fn to_index_string(&self) -> String { + self.to_token_string(&DUMMY_VOCAB) + } pub fn to_token_string(&self, vocabulary: &dyn Vocabulary) -> String { if self.intervals.is_empty() { diff --git a/src/lexer.rs b/src/lexer.rs index 098b8e6..88fefcd 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -102,7 +102,8 @@ pub struct BaseLexer< hit_eof: bool, /// Channel lexer is currently assigning tokens to pub channel: isize, - mode_stack: Vec, + /// stack of modes, which is used for pushMode,popMode lexer actions + pub mode_stack: Vec, /// Mode lexer is currently in pub mode: usize, /// Make it `Some` to override text for token that is currently being generated by lexer @@ -110,9 +111,9 @@ pub struct BaseLexer< } #[derive(Debug)] -crate struct LexerPosition { - crate line: Cell, - crate char_position_in_line: Cell, +pub(crate) struct LexerPosition { + pub(crate) line: Cell, + pub(crate) char_position_in_line: Cell, } impl<'input, T, Input, TF> Deref for BaseLexer<'input, T, Input, TF> @@ -123,7 +124,9 @@ where { type Target = T; - fn deref(&self) -> &Self::Target { &self.recog } + fn deref(&self) -> &Self::Target { + &self.recog + } } impl<'input, T, Input, TF> DerefMut for BaseLexer<'input, T, Input, TF> @@ -132,7 +135,9 @@ where Input: CharStream, TF: TokenFactory<'input>, { - fn deref_mut(&mut self) -> &mut Self::Target { &mut self.recog } + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.recog + } } impl<'input, T, Input, TF> Recognizer<'input> for BaseLexer<'input, T, Input, TF> @@ -178,8 +183,8 @@ pub use super::token::TOKEN_DEFAULT_CHANNEL as LEXER_DEFAULT_TOKEN_CHANNEL; #[doc(inline)] pub use super::token::TOKEN_HIDDEN_CHANNEL as LEXER_HIDDEN; -crate const LEXER_MIN_CHAR_VALUE: isize = 0x0000; -crate const LEXER_MAX_CHAR_VALUE: isize = 0x10FFFF; +pub(crate) const LEXER_MIN_CHAR_VALUE: isize = 0x0000; +pub(crate) const LEXER_MAX_CHAR_VALUE: isize = 0x10FFFF; impl<'input, T, Input, TF> BaseLexer<'input, T, Input, TF> where @@ -187,7 +192,9 @@ where Input: CharStream, TF: TokenFactory<'input>, { - fn emit_token(&mut self, token: TF::Tok) { self.token = Some(token); } + fn emit_token(&mut self, token: TF::Tok) { + self.token = Some(token); + } fn emit(&mut self) { >::before_emit(self); @@ -220,7 +227,9 @@ where } /// Current position in input stream - pub fn get_char_index(&self) -> isize { self.input.as_ref().unwrap().index() } + pub fn get_char_index(&self) -> isize { + self.input.as_ref().unwrap().index() + } /// Current token text pub fn get_text<'a>(&'a self) -> Cow<'a, TF::Data> @@ -242,7 +251,9 @@ where } /// Used from lexer actions to override text of the token that will be emitted next - pub fn set_text(&mut self, _text: ::Owned) { self.text = Some(_text); } + pub fn set_text(&mut self, _text: ::Owned) { + self.text = Some(_text); + } // fn get_all_tokens(&mut self) -> Vec { unimplemented!() } @@ -254,7 +265,9 @@ where } /// Remove and drop all error listeners - pub fn remove_error_listeners(&mut self) { self.error_listeners.borrow_mut().clear(); } + pub fn remove_error_listeners(&mut self) { + self.error_listeners.borrow_mut().clear(); + } /// Creates new lexer instance pub fn new_base_lexer( @@ -375,9 +388,13 @@ where self.token.take().unwrap() } - fn get_line(&self) -> isize { self.current_pos.line.get() } + fn get_line(&self) -> isize { + self.current_pos.line.get() + } - fn get_char_position_in_line(&self) -> isize { self.current_pos.char_position_in_line.get() } + fn get_char_position_in_line(&self) -> isize { + self.current_pos.char_position_in_line.get() + } fn get_input_stream(&mut self) -> Option<&mut dyn IntStream> { match &mut self.input { @@ -397,7 +414,9 @@ where // self.factory = f; // } - fn get_token_factory(&self) -> &'input TF { self.factory } + fn get_token_factory(&self) -> &'input TF { + self.factory + } } #[cold] @@ -440,9 +459,13 @@ where { type Input = Input; - fn input(&mut self) -> &mut Self::Input { self.input.as_mut().unwrap() } + fn input(&mut self) -> &mut Self::Input { + self.input.as_mut().unwrap() + } - fn set_channel(&mut self, v: isize) { self.channel = v; } + fn set_channel(&mut self, v: isize) { + self.channel = v; + } fn push_mode(&mut self, m: usize) { self.mode_stack.push(self.mode); @@ -456,15 +479,27 @@ where }) } - fn set_type(&mut self, t: isize) { self.token_type = t; } + fn set_type(&mut self, t: isize) { + self.token_type = t; + } - fn set_mode(&mut self, m: usize) { self.mode = m; } + fn set_mode(&mut self, m: usize) { + self.mode = m; + } - fn more(&mut self) { self.set_type(LEXER_MORE) } + fn more(&mut self) { + self.set_type(LEXER_MORE) + } - fn skip(&mut self) { self.set_type(LEXER_SKIP) } + fn skip(&mut self) { + self.set_type(LEXER_SKIP) + } - fn reset(&mut self) { unimplemented!() } + fn reset(&mut self) { + unimplemented!() + } - fn get_interpreter(&self) -> Option<&LexerATNSimulator> { self.interpreter.as_deref() } + fn get_interpreter(&self) -> Option<&LexerATNSimulator> { + self.interpreter.as_deref() + } } diff --git a/src/lexer_action_executor.rs b/src/lexer_action_executor.rs index 17a7d04..4bfaaa7 100644 --- a/src/lexer_action_executor.rs +++ b/src/lexer_action_executor.rs @@ -14,7 +14,9 @@ pub(crate) struct LexerActionExecutor { } impl Hash for LexerActionExecutor { - fn hash(&self, state: &mut H) { state.write_u64(self.cached_hash) } + fn hash(&self, state: &mut H) { + state.write_u64(self.cached_hash) + } } impl LexerActionExecutor { diff --git a/src/lexer_atn_simulator.rs b/src/lexer_atn_simulator.rs index 4394bc3..433324d 100644 --- a/src/lexer_atn_simulator.rs +++ b/src/lexer_atn_simulator.rs @@ -29,6 +29,7 @@ use crate::token::TOKEN_EOF; use crate::transition::{ ActionTransition, PredicateTransition, RuleTransition, Transition, TransitionType, }; +use crate::utils::cell_update; use parking_lot::{RwLock, RwLockUpgradableReadGuard, RwLockWriteGuard}; #[allow(missing_docs)] @@ -70,7 +71,9 @@ pub struct LexerATNSimulator { } impl ILexerATNSimulator for LexerATNSimulator { - fn reset(&mut self) { self.prev_accept.reset() } + fn reset(&mut self) { + self.prev_accept.reset() + } fn match_token<'input>( &mut self, @@ -101,23 +104,29 @@ impl ILexerATNSimulator for LexerATNSimulator { result } - fn get_char_position_in_line(&self) -> isize { self.current_pos.char_position_in_line.get() } + fn get_char_position_in_line(&self) -> isize { + self.current_pos.char_position_in_line.get() + } fn set_char_position_in_line(&mut self, column: isize) { self.current_pos.char_position_in_line.set(column) } - fn get_line(&self) -> isize { self.current_pos.line.get() } + fn get_line(&self) -> isize { + self.current_pos.line.get() + } - fn set_line(&mut self, line: isize) { self.current_pos.char_position_in_line.set(line) } + fn set_line(&mut self, line: isize) { + self.current_pos.char_position_in_line.set(line) + } fn consume(&self, _input: &mut T) { let ch = _input.la(1); if ch == '\n' as isize { - self.current_pos.line.update(|x| x + 1); + cell_update(&self.current_pos.line, |x| x + 1); self.current_pos.char_position_in_line.set(0); } else { - self.current_pos.char_position_in_line.update(|x| x + 1); + cell_update(&self.current_pos.char_position_in_line, |x| x + 1); } _input.consume(); } @@ -128,11 +137,17 @@ impl ILexerATNSimulator for LexerATNSimulator { } impl IATNSimulator for LexerATNSimulator { - fn shared_context_cache(&self) -> &PredictionContextCache { self.base.shared_context_cache() } + fn shared_context_cache(&self) -> &PredictionContextCache { + self.base.shared_context_cache() + } - fn atn(&self) -> &ATN { self.base.atn() } + fn atn(&self) -> &ATN { + self.base.atn() + } - fn decision_to_dfa(&self) -> &Vec> { self.base.decision_to_dfa() } + fn decision_to_dfa(&self) -> &Vec> { + self.base.decision_to_dfa() + } } #[allow(missing_docs)] @@ -697,10 +712,14 @@ impl LexerATNSimulator { } /// Returns current DFA that is currently used. - pub fn get_dfa(&self) -> &RwLock { &self.decision_to_dfa()[self.mode] } + pub fn get_dfa(&self) -> &RwLock { + &self.decision_to_dfa()[self.mode] + } /// Returns current DFA for particular lexer mode - pub fn get_dfa_for_mode(&self, mode: usize) -> &RwLock { &self.decision_to_dfa()[mode] } + pub fn get_dfa_for_mode(&self, mode: usize) -> &RwLock { + &self.decision_to_dfa()[mode] + } // fn get_token_name(&self, _tt: isize) -> String { unimplemented!() } diff --git a/src/lib.rs b/src/lib.rs index 976f326..18b2ee0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,20 +1,21 @@ #![crate_type = "lib"] -#![feature(try_blocks)] +// #![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(crate_visibility_modifier)] +// #![feature(raw)] +// #![feature(is_sorted)] +// #![feature(cell_update)] +// #![feature(get_mut_unchecked)] +// #![feature(specialization)] +// #![feature(coerce_unsized)] +// #![feature(associated_type_defaults)] +// #![feature(generic_associated_types)] +// #![feature(crate_visibility_modifier)] // #![feature(generic_associated_types)] #![warn(rust_2018_idioms)] #![warn(missing_docs)] // warn if there is missing docs #![warn(missing_debug_implementations)] #![warn(trivial_numeric_casts)] -#![allow(incomplete_features)] +// #![allow(incomplete_features)] //! # Antlr4 runtime //! @@ -93,7 +94,7 @@ pub use lazy_static::lazy_static; pub use parking_lot::RwLock; #[doc(hidden)] -pub use better_any::{impl_tid, type_id, Tid, TidAble, TidExt}; +pub use better_any::{tid, Tid, TidAble, TidExt}; #[doc(inline)] pub use error_strategy::{BailErrorStrategy, DefaultErrorStrategy, ErrorStrategy}; @@ -175,3 +176,69 @@ pub mod rule_context; pub mod vocabulary; //#[cfg(test)] // tests are either integration tests in "tests" foulder or unit tests in some modules + +use std::rc::Rc; +/// Stable workaround for CoerceUnsized +// #[doc(hidden)] +pub trait CoerceFrom { + fn coerce_rc(from: Rc) -> Rc; + fn coerce_box(from: Box) -> Box; + fn coerce_ref(from: &T) -> &Self; + fn coerce_mut(from: &mut T) -> &mut Self; +} + +#[doc(hidden)] +#[macro_export] +macro_rules! coerce_from { + ($lt:lifetime : $p:path) => { + const _: () = { + use std::rc::Rc; + impl<$lt, T> $crate::CoerceFrom for dyn $p + $lt + where + T: $p + $lt, + { + fn coerce_rc(from: Rc) -> Rc { + from as _ + } + fn coerce_box(from: Box) -> Box { + from as _ + } + fn coerce_ref(from: &T) -> &Self { + from as _ + } + fn coerce_mut(from: &mut T) -> &mut Self { + from as _ + } + } + }; + }; +} + +/// Stable workaround for CoerceUnsized +// #[doc(hidden)] +pub trait CoerceTo { + fn coerce_rc_to(self: Rc) -> Rc; + fn coerce_box_to(self: Box) -> Box; + fn coerce_ref_to(self: &Self) -> &T; + fn coerce_mut_to(self: &mut Self) -> &mut T; +} + +impl CoerceTo for X +where + T: CoerceFrom, +{ + fn coerce_rc_to(self: Rc) -> Rc { + T::coerce_rc(self) + } + fn coerce_box_to(self: Box) -> Box { + T::coerce_box(self) + } + + fn coerce_ref_to(self: &Self) -> &T { + T::coerce_ref(self) + } + + fn coerce_mut_to(self: &mut Self) -> &mut T { + T::coerce_mut(self) + } +} diff --git a/src/ll1_analyzer.rs b/src/ll1_analyzer.rs index 44a4aa3..4766e9f 100644 --- a/src/ll1_analyzer.rs +++ b/src/ll1_analyzer.rs @@ -20,7 +20,9 @@ pub struct LL1Analyzer<'a> { } impl LL1Analyzer<'_> { - pub fn new(atn: &ATN) -> LL1Analyzer<'_> { LL1Analyzer { atn } } + pub fn new(atn: &ATN) -> LL1Analyzer<'_> { + LL1Analyzer { atn } + } // fn get_decision_lookahead(&self, _s: &dyn ATNState) -> &Vec { unimplemented!() } diff --git a/src/parser.rs b/src/parser.rs index e3c3caf..e5cf988 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -2,7 +2,7 @@ use std::borrow::Borrow; use std::cell::{Cell, RefCell}; use std::marker::PhantomData; -use std::ops::{CoerceUnsized, Deref, DerefMut}; +use std::ops::{Deref, DerefMut}; use std::rc::Rc; use std::sync::Arc; @@ -20,7 +20,9 @@ use crate::token::{Token, TOKEN_EOF}; use crate::token_factory::{TokenAware, TokenFactory}; use crate::token_stream::TokenStream; use crate::tree::{ErrorNode, Listenable, ParseTreeListener, TerminalNode}; +use crate::utils::cell_update; use crate::vocabulary::Vocabulary; +use crate::{CoerceFrom, CoerceTo}; use better_any::{Tid, TidAble}; /// parser functionality required for `ParserATNSimulator` to work @@ -104,10 +106,9 @@ pub trait ParserNodeType<'input>: TidAble<'input> + Sized { /// It is a member of generated parser struct, so /// almost always you don't need to create it yourself. /// Generated parser hides complexity of this struct and expose required flexibility via generic parameters -#[derive(Tid)] pub struct BaseParser< 'input, - Ext: 'static, //: ParserRecog<'input, Self> + 'static, // user provided behavior, such as semantic predicates + Ext, //: 'static, //: ParserRecog<'input, Self> + 'static, // user provided behavior, such as semantic predicates I: TokenStream<'input>, // input stream Ctx: ParserNodeType<'input, TF = I::TF>, // Ctx::Type is trait object type for tree node of the parser T: ParseTreeListener<'input, Ctx> + ?Sized = dyn ParseTreeListener<'input, Ctx>, @@ -150,9 +151,16 @@ pub struct BaseParser< pd: PhantomData &'input str>, } +better_any::tid! { + impl<'input, Ext, I, Ctx, T> TidAble<'input> for BaseParser<'input,Ext, I, Ctx, T> + where I: TokenStream<'input>, + Ctx: ParserNodeType<'input, TF = I::TF>, + T: ParseTreeListener<'input, Ctx> + ?Sized +} + impl<'input, Ext, I, Ctx, T> Deref for BaseParser<'input, Ext, I, Ctx, T> where - Ext: ParserRecog<'input, Self> + 'static, + Ext: ParserRecog<'input, Self>, I: TokenStream<'input>, Ctx: ParserNodeType<'input, TF = I::TF>, T: ParseTreeListener<'input, Ctx> + ?Sized, @@ -160,18 +168,22 @@ where { type Target = Ext; - fn deref(&self) -> &Self::Target { &self.ext } + fn deref(&self) -> &Self::Target { + &self.ext + } } impl<'input, Ext, I, Ctx, T> DerefMut for BaseParser<'input, Ext, I, Ctx, T> where - Ext: ParserRecog<'input, Self> + 'static, + Ext: ParserRecog<'input, Self>, I: TokenStream<'input>, Ctx: ParserNodeType<'input, TF = I::TF>, T: ParseTreeListener<'input, Ctx> + ?Sized, // Ctx::Type: Listenable, { - fn deref_mut(&mut self) -> &mut Self::Target { &mut self.ext } + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.ext + } } /// @@ -179,7 +191,7 @@ pub trait ParserRecog<'a, P: Recognizer<'a>>: Actions<'a, P> {} impl<'input, Ext, I, Ctx, T> Recognizer<'input> for BaseParser<'input, Ext, I, Ctx, T> where - Ext: ParserRecog<'input, Self> + 'static, + Ext: ParserRecog<'input, Self>, I: TokenStream<'input>, Ctx: ParserNodeType<'input, TF = I::TF>, T: ParseTreeListener<'input, Ctx> + ?Sized, @@ -196,18 +208,26 @@ where >::sempred(localctx, rule_index, action_index, self) } - fn get_rule_names(&self) -> &[&str] { self.ext.get_rule_names() } + fn get_rule_names(&self) -> &[&str] { + self.ext.get_rule_names() + } - fn get_vocabulary(&self) -> &dyn Vocabulary { self.ext.get_vocabulary() } + fn get_vocabulary(&self) -> &dyn Vocabulary { + self.ext.get_vocabulary() + } - fn get_grammar_file_name(&self) -> &str { self.ext.get_grammar_file_name() } + fn get_grammar_file_name(&self) -> &str { + self.ext.get_grammar_file_name() + } - fn get_atn(&self) -> &ATN { self.interp.atn() } + fn get_atn(&self) -> &ATN { + self.interp.atn() + } } impl<'input, Ext, I, Ctx, T> TokenAware<'input> for BaseParser<'input, Ext, I, Ctx, T> where - Ext: ParserRecog<'input, Self> + 'static, + Ext: ParserRecog<'input, Self>, I: TokenStream<'input>, Ctx: ParserNodeType<'input, TF = I::TF>, T: ParseTreeListener<'input, Ctx> + ?Sized, @@ -218,15 +238,18 @@ where impl<'input, Ext, I, Ctx, T> Parser<'input> for BaseParser<'input, Ext, I, Ctx, T> where - Ext: ParserRecog<'input, Self> + 'static, + Ext: ParserRecog<'input, Self>, I: TokenStream<'input>, Ctx: ParserNodeType<'input, TF = I::TF>, T: ParseTreeListener<'input, Ctx> + ?Sized, - Ctx::Type: Listenable, - Rc>: CoerceUnsized>, - Rc>: CoerceUnsized>, + Ctx::Type: + Listenable + CoerceFrom> + CoerceFrom>, + // TerminalNode<'input, Ctx>: CoerceTo, + // ErrorNode<'input, Ctx>: CoerceTo, { - fn get_interpreter(&self) -> &ParserATNSimulator { self.interp.as_ref() } + fn get_interpreter(&self) -> &ParserATNSimulator { + self.interp.as_ref() + } fn get_token_factory(&self) -> &'input Self::TF { // &**crate::common_token_factory::COMMON_TOKEN_FACTORY_DEFAULT @@ -234,7 +257,9 @@ where } #[inline(always)] - fn get_parser_rule_context(&self) -> &Rc { self.ctx.as_ref().unwrap() } + fn get_parser_rule_context(&self) -> &Rc { + self.ctx.as_ref().unwrap() + } fn consume(&mut self, err_handler: &mut impl ErrorStrategy<'input, Self>) { let o = self.get_current_token().clone(); @@ -248,7 +273,7 @@ where self.ctx .as_deref() .unwrap() - .add_child(node.clone() as Rc); + .add_child(node.clone().coerce_rc_to()); for listener in &mut self.parse_listeners { listener.visit_error_node(&*node) } @@ -257,7 +282,7 @@ where self.ctx .as_deref() .unwrap() - .add_child(node.clone() as Rc); + .add_child(node.clone().coerce_rc_to()); for listener in &mut self.parse_listeners { listener.visit_terminal(&*node) } @@ -275,7 +300,9 @@ where &mut self.input //.as_mut() } - fn get_input_stream(&self) -> &dyn TokenStream<'input, TF = Self::TF> { &self.input } + fn get_input_stream(&self) -> &dyn TokenStream<'input, TF = Self::TF> { + &self.input + } #[inline] fn get_current_token(&self) -> &>::Tok { @@ -293,7 +320,9 @@ where self.error_listeners.borrow_mut().push(listener) } - fn remove_error_listeners(&mut self) { self.error_listeners.borrow_mut().clear(); } + fn remove_error_listeners(&mut self) { + self.error_listeners.borrow_mut().clear(); + } fn notify_error_listeners( &self, @@ -301,7 +330,7 @@ where offending_token: Option, err: Option<&ANTLRError>, ) { - self._syntax_errors.update(|it| it + 1); + cell_update(&self._syntax_errors, |it| it + 1); let offending_token: Option<&_> = match offending_token { None => Some(self.get_current_token().borrow()), Some(x) => Some(self.input.get(x).borrow()), @@ -320,15 +349,23 @@ where }) } - fn is_expected_token(&self, _symbol: isize) -> bool { unimplemented!() } + fn is_expected_token(&self, _symbol: isize) -> bool { + unimplemented!() + } - fn get_precedence(&self) -> isize { *self.precedence_stack.last().unwrap_or(&-1) } + fn get_precedence(&self) -> isize { + *self.precedence_stack.last().unwrap_or(&-1) + } #[inline(always)] - fn get_state(&self) -> isize { self.state } + fn get_state(&self) -> isize { + self.state + } #[inline(always)] - fn set_state(&mut self, v: isize) { self.state = v; } + fn set_state(&mut self, v: isize) { + self.state = v; + } fn get_rule_invocation_stack(&self) -> Vec { let mut vec = Vec::new(); @@ -354,13 +391,14 @@ where #[allow(missing_docs)] // todo docs impl<'input, Ext, I, Ctx, T> BaseParser<'input, Ext, I, Ctx, T> where - Ext: ParserRecog<'input, Self> + 'static, + Ext: ParserRecog<'input, Self>, I: TokenStream<'input>, Ctx: ParserNodeType<'input, TF = I::TF>, T: ParseTreeListener<'input, Ctx> + ?Sized, - Ctx::Type: Listenable, - Rc>: CoerceUnsized>, - Rc>: CoerceUnsized>, + Ctx::Type: + Listenable + CoerceFrom> + CoerceFrom>, + // TerminalNode<'input, Ctx>: CoerceTo, + // ErrorNode<'input, Ctx>: CoerceTo, { pub fn new_base_parser(input: I, interpreter: Arc, ext: Ext) -> Self { Self { @@ -402,7 +440,7 @@ where self.ctx .as_ref() .unwrap() - .add_child(self.create_error_node(token.clone()) as Rc); + .add_child(self.create_error_node(token.clone()).coerce_rc_to()); } } return Ok(token); @@ -423,7 +461,7 @@ where self.ctx .as_ref() .unwrap() - .add_child(self.create_error_node(t.clone()) as Rc); + .add_child(self.create_error_node(t.clone()).coerce_rc_to()); } } return Ok(t); @@ -438,10 +476,10 @@ where /// todo pub fn add_parse_listener(&mut self, listener: Box) -> ListenerId where - Box: CoerceUnsized>, + L: CoerceTo, { let id = ListenerId::new(&listener); - self.parse_listeners.push(listener); + self.parse_listeners.push(listener.coerce_box_to()); id } @@ -449,7 +487,7 @@ where /// `listener_id` is returned when listener is added via `add_parse_listener` pub fn remove_parse_listener(&mut self, listener_id: ListenerId) -> Box where - Box: CoerceUnsized>, + L: CoerceTo, { let index = self .parse_listeners @@ -460,7 +498,9 @@ where } /// Removes all added parse listeners without returning them - pub fn remove_parse_listeners(&mut self) { self.parse_listeners.clear() } + pub fn remove_parse_listeners(&mut self) { + self.parse_listeners.clear() + } pub fn trigger_enter_rule_event(&mut self) { let ctx = self.ctx.as_deref().unwrap(); diff --git a/src/parser_atn_simulator.rs b/src/parser_atn_simulator.rs index 8b3699c..9043c48 100644 --- a/src/parser_atn_simulator.rs +++ b/src/parser_atn_simulator.rs @@ -99,8 +99,12 @@ struct Local<'a, 'input, T: Parser<'input>> { } impl<'a, 'input, T: Parser<'input> + 'a> Local<'a, 'input, T> { - fn dfa(&self) -> &DFA { self.dfa.as_deref().unwrap() } - fn dfa_mut(&mut self) -> &mut DFA { self.dfa_mut.as_mut().unwrap().deref_mut() } + fn dfa(&self) -> &DFA { + self.dfa.as_deref().unwrap() + } + fn dfa_mut(&mut self) -> &mut DFA { + self.dfa_mut.as_mut().unwrap().deref_mut() + } fn upgrade_lock(&mut self) { let lock = self.dfa.take().unwrap(); self.dfa_mut = Some(RwLockUpgradableReadGuard::upgrade(lock)); @@ -143,10 +147,14 @@ impl ParserATNSimulator { } /// Returns current prediction mode - pub fn get_prediction_mode(&self) -> PredictionMode { self.prediction_mode.get() } + pub fn get_prediction_mode(&self) -> PredictionMode { + self.prediction_mode.get() + } /// Sets current prediction mode - pub fn set_prediction_mode(&self, v: PredictionMode) { self.prediction_mode.set(v) } + pub fn set_prediction_mode(&self, v: PredictionMode) { + self.prediction_mode.set(v) + } // fn reset(&self) { unimplemented!() } @@ -1510,9 +1518,15 @@ impl ParserATNSimulator { } impl IATNSimulator for ParserATNSimulator { - fn shared_context_cache(&self) -> &PredictionContextCache { self.base.shared_context_cache() } + fn shared_context_cache(&self) -> &PredictionContextCache { + self.base.shared_context_cache() + } - fn atn(&self) -> &ATN { self.base.atn() } + fn atn(&self) -> &ATN { + self.base.atn() + } - fn decision_to_dfa(&self) -> &Vec> { self.base.decision_to_dfa() } + fn decision_to_dfa(&self) -> &Vec> { + self.base.decision_to_dfa() + } } diff --git a/src/parser_rule_context.rs b/src/parser_rule_context.rs index 4926ad8..2cb6aff 100644 --- a/src/parser_rule_context.rs +++ b/src/parser_rule_context.rs @@ -3,7 +3,7 @@ use std::any::{type_name, Any}; use std::borrow::{Borrow, BorrowMut}; use std::cell::{Ref, RefCell, RefMut}; use std::fmt::{Debug, Error, Formatter}; -use std::ops::{CoerceUnsized, Deref, DerefMut}; +use std::ops::{Deref, DerefMut}; use std::rc::Rc; use better_any::{Tid, TidAble, TidExt}; @@ -15,6 +15,7 @@ use crate::rule_context::{BaseRuleContext, CustomRuleContext, RuleContext}; use crate::token::Token; use crate::token_factory::TokenFactory; use crate::tree::{ParseTree, ParseTreeVisitor, TerminalNode, Tree, VisitableDyn}; +use crate::CoerceTo; /// Syntax tree node for particular parser rule. /// @@ -125,7 +126,7 @@ pub trait RuleContextExt<'input>: ParserRuleContext<'input> { where Z: ParserRuleContext<'input, Ctx = Self::Ctx, TF = Self::TF> + ?Sized + 'input, Self::Ctx: ParserNodeType<'input, Type = Z>, - Rc: CoerceUnsized>; + Self: CoerceTo; #[doc(hidden)] fn accept_children(&self, visitor: &mut V) @@ -139,10 +140,10 @@ impl<'input, T: ParserRuleContext<'input> + ?Sized + 'input> RuleContextExt<'inp where Z: ParserRuleContext<'input, Ctx = T::Ctx, TF = T::TF> + ?Sized + 'input, T::Ctx: ParserNodeType<'input, Type = Z>, - Rc: CoerceUnsized>, + T: CoerceTo, { let mut result = String::from("["); - let mut next: Option> = Some(self.clone() as Rc); + let mut next: Option> = Some(self.clone().coerce_rc_to()); while let Some(ref p) = next { if stop.is_some() && (stop.is_none() || Rc::ptr_eq(p, stop.as_ref().unwrap())) { break; @@ -207,7 +208,12 @@ 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_unchecked(ctx) as *mut T as *mut Result) } + unsafe { + let ptr = Rc::as_ptr(ctx) as *mut T as *mut Result; + + &mut *ptr + } } // workaround newtype for cycle in trait definition @@ -227,7 +233,6 @@ pub fn cast_mut<'a, T: ParserRuleContext<'a> + 'a + ?Sized, Result: 'a>( // pub type ParserRuleContextType<'input,T> = ParseTreeNode<'input,T>; /// Default rule context implementation that keeps everything provided by parser -#[derive(Tid)] pub struct BaseParserRuleContext<'input, Ctx: CustomRuleContext<'input>> { base: BaseRuleContext<'input, Ctx>, @@ -239,8 +244,10 @@ pub struct BaseParserRuleContext<'input, Ctx: CustomRuleContext<'input>> { pub(crate) children: RefCell>::Type>>>, } +better_any::tid! { impl<'i,Ctx> TidAble<'i> for BaseParserRuleContext<'i,Ctx> where Ctx:CustomRuleContext<'i> } + 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::()) } } @@ -248,9 +255,13 @@ impl<'input, Ctx: CustomRuleContext<'input>> Debug for BaseParserRuleContext<'in impl<'input, Ctx: CustomRuleContext<'input>> RuleContext<'input> for BaseParserRuleContext<'input, Ctx> { - fn get_invoking_state(&self) -> isize { self.base.get_invoking_state() } + fn get_invoking_state(&self) -> isize { + self.base.get_invoking_state() + } - fn set_invoking_state(&self, t: isize) { self.base.set_invoking_state(t) } + fn set_invoking_state(&self, t: isize) { + self.base.set_invoking_state(t) + } fn get_parent_ctx(&self) -> Option>::Type>> { self.base.get_parent_ctx() @@ -267,7 +278,9 @@ impl<'input, Ctx: CustomRuleContext<'input>> CustomRuleContext<'input> type TF = Ctx::TF; type Ctx = Ctx::Ctx; - fn get_rule_index(&self) -> usize { self.base.ext.get_rule_index() } + fn get_rule_index(&self) -> usize { + self.base.ext.get_rule_index() + } } // unsafe impl<'input, Ctx: CustomRuleContext<'input>> Tid for BaseParserRuleContext<'input, Ctx> { @@ -284,19 +297,27 @@ impl<'input, Ctx: CustomRuleContext<'input>> CustomRuleContext<'input> impl<'input, Ctx: CustomRuleContext<'input>> Deref for BaseParserRuleContext<'input, Ctx> { type Target = Ctx; - fn deref(&self) -> &Self::Target { &self.base.ext } + fn deref(&self) -> &Self::Target { + &self.base.ext + } } impl<'input, Ctx: CustomRuleContext<'input>> DerefMut for BaseParserRuleContext<'input, Ctx> { - fn deref_mut(&mut self) -> &mut Self::Target { &mut self.base.ext } + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.base.ext + } } impl<'input, Ctx: CustomRuleContext<'input>> Borrow for BaseParserRuleContext<'input, Ctx> { - fn borrow(&self) -> &Ctx { &self.base.ext } + fn borrow(&self) -> &Ctx { + &self.base.ext + } } impl<'input, Ctx: CustomRuleContext<'input>> BorrowMut for BaseParserRuleContext<'input, Ctx> { - fn borrow_mut(&mut self) -> &mut Ctx { &mut self.base.ext } + fn borrow_mut(&mut self) -> &mut Ctx { + &mut self.base.ext + } } impl<'input, Ctx: CustomRuleContext<'input> + TidAble<'input>> ParserRuleContext<'input> @@ -360,7 +381,9 @@ impl<'input, Ctx: CustomRuleContext<'input> + TidAble<'input>> ParserRuleContext self.children.borrow_mut().push(child); } - fn remove_last_child(&self) { self.children.borrow_mut().pop(); } + fn remove_last_child(&self) { + self.children.borrow_mut().pop(); + } // fn enter_rule(&self, listener: &mut dyn Any) { // Ctx::enter(self, listener) @@ -380,15 +403,21 @@ impl<'input, Ctx: CustomRuleContext<'input>> Tree<'input> for BaseParserRuleCont self.get_parent_ctx() } - fn has_parent(&self) -> bool { self.base.parent_ctx.borrow().is_some() } + fn has_parent(&self) -> bool { + self.base.parent_ctx.borrow().is_some() + } - fn get_payload(&self) -> Box { unimplemented!() } + fn get_payload(&self) -> Box { + unimplemented!() + } fn get_child(&self, i: usize) -> Option>::Type>> { self.children.borrow().get(i).cloned() } - fn get_child_count(&self) -> usize { self.children.borrow().len() } + fn get_child_count(&self) -> usize { + self.children.borrow().len() + } // fn get_children<'a>(&'a self) -> Box>::Type>> + 'a> where 'input:'a{ // let len = self.children.borrow().len(); @@ -411,7 +440,7 @@ impl<'input, Ctx: CustomRuleContext<'input> + TidAble<'input>> ParseTree<'input> } } - default fn get_text(&self) -> String { + fn get_text(&self) -> String { let children = self.get_children(); let mut result = String::new(); @@ -473,7 +502,9 @@ where T: DerefSeal + 'input + Debug + Tid<'input>, I: ParserRuleContext<'input> + 'input + ?Sized, { - fn set_exception(&self, e: ANTLRError) { self.deref().set_exception(e) } + fn set_exception(&self, e: ANTLRError) { + self.deref().set_exception(e) + } fn set_start(&self, t: Option<>::Tok>) { self.deref().set_start(t) @@ -515,7 +546,9 @@ where self.deref().add_child(child) } - fn remove_last_child(&self) { self.deref().remove_last_child() } + fn remove_last_child(&self) { + self.deref().remove_last_child() + } // fn enter_rule(&self, listener: &mut dyn Any) { self.deref().enter_rule(listener) } // @@ -529,11 +562,17 @@ where T: DerefSeal + 'input + Debug + Tid<'input>, I: ParserRuleContext<'input> + 'input + ?Sized, { - fn get_invoking_state(&self) -> isize { self.deref().get_invoking_state() } + fn get_invoking_state(&self) -> isize { + self.deref().get_invoking_state() + } - fn set_invoking_state(&self, t: isize) { self.deref().set_invoking_state(t) } + fn set_invoking_state(&self, t: isize) { + self.deref().set_invoking_state(t) + } - fn is_empty(&self) -> bool { self.deref().is_empty() } + fn is_empty(&self) -> bool { + self.deref().is_empty() + } fn get_parent_ctx(&self) -> Option>::Type>> { self.deref().get_parent_ctx() @@ -549,9 +588,13 @@ where T: DerefSeal + 'input + Debug + Tid<'input>, I: ParserRuleContext<'input> + 'input + ?Sized, { - fn get_source_interval(&self) -> Interval { self.deref().get_source_interval() } + fn get_source_interval(&self) -> Interval { + self.deref().get_source_interval() + } - fn get_text(&self) -> String { self.deref().get_text() } + fn get_text(&self) -> String { + self.deref().get_text() + } } impl<'input, T, I> Tree<'input> for T @@ -563,15 +606,21 @@ where self.deref().get_parent() } - fn has_parent(&self) -> bool { self.deref().has_parent() } + fn has_parent(&self) -> bool { + self.deref().has_parent() + } - fn get_payload(&self) -> Box { self.deref().get_payload() } + fn get_payload(&self) -> Box { + self.deref().get_payload() + } fn get_child(&self, i: usize) -> Option>::Type>> { self.deref().get_child(i) } - fn get_child_count(&self) -> usize { self.deref().get_child_count() } + fn get_child_count(&self) -> usize { + self.deref().get_child_count() + } fn get_children<'a>( &'a self, @@ -593,13 +642,19 @@ where type TF = I::TF; type Ctx = I::Ctx; - fn get_rule_index(&self) -> usize { self.deref().get_rule_index() } + fn get_rule_index(&self) -> usize { + self.deref().get_rule_index() + } // fn type_rule_index() -> usize where Self: Sized { unimplemented!() } - fn get_alt_number(&self) -> isize { self.deref().get_alt_number() } + fn get_alt_number(&self) -> isize { + self.deref().get_alt_number() + } - fn set_alt_number(&self, _alt_number: isize) { self.deref().set_alt_number(_alt_number) } + fn set_alt_number(&self, _alt_number: isize) { + self.deref().set_alt_number(_alt_number) + } } // diff --git a/src/prediction_context.rs b/src/prediction_context.rs index 246eecc..3083506 100644 --- a/src/prediction_context.rs +++ b/src/prediction_context.rs @@ -137,7 +137,9 @@ impl Display for PredictionContext { //} impl Hash for PredictionContext { - fn hash(&self, state: &mut H) { state.write_i32(self.hash_code()) } + fn hash(&self, state: &mut H) { + state.write_i32(self.hash_code()) + } } lazy_static! { @@ -385,7 +387,8 @@ impl PredictionContext { parents, return_states: vec![a.return_state, b.return_state], }; - if !result.return_states.is_sorted() { + // if !result.return_states.is_sorted() + if !result.return_states.windows(2).all(|x| x[0] <= x[1]) { result.parents.swap(0, 1); result.return_states.swap(0, 1); } @@ -572,7 +575,9 @@ pub struct MurmurHasherBuilder {} impl BuildHasher for MurmurHasherBuilder { type Hasher = MurmurHasher; - fn build_hasher(&self) -> Self::Hasher { MurmurHasher::default() } + fn build_hasher(&self) -> Self::Hasher { + MurmurHasher::default() + } } impl PredictionContextCache { @@ -647,5 +652,7 @@ impl PredictionContextCache { } #[doc(hidden)] - pub fn length(&self) -> usize { self.cache.read().unwrap().len() } + pub fn length(&self) -> usize { + self.cache.read().unwrap().len() + } } diff --git a/src/prediction_context/test.rs b/src/prediction_context/test.rs index 2be7152..7ca2df6 100644 --- a/src/prediction_context/test.rs +++ b/src/prediction_context/test.rs @@ -4,9 +4,13 @@ use std::collections::VecDeque; use super::*; -fn root_is_wildcard() -> bool { true } +fn root_is_wildcard() -> bool { + true +} -fn full_ctx() -> bool { false } +fn full_ctx() -> bool { + false +} #[test] fn test_e_e() { diff --git a/src/recognizer.rs b/src/recognizer.rs index c58cb5b..286a87a 100644 --- a/src/recognizer.rs +++ b/src/recognizer.rs @@ -46,12 +46,20 @@ pub trait Recognizer<'input>: TokenAware<'input> { /// Returns array of rule names. /// Used for debugging and error reporting - fn get_rule_names(&self) -> &[&str] { &[] } - fn get_vocabulary(&self) -> &dyn Vocabulary { unimplemented!() } + fn get_rule_names(&self) -> &[&str] { + &[] + } + fn get_vocabulary(&self) -> &dyn Vocabulary { + unimplemented!() + } /// Name of the file this recognizer was generated from - fn get_grammar_file_name(&self) -> &str { "" } - fn get_atn(&self) -> &ATN { unimplemented!() } + fn get_grammar_file_name(&self) -> &str { + "" + } + fn get_atn(&self) -> &ATN { + unimplemented!() + } } /// **! Usually generated by ANTLR !** @@ -78,12 +86,20 @@ pub trait Actions<'a, P: Recognizer<'a>> { /// Returns array of rule names. /// Used for debugging and error reporting - fn get_rule_names(&self) -> &[&str] { &[] } - fn get_vocabulary(&self) -> &dyn Vocabulary { unimplemented!() } + fn get_rule_names(&self) -> &[&str] { + &[] + } + fn get_vocabulary(&self) -> &dyn Vocabulary { + unimplemented!() + } /// Name of the file this recognizer was generated from - fn get_grammar_file_name(&self) -> &str { "" } - fn get_atn(&self) -> &ATN { unimplemented!() } + fn get_grammar_file_name(&self) -> &str { + "" + } + fn get_atn(&self) -> &ATN { + unimplemented!() + } } //impl Recognizer for BaseRecognizer { diff --git a/src/rule_context.rs b/src/rule_context.rs index a9ffa31..57be242 100644 --- a/src/rule_context.rs +++ b/src/rule_context.rs @@ -18,17 +18,23 @@ use std::any::type_name; /// Minimal rule context functionality required for parser to work properly pub trait RuleContext<'input>: CustomRuleContext<'input> { /// Internal parser state - fn get_invoking_state(&self) -> isize { -1 } + fn get_invoking_state(&self) -> isize { + -1 + } /// Sets internal parser state fn set_invoking_state(&self, _t: isize) {} /// A context is empty if there is no invoking state; meaning nobody called /// current context. Which is usually true for the root of the syntax tree - fn is_empty(&self) -> bool { self.get_invoking_state() == -1 } + fn is_empty(&self) -> bool { + self.get_invoking_state() == -1 + } /// Get parent context - fn get_parent_ctx(&self) -> Option>::Type>> { None } + fn get_parent_ctx(&self) -> Option>::Type>> { + None + } /// Set parent context fn set_parent(&self, _parent: &Option>::Type>>) {} @@ -59,17 +65,21 @@ where // Self: Sized; // } -#[derive(Tid, Debug)] +#[derive(Debug)] #[doc(hidden)] pub struct EmptyCustomRuleContext<'a, TF: TokenFactory<'a> + 'a>( pub(crate) PhantomData<&'a TF::Tok>, ); +better_any::tid! { impl <'a,TF> TidAble<'a> for EmptyCustomRuleContext<'a,TF> where TF:TokenFactory<'a> + 'a} + impl<'a, TF: TokenFactory<'a> + 'a> CustomRuleContext<'a> for EmptyCustomRuleContext<'a, TF> { type TF = TF; type Ctx = EmptyContextType<'a, TF>; - fn get_rule_index(&self) -> usize { usize::max_value() } + fn get_rule_index(&self) -> usize { + usize::max_value() + } } // unsafe impl<'a, TF: TokenFactory<'a> + 'a> Tid for EmptyCustomRuleContext<'a, TF> { @@ -88,10 +98,12 @@ impl<'a, TF: TokenFactory<'a> + 'a> CustomRuleContext<'a> for EmptyCustomRuleCon pub type EmptyContext<'a, TF> = dyn ParserRuleContext<'a, TF = TF, Ctx = EmptyContextType<'a, TF>> + 'a; -#[derive(Tid, Debug)] +#[derive(Debug)] #[doc(hidden)] // public for implementation reasons pub struct EmptyContextType<'a, TF: TokenFactory<'a>>(pub PhantomData<&'a TF>); +better_any::tid! { impl <'a,TF> TidAble<'a> for EmptyContextType<'a,TF> where TF:TokenFactory<'a> } + impl<'a, TF: TokenFactory<'a>> ParserNodeType<'a> for EmptyContextType<'a, TF> { type TF = TF; type Type = dyn ParserRuleContext<'a, TF = Self::TF, Ctx = Self> + 'a; @@ -108,20 +120,35 @@ pub trait CustomRuleContext<'input> { /// Rule index that corresponds to this context type fn get_rule_index(&self) -> usize; - fn get_alt_number(&self) -> isize { INVALID_ALT } + fn get_alt_number(&self) -> isize { + INVALID_ALT + } fn set_alt_number(&self, _alt_number: isize) {} + + /// 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 { + 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(); + } // fn enter(_ctx: &dyn Tree<'input, Node=Self>, _listener: &mut dyn Any) where Self: Sized {} // fn exit(_ctx: &dyn Tree<'input, Node=Self>, _listener: &mut dyn Any) where Self: Sized {} } /// Minimal parse tree node implementation, that stores only data required for correct parsing -#[derive(Tid)] pub struct BaseRuleContext<'input, ExtCtx: CustomRuleContext<'input>> { pub(crate) parent_ctx: RefCell>::Type>>>, invoking_state: Cell, pub(crate) ext: ExtCtx, } +better_any::tid! { impl <'input,Ctx> TidAble<'input> for BaseRuleContext<'input,Ctx> where Ctx:CustomRuleContext<'input>} + #[allow(missing_docs)] impl<'input, ExtCtx: CustomRuleContext<'input>> BaseRuleContext<'input, ExtCtx> { pub fn new_parser_ctx( @@ -145,11 +172,15 @@ impl<'input, ExtCtx: CustomRuleContext<'input>> BaseRuleContext<'input, ExtCtx> } impl<'input, Ctx: CustomRuleContext<'input>> Borrow for BaseRuleContext<'input, Ctx> { - fn borrow(&self) -> &Ctx { &self.ext } + fn borrow(&self) -> &Ctx { + &self.ext + } } impl<'input, Ctx: CustomRuleContext<'input>> BorrowMut for BaseRuleContext<'input, Ctx> { - fn borrow_mut(&mut self) -> &mut Ctx { &mut self.ext } + fn borrow_mut(&mut self) -> &mut Ctx { + &mut self.ext + } } impl<'input, ExtCtx: CustomRuleContext<'input>> CustomRuleContext<'input> @@ -158,7 +189,9 @@ impl<'input, ExtCtx: CustomRuleContext<'input>> CustomRuleContext<'input> type TF = ExtCtx::TF; type Ctx = ExtCtx::Ctx; - fn get_rule_index(&self) -> usize { self.ext.get_rule_index() } + fn get_rule_index(&self) -> usize { + self.ext.get_rule_index() + } } // unsafe impl<'input, Ctx: CustomRuleContext<'input>> Tid for BaseRuleContext<'input, Ctx> { @@ -175,9 +208,13 @@ impl<'input, ExtCtx: CustomRuleContext<'input>> CustomRuleContext<'input> impl<'input, ExtCtx: CustomRuleContext<'input>> RuleContext<'input> for BaseRuleContext<'input, ExtCtx> { - fn get_invoking_state(&self) -> isize { self.invoking_state.get() } + fn get_invoking_state(&self) -> isize { + self.invoking_state.get() + } - fn set_invoking_state(&self, t: isize) { self.invoking_state.set(t) } + fn set_invoking_state(&self, t: isize) { + self.invoking_state.set(t) + } fn get_parent_ctx(&self) -> Option>::Type>> { self.parent_ctx diff --git a/src/token.rs b/src/token.rs index a56e8cc..f11e7ad 100644 --- a/src/token.rs +++ b/src/token.rs @@ -35,16 +35,28 @@ pub trait Token: Debug + Display { type Data: ?Sized + InputData; // fn get_source(&self) -> Option<(Box, Box)>; fn get_token_type(&self) -> isize; - fn get_channel(&self) -> isize { TOKEN_DEFAULT_CHANNEL } - fn get_start(&self) -> isize { 0 } - fn get_stop(&self) -> isize { 0 } - fn get_line(&self) -> isize { 0 } - fn get_column(&self) -> isize { 0 } + fn get_channel(&self) -> isize { + TOKEN_DEFAULT_CHANNEL + } + fn get_start(&self) -> isize { + 0 + } + fn get_stop(&self) -> isize { + 0 + } + fn get_line(&self) -> isize { + 0 + } + fn get_column(&self) -> isize { + 0 + } fn get_text(&self) -> &Self::Data; fn set_text(&mut self, _text: ::Owned) {} - fn get_token_index(&self) -> isize { 0 } + fn get_token_index(&self) -> isize { + 0 + } fn set_token_index(&self, _v: isize) {} // fn get_token_source(&self) -> &dyn TokenSource; @@ -143,17 +155,29 @@ impl + Debug> Display for GenericToken { impl + Debug> Token for GenericToken { type Data = str; - fn get_token_type(&self) -> isize { self.token_type } + fn get_token_type(&self) -> isize { + self.token_type + } - fn get_channel(&self) -> isize { self.channel } + fn get_channel(&self) -> isize { + self.channel + } - fn get_start(&self) -> isize { self.start } + fn get_start(&self) -> isize { + self.start + } - fn get_stop(&self) -> isize { self.stop } + fn get_stop(&self) -> isize { + self.stop + } - fn get_line(&self) -> isize { self.line } + fn get_line(&self) -> isize { + self.line + } - fn get_column(&self) -> isize { self.column } + fn get_column(&self) -> isize { + self.column + } // fn get_source(&self) -> Option<(Box, Box)> { // unimplemented!() @@ -167,11 +191,17 @@ impl + Debug> Token for GenericToken { } } - fn set_text(&mut self, _text: String) { unimplemented!() } + fn set_text(&mut self, _text: String) { + unimplemented!() + } - fn get_token_index(&self) -> isize { self.token_index.load(Ordering::Relaxed) } + fn get_token_index(&self) -> isize { + self.token_index.load(Ordering::Relaxed) + } - fn set_token_index(&self, _v: isize) { self.token_index.store(_v, Ordering::Relaxed) } + fn set_token_index(&self, _v: isize) { + self.token_index.store(_v, Ordering::Relaxed) + } fn to_owned(&self) -> OwningToken { OwningToken { @@ -189,11 +219,15 @@ impl + Debug> Token for GenericToken { } impl Default for &'_ OwningToken { - fn default() -> Self { &**INVALID_OWNING } + fn default() -> Self { + &**INVALID_OWNING + } } impl Default for &'_ CommonToken<'_> { - fn default() -> Self { &**INVALID_COMMON } + fn default() -> Self { + &**INVALID_COMMON + } } // diff --git a/src/token_factory.rs b/src/token_factory.rs index 296a991..8b83fde 100644 --- a/src/token_factory.rs +++ b/src/token_factory.rs @@ -80,11 +80,15 @@ pub trait TokenFactory<'a>: TidAble<'a> + Sized { } /// Default token factory -#[derive(Default, Tid, Debug)] +#[derive(Default, Debug)] pub struct CommonTokenFactory; +better_any::tid! {CommonTokenFactory} + impl Default for &'_ CommonTokenFactory { - fn default() -> Self { &**COMMON_TOKEN_FACTORY_DEFAULT } + fn default() -> Self { + &**COMMON_TOKEN_FACTORY_DEFAULT + } } impl<'a> TokenFactory<'a> for CommonTokenFactory { @@ -132,16 +136,22 @@ impl<'a> TokenFactory<'a> for CommonTokenFactory { }) } - fn create_invalid() -> Self::Tok { INVALID_COMMON.clone() } + fn create_invalid() -> Self::Tok { + INVALID_COMMON.clone() + } - fn get_data(from: Self::From) -> Cow<'a, Self::Data> { from } + fn get_data(from: Self::From) -> Cow<'a, Self::Data> { + from + } } /// Token factory that produces heap allocated /// `OwningToken`s -#[derive(Default, Tid, Debug)] +#[derive(Default, Debug)] pub struct OwningTokenFactory; +better_any::tid! {OwningTokenFactory} + impl<'a> TokenFactory<'a> for OwningTokenFactory { type Inner = OwningToken; type Tok = Box; @@ -187,9 +197,13 @@ impl<'a> TokenFactory<'a> for OwningTokenFactory { }) } - fn create_invalid() -> Self::Tok { INVALID_OWNING.clone() } + fn create_invalid() -> Self::Tok { + INVALID_OWNING.clone() + } - fn get_data(from: Self::From) -> Cow<'a, Self::Data> { from.into() } + fn get_data(from: Self::From) -> Cow<'a, Self::Data> { + from.into() + } } // pub struct DynFactory<'input,TF:TokenFactory<'.into()input>>(TF) where TF::Tok:CoerceUnsized>; @@ -220,13 +234,14 @@ pub type ArenaCommonFactory<'a> = ArenaFactory<'a, CommonTokenFactory, CommonTok /// const INVALID_TOKEN:CustomToken = ... /// ``` // Box is used here because it is almost always should be used for token factory -#[derive(Tid)] pub struct ArenaFactory<'input, TF, T> { arena: Arena, factory: TF, pd: PhantomData<&'input str>, } +better_any::tid! {impl<'input,TF,T> TidAble<'input> for ArenaFactory<'input,TF,T>} + impl<'input, TF: Debug, T> Debug for ArenaFactory<'input, TF, T> { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { f.debug_struct("ArenaFactory") @@ -282,9 +297,13 @@ where self.arena.alloc(*token) } - fn create_invalid() -> &'input Tok { <&Tok as Default>::default() } + fn create_invalid() -> &'input Tok { + <&Tok as Default>::default() + } - fn get_data(from: Self::From) -> Cow<'input, Self::Data> { TF::get_data(from) } + fn get_data(from: Self::From) -> Cow<'input, Self::Data> { + TF::get_data(from) + } } #[doc(hidden)] diff --git a/src/token_source.rs b/src/token_source.rs index 4093ef3..7395ae3 100644 --- a/src/token_source.rs +++ b/src/token_source.rs @@ -18,7 +18,9 @@ pub trait TokenSource<'input> { * Returns the line number for the current position in the input stream, or * 0 if the current token source does not track line numbers. */ - fn get_line(&self) -> isize { 0 } + fn get_line(&self) -> isize { + 0 + } /** * Get the index into the current line for the current position in the input * stream. The first character on a line has position 0. @@ -26,7 +28,9 @@ pub trait TokenSource<'input> { * Returns the line number for the current position in the input stream, or * -1 if the current token source does not track character positions. */ - fn get_char_position_in_line(&self) -> isize { -1 } + fn get_char_position_in_line(&self) -> isize { + -1 + } /// Returns underlying input stream fn get_input_stream(&mut self) -> Option<&mut dyn IntStream>; @@ -48,22 +52,34 @@ where { type TF = T::TF; #[inline(always)] - fn next_token(&mut self) -> >::Tok { (**self).next_token() } + fn next_token(&mut self) -> >::Tok { + (**self).next_token() + } #[inline(always)] - fn get_line(&self) -> isize { (**self).get_line() } + fn get_line(&self) -> isize { + (**self).get_line() + } #[inline(always)] - fn get_char_position_in_line(&self) -> isize { (**self).get_char_position_in_line() } + fn get_char_position_in_line(&self) -> isize { + (**self).get_char_position_in_line() + } #[inline(always)] - fn get_input_stream(&mut self) -> Option<&mut dyn IntStream> { (**self).get_input_stream() } + fn get_input_stream(&mut self) -> Option<&mut dyn IntStream> { + (**self).get_input_stream() + } #[inline(always)] - fn get_source_name(&self) -> String { (**self).get_source_name() } + fn get_source_name(&self) -> String { + (**self).get_source_name() + } #[inline(always)] - fn get_token_factory(&self) -> &'input Self::TF { (**self).get_token_factory() } + fn get_token_factory(&self) -> &'input Self::TF { + (**self).get_token_factory() + } } // / adaptor to feed parser with existing tokens diff --git a/src/token_stream.rs b/src/token_stream.rs index 5028808..9623c72 100644 --- a/src/token_stream.rs +++ b/src/token_stream.rs @@ -29,7 +29,9 @@ pub trait TokenStream<'input>: IntStream { fn get_token_source(&self) -> &dyn TokenSource<'input, TF = Self::TF>; // fn set_token_source(&self,source: Box); /// Get combined text of all tokens in this stream - fn get_all_text(&self) -> String { self.get_text_from_interval(0, self.size() - 1) } + fn get_all_text(&self) -> String { + self.get_text_from_interval(0, self.size() - 1) + } /// Get combined text of tokens in start..=stop interval fn get_text_from_interval(&self, start: isize, stop: isize) -> String; // fn get_text_from_rule_context(&self,context: RuleContext) -> String; @@ -68,7 +70,6 @@ impl<'a, 'input: 'a, T: TokenStream<'input>> Iterator for TokenIter<'a, 'input, } /// Token stream that keeps all data in internal Vec -#[derive(Tid)] pub struct UnbufferedTokenStream<'input, T: TokenSource<'input>> { token_source: T, pub(crate) tokens: Vec<>::Tok>, @@ -77,6 +78,8 @@ pub struct UnbufferedTokenStream<'input, T: TokenSource<'input>> { markers_count: isize, pub(crate) p: isize, } +better_any::tid! { impl<'input,T> TidAble<'input> for UnbufferedTokenStream<'input, T> where T: TokenSource<'input>} + impl<'input, T: TokenSource<'input>> Debug for UnbufferedTokenStream<'input, T> { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { f.debug_struct("UnbufferedTokenStream") @@ -90,7 +93,9 @@ impl<'input, T: TokenSource<'input>> Debug for UnbufferedTokenStream<'input, T> impl<'input, T: TokenSource<'input>> UnbufferedTokenStream<'input, T> { /// Creates iterator over this token stream - pub fn iter(&mut self) -> IterWrapper<'_, Self> { IterWrapper(self) } + pub fn iter(&mut self) -> IterWrapper<'_, Self> { + IterWrapper(self) + } /// Creates iterator over tokens in this token stream pub fn token_iter(&mut self) -> TokenIter<'_, 'input, Self> { @@ -122,7 +127,9 @@ impl<'input, T: TokenSource<'input>> UnbufferedTokenStream<'input, T> { } } - fn get_buffer_start_index(&self) -> isize { self.current_token_index - self.p } + fn get_buffer_start_index(&self) -> isize { + self.current_token_index - self.p + } pub(crate) fn fill(&mut self, need: isize) -> isize { for i in 0..need { @@ -161,7 +168,9 @@ impl<'input, T: TokenSource<'input>> TokenStream<'input> for UnbufferedTokenStre &self.tokens[(index - self.get_buffer_start_index()) as usize] } - fn get_token_source(&self) -> &dyn TokenSource<'input, TF = Self::TF> { &self.token_source } + fn get_token_source(&self) -> &dyn TokenSource<'input, TF = Self::TF> { + &self.token_source + } fn get_text_from_interval(&self, start: isize, stop: isize) -> String { // println!("get_text_from_interval {}..{}",start,stop); @@ -255,7 +264,9 @@ impl<'input, T: TokenSource<'input>> IntStream for UnbufferedTokenStream<'input, } #[inline(always)] - fn index(&self) -> isize { self.current_token_index } + fn index(&self) -> isize { + self.current_token_index + } #[inline] fn seek(&mut self, mut index: isize) { @@ -276,7 +287,11 @@ impl<'input, T: TokenSource<'input>> IntStream for UnbufferedTokenStream<'input, } #[inline(always)] - fn size(&self) -> isize { self.tokens.len() as isize } + fn size(&self) -> isize { + self.tokens.len() as isize + } - fn get_source_name(&self) -> String { self.token_source.get_source_name() } + fn get_source_name(&self) -> String { + self.token_source.get_source_name() + } } diff --git a/src/transition.rs b/src/transition.rs index 0ebc6e2..15f527c 100644 --- a/src/transition.rs +++ b/src/transition.rs @@ -52,11 +52,17 @@ pub enum TransitionType { pub trait Transition: Sync + Send + Debug + Any { fn get_target(&self) -> ATNStateRef; fn set_target(&mut self, s: ATNStateRef); - fn is_epsilon(&self) -> bool { false } - fn get_label(&self) -> Option> { None } + fn is_epsilon(&self) -> bool { + false + } + fn get_label(&self) -> Option> { + None + } fn get_serialization_type(&self) -> TransitionType; fn matches(&self, symbol: isize, min_vocab_symbol: isize, max_vocab_symbol: isize) -> bool; - fn get_predicate(&self) -> Option { None } + fn get_predicate(&self) -> Option { + None + } fn get_reachable_target(&self, symbol: isize) -> Option { // println!("reachable target called on {:?}", self); if self.matches(symbol, LEXER_MIN_CHAR_VALUE, LEXER_MAX_CHAR_VALUE) { @@ -81,9 +87,13 @@ pub struct AtomTransition { } impl Transition for AtomTransition { - fn get_target(&self) -> ATNStateRef { self.target } + fn get_target(&self) -> ATNStateRef { + self.target + } - fn set_target(&mut self, s: ATNStateRef) { self.target = s } + fn set_target(&mut self, s: ATNStateRef) { + self.target = s + } fn get_label(&self) -> Option> { let mut r = IntervalSet::new(); @@ -91,7 +101,9 @@ impl Transition for AtomTransition { Some(Cow::Owned(r)) } - fn get_serialization_type(&self) -> TransitionType { TransitionType::TRANSITION_ATOM } + fn get_serialization_type(&self) -> TransitionType { + TransitionType::TRANSITION_ATOM + } fn matches(&self, _symbol: isize, _min_vocab_symbol: isize, _max_vocab_symbol: isize) -> bool { _symbol == self.label @@ -107,12 +119,20 @@ pub struct RuleTransition { } impl Transition for RuleTransition { - fn get_target(&self) -> ATNStateRef { self.target } - fn set_target(&mut self, s: ATNStateRef) { self.target = s } + fn get_target(&self) -> ATNStateRef { + self.target + } + fn set_target(&mut self, s: ATNStateRef) { + self.target = s + } - fn is_epsilon(&self) -> bool { true } + fn is_epsilon(&self) -> bool { + true + } - fn get_serialization_type(&self) -> TransitionType { TransitionType::TRANSITION_RULE } + fn get_serialization_type(&self) -> TransitionType { + TransitionType::TRANSITION_RULE + } fn matches(&self, _symbol: isize, _min_vocab_symbol: isize, _max_vocab_symbol: isize) -> bool { unimplemented!() @@ -126,12 +146,20 @@ pub struct EpsilonTransition { } impl Transition for EpsilonTransition { - fn get_target(&self) -> ATNStateRef { self.target } - fn set_target(&mut self, s: ATNStateRef) { self.target = s } + fn get_target(&self) -> ATNStateRef { + self.target + } + fn set_target(&mut self, s: ATNStateRef) { + self.target = s + } - fn is_epsilon(&self) -> bool { true } + fn is_epsilon(&self) -> bool { + true + } - fn get_serialization_type(&self) -> TransitionType { TransitionType::TRANSITION_EPSILON } + fn get_serialization_type(&self) -> TransitionType { + TransitionType::TRANSITION_EPSILON + } fn matches(&self, _symbol: isize, _min_vocab_symbol: isize, _max_vocab_symbol: isize) -> bool { false @@ -146,8 +174,12 @@ pub struct RangeTransition { } impl Transition for RangeTransition { - fn get_target(&self) -> ATNStateRef { self.target } - fn set_target(&mut self, s: ATNStateRef) { self.target = s } + fn get_target(&self) -> ATNStateRef { + self.target + } + fn set_target(&mut self, s: ATNStateRef) { + self.target = s + } fn get_label(&self) -> Option> { let mut r = IntervalSet::new(); @@ -155,7 +187,9 @@ impl Transition for RangeTransition { Some(Cow::Owned(r)) } - fn get_serialization_type(&self) -> TransitionType { TransitionType::TRANSITION_RANGE } + fn get_serialization_type(&self) -> TransitionType { + TransitionType::TRANSITION_RANGE + } fn matches(&self, _symbol: isize, _min_vocab_symbol: isize, _max_vocab_symbol: isize) -> bool { _symbol >= self.start && _symbol <= self.stop @@ -172,12 +206,20 @@ pub struct ActionTransition { } impl Transition for ActionTransition { - fn get_target(&self) -> ATNStateRef { self.target } - fn set_target(&mut self, s: ATNStateRef) { self.target = s } + fn get_target(&self) -> ATNStateRef { + self.target + } + fn set_target(&mut self, s: ATNStateRef) { + self.target = s + } - fn is_epsilon(&self) -> bool { true } + fn is_epsilon(&self) -> bool { + true + } - fn get_serialization_type(&self) -> TransitionType { TransitionType::TRANSITION_ACTION } + fn get_serialization_type(&self) -> TransitionType { + TransitionType::TRANSITION_ACTION + } fn matches(&self, _symbol: isize, _min_vocab_symbol: isize, _max_vocab_symbol: isize) -> bool { false @@ -191,12 +233,20 @@ pub struct SetTransition { } impl Transition for SetTransition { - fn get_target(&self) -> ATNStateRef { self.target } - fn set_target(&mut self, s: ATNStateRef) { self.target = s } + fn get_target(&self) -> ATNStateRef { + self.target + } + fn set_target(&mut self, s: ATNStateRef) { + self.target = s + } - fn get_label(&self) -> Option> { Some(Cow::Borrowed(&self.set)) } + fn get_label(&self) -> Option> { + Some(Cow::Borrowed(&self.set)) + } - fn get_serialization_type(&self) -> TransitionType { TransitionType::TRANSITION_SET } + fn get_serialization_type(&self) -> TransitionType { + TransitionType::TRANSITION_SET + } fn matches(&self, _symbol: isize, _min_vocab_symbol: isize, _max_vocab_symbol: isize) -> bool { self.set.contains(_symbol) @@ -210,12 +260,20 @@ pub struct NotSetTransition { } impl Transition for NotSetTransition { - fn get_target(&self) -> ATNStateRef { self.target } - fn set_target(&mut self, s: ATNStateRef) { self.target = s } + fn get_target(&self) -> ATNStateRef { + self.target + } + fn set_target(&mut self, s: ATNStateRef) { + self.target = s + } - fn get_label(&self) -> Option> { Some(Cow::Borrowed(&self.set)) } + fn get_label(&self) -> Option> { + Some(Cow::Borrowed(&self.set)) + } - fn get_serialization_type(&self) -> TransitionType { TransitionType::TRANSITION_NOTSET } + fn get_serialization_type(&self) -> TransitionType { + TransitionType::TRANSITION_NOTSET + } fn matches(&self, _symbol: isize, _min_vocab_symbol: isize, _max_vocab_symbol: isize) -> bool { _symbol >= _min_vocab_symbol && _symbol <= _max_vocab_symbol && !self.set.contains(_symbol) @@ -228,10 +286,16 @@ pub struct WildcardTransition { } impl Transition for WildcardTransition { - fn get_target(&self) -> ATNStateRef { self.target } - fn set_target(&mut self, s: ATNStateRef) { self.target = s } + fn get_target(&self) -> ATNStateRef { + self.target + } + fn set_target(&mut self, s: ATNStateRef) { + self.target = s + } - fn get_serialization_type(&self) -> TransitionType { TransitionType::TRANSITION_WILDCARD } + fn get_serialization_type(&self) -> TransitionType { + TransitionType::TRANSITION_WILDCARD + } fn matches(&self, _symbol: isize, _min_vocab_symbol: isize, _max_vocab_symbol: isize) -> bool { _symbol < _max_vocab_symbol && _symbol > _min_vocab_symbol @@ -247,13 +311,21 @@ pub struct PredicateTransition { } impl Transition for PredicateTransition { - fn get_target(&self) -> ATNStateRef { self.target } + fn get_target(&self) -> ATNStateRef { + self.target + } - fn set_target(&mut self, s: ATNStateRef) { self.target = s } + fn set_target(&mut self, s: ATNStateRef) { + self.target = s + } - fn is_epsilon(&self) -> bool { true } + fn is_epsilon(&self) -> bool { + true + } - fn get_serialization_type(&self) -> TransitionType { TransitionType::TRANSITION_PREDICATE } + fn get_serialization_type(&self) -> TransitionType { + TransitionType::TRANSITION_PREDICATE + } fn matches(&self, _symbol: isize, _min_vocab_symbol: isize, _max_vocab_symbol: isize) -> bool { false @@ -275,12 +347,20 @@ pub struct PrecedencePredicateTransition { } impl Transition for PrecedencePredicateTransition { - fn get_target(&self) -> ATNStateRef { self.target } - fn set_target(&mut self, s: ATNStateRef) { self.target = s } + fn get_target(&self) -> ATNStateRef { + self.target + } + fn set_target(&mut self, s: ATNStateRef) { + self.target = s + } - fn is_epsilon(&self) -> bool { true } + fn is_epsilon(&self) -> bool { + true + } - fn get_serialization_type(&self) -> TransitionType { TransitionType::TRANSITION_PRECEDENCE } + fn get_serialization_type(&self) -> TransitionType { + TransitionType::TRANSITION_PRECEDENCE + } fn matches(&self, _symbol: isize, _min_vocab_symbol: isize, _max_vocab_symbol: isize) -> bool { false diff --git a/src/tree.rs b/src/tree.rs index f4ff24d..fce1359 100644 --- a/src/tree.rs +++ b/src/tree.rs @@ -5,7 +5,7 @@ use std::borrow::Borrow; use std::fmt::{Debug, Formatter}; use std::iter::from_fn; use std::marker::PhantomData; -use std::ops::{CoerceUnsized, Deref}; +use std::ops::Deref; use std::rc::Rc; use crate::atn::INVALID_ALT; @@ -18,19 +18,28 @@ 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, trees, CoerceTo}; use better_any::{Tid, TidAble}; +use std::mem; //todo try to make in more generic #[allow(missing_docs)] -pub trait Tree<'input>: NodeText + RuleContext<'input> { - fn get_parent(&self) -> Option>::Type>> { None } - fn has_parent(&self) -> bool { false } - fn get_payload(&self) -> Box { unimplemented!() } +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!() + } fn get_child(&self, _i: usize) -> Option>::Type>> { None } - fn get_child_count(&self) -> usize { 0 } + fn get_child_count(&self) -> usize { + 0 + } fn get_children<'a>( &'a self, ) -> Box>::Type>> + 'a> @@ -58,7 +67,9 @@ pub trait ParseTree<'input>: Tree<'input> { /// {@link TokenStream} of the first and last token associated with this /// subtree. If this node is a leaf, then the interval represents a single /// token and has interval i..i for token index i. - fn get_source_interval(&self) -> Interval { interval_set::INVALID } + fn get_source_interval(&self) -> Interval { + interval_set::INVALID + } /// Return combined text of this AST node. /// To create resulting string it does traverse whole subtree, @@ -67,7 +78,9 @@ pub trait ParseTree<'input>: Tree<'input> { /// Since tokens on hidden channels (e.g. whitespace or comments) are not /// added to the parse trees, they will not appear in the output of this /// method. - fn get_text(&self) -> String { String::new() } + 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. @@ -82,43 +95,41 @@ pub trait ParseTree<'input>: Tree<'input> { /// 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(); - } -} +// pub trait NodeText { +// 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)] -#[derive(Tid, Debug)] +#[derive(Debug)] pub struct NoError; #[doc(hidden)] -#[derive(Tid, Debug)] +#[derive(Debug)] pub struct IsError; /// Generic leaf AST node -#[derive(Tid)] pub struct LeafNode<'input, Node: ParserNodeType<'input>, T: 'static> { /// Token, this leaf consist of pub symbol: >::Tok, iserror: PhantomData, } +better_any::tid! { impl <'input, Node, T:'static> TidAble<'input> for LeafNode<'input, Node, T> where Node:ParserNodeType<'input> } impl<'input, Node: ParserNodeType<'input>, T: 'static> CustomRuleContext<'input> for LeafNode<'input, Node, T> @@ -126,11 +137,17 @@ impl<'input, Node: ParserNodeType<'input>, T: 'static> CustomRuleContext<'input> type TF = Node::TF; type Ctx = Node; - fn get_rule_index(&self) -> usize { usize::max_value() } + fn get_rule_index(&self) -> usize { + usize::max_value() + } + + fn get_node_text(&self, rule_names: &[&str]) -> String { + self.symbol.borrow().get_text().to_display() + } } -impl<'input, Node: ParserNodeType<'input> + TidAble<'input>, T: 'static + TidAble<'input>> - ParserRuleContext<'input> for LeafNode<'input, Node, T> +impl<'input, Node: ParserNodeType<'input>, T: 'static> ParserRuleContext<'input> + for LeafNode<'input, Node, T> { } @@ -141,11 +158,11 @@ 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> 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> @@ -155,7 +172,9 @@ impl<'input, Node: ParserNodeType<'input>, T: 'static> ParseTree<'input> Interval { a: i, b: i } } - fn get_text(&self) -> String { self.symbol.borrow().get_text().to_display() } + fn get_text(&self) -> String { + self.symbol.borrow().get_text().to_display() + } } impl<'input, Node: ParserNodeType<'input>, T: 'static> Debug for LeafNode<'input, Node, T> { @@ -185,7 +204,9 @@ pub type TerminalNode<'input, NodeType> = LeafNode<'input, NodeType, NoError>; impl<'input, Node: ParserNodeType<'input>, Listener: ParseTreeListener<'input, Node> + ?Sized> Listenable for TerminalNode<'input, Node> { - fn enter(&self, listener: &mut Listener) { listener.visit_terminal(self) } + fn enter(&self, listener: &mut Listener) { + listener.visit_terminal(self) + } fn exit(&self, _listener: &mut Listener) { // do nothing @@ -195,7 +216,9 @@ impl<'input, Node: ParserNodeType<'input>, Listener: ParseTreeListener<'input, N impl<'input, Node: ParserNodeType<'input>, Visitor: ParseTreeVisitor<'input, Node> + ?Sized> Visitable for TerminalNode<'input, Node> { - fn accept(&self, visitor: &mut Visitor) { visitor.visit_terminal(self) } + fn accept(&self, visitor: &mut Visitor) { + visitor.visit_terminal(self) + } } /// # Error Leaf @@ -205,7 +228,9 @@ pub type ErrorNode<'input, NodeType> = LeafNode<'input, NodeType, IsError>; impl<'input, Node: ParserNodeType<'input>, Listener: ParseTreeListener<'input, Node> + ?Sized> Listenable for ErrorNode<'input, Node> { - fn enter(&self, listener: &mut Listener) { listener.visit_error_node(self) } + fn enter(&self, listener: &mut Listener) { + listener.visit_error_node(self) + } fn exit(&self, _listener: &mut Listener) { // do nothing @@ -215,19 +240,114 @@ impl<'input, Node: ParserNodeType<'input>, Listener: ParseTreeListener<'input, N impl<'input, Node: ParserNodeType<'input>, Visitor: ParseTreeVisitor<'input, Node> + ?Sized> Visitable for ErrorNode<'input, Node> { - fn accept(&self, visitor: &mut Visitor) { visitor.visit_error_node(self) } + fn accept(&self, visitor: &mut Visitor) { + visitor.visit_error_node(self) + } +} + +pub trait ParseTreeVisitorCompat<'input>: VisitChildren<'input, Self::Node> { + type Node: ParserNodeType<'input>; + type Return: Default; + + /// Temporary storage for `ParseTreeVisitor` blanket implementation to work + /// + /// If you have `()` as a return value + /// either use `YourGrammarParseTreeVisitor` directly + /// or make + /// ```rust + /// Box::leak(Box::new(())) + /// # ; + /// ``` + /// as an implementation of that method so that there is no need to create dummy field in your visitor + fn temp_result(&mut self) -> &mut Self::Return; + + fn visit(&mut self, node: &>::Type) -> Self::Return { + self.visit_node(&node); + mem::take(self.temp_result()) + } + + /// Called on terminal(leaf) node + fn visit_terminal(&mut self, _node: &TerminalNode<'input, Self::Node>) -> Self::Return { + Self::Return::default() + } + /// Called on error node + fn visit_error_node(&mut self, _node: &ErrorNode<'input, Self::Node>) -> Self::Return { + Self::Return::default() + } + + fn visit_children( + &mut self, + node: &>::Type, + ) -> Self::Return { + let mut result = Self::Return::default(); + for node in node.get_children() { + if !self.should_visit_next_child(&node, &result) { + break; + } + + let child_result = self.visit(&node); + result = self.aggregate_results(result, child_result); + } + return result; + } + + fn aggregate_results(&self, aggregate: Self::Return, next: Self::Return) -> Self::Return { + next + } + + fn should_visit_next_child( + &self, + node: &>::Type, + current: &Self::Return, + ) -> bool { + true + } +} + +// struct VisitorAdapter<'input, T: ParseTreeVisitorCompat<'input>> { +// visitor: T, +// pub curr_value: T::Return, +// _pd: PhantomData<&'input str>, +// } + +impl<'input, Node, T> ParseTreeVisitor<'input, Node> for T +where + Node: ParserNodeType<'input>, + Node::Type: VisitableDyn, + T: ParseTreeVisitorCompat<'input, Node = Node>, +{ + fn visit_terminal(&mut self, node: &TerminalNode<'input, Node>) { + let result = ::visit_terminal(self, node); + *::temp_result(self) = result; + } + + fn visit_error_node(&mut self, node: &ErrorNode<'input, Node>) { + let result = ::visit_error_node(self, node); + *::temp_result(self) = result; + } + + fn visit_children(&mut self, node: &Node::Type) { + let result = ::visit_children(self, node); + *::temp_result(self) = result; + } } /// Base interface for visiting over syntax tree pub trait ParseTreeVisitor<'input, Node: ParserNodeType<'input>>: VisitChildren<'input, Node> { + /// Basically alias for `node.accept(self)` in visitor implementation + /// just to make api closer to java + /// Called on terminal(leaf) node fn visit_terminal(&mut self, _node: &TerminalNode<'input, Node>) {} /// Called on error node fn visit_error_node(&mut self, _node: &ErrorNode<'input, Node>) {} /// Implement this only if you want to change children visiting algorithm - fn visit_children(&mut self, node: &Node::Type) { self.visit_children_inner(node) } + fn visit_children(&mut self, node: &Node::Type) { + node.get_children() + .for_each(|child| self.visit_node(&child)) + } } /// Workaround for default recursive children visiting @@ -235,8 +355,8 @@ pub trait ParseTreeVisitor<'input, Node: ParserNodeType<'input>>: /// Already blanket implemented for all visitors. /// To override it you would need to implement `ParseTreeVisitor::visit_children` pub trait VisitChildren<'input, Node: ParserNodeType<'input>> { - #[doc(hidden)] - fn visit_children_inner(&mut self, node: &Node::Type); + // fn visit_children_inner(&mut self, node: &Node::Type); + fn visit_node(&mut self, node: &Node::Type); } impl<'input, Node, T> VisitChildren<'input, Node> for T @@ -246,8 +366,15 @@ where // for<'a> &'a mut Self: CoerceUnsized<&'a mut Node::Visitor>, Node::Type: VisitableDyn, { - #[inline(always)] - fn visit_children_inner(&mut self, node: &Node::Type) { node.accept_children(self) } + // #[inline(always)] + // fn visit_children_inner(&mut self, node: &Node::Type) { + // // node.accept_children(self) + // + // } + + fn visit_node(&mut self, node: &Node::Type) { + node.accept_dyn(self) + } } /// Types that can accept particular visitor @@ -312,11 +439,13 @@ where /// Walks recursively over tree `t` with `listener` pub fn walk(mut listener: Box, t: &Ctx) -> Box where - for<'x> &'x mut Listener: CoerceUnsized<&'x mut T>, - for<'x> &'x Ctx: CoerceUnsized<&'x Node::Type>, + // for<'x> &'x mut Listener: CoerceUnsized<&'x mut T>, + // for<'x> &'x Ctx: CoerceUnsized<&'x Node::Type>, + Listener: CoerceTo, + Ctx: CoerceTo, { // let mut listener = listener as Box; - Self::walk_inner(listener.as_mut(), t as &Node::Type); + Self::walk_inner(listener.as_mut().coerce_mut_to(), t.coerce_ref_to()); // just cast back // unsafe { Box::::from_raw(Box::into_raw(listener) as *mut _) } diff --git a/src/utils.rs b/src/utils.rs index aa80806..ac6e1f6 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,4 +1,5 @@ use std::borrow::Borrow; +use std::cell::Cell; // use crate::utils::Cow2::{Borrowed2, Owned2}; pub fn escape_whitespaces(data: impl Borrow, escape_spaces: bool) -> String { @@ -16,6 +17,16 @@ pub fn escape_whitespaces(data: impl Borrow, escape_spaces: bool) -> String pub trait Sealed {} +pub fn cell_update(cell: &Cell, f: F) -> T +where + F: FnOnce(T) -> T, +{ + let old = cell.get(); + let new = f(old); + cell.set(new); + new +} + // pub enum Cow2<'a,Ref,T:Borrow = Ref>{ // Borrowed2(&'a Ref), // Owned2(T) diff --git a/src/vocabulary.rs b/src/vocabulary.rs index b5c597b..cf734bf 100644 --- a/src/vocabulary.rs +++ b/src/vocabulary.rs @@ -87,7 +87,9 @@ impl VocabularyImpl { } impl Vocabulary for VocabularyImpl { - fn get_max_token_type(&self) -> isize { self.max_token_type } + fn get_max_token_type(&self) -> isize { + self.max_token_type + } fn get_literal_name(&self, token_type: isize) -> Option<&str> { self.literal_names @@ -121,11 +123,19 @@ pub(crate) static DUMMY_VOCAB: DummyVocab = DummyVocab; pub(crate) struct DummyVocab; impl Vocabulary for DummyVocab { - fn get_max_token_type(&self) -> isize { unimplemented!() } + fn get_max_token_type(&self) -> isize { + unimplemented!() + } - fn get_literal_name(&self, _token_type: isize) -> Option<&str> { unimplemented!() } + fn get_literal_name(&self, _token_type: isize) -> Option<&str> { + unimplemented!() + } - fn get_symbolic_name(&self, _token_type: isize) -> Option<&str> { unimplemented!() } + fn get_symbolic_name(&self, _token_type: isize) -> Option<&str> { + unimplemented!() + } - fn get_display_name(&self, token_type: isize) -> Cow<'_, str> { token_type.to_string().into() } + fn get_display_name(&self, token_type: isize) -> Cow<'_, str> { + token_type.to_string().into() + } } diff --git a/templates/BaseRustTest.java b/templates/BaseRustTest.java index fbfa8ee..5dfec41 100644 --- a/templates/BaseRustTest.java +++ b/templates/BaseRustTest.java @@ -640,9 +640,10 @@ public void setTokenTypeChannel(int ttype, int channel) { protected void writeParserTestFile(String parserName, String lexerName, String listenerName, String visitorName, String parserStartRuleName, boolean debug) { - ST outputFileST = new ST("#![feature(try_blocks)]\n" + - "#![feature(inner_deref)]\n" + - "#![feature(specialization)]\n" + + ST outputFileST = new ST( +// "#![feature(try_blocks)]\n" + +// "#![feature(inner_deref)]\n" + +// "#![feature(specialization)]\n" + "mod ;\n" + "use ::*;\n" + "mod ;\n" + diff --git a/templates/Rust.stg b/templates/Rust.stg index ffc6761..e945963 100644 --- a/templates/Rust.stg +++ b/templates/Rust.stg @@ -51,6 +51,7 @@ ParserFile(file, parser, namedActions, contextSuperClass) ::= << #![allow(nonstandard_style)] #![allow(unused_imports)] #![allow(unused_mut)] +#![allow(unused_braces)] use antlr_rust::PredictionContextCache; use antlr_rust::parser::{Parser, BaseParser, ParserRecog, ParserNodeType}; @@ -100,9 +101,7 @@ use antlr_rust::tree::ParseTreeListener; use super::::*; pub trait Listener\<'input> : ParseTreeListener\<'input,ContextType>{ - * Enter a parse tree produced by the {@code \} @@ -126,6 +125,10 @@ fn exit_(&mut self, _ctx: &Context\<'input>) { \}}; } +antlr_rust::coerce_from!{ 'input : Listener\<'input> } + + + >> BaseListenerFile(file, header, namedActions) ::= <<>> @@ -134,7 +137,7 @@ VisitorFile(file, header, namedActions) ::= << #![allow(nonstandard_style)]
-use antlr_rust::tree::{ParseTreeVisitor}; +use antlr_rust::tree::{ParseTreeVisitor,ParseTreeVisitorCompat}; use super::::*; /** @@ -154,7 +157,35 @@ pub trait Visitor\<'input>: ParseTreeVisitor\<'input,(&mut self, ctx: &Context\<'input>) { self.visit_children(ctx) \} }; separator="\n"> +} +pub trait VisitorCompat\<'input>:ParseTreeVisitorCompat\<'input, Node= ContextType>{ + + * Visit a parse tree produced by the {@code \} + * labeled alternative in {@link #\}. + + * Visit a parse tree produced by {@link #\}. + + * @param ctx the parse tree + */ + fn visit_(&mut self, ctx: &Context\<'input>) -> Self::Return { + self.visit_children(ctx) + \} +}; separator="\n"> +} + +impl\<'input,T> Visitor\<'input> for T +where + T: VisitorCompat\<'input> +{ +(&mut self, ctx: &Context\<'input>){ + let result = \VisitorCompat>::visit_(self, ctx); + *\::temp_result(self) = result; + \} +}; separator="\n"> } >> @@ -183,7 +214,7 @@ Parser_(parser, funcs, atn, sempredFuncs, ctor, superClass) ::= << type BaseParserType\<'input, I> = - BaseParser\<'input,Ext, I, ContextType , dyn Listener\<'input> + 'input >; + BaseParser\<'input,Ext\<'input>, I, ContextType , dyn Listener\<'input> + 'input >; type TokenType\<'input> = \< as TokenFactory\<'input>\>::Tok; @@ -219,7 +250,7 @@ where } pub fn with_strategy(input: I, strategy: H) -> Self { - antlr_rust::recognizer::check_version("0","2"); + antlr_rust::recognizer::check_version("0","3"); let interpreter = Arc::new(ParserATNSimulator::new( _ATN.clone(), _decision_to_DFA.clone(), @@ -230,6 +261,7 @@ where input, Arc::clone(&interpreter), Ext{ + _pd: Default::default(), } ), @@ -268,6 +300,8 @@ pub trait Context\<'input>: ParserRuleContext\<'input, TF=, Ctx=ContextType> {} +antlr_rust::coerce_from!{ 'input : Context\<'input> } + impl\<'input, 'x, T> VisitableDyn\ for dyn Context\<'input> + 'input where @@ -282,14 +316,12 @@ where impl\<'input> Context\<'input> for TerminalNode\<'input,ContextType> {} impl\<'input> Context\<'input> for ErrorNode\<'input,ContextType> {} -#[antlr_rust::impl_tid] -impl\<'input> antlr_rust::TidAble\<'input> for dyn Context\<'input> + 'input{} +antlr_rust::tid! { impl\<'input> TidAble\<'input> for dyn Context\<'input> + 'input } -#[antlr_rust::impl_tid] -impl\<'input> antlr_rust::TidAble\<'input> for dyn Listener\<'input> + 'input{} +antlr_rust::tid! { impl\<'input> TidAble\<'input> for dyn Listener\<'input> + 'input } pub struct ContextType; -antlr_rust::type_id!{ContextType} +antlr_rust::tid!{ContextType} impl\<'input> ParserNodeType\<'input> for ContextType{ type TF = ; @@ -322,22 +354,23 @@ where } } -pub struct Ext{ +pub struct Ext\<'input>{ + _pd: PhantomData\<&'input str>, } -impl Ext{ +impl\<'input> Ext\<'input>{ } +antlr_rust::tid! { Ext\<'a> } - -impl\<'input> TokenAware\<'input> for Ext{ +impl\<'input> TokenAware\<'input> for Ext\<'input>{ type TF = ; } -impl\<'input,I: TokenStream\<'input, TF = > + TidAble\<'input>\> ParserRecog\<'input, BaseParserType\<'input,I>\> for Ext{} +impl\<'input,I: TokenStream\<'input, TF = > + TidAble\<'input>\> ParserRecog\<'input, BaseParserType\<'input,I>\> for Ext\<'input>{} -impl\<'input,I: TokenStream\<'input, TF = > + TidAble\<'input>\> Actions\<'input, BaseParserType\<'input,I>\> for Ext{ +impl\<'input,I: TokenStream\<'input, TF = > + TidAble\<'input>\> Actions\<'input, BaseParserType\<'input,I>\> for Ext\<'input>{ fn get_grammar_file_name(&self) -> & str{ ""} fn get_rule_names(&self) -> &[& str] {&ruleNames} @@ -461,7 +494,7 @@ pub enum All\<'input>{ (\<'input>), }>Error(\<'input>) } -antlr_rust::type_id!{All\<'a>} +antlr_rust::tid!{All\<'a>} impl\<'input> antlr_rust::parser_rule_context::DerefSeal for All\<'input>{} @@ -517,7 +550,7 @@ where let mut _localctx: Rc\<All> = _localctx; - let result: Result\<(), ANTLRError> = try { + let result: Result\<(), ANTLRError> = (|| { let mut _alt: isize; @@ -525,7 +558,8 @@ where - }; + Ok(()) + })(); match result { Ok(_)=>{}, @@ -576,14 +610,15 @@ where let _startState = ; - let result: Result\<(), ANTLRError> = try { + let result: Result\<(), ANTLRError> = (|| { let mut _alt: isize; - }; + Ok(()) + })(); match result { Ok(_) => {}, @@ -913,7 +948,7 @@ let temp = ; .push(temp);})> >> TokenDecl(t) ::= ": Option\<>" -TokenTypeDecl(t) ::= "let mut : isize;" +TokenTypeDecl(t) ::= "let mut : isize = -1;" TokenListDecl(t) ::= ":Vec\<>" RuleContextDecl(r) ::= ": Option\All\<'input>>>" RuleContextListDecl(rdecl) ::= ":Vec\All\<'input>>>" @@ -982,7 +1017,11 @@ impl\<'input> Context\<'input> for \<'input>{} impl\<'input,'a> Listenable\Listener\<'input> + 'a> for \<'input>{ - + + + + + } @@ -998,7 +1037,7 @@ impl\<'input> CustomRuleContext\<'input> for Ext\<'input>{ fn get_rule_index(&self) -> usize { RULE_ } //fn type_rule_index() -> usize where Self: Sized { RULE_ } } -antlr_rust::type_id!{Ext\<'a>} +antlr_rust::tid!{Ext\<'a>} impl\<'input> Ext\<'input>{ fn new(parent: Option\Context\<'input> + 'input > >, invoking_state: isize}>) -> Rc\<All\<'input>\> { @@ -1049,7 +1088,7 @@ pub struct Ext\<'input>{ }> ph:PhantomData\<&'input str> } -antlr_rust::type_id!{Ext\<'a>} +antlr_rust::tid!{Ext\<'a>} impl\<'input> Context\<'input> for \<'input>{} @@ -1260,11 +1299,12 @@ pub type LocalTokenFactory\<'input> = CommonTokenFactory; type From\<'a> = \ as TokenFactory\<'a> >::From; -#[derive(Tid)] pub struct \<'input, Input:CharStream\ >\> { base: BaseLexer\<'input,Actions,Input,\>, } +antlr_rust::tid! { impl\<'input,Input> TidAble\<'input> for \<'input,Input> where Input:CharStream\ > } + impl\<'input, Input:CharStream\ >\> Deref for \<'input,Input>{ type Target = BaseLexer\<'input,Actions,Input,\>; @@ -1297,7 +1337,7 @@ impl\<'input, Input:CharStream\ >\> \<'input,Input>{ } pub fn new_with_token_factory(input: Input, tf: &'input ) -> Self { - antlr_rust::recognizer::check_version("0","2"); + antlr_rust::recognizer::check_version("0","3"); Self { base: BaseLexer::new_base_lexer( input, diff --git a/tests/gen/csvlexer.rs b/tests/gen/csvlexer.rs index 0dd085b..beec6b9 100644 --- a/tests/gen/csvlexer.rs +++ b/tests/gen/csvlexer.rs @@ -68,31 +68,44 @@ pub type LocalTokenFactory<'input> = antlr_rust::token_factory::ArenaCommonFacto type From<'a> = as TokenFactory<'a>>::From; -#[derive(Tid)] pub struct CSVLexer<'input, Input: CharStream>> { base: BaseLexer<'input, CSVLexerActions, Input, LocalTokenFactory<'input>>, } +antlr_rust::tid! { impl<'input,Input> TidAble<'input> for CSVLexer<'input,Input> where Input:CharStream > } + impl<'input, Input: CharStream>> Deref for CSVLexer<'input, Input> { type Target = BaseLexer<'input, CSVLexerActions, Input, LocalTokenFactory<'input>>; - fn deref(&self) -> &Self::Target { &self.base } + fn deref(&self) -> &Self::Target { + &self.base + } } impl<'input, Input: CharStream>> DerefMut for CSVLexer<'input, Input> { - fn deref_mut(&mut self) -> &mut Self::Target { &mut self.base } + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.base + } } impl<'input, Input: CharStream>> CSVLexer<'input, Input> { - fn get_rule_names(&self) -> &'static [&'static str] { &ruleNames } - fn get_literal_names(&self) -> &[Option<&str>] { &_LITERAL_NAMES } + fn get_rule_names(&self) -> &'static [&'static str] { + &ruleNames + } + fn get_literal_names(&self) -> &[Option<&str>] { + &_LITERAL_NAMES + } - fn get_symbolic_names(&self) -> &[Option<&str>] { &_SYMBOLIC_NAMES } + fn get_symbolic_names(&self) -> &[Option<&str>] { + &_SYMBOLIC_NAMES + } - fn get_grammar_file_name(&self) -> &'static str { "CSVLexer.g4" } + fn get_grammar_file_name(&self) -> &'static str { + "CSVLexer.g4" + } pub fn new_with_token_factory(input: Input, tf: &'input LocalTokenFactory<'input>) -> Self { - antlr_rust::recognizer::check_version("0", "2"); + antlr_rust::recognizer::check_version("0", "3"); Self { base: BaseLexer::new_base_lexer( input, @@ -141,17 +154,29 @@ impl<'input> TokenAware<'input> for CSVLexerActions { impl<'input, Input: CharStream>> TokenSource<'input> for CSVLexer<'input, Input> { type TF = LocalTokenFactory<'input>; - fn next_token(&mut self) -> >::Tok { self.base.next_token() } + fn next_token(&mut self) -> >::Tok { + self.base.next_token() + } - fn get_line(&self) -> isize { self.base.get_line() } + fn get_line(&self) -> isize { + self.base.get_line() + } - fn get_char_position_in_line(&self) -> isize { self.base.get_char_position_in_line() } + fn get_char_position_in_line(&self) -> isize { + self.base.get_char_position_in_line() + } - fn get_input_stream(&mut self) -> Option<&mut dyn IntStream> { self.base.get_input_stream() } + fn get_input_stream(&mut self) -> Option<&mut dyn IntStream> { + self.base.get_input_stream() + } - fn get_source_name(&self) -> String { self.base.get_source_name() } + fn get_source_name(&self) -> String { + self.base.get_source_name() + } - fn get_token_factory(&self) -> &'input Self::TF { self.base.get_token_factory() } + fn get_token_factory(&self) -> &'input Self::TF { + self.base.get_token_factory() + } } lazy_static! { diff --git a/tests/gen/csvlistener.rs b/tests/gen/csvlistener.rs index 062682b..d063ce6 100644 --- a/tests/gen/csvlistener.rs +++ b/tests/gen/csvlistener.rs @@ -14,7 +14,6 @@ pub trait CSVListener<'input>: ParseTreeListener<'input, CSVParserContextType> { * @param ctx the parse tree */ fn exit_csvFile(&mut self, _ctx: &CsvFileContext<'input>) {} - /** * Enter a parse tree produced by {@link CSVParser#hdr}. * @param ctx the parse tree @@ -25,7 +24,6 @@ pub trait CSVListener<'input>: ParseTreeListener<'input, CSVParserContextType> { * @param ctx the parse tree */ fn exit_hdr(&mut self, _ctx: &HdrContext<'input>) {} - /** * Enter a parse tree produced by {@link CSVParser#row}. * @param ctx the parse tree @@ -36,7 +34,6 @@ pub trait CSVListener<'input>: ParseTreeListener<'input, CSVParserContextType> { * @param ctx the parse tree */ fn exit_row(&mut self, _ctx: &RowContext<'input>) {} - /** * Enter a parse tree produced by {@link CSVParser#field}. * @param ctx the parse tree @@ -48,3 +45,5 @@ pub trait CSVListener<'input>: ParseTreeListener<'input, CSVParserContextType> { */ fn exit_field(&mut self, _ctx: &FieldContext<'input>) {} } + +antlr_rust::coerce_from! { 'input : CSVListener<'input> } diff --git a/tests/gen/csvparser.rs b/tests/gen/csvparser.rs index 96f8dd3..afb9bfe 100644 --- a/tests/gen/csvparser.rs +++ b/tests/gen/csvparser.rs @@ -5,6 +5,7 @@ #![allow(nonstandard_style)] #![allow(unused_imports)] #![allow(unused_mut)] +#![allow(unused_braces)] use super::csvlistener::*; use super::csvvisitor::*; use antlr_rust::atn::{ATN, INVALID_ALT}; @@ -71,8 +72,13 @@ lazy_static! { )); } -type BaseParserType<'input, I> = - BaseParser<'input, CSVParserExt, I, CSVParserContextType, dyn CSVListener<'input> + 'input>; +type BaseParserType<'input, I> = BaseParser< + 'input, + CSVParserExt<'input>, + I, + CSVParserContextType, + dyn CSVListener<'input> + 'input, +>; type TokenType<'input> = as TokenFactory<'input>>::Tok; @@ -98,19 +104,29 @@ where I: TokenStream<'input, TF = LocalTokenFactory<'input>> + TidAble<'input>, H: ErrorStrategy<'input, BaseParserType<'input, I>>, { - pub fn get_serialized_atn() -> &'static str { _serializedATN } + pub fn get_serialized_atn() -> &'static str { + _serializedATN + } - pub fn set_error_strategy(&mut self, strategy: H) { self.err_handler = strategy } + pub fn set_error_strategy(&mut self, strategy: H) { + self.err_handler = strategy + } pub fn with_strategy(input: I, strategy: H) -> Self { - antlr_rust::recognizer::check_version("0", "2"); + antlr_rust::recognizer::check_version("0", "3"); let interpreter = Arc::new(ParserATNSimulator::new( _ATN.clone(), _decision_to_DFA.clone(), _shared_context_cache.clone(), )); Self { - base: BaseParser::new_base_parser(input, Arc::clone(&interpreter), CSVParserExt {}), + base: BaseParser::new_base_parser( + input, + Arc::clone(&interpreter), + CSVParserExt { + _pd: Default::default(), + }, + ), interpreter, _shared_context_cache: Box::new(PredictionContextCache::new()), err_handler: strategy, @@ -133,7 +149,9 @@ impl<'input, I> CSVParser<'input, I, DefaultErrorStrategy<'input, CSVParserConte where I: TokenStream<'input, TF = LocalTokenFactory<'input>> + TidAble<'input>, { - pub fn new(input: I) -> Self { Self::with_strategy(input, DefaultErrorStrategy::new()) } + pub fn new(input: I) -> Self { + Self::with_strategy(input, DefaultErrorStrategy::new()) + } } /// Trait for monomorphized trait object that corresponds to the nodes of parse tree generated for CSVParser @@ -144,6 +162,8 @@ pub trait CSVParserContext<'input>: { } +antlr_rust::coerce_from! { 'input : CSVParserContext<'input> } + impl<'input, 'x, T> VisitableDyn for dyn CSVParserContext<'input> + 'input where T: CSVVisitor<'input> + 'x, @@ -156,14 +176,12 @@ where impl<'input> CSVParserContext<'input> for TerminalNode<'input, CSVParserContextType> {} impl<'input> CSVParserContext<'input> for ErrorNode<'input, CSVParserContextType> {} -#[antlr_rust::impl_tid] -impl<'input> antlr_rust::TidAble<'input> for dyn CSVParserContext<'input> + 'input {} +antlr_rust::tid! { impl<'input> TidAble<'input> for dyn CSVParserContext<'input> + 'input } -#[antlr_rust::impl_tid] -impl<'input> antlr_rust::TidAble<'input> for dyn CSVListener<'input> + 'input {} +antlr_rust::tid! { impl<'input> TidAble<'input> for dyn CSVListener<'input> + 'input } pub struct CSVParserContextType; -antlr_rust::type_id! {CSVParserContextType} +antlr_rust::tid! {CSVParserContextType} impl<'input> ParserNodeType<'input> for CSVParserContextType { type TF = LocalTokenFactory<'input>; @@ -177,7 +195,9 @@ where { type Target = BaseParserType<'input, I>; - fn deref(&self) -> &Self::Target { &self.base } + fn deref(&self) -> &Self::Target { + &self.base + } } impl<'input, I, H> DerefMut for CSVParser<'input, I, H> @@ -185,30 +205,41 @@ where I: TokenStream<'input, TF = LocalTokenFactory<'input>> + TidAble<'input>, H: ErrorStrategy<'input, BaseParserType<'input, I>>, { - fn deref_mut(&mut self) -> &mut Self::Target { &mut self.base } + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.base + } } -pub struct CSVParserExt {} +pub struct CSVParserExt<'input> { + _pd: PhantomData<&'input str>, +} -impl CSVParserExt {} +impl<'input> CSVParserExt<'input> {} +antlr_rust::tid! { CSVParserExt<'a> } -impl<'input> TokenAware<'input> for CSVParserExt { +impl<'input> TokenAware<'input> for CSVParserExt<'input> { type TF = LocalTokenFactory<'input>; } impl<'input, I: TokenStream<'input, TF = LocalTokenFactory<'input>> + TidAble<'input>> - ParserRecog<'input, BaseParserType<'input, I>> for CSVParserExt + ParserRecog<'input, BaseParserType<'input, I>> for CSVParserExt<'input> { } impl<'input, I: TokenStream<'input, TF = LocalTokenFactory<'input>> + TidAble<'input>> - Actions<'input, BaseParserType<'input, I>> for CSVParserExt + Actions<'input, BaseParserType<'input, I>> for CSVParserExt<'input> { - fn get_grammar_file_name(&self) -> &str { "CSV.g4" } + fn get_grammar_file_name(&self) -> &str { + "CSV.g4" + } - fn get_rule_names(&self) -> &[&str] { &ruleNames } + fn get_rule_names(&self) -> &[&str] { + &ruleNames + } - fn get_vocabulary(&self) -> &dyn Vocabulary { &**VOCABULARY } + fn get_vocabulary(&self) -> &dyn Vocabulary { + &**VOCABULARY + } } //------------------- csvFile ---------------- pub type CsvFileContextAll<'input> = CsvFileContext<'input>; @@ -234,16 +265,20 @@ impl<'input, 'a> Listenable + 'a> for CsvFileContext<'in } impl<'input, 'a> Visitable + 'a> for CsvFileContext<'input> { - fn accept(&self, visitor: &mut (dyn CSVVisitor<'input> + 'a)) { visitor.visit_csvFile(self); } + fn accept(&self, visitor: &mut (dyn CSVVisitor<'input> + 'a)) { + visitor.visit_csvFile(self); + } } impl<'input> CustomRuleContext<'input> for CsvFileContextExt<'input> { type TF = LocalTokenFactory<'input>; type Ctx = CSVParserContextType; - fn get_rule_index(&self) -> usize { RULE_csvFile } + fn get_rule_index(&self) -> usize { + RULE_csvFile + } //fn type_rule_index() -> usize where Self: Sized { RULE_csvFile } } -antlr_rust::type_id! {CsvFileContextExt<'a>} +antlr_rust::tid! {CsvFileContextExt<'a>} impl<'input> CsvFileContextExt<'input> { fn new( @@ -294,8 +329,8 @@ where let mut _localctx = CsvFileContextExt::new(_parentctx.clone(), recog.base.get_state()); recog.base.enter_rule(_localctx.clone(), 0, RULE_csvFile); let mut _localctx: Rc = _localctx; - let mut _la: isize; - let result: Result<(), ANTLRError> = try { + let mut _la: isize = -1; + let result: Result<(), ANTLRError> = (|| { //recog.base.enter_outer_alt(_localctx.clone(), 1); recog.base.enter_outer_alt(None, 1); { @@ -330,7 +365,8 @@ where } } } - }; + Ok(()) + })(); match result { Ok(_) => {} Err(e @ ANTLRError::FallThrough(_)) => return Err(e), @@ -369,16 +405,20 @@ impl<'input, 'a> Listenable + 'a> for HdrContext<'input> } impl<'input, 'a> Visitable + 'a> for HdrContext<'input> { - fn accept(&self, visitor: &mut (dyn CSVVisitor<'input> + 'a)) { visitor.visit_hdr(self); } + fn accept(&self, visitor: &mut (dyn CSVVisitor<'input> + 'a)) { + visitor.visit_hdr(self); + } } impl<'input> CustomRuleContext<'input> for HdrContextExt<'input> { type TF = LocalTokenFactory<'input>; type Ctx = CSVParserContextType; - fn get_rule_index(&self) -> usize { RULE_hdr } + fn get_rule_index(&self) -> usize { + RULE_hdr + } //fn type_rule_index() -> usize where Self: Sized { RULE_hdr } } -antlr_rust::type_id! {HdrContextExt<'a>} +antlr_rust::tid! {HdrContextExt<'a>} impl<'input> HdrContextExt<'input> { fn new( @@ -417,7 +457,7 @@ where let mut _localctx = HdrContextExt::new(_parentctx.clone(), recog.base.get_state()); recog.base.enter_rule(_localctx.clone(), 2, RULE_hdr); let mut _localctx: Rc = _localctx; - let result: Result<(), ANTLRError> = try { + let result: Result<(), ANTLRError> = (|| { //recog.base.enter_outer_alt(_localctx.clone(), 1); recog.base.enter_outer_alt(None, 1); { @@ -425,7 +465,8 @@ where recog.base.set_state(14); recog.row()?; } - }; + Ok(()) + })(); match result { Ok(_) => {} Err(e @ ANTLRError::FallThrough(_)) => return Err(e), @@ -464,16 +505,20 @@ impl<'input, 'a> Listenable + 'a> for RowContext<'input> } impl<'input, 'a> Visitable + 'a> for RowContext<'input> { - fn accept(&self, visitor: &mut (dyn CSVVisitor<'input> + 'a)) { visitor.visit_row(self); } + fn accept(&self, visitor: &mut (dyn CSVVisitor<'input> + 'a)) { + visitor.visit_row(self); + } } impl<'input> CustomRuleContext<'input> for RowContextExt<'input> { type TF = LocalTokenFactory<'input>; type Ctx = CSVParserContextType; - fn get_rule_index(&self) -> usize { RULE_row } + fn get_rule_index(&self) -> usize { + RULE_row + } //fn type_rule_index() -> usize where Self: Sized { RULE_row } } -antlr_rust::type_id! {RowContextExt<'a>} +antlr_rust::tid! {RowContextExt<'a>} impl<'input> RowContextExt<'input> { fn new( @@ -518,8 +563,8 @@ where let mut _localctx = RowContextExt::new(_parentctx.clone(), recog.base.get_state()); recog.base.enter_rule(_localctx.clone(), 4, RULE_row); let mut _localctx: Rc = _localctx; - let mut _la: isize; - let result: Result<(), ANTLRError> = try { + let mut _la: isize = -1; + let result: Result<(), ANTLRError> = (|| { //recog.base.enter_outer_alt(_localctx.clone(), 1); recog.base.enter_outer_alt(None, 1); { @@ -558,7 +603,8 @@ where recog.base.set_state(27); recog.base.match_token(T__2, &mut recog.err_handler)?; } - }; + Ok(()) + })(); match result { Ok(_) => {} Err(e @ ANTLRError::FallThrough(_)) => return Err(e), @@ -597,16 +643,20 @@ impl<'input, 'a> Listenable + 'a> for FieldContext<'inpu } impl<'input, 'a> Visitable + 'a> for FieldContext<'input> { - fn accept(&self, visitor: &mut (dyn CSVVisitor<'input> + 'a)) { visitor.visit_field(self); } + fn accept(&self, visitor: &mut (dyn CSVVisitor<'input> + 'a)) { + visitor.visit_field(self); + } } impl<'input> CustomRuleContext<'input> for FieldContextExt<'input> { type TF = LocalTokenFactory<'input>; type Ctx = CSVParserContextType; - fn get_rule_index(&self) -> usize { RULE_field } + fn get_rule_index(&self) -> usize { + RULE_field + } //fn type_rule_index() -> usize where Self: Sized { RULE_field } } -antlr_rust::type_id! {FieldContextExt<'a>} +antlr_rust::tid! {FieldContextExt<'a>} impl<'input> FieldContextExt<'input> { fn new( @@ -655,7 +705,7 @@ where let mut _localctx = FieldContextExt::new(_parentctx.clone(), recog.base.get_state()); recog.base.enter_rule(_localctx.clone(), 6, RULE_field); let mut _localctx: Rc = _localctx; - let result: Result<(), ANTLRError> = try { + let result: Result<(), ANTLRError> = (|| { recog.base.set_state(32); recog.err_handler.sync(&mut recog.base)?; match recog.base.input.la(1) { @@ -687,7 +737,8 @@ where &mut recog.base, )))?, } - }; + Ok(()) + })(); match result { Ok(_) => {} Err(e @ ANTLRError::FallThrough(_)) => return Err(e), diff --git a/tests/gen/csvvisitor.rs b/tests/gen/csvvisitor.rs index 5c96648..3d63ae8 100644 --- a/tests/gen/csvvisitor.rs +++ b/tests/gen/csvvisitor.rs @@ -1,7 +1,7 @@ #![allow(nonstandard_style)] // Generated from CSV.g4 by ANTLR 4.8 use super::csvparser::*; -use antlr_rust::tree::ParseTreeVisitor; +use antlr_rust::tree::{ParseTreeVisitor, ParseTreeVisitorCompat}; /** * This interface defines a complete generic visitor for a parse tree produced @@ -12,23 +12,92 @@ pub trait CSVVisitor<'input>: ParseTreeVisitor<'input, CSVParserContextType> { * Visit a parse tree produced by {@link CSVParser#csvFile}. * @param ctx the parse tree */ - fn visit_csvFile(&mut self, ctx: &CsvFileContext<'input>) { self.visit_children(ctx) } + fn visit_csvFile(&mut self, ctx: &CsvFileContext<'input>) { + self.visit_children(ctx) + } /** * Visit a parse tree produced by {@link CSVParser#hdr}. * @param ctx the parse tree */ - fn visit_hdr(&mut self, ctx: &HdrContext<'input>) { self.visit_children(ctx) } + fn visit_hdr(&mut self, ctx: &HdrContext<'input>) { + self.visit_children(ctx) + } /** * Visit a parse tree produced by {@link CSVParser#row}. * @param ctx the parse tree */ - fn visit_row(&mut self, ctx: &RowContext<'input>) { self.visit_children(ctx) } + fn visit_row(&mut self, ctx: &RowContext<'input>) { + self.visit_children(ctx) + } /** * Visit a parse tree produced by {@link CSVParser#field}. * @param ctx the parse tree */ - fn visit_field(&mut self, ctx: &FieldContext<'input>) { self.visit_children(ctx) } + fn visit_field(&mut self, ctx: &FieldContext<'input>) { + self.visit_children(ctx) + } +} + +pub trait CSVVisitorCompat<'input>: + ParseTreeVisitorCompat<'input, Node = CSVParserContextType> +{ + /** + * Visit a parse tree produced by {@link CSVParser#csvFile}. + * @param ctx the parse tree + */ + fn visit_csvFile(&mut self, ctx: &CsvFileContext<'input>) -> Self::Return { + self.visit_children(ctx) + } + + /** + * Visit a parse tree produced by {@link CSVParser#hdr}. + * @param ctx the parse tree + */ + fn visit_hdr(&mut self, ctx: &HdrContext<'input>) -> Self::Return { + self.visit_children(ctx) + } + + /** + * Visit a parse tree produced by {@link CSVParser#row}. + * @param ctx the parse tree + */ + fn visit_row(&mut self, ctx: &RowContext<'input>) -> Self::Return { + self.visit_children(ctx) + } + + /** + * Visit a parse tree produced by {@link CSVParser#field}. + * @param ctx the parse tree + */ + fn visit_field(&mut self, ctx: &FieldContext<'input>) -> Self::Return { + self.visit_children(ctx) + } +} + +impl<'input, T> CSVVisitor<'input> for T +where + T: CSVVisitorCompat<'input>, +{ + fn visit_csvFile(&mut self, ctx: &CsvFileContext<'input>) { + let result = ::visit_csvFile(self, ctx); + *::temp_result(self) = result; + } + + fn visit_hdr(&mut self, ctx: &HdrContext<'input>) { + let result = ::visit_hdr(self, ctx); + *::temp_result(self) = result; + } + + fn visit_row(&mut self, ctx: &RowContext<'input>) { + let result = ::visit_row(self, ctx); + *::temp_result(self) = result; + } + + fn visit_field(&mut self, ctx: &FieldContext<'input>) { + let result = ::visit_field(self, ctx); + *::temp_result(self) = result; + } } diff --git a/tests/gen/labelslexer.rs b/tests/gen/labelslexer.rs index 420c41d..f8960be 100644 --- a/tests/gen/labelslexer.rs +++ b/tests/gen/labelslexer.rs @@ -82,31 +82,44 @@ pub type LocalTokenFactory<'input> = CommonTokenFactory; type From<'a> = as TokenFactory<'a>>::From; -#[derive(Tid)] pub struct LabelsLexer<'input, Input: CharStream>> { base: BaseLexer<'input, LabelsLexerActions, Input, LocalTokenFactory<'input>>, } +antlr_rust::tid! { impl<'input,Input> TidAble<'input> for LabelsLexer<'input,Input> where Input:CharStream > } + impl<'input, Input: CharStream>> Deref for LabelsLexer<'input, Input> { type Target = BaseLexer<'input, LabelsLexerActions, Input, LocalTokenFactory<'input>>; - fn deref(&self) -> &Self::Target { &self.base } + fn deref(&self) -> &Self::Target { + &self.base + } } impl<'input, Input: CharStream>> DerefMut for LabelsLexer<'input, Input> { - fn deref_mut(&mut self) -> &mut Self::Target { &mut self.base } + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.base + } } impl<'input, Input: CharStream>> LabelsLexer<'input, Input> { - fn get_rule_names(&self) -> &'static [&'static str] { &ruleNames } - fn get_literal_names(&self) -> &[Option<&str>] { &_LITERAL_NAMES } + fn get_rule_names(&self) -> &'static [&'static str] { + &ruleNames + } + fn get_literal_names(&self) -> &[Option<&str>] { + &_LITERAL_NAMES + } - fn get_symbolic_names(&self) -> &[Option<&str>] { &_SYMBOLIC_NAMES } + fn get_symbolic_names(&self) -> &[Option<&str>] { + &_SYMBOLIC_NAMES + } - fn get_grammar_file_name(&self) -> &'static str { "LabelsLexer.g4" } + fn get_grammar_file_name(&self) -> &'static str { + "LabelsLexer.g4" + } pub fn new_with_token_factory(input: Input, tf: &'input LocalTokenFactory<'input>) -> Self { - antlr_rust::recognizer::check_version("0", "2"); + antlr_rust::recognizer::check_version("0", "3"); Self { base: BaseLexer::new_base_lexer( input, @@ -158,17 +171,29 @@ impl<'input> TokenAware<'input> for LabelsLexerActions { impl<'input, Input: CharStream>> TokenSource<'input> for LabelsLexer<'input, Input> { type TF = LocalTokenFactory<'input>; - fn next_token(&mut self) -> >::Tok { self.base.next_token() } + fn next_token(&mut self) -> >::Tok { + self.base.next_token() + } - fn get_line(&self) -> isize { self.base.get_line() } + fn get_line(&self) -> isize { + self.base.get_line() + } - fn get_char_position_in_line(&self) -> isize { self.base.get_char_position_in_line() } + fn get_char_position_in_line(&self) -> isize { + self.base.get_char_position_in_line() + } - fn get_input_stream(&mut self) -> Option<&mut dyn IntStream> { self.base.get_input_stream() } + fn get_input_stream(&mut self) -> Option<&mut dyn IntStream> { + self.base.get_input_stream() + } - fn get_source_name(&self) -> String { self.base.get_source_name() } + fn get_source_name(&self) -> String { + self.base.get_source_name() + } - fn get_token_factory(&self) -> &'input Self::TF { self.base.get_token_factory() } + fn get_token_factory(&self) -> &'input Self::TF { + self.base.get_token_factory() + } } lazy_static! { diff --git a/tests/gen/labelslistener.rs b/tests/gen/labelslistener.rs index 6f648cc..2448b8a 100644 --- a/tests/gen/labelslistener.rs +++ b/tests/gen/labelslistener.rs @@ -14,7 +14,6 @@ pub trait LabelsListener<'input>: ParseTreeListener<'input, LabelsParserContextT * @param ctx the parse tree */ fn exit_s(&mut self, _ctx: &SContext<'input>) {} - /** * Enter a parse tree produced by the {@code add} * labeled alternative in {@link LabelsParser#e}. @@ -27,7 +26,6 @@ pub trait LabelsListener<'input>: ParseTreeListener<'input, LabelsParserContextT * @param ctx the parse tree */ fn exit_add(&mut self, _ctx: &AddContext<'input>) {} - /** * Enter a parse tree produced by the {@code parens} * labeled alternative in {@link LabelsParser#e}. @@ -40,7 +38,6 @@ pub trait LabelsListener<'input>: ParseTreeListener<'input, LabelsParserContextT * @param ctx the parse tree */ fn exit_parens(&mut self, _ctx: &ParensContext<'input>) {} - /** * Enter a parse tree produced by the {@code mult} * labeled alternative in {@link LabelsParser#e}. @@ -53,7 +50,6 @@ pub trait LabelsListener<'input>: ParseTreeListener<'input, LabelsParserContextT * @param ctx the parse tree */ fn exit_mult(&mut self, _ctx: &MultContext<'input>) {} - /** * Enter a parse tree produced by the {@code dec} * labeled alternative in {@link LabelsParser#e}. @@ -66,7 +62,6 @@ pub trait LabelsListener<'input>: ParseTreeListener<'input, LabelsParserContextT * @param ctx the parse tree */ fn exit_dec(&mut self, _ctx: &DecContext<'input>) {} - /** * Enter a parse tree produced by the {@code anID} * labeled alternative in {@link LabelsParser#e}. @@ -79,7 +74,6 @@ pub trait LabelsListener<'input>: ParseTreeListener<'input, LabelsParserContextT * @param ctx the parse tree */ fn exit_anID(&mut self, _ctx: &AnIDContext<'input>) {} - /** * Enter a parse tree produced by the {@code anInt} * labeled alternative in {@link LabelsParser#e}. @@ -92,7 +86,6 @@ pub trait LabelsListener<'input>: ParseTreeListener<'input, LabelsParserContextT * @param ctx the parse tree */ fn exit_anInt(&mut self, _ctx: &AnIntContext<'input>) {} - /** * Enter a parse tree produced by the {@code inc} * labeled alternative in {@link LabelsParser#e}. @@ -106,3 +99,5 @@ pub trait LabelsListener<'input>: ParseTreeListener<'input, LabelsParserContextT */ fn exit_inc(&mut self, _ctx: &IncContext<'input>) {} } + +antlr_rust::coerce_from! { 'input : LabelsListener<'input> } diff --git a/tests/gen/labelsparser.rs b/tests/gen/labelsparser.rs index cff678b..2f93a7a 100644 --- a/tests/gen/labelsparser.rs +++ b/tests/gen/labelsparser.rs @@ -5,6 +5,7 @@ #![allow(nonstandard_style)] #![allow(unused_imports)] #![allow(unused_mut)] +#![allow(unused_braces)] use super::labelslistener::*; use antlr_rust::atn::{ATN, INVALID_ALT}; use antlr_rust::atn_deserializer::ATNDeserializer; @@ -82,7 +83,7 @@ lazy_static! { type BaseParserType<'input, I> = BaseParser< 'input, - LabelsParserExt, + LabelsParserExt<'input>, I, LabelsParserContextType, dyn LabelsListener<'input> + 'input, @@ -111,19 +112,29 @@ where I: TokenStream<'input, TF = LocalTokenFactory<'input>> + TidAble<'input>, H: ErrorStrategy<'input, BaseParserType<'input, I>>, { - pub fn get_serialized_atn() -> &'static str { _serializedATN } + pub fn get_serialized_atn() -> &'static str { + _serializedATN + } - pub fn set_error_strategy(&mut self, strategy: H) { self.err_handler = strategy } + pub fn set_error_strategy(&mut self, strategy: H) { + self.err_handler = strategy + } pub fn with_strategy(input: I, strategy: H) -> Self { - antlr_rust::recognizer::check_version("0", "2"); + antlr_rust::recognizer::check_version("0", "3"); let interpreter = Arc::new(ParserATNSimulator::new( _ATN.clone(), _decision_to_DFA.clone(), _shared_context_cache.clone(), )); Self { - base: BaseParser::new_base_parser(input, Arc::clone(&interpreter), LabelsParserExt {}), + base: BaseParser::new_base_parser( + input, + Arc::clone(&interpreter), + LabelsParserExt { + _pd: Default::default(), + }, + ), interpreter, _shared_context_cache: Box::new(PredictionContextCache::new()), err_handler: strategy, @@ -146,7 +157,9 @@ impl<'input, I> LabelsParser<'input, I, DefaultErrorStrategy<'input, LabelsParse where I: TokenStream<'input, TF = LocalTokenFactory<'input>> + TidAble<'input>, { - pub fn new(input: I) -> Self { Self::with_strategy(input, DefaultErrorStrategy::new()) } + pub fn new(input: I) -> Self { + Self::with_strategy(input, DefaultErrorStrategy::new()) + } } /// Trait for monomorphized trait object that corresponds to the nodes of parse tree generated for LabelsParser @@ -156,17 +169,17 @@ pub trait LabelsParserContext<'input>: { } +antlr_rust::coerce_from! { 'input : LabelsParserContext<'input> } + impl<'input> LabelsParserContext<'input> for TerminalNode<'input, LabelsParserContextType> {} impl<'input> LabelsParserContext<'input> for ErrorNode<'input, LabelsParserContextType> {} -#[antlr_rust::impl_tid] -impl<'input> antlr_rust::TidAble<'input> for dyn LabelsParserContext<'input> + 'input {} +antlr_rust::tid! { impl<'input> TidAble<'input> for dyn LabelsParserContext<'input> + 'input } -#[antlr_rust::impl_tid] -impl<'input> antlr_rust::TidAble<'input> for dyn LabelsListener<'input> + 'input {} +antlr_rust::tid! { impl<'input> TidAble<'input> for dyn LabelsListener<'input> + 'input } pub struct LabelsParserContextType; -antlr_rust::type_id! {LabelsParserContextType} +antlr_rust::tid! {LabelsParserContextType} impl<'input> ParserNodeType<'input> for LabelsParserContextType { type TF = LocalTokenFactory<'input>; @@ -180,7 +193,9 @@ where { type Target = BaseParserType<'input, I>; - fn deref(&self) -> &Self::Target { &self.base } + fn deref(&self) -> &Self::Target { + &self.base + } } impl<'input, I, H> DerefMut for LabelsParser<'input, I, H> @@ -188,30 +203,41 @@ where I: TokenStream<'input, TF = LocalTokenFactory<'input>> + TidAble<'input>, H: ErrorStrategy<'input, BaseParserType<'input, I>>, { - fn deref_mut(&mut self) -> &mut Self::Target { &mut self.base } + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.base + } } -pub struct LabelsParserExt {} +pub struct LabelsParserExt<'input> { + _pd: PhantomData<&'input str>, +} -impl LabelsParserExt {} +impl<'input> LabelsParserExt<'input> {} +antlr_rust::tid! { LabelsParserExt<'a> } -impl<'input> TokenAware<'input> for LabelsParserExt { +impl<'input> TokenAware<'input> for LabelsParserExt<'input> { type TF = LocalTokenFactory<'input>; } impl<'input, I: TokenStream<'input, TF = LocalTokenFactory<'input>> + TidAble<'input>> - ParserRecog<'input, BaseParserType<'input, I>> for LabelsParserExt + ParserRecog<'input, BaseParserType<'input, I>> for LabelsParserExt<'input> { } impl<'input, I: TokenStream<'input, TF = LocalTokenFactory<'input>> + TidAble<'input>> - Actions<'input, BaseParserType<'input, I>> for LabelsParserExt + Actions<'input, BaseParserType<'input, I>> for LabelsParserExt<'input> { - fn get_grammar_file_name(&self) -> &str { "Labels.g4" } + fn get_grammar_file_name(&self) -> &str { + "Labels.g4" + } - fn get_rule_names(&self) -> &[&str] { &ruleNames } + fn get_rule_names(&self) -> &[&str] { + &ruleNames + } - fn get_vocabulary(&self) -> &dyn Vocabulary { &**VOCABULARY } + fn get_vocabulary(&self) -> &dyn Vocabulary { + &**VOCABULARY + } fn sempred( _localctx: Option<&(dyn LabelsParserContext<'input> + 'input)>, rule_index: isize, @@ -265,15 +291,21 @@ impl<'input, 'a> Listenable + 'a> for SContext<'input listener.enter_every_rule(self); listener.enter_s(self); } + fn exit(&self, listener: &mut (dyn LabelsListener<'input> + 'a)) { + listener.exit_s(self); + listener.exit_every_rule(self); + } } impl<'input> CustomRuleContext<'input> for SContextExt<'input> { type TF = LocalTokenFactory<'input>; type Ctx = LabelsParserContextType; - fn get_rule_index(&self) -> usize { RULE_s } + fn get_rule_index(&self) -> usize { + RULE_s + } //fn type_rule_index() -> usize where Self: Sized { RULE_s } } -antlr_rust::type_id! {SContextExt<'a>} +antlr_rust::tid! {SContextExt<'a>} impl<'input> SContextExt<'input> { fn new( @@ -315,7 +347,7 @@ where let mut _localctx = SContextExt::new(_parentctx.clone(), recog.base.get_state()); recog.base.enter_rule(_localctx.clone(), 0, RULE_s); let mut _localctx: Rc = _localctx; - let result: Result<(), ANTLRError> = try { + let result: Result<(), ANTLRError> = (|| { //recog.base.enter_outer_alt(_localctx.clone(), 1); recog.base.enter_outer_alt(None, 1); { @@ -324,7 +356,8 @@ where let tmp = recog.e_rec(0)?; cast_mut::<_, SContext>(&mut _localctx).q = Some(tmp.clone()); } - }; + Ok(()) + })(); match result { Ok(_) => {} Err(e @ ANTLRError::FallThrough(_)) => return Err(e), @@ -351,7 +384,7 @@ pub enum EContextAll<'input> { IncContext(IncContext<'input>), Error(EContext<'input>), } -antlr_rust::type_id! {EContextAll<'a>} +antlr_rust::tid! {EContextAll<'a>} impl<'input> antlr_rust::parser_rule_context::DerefSeal for EContextAll<'input> {} @@ -377,7 +410,9 @@ impl<'input, 'a> Listenable + 'a> for EContextAll<'in fn enter(&self, listener: &mut (dyn LabelsListener<'input> + 'a)) { self.deref().enter(listener) } - fn exit(&self, listener: &mut (dyn LabelsListener<'input> + 'a)) { self.deref().exit(listener) } + fn exit(&self, listener: &mut (dyn LabelsListener<'input> + 'a)) { + self.deref().exit(listener) + } } pub type EContext<'input> = BaseParserRuleContext<'input, EContextExt<'input>>; @@ -395,10 +430,12 @@ impl<'input, 'a> Listenable + 'a> for EContext<'input impl<'input> CustomRuleContext<'input> for EContextExt<'input> { type TF = LocalTokenFactory<'input>; type Ctx = LabelsParserContextType; - fn get_rule_index(&self) -> usize { RULE_e } + fn get_rule_index(&self) -> usize { + RULE_e + } //fn type_rule_index() -> usize where Self: Sized { RULE_e } } -antlr_rust::type_id! {EContextExt<'a>} +antlr_rust::tid! {EContextExt<'a>} impl<'input> EContextExt<'input> { fn new( @@ -425,7 +462,9 @@ pub trait EContextAttrs<'input>: { &self.borrow().v } - fn set_v(&mut self, attr: String) { self.borrow_mut().v = attr; } + fn set_v(&mut self, attr: String) { + self.borrow_mut().v = attr; + } } impl<'input> EContextAttrs<'input> for EContext<'input> {} @@ -456,7 +495,7 @@ pub struct AddContextExt<'input> { ph: PhantomData<&'input str>, } -antlr_rust::type_id! {AddContextExt<'a>} +antlr_rust::tid! {AddContextExt<'a>} impl<'input> LabelsParserContext<'input> for AddContext<'input> {} @@ -470,15 +509,21 @@ impl<'input, 'a> Listenable + 'a> for AddContext<'inp impl<'input> CustomRuleContext<'input> for AddContextExt<'input> { type TF = LocalTokenFactory<'input>; type Ctx = LabelsParserContextType; - fn get_rule_index(&self) -> usize { RULE_e } + fn get_rule_index(&self) -> usize { + RULE_e + } //fn type_rule_index() -> usize where Self: Sized { RULE_e } } impl<'input> Borrow> for AddContext<'input> { - fn borrow(&self) -> &EContextExt<'input> { &self.base } + fn borrow(&self) -> &EContextExt<'input> { + &self.base + } } impl<'input> BorrowMut> for AddContext<'input> { - fn borrow_mut(&mut self) -> &mut EContextExt<'input> { &mut self.base } + fn borrow_mut(&mut self) -> &mut EContextExt<'input> { + &mut self.base + } } impl<'input> EContextAttrs<'input> for AddContext<'input> {} @@ -516,7 +561,7 @@ pub struct ParensContextExt<'input> { ph: PhantomData<&'input str>, } -antlr_rust::type_id! {ParensContextExt<'a>} +antlr_rust::tid! {ParensContextExt<'a>} impl<'input> LabelsParserContext<'input> for ParensContext<'input> {} @@ -530,15 +575,21 @@ impl<'input, 'a> Listenable + 'a> for ParensContext<' impl<'input> CustomRuleContext<'input> for ParensContextExt<'input> { type TF = LocalTokenFactory<'input>; type Ctx = LabelsParserContextType; - fn get_rule_index(&self) -> usize { RULE_e } + fn get_rule_index(&self) -> usize { + RULE_e + } //fn type_rule_index() -> usize where Self: Sized { RULE_e } } impl<'input> Borrow> for ParensContext<'input> { - fn borrow(&self) -> &EContextExt<'input> { &self.base } + fn borrow(&self) -> &EContextExt<'input> { + &self.base + } } impl<'input> BorrowMut> for ParensContext<'input> { - fn borrow_mut(&mut self) -> &mut EContextExt<'input> { &mut self.base } + fn borrow_mut(&mut self) -> &mut EContextExt<'input> { + &mut self.base + } } impl<'input> EContextAttrs<'input> for ParensContext<'input> {} @@ -585,7 +636,7 @@ pub struct MultContextExt<'input> { ph: PhantomData<&'input str>, } -antlr_rust::type_id! {MultContextExt<'a>} +antlr_rust::tid! {MultContextExt<'a>} impl<'input> LabelsParserContext<'input> for MultContext<'input> {} @@ -599,15 +650,21 @@ impl<'input, 'a> Listenable + 'a> for MultContext<'in impl<'input> CustomRuleContext<'input> for MultContextExt<'input> { type TF = LocalTokenFactory<'input>; type Ctx = LabelsParserContextType; - fn get_rule_index(&self) -> usize { RULE_e } + fn get_rule_index(&self) -> usize { + RULE_e + } //fn type_rule_index() -> usize where Self: Sized { RULE_e } } impl<'input> Borrow> for MultContext<'input> { - fn borrow(&self) -> &EContextExt<'input> { &self.base } + fn borrow(&self) -> &EContextExt<'input> { + &self.base + } } impl<'input> BorrowMut> for MultContext<'input> { - fn borrow_mut(&mut self) -> &mut EContextExt<'input> { &mut self.base } + fn borrow_mut(&mut self) -> &mut EContextExt<'input> { + &mut self.base + } } impl<'input> EContextAttrs<'input> for MultContext<'input> {} @@ -646,7 +703,7 @@ pub struct DecContextExt<'input> { ph: PhantomData<&'input str>, } -antlr_rust::type_id! {DecContextExt<'a>} +antlr_rust::tid! {DecContextExt<'a>} impl<'input> LabelsParserContext<'input> for DecContext<'input> {} @@ -660,15 +717,21 @@ impl<'input, 'a> Listenable + 'a> for DecContext<'inp impl<'input> CustomRuleContext<'input> for DecContextExt<'input> { type TF = LocalTokenFactory<'input>; type Ctx = LabelsParserContextType; - fn get_rule_index(&self) -> usize { RULE_e } + fn get_rule_index(&self) -> usize { + RULE_e + } //fn type_rule_index() -> usize where Self: Sized { RULE_e } } impl<'input> Borrow> for DecContext<'input> { - fn borrow(&self) -> &EContextExt<'input> { &self.base } + fn borrow(&self) -> &EContextExt<'input> { + &self.base + } } impl<'input> BorrowMut> for DecContext<'input> { - fn borrow_mut(&mut self) -> &mut EContextExt<'input> { &mut self.base } + fn borrow_mut(&mut self) -> &mut EContextExt<'input> { + &mut self.base + } } impl<'input> EContextAttrs<'input> for DecContext<'input> {} @@ -707,7 +770,7 @@ pub struct AnIDContextExt<'input> { ph: PhantomData<&'input str>, } -antlr_rust::type_id! {AnIDContextExt<'a>} +antlr_rust::tid! {AnIDContextExt<'a>} impl<'input> LabelsParserContext<'input> for AnIDContext<'input> {} @@ -721,15 +784,21 @@ impl<'input, 'a> Listenable + 'a> for AnIDContext<'in impl<'input> CustomRuleContext<'input> for AnIDContextExt<'input> { type TF = LocalTokenFactory<'input>; type Ctx = LabelsParserContextType; - fn get_rule_index(&self) -> usize { RULE_e } + fn get_rule_index(&self) -> usize { + RULE_e + } //fn type_rule_index() -> usize where Self: Sized { RULE_e } } impl<'input> Borrow> for AnIDContext<'input> { - fn borrow(&self) -> &EContextExt<'input> { &self.base } + fn borrow(&self) -> &EContextExt<'input> { + &self.base + } } impl<'input> BorrowMut> for AnIDContext<'input> { - fn borrow_mut(&mut self) -> &mut EContextExt<'input> { &mut self.base } + fn borrow_mut(&mut self) -> &mut EContextExt<'input> { + &mut self.base + } } impl<'input> EContextAttrs<'input> for AnIDContext<'input> {} @@ -768,7 +837,7 @@ pub struct AnIntContextExt<'input> { ph: PhantomData<&'input str>, } -antlr_rust::type_id! {AnIntContextExt<'a>} +antlr_rust::tid! {AnIntContextExt<'a>} impl<'input> LabelsParserContext<'input> for AnIntContext<'input> {} @@ -782,15 +851,21 @@ impl<'input, 'a> Listenable + 'a> for AnIntContext<'i impl<'input> CustomRuleContext<'input> for AnIntContextExt<'input> { type TF = LocalTokenFactory<'input>; type Ctx = LabelsParserContextType; - fn get_rule_index(&self) -> usize { RULE_e } + fn get_rule_index(&self) -> usize { + RULE_e + } //fn type_rule_index() -> usize where Self: Sized { RULE_e } } impl<'input> Borrow> for AnIntContext<'input> { - fn borrow(&self) -> &EContextExt<'input> { &self.base } + fn borrow(&self) -> &EContextExt<'input> { + &self.base + } } impl<'input> BorrowMut> for AnIntContext<'input> { - fn borrow_mut(&mut self) -> &mut EContextExt<'input> { &mut self.base } + fn borrow_mut(&mut self) -> &mut EContextExt<'input> { + &mut self.base + } } impl<'input> EContextAttrs<'input> for AnIntContext<'input> {} @@ -827,7 +902,7 @@ pub struct IncContextExt<'input> { ph: PhantomData<&'input str>, } -antlr_rust::type_id! {IncContextExt<'a>} +antlr_rust::tid! {IncContextExt<'a>} impl<'input> LabelsParserContext<'input> for IncContext<'input> {} @@ -841,15 +916,21 @@ impl<'input, 'a> Listenable + 'a> for IncContext<'inp impl<'input> CustomRuleContext<'input> for IncContextExt<'input> { type TF = LocalTokenFactory<'input>; type Ctx = LabelsParserContextType; - fn get_rule_index(&self) -> usize { RULE_e } + fn get_rule_index(&self) -> usize { + RULE_e + } //fn type_rule_index() -> usize where Self: Sized { RULE_e } } impl<'input> Borrow> for IncContext<'input> { - fn borrow(&self) -> &EContextExt<'input> { &self.base } + fn borrow(&self) -> &EContextExt<'input> { + &self.base + } } impl<'input> BorrowMut> for IncContext<'input> { - fn borrow_mut(&mut self) -> &mut EContextExt<'input> { &mut self.base } + fn borrow_mut(&mut self) -> &mut EContextExt<'input> { + &mut self.base + } } impl<'input> EContextAttrs<'input> for IncContext<'input> {} @@ -872,7 +953,9 @@ where I: TokenStream<'input, TF = LocalTokenFactory<'input>> + TidAble<'input>, H: ErrorStrategy<'input, BaseParserType<'input, I>>, { - pub fn e(&mut self) -> Result>, ANTLRError> { self.e_rec(0) } + pub fn e(&mut self) -> Result>, ANTLRError> { + self.e_rec(0) + } fn e_rec(&mut self, _p: isize) -> Result>, ANTLRError> { let recog = self; @@ -885,7 +968,7 @@ where let mut _localctx: Rc = _localctx; let mut _prevctx = _localctx.clone(); let _startState = 2; - let result: Result<(), ANTLRError> = try { + let result: Result<(), ANTLRError> = (|| { let mut _alt: isize; //recog.base.enter_outer_alt(_localctx.clone(), 1); recog.base.enter_outer_alt(None, 1); @@ -1325,7 +1408,8 @@ where _alt = recog.interpreter.adaptive_predict(2, &mut recog.base)?; } } - }; + Ok(()) + })(); match result { Ok(_) => {} Err(e @ ANTLRError::FallThrough(_)) => return Err(e), diff --git a/tests/gen/referencetoatnlexer.rs b/tests/gen/referencetoatnlexer.rs index 8739255..28c9291 100644 --- a/tests/gen/referencetoatnlexer.rs +++ b/tests/gen/referencetoatnlexer.rs @@ -56,31 +56,44 @@ pub type LocalTokenFactory<'input> = antlr_rust::token_factory::OwningTokenFacto type From<'a> = as TokenFactory<'a>>::From; -#[derive(Tid)] pub struct ReferenceToATNLexer<'input, Input: CharStream>> { base: BaseLexer<'input, ReferenceToATNLexerActions, Input, LocalTokenFactory<'input>>, } +antlr_rust::tid! { impl<'input,Input> TidAble<'input> for ReferenceToATNLexer<'input,Input> where Input:CharStream > } + impl<'input, Input: CharStream>> Deref for ReferenceToATNLexer<'input, Input> { type Target = BaseLexer<'input, ReferenceToATNLexerActions, Input, LocalTokenFactory<'input>>; - fn deref(&self) -> &Self::Target { &self.base } + fn deref(&self) -> &Self::Target { + &self.base + } } impl<'input, Input: CharStream>> DerefMut for ReferenceToATNLexer<'input, Input> { - fn deref_mut(&mut self) -> &mut Self::Target { &mut self.base } + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.base + } } impl<'input, Input: CharStream>> ReferenceToATNLexer<'input, Input> { - fn get_rule_names(&self) -> &'static [&'static str] { &ruleNames } - fn get_literal_names(&self) -> &[Option<&str>] { &_LITERAL_NAMES } + fn get_rule_names(&self) -> &'static [&'static str] { + &ruleNames + } + fn get_literal_names(&self) -> &[Option<&str>] { + &_LITERAL_NAMES + } - fn get_symbolic_names(&self) -> &[Option<&str>] { &_SYMBOLIC_NAMES } + fn get_symbolic_names(&self) -> &[Option<&str>] { + &_SYMBOLIC_NAMES + } - fn get_grammar_file_name(&self) -> &'static str { "ReferenceToATNLexer.g4" } + fn get_grammar_file_name(&self) -> &'static str { + "ReferenceToATNLexer.g4" + } pub fn new_with_token_factory(input: Input, tf: &'input LocalTokenFactory<'input>) -> Self { - antlr_rust::recognizer::check_version("0", "2"); + antlr_rust::recognizer::check_version("0", "3"); Self { base: BaseLexer::new_base_lexer( input, @@ -136,17 +149,29 @@ impl<'input, Input: CharStream>> TokenSource<'input> { type TF = LocalTokenFactory<'input>; - fn next_token(&mut self) -> >::Tok { self.base.next_token() } + fn next_token(&mut self) -> >::Tok { + self.base.next_token() + } - fn get_line(&self) -> isize { self.base.get_line() } + fn get_line(&self) -> isize { + self.base.get_line() + } - fn get_char_position_in_line(&self) -> isize { self.base.get_char_position_in_line() } + fn get_char_position_in_line(&self) -> isize { + self.base.get_char_position_in_line() + } - fn get_input_stream(&mut self) -> Option<&mut dyn IntStream> { self.base.get_input_stream() } + fn get_input_stream(&mut self) -> Option<&mut dyn IntStream> { + self.base.get_input_stream() + } - fn get_source_name(&self) -> String { self.base.get_source_name() } + fn get_source_name(&self) -> String { + self.base.get_source_name() + } - fn get_token_factory(&self) -> &'input Self::TF { self.base.get_token_factory() } + fn get_token_factory(&self) -> &'input Self::TF { + self.base.get_token_factory() + } } lazy_static! { diff --git a/tests/gen/referencetoatnlistener.rs b/tests/gen/referencetoatnlistener.rs index 32954d6..c4208b7 100644 --- a/tests/gen/referencetoatnlistener.rs +++ b/tests/gen/referencetoatnlistener.rs @@ -17,3 +17,5 @@ pub trait ReferenceToATNListener<'input>: */ fn exit_a(&mut self, _ctx: &AContext<'input>) {} } + +antlr_rust::coerce_from! { 'input : ReferenceToATNListener<'input> } diff --git a/tests/gen/referencetoatnparser.rs b/tests/gen/referencetoatnparser.rs index bc310cf..2b72959 100644 --- a/tests/gen/referencetoatnparser.rs +++ b/tests/gen/referencetoatnparser.rs @@ -5,6 +5,7 @@ #![allow(nonstandard_style)] #![allow(unused_imports)] #![allow(unused_mut)] +#![allow(unused_braces)] use super::referencetoatnlistener::*; use antlr_rust::atn::{ATN, INVALID_ALT}; use antlr_rust::atn_deserializer::ATNDeserializer; @@ -56,7 +57,7 @@ lazy_static! { type BaseParserType<'input, I> = BaseParser< 'input, - ReferenceToATNParserExt, + ReferenceToATNParserExt<'input>, I, ReferenceToATNParserContextType, dyn ReferenceToATNListener<'input> + 'input, @@ -90,12 +91,16 @@ where I: TokenStream<'input, TF = LocalTokenFactory<'input>> + TidAble<'input>, H: ErrorStrategy<'input, BaseParserType<'input, I>>, { - pub fn get_serialized_atn() -> &'static str { _serializedATN } + pub fn get_serialized_atn() -> &'static str { + _serializedATN + } - pub fn set_error_strategy(&mut self, strategy: H) { self.err_handler = strategy } + pub fn set_error_strategy(&mut self, strategy: H) { + self.err_handler = strategy + } pub fn with_strategy(input: I, strategy: H) -> Self { - antlr_rust::recognizer::check_version("0", "2"); + antlr_rust::recognizer::check_version("0", "3"); let interpreter = Arc::new(ParserATNSimulator::new( _ATN.clone(), _decision_to_DFA.clone(), @@ -105,7 +110,9 @@ where base: BaseParser::new_base_parser( input, Arc::clone(&interpreter), - ReferenceToATNParserExt {}, + ReferenceToATNParserExt { + _pd: Default::default(), + }, ), interpreter, _shared_context_cache: Box::new(PredictionContextCache::new()), @@ -130,7 +137,9 @@ impl<'input, I> where I: TokenStream<'input, TF = LocalTokenFactory<'input>> + TidAble<'input>, { - pub fn new(input: I) -> Self { Self::with_strategy(input, DefaultErrorStrategy::new()) } + pub fn new(input: I) -> Self { + Self::with_strategy(input, DefaultErrorStrategy::new()) + } } /// Trait for monomorphized trait object that corresponds to the nodes of parse tree generated for ReferenceToATNParser @@ -139,6 +148,8 @@ pub trait ReferenceToATNParserContext<'input>: for<'x> Listenable } + impl<'input> ReferenceToATNParserContext<'input> for TerminalNode<'input, ReferenceToATNParserContextType> { @@ -148,14 +159,12 @@ impl<'input> ReferenceToATNParserContext<'input> { } -#[antlr_rust::impl_tid] -impl<'input> antlr_rust::TidAble<'input> for dyn ReferenceToATNParserContext<'input> + 'input {} +antlr_rust::tid! { impl<'input> TidAble<'input> for dyn ReferenceToATNParserContext<'input> + 'input } -#[antlr_rust::impl_tid] -impl<'input> antlr_rust::TidAble<'input> for dyn ReferenceToATNListener<'input> + 'input {} +antlr_rust::tid! { impl<'input> TidAble<'input> for dyn ReferenceToATNListener<'input> + 'input } pub struct ReferenceToATNParserContextType; -antlr_rust::type_id! {ReferenceToATNParserContextType} +antlr_rust::tid! {ReferenceToATNParserContextType} impl<'input> ParserNodeType<'input> for ReferenceToATNParserContextType { type TF = LocalTokenFactory<'input>; @@ -169,7 +178,9 @@ where { type Target = BaseParserType<'input, I>; - fn deref(&self) -> &Self::Target { &self.base } + fn deref(&self) -> &Self::Target { + &self.base + } } impl<'input, I, H> DerefMut for ReferenceToATNParser<'input, I, H> @@ -177,30 +188,41 @@ where I: TokenStream<'input, TF = LocalTokenFactory<'input>> + TidAble<'input>, H: ErrorStrategy<'input, BaseParserType<'input, I>>, { - fn deref_mut(&mut self) -> &mut Self::Target { &mut self.base } + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.base + } } -pub struct ReferenceToATNParserExt {} +pub struct ReferenceToATNParserExt<'input> { + _pd: PhantomData<&'input str>, +} -impl ReferenceToATNParserExt {} +impl<'input> ReferenceToATNParserExt<'input> {} +antlr_rust::tid! { ReferenceToATNParserExt<'a> } -impl<'input> TokenAware<'input> for ReferenceToATNParserExt { +impl<'input> TokenAware<'input> for ReferenceToATNParserExt<'input> { type TF = LocalTokenFactory<'input>; } impl<'input, I: TokenStream<'input, TF = LocalTokenFactory<'input>> + TidAble<'input>> - ParserRecog<'input, BaseParserType<'input, I>> for ReferenceToATNParserExt + ParserRecog<'input, BaseParserType<'input, I>> for ReferenceToATNParserExt<'input> { } impl<'input, I: TokenStream<'input, TF = LocalTokenFactory<'input>> + TidAble<'input>> - Actions<'input, BaseParserType<'input, I>> for ReferenceToATNParserExt + Actions<'input, BaseParserType<'input, I>> for ReferenceToATNParserExt<'input> { - fn get_grammar_file_name(&self) -> &str { "ReferenceToATN.g4" } + fn get_grammar_file_name(&self) -> &str { + "ReferenceToATN.g4" + } - fn get_rule_names(&self) -> &[&str] { &ruleNames } + fn get_rule_names(&self) -> &[&str] { + &ruleNames + } - fn get_vocabulary(&self) -> &dyn Vocabulary { &**VOCABULARY } + fn get_vocabulary(&self) -> &dyn Vocabulary { + &**VOCABULARY + } } //------------------- a ---------------- pub type AContextAll<'input> = AContext<'input>; @@ -219,15 +241,21 @@ impl<'input, 'a> Listenable + 'a> for AContex listener.enter_every_rule(self); listener.enter_a(self); } + fn exit(&self, listener: &mut (dyn ReferenceToATNListener<'input> + 'a)) { + listener.exit_a(self); + listener.exit_every_rule(self); + } } impl<'input> CustomRuleContext<'input> for AContextExt<'input> { type TF = LocalTokenFactory<'input>; type Ctx = ReferenceToATNParserContextType; - fn get_rule_index(&self) -> usize { RULE_a } + fn get_rule_index(&self) -> usize { + RULE_a + } //fn type_rule_index() -> usize where Self: Sized { RULE_a } } -antlr_rust::type_id! {AContextExt<'a>} +antlr_rust::tid! {AContextExt<'a>} impl<'input> AContextExt<'input> { fn new( @@ -290,8 +318,8 @@ where let mut _localctx = AContextExt::new(_parentctx.clone(), recog.base.get_state()); recog.base.enter_rule(_localctx.clone(), 0, RULE_a); let mut _localctx: Rc = _localctx; - let mut _la: isize; - let result: Result<(), ANTLRError> = try { + let mut _la: isize = -1; + let result: Result<(), ANTLRError> = (|| { let mut _alt: isize; //recog.base.enter_outer_alt(_localctx.clone(), 1); recog.base.enter_outer_alt(None, 1); @@ -344,7 +372,8 @@ where ) }); } - }; + Ok(()) + })(); match result { Ok(_) => {} Err(e @ ANTLRError::FallThrough(_)) => return Err(e), diff --git a/tests/gen/simplelrlexer.rs b/tests/gen/simplelrlexer.rs index a3f8ef7..2a06f23 100644 --- a/tests/gen/simplelrlexer.rs +++ b/tests/gen/simplelrlexer.rs @@ -54,31 +54,44 @@ pub type LocalTokenFactory<'input> = CommonTokenFactory; type From<'a> = as TokenFactory<'a>>::From; -#[derive(Tid)] pub struct SimpleLRLexer<'input, Input: CharStream>> { base: BaseLexer<'input, SimpleLRLexerActions, Input, LocalTokenFactory<'input>>, } +antlr_rust::tid! { impl<'input,Input> TidAble<'input> for SimpleLRLexer<'input,Input> where Input:CharStream > } + impl<'input, Input: CharStream>> Deref for SimpleLRLexer<'input, Input> { type Target = BaseLexer<'input, SimpleLRLexerActions, Input, LocalTokenFactory<'input>>; - fn deref(&self) -> &Self::Target { &self.base } + fn deref(&self) -> &Self::Target { + &self.base + } } impl<'input, Input: CharStream>> DerefMut for SimpleLRLexer<'input, Input> { - fn deref_mut(&mut self) -> &mut Self::Target { &mut self.base } + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.base + } } impl<'input, Input: CharStream>> SimpleLRLexer<'input, Input> { - fn get_rule_names(&self) -> &'static [&'static str] { &ruleNames } - fn get_literal_names(&self) -> &[Option<&str>] { &_LITERAL_NAMES } + fn get_rule_names(&self) -> &'static [&'static str] { + &ruleNames + } + fn get_literal_names(&self) -> &[Option<&str>] { + &_LITERAL_NAMES + } - fn get_symbolic_names(&self) -> &[Option<&str>] { &_SYMBOLIC_NAMES } + fn get_symbolic_names(&self) -> &[Option<&str>] { + &_SYMBOLIC_NAMES + } - fn get_grammar_file_name(&self) -> &'static str { "SimpleLRLexer.g4" } + fn get_grammar_file_name(&self) -> &'static str { + "SimpleLRLexer.g4" + } pub fn new_with_token_factory(input: Input, tf: &'input LocalTokenFactory<'input>) -> Self { - antlr_rust::recognizer::check_version("0", "2"); + antlr_rust::recognizer::check_version("0", "3"); Self { base: BaseLexer::new_base_lexer( input, @@ -130,17 +143,29 @@ impl<'input> TokenAware<'input> for SimpleLRLexerActions { impl<'input, Input: CharStream>> TokenSource<'input> for SimpleLRLexer<'input, Input> { type TF = LocalTokenFactory<'input>; - fn next_token(&mut self) -> >::Tok { self.base.next_token() } + fn next_token(&mut self) -> >::Tok { + self.base.next_token() + } - fn get_line(&self) -> isize { self.base.get_line() } + fn get_line(&self) -> isize { + self.base.get_line() + } - fn get_char_position_in_line(&self) -> isize { self.base.get_char_position_in_line() } + fn get_char_position_in_line(&self) -> isize { + self.base.get_char_position_in_line() + } - fn get_input_stream(&mut self) -> Option<&mut dyn IntStream> { self.base.get_input_stream() } + fn get_input_stream(&mut self) -> Option<&mut dyn IntStream> { + self.base.get_input_stream() + } - fn get_source_name(&self) -> String { self.base.get_source_name() } + fn get_source_name(&self) -> String { + self.base.get_source_name() + } - fn get_token_factory(&self) -> &'input Self::TF { self.base.get_token_factory() } + fn get_token_factory(&self) -> &'input Self::TF { + self.base.get_token_factory() + } } lazy_static! { diff --git a/tests/gen/simplelrlistener.rs b/tests/gen/simplelrlistener.rs index ab2b63c..89e9d5f 100644 --- a/tests/gen/simplelrlistener.rs +++ b/tests/gen/simplelrlistener.rs @@ -14,7 +14,6 @@ pub trait SimpleLRListener<'input>: ParseTreeListener<'input, SimpleLRParserCont * @param ctx the parse tree */ fn exit_s(&mut self, _ctx: &SContext<'input>) {} - /** * Enter a parse tree produced by {@link SimpleLRParser#a}. * @param ctx the parse tree @@ -26,3 +25,5 @@ pub trait SimpleLRListener<'input>: ParseTreeListener<'input, SimpleLRParserCont */ fn exit_a(&mut self, _ctx: &AContext<'input>) {} } + +antlr_rust::coerce_from! { 'input : SimpleLRListener<'input> } diff --git a/tests/gen/simplelrparser.rs b/tests/gen/simplelrparser.rs index ac75df8..9355d53 100644 --- a/tests/gen/simplelrparser.rs +++ b/tests/gen/simplelrparser.rs @@ -5,6 +5,7 @@ #![allow(nonstandard_style)] #![allow(unused_imports)] #![allow(unused_mut)] +#![allow(unused_braces)] use super::simplelrlistener::*; use antlr_rust::atn::{ATN, INVALID_ALT}; use antlr_rust::atn_deserializer::ATNDeserializer; @@ -56,7 +57,7 @@ lazy_static! { type BaseParserType<'input, I> = BaseParser< 'input, - SimpleLRParserExt, + SimpleLRParserExt<'input>, I, SimpleLRParserContextType, dyn SimpleLRListener<'input> + 'input, @@ -85,12 +86,16 @@ where I: TokenStream<'input, TF = LocalTokenFactory<'input>> + TidAble<'input>, H: ErrorStrategy<'input, BaseParserType<'input, I>>, { - pub fn get_serialized_atn() -> &'static str { _serializedATN } + pub fn get_serialized_atn() -> &'static str { + _serializedATN + } - pub fn set_error_strategy(&mut self, strategy: H) { self.err_handler = strategy } + pub fn set_error_strategy(&mut self, strategy: H) { + self.err_handler = strategy + } pub fn with_strategy(input: I, strategy: H) -> Self { - antlr_rust::recognizer::check_version("0", "2"); + antlr_rust::recognizer::check_version("0", "3"); let interpreter = Arc::new(ParserATNSimulator::new( _ATN.clone(), _decision_to_DFA.clone(), @@ -100,7 +105,9 @@ where base: BaseParser::new_base_parser( input, Arc::clone(&interpreter), - SimpleLRParserExt {}, + SimpleLRParserExt { + _pd: Default::default(), + }, ), interpreter, _shared_context_cache: Box::new(PredictionContextCache::new()), @@ -124,7 +131,9 @@ impl<'input, I> SimpleLRParser<'input, I, DefaultErrorStrategy<'input, SimpleLRP where I: TokenStream<'input, TF = LocalTokenFactory<'input>> + TidAble<'input>, { - pub fn new(input: I) -> Self { Self::with_strategy(input, DefaultErrorStrategy::new()) } + pub fn new(input: I) -> Self { + Self::with_strategy(input, DefaultErrorStrategy::new()) + } } /// Trait for monomorphized trait object that corresponds to the nodes of parse tree generated for SimpleLRParser @@ -134,17 +143,17 @@ pub trait SimpleLRParserContext<'input>: { } +antlr_rust::coerce_from! { 'input : SimpleLRParserContext<'input> } + impl<'input> SimpleLRParserContext<'input> for TerminalNode<'input, SimpleLRParserContextType> {} impl<'input> SimpleLRParserContext<'input> for ErrorNode<'input, SimpleLRParserContextType> {} -#[antlr_rust::impl_tid] -impl<'input> antlr_rust::TidAble<'input> for dyn SimpleLRParserContext<'input> + 'input {} +antlr_rust::tid! { impl<'input> TidAble<'input> for dyn SimpleLRParserContext<'input> + 'input } -#[antlr_rust::impl_tid] -impl<'input> antlr_rust::TidAble<'input> for dyn SimpleLRListener<'input> + 'input {} +antlr_rust::tid! { impl<'input> TidAble<'input> for dyn SimpleLRListener<'input> + 'input } pub struct SimpleLRParserContextType; -antlr_rust::type_id! {SimpleLRParserContextType} +antlr_rust::tid! {SimpleLRParserContextType} impl<'input> ParserNodeType<'input> for SimpleLRParserContextType { type TF = LocalTokenFactory<'input>; @@ -158,7 +167,9 @@ where { type Target = BaseParserType<'input, I>; - fn deref(&self) -> &Self::Target { &self.base } + fn deref(&self) -> &Self::Target { + &self.base + } } impl<'input, I, H> DerefMut for SimpleLRParser<'input, I, H> @@ -166,30 +177,41 @@ where I: TokenStream<'input, TF = LocalTokenFactory<'input>> + TidAble<'input>, H: ErrorStrategy<'input, BaseParserType<'input, I>>, { - fn deref_mut(&mut self) -> &mut Self::Target { &mut self.base } + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.base + } } -pub struct SimpleLRParserExt {} +pub struct SimpleLRParserExt<'input> { + _pd: PhantomData<&'input str>, +} -impl SimpleLRParserExt {} +impl<'input> SimpleLRParserExt<'input> {} +antlr_rust::tid! { SimpleLRParserExt<'a> } -impl<'input> TokenAware<'input> for SimpleLRParserExt { +impl<'input> TokenAware<'input> for SimpleLRParserExt<'input> { type TF = LocalTokenFactory<'input>; } impl<'input, I: TokenStream<'input, TF = LocalTokenFactory<'input>> + TidAble<'input>> - ParserRecog<'input, BaseParserType<'input, I>> for SimpleLRParserExt + ParserRecog<'input, BaseParserType<'input, I>> for SimpleLRParserExt<'input> { } impl<'input, I: TokenStream<'input, TF = LocalTokenFactory<'input>> + TidAble<'input>> - Actions<'input, BaseParserType<'input, I>> for SimpleLRParserExt + Actions<'input, BaseParserType<'input, I>> for SimpleLRParserExt<'input> { - fn get_grammar_file_name(&self) -> &str { "SimpleLR.g4" } + fn get_grammar_file_name(&self) -> &str { + "SimpleLR.g4" + } - fn get_rule_names(&self) -> &[&str] { &ruleNames } + fn get_rule_names(&self) -> &[&str] { + &ruleNames + } - fn get_vocabulary(&self) -> &dyn Vocabulary { &**VOCABULARY } + fn get_vocabulary(&self) -> &dyn Vocabulary { + &**VOCABULARY + } fn sempred( _localctx: Option<&(dyn SimpleLRParserContext<'input> + 'input)>, rule_index: isize, @@ -239,15 +261,21 @@ impl<'input, 'a> Listenable + 'a> for SContext<'inp listener.enter_every_rule(self); listener.enter_s(self); } + fn exit(&self, listener: &mut (dyn SimpleLRListener<'input> + 'a)) { + listener.exit_s(self); + listener.exit_every_rule(self); + } } impl<'input> CustomRuleContext<'input> for SContextExt<'input> { type TF = LocalTokenFactory<'input>; type Ctx = SimpleLRParserContextType; - fn get_rule_index(&self) -> usize { RULE_s } + fn get_rule_index(&self) -> usize { + RULE_s + } //fn type_rule_index() -> usize where Self: Sized { RULE_s } } -antlr_rust::type_id! {SContextExt<'a>} +antlr_rust::tid! {SContextExt<'a>} impl<'input> SContextExt<'input> { fn new( @@ -286,7 +314,7 @@ where let mut _localctx = SContextExt::new(_parentctx.clone(), recog.base.get_state()); recog.base.enter_rule(_localctx.clone(), 0, RULE_s); let mut _localctx: Rc = _localctx; - let result: Result<(), ANTLRError> = try { + let result: Result<(), ANTLRError> = (|| { //recog.base.enter_outer_alt(_localctx.clone(), 1); recog.base.enter_outer_alt(None, 1); { @@ -297,7 +325,8 @@ where let tmp = recog.input.lt(-1).cloned(); recog.ctx.as_ref().unwrap().set_stop(tmp); println!("test"); - }; + Ok(()) + })(); match result { Ok(_) => {} Err(e @ ANTLRError::FallThrough(_)) => return Err(e), @@ -329,15 +358,21 @@ impl<'input, 'a> Listenable + 'a> for AContext<'inp listener.enter_every_rule(self); listener.enter_a(self); } + fn exit(&self, listener: &mut (dyn SimpleLRListener<'input> + 'a)) { + listener.exit_a(self); + listener.exit_every_rule(self); + } } impl<'input> CustomRuleContext<'input> for AContextExt<'input> { type TF = LocalTokenFactory<'input>; type Ctx = SimpleLRParserContextType; - fn get_rule_index(&self) -> usize { RULE_a } + fn get_rule_index(&self) -> usize { + RULE_a + } //fn type_rule_index() -> usize where Self: Sized { RULE_a } } -antlr_rust::type_id! {AContextExt<'a>} +antlr_rust::tid! {AContextExt<'a>} impl<'input> AContextExt<'input> { fn new( @@ -378,7 +413,9 @@ where I: TokenStream<'input, TF = LocalTokenFactory<'input>> + TidAble<'input>, H: ErrorStrategy<'input, BaseParserType<'input, I>>, { - pub fn a(&mut self) -> Result>, ANTLRError> { self.a_rec(0) } + pub fn a(&mut self) -> Result>, ANTLRError> { + self.a_rec(0) + } fn a_rec(&mut self, _p: isize) -> Result>, ANTLRError> { let recog = self; @@ -391,7 +428,7 @@ where let mut _localctx: Rc = _localctx; let mut _prevctx = _localctx.clone(); let _startState = 2; - let result: Result<(), ANTLRError> = try { + let result: Result<(), ANTLRError> = (|| { let mut _alt: isize; //recog.base.enter_outer_alt(_localctx.clone(), 1); recog.base.enter_outer_alt(None, 1); @@ -434,7 +471,8 @@ where _alt = recog.interpreter.adaptive_predict(0, &mut recog.base)?; } } - }; + Ok(()) + })(); match result { Ok(_) => {} Err(e @ ANTLRError::FallThrough(_)) => return Err(e), diff --git a/tests/gen/visitorbasiclexer.rs b/tests/gen/visitorbasiclexer.rs new file mode 100644 index 0000000..4f81bd8 --- /dev/null +++ b/tests/gen/visitorbasiclexer.rs @@ -0,0 +1,191 @@ +// Generated from VisitorBasic.g4 by ANTLR 4.8 +#![allow(dead_code)] +#![allow(nonstandard_style)] +#![allow(unused_imports)] +#![allow(unused_variables)] +use antlr_rust::atn::ATN; +use antlr_rust::atn_deserializer::ATNDeserializer; +use antlr_rust::char_stream::CharStream; +use antlr_rust::dfa::DFA; +use antlr_rust::error_listener::ErrorListener; +use antlr_rust::int_stream::IntStream; +use antlr_rust::lexer::{BaseLexer, Lexer, LexerRecog}; +use antlr_rust::lexer_atn_simulator::{ILexerATNSimulator, LexerATNSimulator}; +use antlr_rust::parser_rule_context::{cast, BaseParserRuleContext, ParserRuleContext}; +use antlr_rust::recognizer::{Actions, Recognizer}; +use antlr_rust::rule_context::{BaseRuleContext, EmptyContext, EmptyCustomRuleContext}; +use antlr_rust::token::*; +use antlr_rust::token_factory::{CommonTokenFactory, TokenAware, TokenFactory}; +use antlr_rust::vocabulary::{Vocabulary, VocabularyImpl}; +use antlr_rust::PredictionContextCache; +use antlr_rust::TokenSource; + +use antlr_rust::{lazy_static, Tid, TidAble, TidExt}; + +use std::cell::RefCell; +use std::marker::PhantomData; +use std::ops::{Deref, DerefMut}; +use std::rc::Rc; +use std::sync::Arc; + +pub const A: isize = 1; +pub const channelNames: [&'static str; 0 + 2] = ["DEFAULT_TOKEN_CHANNEL", "HIDDEN"]; + +pub const modeNames: [&'static str; 1] = ["DEFAULT_MODE"]; + +pub const ruleNames: [&'static str; 1] = ["A"]; + +pub const _LITERAL_NAMES: [Option<&'static str>; 2] = [None, Some("'A'")]; +pub const _SYMBOLIC_NAMES: [Option<&'static str>; 2] = [None, Some("A")]; +lazy_static! { + static ref _shared_context_cache: Arc = + Arc::new(PredictionContextCache::new()); + static ref VOCABULARY: Box = Box::new(VocabularyImpl::new( + _LITERAL_NAMES.iter(), + _SYMBOLIC_NAMES.iter(), + None + )); +} + +pub type LexerContext<'input> = + BaseRuleContext<'input, EmptyCustomRuleContext<'input, LocalTokenFactory<'input>>>; +pub type LocalTokenFactory<'input> = CommonTokenFactory; + +type From<'a> = as TokenFactory<'a>>::From; + +pub struct VisitorBasicLexer<'input, Input: CharStream>> { + base: BaseLexer<'input, VisitorBasicLexerActions, Input, LocalTokenFactory<'input>>, +} + +antlr_rust::tid! { impl<'input,Input> TidAble<'input> for VisitorBasicLexer<'input,Input> where Input:CharStream > } + +impl<'input, Input: CharStream>> Deref for VisitorBasicLexer<'input, Input> { + type Target = BaseLexer<'input, VisitorBasicLexerActions, Input, LocalTokenFactory<'input>>; + + fn deref(&self) -> &Self::Target { + &self.base + } +} + +impl<'input, Input: CharStream>> DerefMut for VisitorBasicLexer<'input, Input> { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.base + } +} + +impl<'input, Input: CharStream>> VisitorBasicLexer<'input, Input> { + fn get_rule_names(&self) -> &'static [&'static str] { + &ruleNames + } + fn get_literal_names(&self) -> &[Option<&str>] { + &_LITERAL_NAMES + } + + fn get_symbolic_names(&self) -> &[Option<&str>] { + &_SYMBOLIC_NAMES + } + + fn get_grammar_file_name(&self) -> &'static str { + "VisitorBasicLexer.g4" + } + + pub fn new_with_token_factory(input: Input, tf: &'input LocalTokenFactory<'input>) -> Self { + antlr_rust::recognizer::check_version("0", "3"); + Self { + base: BaseLexer::new_base_lexer( + input, + LexerATNSimulator::new_lexer_atnsimulator( + _ATN.clone(), + _decision_to_DFA.clone(), + _shared_context_cache.clone(), + ), + VisitorBasicLexerActions {}, + tf, + ), + } + } +} + +impl<'input, Input: CharStream>> VisitorBasicLexer<'input, Input> +where + &'input LocalTokenFactory<'input>: Default, +{ + pub fn new(input: Input) -> Self { + VisitorBasicLexer::new_with_token_factory( + input, + <&LocalTokenFactory<'input> as Default>::default(), + ) + } +} + +pub struct VisitorBasicLexerActions {} + +impl VisitorBasicLexerActions {} + +impl<'input, Input: CharStream>> + Actions<'input, BaseLexer<'input, VisitorBasicLexerActions, Input, LocalTokenFactory<'input>>> + for VisitorBasicLexerActions +{ +} + +impl<'input, Input: CharStream>> VisitorBasicLexer<'input, Input> {} + +impl<'input, Input: CharStream>> + LexerRecog< + 'input, + BaseLexer<'input, VisitorBasicLexerActions, Input, LocalTokenFactory<'input>>, + > for VisitorBasicLexerActions +{ +} +impl<'input> TokenAware<'input> for VisitorBasicLexerActions { + type TF = LocalTokenFactory<'input>; +} + +impl<'input, Input: CharStream>> TokenSource<'input> + for VisitorBasicLexer<'input, Input> +{ + type TF = LocalTokenFactory<'input>; + + fn next_token(&mut self) -> >::Tok { + self.base.next_token() + } + + fn get_line(&self) -> isize { + self.base.get_line() + } + + fn get_char_position_in_line(&self) -> isize { + self.base.get_char_position_in_line() + } + + fn get_input_stream(&mut self) -> Option<&mut dyn IntStream> { + self.base.get_input_stream() + } + + fn get_source_name(&self) -> String { + self.base.get_source_name() + } + + fn get_token_factory(&self) -> &'input Self::TF { + self.base.get_token_factory() + } +} + +lazy_static! { + static ref _ATN: Arc = + Arc::new(ATNDeserializer::new(None).deserialize(_serializedATN.chars())); + static ref _decision_to_DFA: Arc>> = { + let mut dfa = Vec::new(); + let size = _ATN.decision_to_state.len(); + for i in 0..size { + dfa.push(DFA::new(_ATN.clone(), _ATN.get_decision_state(i), i as isize).into()) + } + Arc::new(dfa) + }; +} + +const _serializedATN: &'static str = + "\x03\u{608b}\u{a72a}\u{8133}\u{b9ed}\u{417c}\u{3be7}\u{7786}\u{5964}\x02\ + \x03\x07\x08\x01\x04\x02\x09\x02\x03\x02\x03\x02\x02\x02\x03\x03\x03\x03\ + \x02\x02\x02\x06\x02\x03\x03\x02\x02\x02\x03\x05\x03\x02\x02\x02\x05\x06\ + \x07\x43\x02\x02\x06\x04\x03\x02\x02\x02\x03\x02\x02"; diff --git a/tests/gen/visitorbasiclistener.rs b/tests/gen/visitorbasiclistener.rs new file mode 100644 index 0000000..d87b335 --- /dev/null +++ b/tests/gen/visitorbasiclistener.rs @@ -0,0 +1,21 @@ +#![allow(nonstandard_style)] +// Generated from VisitorBasic.g4 by ANTLR 4.8 +use super::visitorbasicparser::*; +use antlr_rust::tree::ParseTreeListener; + +pub trait VisitorBasicListener<'input>: + ParseTreeListener<'input, VisitorBasicParserContextType> +{ + /** + * Enter a parse tree produced by {@link VisitorBasicParser#s}. + * @param ctx the parse tree + */ + fn enter_s(&mut self, _ctx: &SContext<'input>) {} + /** + * Exit a parse tree produced by {@link VisitorBasicParser#s}. + * @param ctx the parse tree + */ + fn exit_s(&mut self, _ctx: &SContext<'input>) {} +} + +antlr_rust::coerce_from! { 'input : VisitorBasicListener<'input> } diff --git a/tests/gen/visitorbasicparser.rs b/tests/gen/visitorbasicparser.rs new file mode 100644 index 0000000..94fc461 --- /dev/null +++ b/tests/gen/visitorbasicparser.rs @@ -0,0 +1,367 @@ +// Generated from VisitorBasic.g4 by ANTLR 4.8 +#![allow(dead_code)] +#![allow(non_snake_case)] +#![allow(non_upper_case_globals)] +#![allow(nonstandard_style)] +#![allow(unused_imports)] +#![allow(unused_mut)] +#![allow(unused_braces)] +use super::visitorbasiclistener::*; +use super::visitorbasicvisitor::*; +use antlr_rust::atn::{ATN, INVALID_ALT}; +use antlr_rust::atn_deserializer::ATNDeserializer; +use antlr_rust::dfa::DFA; +use antlr_rust::error_strategy::{DefaultErrorStrategy, ErrorStrategy}; +use antlr_rust::errors::*; +use antlr_rust::int_stream::EOF; +use antlr_rust::parser::{BaseParser, Parser, ParserNodeType, ParserRecog}; +use antlr_rust::parser_atn_simulator::ParserATNSimulator; +use antlr_rust::parser_rule_context::{cast, cast_mut, BaseParserRuleContext, ParserRuleContext}; +use antlr_rust::recognizer::{Actions, Recognizer}; +use antlr_rust::rule_context::{BaseRuleContext, CustomRuleContext, RuleContext}; +use antlr_rust::token::{OwningToken, Token, TOKEN_EOF}; +use antlr_rust::token_factory::{CommonTokenFactory, TokenAware, TokenFactory}; +use antlr_rust::token_stream::TokenStream; +use antlr_rust::tree::*; +use antlr_rust::vocabulary::{Vocabulary, VocabularyImpl}; +use antlr_rust::PredictionContextCache; +use antlr_rust::TokenSource; + +use antlr_rust::lazy_static; +use antlr_rust::{TidAble, TidExt}; + +use std::any::{Any, TypeId}; +use std::borrow::{Borrow, BorrowMut}; +use std::cell::RefCell; +use std::convert::TryFrom; +use std::marker::PhantomData; +use std::ops::{Deref, DerefMut}; +use std::rc::Rc; +use std::sync::Arc; + +pub const A: isize = 1; +pub const RULE_s: usize = 0; +pub const ruleNames: [&'static str; 1] = ["s"]; + +pub const _LITERAL_NAMES: [Option<&'static str>; 2] = [None, Some("'A'")]; +pub const _SYMBOLIC_NAMES: [Option<&'static str>; 2] = [None, Some("A")]; +lazy_static! { + static ref _shared_context_cache: Arc = + Arc::new(PredictionContextCache::new()); + static ref VOCABULARY: Box = Box::new(VocabularyImpl::new( + _LITERAL_NAMES.iter(), + _SYMBOLIC_NAMES.iter(), + None + )); +} + +type BaseParserType<'input, I> = BaseParser< + 'input, + VisitorBasicParserExt<'input>, + I, + VisitorBasicParserContextType, + dyn VisitorBasicListener<'input> + 'input, +>; + +type TokenType<'input> = as TokenFactory<'input>>::Tok; +pub type LocalTokenFactory<'input> = CommonTokenFactory; + +pub type VisitorBasicTreeWalker<'input, 'a> = ParseTreeWalker< + 'input, + 'a, + VisitorBasicParserContextType, + dyn VisitorBasicListener<'input> + 'a, +>; + +/// Parser for VisitorBasic grammar +pub struct VisitorBasicParser<'input, I, H> +where + I: TokenStream<'input, TF = LocalTokenFactory<'input>> + TidAble<'input>, + H: ErrorStrategy<'input, BaseParserType<'input, I>>, +{ + base: BaseParserType<'input, I>, + interpreter: Arc, + _shared_context_cache: Box, + pub err_handler: H, +} + +impl<'input, I, H> VisitorBasicParser<'input, I, H> +where + I: TokenStream<'input, TF = LocalTokenFactory<'input>> + TidAble<'input>, + H: ErrorStrategy<'input, BaseParserType<'input, I>>, +{ + pub fn get_serialized_atn() -> &'static str { + _serializedATN + } + + pub fn set_error_strategy(&mut self, strategy: H) { + self.err_handler = strategy + } + + pub fn with_strategy(input: I, strategy: H) -> Self { + antlr_rust::recognizer::check_version("0", "3"); + let interpreter = Arc::new(ParserATNSimulator::new( + _ATN.clone(), + _decision_to_DFA.clone(), + _shared_context_cache.clone(), + )); + Self { + base: BaseParser::new_base_parser( + input, + Arc::clone(&interpreter), + VisitorBasicParserExt { + _pd: Default::default(), + }, + ), + interpreter, + _shared_context_cache: Box::new(PredictionContextCache::new()), + err_handler: strategy, + } + } +} + +type DynStrategy<'input, I> = Box> + 'input>; + +impl<'input, I> VisitorBasicParser<'input, I, DynStrategy<'input, I>> +where + I: TokenStream<'input, TF = LocalTokenFactory<'input>> + TidAble<'input>, +{ + pub fn with_dyn_strategy(input: I) -> Self { + Self::with_strategy(input, Box::new(DefaultErrorStrategy::new())) + } +} + +impl<'input, I> + VisitorBasicParser<'input, I, DefaultErrorStrategy<'input, VisitorBasicParserContextType>> +where + I: TokenStream<'input, TF = LocalTokenFactory<'input>> + TidAble<'input>, +{ + pub fn new(input: I) -> Self { + Self::with_strategy(input, DefaultErrorStrategy::new()) + } +} + +/// Trait for monomorphized trait object that corresponds to the nodes of parse tree generated for VisitorBasicParser +pub trait VisitorBasicParserContext<'input>: + for<'x> Listenable + 'x> + + for<'x> Visitable + 'x> + + ParserRuleContext<'input, TF = LocalTokenFactory<'input>, Ctx = VisitorBasicParserContextType> +{ +} + +antlr_rust::coerce_from! { 'input : VisitorBasicParserContext<'input> } + +impl<'input, 'x, T> VisitableDyn for dyn VisitorBasicParserContext<'input> + 'input +where + T: VisitorBasicVisitor<'input> + 'x, +{ + fn accept_dyn(&self, visitor: &mut T) { + self.accept(visitor as &mut (dyn VisitorBasicVisitor<'input> + 'x)) + } +} + +impl<'input> VisitorBasicParserContext<'input> + for TerminalNode<'input, VisitorBasicParserContextType> +{ +} +impl<'input> VisitorBasicParserContext<'input> + for ErrorNode<'input, VisitorBasicParserContextType> +{ +} + +antlr_rust::tid! { impl<'input> TidAble<'input> for dyn VisitorBasicParserContext<'input> + 'input } + +antlr_rust::tid! { impl<'input> TidAble<'input> for dyn VisitorBasicListener<'input> + 'input } + +pub struct VisitorBasicParserContextType; +antlr_rust::tid! {VisitorBasicParserContextType} + +impl<'input> ParserNodeType<'input> for VisitorBasicParserContextType { + type TF = LocalTokenFactory<'input>; + type Type = dyn VisitorBasicParserContext<'input> + 'input; +} + +impl<'input, I, H> Deref for VisitorBasicParser<'input, I, H> +where + I: TokenStream<'input, TF = LocalTokenFactory<'input>> + TidAble<'input>, + H: ErrorStrategy<'input, BaseParserType<'input, I>>, +{ + type Target = BaseParserType<'input, I>; + + fn deref(&self) -> &Self::Target { + &self.base + } +} + +impl<'input, I, H> DerefMut for VisitorBasicParser<'input, I, H> +where + I: TokenStream<'input, TF = LocalTokenFactory<'input>> + TidAble<'input>, + H: ErrorStrategy<'input, BaseParserType<'input, I>>, +{ + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.base + } +} + +pub struct VisitorBasicParserExt<'input> { + _pd: PhantomData<&'input str>, +} + +impl<'input> VisitorBasicParserExt<'input> {} +antlr_rust::tid! { VisitorBasicParserExt<'a> } + +impl<'input> TokenAware<'input> for VisitorBasicParserExt<'input> { + type TF = LocalTokenFactory<'input>; +} + +impl<'input, I: TokenStream<'input, TF = LocalTokenFactory<'input>> + TidAble<'input>> + ParserRecog<'input, BaseParserType<'input, I>> for VisitorBasicParserExt<'input> +{ +} + +impl<'input, I: TokenStream<'input, TF = LocalTokenFactory<'input>> + TidAble<'input>> + Actions<'input, BaseParserType<'input, I>> for VisitorBasicParserExt<'input> +{ + fn get_grammar_file_name(&self) -> &str { + "VisitorBasic.g4" + } + + fn get_rule_names(&self) -> &[&str] { + &ruleNames + } + + fn get_vocabulary(&self) -> &dyn Vocabulary { + &**VOCABULARY + } +} +//------------------- s ---------------- +pub type SContextAll<'input> = SContext<'input>; + +pub type SContext<'input> = BaseParserRuleContext<'input, SContextExt<'input>>; + +#[derive(Clone)] +pub struct SContextExt<'input> { + ph: PhantomData<&'input str>, +} + +impl<'input> VisitorBasicParserContext<'input> for SContext<'input> {} + +impl<'input, 'a> Listenable + 'a> for SContext<'input> { + fn enter(&self, listener: &mut (dyn VisitorBasicListener<'input> + 'a)) { + listener.enter_every_rule(self); + listener.enter_s(self); + } + fn exit(&self, listener: &mut (dyn VisitorBasicListener<'input> + 'a)) { + listener.exit_s(self); + listener.exit_every_rule(self); + } +} + +impl<'input, 'a> Visitable + 'a> for SContext<'input> { + fn accept(&self, visitor: &mut (dyn VisitorBasicVisitor<'input> + 'a)) { + visitor.visit_s(self); + } +} + +impl<'input> CustomRuleContext<'input> for SContextExt<'input> { + type TF = LocalTokenFactory<'input>; + type Ctx = VisitorBasicParserContextType; + fn get_rule_index(&self) -> usize { + RULE_s + } + //fn type_rule_index() -> usize where Self: Sized { RULE_s } +} +antlr_rust::tid! {SContextExt<'a>} + +impl<'input> SContextExt<'input> { + fn new( + parent: Option + 'input>>, + invoking_state: isize, + ) -> Rc> { + Rc::new(BaseParserRuleContext::new_parser_ctx( + parent, + invoking_state, + SContextExt { ph: PhantomData }, + )) + } +} + +pub trait SContextAttrs<'input>: + VisitorBasicParserContext<'input> + BorrowMut> +{ + /// Retrieves first TerminalNode corresponding to token A + /// Returns `None` if there is no child corresponding to token A + fn A(&self) -> Option>> + where + Self: Sized, + { + self.get_token(A, 0) + } + /// Retrieves first TerminalNode corresponding to token EOF + /// Returns `None` if there is no child corresponding to token EOF + fn EOF(&self) -> Option>> + where + Self: Sized, + { + self.get_token(EOF, 0) + } +} + +impl<'input> SContextAttrs<'input> for SContext<'input> {} + +impl<'input, I, H> VisitorBasicParser<'input, I, H> +where + I: TokenStream<'input, TF = LocalTokenFactory<'input>> + TidAble<'input>, + H: ErrorStrategy<'input, BaseParserType<'input, I>>, +{ + pub fn s(&mut self) -> Result>, ANTLRError> { + let mut recog = self; + let _parentctx = recog.ctx.take(); + let mut _localctx = SContextExt::new(_parentctx.clone(), recog.base.get_state()); + recog.base.enter_rule(_localctx.clone(), 0, RULE_s); + let mut _localctx: Rc = _localctx; + let result: Result<(), ANTLRError> = (|| { + //recog.base.enter_outer_alt(_localctx.clone(), 1); + recog.base.enter_outer_alt(None, 1); + { + recog.base.set_state(2); + recog.base.match_token(A, &mut recog.err_handler)?; + + recog.base.set_state(3); + recog.base.match_token(EOF, &mut recog.err_handler)?; + } + Ok(()) + })(); + match result { + Ok(_) => {} + Err(e @ ANTLRError::FallThrough(_)) => return Err(e), + Err(ref re) => { + //_localctx.exception = re; + recog.err_handler.report_error(&mut recog.base, re); + recog.err_handler.recover(&mut recog.base, re)?; + } + } + recog.base.exit_rule(); + + Ok(_localctx) + } +} + +lazy_static! { + static ref _ATN: Arc = + Arc::new(ATNDeserializer::new(None).deserialize(_serializedATN.chars())); + static ref _decision_to_DFA: Arc>> = { + let mut dfa = Vec::new(); + let size = _ATN.decision_to_state.len(); + for i in 0..size { + dfa.push(DFA::new(_ATN.clone(), _ATN.get_decision_state(i), i as isize).into()) + } + Arc::new(dfa) + }; +} + +const _serializedATN: &'static str = + "\x03\u{608b}\u{a72a}\u{8133}\u{b9ed}\u{417c}\u{3be7}\u{7786}\u{5964}\x03\ + \x03\x08\x04\x02\x09\x02\x03\x02\x03\x02\x03\x02\x03\x02\x02\x02\x03\x02\ + \x02\x02\x02\x06\x02\x04\x03\x02\x02\x02\x04\x05\x07\x03\x02\x02\x05\x06\ + \x07\x02\x02\x03\x06\x03\x03\x02\x02\x02\x02"; diff --git a/tests/gen/visitorbasicvisitor.rs b/tests/gen/visitorbasicvisitor.rs new file mode 100644 index 0000000..9c8d027 --- /dev/null +++ b/tests/gen/visitorbasicvisitor.rs @@ -0,0 +1,42 @@ +#![allow(nonstandard_style)] +// Generated from VisitorBasic.g4 by ANTLR 4.8 +use super::visitorbasicparser::*; +use antlr_rust::tree::{ParseTreeVisitor, ParseTreeVisitorCompat}; + +/** + * This interface defines a complete generic visitor for a parse tree produced + * by {@link VisitorBasicParser}. + */ +pub trait VisitorBasicVisitor<'input>: + ParseTreeVisitor<'input, VisitorBasicParserContextType> +{ + /** + * Visit a parse tree produced by {@link VisitorBasicParser#s}. + * @param ctx the parse tree + */ + fn visit_s(&mut self, ctx: &SContext<'input>) { + self.visit_children(ctx) + } +} + +pub trait VisitorBasicVisitorCompat<'input>: + ParseTreeVisitorCompat<'input, Node = VisitorBasicParserContextType> +{ + /** + * Visit a parse tree produced by {@link VisitorBasicParser#s}. + * @param ctx the parse tree + */ + fn visit_s(&mut self, ctx: &SContext<'input>) -> Self::Return { + self.visit_children(ctx) + } +} + +impl<'input, T> VisitorBasicVisitor<'input> for T +where + T: VisitorBasicVisitorCompat<'input>, +{ + fn visit_s(&mut self, ctx: &SContext<'input>) { + let result = ::visit_s(self, ctx); + *::temp_result(self) = result; + } +} diff --git a/tests/gen/visitorcalclexer.rs b/tests/gen/visitorcalclexer.rs new file mode 100644 index 0000000..b4dc3a7 --- /dev/null +++ b/tests/gen/visitorcalclexer.rs @@ -0,0 +1,224 @@ +// Generated from VisitorCalc.g4 by ANTLR 4.8 +#![allow(dead_code)] +#![allow(nonstandard_style)] +#![allow(unused_imports)] +#![allow(unused_variables)] +use antlr_rust::atn::ATN; +use antlr_rust::atn_deserializer::ATNDeserializer; +use antlr_rust::char_stream::CharStream; +use antlr_rust::dfa::DFA; +use antlr_rust::error_listener::ErrorListener; +use antlr_rust::int_stream::IntStream; +use antlr_rust::lexer::{BaseLexer, Lexer, LexerRecog}; +use antlr_rust::lexer_atn_simulator::{ILexerATNSimulator, LexerATNSimulator}; +use antlr_rust::parser_rule_context::{cast, BaseParserRuleContext, ParserRuleContext}; +use antlr_rust::recognizer::{Actions, Recognizer}; +use antlr_rust::rule_context::{BaseRuleContext, EmptyContext, EmptyCustomRuleContext}; +use antlr_rust::token::*; +use antlr_rust::token_factory::{CommonTokenFactory, TokenAware, TokenFactory}; +use antlr_rust::vocabulary::{Vocabulary, VocabularyImpl}; +use antlr_rust::PredictionContextCache; +use antlr_rust::TokenSource; + +use antlr_rust::{lazy_static, Tid, TidAble, TidExt}; + +use std::cell::RefCell; +use std::marker::PhantomData; +use std::ops::{Deref, DerefMut}; +use std::rc::Rc; +use std::sync::Arc; + +pub const INT: isize = 1; +pub const MUL: isize = 2; +pub const DIV: isize = 3; +pub const ADD: isize = 4; +pub const SUB: isize = 5; +pub const WS: isize = 6; +pub const channelNames: [&'static str; 0 + 2] = ["DEFAULT_TOKEN_CHANNEL", "HIDDEN"]; + +pub const modeNames: [&'static str; 1] = ["DEFAULT_MODE"]; + +pub const ruleNames: [&'static str; 6] = ["INT", "MUL", "DIV", "ADD", "SUB", "WS"]; + +pub const _LITERAL_NAMES: [Option<&'static str>; 6] = [ + None, + None, + Some("'*'"), + Some("'/'"), + Some("'+'"), + Some("'-'"), +]; +pub const _SYMBOLIC_NAMES: [Option<&'static str>; 7] = [ + None, + Some("INT"), + Some("MUL"), + Some("DIV"), + Some("ADD"), + Some("SUB"), + Some("WS"), +]; +lazy_static! { + static ref _shared_context_cache: Arc = + Arc::new(PredictionContextCache::new()); + static ref VOCABULARY: Box = Box::new(VocabularyImpl::new( + _LITERAL_NAMES.iter(), + _SYMBOLIC_NAMES.iter(), + None + )); +} + +pub type LexerContext<'input> = + BaseRuleContext<'input, EmptyCustomRuleContext<'input, LocalTokenFactory<'input>>>; +pub type LocalTokenFactory<'input> = CommonTokenFactory; + +type From<'a> = as TokenFactory<'a>>::From; + +pub struct VisitorCalcLexer<'input, Input: CharStream>> { + base: BaseLexer<'input, VisitorCalcLexerActions, Input, LocalTokenFactory<'input>>, +} + +antlr_rust::tid! { impl<'input,Input> TidAble<'input> for VisitorCalcLexer<'input,Input> where Input:CharStream > } + +impl<'input, Input: CharStream>> Deref for VisitorCalcLexer<'input, Input> { + type Target = BaseLexer<'input, VisitorCalcLexerActions, Input, LocalTokenFactory<'input>>; + + fn deref(&self) -> &Self::Target { + &self.base + } +} + +impl<'input, Input: CharStream>> DerefMut for VisitorCalcLexer<'input, Input> { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.base + } +} + +impl<'input, Input: CharStream>> VisitorCalcLexer<'input, Input> { + fn get_rule_names(&self) -> &'static [&'static str] { + &ruleNames + } + fn get_literal_names(&self) -> &[Option<&str>] { + &_LITERAL_NAMES + } + + fn get_symbolic_names(&self) -> &[Option<&str>] { + &_SYMBOLIC_NAMES + } + + fn get_grammar_file_name(&self) -> &'static str { + "VisitorCalcLexer.g4" + } + + pub fn new_with_token_factory(input: Input, tf: &'input LocalTokenFactory<'input>) -> Self { + antlr_rust::recognizer::check_version("0", "3"); + Self { + base: BaseLexer::new_base_lexer( + input, + LexerATNSimulator::new_lexer_atnsimulator( + _ATN.clone(), + _decision_to_DFA.clone(), + _shared_context_cache.clone(), + ), + VisitorCalcLexerActions {}, + tf, + ), + } + } +} + +impl<'input, Input: CharStream>> VisitorCalcLexer<'input, Input> +where + &'input LocalTokenFactory<'input>: Default, +{ + pub fn new(input: Input) -> Self { + VisitorCalcLexer::new_with_token_factory( + input, + <&LocalTokenFactory<'input> as Default>::default(), + ) + } +} + +pub struct VisitorCalcLexerActions {} + +impl VisitorCalcLexerActions {} + +impl<'input, Input: CharStream>> + Actions<'input, BaseLexer<'input, VisitorCalcLexerActions, Input, LocalTokenFactory<'input>>> + for VisitorCalcLexerActions +{ +} + +impl<'input, Input: CharStream>> VisitorCalcLexer<'input, Input> {} + +impl<'input, Input: CharStream>> + LexerRecog<'input, BaseLexer<'input, VisitorCalcLexerActions, Input, LocalTokenFactory<'input>>> + for VisitorCalcLexerActions +{ +} +impl<'input> TokenAware<'input> for VisitorCalcLexerActions { + type TF = LocalTokenFactory<'input>; +} + +impl<'input, Input: CharStream>> TokenSource<'input> + for VisitorCalcLexer<'input, Input> +{ + type TF = LocalTokenFactory<'input>; + + fn next_token(&mut self) -> >::Tok { + self.base.next_token() + } + + fn get_line(&self) -> isize { + self.base.get_line() + } + + fn get_char_position_in_line(&self) -> isize { + self.base.get_char_position_in_line() + } + + fn get_input_stream(&mut self) -> Option<&mut dyn IntStream> { + self.base.get_input_stream() + } + + fn get_source_name(&self) -> String { + self.base.get_source_name() + } + + fn get_token_factory(&self) -> &'input Self::TF { + self.base.get_token_factory() + } +} + +lazy_static! { + static ref _ATN: Arc = + Arc::new(ATNDeserializer::new(None).deserialize(_serializedATN.chars())); + static ref _decision_to_DFA: Arc>> = { + let mut dfa = Vec::new(); + let size = _ATN.decision_to_state.len(); + for i in 0..size { + dfa.push(DFA::new(_ATN.clone(), _ATN.get_decision_state(i), i as isize).into()) + } + Arc::new(dfa) + }; +} + +const _serializedATN: &'static str = + "\x03\u{608b}\u{a72a}\u{8133}\u{b9ed}\u{417c}\u{3be7}\u{7786}\u{5964}\x02\ + \x08\x23\x08\x01\x04\x02\x09\x02\x04\x03\x09\x03\x04\x04\x09\x04\x04\x05\ + \x09\x05\x04\x06\x09\x06\x04\x07\x09\x07\x03\x02\x06\x02\x11\x0a\x02\x0d\ + \x02\x0e\x02\x12\x03\x03\x03\x03\x03\x04\x03\x04\x03\x05\x03\x05\x03\x06\ + \x03\x06\x03\x07\x06\x07\x1e\x0a\x07\x0d\x07\x0e\x07\x1f\x03\x07\x03\x07\ + \x02\x02\x08\x03\x03\x05\x04\x07\x05\x09\x06\x0b\x07\x0d\x08\x03\x02\x04\ + \x03\x02\x32\x3b\x04\x02\x0b\x0b\x22\x22\x02\x24\x02\x03\x03\x02\x02\x02\ + \x02\x05\x03\x02\x02\x02\x02\x07\x03\x02\x02\x02\x02\x09\x03\x02\x02\x02\ + \x02\x0b\x03\x02\x02\x02\x02\x0d\x03\x02\x02\x02\x03\x10\x03\x02\x02\x02\ + \x05\x14\x03\x02\x02\x02\x07\x16\x03\x02\x02\x02\x09\x18\x03\x02\x02\x02\ + \x0b\x1a\x03\x02\x02\x02\x0d\x1d\x03\x02\x02\x02\x0f\x11\x09\x02\x02\x02\ + \x10\x0f\x03\x02\x02\x02\x11\x12\x03\x02\x02\x02\x12\x10\x03\x02\x02\x02\ + \x12\x13\x03\x02\x02\x02\x13\x04\x03\x02\x02\x02\x14\x15\x07\x2c\x02\x02\ + \x15\x06\x03\x02\x02\x02\x16\x17\x07\x31\x02\x02\x17\x08\x03\x02\x02\x02\ + \x18\x19\x07\x2d\x02\x02\x19\x0a\x03\x02\x02\x02\x1a\x1b\x07\x2f\x02\x02\ + \x1b\x0c\x03\x02\x02\x02\x1c\x1e\x09\x03\x02\x02\x1d\x1c\x03\x02\x02\x02\ + \x1e\x1f\x03\x02\x02\x02\x1f\x1d\x03\x02\x02\x02\x1f\x20\x03\x02\x02\x02\ + \x20\x21\x03\x02\x02\x02\x21\x22\x08\x07\x02\x02\x22\x0e\x03\x02\x02\x02\ + \x05\x02\x12\x1f\x03\x02\x03\x02"; diff --git a/tests/gen/visitorcalclistener.rs b/tests/gen/visitorcalclistener.rs new file mode 100644 index 0000000..9d95352 --- /dev/null +++ b/tests/gen/visitorcalclistener.rs @@ -0,0 +1,57 @@ +#![allow(nonstandard_style)] +// Generated from VisitorCalc.g4 by ANTLR 4.8 +use super::visitorcalcparser::*; +use antlr_rust::tree::ParseTreeListener; + +pub trait VisitorCalcListener<'input>: + ParseTreeListener<'input, VisitorCalcParserContextType> +{ + /** + * Enter a parse tree produced by {@link VisitorCalcParser#s}. + * @param ctx the parse tree + */ + fn enter_s(&mut self, _ctx: &SContext<'input>) {} + /** + * Exit a parse tree produced by {@link VisitorCalcParser#s}. + * @param ctx the parse tree + */ + fn exit_s(&mut self, _ctx: &SContext<'input>) {} + /** + * Enter a parse tree produced by the {@code add} + * labeled alternative in {@link VisitorCalcParser#expr}. + * @param ctx the parse tree + */ + fn enter_add(&mut self, _ctx: &AddContext<'input>) {} + /** + * Exit a parse tree produced by the {@code add} + * labeled alternative in {@link VisitorCalcParser#expr}. + * @param ctx the parse tree + */ + fn exit_add(&mut self, _ctx: &AddContext<'input>) {} + /** + * Enter a parse tree produced by the {@code number} + * labeled alternative in {@link VisitorCalcParser#expr}. + * @param ctx the parse tree + */ + fn enter_number(&mut self, _ctx: &NumberContext<'input>) {} + /** + * Exit a parse tree produced by the {@code number} + * labeled alternative in {@link VisitorCalcParser#expr}. + * @param ctx the parse tree + */ + fn exit_number(&mut self, _ctx: &NumberContext<'input>) {} + /** + * Enter a parse tree produced by the {@code multiply} + * labeled alternative in {@link VisitorCalcParser#expr}. + * @param ctx the parse tree + */ + fn enter_multiply(&mut self, _ctx: &MultiplyContext<'input>) {} + /** + * Exit a parse tree produced by the {@code multiply} + * labeled alternative in {@link VisitorCalcParser#expr}. + * @param ctx the parse tree + */ + fn exit_multiply(&mut self, _ctx: &MultiplyContext<'input>) {} +} + +antlr_rust::coerce_from! { 'input : VisitorCalcListener<'input> } diff --git a/tests/gen/visitorcalcparser.rs b/tests/gen/visitorcalcparser.rs new file mode 100644 index 0000000..c354274 --- /dev/null +++ b/tests/gen/visitorcalcparser.rs @@ -0,0 +1,927 @@ +// Generated from VisitorCalc.g4 by ANTLR 4.8 +#![allow(dead_code)] +#![allow(non_snake_case)] +#![allow(non_upper_case_globals)] +#![allow(nonstandard_style)] +#![allow(unused_imports)] +#![allow(unused_mut)] +#![allow(unused_braces)] +use super::visitorcalclistener::*; +use super::visitorcalcvisitor::*; +use antlr_rust::atn::{ATN, INVALID_ALT}; +use antlr_rust::atn_deserializer::ATNDeserializer; +use antlr_rust::dfa::DFA; +use antlr_rust::error_strategy::{DefaultErrorStrategy, ErrorStrategy}; +use antlr_rust::errors::*; +use antlr_rust::int_stream::EOF; +use antlr_rust::parser::{BaseParser, Parser, ParserNodeType, ParserRecog}; +use antlr_rust::parser_atn_simulator::ParserATNSimulator; +use antlr_rust::parser_rule_context::{cast, cast_mut, BaseParserRuleContext, ParserRuleContext}; +use antlr_rust::recognizer::{Actions, Recognizer}; +use antlr_rust::rule_context::{BaseRuleContext, CustomRuleContext, RuleContext}; +use antlr_rust::token::{OwningToken, Token, TOKEN_EOF}; +use antlr_rust::token_factory::{CommonTokenFactory, TokenAware, TokenFactory}; +use antlr_rust::token_stream::TokenStream; +use antlr_rust::tree::*; +use antlr_rust::vocabulary::{Vocabulary, VocabularyImpl}; +use antlr_rust::PredictionContextCache; +use antlr_rust::TokenSource; + +use antlr_rust::lazy_static; +use antlr_rust::{TidAble, TidExt}; + +use std::any::{Any, TypeId}; +use std::borrow::{Borrow, BorrowMut}; +use std::cell::RefCell; +use std::convert::TryFrom; +use std::marker::PhantomData; +use std::ops::{Deref, DerefMut}; +use std::rc::Rc; +use std::sync::Arc; + +pub const INT: isize = 1; +pub const MUL: isize = 2; +pub const DIV: isize = 3; +pub const ADD: isize = 4; +pub const SUB: isize = 5; +pub const WS: isize = 6; +pub const RULE_s: usize = 0; +pub const RULE_expr: usize = 1; +pub const ruleNames: [&'static str; 2] = ["s", "expr"]; + +pub const _LITERAL_NAMES: [Option<&'static str>; 6] = [ + None, + None, + Some("'*'"), + Some("'/'"), + Some("'+'"), + Some("'-'"), +]; +pub const _SYMBOLIC_NAMES: [Option<&'static str>; 7] = [ + None, + Some("INT"), + Some("MUL"), + Some("DIV"), + Some("ADD"), + Some("SUB"), + Some("WS"), +]; +lazy_static! { + static ref _shared_context_cache: Arc = + Arc::new(PredictionContextCache::new()); + static ref VOCABULARY: Box = Box::new(VocabularyImpl::new( + _LITERAL_NAMES.iter(), + _SYMBOLIC_NAMES.iter(), + None + )); +} + +type BaseParserType<'input, I> = BaseParser< + 'input, + VisitorCalcParserExt<'input>, + I, + VisitorCalcParserContextType, + dyn VisitorCalcListener<'input> + 'input, +>; + +type TokenType<'input> = as TokenFactory<'input>>::Tok; +pub type LocalTokenFactory<'input> = CommonTokenFactory; + +pub type VisitorCalcTreeWalker<'input, 'a> = + ParseTreeWalker<'input, 'a, VisitorCalcParserContextType, dyn VisitorCalcListener<'input> + 'a>; + +/// Parser for VisitorCalc grammar +pub struct VisitorCalcParser<'input, I, H> +where + I: TokenStream<'input, TF = LocalTokenFactory<'input>> + TidAble<'input>, + H: ErrorStrategy<'input, BaseParserType<'input, I>>, +{ + base: BaseParserType<'input, I>, + interpreter: Arc, + _shared_context_cache: Box, + pub err_handler: H, +} + +impl<'input, I, H> VisitorCalcParser<'input, I, H> +where + I: TokenStream<'input, TF = LocalTokenFactory<'input>> + TidAble<'input>, + H: ErrorStrategy<'input, BaseParserType<'input, I>>, +{ + pub fn get_serialized_atn() -> &'static str { + _serializedATN + } + + pub fn set_error_strategy(&mut self, strategy: H) { + self.err_handler = strategy + } + + pub fn with_strategy(input: I, strategy: H) -> Self { + antlr_rust::recognizer::check_version("0", "3"); + let interpreter = Arc::new(ParserATNSimulator::new( + _ATN.clone(), + _decision_to_DFA.clone(), + _shared_context_cache.clone(), + )); + Self { + base: BaseParser::new_base_parser( + input, + Arc::clone(&interpreter), + VisitorCalcParserExt { + _pd: Default::default(), + }, + ), + interpreter, + _shared_context_cache: Box::new(PredictionContextCache::new()), + err_handler: strategy, + } + } +} + +type DynStrategy<'input, I> = Box> + 'input>; + +impl<'input, I> VisitorCalcParser<'input, I, DynStrategy<'input, I>> +where + I: TokenStream<'input, TF = LocalTokenFactory<'input>> + TidAble<'input>, +{ + pub fn with_dyn_strategy(input: I) -> Self { + Self::with_strategy(input, Box::new(DefaultErrorStrategy::new())) + } +} + +impl<'input, I> + VisitorCalcParser<'input, I, DefaultErrorStrategy<'input, VisitorCalcParserContextType>> +where + I: TokenStream<'input, TF = LocalTokenFactory<'input>> + TidAble<'input>, +{ + pub fn new(input: I) -> Self { + Self::with_strategy(input, DefaultErrorStrategy::new()) + } +} + +/// Trait for monomorphized trait object that corresponds to the nodes of parse tree generated for VisitorCalcParser +pub trait VisitorCalcParserContext<'input>: + for<'x> Listenable + 'x> + + for<'x> Visitable + 'x> + + ParserRuleContext<'input, TF = LocalTokenFactory<'input>, Ctx = VisitorCalcParserContextType> +{ +} + +antlr_rust::coerce_from! { 'input : VisitorCalcParserContext<'input> } + +impl<'input, 'x, T> VisitableDyn for dyn VisitorCalcParserContext<'input> + 'input +where + T: VisitorCalcVisitor<'input> + 'x, +{ + fn accept_dyn(&self, visitor: &mut T) { + self.accept(visitor as &mut (dyn VisitorCalcVisitor<'input> + 'x)) + } +} + +impl<'input> VisitorCalcParserContext<'input> + for TerminalNode<'input, VisitorCalcParserContextType> +{ +} +impl<'input> VisitorCalcParserContext<'input> for ErrorNode<'input, VisitorCalcParserContextType> {} + +antlr_rust::tid! { impl<'input> TidAble<'input> for dyn VisitorCalcParserContext<'input> + 'input } + +antlr_rust::tid! { impl<'input> TidAble<'input> for dyn VisitorCalcListener<'input> + 'input } + +pub struct VisitorCalcParserContextType; +antlr_rust::tid! {VisitorCalcParserContextType} + +impl<'input> ParserNodeType<'input> for VisitorCalcParserContextType { + type TF = LocalTokenFactory<'input>; + type Type = dyn VisitorCalcParserContext<'input> + 'input; +} + +impl<'input, I, H> Deref for VisitorCalcParser<'input, I, H> +where + I: TokenStream<'input, TF = LocalTokenFactory<'input>> + TidAble<'input>, + H: ErrorStrategy<'input, BaseParserType<'input, I>>, +{ + type Target = BaseParserType<'input, I>; + + fn deref(&self) -> &Self::Target { + &self.base + } +} + +impl<'input, I, H> DerefMut for VisitorCalcParser<'input, I, H> +where + I: TokenStream<'input, TF = LocalTokenFactory<'input>> + TidAble<'input>, + H: ErrorStrategy<'input, BaseParserType<'input, I>>, +{ + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.base + } +} + +pub struct VisitorCalcParserExt<'input> { + _pd: PhantomData<&'input str>, +} + +impl<'input> VisitorCalcParserExt<'input> {} +antlr_rust::tid! { VisitorCalcParserExt<'a> } + +impl<'input> TokenAware<'input> for VisitorCalcParserExt<'input> { + type TF = LocalTokenFactory<'input>; +} + +impl<'input, I: TokenStream<'input, TF = LocalTokenFactory<'input>> + TidAble<'input>> + ParserRecog<'input, BaseParserType<'input, I>> for VisitorCalcParserExt<'input> +{ +} + +impl<'input, I: TokenStream<'input, TF = LocalTokenFactory<'input>> + TidAble<'input>> + Actions<'input, BaseParserType<'input, I>> for VisitorCalcParserExt<'input> +{ + fn get_grammar_file_name(&self) -> &str { + "VisitorCalc.g4" + } + + fn get_rule_names(&self) -> &[&str] { + &ruleNames + } + + fn get_vocabulary(&self) -> &dyn Vocabulary { + &**VOCABULARY + } + fn sempred( + _localctx: Option<&(dyn VisitorCalcParserContext<'input> + 'input)>, + rule_index: isize, + pred_index: isize, + recog: &mut BaseParserType<'input, I>, + ) -> bool { + match rule_index { + 1 => VisitorCalcParser::<'input, I, _>::expr_sempred( + _localctx.and_then(|x| x.downcast_ref()), + pred_index, + recog, + ), + _ => true, + } + } +} + +impl<'input, I> + VisitorCalcParser<'input, I, DefaultErrorStrategy<'input, VisitorCalcParserContextType>> +where + I: TokenStream<'input, TF = LocalTokenFactory<'input>> + TidAble<'input>, +{ + fn expr_sempred( + _localctx: Option<&ExprContext<'input>>, + pred_index: isize, + recog: &mut ::Target, + ) -> bool { + match pred_index { + 0 => recog.precpred(None, 2), + 1 => recog.precpred(None, 1), + _ => true, + } + } +} +//------------------- s ---------------- +pub type SContextAll<'input> = SContext<'input>; + +pub type SContext<'input> = BaseParserRuleContext<'input, SContextExt<'input>>; + +#[derive(Clone)] +pub struct SContextExt<'input> { + ph: PhantomData<&'input str>, +} + +impl<'input> VisitorCalcParserContext<'input> for SContext<'input> {} + +impl<'input, 'a> Listenable + 'a> for SContext<'input> { + fn enter(&self, listener: &mut (dyn VisitorCalcListener<'input> + 'a)) { + listener.enter_every_rule(self); + listener.enter_s(self); + } + fn exit(&self, listener: &mut (dyn VisitorCalcListener<'input> + 'a)) { + listener.exit_s(self); + listener.exit_every_rule(self); + } +} + +impl<'input, 'a> Visitable + 'a> for SContext<'input> { + fn accept(&self, visitor: &mut (dyn VisitorCalcVisitor<'input> + 'a)) { + visitor.visit_s(self); + } +} + +impl<'input> CustomRuleContext<'input> for SContextExt<'input> { + type TF = LocalTokenFactory<'input>; + type Ctx = VisitorCalcParserContextType; + fn get_rule_index(&self) -> usize { + RULE_s + } + //fn type_rule_index() -> usize where Self: Sized { RULE_s } +} +antlr_rust::tid! {SContextExt<'a>} + +impl<'input> SContextExt<'input> { + fn new( + parent: Option + 'input>>, + invoking_state: isize, + ) -> Rc> { + Rc::new(BaseParserRuleContext::new_parser_ctx( + parent, + invoking_state, + SContextExt { ph: PhantomData }, + )) + } +} + +pub trait SContextAttrs<'input>: + VisitorCalcParserContext<'input> + BorrowMut> +{ + fn expr(&self) -> Option>> + where + Self: Sized, + { + self.child_of_type(0) + } + /// Retrieves first TerminalNode corresponding to token EOF + /// Returns `None` if there is no child corresponding to token EOF + fn EOF(&self) -> Option>> + where + Self: Sized, + { + self.get_token(EOF, 0) + } +} + +impl<'input> SContextAttrs<'input> for SContext<'input> {} + +impl<'input, I, H> VisitorCalcParser<'input, I, H> +where + I: TokenStream<'input, TF = LocalTokenFactory<'input>> + TidAble<'input>, + H: ErrorStrategy<'input, BaseParserType<'input, I>>, +{ + pub fn s(&mut self) -> Result>, ANTLRError> { + let mut recog = self; + let _parentctx = recog.ctx.take(); + let mut _localctx = SContextExt::new(_parentctx.clone(), recog.base.get_state()); + recog.base.enter_rule(_localctx.clone(), 0, RULE_s); + let mut _localctx: Rc = _localctx; + let result: Result<(), ANTLRError> = (|| { + //recog.base.enter_outer_alt(_localctx.clone(), 1); + recog.base.enter_outer_alt(None, 1); + { + /*InvokeRule expr*/ + recog.base.set_state(4); + recog.expr_rec(0)?; + + recog.base.set_state(5); + recog.base.match_token(EOF, &mut recog.err_handler)?; + } + Ok(()) + })(); + match result { + Ok(_) => {} + Err(e @ ANTLRError::FallThrough(_)) => return Err(e), + Err(ref re) => { + //_localctx.exception = re; + recog.err_handler.report_error(&mut recog.base, re); + recog.err_handler.recover(&mut recog.base, re)?; + } + } + recog.base.exit_rule(); + + Ok(_localctx) + } +} +//------------------- expr ---------------- +#[derive(Debug)] +pub enum ExprContextAll<'input> { + AddContext(AddContext<'input>), + NumberContext(NumberContext<'input>), + MultiplyContext(MultiplyContext<'input>), + Error(ExprContext<'input>), +} +antlr_rust::tid! {ExprContextAll<'a>} + +impl<'input> antlr_rust::parser_rule_context::DerefSeal for ExprContextAll<'input> {} + +impl<'input> VisitorCalcParserContext<'input> for ExprContextAll<'input> {} + +impl<'input> Deref for ExprContextAll<'input> { + type Target = dyn ExprContextAttrs<'input> + 'input; + fn deref(&self) -> &Self::Target { + use ExprContextAll::*; + match self { + AddContext(inner) => inner, + NumberContext(inner) => inner, + MultiplyContext(inner) => inner, + Error(inner) => inner, + } + } +} +impl<'input, 'a> Visitable + 'a> for ExprContextAll<'input> { + fn accept(&self, visitor: &mut (dyn VisitorCalcVisitor<'input> + 'a)) { + self.deref().accept(visitor) + } +} +impl<'input, 'a> Listenable + 'a> for ExprContextAll<'input> { + fn enter(&self, listener: &mut (dyn VisitorCalcListener<'input> + 'a)) { + self.deref().enter(listener) + } + fn exit(&self, listener: &mut (dyn VisitorCalcListener<'input> + 'a)) { + self.deref().exit(listener) + } +} + +pub type ExprContext<'input> = BaseParserRuleContext<'input, ExprContextExt<'input>>; + +#[derive(Clone)] +pub struct ExprContextExt<'input> { + ph: PhantomData<&'input str>, +} + +impl<'input> VisitorCalcParserContext<'input> for ExprContext<'input> {} + +impl<'input, 'a> Listenable + 'a> for ExprContext<'input> {} + +impl<'input, 'a> Visitable + 'a> for ExprContext<'input> {} + +impl<'input> CustomRuleContext<'input> for ExprContextExt<'input> { + type TF = LocalTokenFactory<'input>; + type Ctx = VisitorCalcParserContextType; + fn get_rule_index(&self) -> usize { + RULE_expr + } + //fn type_rule_index() -> usize where Self: Sized { RULE_expr } +} +antlr_rust::tid! {ExprContextExt<'a>} + +impl<'input> ExprContextExt<'input> { + fn new( + parent: Option + 'input>>, + invoking_state: isize, + ) -> Rc> { + Rc::new(ExprContextAll::Error( + BaseParserRuleContext::new_parser_ctx( + parent, + invoking_state, + ExprContextExt { ph: PhantomData }, + ), + )) + } +} + +pub trait ExprContextAttrs<'input>: + VisitorCalcParserContext<'input> + BorrowMut> +{ +} + +impl<'input> ExprContextAttrs<'input> for ExprContext<'input> {} + +pub type AddContext<'input> = BaseParserRuleContext<'input, AddContextExt<'input>>; + +pub trait AddContextAttrs<'input>: VisitorCalcParserContext<'input> { + fn expr_all(&self) -> Vec>> + where + Self: Sized, + { + self.children_of_type() + } + fn expr(&self, i: usize) -> Option>> + where + Self: Sized, + { + self.child_of_type(i) + } + /// Retrieves first TerminalNode corresponding to token ADD + /// Returns `None` if there is no child corresponding to token ADD + fn ADD(&self) -> Option>> + where + Self: Sized, + { + self.get_token(ADD, 0) + } + /// Retrieves first TerminalNode corresponding to token SUB + /// Returns `None` if there is no child corresponding to token SUB + fn SUB(&self) -> Option>> + where + Self: Sized, + { + self.get_token(SUB, 0) + } +} + +impl<'input> AddContextAttrs<'input> for AddContext<'input> {} + +pub struct AddContextExt<'input> { + base: ExprContextExt<'input>, + ph: PhantomData<&'input str>, +} + +antlr_rust::tid! {AddContextExt<'a>} + +impl<'input> VisitorCalcParserContext<'input> for AddContext<'input> {} + +impl<'input, 'a> Listenable + 'a> for AddContext<'input> { + fn enter(&self, listener: &mut (dyn VisitorCalcListener<'input> + 'a)) { + listener.enter_every_rule(self); + listener.enter_add(self); + } + fn exit(&self, listener: &mut (dyn VisitorCalcListener<'input> + 'a)) { + listener.exit_add(self); + listener.exit_every_rule(self); + } +} + +impl<'input, 'a> Visitable + 'a> for AddContext<'input> { + fn accept(&self, visitor: &mut (dyn VisitorCalcVisitor<'input> + 'a)) { + visitor.visit_add(self); + } +} + +impl<'input> CustomRuleContext<'input> for AddContextExt<'input> { + type TF = LocalTokenFactory<'input>; + type Ctx = VisitorCalcParserContextType; + fn get_rule_index(&self) -> usize { + RULE_expr + } + //fn type_rule_index() -> usize where Self: Sized { RULE_expr } +} + +impl<'input> Borrow> for AddContext<'input> { + fn borrow(&self) -> &ExprContextExt<'input> { + &self.base + } +} +impl<'input> BorrowMut> for AddContext<'input> { + fn borrow_mut(&mut self) -> &mut ExprContextExt<'input> { + &mut self.base + } +} + +impl<'input> ExprContextAttrs<'input> for AddContext<'input> {} + +impl<'input> AddContextExt<'input> { + fn new(ctx: &dyn ExprContextAttrs<'input>) -> Rc> { + Rc::new(ExprContextAll::AddContext( + BaseParserRuleContext::copy_from( + ctx, + AddContextExt { + base: ctx.borrow().clone(), + ph: PhantomData, + }, + ), + )) + } +} + +pub type NumberContext<'input> = BaseParserRuleContext<'input, NumberContextExt<'input>>; + +pub trait NumberContextAttrs<'input>: VisitorCalcParserContext<'input> { + /// Retrieves first TerminalNode corresponding to token INT + /// Returns `None` if there is no child corresponding to token INT + fn INT(&self) -> Option>> + where + Self: Sized, + { + self.get_token(INT, 0) + } +} + +impl<'input> NumberContextAttrs<'input> for NumberContext<'input> {} + +pub struct NumberContextExt<'input> { + base: ExprContextExt<'input>, + ph: PhantomData<&'input str>, +} + +antlr_rust::tid! {NumberContextExt<'a>} + +impl<'input> VisitorCalcParserContext<'input> for NumberContext<'input> {} + +impl<'input, 'a> Listenable + 'a> for NumberContext<'input> { + fn enter(&self, listener: &mut (dyn VisitorCalcListener<'input> + 'a)) { + listener.enter_every_rule(self); + listener.enter_number(self); + } + fn exit(&self, listener: &mut (dyn VisitorCalcListener<'input> + 'a)) { + listener.exit_number(self); + listener.exit_every_rule(self); + } +} + +impl<'input, 'a> Visitable + 'a> for NumberContext<'input> { + fn accept(&self, visitor: &mut (dyn VisitorCalcVisitor<'input> + 'a)) { + visitor.visit_number(self); + } +} + +impl<'input> CustomRuleContext<'input> for NumberContextExt<'input> { + type TF = LocalTokenFactory<'input>; + type Ctx = VisitorCalcParserContextType; + fn get_rule_index(&self) -> usize { + RULE_expr + } + //fn type_rule_index() -> usize where Self: Sized { RULE_expr } +} + +impl<'input> Borrow> for NumberContext<'input> { + fn borrow(&self) -> &ExprContextExt<'input> { + &self.base + } +} +impl<'input> BorrowMut> for NumberContext<'input> { + fn borrow_mut(&mut self) -> &mut ExprContextExt<'input> { + &mut self.base + } +} + +impl<'input> ExprContextAttrs<'input> for NumberContext<'input> {} + +impl<'input> NumberContextExt<'input> { + fn new(ctx: &dyn ExprContextAttrs<'input>) -> Rc> { + Rc::new(ExprContextAll::NumberContext( + BaseParserRuleContext::copy_from( + ctx, + NumberContextExt { + base: ctx.borrow().clone(), + ph: PhantomData, + }, + ), + )) + } +} + +pub type MultiplyContext<'input> = BaseParserRuleContext<'input, MultiplyContextExt<'input>>; + +pub trait MultiplyContextAttrs<'input>: VisitorCalcParserContext<'input> { + fn expr_all(&self) -> Vec>> + where + Self: Sized, + { + self.children_of_type() + } + fn expr(&self, i: usize) -> Option>> + where + Self: Sized, + { + self.child_of_type(i) + } + /// Retrieves first TerminalNode corresponding to token MUL + /// Returns `None` if there is no child corresponding to token MUL + fn MUL(&self) -> Option>> + where + Self: Sized, + { + self.get_token(MUL, 0) + } + /// Retrieves first TerminalNode corresponding to token DIV + /// Returns `None` if there is no child corresponding to token DIV + fn DIV(&self) -> Option>> + where + Self: Sized, + { + self.get_token(DIV, 0) + } +} + +impl<'input> MultiplyContextAttrs<'input> for MultiplyContext<'input> {} + +pub struct MultiplyContextExt<'input> { + base: ExprContextExt<'input>, + ph: PhantomData<&'input str>, +} + +antlr_rust::tid! {MultiplyContextExt<'a>} + +impl<'input> VisitorCalcParserContext<'input> for MultiplyContext<'input> {} + +impl<'input, 'a> Listenable + 'a> for MultiplyContext<'input> { + fn enter(&self, listener: &mut (dyn VisitorCalcListener<'input> + 'a)) { + listener.enter_every_rule(self); + listener.enter_multiply(self); + } + fn exit(&self, listener: &mut (dyn VisitorCalcListener<'input> + 'a)) { + listener.exit_multiply(self); + listener.exit_every_rule(self); + } +} + +impl<'input, 'a> Visitable + 'a> for MultiplyContext<'input> { + fn accept(&self, visitor: &mut (dyn VisitorCalcVisitor<'input> + 'a)) { + visitor.visit_multiply(self); + } +} + +impl<'input> CustomRuleContext<'input> for MultiplyContextExt<'input> { + type TF = LocalTokenFactory<'input>; + type Ctx = VisitorCalcParserContextType; + fn get_rule_index(&self) -> usize { + RULE_expr + } + //fn type_rule_index() -> usize where Self: Sized { RULE_expr } +} + +impl<'input> Borrow> for MultiplyContext<'input> { + fn borrow(&self) -> &ExprContextExt<'input> { + &self.base + } +} +impl<'input> BorrowMut> for MultiplyContext<'input> { + fn borrow_mut(&mut self) -> &mut ExprContextExt<'input> { + &mut self.base + } +} + +impl<'input> ExprContextAttrs<'input> for MultiplyContext<'input> {} + +impl<'input> MultiplyContextExt<'input> { + fn new(ctx: &dyn ExprContextAttrs<'input>) -> Rc> { + Rc::new(ExprContextAll::MultiplyContext( + BaseParserRuleContext::copy_from( + ctx, + MultiplyContextExt { + base: ctx.borrow().clone(), + ph: PhantomData, + }, + ), + )) + } +} + +impl<'input, I, H> VisitorCalcParser<'input, I, H> +where + I: TokenStream<'input, TF = LocalTokenFactory<'input>> + TidAble<'input>, + H: ErrorStrategy<'input, BaseParserType<'input, I>>, +{ + pub fn expr(&mut self) -> Result>, ANTLRError> { + self.expr_rec(0) + } + + fn expr_rec(&mut self, _p: isize) -> Result>, ANTLRError> { + let recog = self; + let _parentctx = recog.ctx.take(); + let _parentState = recog.base.get_state(); + let mut _localctx = ExprContextExt::new(_parentctx.clone(), recog.base.get_state()); + recog + .base + .enter_recursion_rule(_localctx.clone(), 2, RULE_expr, _p); + let mut _localctx: Rc = _localctx; + let mut _prevctx = _localctx.clone(); + let _startState = 2; + let mut _la: isize = -1; + let result: Result<(), ANTLRError> = (|| { + let mut _alt: isize; + //recog.base.enter_outer_alt(_localctx.clone(), 1); + recog.base.enter_outer_alt(None, 1); + { + { + let mut tmp = NumberContextExt::new(&**_localctx); + recog.ctx = Some(tmp.clone()); + _localctx = tmp; + _prevctx = _localctx.clone(); + + recog.base.set_state(8); + recog.base.match_token(INT, &mut recog.err_handler)?; + } + + let tmp = recog.input.lt(-1).cloned(); + recog.ctx.as_ref().unwrap().set_stop(tmp); + recog.base.set_state(18); + recog.err_handler.sync(&mut recog.base)?; + _alt = recog.interpreter.adaptive_predict(1, &mut recog.base)?; + while { _alt != 2 && _alt != INVALID_ALT } { + if _alt == 1 { + recog.trigger_exit_rule_event(); + _prevctx = _localctx.clone(); + { + recog.base.set_state(16); + recog.err_handler.sync(&mut recog.base)?; + match recog.interpreter.adaptive_predict(0, &mut recog.base)? { + 1 => { + { + /*recRuleLabeledAltStartAction*/ + let mut tmp = + MultiplyContextExt::new(&**ExprContextExt::new( + _parentctx.clone(), + _parentState, + )); + recog.push_new_recursion_context( + tmp.clone(), + _startState, + RULE_expr, + ); + _localctx = tmp; + recog.base.set_state(10); + if !({ recog.precpred(None, 2) }) { + Err(FailedPredicateError::new( + &mut recog.base, + Some("recog.precpred(None, 2)".to_owned()), + None, + ))?; + } + recog.base.set_state(11); + _la = recog.base.input.la(1); + if { !(_la == MUL || _la == DIV) } { + recog.err_handler.recover_inline(&mut recog.base)?; + } else { + if recog.base.input.la(1) == TOKEN_EOF { + recog.base.matched_eof = true + }; + recog.err_handler.report_match(&mut recog.base); + recog.base.consume(&mut recog.err_handler); + } + /*InvokeRule expr*/ + recog.base.set_state(12); + recog.expr_rec(3)?; + } + } + 2 => { + { + /*recRuleLabeledAltStartAction*/ + let mut tmp = AddContextExt::new(&**ExprContextExt::new( + _parentctx.clone(), + _parentState, + )); + recog.push_new_recursion_context( + tmp.clone(), + _startState, + RULE_expr, + ); + _localctx = tmp; + recog.base.set_state(13); + if !({ recog.precpred(None, 1) }) { + Err(FailedPredicateError::new( + &mut recog.base, + Some("recog.precpred(None, 1)".to_owned()), + None, + ))?; + } + recog.base.set_state(14); + _la = recog.base.input.la(1); + if { !(_la == ADD || _la == SUB) } { + recog.err_handler.recover_inline(&mut recog.base)?; + } else { + if recog.base.input.la(1) == TOKEN_EOF { + recog.base.matched_eof = true + }; + recog.err_handler.report_match(&mut recog.base); + recog.base.consume(&mut recog.err_handler); + } + /*InvokeRule expr*/ + recog.base.set_state(15); + recog.expr_rec(2)?; + } + } + + _ => {} + } + } + } + recog.base.set_state(20); + recog.err_handler.sync(&mut recog.base)?; + _alt = recog.interpreter.adaptive_predict(1, &mut recog.base)?; + } + } + Ok(()) + })(); + match result { + Ok(_) => {} + Err(e @ ANTLRError::FallThrough(_)) => return Err(e), + Err(ref re) => { + //_localctx.exception = re; + recog.err_handler.report_error(&mut recog.base, re); + recog.err_handler.recover(&mut recog.base, re)?; + } + } + recog.base.unroll_recursion_context(_parentctx); + + Ok(_localctx) + } +} + +lazy_static! { + static ref _ATN: Arc = + Arc::new(ATNDeserializer::new(None).deserialize(_serializedATN.chars())); + static ref _decision_to_DFA: Arc>> = { + let mut dfa = Vec::new(); + let size = _ATN.decision_to_state.len(); + for i in 0..size { + dfa.push(DFA::new(_ATN.clone(), _ATN.get_decision_state(i), i as isize).into()) + } + Arc::new(dfa) + }; +} + +const _serializedATN: &'static str = + "\x03\u{608b}\u{a72a}\u{8133}\u{b9ed}\u{417c}\u{3be7}\u{7786}\u{5964}\x03\ + \x08\x18\x04\x02\x09\x02\x04\x03\x09\x03\x03\x02\x03\x02\x03\x02\x03\x03\ + \x03\x03\x03\x03\x03\x03\x03\x03\x03\x03\x03\x03\x03\x03\x03\x03\x07\x03\ + \x13\x0a\x03\x0c\x03\x0e\x03\x16\x0b\x03\x03\x03\x02\x03\x04\x04\x02\x04\ + \x02\x04\x03\x02\x04\x05\x03\x02\x06\x07\x02\x17\x02\x06\x03\x02\x02\x02\ + \x04\x09\x03\x02\x02\x02\x06\x07\x05\x04\x03\x02\x07\x08\x07\x02\x02\x03\ + \x08\x03\x03\x02\x02\x02\x09\x0a\x08\x03\x01\x02\x0a\x0b\x07\x03\x02\x02\ + \x0b\x14\x03\x02\x02\x02\x0c\x0d\x0c\x04\x02\x02\x0d\x0e\x09\x02\x02\x02\ + \x0e\x13\x05\x04\x03\x05\x0f\x10\x0c\x03\x02\x02\x10\x11\x09\x03\x02\x02\ + \x11\x13\x05\x04\x03\x04\x12\x0c\x03\x02\x02\x02\x12\x0f\x03\x02\x02\x02\ + \x13\x16\x03\x02\x02\x02\x14\x12\x03\x02\x02\x02\x14\x15\x03\x02\x02\x02\ + \x15\x05\x03\x02\x02\x02\x16\x14\x03\x02\x02\x02\x04\x12\x14"; diff --git a/tests/gen/visitorcalcvisitor.rs b/tests/gen/visitorcalcvisitor.rs new file mode 100644 index 0000000..be022c2 --- /dev/null +++ b/tests/gen/visitorcalcvisitor.rs @@ -0,0 +1,111 @@ +#![allow(nonstandard_style)] +// Generated from VisitorCalc.g4 by ANTLR 4.8 +use super::visitorcalcparser::*; +use antlr_rust::tree::{ParseTreeVisitor, ParseTreeVisitorCompat}; + +/** + * This interface defines a complete generic visitor for a parse tree produced + * by {@link VisitorCalcParser}. + */ +pub trait VisitorCalcVisitor<'input>: + ParseTreeVisitor<'input, VisitorCalcParserContextType> +{ + /** + * Visit a parse tree produced by {@link VisitorCalcParser#s}. + * @param ctx the parse tree + */ + fn visit_s(&mut self, ctx: &SContext<'input>) { + self.visit_children(ctx) + } + + /** + * Visit a parse tree produced by the {@code add} + * labeled alternative in {@link VisitorCalcParser#expr}. + * @param ctx the parse tree + */ + fn visit_add(&mut self, ctx: &AddContext<'input>) { + self.visit_children(ctx) + } + + /** + * Visit a parse tree produced by the {@code number} + * labeled alternative in {@link VisitorCalcParser#expr}. + * @param ctx the parse tree + */ + fn visit_number(&mut self, ctx: &NumberContext<'input>) { + self.visit_children(ctx) + } + + /** + * Visit a parse tree produced by the {@code multiply} + * labeled alternative in {@link VisitorCalcParser#expr}. + * @param ctx the parse tree + */ + fn visit_multiply(&mut self, ctx: &MultiplyContext<'input>) { + self.visit_children(ctx) + } +} + +pub trait VisitorCalcVisitorCompat<'input>: + ParseTreeVisitorCompat<'input, Node = VisitorCalcParserContextType> +{ + /** + * Visit a parse tree produced by {@link VisitorCalcParser#s}. + * @param ctx the parse tree + */ + fn visit_s(&mut self, ctx: &SContext<'input>) -> Self::Return { + self.visit_children(ctx) + } + + /** + * Visit a parse tree produced by the {@code add} + * labeled alternative in {@link VisitorCalcParser#expr}. + * @param ctx the parse tree + */ + fn visit_add(&mut self, ctx: &AddContext<'input>) -> Self::Return { + self.visit_children(ctx) + } + + /** + * Visit a parse tree produced by the {@code number} + * labeled alternative in {@link VisitorCalcParser#expr}. + * @param ctx the parse tree + */ + fn visit_number(&mut self, ctx: &NumberContext<'input>) -> Self::Return { + self.visit_children(ctx) + } + + /** + * Visit a parse tree produced by the {@code multiply} + * labeled alternative in {@link VisitorCalcParser#expr}. + * @param ctx the parse tree + */ + fn visit_multiply(&mut self, ctx: &MultiplyContext<'input>) -> Self::Return { + self.visit_children(ctx) + } +} + +impl<'input, T> VisitorCalcVisitor<'input> for T +where + T: VisitorCalcVisitorCompat<'input>, +{ + fn visit_s(&mut self, ctx: &SContext<'input>) { + let result = ::visit_s(self, ctx); + *::temp_result(self) = result; + } + + fn visit_add(&mut self, ctx: &AddContext<'input>) { + let result = ::visit_add(self, ctx); + *::temp_result(self) = result; + } + + fn visit_number(&mut self, ctx: &NumberContext<'input>) { + let result = ::visit_number(self, ctx); + *::temp_result(self) = result; + } + + fn visit_multiply(&mut self, ctx: &MultiplyContext<'input>) { + let result = ::visit_multiply(self, ctx); + *::temp_result(self) = result; + } +} diff --git a/tests/gen/xmllexer.rs b/tests/gen/xmllexer.rs index 44cbc43..3d73ded 100644 --- a/tests/gen/xmllexer.rs +++ b/tests/gen/xmllexer.rs @@ -133,31 +133,44 @@ pub type LocalTokenFactory<'input> = CommonTokenFactory; type From<'a> = as TokenFactory<'a>>::From; -#[derive(Tid)] pub struct XMLLexer<'input, Input: CharStream>> { base: BaseLexer<'input, XMLLexerActions, Input, LocalTokenFactory<'input>>, } +antlr_rust::tid! { impl<'input,Input> TidAble<'input> for XMLLexer<'input,Input> where Input:CharStream > } + impl<'input, Input: CharStream>> Deref for XMLLexer<'input, Input> { type Target = BaseLexer<'input, XMLLexerActions, Input, LocalTokenFactory<'input>>; - fn deref(&self) -> &Self::Target { &self.base } + fn deref(&self) -> &Self::Target { + &self.base + } } impl<'input, Input: CharStream>> DerefMut for XMLLexer<'input, Input> { - fn deref_mut(&mut self) -> &mut Self::Target { &mut self.base } + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.base + } } impl<'input, Input: CharStream>> XMLLexer<'input, Input> { - fn get_rule_names(&self) -> &'static [&'static str] { &ruleNames } - fn get_literal_names(&self) -> &[Option<&str>] { &_LITERAL_NAMES } + fn get_rule_names(&self) -> &'static [&'static str] { + &ruleNames + } + fn get_literal_names(&self) -> &[Option<&str>] { + &_LITERAL_NAMES + } - fn get_symbolic_names(&self) -> &[Option<&str>] { &_SYMBOLIC_NAMES } + fn get_symbolic_names(&self) -> &[Option<&str>] { + &_SYMBOLIC_NAMES + } - fn get_grammar_file_name(&self) -> &'static str { "XMLLexer.g4" } + fn get_grammar_file_name(&self) -> &'static str { + "XMLLexer.g4" + } pub fn new_with_token_factory(input: Input, tf: &'input LocalTokenFactory<'input>) -> Self { - antlr_rust::recognizer::check_version("0", "2"); + antlr_rust::recognizer::check_version("0", "3"); Self { base: BaseLexer::new_base_lexer( input, @@ -252,17 +265,29 @@ impl<'input> TokenAware<'input> for XMLLexerActions { impl<'input, Input: CharStream>> TokenSource<'input> for XMLLexer<'input, Input> { type TF = LocalTokenFactory<'input>; - fn next_token(&mut self) -> >::Tok { self.base.next_token() } + fn next_token(&mut self) -> >::Tok { + self.base.next_token() + } - fn get_line(&self) -> isize { self.base.get_line() } + fn get_line(&self) -> isize { + self.base.get_line() + } - fn get_char_position_in_line(&self) -> isize { self.base.get_char_position_in_line() } + fn get_char_position_in_line(&self) -> isize { + self.base.get_char_position_in_line() + } - fn get_input_stream(&mut self) -> Option<&mut dyn IntStream> { self.base.get_input_stream() } + fn get_input_stream(&mut self) -> Option<&mut dyn IntStream> { + self.base.get_input_stream() + } - fn get_source_name(&self) -> String { self.base.get_source_name() } + fn get_source_name(&self) -> String { + self.base.get_source_name() + } - fn get_token_factory(&self) -> &'input Self::TF { self.base.get_token_factory() } + fn get_token_factory(&self) -> &'input Self::TF { + self.base.get_token_factory() + } } lazy_static! { diff --git a/tests/my_test.rs b/tests/general_tests.rs similarity index 86% rename from tests/my_test.rs rename to tests/general_tests.rs index 7c79311..e841b03 100644 --- a/tests/my_test.rs +++ b/tests/general_tests.rs @@ -1,7 +1,7 @@ -#![feature(try_blocks)] -#![feature(inner_deref)] -#![feature(specialization)] -#![feature(coerce_unsized)] +// #![feature(try_blocks)] +// #![feature(inner_deref)] +// #![feature(specialization)] +// #![feature(coerce_unsized)] //! Integration tests // #[macro_use] @@ -20,8 +20,8 @@ mod gen { use antlr_rust::token_factory::{ArenaCommonFactory, OwningTokenFactory}; use antlr_rust::token_stream::{TokenStream, UnbufferedTokenStream}; use antlr_rust::tree::{ - ParseTree, ParseTreeListener, ParseTreeVisitor, ParseTreeWalker, TerminalNode, Tree, - VisitChildren, Visitable, + ParseTree, ParseTreeListener, ParseTreeVisitor, ParseTreeVisitorCompat, ParseTreeWalker, + TerminalNode, Tree, VisitChildren, Visitable, }; use antlr_rust::InputStream; use csvlexer::*; @@ -57,6 +57,10 @@ mod gen { mod simplelrlexer; mod simplelrlistener; mod simplelrparser; + mod visitorcalclexer; + mod visitorcalclistener; + mod visitorcalcparser; + mod visitorcalcvisitor; mod xmllexer; fn test_static(_arg: T) {} @@ -348,52 +352,4 @@ if (x < x && a > 0) then duh _ => panic!("oops"), } } - - struct MyCSVVisitor<'i, T>(Vec<&'i str>, T); - - impl<'i, T> ParseTreeVisitor<'i, CSVParserContextType> for MyCSVVisitor<'i, T> { - fn visit_terminal(&mut self, node: &TerminalNode<'i, CSVParserContextType>) { - if node.symbol.get_token_type() == csvparser::TEXT { - if let Cow::Borrowed(s) = node.symbol.text { - self.0.push(s); - } - } - } - } - - use csvparser::RowContextAttrs; - use std::borrow::Cow; - use std::rc::Rc; - - impl<'i, T> CSVVisitor<'i> for MyCSVVisitor<'i, T> { - fn visit_hdr(&mut self, _ctx: &HdrContext<'i>) {} - - fn visit_row(&mut self, ctx: &RowContext<'i>) { - if ctx.field_all().len() > 1 { - self.visit_children(ctx) - } - } - } - - // tests zero-copy parsing with non static visitor - #[test] - fn test_visitor() { - fn parse<'a>(tf: &'a ArenaCommonFactory<'a>) -> Rc> { - let mut _lexer = - CSVLexer::new_with_token_factory(InputStream::new("h1,h2\nd1,d2\nd3\n".into()), tf); - let token_source = CommonTokenStream::new(_lexer); - let mut parser = CSVParser::new(token_source); - let result = parser.csvFile().expect("parsed unsuccessfully"); - - let mut test = 5; - let mut visitor = MyCSVVisitor(Vec::new(), &mut test); - result.accept(&mut visitor); - assert_eq!(visitor.0, vec!["d1", "d2"]); - - result - } - let tf = ArenaCommonFactory::default(); - - let _result = parse(&tf); - } } diff --git a/tests/perf.rs b/tests/perf.rs index 101b6ab..4a1f0f9 100644 --- a/tests/perf.rs +++ b/tests/perf.rs @@ -1,48 +1,48 @@ -#![feature(try_blocks)] -#![feature(inner_deref)] -#![feature(test)] +// #![feature(try_blocks)] +// #![feature(inner_deref)] +// #![feature(test)] #[macro_use] extern crate lazy_static; -extern crate test; +// extern crate test; -mod gen { - use test::Bencher; - - // use crate::gen::perflexer::PerfLexer; - // use crate::gen::perfparser::PerfParser; - // mod perflexer; - // mod perfparser; - // mod perflistener; - - // #[cfg(not(debug_assertions))] - // #[test] - // fn performance(){ - #[ignore] - // #[bench] - fn performance(_b: &mut Bencher) { - // b.iter(|| { - // let lexer = PerfLexer::new(Box::new(InputStream::new(input.to_string()))); - // let source = CommonTokenStream::new(lexer); - // let mut parser = PerfParser::new(Box::new(source)); - // let result = parser.stat().expect("oops"); - // }); - } - - const input: &str = "\ - between X1 and X2 or between X3 and X4 and - between X1 and X2 or between X3 and X4 and - between X1 and X2 or between X3 and X4 and - between X1 and X2 or between X3 and X4 and - between X1 and X2 or between X3 and X4 and - between X1 and X2 or between X3 and X4 and - between X1 and X2 or between X3 and X4 and - between X1 and X2 or between X3 and X4 and - between X1 and X2 or between X3 and X4 and - between X1 and X2 or between X3 and X4 and - between X1 and X2 or between X3 and X4 and - between X1 and X2 or between X3 and X4 and - between X1 and X2 or between X3 and X4 and - between X1 and X2 or between X3 and X4 and - between X1 and X2 or between X3 and X4 - ;"; -} +// mod gen { +// use test::Bencher; +// +// // use crate::gen::perflexer::PerfLexer; +// // use crate::gen::perfparser::PerfParser; +// // mod perflexer; +// // mod perfparser; +// // mod perflistener; +// +// // #[cfg(not(debug_assertions))] +// // #[test] +// // fn performance(){ +// #[ignore] +// // #[bench] +// fn performance(_b: &mut Bencher) { +// // b.iter(|| { +// // let lexer = PerfLexer::new(Box::new(InputStream::new(input.to_string()))); +// // let source = CommonTokenStream::new(lexer); +// // let mut parser = PerfParser::new(Box::new(source)); +// // let result = parser.stat().expect("oops"); +// // }); +// } +// +// const input: &str = "\ +// between X1 and X2 or between X3 and X4 and +// between X1 and X2 or between X3 and X4 and +// between X1 and X2 or between X3 and X4 and +// between X1 and X2 or between X3 and X4 and +// between X1 and X2 or between X3 and X4 and +// between X1 and X2 or between X3 and X4 and +// between X1 and X2 or between X3 and X4 and +// between X1 and X2 or between X3 and X4 and +// between X1 and X2 or between X3 and X4 and +// between X1 and X2 or between X3 and X4 and +// between X1 and X2 or between X3 and X4 and +// between X1 and X2 or between X3 and X4 and +// between X1 and X2 or between X3 and X4 and +// between X1 and X2 or between X3 and X4 and +// between X1 and X2 or between X3 and X4 +// ;"; +// } diff --git a/tests/visitors_tests.rs b/tests/visitors_tests.rs new file mode 100644 index 0000000..4a49945 --- /dev/null +++ b/tests/visitors_tests.rs @@ -0,0 +1,385 @@ +mod gen { + mod csvlexer; + mod csvlistener; + mod csvparser; + mod csvvisitor; + mod visitorbasiclexer; + mod visitorbasiclistener; + mod visitorbasicparser; + mod visitorbasicvisitor; + mod visitorcalclexer; + mod visitorcalclistener; + mod visitorcalcparser; + mod visitorcalcvisitor; + + use crate::gen::csvparser::CSVParserContextType; + use crate::gen::visitorbasiclexer::VisitorBasicLexer; + use crate::gen::visitorbasicparser::{VisitorBasicParser, VisitorBasicParserContextType}; + use crate::gen::visitorbasicvisitor::VisitorBasicVisitorCompat; + use crate::gen::visitorcalclexer::VisitorCalcLexer; + use antlr_rust::common_token_stream::CommonTokenStream; + use antlr_rust::parser::ParserNodeType; + use antlr_rust::token::Token; + use antlr_rust::tree::{ErrorNode, ParseTree, ParseTreeVisitorCompat, TerminalNode, Visitable}; + use antlr_rust::InputStream; + use visitorcalcparser::{ + AddContext, AddContextAttrs, MultiplyContext, MultiplyContextAttrs, NumberContext, + NumberContextAttrs, SContext, SContextAttrs, VisitorCalcParser, + VisitorCalcParserContextType, + }; + use visitorcalcvisitor::VisitorCalcVisitorCompat; + + #[test] + fn test_visit_terminal_node() { + let mut lexer = VisitorBasicLexer::new(InputStream::new("A".into())); + let mut parser = VisitorBasicParser::new(CommonTokenStream::new(lexer)); + + let root = parser.s().unwrap(); + assert_eq!("(s A )", root.to_string_tree(&*parser)); + + struct TestVisitor(String); + impl ParseTreeVisitorCompat<'_> for TestVisitor { + type Node = VisitorBasicParserContextType; + type Return = String; + + fn temp_result(&mut self) -> &mut Self::Return { + &mut self.0 + } + + fn visit_terminal(&mut self, _node: &TerminalNode<'_, Self::Node>) -> Self::Return { + _node.symbol.to_string() + "\n" + } + + fn aggregate_results( + &self, + aggregate: Self::Return, + next: Self::Return, + ) -> Self::Return { + aggregate + &next + } + } + impl VisitorBasicVisitorCompat<'_> for TestVisitor {} + + let result = TestVisitor(String::new()).visit(&*root); + let expected = "[@0,0:0='A',<1>,1:0]\n\ + [@1,1:0='',<-1>,1:1]\n"; + assert_eq!(result, expected) + } + + #[test] + fn test_visit_error_node() { + let mut lexer = VisitorBasicLexer::new(InputStream::new("".into())); + let mut parser = VisitorBasicParser::new(CommonTokenStream::new(lexer)); + + let root = parser.s().unwrap(); + assert_eq!("(s )", root.to_string_tree(&*parser)); + + struct TestVisitor(String); + impl ParseTreeVisitorCompat<'_> for TestVisitor { + type Node = VisitorBasicParserContextType; + type Return = String; + + fn temp_result(&mut self) -> &mut Self::Return { + &mut self.0 + } + + fn visit_error_node(&mut self, _node: &ErrorNode<'_, Self::Node>) -> Self::Return { + format!("Error encountered: {}", _node.symbol) + } + + fn aggregate_results( + &self, + aggregate: Self::Return, + next: Self::Return, + ) -> Self::Return { + aggregate + &next + } + } + impl VisitorBasicVisitorCompat<'_> for TestVisitor {} + + let result = TestVisitor(String::new()).visit(&*root); + let expected = "Error encountered: [@-1,-1:-1='',<1>,1:0]"; + assert_eq!(result, expected) + } + + #[test] + fn test_should_not_visit_EOF() { + let mut lexer = VisitorBasicLexer::new(InputStream::new("A".into())); + let mut parser = VisitorBasicParser::new(CommonTokenStream::new(lexer)); + + let root = parser.s().unwrap(); + assert_eq!("(s A )", root.to_string_tree(&*parser)); + + struct TestVisitor(String); + impl ParseTreeVisitorCompat<'_> for TestVisitor { + type Node = VisitorBasicParserContextType; + type Return = String; + + fn temp_result(&mut self) -> &mut Self::Return { + &mut self.0 + } + + fn visit_terminal(&mut self, _node: &TerminalNode<'_, Self::Node>) -> Self::Return { + _node.symbol.to_string() + "\n" + } + + fn should_visit_next_child( + &self, + node: &>::Type, + current: &Self::Return, + ) -> bool { + current.is_empty() + } + } + impl VisitorBasicVisitorCompat<'_> for TestVisitor {} + + let result = TestVisitor(String::new()).visit(&*root); + let expected = "[@0,0:0='A',<1>,1:0]\n"; + assert_eq!(result, expected); + + struct TestVisitorUnit(String); + impl ParseTreeVisitorCompat<'_> for TestVisitorUnit { + type Node = VisitorBasicParserContextType; + type Return = (); + + fn temp_result(&mut self) -> &mut Self::Return { + Box::leak(Box::new(())) + } + + fn visit_terminal(&mut self, _node: &TerminalNode<'_, Self::Node>) -> Self::Return { + self.0 += &_node.symbol.to_string(); + } + } + impl VisitorBasicVisitorCompat<'_> for TestVisitorUnit {} + } + + #[test] + fn test_should_not_visit_anything() { + let mut lexer = VisitorBasicLexer::new(InputStream::new("A".into())); + let mut parser = VisitorBasicParser::new(CommonTokenStream::new(lexer)); + + let root = parser.s().unwrap(); + assert_eq!("(s A )", root.to_string_tree(&*parser)); + + struct TestVisitor(String); + impl ParseTreeVisitorCompat<'_> for TestVisitor { + type Node = VisitorBasicParserContextType; + type Return = String; + + fn temp_result(&mut self) -> &mut Self::Return { + &mut self.0 + } + + fn visit_terminal(&mut self, _node: &TerminalNode<'_, Self::Node>) -> Self::Return { + unreachable!() + } + + fn should_visit_next_child( + &self, + node: &>::Type, + current: &Self::Return, + ) -> bool { + false + } + } + impl VisitorBasicVisitorCompat<'_> for TestVisitor {} + + let result = TestVisitor(String::new()).visit(&*root); + let expected = ""; + assert_eq!(result, expected) + } + + #[test] + fn test_visitor_with_return() { + struct CalcVisitor(isize); + + impl ParseTreeVisitorCompat<'_> for CalcVisitor { + type Node = VisitorCalcParserContextType; + type Return = isize; + + fn temp_result(&mut self) -> &mut Self::Return { + &mut self.0 + } + + fn aggregate_results( + &self, + aggregate: Self::Return, + next: Self::Return, + ) -> Self::Return { + panic!("Should not be reachable") + } + } + + impl VisitorCalcVisitorCompat<'_> for CalcVisitor { + fn visit_s(&mut self, ctx: &SContext<'_>) -> Self::Return { + self.visit(&*ctx.expr().unwrap()) + } + + fn visit_add(&mut self, ctx: &AddContext<'_>) -> Self::Return { + let left = self.visit(&*ctx.expr(0).unwrap()); + let right = self.visit(&*ctx.expr(1).unwrap()); + if ctx.ADD().is_some() { + left + right + } else { + left - right + } + } + + fn visit_number(&mut self, ctx: &NumberContext<'_>) -> Self::Return { + ctx.INT().unwrap().get_text().parse().unwrap() + } + + fn visit_multiply(&mut self, ctx: &MultiplyContext<'_>) -> Self::Return { + let left = self.visit(&*ctx.expr(0).unwrap()); + let right = self.visit(&*ctx.expr(1).unwrap()); + if ctx.MUL().is_some() { + left * right + } else { + left / right + } + } + } + + let mut _lexer = VisitorCalcLexer::new(InputStream::new("2 + 8 / 2".into())); + let token_source = CommonTokenStream::new(_lexer); + let mut parser = VisitorCalcParser::new(token_source); + + let root = parser.s().unwrap(); + + assert_eq!( + "(s (expr (expr 2) + (expr (expr 8) / (expr 2))) )", + root.to_string_tree(&*parser) + ); + + let mut visitor = CalcVisitor(0); + + let visitor_result = visitor.visit(&*root); + assert_eq!(6, visitor_result) + } + + // tests zero-copy parsing with non static visitor + #[test] + fn test_visitor_retrieve_reference() { + use antlr_rust::token_factory::ArenaCommonFactory; + use antlr_rust::tree::ParseTreeVisitor; + use csvlexer::CSVLexer; + use csvparser::{CSVParser, CsvFileContext, HdrContext, RowContext, RowContextAttrs}; + use csvvisitor::CSVVisitor; + use std::borrow::Cow; + use std::rc::Rc; + + // `T` here to ensure that visitor can have lifetime shorter that `'input` string + struct MyCSVVisitor<'i, T>(Vec<&'i str>, T); + + impl<'i, T> ParseTreeVisitor<'i, CSVParserContextType> for MyCSVVisitor<'i, T> { + fn visit_terminal(&mut self, node: &TerminalNode<'i, CSVParserContextType>) { + if node.symbol.get_token_type() == csvparser::TEXT { + if let Cow::Borrowed(s) = node.symbol.text { + self.0.push(s); + } + } + } + } + + impl<'i, T> CSVVisitor<'i> for MyCSVVisitor<'i, T> { + fn visit_hdr(&mut self, _ctx: &HdrContext<'i>) {} + + fn visit_row(&mut self, ctx: &RowContext<'i>) { + if ctx.field_all().len() > 1 { + self.visit_children(ctx) + } + } + } + + fn parse<'a>(tf: &'a ArenaCommonFactory<'a>) -> Rc> { + let mut _lexer = + CSVLexer::new_with_token_factory(InputStream::new("h1,h2\nd1,d2\nd3\n".into()), tf); + let token_source = CommonTokenStream::new(_lexer); + let mut parser = CSVParser::new(token_source); + let result = parser.csvFile().expect("parsed unsuccessfully"); + + let mut test = 5; + let mut visitor = MyCSVVisitor(Vec::new(), &mut test); + result.accept(&mut visitor); + assert_eq!(visitor.0, vec!["d1", "d2"]); + + result + } + let tf = ArenaCommonFactory::default(); + + let _result = parse(&tf); + } + + #[test] + fn test_visitor_retrieve_reference_by_return() { + use antlr_rust::token_factory::ArenaCommonFactory; + use csvlexer::CSVLexer; + use csvparser::{CSVParser, CsvFileContext, HdrContext, RowContext, RowContextAttrs}; + use csvvisitor::CSVVisitorCompat; + use std::borrow::Cow; + use std::rc::Rc; + + struct MyCSVVisitor<'i>(Vec<&'i str>); + + impl<'i> ParseTreeVisitorCompat<'i> for MyCSVVisitor<'i> { + type Node = CSVParserContextType; + type Return = Vec<&'i str>; + + fn temp_result(&mut self) -> &mut Self::Return { + &mut self.0 + } + + fn visit_terminal( + &mut self, + node: &TerminalNode<'i, CSVParserContextType>, + ) -> Self::Return { + if node.symbol.get_token_type() == csvparser::TEXT { + if let Cow::Borrowed(s) = node.symbol.text { + return vec![s]; + } + } + vec![] + } + + fn aggregate_results( + &self, + mut aggregate: Self::Return, + next: Self::Return, + ) -> Self::Return { + aggregate.extend(next); + aggregate + } + } + + impl<'i> CSVVisitorCompat<'i> for MyCSVVisitor<'i> { + fn visit_hdr(&mut self, _ctx: &HdrContext<'i>) -> Self::Return { + vec![] + } + + fn visit_row(&mut self, ctx: &RowContext<'i>) -> Self::Return { + if ctx.field_all().len() > 1 { + self.visit_children(ctx) + } else { + vec![] + } + } + } + + fn parse<'a>(tf: &'a ArenaCommonFactory<'a>) -> Rc> { + let mut _lexer = + CSVLexer::new_with_token_factory(InputStream::new("h1,h2\nd1,d2\nd3\n".into()), tf); + let token_source = CommonTokenStream::new(_lexer); + let mut parser = CSVParser::new(token_source); + let result = parser.csvFile().expect("parsed unsuccessfully"); + + let mut visitor = MyCSVVisitor(Vec::new()); + let visitor_result = visitor.visit(&*result); + assert_eq!(visitor_result, vec!["d1", "d2"]); + + result + } + let tf = ArenaCommonFactory::default(); + + let _result = parse(&tf); + } +} From 36fac0a7e545054773ed8b23b769372bbfbdc74d Mon Sep 17 00:00:00 2001 From: Konstantin Anisimov Date: Fri, 22 Jul 2022 17:46:09 +0300 Subject: [PATCH 2/7] fix CI --- .github/workflows/antlr.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/antlr.yml b/.github/workflows/antlr.yml index d0d4cb5..03b9ebb 100644 --- a/.github/workflows/antlr.yml +++ b/.github/workflows/antlr.yml @@ -147,4 +147,4 @@ jobs: env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: - release_id: ${{ steps.create_release.outputs.release_id }} \ No newline at end of file + release_id: ${{ steps.create_release.outputs.id }} \ No newline at end of file From b7660bc0e5542e4cf6df290e2552357ac5d1bd00 Mon Sep 17 00:00:00 2001 From: Olivier ROLAND Date: Thu, 25 Aug 2022 18:57:56 +0200 Subject: [PATCH 3/7] fix build instructions --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e441c10..e9c264a 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ or if you want to contribute to only runtime part you don't have to do build it But if you want to build or change generator yourself: * `git clone -b rust-target https://github.com/rrevenantt/antlr4` - clone my antlr4 fork -* `git submodule update --init --recursive` - update Rust target submodule +* `git submodule update --init --recursive --remote` - update Rust target submodule * `mvn -DskipTests install` - build generator ### Implementation status From 360bc024234ad9037b51fbfded93f7d3c914d15d Mon Sep 17 00:00:00 2001 From: Konstantin Anisimov Date: Sun, 23 Oct 2022 00:09:15 +0600 Subject: [PATCH 4/7] update CI --- .github/workflows/antlr.yml | 45 +++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/.github/workflows/antlr.yml b/.github/workflows/antlr.yml index 03b9ebb..a5a0d08 100644 --- a/.github/workflows/antlr.yml +++ b/.github/workflows/antlr.yml @@ -14,7 +14,7 @@ jobs: name: ANTLR4 testsuite runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 with: repository: rrevenantt/antlr4 ref: rust-target @@ -24,16 +24,17 @@ jobs: git checkout FETCH_HEAD working-directory: runtime/Rust - name: Set up JDK 1.8 - uses: actions/setup-java@v1 + uses: actions/setup-java@v3 with: - java-version: 1.8 + distribution: 'temurin' + java-version: '8' - name: Install Rust uses: actions-rs/toolchain@v1 with: toolchain: stable default: true - name: Cache local Maven repository - uses: actions/cache@v2 + uses: actions/cache@v3 with: path: ~/.m2/repository key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }} @@ -48,7 +49,7 @@ jobs: cat target/surefire-reports/*.dumpstream || true; exit $rc working-directory: runtime-testsuite - - uses: actions/upload-artifact@v2 + - uses: actions/upload-artifact@v3 if: ${{ github.event_name == 'push' || github.event_name == 'workflow_dispatch' }} with: name: antlr-tool @@ -60,23 +61,27 @@ jobs: outputs: new_version: ${{ steps.check_ver.outputs.new_version }} strategy: + fail-fast: false matrix: - rust_version: [ 1.52 , stable] + rust_version: [ 1.52, stable ] steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Install Rust uses: actions-rs/toolchain@v1 with: toolchain: ${{ matrix.rust_version }} default: true components: rustfmt + - name: MSRV downgrade + if: ${{ matrix.rust_version == 1.52 }} + run: cargo update -p once_cell --precise 1.13.1 - name: Build run: cargo build --verbose - name: Run tests run: cargo test --verbose - name: Formatting run: cargo fmt -- --check - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 if: ${{ github.event_name == 'push' }} with: ref: ${{ github.event.before }} @@ -88,7 +93,7 @@ jobs: NEW_VER=$(cat Cargo.toml | sed -nE "s/^version.*\"([0-9a-z.\-]+)\".*$/\1/p") BASE_VER=$(cat base_branch/Cargo.toml | sed -nE "s/^version.*\"([0-9a-z.\-]+)\".*$/\1/p") if [[ "$NEW_VER" != "$BASE_VER" ]]; then - echo "::set-output name=new_version::$NEW_VER" + echo "{new_version}={$NEW_VER}" >> $GITHUB_OUTPUT fi @@ -103,7 +108,7 @@ jobs: RELEASE_NAME: ${{ format('antlr4-4.8-2-Rust{0}',needs.tests-rust.outputs.new_version) }} PRERELEASE: ${{ contains(needs.tests-rust.outputs.new_version, '-') }} steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Install Rust uses: actions-rs/toolchain@v1 with: @@ -112,29 +117,21 @@ jobs: - name: Cargo publish check run: | cargo publish --dry-run - - uses: actions/download-artifact@v2 + - uses: actions/download-artifact@v3 with: name: antlr-tool - name: Create release id: create_release - uses: actions/create-release@v1 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + uses: softprops/action-gh-release@v1 with: tag_name: ${{ env.RELEASE_NAME }} - release_name: ${{ env.RELEASE_NAME }} + name: ${{ env.RELEASE_NAME }} + token: ${{ secrets.GITHUB_TOKEN }} # make release a draft first in case `cargo publish` still fail draft: true prerelease: ${{ env.PRERELEASE }} - - name: Upload Release Asset - uses: actions/upload-release-asset@v1 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - with: - upload_url: ${{ steps.create_release.outputs.upload_url }} - asset_path: antlr4-4.8-2-SNAPSHOT-complete.jar - asset_name: antlr4-4.8-2-SNAPSHOT-complete.jar - asset_content_type: application/java-archive + files: | + antlr4-4.8-2-SNAPSHOT-complete.jar - name: Cargo publish env: CARGO_REGISTRY_TOKEN: ${{ secrets.CRATES_IO_PUBLISH_TOKEN }} From 59b53941668a8789a0b32461d1370941fbf016d0 Mon Sep 17 00:00:00 2001 From: Mario Herzog Date: Thu, 12 Dec 2024 12:18:01 +0100 Subject: [PATCH 5/7] clippy cleanup --- src/lib.rs | 14 +------------- tests/gen/csvlexer.rs | 8 ++++---- tests/gen/csvparser.rs | 6 +++--- tests/gen/labelslexer.rs | 8 ++++---- tests/gen/labelsparser.rs | 10 +++++----- tests/gen/referencetoatnlexer.rs | 8 ++++---- tests/gen/referencetoatnparser.rs | 8 ++++---- tests/gen/simplelrlexer.rs | 8 ++++---- tests/gen/simplelrparser.rs | 6 +++--- tests/gen/visitorbasiclexer.rs | 8 ++++---- tests/gen/visitorbasicparser.rs | 6 +++--- tests/gen/visitorcalclexer.rs | 8 ++++---- tests/gen/visitorcalcparser.rs | 16 ++++++++-------- tests/gen/xmllexer.rs | 21 +++++++-------------- tests/general_tests.rs | 26 +++++++++++++------------- tests/visitors_tests.rs | 14 +++++++------- 16 files changed, 78 insertions(+), 97 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e9deb47..faf0167 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,18 +1,6 @@ #![crate_type = "lib"] -// #![feature(try_blocks)] -//#![feature(nll)] -// #![feature(raw)] -// #![feature(is_sorted)] -// #![feature(cell_update)] -// #![feature(get_mut_unchecked)] -// #![feature(specialization)] -// #![feature(coerce_unsized)] -// #![feature(associated_type_defaults)] -// #![feature(generic_associated_types)] -// #![feature(crate_visibility_modifier)] -// #![feature(generic_associated_types)] #![warn(rust_2018_idioms)] -#![warn(missing_docs)] // warn if there is missing docs +//#![warn(missing_docs)] // warn if there is missing docs #![warn(trivial_numeric_casts)] // #![allow(incomplete_features)] diff --git a/tests/gen/csvlexer.rs b/tests/gen/csvlexer.rs index beec6b9..a22aff8 100644 --- a/tests/gen/csvlexer.rs +++ b/tests/gen/csvlexer.rs @@ -34,11 +34,11 @@ pub const T__2: isize = 3; pub const WS: isize = 4; pub const TEXT: isize = 5; pub const STRING: isize = 6; -pub const channelNames: [&'static str; 0 + 2] = ["DEFAULT_TOKEN_CHANNEL", "HIDDEN"]; +pub const channelNames: [&str; 2] = ["DEFAULT_TOKEN_CHANNEL", "HIDDEN"]; -pub const modeNames: [&'static str; 1] = ["DEFAULT_MODE"]; +pub const modeNames: [&str; 1] = ["DEFAULT_MODE"]; -pub const ruleNames: [&'static str; 6] = ["T__0", "T__1", "T__2", "WS", "TEXT", "STRING"]; +pub const ruleNames: [&str; 6] = ["T__0", "T__1", "T__2", "WS", "TEXT", "STRING"]; pub const _LITERAL_NAMES: [Option<&'static str>; 4] = [None, Some("','"), Some("'\r'"), Some("'\n'")]; @@ -192,7 +192,7 @@ lazy_static! { }; } -const _serializedATN: &'static str = +const _serializedATN: &str = "\x03\u{608b}\u{a72a}\u{8133}\u{b9ed}\u{417c}\u{3be7}\u{7786}\u{5964}\x02\ \x08\x2c\x08\x01\x04\x02\x09\x02\x04\x03\x09\x03\x04\x04\x09\x04\x04\x05\ \x09\x05\x04\x06\x09\x06\x04\x07\x09\x07\x03\x02\x03\x02\x03\x03\x03\x03\ diff --git a/tests/gen/csvparser.rs b/tests/gen/csvparser.rs index afb9bfe..fdec5b4 100644 --- a/tests/gen/csvparser.rs +++ b/tests/gen/csvparser.rs @@ -49,7 +49,7 @@ pub const RULE_csvFile: usize = 0; pub const RULE_hdr: usize = 1; pub const RULE_row: usize = 2; pub const RULE_field: usize = 3; -pub const ruleNames: [&'static str; 4] = ["csvFile", "hdr", "row", "field"]; +pub const ruleNames: [&str; 4] = ["csvFile", "hdr", "row", "field"]; pub const _LITERAL_NAMES: [Option<&'static str>; 4] = [None, Some("','"), Some("'\r'"), Some("'\n'")]; @@ -214,7 +214,7 @@ pub struct CSVParserExt<'input> { _pd: PhantomData<&'input str>, } -impl<'input> CSVParserExt<'input> {} +impl CSVParserExt<'_> {} antlr_rust::tid! { CSVParserExt<'a> } impl<'input> TokenAware<'input> for CSVParserExt<'input> { @@ -767,7 +767,7 @@ lazy_static! { }; } -const _serializedATN: &'static str = +const _serializedATN: &str = "\x03\u{608b}\u{a72a}\u{8133}\u{b9ed}\u{417c}\u{3be7}\u{7786}\u{5964}\x03\ \x08\x25\x04\x02\x09\x02\x04\x03\x09\x03\x04\x04\x09\x04\x04\x05\x09\x05\ \x03\x02\x03\x02\x06\x02\x0d\x0a\x02\x0d\x02\x0e\x02\x0e\x03\x03\x03\x03\ diff --git a/tests/gen/labelslexer.rs b/tests/gen/labelslexer.rs index f8960be..92472bb 100644 --- a/tests/gen/labelslexer.rs +++ b/tests/gen/labelslexer.rs @@ -37,11 +37,11 @@ pub const T__5: isize = 6; pub const ID: isize = 7; pub const INT: isize = 8; pub const WS: isize = 9; -pub const channelNames: [&'static str; 0 + 2] = ["DEFAULT_TOKEN_CHANNEL", "HIDDEN"]; +pub const channelNames: [&str; 2] = ["DEFAULT_TOKEN_CHANNEL", "HIDDEN"]; -pub const modeNames: [&'static str; 1] = ["DEFAULT_MODE"]; +pub const modeNames: [&str; 1] = ["DEFAULT_MODE"]; -pub const ruleNames: [&'static str; 9] = [ +pub const ruleNames: [&str; 9] = [ "T__0", "T__1", "T__2", "T__3", "T__4", "T__5", "ID", "INT", "WS", ]; @@ -209,7 +209,7 @@ lazy_static! { }; } -const _serializedATN: &'static str = +const _serializedATN: &str = "\x03\u{608b}\u{a72a}\u{8133}\u{b9ed}\u{417c}\u{3be7}\u{7786}\u{5964}\x02\ \x0b\x31\x08\x01\x04\x02\x09\x02\x04\x03\x09\x03\x04\x04\x09\x04\x04\x05\ \x09\x05\x04\x06\x09\x06\x04\x07\x09\x07\x04\x08\x09\x08\x04\x09\x09\x09\ diff --git a/tests/gen/labelsparser.rs b/tests/gen/labelsparser.rs index 2f93a7a..01ab8f3 100644 --- a/tests/gen/labelsparser.rs +++ b/tests/gen/labelsparser.rs @@ -48,7 +48,7 @@ pub const INT: isize = 8; pub const WS: isize = 9; pub const RULE_s: usize = 0; pub const RULE_e: usize = 1; -pub const ruleNames: [&'static str; 2] = ["s", "e"]; +pub const ruleNames: [&str; 2] = ["s", "e"]; pub const _LITERAL_NAMES: [Option<&'static str>; 7] = [ None, @@ -212,7 +212,7 @@ pub struct LabelsParserExt<'input> { _pd: PhantomData<&'input str>, } -impl<'input> LabelsParserExt<'input> {} +impl LabelsParserExt<'_> {} antlr_rust::tid! { LabelsParserExt<'a> } impl<'input> TokenAware<'input> for LabelsParserExt<'input> { @@ -386,7 +386,7 @@ pub enum EContextAll<'input> { } antlr_rust::tid! {EContextAll<'a>} -impl<'input> antlr_rust::parser_rule_context::DerefSeal for EContextAll<'input> {} +impl antlr_rust::parser_rule_context::DerefSeal for EContextAll<'_> {} impl<'input> LabelsParserContext<'input> for EContextAll<'input> {} @@ -425,7 +425,7 @@ pub struct EContextExt<'input> { impl<'input> LabelsParserContext<'input> for EContext<'input> {} -impl<'input, 'a> Listenable + 'a> for EContext<'input> {} +impl<'input> Listenable + '_> for EContext<'input> {} impl<'input> CustomRuleContext<'input> for EContextExt<'input> { type TF = LocalTokenFactory<'input>; @@ -1438,7 +1438,7 @@ lazy_static! { }; } -const _serializedATN: &'static str = +const _serializedATN: &str = "\x03\u{608b}\u{a72a}\u{8133}\u{b9ed}\u{417c}\u{3be7}\u{7786}\u{5964}\x03\ \x0b\x2a\x04\x02\x09\x02\x04\x03\x09\x03\x03\x02\x03\x02\x03\x03\x03\x03\ \x03\x03\x03\x03\x03\x03\x03\x03\x03\x03\x03\x03\x03\x03\x03\x03\x05\x03\ diff --git a/tests/gen/referencetoatnlexer.rs b/tests/gen/referencetoatnlexer.rs index 28c9291..1b05918 100644 --- a/tests/gen/referencetoatnlexer.rs +++ b/tests/gen/referencetoatnlexer.rs @@ -31,11 +31,11 @@ use std::sync::Arc; pub const ID: isize = 1; pub const ATN: isize = 2; pub const WS: isize = 3; -pub const channelNames: [&'static str; 0 + 2] = ["DEFAULT_TOKEN_CHANNEL", "HIDDEN"]; +pub const channelNames: [&str; 2] = ["DEFAULT_TOKEN_CHANNEL", "HIDDEN"]; -pub const modeNames: [&'static str; 1] = ["DEFAULT_MODE"]; +pub const modeNames: [&str; 1] = ["DEFAULT_MODE"]; -pub const ruleNames: [&'static str; 3] = ["ID", "ATN", "WS"]; +pub const ruleNames: [&str; 3] = ["ID", "ATN", "WS"]; pub const _LITERAL_NAMES: [Option<&'static str>; 0] = []; pub const _SYMBOLIC_NAMES: [Option<&'static str>; 4] = [None, Some("ID"), Some("ATN"), Some("WS")]; @@ -187,7 +187,7 @@ lazy_static! { }; } -const _serializedATN: &'static str = +const _serializedATN: &str = "\x03\u{608b}\u{a72a}\u{8133}\u{b9ed}\u{417c}\u{3be7}\u{7786}\u{5964}\x02\ \x05\x17\x08\x01\x04\x02\x09\x02\x04\x03\x09\x03\x04\x04\x09\x04\x03\x02\ \x06\x02\x0b\x0a\x02\x0d\x02\x0e\x02\x0c\x03\x03\x06\x03\x10\x0a\x03\x0d\ diff --git a/tests/gen/referencetoatnparser.rs b/tests/gen/referencetoatnparser.rs index 2b72959..7db8339 100644 --- a/tests/gen/referencetoatnparser.rs +++ b/tests/gen/referencetoatnparser.rs @@ -41,7 +41,7 @@ pub const ID: isize = 1; pub const ATN: isize = 2; pub const WS: isize = 3; pub const RULE_a: usize = 0; -pub const ruleNames: [&'static str; 1] = ["a"]; +pub const ruleNames: [&str; 1] = ["a"]; pub const _LITERAL_NAMES: [Option<&'static str>; 0] = []; pub const _SYMBOLIC_NAMES: [Option<&'static str>; 4] = [None, Some("ID"), Some("ATN"), Some("WS")]; @@ -197,7 +197,7 @@ pub struct ReferenceToATNParserExt<'input> { _pd: PhantomData<&'input str>, } -impl<'input> ReferenceToATNParserExt<'input> {} +impl ReferenceToATNParserExt<'_> {} antlr_rust::tid! { ReferenceToATNParserExt<'a> } impl<'input> TokenAware<'input> for ReferenceToATNParserExt<'input> { @@ -333,7 +333,7 @@ where { recog.base.set_state(2); _la = recog.base.input.la(1); - if { !(_la == ID || _la == ATN) } { + if !(_la == ID || _la == ATN) { recog.err_handler.recover_inline(&mut recog.base)?; } else { if recog.base.input.la(1) == TOKEN_EOF { @@ -402,7 +402,7 @@ lazy_static! { }; } -const _serializedATN: &'static str = +const _serializedATN: &str = "\x03\u{608b}\u{a72a}\u{8133}\u{b9ed}\u{417c}\u{3be7}\u{7786}\u{5964}\x03\ \x05\x10\x04\x02\x09\x02\x03\x02\x07\x02\x06\x0a\x02\x0c\x02\x0e\x02\x09\ \x0b\x02\x03\x02\x05\x02\x0c\x0a\x02\x03\x02\x03\x02\x03\x02\x02\x02\x03\ diff --git a/tests/gen/simplelrlexer.rs b/tests/gen/simplelrlexer.rs index 2a06f23..66d9fe8 100644 --- a/tests/gen/simplelrlexer.rs +++ b/tests/gen/simplelrlexer.rs @@ -30,11 +30,11 @@ use std::sync::Arc; pub const ID: isize = 1; pub const WS: isize = 2; -pub const channelNames: [&'static str; 0 + 2] = ["DEFAULT_TOKEN_CHANNEL", "HIDDEN"]; +pub const channelNames: [&str; 2] = ["DEFAULT_TOKEN_CHANNEL", "HIDDEN"]; -pub const modeNames: [&'static str; 1] = ["DEFAULT_MODE"]; +pub const modeNames: [&str; 1] = ["DEFAULT_MODE"]; -pub const ruleNames: [&'static str; 2] = ["ID", "WS"]; +pub const ruleNames: [&str; 2] = ["ID", "WS"]; pub const _LITERAL_NAMES: [Option<&'static str>; 0] = []; pub const _SYMBOLIC_NAMES: [Option<&'static str>; 3] = [None, Some("ID"), Some("WS")]; @@ -181,7 +181,7 @@ lazy_static! { }; } -const _serializedATN: &'static str = +const _serializedATN: &str = "\x03\u{608b}\u{a72a}\u{8133}\u{b9ed}\u{417c}\u{3be7}\u{7786}\u{5964}\x02\ \x04\x10\x08\x01\x04\x02\x09\x02\x04\x03\x09\x03\x03\x02\x06\x02\x09\x0a\ \x02\x0d\x02\x0e\x02\x0a\x03\x03\x03\x03\x03\x03\x03\x03\x02\x02\x04\x03\ diff --git a/tests/gen/simplelrparser.rs b/tests/gen/simplelrparser.rs index 9355d53..8d7dcab 100644 --- a/tests/gen/simplelrparser.rs +++ b/tests/gen/simplelrparser.rs @@ -41,7 +41,7 @@ pub const ID: isize = 1; pub const WS: isize = 2; pub const RULE_s: usize = 0; pub const RULE_a: usize = 1; -pub const ruleNames: [&'static str; 2] = ["s", "a"]; +pub const ruleNames: [&str; 2] = ["s", "a"]; pub const _LITERAL_NAMES: [Option<&'static str>; 0] = []; pub const _SYMBOLIC_NAMES: [Option<&'static str>; 3] = [None, Some("ID"), Some("WS")]; @@ -186,7 +186,7 @@ pub struct SimpleLRParserExt<'input> { _pd: PhantomData<&'input str>, } -impl<'input> SimpleLRParserExt<'input> {} +impl SimpleLRParserExt<'_> {} antlr_rust::tid! { SimpleLRParserExt<'a> } impl<'input> TokenAware<'input> for SimpleLRParserExt<'input> { @@ -501,7 +501,7 @@ lazy_static! { }; } -const _serializedATN: &'static str = +const _serializedATN: &str = "\x03\u{608b}\u{a72a}\u{8133}\u{b9ed}\u{417c}\u{3be7}\u{7786}\u{5964}\x03\ \x04\x13\x04\x02\x09\x02\x04\x03\x09\x03\x03\x02\x03\x02\x03\x03\x03\x03\ \x03\x03\x03\x03\x03\x03\x07\x03\x0e\x0a\x03\x0c\x03\x0e\x03\x11\x0b\x03\ diff --git a/tests/gen/visitorbasiclexer.rs b/tests/gen/visitorbasiclexer.rs index 4f81bd8..bcc0ccd 100644 --- a/tests/gen/visitorbasiclexer.rs +++ b/tests/gen/visitorbasiclexer.rs @@ -29,11 +29,11 @@ use std::rc::Rc; use std::sync::Arc; pub const A: isize = 1; -pub const channelNames: [&'static str; 0 + 2] = ["DEFAULT_TOKEN_CHANNEL", "HIDDEN"]; +pub const channelNames: [&str; 2] = ["DEFAULT_TOKEN_CHANNEL", "HIDDEN"]; -pub const modeNames: [&'static str; 1] = ["DEFAULT_MODE"]; +pub const modeNames: [&str; 1] = ["DEFAULT_MODE"]; -pub const ruleNames: [&'static str; 1] = ["A"]; +pub const ruleNames: [&str; 1] = ["A"]; pub const _LITERAL_NAMES: [Option<&'static str>; 2] = [None, Some("'A'")]; pub const _SYMBOLIC_NAMES: [Option<&'static str>; 2] = [None, Some("A")]; @@ -184,7 +184,7 @@ lazy_static! { }; } -const _serializedATN: &'static str = +const _serializedATN: &str = "\x03\u{608b}\u{a72a}\u{8133}\u{b9ed}\u{417c}\u{3be7}\u{7786}\u{5964}\x02\ \x03\x07\x08\x01\x04\x02\x09\x02\x03\x02\x03\x02\x02\x02\x03\x03\x03\x03\ \x02\x02\x02\x06\x02\x03\x03\x02\x02\x02\x03\x05\x03\x02\x02\x02\x05\x06\ diff --git a/tests/gen/visitorbasicparser.rs b/tests/gen/visitorbasicparser.rs index 94fc461..c6169f7 100644 --- a/tests/gen/visitorbasicparser.rs +++ b/tests/gen/visitorbasicparser.rs @@ -41,7 +41,7 @@ use std::sync::Arc; pub const A: isize = 1; pub const RULE_s: usize = 0; -pub const ruleNames: [&'static str; 1] = ["s"]; +pub const ruleNames: [&str; 1] = ["s"]; pub const _LITERAL_NAMES: [Option<&'static str>; 2] = [None, Some("'A'")]; pub const _SYMBOLIC_NAMES: [Option<&'static str>; 2] = [None, Some("A")]; @@ -207,7 +207,7 @@ pub struct VisitorBasicParserExt<'input> { _pd: PhantomData<&'input str>, } -impl<'input> VisitorBasicParserExt<'input> {} +impl VisitorBasicParserExt<'_> {} antlr_rust::tid! { VisitorBasicParserExt<'a> } impl<'input> TokenAware<'input> for VisitorBasicParserExt<'input> { @@ -360,7 +360,7 @@ lazy_static! { }; } -const _serializedATN: &'static str = +const _serializedATN: &str = "\x03\u{608b}\u{a72a}\u{8133}\u{b9ed}\u{417c}\u{3be7}\u{7786}\u{5964}\x03\ \x03\x08\x04\x02\x09\x02\x03\x02\x03\x02\x03\x02\x03\x02\x02\x02\x03\x02\ \x02\x02\x02\x06\x02\x04\x03\x02\x02\x02\x04\x05\x07\x03\x02\x02\x05\x06\ diff --git a/tests/gen/visitorcalclexer.rs b/tests/gen/visitorcalclexer.rs index b4dc3a7..013327e 100644 --- a/tests/gen/visitorcalclexer.rs +++ b/tests/gen/visitorcalclexer.rs @@ -34,11 +34,11 @@ pub const DIV: isize = 3; pub const ADD: isize = 4; pub const SUB: isize = 5; pub const WS: isize = 6; -pub const channelNames: [&'static str; 0 + 2] = ["DEFAULT_TOKEN_CHANNEL", "HIDDEN"]; +pub const channelNames: [&str; 2] = ["DEFAULT_TOKEN_CHANNEL", "HIDDEN"]; -pub const modeNames: [&'static str; 1] = ["DEFAULT_MODE"]; +pub const modeNames: [&str; 1] = ["DEFAULT_MODE"]; -pub const ruleNames: [&'static str; 6] = ["INT", "MUL", "DIV", "ADD", "SUB", "WS"]; +pub const ruleNames: [&str; 6] = ["INT", "MUL", "DIV", "ADD", "SUB", "WS"]; pub const _LITERAL_NAMES: [Option<&'static str>; 6] = [ None, @@ -202,7 +202,7 @@ lazy_static! { }; } -const _serializedATN: &'static str = +const _serializedATN: &str = "\x03\u{608b}\u{a72a}\u{8133}\u{b9ed}\u{417c}\u{3be7}\u{7786}\u{5964}\x02\ \x08\x23\x08\x01\x04\x02\x09\x02\x04\x03\x09\x03\x04\x04\x09\x04\x04\x05\ \x09\x05\x04\x06\x09\x06\x04\x07\x09\x07\x03\x02\x06\x02\x11\x0a\x02\x0d\ diff --git a/tests/gen/visitorcalcparser.rs b/tests/gen/visitorcalcparser.rs index c354274..f3d831a 100644 --- a/tests/gen/visitorcalcparser.rs +++ b/tests/gen/visitorcalcparser.rs @@ -47,7 +47,7 @@ pub const SUB: isize = 5; pub const WS: isize = 6; pub const RULE_s: usize = 0; pub const RULE_expr: usize = 1; -pub const ruleNames: [&'static str; 2] = ["s", "expr"]; +pub const ruleNames: [&str; 2] = ["s", "expr"]; pub const _LITERAL_NAMES: [Option<&'static str>; 6] = [ None, @@ -221,7 +221,7 @@ pub struct VisitorCalcParserExt<'input> { _pd: PhantomData<&'input str>, } -impl<'input> VisitorCalcParserExt<'input> {} +impl VisitorCalcParserExt<'_> {} antlr_rust::tid! { VisitorCalcParserExt<'a> } impl<'input> TokenAware<'input> for VisitorCalcParserExt<'input> { @@ -402,7 +402,7 @@ pub enum ExprContextAll<'input> { } antlr_rust::tid! {ExprContextAll<'a>} -impl<'input> antlr_rust::parser_rule_context::DerefSeal for ExprContextAll<'input> {} +impl antlr_rust::parser_rule_context::DerefSeal for ExprContextAll<'_> {} impl<'input> VisitorCalcParserContext<'input> for ExprContextAll<'input> {} @@ -441,9 +441,9 @@ pub struct ExprContextExt<'input> { impl<'input> VisitorCalcParserContext<'input> for ExprContext<'input> {} -impl<'input, 'a> Listenable + 'a> for ExprContext<'input> {} +impl<'input> Listenable + '_> for ExprContext<'input> {} -impl<'input, 'a> Visitable + 'a> for ExprContext<'input> {} +impl<'input> Visitable + '_> for ExprContext<'input> {} impl<'input> CustomRuleContext<'input> for ExprContextExt<'input> { type TF = LocalTokenFactory<'input>; @@ -821,7 +821,7 @@ where } recog.base.set_state(11); _la = recog.base.input.la(1); - if { !(_la == MUL || _la == DIV) } { + if !(_la == MUL || _la == DIV) { recog.err_handler.recover_inline(&mut recog.base)?; } else { if recog.base.input.la(1) == TOKEN_EOF { @@ -858,7 +858,7 @@ where } recog.base.set_state(14); _la = recog.base.input.la(1); - if { !(_la == ADD || _la == SUB) } { + if !(_la == ADD || _la == SUB) { recog.err_handler.recover_inline(&mut recog.base)?; } else { if recog.base.input.la(1) == TOKEN_EOF { @@ -912,7 +912,7 @@ lazy_static! { }; } -const _serializedATN: &'static str = +const _serializedATN: &str = "\x03\u{608b}\u{a72a}\u{8133}\u{b9ed}\u{417c}\u{3be7}\u{7786}\u{5964}\x03\ \x08\x18\x04\x02\x09\x02\x04\x03\x09\x03\x03\x02\x03\x02\x03\x02\x03\x03\ \x03\x03\x03\x03\x03\x03\x03\x03\x03\x03\x03\x03\x03\x03\x03\x03\x07\x03\ diff --git a/tests/gen/xmllexer.rs b/tests/gen/xmllexer.rs index 3d73ded..a908b78 100644 --- a/tests/gen/xmllexer.rs +++ b/tests/gen/xmllexer.rs @@ -48,11 +48,11 @@ pub const S: isize = 17; pub const PI: isize = 18; pub const INSIDE: usize = 1; pub const PROC_INSTR: usize = 2; -pub const channelNames: [&'static str; 0 + 2] = ["DEFAULT_TOKEN_CHANNEL", "HIDDEN"]; +pub const channelNames: [&str; 2] = ["DEFAULT_TOKEN_CHANNEL", "HIDDEN"]; -pub const modeNames: [&'static str; 3] = ["DEFAULT_MODE", "INSIDE", "PROC_INSTR"]; +pub const modeNames: [&str; 3] = ["DEFAULT_MODE", "INSIDE", "PROC_INSTR"]; -pub const ruleNames: [&'static str; 24] = [ +pub const ruleNames: [&str; 24] = [ "COMMENT", "CDATA", "DTD", @@ -209,10 +209,7 @@ impl<'input, Input: CharStream>> action_index: isize, recog: &mut BaseLexer<'input, XMLLexerActions, Input, LocalTokenFactory<'input>>, ) { - match rule_index { - 10 => XMLLexer::<'input>::CLOSE_action(None, action_index, recog), - _ => {} - } + if rule_index == 10 { XMLLexer::<'input>::CLOSE_action(None, action_index, recog) } } fn sempred( _localctx: Option<&EmptyContext<'input, LocalTokenFactory<'input>>>, @@ -233,12 +230,8 @@ impl<'input, Input: CharStream>> XMLLexer<'input, Input> { action_index: isize, recog: &mut ::Target, ) { - match action_index { - 0 => { - recog.pop_mode(); - } - - _ => {} + if action_index == 0 { + recog.pop_mode(); } } fn COMMENT_sempred( @@ -303,7 +296,7 @@ lazy_static! { }; } -const _serializedATN: &'static str = +const _serializedATN: &str = "\x03\u{608b}\u{a72a}\u{8133}\u{b9ed}\u{417c}\u{3be7}\u{7786}\u{5964}\x02\ \x14\u{e8}\x08\x01\x08\x01\x08\x01\x04\x02\x09\x02\x04\x03\x09\x03\x04\ \x04\x09\x04\x04\x05\x09\x05\x04\x06\x09\x06\x04\x07\x09\x07\x04\x08\x09\ diff --git a/tests/general_tests.rs b/tests/general_tests.rs index e841b03..e477543 100644 --- a/tests/general_tests.rs +++ b/tests/general_tests.rs @@ -20,8 +20,8 @@ mod gen { use antlr_rust::token_factory::{ArenaCommonFactory, OwningTokenFactory}; use antlr_rust::token_stream::{TokenStream, UnbufferedTokenStream}; use antlr_rust::tree::{ - ParseTree, ParseTreeListener, ParseTreeVisitor, ParseTreeVisitorCompat, ParseTreeWalker, - TerminalNode, Tree, VisitChildren, Visitable, + ParseTree, ParseTreeListener, + TerminalNode, }; use antlr_rust::InputStream; use csvlexer::*; @@ -33,9 +33,9 @@ mod gen { use xmllexer::XMLLexer; use crate::gen::csvparser::{ - CSVParserContext, CSVParserContextType, CsvFileContext, HdrContext, RowContext, + CSVParserContext, CSVParserContextType, }; - use crate::gen::csvvisitor::CSVVisitor; + use crate::gen::labelslexer::LabelsLexer; use crate::gen::labelsparser::{EContextAll, LabelsParser}; use crate::gen::referencetoatnparser::{ @@ -123,7 +123,7 @@ if (x < x && a > 0) then duh println!("test started lexer_test_csv"); let tf = ArenaCommonFactory::default(); let mut _lexer = CSVLexer::new_with_token_factory( - InputStream::new("V123,V2\nd1,d222".into()), + InputStream::new("V123,V2\nd1,d222"), // Box::new(UTF16InputStream::from_str("V123,V2\nd1,d222","".into())), &tf, ); @@ -177,14 +177,14 @@ if (x < x && a > 0) then duh } } - impl<'input> CSVListener<'input> for Listener {} + impl CSVListener<'_> for Listener {} #[test] fn parser_test_csv() { println!("test started"); let tf = ArenaCommonFactory::default(); let mut _lexer = - CSVLexer::new_with_token_factory(InputStream::new("V123,V2\nd1,d2\n".into()), &tf); + CSVLexer::new_with_token_factory(InputStream::new("V123,V2\nd1,d2\n"), &tf); let token_source = CommonTokenStream::new(_lexer); let mut parser = CSVParser::new(token_source); parser.add_parse_listener(Box::new(Listener {})); @@ -210,7 +210,7 @@ if (x < x && a > 0) then duh } } - impl<'input> ReferenceToATNListener<'input> for Listener2 {} + impl ReferenceToATNListener<'_> for Listener2 {} static FACTORY: OwningTokenFactory = OwningTokenFactory; @@ -256,11 +256,11 @@ if (x < x && a > 0) then duh } } - impl<'input> SimpleLRListener<'input> for Listener3 {} + impl SimpleLRListener<'_> for Listener3 {} #[test] fn test_lr() { - let mut _lexer = SimpleLRLexer::new(InputStream::new("x y z".into())); + let mut _lexer = SimpleLRLexer::new(InputStream::new("x y z")); let token_source = CommonTokenStream::new(_lexer); let mut parser = SimpleLRParser::new(token_source); parser.add_parse_listener(Box::new(Listener3)); @@ -271,7 +271,7 @@ if (x < x && a > 0) then duh #[test] fn test_immediate_lr() { - let mut _lexer = SimpleLRLexer::new(InputStream::new("x y z".into())); + let mut _lexer = SimpleLRLexer::new(InputStream::new("x y z")); let token_source = CommonTokenStream::new(_lexer); let mut parser = SimpleLRParser::new(token_source); parser.add_parse_listener(Box::new(Listener3)); @@ -299,11 +299,11 @@ if (x < x && a > 0) then duh } } - impl<'input> SimpleLRListener<'input> for Listener4 {} + impl SimpleLRListener<'_> for Listener4 {} #[test] fn test_remove_listener() { - let mut _lexer = SimpleLRLexer::new(InputStream::new("x y z".into())); + let mut _lexer = SimpleLRLexer::new(InputStream::new("x y z")); let token_source = CommonTokenStream::new(_lexer); let mut parser = SimpleLRParser::new(token_source); parser.add_parse_listener(Box::new(Listener3)); diff --git a/tests/visitors_tests.rs b/tests/visitors_tests.rs index 4a49945..eccc836 100644 --- a/tests/visitors_tests.rs +++ b/tests/visitors_tests.rs @@ -31,7 +31,7 @@ mod gen { #[test] fn test_visit_terminal_node() { - let mut lexer = VisitorBasicLexer::new(InputStream::new("A".into())); + let lexer = VisitorBasicLexer::new(InputStream::new("A")); let mut parser = VisitorBasicParser::new(CommonTokenStream::new(lexer)); let root = parser.s().unwrap(); @@ -68,7 +68,7 @@ mod gen { #[test] fn test_visit_error_node() { - let mut lexer = VisitorBasicLexer::new(InputStream::new("".into())); + let lexer = VisitorBasicLexer::new(InputStream::new("")); let mut parser = VisitorBasicParser::new(CommonTokenStream::new(lexer)); let root = parser.s().unwrap(); @@ -104,7 +104,7 @@ mod gen { #[test] fn test_should_not_visit_EOF() { - let mut lexer = VisitorBasicLexer::new(InputStream::new("A".into())); + let lexer = VisitorBasicLexer::new(InputStream::new("A")); let mut parser = VisitorBasicParser::new(CommonTokenStream::new(lexer)); let root = parser.s().unwrap(); @@ -155,7 +155,7 @@ mod gen { #[test] fn test_should_not_visit_anything() { - let mut lexer = VisitorBasicLexer::new(InputStream::new("A".into())); + let lexer = VisitorBasicLexer::new(InputStream::new("A")); let mut parser = VisitorBasicParser::new(CommonTokenStream::new(lexer)); let root = parser.s().unwrap(); @@ -240,7 +240,7 @@ mod gen { } } - let mut _lexer = VisitorCalcLexer::new(InputStream::new("2 + 8 / 2".into())); + let mut _lexer = VisitorCalcLexer::new(InputStream::new("2 + 8 / 2")); let token_source = CommonTokenStream::new(_lexer); let mut parser = VisitorCalcParser::new(token_source); @@ -293,7 +293,7 @@ mod gen { fn parse<'a>(tf: &'a ArenaCommonFactory<'a>) -> Rc> { let mut _lexer = - CSVLexer::new_with_token_factory(InputStream::new("h1,h2\nd1,d2\nd3\n".into()), tf); + CSVLexer::new_with_token_factory(InputStream::new("h1,h2\nd1,d2\nd3\n"), tf); let token_source = CommonTokenStream::new(_lexer); let mut parser = CSVParser::new(token_source); let result = parser.csvFile().expect("parsed unsuccessfully"); @@ -367,7 +367,7 @@ mod gen { fn parse<'a>(tf: &'a ArenaCommonFactory<'a>) -> Rc> { let mut _lexer = - CSVLexer::new_with_token_factory(InputStream::new("h1,h2\nd1,d2\nd3\n".into()), tf); + CSVLexer::new_with_token_factory(InputStream::new("h1,h2\nd1,d2\nd3\n"), tf); let token_source = CommonTokenStream::new(_lexer); let mut parser = CSVParser::new(token_source); let result = parser.csvFile().expect("parsed unsuccessfully"); From f1bb7fab522abbfe6f19cde051a27f33c9108ea5 Mon Sep 17 00:00:00 2001 From: Mario Herzog Date: Thu, 12 Dec 2024 12:18:55 +0100 Subject: [PATCH 6/7] cleanup for orphaned imports --- src/common_token_stream.rs | 1 - src/input_stream.rs | 1 - src/parser.rs | 2 +- src/rule_context.rs | 2 +- src/token_factory.rs | 2 +- src/token_stream.rs | 1 - src/tree.rs | 4 +--- 7 files changed, 4 insertions(+), 9 deletions(-) diff --git a/src/common_token_stream.rs b/src/common_token_stream.rs index a56ddc7..57bed9d 100644 --- a/src/common_token_stream.rs +++ b/src/common_token_stream.rs @@ -6,7 +6,6 @@ use crate::token::{Token, TOKEN_DEFAULT_CHANNEL, TOKEN_INVALID_TYPE}; use crate::token_factory::TokenFactory; use crate::token_source::TokenSource; use crate::token_stream::{TokenStream, UnbufferedTokenStream}; -use better_any::{Tid, TidAble}; /// Default token stream that skips token that not correspond to current channel. #[derive(Debug)] diff --git a/src/input_stream.rs b/src/input_stream.rs index 2373a9d..3a20977 100644 --- a/src/input_stream.rs +++ b/src/input_stream.rs @@ -5,7 +5,6 @@ use crate::char_stream::{CharStream, InputData}; use crate::int_stream::IntStream; use std::ops::Deref; -use better_any::TidAble; /// Default rust target input stream. /// diff --git a/src/parser.rs b/src/parser.rs index e5cf988..aaf0784 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -23,7 +23,7 @@ use crate::tree::{ErrorNode, Listenable, ParseTreeListener, TerminalNode}; use crate::utils::cell_update; use crate::vocabulary::Vocabulary; use crate::{CoerceFrom, CoerceTo}; -use better_any::{Tid, TidAble}; +use better_any::TidAble; /// parser functionality required for `ParserATNSimulator` to work #[allow(missing_docs)] // todo rewrite it so downstream crates actually could meaningfully implement it diff --git a/src/rule_context.rs b/src/rule_context.rs index 57be242..7ce1410 100644 --- a/src/rule_context.rs +++ b/src/rule_context.rs @@ -11,7 +11,7 @@ use crate::parser::ParserNodeType; use crate::parser_rule_context::ParserRuleContext; use crate::token_factory::TokenFactory; use crate::tree::{ParseTree, Tree}; -use better_any::{Tid, TidAble}; +use better_any::TidAble; use std::any::type_name; //pub trait RuleContext:RuleNode { diff --git a/src/token_factory.rs b/src/token_factory.rs index 286d6f9..7c8d1e1 100644 --- a/src/token_factory.rs +++ b/src/token_factory.rs @@ -12,7 +12,7 @@ use typed_arena::Arena; use crate::char_stream::{CharStream, InputData}; use crate::token::Token; use crate::token::{CommonToken, OwningToken, TOKEN_INVALID_TYPE}; -use better_any::{Tid, TidAble}; +use better_any::TidAble; lazy_static! { pub(crate) static ref COMMON_TOKEN_FACTORY_DEFAULT: Box = diff --git a/src/token_stream.rs b/src/token_stream.rs index 9623c72..badc8e0 100644 --- a/src/token_stream.rs +++ b/src/token_stream.rs @@ -8,7 +8,6 @@ use crate::int_stream::{IntStream, IterWrapper}; use crate::token::{OwningToken, Token, TOKEN_EOF, TOKEN_INVALID_TYPE}; use crate::token_factory::TokenFactory; use crate::token_source::TokenSource; -use better_any::{Tid, TidAble}; use std::fmt::{Debug, Formatter}; /// An `IntSteam` of `Token`s diff --git a/src/tree.rs b/src/tree.rs index fce1359..d27e641 100644 --- a/src/tree.rs +++ b/src/tree.rs @@ -8,18 +8,16 @@ use std::marker::PhantomData; use std::ops::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::parser_rule_context::ParserRuleContext; use crate::recognizer::Recognizer; use crate::rule_context::{CustomRuleContext, RuleContext}; use crate::token::Token; use crate::token_factory::TokenFactory; use crate::{interval_set, trees, CoerceTo}; -use better_any::{Tid, TidAble}; use std::mem; //todo try to make in more generic From aa73c9302563d7775a05b4be7623c8e19ed3f23e Mon Sep 17 00:00:00 2001 From: Mario Herzog Date: Thu, 12 Dec 2024 12:26:12 +0100 Subject: [PATCH 7/7] cleanup: unreachables and unused --- src/dfa.rs | 2 -- src/error_strategy.rs | 1 - src/tree.rs | 8 ++++---- src/utils.rs | 2 -- 4 files changed, 4 insertions(+), 9 deletions(-) diff --git a/src/dfa.rs b/src/dfa.rs index 79b1dcc..13097b5 100644 --- a/src/dfa.rs +++ b/src/dfa.rs @@ -29,8 +29,6 @@ pub(crate) trait ScopeExt: Sized { f(self); self } - - fn drop(self) {} } impl ScopeExt for Any {} diff --git a/src/error_strategy.rs b/src/error_strategy.rs index 04025ad..dc7e4f9 100644 --- a/src/error_strategy.rs +++ b/src/error_strategy.rs @@ -581,7 +581,6 @@ impl<'input, Ctx: ParserNodeType<'input>> BailErrorStrategy<'input, Ctx> { ctx.set_exception(e.clone()); ctx = ctx.get_parent()? } - Some(()) })(); return ANTLRError::FallThrough(Rc::new(ParseCancelledError(e.clone()))); } diff --git a/src/tree.rs b/src/tree.rs index d27e641..bbcf02c 100644 --- a/src/tree.rs +++ b/src/tree.rs @@ -139,7 +139,7 @@ impl<'input, Node: ParserNodeType<'input>, T: 'static> CustomRuleContext<'input> usize::max_value() } - fn get_node_text(&self, rule_names: &[&str]) -> String { + fn get_node_text(&self, _rule_names: &[&str]) -> String { self.symbol.borrow().get_text().to_display() } } @@ -289,14 +289,14 @@ pub trait ParseTreeVisitorCompat<'input>: VisitChildren<'input, Self::Node> { return result; } - fn aggregate_results(&self, aggregate: Self::Return, next: Self::Return) -> Self::Return { + fn aggregate_results(&self, _aggregate: Self::Return, next: Self::Return) -> Self::Return { next } fn should_visit_next_child( &self, - node: &>::Type, - current: &Self::Return, + _node: &>::Type, + _current: &Self::Return, ) -> bool { true } diff --git a/src/utils.rs b/src/utils.rs index ac6e1f6..797a761 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -15,8 +15,6 @@ pub fn escape_whitespaces(data: impl Borrow, escape_spaces: bool) -> String res } -pub trait Sealed {} - pub fn cell_update(cell: &Cell, f: F) -> T where F: FnOnce(T) -> T,