From 8f78cd80bb04153273f4fb53626c71c1509c22c3 Mon Sep 17 00:00:00 2001 From: Igor Dejanovic Date: Fri, 15 Dec 2023 22:33:22 +0100 Subject: [PATCH] rework: hide Rc from LRParser::new --- .../calculator/calculator1/src/calculator.rs | 3 +-- .../calculator/calculator2/src/calculator.rs | 3 +-- .../calculator/calculator3/src/calculator.rs | 3 +-- .../calculator/calculator4/src/calculator.rs | 3 +-- .../calculator/calculator5/src/calculator.rs | 3 +-- rustemo-compiler/src/lang/rustemo.rs | 3 +-- rustemo/src/glr/parser.rs | 4 +-- rustemo/src/lr/parser.rs | 26 ++++++++++++++++--- tests/src/output_dir/output_dir.rs | 3 +-- 9 files changed, 31 insertions(+), 20 deletions(-) diff --git a/docs/src/tutorials/calculator/calculator1/src/calculator.rs b/docs/src/tutorials/calculator/calculator1/src/calculator.rs index 2e0c54d4..2a7d1a85 100644 --- a/docs/src/tutorials/calculator/calculator1/src/calculator.rs +++ b/docs/src/tutorials/calculator/calculator1/src/calculator.rs @@ -1,7 +1,6 @@ /// Generated by rustemo. Do not edit manually! use std::fmt::Debug; use std::hash::Hash; -use std::rc::Rc; use rustemo::{ Result, Input as InputT, Lexer, Token, TokenRecognizer as TokenRecognizerT, Parser, ParserDefinition, State as StateT, Builder, @@ -193,7 +192,7 @@ impl< State::default(), false, false, - Rc::new(StringLexer::new(true, &RECOGNIZERS)), + StringLexer::new(true, &RECOGNIZERS), DefaultBuilder::new(), ), ) diff --git a/docs/src/tutorials/calculator/calculator2/src/calculator.rs b/docs/src/tutorials/calculator/calculator2/src/calculator.rs index 4a250fff..038de9e2 100644 --- a/docs/src/tutorials/calculator/calculator2/src/calculator.rs +++ b/docs/src/tutorials/calculator/calculator2/src/calculator.rs @@ -1,7 +1,6 @@ /// Generated by rustemo. Do not edit manually! use std::fmt::Debug; use std::hash::Hash; -use std::rc::Rc; use rustemo::{ Result, Input as InputT, Lexer, Token, TokenRecognizer as TokenRecognizerT, Parser, ParserDefinition, State as StateT, Builder, @@ -319,7 +318,7 @@ impl< State::default(), false, false, - Rc::new(StringLexer::new(true, &RECOGNIZERS)), + StringLexer::new(true, &RECOGNIZERS), DefaultBuilder::new(), ), ) diff --git a/docs/src/tutorials/calculator/calculator3/src/calculator.rs b/docs/src/tutorials/calculator/calculator3/src/calculator.rs index dd90bbf3..6b58fe09 100644 --- a/docs/src/tutorials/calculator/calculator3/src/calculator.rs +++ b/docs/src/tutorials/calculator/calculator3/src/calculator.rs @@ -1,7 +1,6 @@ /// Generated by rustemo. Do not edit manually! use std::fmt::Debug; use std::hash::Hash; -use std::rc::Rc; use rustemo::{ Result, Input as InputT, Lexer, Token, TokenRecognizer as TokenRecognizerT, Parser, ParserDefinition, State as StateT, Builder, @@ -319,7 +318,7 @@ impl< State::default(), false, false, - Rc::new(StringLexer::new(true, &RECOGNIZERS)), + StringLexer::new(true, &RECOGNIZERS), DefaultBuilder::new(), ), ) diff --git a/docs/src/tutorials/calculator/calculator4/src/calculator.rs b/docs/src/tutorials/calculator/calculator4/src/calculator.rs index dd90bbf3..6b58fe09 100644 --- a/docs/src/tutorials/calculator/calculator4/src/calculator.rs +++ b/docs/src/tutorials/calculator/calculator4/src/calculator.rs @@ -1,7 +1,6 @@ /// Generated by rustemo. Do not edit manually! use std::fmt::Debug; use std::hash::Hash; -use std::rc::Rc; use rustemo::{ Result, Input as InputT, Lexer, Token, TokenRecognizer as TokenRecognizerT, Parser, ParserDefinition, State as StateT, Builder, @@ -319,7 +318,7 @@ impl< State::default(), false, false, - Rc::new(StringLexer::new(true, &RECOGNIZERS)), + StringLexer::new(true, &RECOGNIZERS), DefaultBuilder::new(), ), ) diff --git a/docs/src/tutorials/calculator/calculator5/src/calculator.rs b/docs/src/tutorials/calculator/calculator5/src/calculator.rs index dd90bbf3..6b58fe09 100644 --- a/docs/src/tutorials/calculator/calculator5/src/calculator.rs +++ b/docs/src/tutorials/calculator/calculator5/src/calculator.rs @@ -1,7 +1,6 @@ /// Generated by rustemo. Do not edit manually! use std::fmt::Debug; use std::hash::Hash; -use std::rc::Rc; use rustemo::{ Result, Input as InputT, Lexer, Token, TokenRecognizer as TokenRecognizerT, Parser, ParserDefinition, State as StateT, Builder, @@ -319,7 +318,7 @@ impl< State::default(), false, false, - Rc::new(StringLexer::new(true, &RECOGNIZERS)), + StringLexer::new(true, &RECOGNIZERS), DefaultBuilder::new(), ), ) diff --git a/rustemo-compiler/src/lang/rustemo.rs b/rustemo-compiler/src/lang/rustemo.rs index 247d3d17..cb75184b 100644 --- a/rustemo-compiler/src/lang/rustemo.rs +++ b/rustemo-compiler/src/lang/rustemo.rs @@ -1,7 +1,6 @@ /// Generated by rustemo. Do not edit manually! use std::fmt::Debug; use std::hash::Hash; -use std::rc::Rc; use rustemo::{ Result, Input as InputT, Lexer, Token, TokenRecognizer as TokenRecognizerT, Parser, ParserDefinition, State as StateT, Builder, @@ -16764,7 +16763,7 @@ impl< State::default(), false, true, - Rc::new(StringLexer::new(false, &RECOGNIZERS)), + StringLexer::new(false, &RECOGNIZERS), DefaultBuilder::new(), ), ) diff --git a/rustemo/src/glr/parser.rs b/rustemo/src/glr/parser.rs index 8716880c..83d0e4dc 100644 --- a/rustemo/src/glr/parser.rs +++ b/rustemo/src/glr/parser.rs @@ -957,13 +957,13 @@ where let mut gss: GssGraph<'i, I, S, P, TK> = GssGraph::new(); let start_head = gss.add_head(context.clone()); if self.has_layout { - *self.layout_parser.borrow_mut() = Some(LRParser::new( + *self.layout_parser.borrow_mut() = Some(LRParser::new_default( self.definition, S::default_layout().expect("Layout state not defined."), true, false, Rc::clone(&self.lexer), - SliceBuilder::new(input), + RefCell::new(SliceBuilder::new(input)), )) } diff --git a/rustemo/src/lr/parser.rs b/rustemo/src/lr/parser.rs index 7c3e8616..8391d534 100644 --- a/rustemo/src/lr/parser.rs +++ b/rustemo/src/lr/parser.rs @@ -171,8 +171,26 @@ where state: S, partial_parse: bool, has_layout: bool, - lexer: Rc, + lexer: L, builder: B, + ) -> Self { + Self::new_default( + definition, + state, + partial_parse, + has_layout, + Rc::new(lexer), + RefCell::new(builder), + ) + } + + pub(crate) fn new_default( + definition: &'i D, + state: S, + partial_parse: bool, + has_layout: bool, + lexer: Rc, + builder: RefCell, ) -> Self { Self { definition, @@ -183,7 +201,7 @@ where start_state: state, has_layout, lexer, - builder: RefCell::new(builder), + builder, phantom: PhantomData, } } @@ -308,13 +326,13 @@ where // produce the output and it never uses partial parse. let layout_parser: LayoutParser<'i, C, S, P, TK, NTK, D, L, I> = self.has_layout.then(|| { - LRParser::new( + LRParser::new_default( self.definition, S::default_layout().expect("Layout state not defined."), true, false, Rc::clone(&self.lexer), - SliceBuilder::new(input), + RefCell::new(SliceBuilder::new(input)), ) }); diff --git a/tests/src/output_dir/output_dir.rs b/tests/src/output_dir/output_dir.rs index a442aca7..0d79c35d 100644 --- a/tests/src/output_dir/output_dir.rs +++ b/tests/src/output_dir/output_dir.rs @@ -1,7 +1,6 @@ /// Generated by rustemo. Do not edit manually! use std::fmt::Debug; use std::hash::Hash; -use std::rc::Rc; use rustemo::{ Result, Input as InputT, Lexer, Token, TokenRecognizer as TokenRecognizerT, Parser, ParserDefinition, State as StateT, Builder, @@ -216,7 +215,7 @@ impl< State::default(), false, false, - Rc::new(StringLexer::new(true, &RECOGNIZERS)), + StringLexer::new(true, &RECOGNIZERS), DefaultBuilder::new(), ), )