From c7a18ef821458ca414d36c2f1b84c22a39e99641 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Mon, 22 Apr 2024 21:50:26 -0400 Subject: [PATCH] Add AST node field name with no suffix when unique - #22 --- assets/parser.d.erb | 9 +++++-- assets/parser.h.erb | 9 +++++-- lib/propane/rule_set.rb | 34 ++++++++++++++++-------- spec/test_ast.c | 57 +++++++++++++++++++++-------------------- spec/test_ast.d | 57 +++++++++++++++++++++-------------------- 5 files changed, 96 insertions(+), 70 deletions(-) diff --git a/assets/parser.d.erb b/assets/parser.d.erb index e762a5d..052c9ec 100644 --- a/assets/parser.d.erb +++ b/assets/parser.d.erb @@ -74,8 +74,13 @@ public struct Token <% next if name.start_with?("$") %> public struct <%= name %> { -<% rule_set.ast_fields.each do |name, type| %> - <%= type %> * <%= name %>; +<% rule_set.ast_fields.each do |fields| %> + union + { +<% fields.each do |field_name, type| %> + <%= type %> * <%= field_name %>; +<% end %> + } <% end %> } diff --git a/assets/parser.h.erb b/assets/parser.h.erb index 963003c..644e961 100644 --- a/assets/parser.h.erb +++ b/assets/parser.h.erb @@ -71,8 +71,13 @@ struct <%= name %>; <% next if name.start_with?("$") %> typedef struct <%= name %> { -<% rule_set.ast_fields.each do |name, type| %> - struct <%= type %> * <%= name %>; +<% rule_set.ast_fields.each do |fields| %> + union + { +<% fields.each do |field_name, type| %> + struct <%= type %> * <%= field_name %>; +<% end %> + }; <% end %> } <%= name %>; diff --git a/lib/propane/rule_set.rb b/lib/propane/rule_set.rb index bb6a961..dbc09f3 100644 --- a/lib/propane/rule_set.rb +++ b/lib/propane/rule_set.rb @@ -78,16 +78,20 @@ class Propane # Build the set of AST fields for this RuleSet. # - # The keys are the field names and the values are the AST node structure - # names. + # This is an Array of Hashes. Each entry in the Array corresponds to a + # field location in the AST node. The entry is a Hash. It could have one or + # two keys. It will always have the field name with a positional suffix as + # a key. It may also have the field name without the positional suffix if + # that field only exists in one position across all Rules in the RuleSet. # - # @return [Hash] + # @return [Array] # AST fields. def ast_fields @_ast_fields ||= begin - field_indexes = {} - fields = {} + field_ast_node_indexes = {} + field_indexes_across_all_rules = {} + ast_node_fields = [] @rules.each do |rule| rule.components.each_with_index do |component, i| if component.is_a?(Token) @@ -96,14 +100,24 @@ class Propane node_name = component.name end field_name = "p#{node_name}#{i + 1}" - unless field_indexes[field_name] - field_indexes[field_name] = fields.size - fields[field_name] = node_name + unless field_ast_node_indexes[field_name] + field_ast_node_indexes[field_name] = ast_node_fields.size + ast_node_fields << {field_name => node_name} end - rule.rule_set_node_field_index_map[i] = field_indexes[field_name] + field_indexes_across_all_rules[node_name] ||= Set.new + field_indexes_across_all_rules[node_name] << field_ast_node_indexes[field_name] + rule.rule_set_node_field_index_map[i] = field_ast_node_indexes[field_name] end end - fields + field_indexes_across_all_rules.each do |node_name, indexes_across_all_rules| + if indexes_across_all_rules.size == 1 + # If this field was only seen in one position across all rules, + # then add an alias to the positional field name that does not + # include the position. + ast_node_fields[indexes_across_all_rules.first]["p#{node_name}"] = node_name + end + end + ast_node_fields end end diff --git a/spec/test_ast.c b/spec/test_ast.c index 45073e0..73e60cc 100644 --- a/spec/test_ast.c +++ b/spec/test_ast.c @@ -11,44 +11,45 @@ int main() assert_eq(P_SUCCESS, p_parse(&context)); Start * start = p_result(&context); assert(start->pItems1 != NULL); - Items * items = start->pItems1; - assert(items->pItem1 != NULL); - assert(items->pItem1->pToken1 != NULL); - assert_eq(TOKEN_a, items->pItem1->pToken1->token); - assert_eq(11, items->pItem1->pToken1->pvalue); - assert(items->pItemsMore2 != NULL); - ItemsMore * itemsmore = items->pItemsMore2; - assert(itemsmore->pItem2 != NULL); - assert(itemsmore->pItem2->pItem2 != NULL); - assert(itemsmore->pItem2->pItem2->pItem2 != NULL); - assert(itemsmore->pItem2->pItem2->pItem2->pToken1 != NULL); - assert_eq(TOKEN_b, itemsmore->pItem2->pItem2->pItem2->pToken1->token); - assert_eq(22, itemsmore->pItem2->pItem2->pItem2->pToken1->pvalue); - assert(itemsmore->pItemsMore3 != NULL); - itemsmore = itemsmore->pItemsMore3; - assert(itemsmore->pItem2 != NULL); - assert(itemsmore->pItem2->pToken1 != NULL); - assert_eq(TOKEN_b, itemsmore->pItem2->pToken1->token); - assert_eq(22, itemsmore->pItem2->pToken1->pvalue); - assert(itemsmore->pItemsMore3 == NULL); + assert(start->pItems != NULL); + Items * items = start->pItems; + assert(items->pItem != NULL); + assert(items->pItem->pToken1 != NULL); + assert_eq(TOKEN_a, items->pItem->pToken1->token); + assert_eq(11, items->pItem->pToken1->pvalue); + assert(items->pItemsMore != NULL); + ItemsMore * itemsmore = items->pItemsMore; + assert(itemsmore->pItem != NULL); + assert(itemsmore->pItem->pItem != NULL); + assert(itemsmore->pItem->pItem->pItem != NULL); + assert(itemsmore->pItem->pItem->pItem->pToken1 != NULL); + assert_eq(TOKEN_b, itemsmore->pItem->pItem->pItem->pToken1->token); + assert_eq(22, itemsmore->pItem->pItem->pItem->pToken1->pvalue); + assert(itemsmore->pItemsMore != NULL); + itemsmore = itemsmore->pItemsMore; + assert(itemsmore->pItem != NULL); + assert(itemsmore->pItem->pToken1 != NULL); + assert_eq(TOKEN_b, itemsmore->pItem->pToken1->token); + assert_eq(22, itemsmore->pItem->pToken1->pvalue); + assert(itemsmore->pItemsMore == NULL); input = ""; p_context_init(&context, (uint8_t const *)input, strlen(input)); assert_eq(P_SUCCESS, p_parse(&context)); start = p_result(&context); - assert(start->pItems1 == NULL); + assert(start->pItems == NULL); input = "2 1"; p_context_init(&context, (uint8_t const *)input, strlen(input)); assert_eq(P_SUCCESS, p_parse(&context)); start = p_result(&context); - assert(start->pItems1 != NULL); - assert(start->pItems1->pItem1 != NULL); - assert(start->pItems1->pItem1->pDual1 != NULL); - assert(start->pItems1->pItem1->pDual1->pTwo1 != NULL); - assert(start->pItems1->pItem1->pDual1->pOne2 != NULL); - assert(start->pItems1->pItem1->pDual1->pTwo2 == NULL); - assert(start->pItems1->pItem1->pDual1->pOne1 == NULL); + assert(start->pItems != NULL); + assert(start->pItems->pItem != NULL); + assert(start->pItems->pItem->pDual != NULL); + assert(start->pItems->pItem->pDual->pTwo1 != NULL); + assert(start->pItems->pItem->pDual->pOne2 != NULL); + assert(start->pItems->pItem->pDual->pTwo2 == NULL); + assert(start->pItems->pItem->pDual->pOne1 == NULL); return 0; } diff --git a/spec/test_ast.d b/spec/test_ast.d index a554304..caf6ad5 100644 --- a/spec/test_ast.d +++ b/spec/test_ast.d @@ -15,42 +15,43 @@ unittest assert_eq(P_SUCCESS, p_parse(&context)); Start * start = p_result(&context); assert(start.pItems1 !is null); - Items * items = start.pItems1; - assert(items.pItem1 !is null); - assert(items.pItem1.pToken1 !is null); - assert_eq(TOKEN_a, items.pItem1.pToken1.token); - assert_eq(11, items.pItem1.pToken1.pvalue); - assert(items.pItemsMore2 !is null); - ItemsMore * itemsmore = items.pItemsMore2; - assert(itemsmore.pItem2 !is null); - assert(itemsmore.pItem2.pItem2 !is null); - assert(itemsmore.pItem2.pItem2.pItem2 !is null); - assert(itemsmore.pItem2.pItem2.pItem2.pToken1 !is null); - assert_eq(TOKEN_b, itemsmore.pItem2.pItem2.pItem2.pToken1.token); - assert_eq(22, itemsmore.pItem2.pItem2.pItem2.pToken1.pvalue); - assert(itemsmore.pItemsMore3 !is null); - itemsmore = itemsmore.pItemsMore3; - assert(itemsmore.pItem2 !is null); - assert(itemsmore.pItem2.pToken1 !is null); - assert_eq(TOKEN_b, itemsmore.pItem2.pToken1.token); - assert_eq(22, itemsmore.pItem2.pToken1.pvalue); - assert(itemsmore.pItemsMore3 is null); + assert(start.pItems !is null); + Items * items = start.pItems; + assert(items.pItem !is null); + assert(items.pItem.pToken1 !is null); + assert_eq(TOKEN_a, items.pItem.pToken1.token); + assert_eq(11, items.pItem.pToken1.pvalue); + assert(items.pItemsMore !is null); + ItemsMore * itemsmore = items.pItemsMore; + assert(itemsmore.pItem !is null); + assert(itemsmore.pItem.pItem !is null); + assert(itemsmore.pItem.pItem.pItem !is null); + assert(itemsmore.pItem.pItem.pItem.pToken1 !is null); + assert_eq(TOKEN_b, itemsmore.pItem.pItem.pItem.pToken1.token); + assert_eq(22, itemsmore.pItem.pItem.pItem.pToken1.pvalue); + assert(itemsmore.pItemsMore !is null); + itemsmore = itemsmore.pItemsMore; + assert(itemsmore.pItem !is null); + assert(itemsmore.pItem.pToken1 !is null); + assert_eq(TOKEN_b, itemsmore.pItem.pToken1.token); + assert_eq(22, itemsmore.pItem.pToken1.pvalue); + assert(itemsmore.pItemsMore is null); input = ""; p_context_init(&context, input); assert_eq(P_SUCCESS, p_parse(&context)); start = p_result(&context); - assert(start.pItems1 is null); + assert(start.pItems is null); input = "2 1"; p_context_init(&context, input); assert_eq(P_SUCCESS, p_parse(&context)); start = p_result(&context); - assert(start.pItems1 !is null); - assert(start.pItems1.pItem1 !is null); - assert(start.pItems1.pItem1.pDual1 !is null); - assert(start.pItems1.pItem1.pDual1.pTwo1 !is null); - assert(start.pItems1.pItem1.pDual1.pOne2 !is null); - assert(start.pItems1.pItem1.pDual1.pTwo2 is null); - assert(start.pItems1.pItem1.pDual1.pOne1 is null); + assert(start.pItems !is null); + assert(start.pItems.pItem !is null); + assert(start.pItems.pItem.pDual !is null); + assert(start.pItems.pItem.pDual.pTwo1 !is null); + assert(start.pItems.pItem.pDual.pOne2 !is null); + assert(start.pItems.pItem.pDual.pTwo2 is null); + assert(start.pItems.pItem.pDual.pOne1 is null); }