forked from NomicFoundation/slang
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Collect syntax
kinds
directly from DSL v2 and isolate parser genera…
…tion logic (NomicFoundation#991) Work towards NomicFoundation#638 Mainly: - We bundle `quote`ing or any (old) parser codegen now in the new `generator/src/parser/codegen` module - To expand the `kinds.rs.jinja2` we use a new, dedicated `KindsModel` to disentangle, limiting the workarounds like removing the built-in labels only after visiting the grammar - simplified collecting the `ParserModel.referenced_versions` (it's de facto `Language::collect_breaking_changes` but with a caveat; I'll submit a separate PR for that since it's not 100% obvious and would change how we define `ParserModel`). - and some minor fixes like `pub(super)` visibility or updated referenced issues. Now, the remaining work will be to actually revamp how we visit the resulting grammar.
- Loading branch information
Showing
17 changed files
with
216 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
use std::collections::BTreeSet; | ||
|
||
use codegen_language_definition::model::{self, Identifier, Item}; | ||
use serde::Serialize; | ||
|
||
#[derive(Default, Serialize)] | ||
pub struct KindsModel { | ||
/// Defines the `NonterminalKind` enum variants. | ||
nonterminal_kinds: BTreeSet<Identifier>, | ||
/// Defines the `TerminalKind` enum variants. | ||
terminal_kinds: BTreeSet<Identifier>, | ||
/// Defines `TerminalKind::is_trivia` method. | ||
trivia_scanner_names: BTreeSet<Identifier>, | ||
/// Defines `EdgeLabel` enum variants. | ||
labels: BTreeSet<Identifier>, | ||
// Defines the `LexicalContext(Type)` enum and type-level variants. | ||
lexical_contexts: BTreeSet<Identifier>, | ||
} | ||
|
||
impl KindsModel { | ||
pub fn create(language: &model::Language) -> Self { | ||
let terminal_kinds = language | ||
.items() | ||
.filter(|item| item.is_terminal() && !matches!(item, Item::Fragment { .. })) | ||
.map(|item| item.name().clone()) | ||
.collect(); | ||
|
||
let mut nonterminal_kinds = BTreeSet::default(); | ||
for item in language.items() { | ||
match item { | ||
Item::Struct { item } => { | ||
nonterminal_kinds.insert(item.name.clone()); | ||
} | ||
Item::Enum { item } => { | ||
nonterminal_kinds.insert(item.name.clone()); | ||
} | ||
Item::Repeated { item } => { | ||
nonterminal_kinds.insert(item.name.clone()); | ||
} | ||
Item::Separated { item } => { | ||
nonterminal_kinds.insert(item.name.clone()); | ||
} | ||
Item::Precedence { item } => { | ||
nonterminal_kinds.insert(item.name.clone()); | ||
for op in &item.precedence_expressions { | ||
nonterminal_kinds.insert(op.name.clone()); | ||
} | ||
} | ||
// Terminals | ||
_ => {} | ||
} | ||
} | ||
|
||
let trivia_scanner_names = language | ||
.items() | ||
.filter_map(|item| match item { | ||
Item::Trivia { item } => Some(item.name.clone()), | ||
_ => None, | ||
}) | ||
.collect(); | ||
|
||
let mut labels = BTreeSet::default(); | ||
for item in language.items() { | ||
match item { | ||
Item::Struct { item } => { | ||
for field_name in item.fields.keys() { | ||
labels.insert(field_name.clone()); | ||
} | ||
} | ||
Item::Precedence { item } => { | ||
for item in &item.precedence_expressions { | ||
for item in &item.operators { | ||
for field_name in item.fields.keys() { | ||
labels.insert(field_name.clone()); | ||
} | ||
} | ||
} | ||
} | ||
_ => {} | ||
} | ||
} | ||
|
||
let lexical_contexts: BTreeSet<_> = language | ||
.topics() | ||
.filter_map(|t| t.lexical_context.as_ref()) | ||
.cloned() | ||
.chain(std::iter::once(Identifier::from("Default"))) | ||
.collect(); | ||
|
||
KindsModel { | ||
nonterminal_kinds, | ||
terminal_kinds, | ||
trivia_scanner_names, | ||
labels, | ||
lexical_contexts, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ use serde::Serialize; | |
use crate::model::RuntimeModel; | ||
|
||
mod ast; | ||
mod kinds; | ||
mod model; | ||
mod parser; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.