Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor BuiltInLabel to avoid duplication #992

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion crates/codegen/ebnf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 2 additions & 17 deletions crates/codegen/ebnf/src/builder.rs
Original file line number Diff line number Diff line change
@@ -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,
};
Expand All @@ -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,
Expand Down
1 change: 1 addition & 0 deletions crates/codegen/language/definition/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
13 changes: 13 additions & 0 deletions crates/codegen/language/definition/src/model/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,16 @@ pub struct Topic {

pub items: Vec<Item>,
}

#[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,
}
12 changes: 3 additions & 9 deletions crates/codegen/runtime/cargo/src/runtime/kinds.rs.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -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 -%}
Expand Down
2 changes: 1 addition & 1 deletion crates/codegen/runtime/generator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
21 changes: 19 additions & 2 deletions crates/codegen/runtime/generator/src/kinds.rs
Original file line number Diff line number Diff line change
@@ -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<Identifier>,
Expand All @@ -13,10 +14,25 @@ pub struct KindsModel {
trivia_scanner_names: BTreeSet<Identifier>,
/// Defines `EdgeLabel` enum variants.
labels: BTreeSet<Identifier>,
/// Built-in labels for edges
built_in_labels: &'static [&'static str],
// Defines the `LexicalContext(Type)` enum and type-level variants.
lexical_contexts: BTreeSet<Identifier>,
}

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
Expand Down Expand Up @@ -93,6 +109,7 @@ impl KindsModel {
trivia_scanner_names,
labels,
lexical_contexts,
..Self::default()
}
}
}
6 changes: 3 additions & 3 deletions crates/codegen/runtime/generator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ pub enum OutputLanguage {
#[derive(Serialize)]
struct ModelWrapper {
rendering_in_stubs: bool,
model: Option<RuntimeModel>,
model: RuntimeModel,
}

impl OutputLanguage {
pub fn generate_runtime(&self, language: &Rc<Language>, 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()?)?;
Expand All @@ -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()?)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -793,21 +795,6 @@ trait LabeledExt<T> {
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<T> LabeledExt<T> for Labeled<T> {
fn anonymous(value: T) -> Self {
Self {
Expand Down