diff --git a/cli/src/generate/render.rs b/cli/src/generate/render.rs index d16e3ba7..08803b1f 100644 --- a/cli/src/generate/render.rs +++ b/cli/src/generate/render.rs @@ -325,22 +325,18 @@ impl Generator { add_line!(self, "static TSSymbol ts_symbol_map[] = {{"); indent!(self); for symbol in &self.parse_table.symbols { + // There can be multiple symbols in the grammar that have the same name and kind, + // due to simple aliases. When that happens, ensure that they map to the same + // public-facing symbol. If one of the symbols is not aliased, choose that one + // to be the public-facing symbol. Otherwise, pick the symbol with the lowest + // numeric value. let mut mapping = symbol; - - // If this symbol has a simple alias, then check if its alias has the same - // name and kind (e.g. named vs anonymous) as some other symbol in the grammar. - // If so, add an entry to the symbol map that deduplicates these two symbols, - // so that only one of them will ever be returned via the public API. if let Some(alias) = self.simple_aliases.get(symbol) { let kind = alias.kind(); for other_symbol in &self.parse_table.symbols { - if other_symbol == symbol { - continue; - } if let Some(other_alias) = self.simple_aliases.get(other_symbol) { - if other_symbol < symbol && other_alias == alias { + if other_symbol < mapping && other_alias == alias { mapping = other_symbol; - break; } } else if self.metadata_for_symbol(*other_symbol) == (&alias.value, kind) { mapping = other_symbol; @@ -353,7 +349,7 @@ impl Generator { self, "[{}] = {},", self.symbol_ids[&symbol], - self.symbol_ids[&mapping], + self.symbol_ids[mapping], ); }