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

Fix the trampoline functions of recurring labeled alternatives #243

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
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
9 changes: 5 additions & 4 deletions grammarinator/tool/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,13 @@ def __repr__(self):

class RuleNode(Node):

def __init__(self, name, type):
def __init__(self, name, type, trampoline=False):
name = name if isinstance(name, tuple) else (name,)
super().__init__(name)
# Keep rule and label name, but exclude label index if exists.
self.name = '_'.join(part for part in name[:2])
self.type = type
self.trampoline = trampoline
self.min_size = None

self.labels = {}
Expand Down Expand Up @@ -120,8 +121,8 @@ def __init__(self, name=None):

class UnparserRuleNode(RuleNode):

def __init__(self, name):
super().__init__(name, 'UnparserRule')
def __init__(self, name, trampoline=False):
super().__init__(name, 'UnparserRule', trampoline)


class ImagRuleNode(Node):
Expand Down Expand Up @@ -933,7 +934,7 @@ def build_expr(node, parent_id):
for label in recurring_labels:
# Mask conditions to enable only the alternatives with the common label.
new_conditions = [cond if labels[ci] == label else '0' for ci, cond in enumerate(conditions)]
recurring_rule_id = graph.add_node(UnparserRuleNode(name=(rule.name, label)))
recurring_rule_id = graph.add_node(UnparserRuleNode(name=(rule.name, label), trampoline=True))
labeled_alt_id = graph.add_node(AlternationNode(idx=0,
conditions=append_unique(graph.alt_conds, new_conditions) if all(isfloat(cond) for cond in new_conditions) else new_conditions,
rule_id=recurring_rule_id))
Expand Down
11 changes: 9 additions & 2 deletions grammarinator/tool/resources/codegen/GeneratorTemplate.py.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ class {{ graph.name }}({{ graph.superclass }}):
{% endfor -%}
}
{% endif %}
with {{ rule.type }}Context(self, '{{ rule.name }}', parent) as rule:
with {{ rule.type }}Context(self, {% if rule.trampoline %}'{{ rule.id[0] }}'{% else %}'{{ rule.name }}'{% endif %}, parent) as rule:
current = rule.current
{% if rule.init %}
{{ resolveVarRefs(rule.init) | indent | indent | indent }}
Expand All @@ -173,7 +173,14 @@ class {{ graph.name }}({{ graph.superclass }}):
{% for _, k, _ in rule.returns %}
current.{{ k }} = local_ctx['{{ k }}']
{% endfor %}
return current
{% if rule.trampoline %}
current.remove()
current = current.children[0].children[0]
current.remove()
if parent:
parent += current
{% endif %}
return current

{% endfor %}

Expand Down
57 changes: 57 additions & 0 deletions tests/grammars/RecurringLabelTrampolines.g4
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (c) 2024 Renata Hodovan, Akos Kiss.
*
* Licensed under the BSD 3-Clause License
* <LICENSE.rst or https://opensource.org/licenses/BSD-3-Clause>.
* This file may not be copied, modified, or distributed except
* according to those terms.
*/

/*
* This test checks whether the trampoline functions of recurring labeled
* alternatives create the correct tree structure.
*/

// TEST-PROCESS: {grammar}.g4 -o {tmpdir}
// TEST-GENERATE: {grammar}Generator.{grammar}Generator -r start -m {grammar}Generator.CustomModel -j 1 -n 5 -o {tmpdir}/{grammar}%d.txt

grammar RecurringLabelTrampolines;

@header {
from grammarinator.runtime import DispatchingModel

class CustomModel(DispatchingModel):

def __init__(self):
self.alt = 1

def choice_start(self, node, idx, weights):
self.alt = 1 - self.alt
return self.alt

}

start
@after {
assert current.name == 'start', current.name
assert isinstance(current.last_child, UnparserRuleAlternative), repr(current.last_child)
assert len(current.last_child.children) == 1, current.last_child.children
assert current.last_child.last_child.name == 'start_Foo', repr(current.last_child.last_child)
assert current.last_child.last_child.last_child.name == 'Bar', repr(current.last_child.last_child.last_child)
assert str(current) == 'bar', str(current)

current.last_child.last_child.replace(self.start_Foo())

assert current.name == 'start', current.name
assert isinstance(current.last_child, UnparserRuleAlternative), repr(current.last_child)
assert len(current.last_child.children) == 1, current.last_child.children
assert current.last_child.last_child.name == 'start_Foo', repr(current.last_child.last_child)
assert current.last_child.last_child.last_child.name == 'Baz', repr(current.last_child.last_child.last_child)
assert str(current) == 'baz', str(current)
}
: Bar # Foo
| Baz # Foo
;

Bar : 'bar' ;
Baz : 'baz' ;
Loading