Skip to content

Commit

Permalink
Ensure that label names in processor start with capital letter
Browse files Browse the repository at this point in the history
The change is needed since even if ANTLR enables to have lower-case
label names, but it will convert them to start with capital letter
eventually, since the Context class created from them must be capital.
Since the name of this Context class will be used by grammarinator-parse
to create nodes from labelled rules, we need to ensure a common
naming schema.
  • Loading branch information
renatahodovan committed Jun 5, 2024
1 parent 7bb9556 commit 6dfacc4
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions grammarinator/tool/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,9 @@ def build_expr(node, parent_id):
nonlocal alt_idx
conditions = [find_conditions(child) for child in children]
labels = [str(child.identifier().TOKEN_REF() or child.identifier().RULE_REF()) for child in children if child.identifier()] if isinstance(node, ANTLRv4Parser.RuleAltListContext) else []
# Ensure to start labels with capital letter, since ANTLR will also create a context with capital start character.
# It's important to keep them in sync since grammarinator-parse will use this graph for comparison.
labels = [label[0].upper() + label[1:] for label in labels]
recurring_labels = {name for name, cnt in Counter(labels).items() if cnt > 1}
assert len(labels) == 0 or len(labels) == len(children)
alt_id = graph.add_node(AlternationNode(idx=alt_idx, conditions=append_unique(graph.alt_conds, conditions) if all(isfloat(cond) for cond in conditions) else conditions, rule_id=rule.id))
Expand Down

0 comments on commit 6dfacc4

Please sign in to comment.