Skip to content

Commit 956cb0a

Browse files
authored
Fix compilation errors in the C++ generator template (#331)
- Fix incorrectly typed code in assignments to local variables. - Enforce casting of weights specified in the grammar to doubles. - Fix incorrect initialization of arguments, locals, returns, and labels in rules - Don't add rules with arguments to the map of rules-names-to-rule-methods (`_rule_fns`) since they have an incompatible signature.
1 parent d5f41fd commit 956cb0a

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

grammarinator/tool/resources/codegen/GeneratorTemplate.hpp.jinja

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
{% endmacro %}
1313

1414
{% macro processVariableNode(node, inedge) %}
15-
local_ctx.{{ node.name }}{% if node.is_list %}.push_back(current->last_child()){% else %} = current->last_child(){% endif %};
15+
local_ctx.{{ node.name }}{% if node.is_list %}.push_back(static_cast<ParentRule*>(current)->last_child()){% else %} = static_cast<ParentRule*>(current)->last_child(){% endif %};
1616
{% endmacro %}
1717

1818

@@ -63,7 +63,7 @@ current = rule.current();
6363

6464
{% macro processAlternationNode(node, inedge) %}
6565
{
66-
AlternationContext alt{{ node.idx }}(rule, {{ node.idx }}, {{ graph.name }}::_alt_sizes[{{ node.min_sizes }}], {{ inedge.reserve }}, {% if node.conditions is sequence %}{ {{ resolveVarRefs(node.conditions | join(', ')) }} }{% else %}{{ graph.name }}::_alt_conds[{{ node.conditions }}]{% endif %});
66+
AlternationContext alt{{ node.idx }}(rule, {{ node.idx }}, {{ graph.name }}::_alt_sizes[{{ node.min_sizes }}], {{ inedge.reserve }}, {% if node.conditions is sequence %}{ {% for cond in node.conditions %}static_cast<double>({{ resolveVarRefs(cond) }}){% if not loop.last %}, {% endif %}{% endfor %}}{% else %}{{ graph.name }}::_alt_conds[{{ node.conditions }}]{% endif %});
6767
current = rule.current();
6868
{% set simple_lits, simple_rules = node.simple_alternatives() %}
6969
{# In case of alternations with simple literals or rules, the selected option doesn't need to care about reserved tokens, since they have no siblings to spare budget for. #}
@@ -171,15 +171,25 @@ public:
171171
{% if rule.labels or rule.args or rule.locals or rule.returns %}
172172
struct {
173173
{% for t, k, _ in rule.args %}
174-
{{ t }} {{ k }} = {{ k }};
174+
{{ t }} {{ k }};
175175
{% endfor %}
176176
{% for t, k, v in (rule.locals + rule.returns) %}
177-
{{ t }} {{ k }}{% if v %} = {{ resolveVarRefs(v) }}{% endif %};
177+
{{ t }} {{ k }};
178178
{% endfor %}
179179
{% for name, is_list in rule.labels.items() %}
180-
{%+ if is_list %}std::vector<{% endif %}Rule*{% if is_list%}>{% endif %} {{ name }}{% if is_list %} = {}{% endif %};
180+
{%+ if is_list %}std::vector<{% endif %}Rule*{% if is_list%}>{% endif %} {{ name }};
181181
{% endfor %}
182-
} local_ctx;
182+
} local_ctx {
183+
{% for t, k, _ in rule.args %}
184+
.{{ k }} = {{ k }},
185+
{% endfor %}
186+
{% for t, k, v in (rule.locals + rule.returns) %}
187+
{%+ if v %} .{{ k }} = {{ resolveVarRefs(v) }},{% endif %}
188+
{% endfor %}
189+
{% for name, is_list in rule.labels.items() %}
190+
{%+ if is_list %}.{{ name }} = {},{% endif %}
191+
{% endfor %}
192+
};
183193
{% endif %}
184194
{{ rule.type }}Context rule(this, "{% if rule.trampoline %}{{ rule.id[0] }}{% else %}{{ rule.name }}{% endif %}", parent{% if rule.type == 'UnlexerRule' and (rule.name,) in graph.immutables %}, true{% endif %});
185195
Rule* current = rule.current();
@@ -205,7 +215,7 @@ public:
205215

206216
inline static const std::unordered_map<std::string, RuleFn> _rule_fns = {
207217
{% for rule in graph.rules %}
208-
{ "{{ rule.id | join('_') }}", &{{ graph.name }}::{{ rule.id | join('_') }} },
218+
{%+ if rule.args %}// {% endif %}{ "{{ rule.id | join('_') }}", &{{ graph.name }}::{{ rule.id | join('_') }} },
209219
{% endfor %}
210220
};
211221

0 commit comments

Comments
 (0)