Skip to content

Commit

Permalink
Apply feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Xanewok committed Jun 3, 2024
1 parent 2d8b7dd commit 44b4ac5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 32 deletions.
14 changes: 11 additions & 3 deletions crates/codegen/language/definition/src/model/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,23 @@ impl Item {
}
}

/// Whether the language item corresponds to a dedicated terminal kind.
pub fn is_terminal(&self) -> bool {
// NOTE: `Item::Fragment` is inlined.
matches!(
self,
Item::Trivia { .. } | Item::Keyword { .. } | Item::Token { .. } | Item::Fragment { .. }
Item::Trivia { .. } | Item::Keyword { .. } | Item::Token { .. }
)
}

pub fn is_nonterminal(&self) -> bool {
// Items are disjoint, hence it's enough to negate this.
!self.is_terminal()
matches!(
self,
Item::Struct { .. }
| Item::Enum { .. }
| Item::Repeated { .. }
| Item::Separated { .. }
| Item::Precedence { .. }
)
}
}
38 changes: 9 additions & 29 deletions crates/codegen/runtime/generator/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,43 +58,23 @@ pub struct Separated {

impl AstModel {
pub fn create(language: &model::Language) -> Self {
let mut model = Self::default();

// First pass: collect all terminals:
model.collect_terminals(language);
let mut model = Self {
terminals: language
.items()
.filter(|item| item.is_terminal())
.map(|item| item.name())
.cloned()
.collect(),
..Self::default()
};

// Second pass: use them to build nonterminals:
model.collect_nonterminals(language);

model
}

fn collect_terminals(&mut self, language: &model::Language) {
for item in language.items() {
match item {
model::Item::Struct { .. }
| model::Item::Enum { .. }
| model::Item::Repeated { .. }
| model::Item::Separated { .. }
| model::Item::Precedence { .. } => {
// These items are nonterminals.
}
model::Item::Trivia { item } => {
self.terminals.insert(item.name.clone());
}
model::Item::Keyword { item } => {
self.terminals.insert(item.name.clone());
}
model::Item::Token { item } => {
self.terminals.insert(item.name.clone());
}
model::Item::Fragment { .. } => {
// These items are inlined.
}
};
}
}

fn collect_nonterminals(&mut self, language: &model::Language) {
for item in language.items() {
match item {
Expand Down

0 comments on commit 44b4ac5

Please sign in to comment.