Skip to content

Commit

Permalink
Ignore the EOF rule
Browse files Browse the repository at this point in the history
EOF is not a real rule in ANTLR, it works as a parsing instruction to
ensure to match the entire file. However, during generation it doesn't
have any importance, hence it can be safely ignored.
  • Loading branch information
renatahodovan committed Dec 11, 2023
1 parent 4314fed commit 4656c4b
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 36 deletions.
8 changes: 2 additions & 6 deletions examples/fuzzer/HTMLGenerator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Grammarinator 23.7.post72+gccf98cc
# Generated by Grammarinator 23.7.post71+g23fe545

from math import inf
from grammarinator.runtime import *
Expand Down Expand Up @@ -37,9 +37,6 @@ def _style_sheet(self):
def _endOfHtmlElement(self):
pass

def EOF(self, parent=None):
return None

def HTML_COMMENT(self, parent=None):
with UnlexerRuleContext(self, 'HTML_COMMENT', parent) as rule:
current = rule.current
Expand Down Expand Up @@ -727,10 +724,9 @@ def style(self, parent=None):

_default_rule = htmlDocument

_immutable_rules = ('EOF', 'TAG_CLOSE', 'TAG_EQUALS', 'TAG_OPEN', 'TAG_SLASH', 'TAG_SLASH_CLOSE')
_immutable_rules = ('TAG_CLOSE', 'TAG_EQUALS', 'TAG_OPEN', 'TAG_SLASH', 'TAG_SLASH_CLOSE')

_rule_sizes = {
'EOF': RuleSize(0, 0),
'HTML_COMMENT': RuleSize(0, 0),
'HTML_CONDITIONAL_COMMENT': RuleSize(0, 0),
'XML_DECLARATION': RuleSize(0, 0),
Expand Down
2 changes: 1 addition & 1 deletion grammarinator/tool/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def _antlr_to_grammarinator_tree(self, antlr_node, parser, visited=None):
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], antlr_node.symbol.text) if antlr_node.symbol.type != Token.EOF else ('EOF', '')
name, text = (parser.symbolicNames[antlr_node.symbol.type], antlr_node.symbol.text)
assert name, f'{name} is None or empty'

if not self._hidden:
Expand Down
6 changes: 3 additions & 3 deletions grammarinator/tool/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -989,7 +989,7 @@ def build_expr(node, parent_id):
if '_dot' not in graph.vertices:
# Create an artificial `_dot` rule with an alternation of all the lexer rules.
parser_dot_id = graph.add_node(UnparserRuleNode(name='_dot', label=None))
unlexer_ids = [v.name for vid, v in graph.vertices.items() if isinstance(v, UnlexerRuleNode) and v.id != 'EOF']
unlexer_ids = [v.name for vid, v in graph.vertices.items() if isinstance(v, UnlexerRuleNode)]
alt_id = graph.add_node(AlternationNode(rule_id=parser_dot_id, idx=0, conditions=[1] * len(unlexer_ids)))
graph.add_edge(frm=parser_dot_id, to=alt_id)
for i, lexer_id in enumerate(unlexer_ids):
Expand Down Expand Up @@ -1033,7 +1033,8 @@ def build_expr(node, parent_id):

elif isinstance(node, ANTLRv4Parser.TerminalContext):
if node.TOKEN_REF():
graph.add_edge(frm=parent_id, to=str(node.TOKEN_REF()))
if str(node.TOKEN_REF() != 'EOF'):
graph.add_edge(frm=parent_id, to=str(node.TOKEN_REF()))

elif node.STRING_LITERAL():
src = unescape_string(str(node.STRING_LITERAL())[1:-1])
Expand Down Expand Up @@ -1136,7 +1137,6 @@ def build_rules(node):

graph = GrammarGraph()
lambda_id = graph.add_node(LambdaNode())
graph.add_node(UnlexerRuleNode(name='EOF'))

for root in [lexer_root, parser_root]:
if root:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ class {{ graph.name }}({{ graph.superclass }}):

{% for rule in graph.rules %}
def {{ rule.id }}(self, {% for t, k, v in rule.args %}{{ k }}{% if t %}:{{ t }}{% endif %}{% if v %}={{ resolveVarRefs(v) }}{% endif %}, {% endfor %}parent=None):
{% if rule.id != 'EOF' %}
{% if rule.labels or rule.args or rule.locals or rule.returns %}
local_ctx = {
{%- for _, k, _ in rule.args -%}
Expand All @@ -169,9 +168,6 @@ class {{ graph.name }}({{ graph.superclass }}):
current.{{ k }} = local_ctx['{{ k }}']
{% endfor %}
return current
{% else %}
return None
{% endif %}

{% endfor %}

Expand Down
22 changes: 0 additions & 22 deletions tests/grammars/Eof.g4

This file was deleted.

0 comments on commit 4656c4b

Please sign in to comment.