From 2d8b7ddac1d457c43e908b56f356d056d0730507 Mon Sep 17 00:00:00 2001 From: Igor Matuszewski Date: Wed, 29 May 2024 14:40:07 +0200 Subject: [PATCH] Revert "refactor: Migrate `referenced_versions` from the grammar visitor" This reverts commit 117b9487e9d882b372b4944b024b32d90f8296d7. --- .../cargo/src/runtime/language.rs.jinja2 | 1 - .../codegen/runtime/generator/src/parser.rs | 49 ++++++++++++------- .../src/generated/generated/language.rs | 35 ------------- .../src/generated/generated/language.rs | 1 - 4 files changed, 31 insertions(+), 55 deletions(-) diff --git a/crates/codegen/runtime/cargo/src/runtime/language.rs.jinja2 b/crates/codegen/runtime/cargo/src/runtime/language.rs.jinja2 index 01e2b80630..f09bbdf3c8 100644 --- a/crates/codegen/runtime/cargo/src/runtime/language.rs.jinja2 +++ b/crates/codegen/runtime/cargo/src/runtime/language.rs.jinja2 @@ -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 -%} diff --git a/crates/codegen/runtime/generator/src/parser.rs b/crates/codegen/runtime/generator/src/parser.rs index 4f86a2401c..8a2c42e847 100644 --- a/crates/codegen/runtime/generator/src/parser.rs +++ b/crates/codegen/runtime/generator/src/parser.rs @@ -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; @@ -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. @@ -31,7 +31,7 @@ pub struct ParserModel { /// Defines the top-level scanner functions in `Language`. scanner_functions: BTreeMap, // (name of scanner, code) - // Defines the `LexicalContext(Type)` enum and type-level variants. + // Defines the `Lexer::next_terminal` method. scanner_contexts: BTreeMap, /// Defines the top-level compound scanners used when lexing in `Language`. keyword_compound_scanners: BTreeMap, // (name of the KW scanner, code) @@ -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, + + // Defines the `Lexer::next_terminal` method. scanner_contexts: BTreeMap, /// Defines the top-level parser functions in `Language`. @@ -85,12 +88,6 @@ struct ScannerContextAccumulatorState { keyword_scanner_defs: BTreeMap, } -/// 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, -} - impl ParserModel { pub fn from_language(language: &Rc) -> Self { // First, we construct the DSLv1 model from the DSLv2 definition... @@ -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() } } @@ -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() @@ -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, } } } @@ -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()); @@ -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()); diff --git a/crates/solidity/outputs/cargo/slang_solidity/src/generated/generated/language.rs b/crates/solidity/outputs/cargo/slang_solidity/src/generated/generated/language.rs index e935294f83..60a8195b34 100644 --- a/crates/solidity/outputs/cargo/slang_solidity/src/generated/generated/language.rs +++ b/crates/solidity/outputs/cargo/slang_solidity/src/generated/generated/language.rs @@ -31,71 +31,37 @@ use crate::parser_support::{ #[derive(Debug)] #[cfg_attr(feature = "slang_napi_interfaces", napi(namespace = "language"))] pub struct Language { - #[allow(dead_code)] - pub(crate) version_is_at_least_0_4_11: bool, - #[allow(dead_code)] pub(crate) version_is_at_least_0_4_12: bool, - #[allow(dead_code)] pub(crate) version_is_at_least_0_4_14: bool, - #[allow(dead_code)] pub(crate) version_is_at_least_0_4_16: bool, - #[allow(dead_code)] pub(crate) version_is_at_least_0_4_21: bool, - #[allow(dead_code)] pub(crate) version_is_at_least_0_4_22: bool, - #[allow(dead_code)] pub(crate) version_is_at_least_0_4_25: bool, - #[allow(dead_code)] pub(crate) version_is_at_least_0_5_0: bool, - #[allow(dead_code)] pub(crate) version_is_at_least_0_5_3: bool, - #[allow(dead_code)] pub(crate) version_is_at_least_0_5_5: bool, - #[allow(dead_code)] pub(crate) version_is_at_least_0_5_8: bool, - #[allow(dead_code)] pub(crate) version_is_at_least_0_5_10: bool, - #[allow(dead_code)] pub(crate) version_is_at_least_0_5_12: bool, - #[allow(dead_code)] pub(crate) version_is_at_least_0_5_14: bool, - #[allow(dead_code)] pub(crate) version_is_at_least_0_6_0: bool, - #[allow(dead_code)] pub(crate) version_is_at_least_0_6_2: bool, - #[allow(dead_code)] pub(crate) version_is_at_least_0_6_5: bool, - #[allow(dead_code)] pub(crate) version_is_at_least_0_6_7: bool, - #[allow(dead_code)] pub(crate) version_is_at_least_0_6_8: bool, - #[allow(dead_code)] pub(crate) version_is_at_least_0_6_11: bool, - #[allow(dead_code)] pub(crate) version_is_at_least_0_7_0: bool, - #[allow(dead_code)] pub(crate) version_is_at_least_0_7_1: bool, - #[allow(dead_code)] pub(crate) version_is_at_least_0_7_4: bool, - #[allow(dead_code)] pub(crate) version_is_at_least_0_8_0: bool, - #[allow(dead_code)] pub(crate) version_is_at_least_0_8_4: bool, - #[allow(dead_code)] pub(crate) version_is_at_least_0_8_7: bool, - #[allow(dead_code)] pub(crate) version_is_at_least_0_8_8: bool, - #[allow(dead_code)] pub(crate) version_is_at_least_0_8_10: bool, - #[allow(dead_code)] pub(crate) version_is_at_least_0_8_13: bool, - #[allow(dead_code)] pub(crate) version_is_at_least_0_8_18: bool, - #[allow(dead_code)] pub(crate) version_is_at_least_0_8_19: bool, - #[allow(dead_code)] pub(crate) version_is_at_least_0_8_22: bool, - #[allow(dead_code)] pub(crate) version_is_at_least_0_8_24: bool, pub(crate) version: Version, } @@ -204,7 +170,6 @@ impl Language { pub fn new(version: Version) -> std::result::Result { if Self::SUPPORTED_VERSIONS.binary_search(&version).is_ok() { Ok(Self { - version_is_at_least_0_4_11: Version::new(0, 4, 11) <= version, version_is_at_least_0_4_12: Version::new(0, 4, 12) <= version, version_is_at_least_0_4_14: Version::new(0, 4, 14) <= version, version_is_at_least_0_4_16: Version::new(0, 4, 16) <= version, diff --git a/crates/testlang/outputs/cargo/slang_testlang/src/generated/generated/language.rs b/crates/testlang/outputs/cargo/slang_testlang/src/generated/generated/language.rs index 1a2da1f0ef..aed3cf41f7 100644 --- a/crates/testlang/outputs/cargo/slang_testlang/src/generated/generated/language.rs +++ b/crates/testlang/outputs/cargo/slang_testlang/src/generated/generated/language.rs @@ -31,7 +31,6 @@ use crate::parser_support::{ #[derive(Debug)] #[cfg_attr(feature = "slang_napi_interfaces", napi(namespace = "language"))] pub struct Language { - #[allow(dead_code)] pub(crate) version_is_at_least_1_0_0: bool, pub(crate) version: Version, }