1+ {# Macros for field generation #}
2+ {% macro render_record_field (field_name , field ) -%}
3+ {{ field_name }}: {% for child_name , child in field .children .items () -%}
4+ {% - set child_type = child |record_child_type -%}
5+ {% - if child .type == 'keyword' -%}
6+ "{{ child.name }}"i
7+ {% - elif child .type == 'union' -%}
8+ {{ child_name }}
9+ {% - else -%}
10+ {{ child_type }}
11+ {% - endif -%}
12+ {% - if not loop .last %} {% endif -%}
13+ {% - endfor %}
14+ {% - endmacro %}
15+
16+ {% macro render_union_field (field_name , field ) -%}
17+ {{ field_name }}: {% for child_name , child in field .children .items () -%}
18+ {{ field_name }}_{{ child_name }}
19+ {% - if not loop .last %} | {% endif -%}
20+ {% - endfor %}
21+ {% for child_name , child in field .children .items () %}
22+ {{ field_name }}_{{ child_name }}: {% if child .type == 'keyword' -%}
23+ "{{ child.name }}"i
24+ {% - else -%}
25+ "{{ child.name }}"i {{ child.type }}
26+ {% - endif %}
27+ {% endfor -%}
28+ {% - endmacro %}
29+
30+ {% macro render_simple_field (field_name , field , field_type ) -%}
31+ {{ field_name }}: "{{ field.name }}"i {{ field_type }}
32+ {% - endmacro %}
33+
34+ {% macro render_nested_union (child_name , child ) -%}
35+ {{ child_name }}: {% for opt_name , opt in child .children .items () -%}
36+ {{ child_name }}_{{ opt_name }}
37+ {% - if not loop .last %} | {% endif -%}
38+ {% - endfor %}
39+ {% for opt_name , opt in child .children .items () %}
40+ {{ child_name }}_{{ opt_name }}: {% if opt .type == 'keyword' -%}
41+ "{{ opt.name }}"i
42+ {% - else -%}
43+ "{{ opt.name }}"i {{ opt.type }}
44+ {% - endif %}
45+ {% endfor -%}
46+ {% - endmacro %}
47+
148// Auto-generated grammar for MF6 {{ name|upper }}
249%import common.WS
350%import common.SH_COMMENT
653%ignore WS
754%ignore SH_COMMENT
855
56+ {# Pre-compute block metadata to avoid multiple iterations #}
57+ {% set block_metadata = {} %}
58+ {% for block_name , block_fields in blocks .items () %}
59+ {% set period_groups = block_fields |group_period_fields %}
60+ {% set _ = block_metadata .update ({
61+ block_name : {
62+ 'fields' : block_fields ,
63+ 'period_groups' : period_groups ,
64+ 'recarray_name' : (block_name |get_recarray_name ) if period_groups else none ,
65+ 'has_index' : block_name == 'period'
66+ }
67+ }) %}
68+ {% endfor %}
69+
970// Top-level structure
1071start: block*
1172block: {% for block_name in blocks .keys () %}
@@ -14,36 +75,34 @@ block: {% for block_name in blocks.keys() %}
1475{% - endfor %}
1576
1677// Block definitions
17- {% for block_name , block_fields in blocks .items () %}
18- {% if block_name == 'period' %}
78+ {% for block_name , meta in block_metadata .items () %}
79+ {% if meta . has_index %}
1980{{ block_name }}_block: "begin"i "{{ block_name }}"i block_index {{ block_name }}_fields "end"i "{{ block_name }}"i block_index
2081{% else %}
2182{{ block_name }}_block: "begin"i "{{ block_name }}"i {{ block_name }}_fields "end"i "{{ block_name }}"i
2283{% endif %}
2384{% endfor %}
24- {% if 'period' in blocks %}
85+ {% if block_metadata . values ()| selectattr ( 'has_index' )| list %}
2586block_index: integer
2687{% endif %}
2788// Block field lists
28- {% for block_name , block_fields in blocks .items () %}
29- {% set period_groups = block_fields |group_period_fields %}
30- {% if period_groups %}
89+ {% for block_name , meta in block_metadata .items () %}
90+ {% if meta .period_groups %}
3191{# This block has period data (e.g., stress_period_data), split fields accordingly #}
32- {% set recarray_name = block_name |get_recarray_name %}
33- {% set grouped_field_names = period_groups .values ()|first %}
92+ {% set grouped_field_names = meta .period_groups .values ()|first %}
3493{% set non_grouped_fields = [] %}
35- {% for field_name in block_fields .keys () %}
94+ {% for field_name in meta . fields .keys () %}
3695 {% - if field_name not in grouped_field_names %}
3796 {% - set _ = non_grouped_fields .append (field_name ) %}
3897 {% - endif %}
3998{% - endfor %}
4099{{ block_name }}_fields: (
41100 {% - for field_name in non_grouped_fields %} {{ field_name }} | {% endfor -%}
42- {{ recarray_name }})*
101+ {{ meta. recarray_name }})*
43102{% else %}
44103{# Regular block - just list all fields #}
45104{{ block_name }}_fields: (
46- {% - for field_name in block_fields .keys () %}
105+ {% - for field_name in meta . fields .keys () %}
47106 {{- field_name }}{% if not loop .last %} | {% endif %}
48107 {% - endfor -%}
49108)*
@@ -55,21 +114,11 @@ block_index: integer
55114{% if field_name not in grouped_fields %}
56115{% set field_type = field |field_type %}
57116{% if field .type == 'record' and field .children is not none %}
58- {{ field_name }}: {% for child_name , child in field .children .items () %}{% set child_type = child |record_child_type
59- %}{% if child .type == 'keyword' %} "{{ child.name }}"i{% elif child .type == 'union' %} {{ child_name }}{%
60- else %} {{ child_type }}{% endif %}{% if not loop .last %} {% endif %}{% endfor %}
61-
117+ {{ render_record_field(field_name, field) }}
62118{% elif field .type == 'union' and field .children is not none %}
63- {{ field_name }}: {% for child_name , child in field .children .items () %} {{ field_name }}_{{ child_name }}{% if not
64- loop .last %} | {% endif %}{% endfor %}
65- {% for child_name , child in field .children .items () %}
66-
67- {{ field_name }}_{{ child_name }}: {% if child .type == 'keyword' %} "{{ child.name }}"i{% else %} "{{ child.name }}"i {{
68- child.type }}{% endif %}
69-
70- {% endfor %}
119+ {{ render_union_field(field_name, field) }}
71120{% else %}
72- {{ field_name }}: "{{ field.name }}"i {{ field_type }}
121+ {{ render_simple_field( field_name, field, field_type) }}
73122{% endif %}
74123{% endif %}
75124{% endfor %}
@@ -79,29 +128,16 @@ child.type }}{% endif %}
79128{% if field .type == 'record' and field .children is not none %}
80129{% for child_name , child in field .children .items () %}
81130{% if child .type == 'union' and child_name not in unions_generated %}
82- {{ child_name }}: {% for opt_name , opt in child .children .items () %} {{ child_name }}_{{ opt_name }}{% if not loop .last
83- %} | {% endif %}
84- {% endfor %}
85-
86- {% for opt_name , opt in child .children .items () %}
87- {{ child_name }}_{{ opt_name }}: {% if opt .type == 'keyword' %} "{{ opt.name }}"i{% else %} "{{ opt.name }}"i {{
88- opt.type }}{% endif %}
89-
90- {% endfor %}
91-
131+ {{ render_nested_union(child_name, child) }}
92132{% set _ = unions_generated .append (child_name ) %}
93133{% endif %}
94134{% endfor %}
95135{% endif %}
96136{% endfor %}
97137// Recarray rules for period data (stress_period_data, etc.)
98- {% for block_name , block_fields in blocks .items () %}
99- {% set period_groups = block_fields |group_period_fields %}
100- {% if period_groups %}
101- {% set recarray_name = block_name |get_recarray_name %}
102- {% set field_names = period_groups .values ()|first %}
103- {% set columns = field_names |get_recarray_columns (block_fields ) %}
104- {{ recarray_name}}: (number | simple_string)+ NEWLINE
138+ {% for block_name , meta in block_metadata .items () %}
139+ {% if meta .recarray_name %}
140+ {{ meta.recarray_name }}: (number | simple_string)+ NEWLINE
105141{% endif %}
106142{% endfor %}
107143
0 commit comments