diff --git a/Cargo.lock b/Cargo.lock index 1aa8cb7940..5ecf8e325a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -330,7 +330,6 @@ dependencies = [ "codegen_language_definition", "derive-new", "indexmap", - "strum_macros", ] [[package]] @@ -346,6 +345,7 @@ dependencies = [ "quote", "semver", "serde", + "strum", "strum_macros", "syn", "thiserror", @@ -413,7 +413,7 @@ dependencies = [ "quote", "semver", "serde", - "strum_macros", + "strum", ] [[package]] diff --git a/crates/codegen/ebnf/Cargo.toml b/crates/codegen/ebnf/Cargo.toml index 43cebdead0..3c91582b73 100644 --- a/crates/codegen/ebnf/Cargo.toml +++ b/crates/codegen/ebnf/Cargo.toml @@ -11,7 +11,6 @@ codegen_language_definition = { workspace = true } derive-new = { workspace = true } indexmap = { workspace = true } Inflector = { workspace = true } -strum_macros = { workspace = true } [lints] workspace = true diff --git a/crates/codegen/ebnf/src/builder.rs b/crates/codegen/ebnf/src/builder.rs index a77dc38885..9b90053be9 100644 --- a/crates/codegen/ebnf/src/builder.rs +++ b/crates/codegen/ebnf/src/builder.rs @@ -1,8 +1,8 @@ use std::collections::HashMap; use codegen_language_definition::model::{ - EnumItem, EnumVariant, Field, FragmentItem, Identifier, Item, KeywordDefinition, KeywordItem, - KeywordValue, Language, OperatorModel, PrecedenceExpression, PrecedenceItem, + BuiltInLabel, EnumItem, EnumVariant, Field, FragmentItem, Identifier, Item, KeywordDefinition, + KeywordItem, KeywordValue, Language, OperatorModel, PrecedenceExpression, PrecedenceItem, PrecedenceOperator, PrimaryExpression, RepeatedItem, Scanner, SeparatedItem, StructItem, TokenDefinition, TokenItem, TriviaItem, VersionSpecifier, }; @@ -11,21 +11,6 @@ use inflector::Inflector; use crate::model::{Definition, DefinitionKind, Entry, Expression, Value}; -#[allow(dead_code)] -#[derive(strum_macros::AsRefStr)] -#[strum(serialize_all = "snake_case")] -enum BuiltInLabel { - // _SLANG_INTERNAL_RESERVED_NODE_LABELS_ (keep in sync) - Item, - Variant, - Separator, - Operand, - LeftOperand, - RightOperand, - LeadingTrivia, - TrailingTrivia, -} - pub struct Builder { section_index: usize, topic_index: usize, diff --git a/crates/codegen/language/definition/Cargo.toml b/crates/codegen/language/definition/Cargo.toml index b1db94fb47..469be1631b 100644 --- a/crates/codegen/language/definition/Cargo.toml +++ b/crates/codegen/language/definition/Cargo.toml @@ -15,6 +15,7 @@ proc-macro2 = { workspace = true } quote = { workspace = true } semver = { workspace = true } serde = { workspace = true } +strum = { workspace = true } strum_macros = { workspace = true } syn = { workspace = true } thiserror = { workspace = true } diff --git a/crates/codegen/language/definition/src/model/manifest.rs b/crates/codegen/language/definition/src/model/manifest.rs index 2455b1ed99..92158ab153 100644 --- a/crates/codegen/language/definition/src/model/manifest.rs +++ b/crates/codegen/language/definition/src/model/manifest.rs @@ -127,3 +127,16 @@ pub struct Topic { pub items: Vec, } + +#[derive(strum_macros::AsRefStr, strum_macros::EnumIter, strum_macros::VariantNames)] +#[strum(serialize_all = "snake_case")] +pub enum BuiltInLabel { + Item, + Variant, + Separator, + Operand, + LeftOperand, + RightOperand, + LeadingTrivia, + TrailingTrivia, +} diff --git a/crates/codegen/runtime/cargo/src/runtime/kinds.rs.jinja2 b/crates/codegen/runtime/cargo/src/runtime/kinds.rs.jinja2 index 850d2499ce..497b2e05a7 100644 --- a/crates/codegen/runtime/cargo/src/runtime/kinds.rs.jinja2 +++ b/crates/codegen/runtime/cargo/src/runtime/kinds.rs.jinja2 @@ -49,15 +49,9 @@ impl metaslang_cst::NonterminalKind for NonterminalKind {} #[cfg_attr(not(feature = "slang_napi_interfaces"), derive(Clone, Copy))] pub enum EdgeLabel { // Built-in: - {# _SLANG_INTERNAL_RESERVED_NODE_LABELS_ (keep in sync) #} - Item, - Variant, - Separator, - Operand, - LeftOperand, - RightOperand, - LeadingTrivia, - TrailingTrivia, + {% for label in model.kinds.built_in_labels -%} + {{ label | pascal_case }}, + {%- endfor %} // Generated: {% if rendering_in_stubs -%} diff --git a/crates/codegen/runtime/generator/Cargo.toml b/crates/codegen/runtime/generator/Cargo.toml index 07ea3ec029..0e729750ec 100644 --- a/crates/codegen/runtime/generator/Cargo.toml +++ b/crates/codegen/runtime/generator/Cargo.toml @@ -17,7 +17,7 @@ proc-macro2 = { workspace = true } quote = { workspace = true } semver = { workspace = true } serde = { workspace = true } -strum_macros = { workspace = true } +strum = { workspace = true } [lints] workspace = true diff --git a/crates/codegen/runtime/generator/src/kinds.rs b/crates/codegen/runtime/generator/src/kinds.rs index 3d157ba90f..8b53dd95e4 100644 --- a/crates/codegen/runtime/generator/src/kinds.rs +++ b/crates/codegen/runtime/generator/src/kinds.rs @@ -1,9 +1,10 @@ use std::collections::BTreeSet; -use codegen_language_definition::model::{self, Identifier, Item}; +use codegen_language_definition::model::{self, BuiltInLabel, Identifier, Item}; use serde::Serialize; +use strum::VariantNames; -#[derive(Default, Serialize)] +#[derive(Serialize)] pub struct KindsModel { /// Defines the `NonterminalKind` enum variants. nonterminal_kinds: BTreeSet, @@ -13,10 +14,25 @@ pub struct KindsModel { trivia_scanner_names: BTreeSet, /// Defines `EdgeLabel` enum variants. labels: BTreeSet, + /// Built-in labels for edges + built_in_labels: &'static [&'static str], // Defines the `LexicalContext(Type)` enum and type-level variants. lexical_contexts: BTreeSet, } +impl Default for KindsModel { + fn default() -> Self { + Self { + nonterminal_kinds: BTreeSet::default(), + terminal_kinds: BTreeSet::default(), + trivia_scanner_names: BTreeSet::default(), + labels: BTreeSet::default(), + built_in_labels: BuiltInLabel::VARIANTS, + lexical_contexts: BTreeSet::default(), + } + } +} + impl KindsModel { pub fn create(language: &model::Language) -> Self { let terminal_kinds = language @@ -93,6 +109,7 @@ impl KindsModel { trivia_scanner_names, labels, lexical_contexts, + ..Self::default() } } } diff --git a/crates/codegen/runtime/generator/src/lib.rs b/crates/codegen/runtime/generator/src/lib.rs index b74b1c8d9f..ee3b42b69f 100644 --- a/crates/codegen/runtime/generator/src/lib.rs +++ b/crates/codegen/runtime/generator/src/lib.rs @@ -25,14 +25,14 @@ pub enum OutputLanguage { #[derive(Serialize)] struct ModelWrapper { rendering_in_stubs: bool, - model: Option, + model: RuntimeModel, } impl OutputLanguage { pub fn generate_runtime(&self, language: &Rc, output_dir: &Path) -> Result<()> { let model = ModelWrapper { rendering_in_stubs: false, - model: Some(RuntimeModel::from_language(language)), + model: RuntimeModel::from_language(language), }; let mut templates = CodegenTemplates::new(self.source_dir()?)?; @@ -43,7 +43,7 @@ impl OutputLanguage { pub fn generate_stubs(&self) -> Result<()> { let model = ModelWrapper { rendering_in_stubs: true, - model: None, + model: RuntimeModel::default(), }; let mut templates = CodegenTemplates::new(self.source_dir()?)?; diff --git a/crates/codegen/runtime/generator/src/parser/grammar/constructor.rs b/crates/codegen/runtime/generator/src/parser/grammar/constructor.rs index 7b22ac1d6c..4e90b88044 100644 --- a/crates/codegen/runtime/generator/src/parser/grammar/constructor.rs +++ b/crates/codegen/runtime/generator/src/parser/grammar/constructor.rs @@ -5,7 +5,9 @@ use std::collections::{BTreeMap, BTreeSet, HashMap}; use std::ops::Deref; use std::rc::Rc; -use codegen_language_definition::model::{self, FieldsErrorRecovery, Identifier, Item}; +use codegen_language_definition::model::{ + self, BuiltInLabel, FieldsErrorRecovery, Identifier, Item, +}; use indexmap::IndexMap; use once_cell::sync::Lazy; @@ -793,21 +795,6 @@ trait LabeledExt { fn with_builtin_label(name: BuiltInLabel, node: T) -> Self; } -#[allow(dead_code)] -#[derive(strum_macros::AsRefStr)] -#[strum(serialize_all = "snake_case")] -enum BuiltInLabel { - // _SLANG_INTERNAL_RESERVED_NODE_LABELS_ (keep in sync) - Item, - Variant, - Separator, - Operand, - LeftOperand, - RightOperand, - LeadingTrivia, - TrailingTrivia, -} - impl LabeledExt for Labeled { fn anonymous(value: T) -> Self { Self {