Skip to content

Commit

Permalink
Ensure not adding EOF token to the parse tree
Browse files Browse the repository at this point in the history
  • Loading branch information
renatahodovan committed Jun 3, 2024
1 parent a9d891d commit 85012c6
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion grammarinator/tool/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from os.path import basename, commonprefix, split, splitext
from subprocess import CalledProcessError, PIPE, run

from antlr4 import CommonTokenStream, error, FileStream, ParserRuleContext, TerminalNode
from antlr4 import CommonTokenStream, error, FileStream, ParserRuleContext, TerminalNode, Token

from ..runtime import RuleSize, UnlexerRule, UnparserRule

Expand Down Expand Up @@ -164,12 +164,16 @@ def _antlr_to_grammarinator_tree(self, antlr_node, parser, visited=None):
depth = 0
for antlr_child in (antlr_node.children or []):
child, child_depth = self._antlr_to_grammarinator_tree(antlr_child, parser, visited)
if not child:
continue
parent_node += child
depth = max(depth, child_depth + 1)
else:
assert isinstance(antlr_node, TerminalNode), f'An ANTLR node must either be a ParserRuleContext or a TerminalNode but {antlr_node.__class__.__name__} was found.'
name, text = parser.symbolicNames[antlr_node.symbol.type] if len(parser.symbolicNames) >= antlr_node.symbol.type else '<INVALID>', antlr_node.symbol.text
assert name, f'{name} is None or empty'
if antlr_node.symbol.type == Token.EOF:
return None, 0

if not self._hidden:
node = UnlexerRule(name=name, src=text)
Expand Down

0 comments on commit 85012c6

Please sign in to comment.