Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure not adding EOF token to the parse tree #227

Merged
merged 1 commit into from
Jun 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading