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.
refactor: Separate a dedicated model for generated kinds.rs
- Loading branch information
Showing
5 changed files
with
109 additions
and
93 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
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
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