Skip to content

Commit

Permalink
Revert the helper changes in ast.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
Xanewok committed Jun 4, 2024
1 parent fe9c6b7 commit dd934b0
Showing 1 changed file with 29 additions and 9 deletions.
38 changes: 29 additions & 9 deletions crates/codegen/runtime/generator/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,43 @@ pub struct Separated {

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

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

// 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 dd934b0

Please sign in to comment.