Skip to content

Commit

Permalink
Revert "refactor: Migrate referenced_versions from the grammar visi…
Browse files Browse the repository at this point in the history
…tor"

This reverts commit 117b9487e9d882b372b4944b024b32d90f8296d7.
  • Loading branch information
Xanewok committed May 29, 2024
1 parent 6ec9ec0 commit 2d8b7dd
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ use crate::parser_support::{
pub struct Language {
{%- if not rendering_in_stubs -%}
{%- for version in model.parser.referenced_versions -%}
#[allow(dead_code)]
pub(crate) version_is_at_least_{{ version | replace(from=".", to="_") }}: bool,
{%- endfor -%}
{%- endif -%}
Expand Down
49 changes: 31 additions & 18 deletions crates/codegen/runtime/generator/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::collections::{BTreeMap, BTreeSet};
use std::rc::Rc;

use codegen_language_definition::model::{Identifier, Language};
use codegen_language_definition::model::{Identifier, Language, VersionSpecifier};
use semver::Version;
use serde::Serialize;

Expand All @@ -16,8 +16,8 @@ use codegen::{
};
use grammar::{
Grammar, GrammarVisitor, KeywordScannerAtomic, KeywordScannerDefinitionRef,
ParserDefinitionNode, ParserDefinitionRef, PrecedenceParserDefinitionRef, ScannerDefinitionRef,
TriviaParserDefinitionRef,
ParserDefinitionNode, ParserDefinitionRef, PrecedenceParserDefinitionRef,
ScannerDefinitionNode, ScannerDefinitionRef, TriviaParserDefinitionRef,
};

/// Newtype for the already generated Rust code, not to be confused with regular strings.
Expand All @@ -31,7 +31,7 @@ pub struct ParserModel {

/// Defines the top-level scanner functions in `Language`.
scanner_functions: BTreeMap<Identifier, RustCode>, // (name of scanner, code)
// Defines the `LexicalContext(Type)` enum and type-level variants.
// Defines the `Lexer::next_terminal` method.
scanner_contexts: BTreeMap<Identifier, ScannerContextModel>,
/// Defines the top-level compound scanners used when lexing in `Language`.
keyword_compound_scanners: BTreeMap<Identifier, RustCode>, // (name of the KW scanner, code)
Expand Down Expand Up @@ -61,7 +61,10 @@ struct ScannerContextModel {

#[derive(Default)]
struct ParserAccumulatorState {
// Defines the `LexicalContext(Type)` enum and type-level variants.
/// Constructs inner `Language` the state to evaluate the version-dependent branches.
referenced_versions: BTreeSet<Version>,

// Defines the `Lexer::next_terminal` method.
scanner_contexts: BTreeMap<Identifier, ScannerContextAccumulatorState>,

/// Defines the top-level parser functions in `Language`.
Expand All @@ -85,12 +88,6 @@ struct ScannerContextAccumulatorState {
keyword_scanner_defs: BTreeMap<Identifier, KeywordScannerDefinitionRef>,
}

/// Collects model state from the DSL v2 rather than via the remaining visitor pattern.
struct DslV2CollectorState {
/// Constructs inner `Language` the state to evaluate the version-dependent branches.
referenced_versions: BTreeSet<Version>,
}

impl ParserModel {
pub fn from_language(language: &Rc<Language>) -> Self {
// First, we construct the DSLv1 model from the DSLv2 definition...
Expand All @@ -99,10 +96,7 @@ impl ParserModel {
let mut acc = ParserAccumulatorState::default();
grammar.accept_visitor(&mut acc);

// WIP(#638): Gradually migrate off `GrammarVisitor`
acc.into_model(DslV2CollectorState {
referenced_versions: language.collect_breaking_versions(),
})
acc.into_model()
}
}

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

fn into_model(self, collected: DslV2CollectorState) -> ParserModel {
fn into_model(self) -> ParserModel {
let contexts = self
.scanner_contexts
.into_iter()
Expand Down Expand Up @@ -198,14 +192,13 @@ impl ParserAccumulatorState {
.collect();

ParserModel {
referenced_versions: self.referenced_versions,
parser_functions: self.parser_functions,
trivia_parser_functions: self.trivia_parser_functions,
// These are derived from the accumulated state
scanner_contexts: contexts,
scanner_functions,
keyword_compound_scanners,
// These are derived from the DSLv2 model directly
referenced_versions: collected.referenced_versions,
}
}
}
Expand All @@ -216,6 +209,15 @@ impl GrammarVisitor for ParserAccumulatorState {
.insert(scanner.name().clone(), Rc::clone(scanner));
}

fn keyword_scanner_definition_enter(&mut self, scanner: &KeywordScannerDefinitionRef) {
for def in scanner.definitions() {
let specifiers = def.enabled.iter().chain(def.reserved.iter());

self.referenced_versions
.extend(specifiers.flat_map(VersionSpecifier::versions).cloned());
}
}

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

Expand Down Expand Up @@ -252,8 +254,19 @@ impl GrammarVisitor for ParserAccumulatorState {
);
}

fn scanner_definition_node_enter(&mut self, node: &ScannerDefinitionNode) {
if let ScannerDefinitionNode::Versioned(_, version_specifier) = node {
self.referenced_versions
.extend(version_specifier.versions().cloned());
}
}

fn parser_definition_node_enter(&mut self, node: &ParserDefinitionNode) {
match node {
ParserDefinitionNode::Versioned(_, version_specifier) => {
self.referenced_versions
.extend(version_specifier.versions().cloned());
}
ParserDefinitionNode::ScannerDefinition(scanner) => {
self.top_level_scanner_names.insert(scanner.name().clone());

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2d8b7dd

Please sign in to comment.