Skip to content

Commit

Permalink
refactor: Derive all_scanners from the grammar elements directly
Browse files Browse the repository at this point in the history
  • Loading branch information
Xanewok committed Jun 5, 2024
1 parent 560b057 commit f077716
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
26 changes: 13 additions & 13 deletions crates/codegen/runtime/generator/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ use codegen::{
};
use grammar::{
GrammarVisitor, ParserDefinitionNode, ParserDefinitionRef, PrecedenceParserDefinitionRef,
ScannerDefinitionRef, TriviaParserDefinitionRef,
TriviaParserDefinitionRef,
};

use crate::parser::codegen::KeywordScannerAtomic;
use crate::parser::grammar::resolver::Resolution;
use crate::parser::grammar::ResolveCtx;

/// Newtype for the already generated Rust code, not to be confused with regular strings.
Expand Down Expand Up @@ -69,8 +70,6 @@ struct ParserAccumulatorState {

/// Makes sure to codegen the scanner functions that are referenced by other scanners.
top_level_scanner_names: BTreeSet<Identifier>,
/// Lookup table for all scanners; used to generate trie scanners.
all_scanners: BTreeMap<Identifier, ScannerDefinitionRef>,
/// The current context of a parent scanner/parser being processed.
current_context_name: Option<Identifier>,
}
Expand All @@ -92,7 +91,7 @@ impl ParserModel {
let mut acc = ParserAccumulatorState::default();
grammar.accept_visitor(&mut acc);

acc.into_model()
acc.into_model(&resolved)
}
}

Expand All @@ -108,7 +107,14 @@ impl ParserAccumulatorState {
.expect("context must be set with `set_current_context`")
}

fn into_model(self) -> ParserModel {
fn into_model(self, resolved: &Resolution) -> ParserModel {
// Lookup table for all scanners; used to generate trie scanners.
let all_scanners: BTreeMap<_, _> = resolved
.items()
.filter_map(|(_, item)| item.try_as_scanner_definition_ref())
.map(|scanner| (scanner.name().clone(), Rc::clone(scanner)))
.collect();

let contexts = self
.scanner_contexts
.into_iter()
Expand All @@ -122,7 +128,7 @@ impl ParserAccumulatorState {
let mut literal_trie = Trie::new();

for scanner_name in &context.scanner_definitions {
let scanner = &self.all_scanners[scanner_name];
let scanner = &all_scanners[scanner_name];

let literals = scanner.literals().unwrap_or_default();
if literals.is_empty() {
Expand Down Expand Up @@ -159,8 +165,7 @@ impl ParserAccumulatorState {
.collect::<BTreeMap<_, _>>();

// Expose the scanner functions that...
let scanner_functions = self
.all_scanners
let scanner_functions = all_scanners
.iter()
.filter(|(name, scanner)| {
// are compound (do not consist of only literals)
Expand Down Expand Up @@ -199,11 +204,6 @@ impl ParserAccumulatorState {
}

impl GrammarVisitor for ParserAccumulatorState {
fn scanner_definition_enter(&mut self, scanner: &ScannerDefinitionRef) {
self.all_scanners
.insert(scanner.name().clone(), Rc::clone(scanner));
}

fn trivia_parser_definition_enter(&mut self, parser: &TriviaParserDefinitionRef) {
self.set_current_context(parser.context().clone());

Expand Down
2 changes: 1 addition & 1 deletion crates/codegen/runtime/generator/src/parser/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl Grammar {
}

#[allow(clippy::enum_variant_names)] // this will be removed soon
#[derive(Clone)]
#[derive(Clone, strum_macros::EnumTryAs)]
pub enum GrammarElement {
ScannerDefinition(ScannerDefinitionRef),
KeywordScannerDefinition(Rc<model::KeywordItem>),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ impl ResolveCtx {
}

impl Resolution {
/// Returns the resolved items.
pub fn items(&self) -> impl Iterator<Item = (&Identifier, &GrammarElement)> {
self.resolved.iter()
}

/// Collects the already resolved item into a [`Grammar`].
pub fn to_grammar(&self) -> Grammar {
let resolved_items = self
Expand Down

0 comments on commit f077716

Please sign in to comment.