Add AST node field name with no suffix when unique - #22

This commit is contained in:
Josh Holtrop 2024-04-22 21:50:26 -04:00
parent cb06a56f81
commit c7a18ef821
5 changed files with 96 additions and 70 deletions

View File

@ -74,8 +74,13 @@ public struct Token
<% next if name.start_with?("$") %> <% next if name.start_with?("$") %>
public struct <%= name %> public struct <%= name %>
{ {
<% rule_set.ast_fields.each do |name, type| %> <% rule_set.ast_fields.each do |fields| %>
<%= type %> * <%= name %>; union
{
<% fields.each do |field_name, type| %>
<%= type %> * <%= field_name %>;
<% end %>
}
<% end %> <% end %>
} }

View File

@ -71,8 +71,13 @@ struct <%= name %>;
<% next if name.start_with?("$") %> <% next if name.start_with?("$") %>
typedef struct <%= name %> typedef struct <%= name %>
{ {
<% rule_set.ast_fields.each do |name, type| %> <% rule_set.ast_fields.each do |fields| %>
struct <%= type %> * <%= name %>; union
{
<% fields.each do |field_name, type| %>
struct <%= type %> * <%= field_name %>;
<% end %>
};
<% end %> <% end %>
} <%= name %>; } <%= name %>;

View File

@ -78,16 +78,20 @@ class Propane
# Build the set of AST fields for this RuleSet. # Build the set of AST fields for this RuleSet.
# #
# The keys are the field names and the values are the AST node structure # This is an Array of Hashes. Each entry in the Array corresponds to a
# names. # 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<Hash>]
# AST fields. # AST fields.
def ast_fields def ast_fields
@_ast_fields ||= @_ast_fields ||=
begin begin
field_indexes = {} field_ast_node_indexes = {}
fields = {} field_indexes_across_all_rules = {}
ast_node_fields = []
@rules.each do |rule| @rules.each do |rule|
rule.components.each_with_index do |component, i| rule.components.each_with_index do |component, i|
if component.is_a?(Token) if component.is_a?(Token)
@ -96,14 +100,24 @@ class Propane
node_name = component.name node_name = component.name
end end
field_name = "p#{node_name}#{i + 1}" field_name = "p#{node_name}#{i + 1}"
unless field_indexes[field_name] unless field_ast_node_indexes[field_name]
field_indexes[field_name] = fields.size field_ast_node_indexes[field_name] = ast_node_fields.size
fields[field_name] = node_name ast_node_fields << {field_name => node_name}
end 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
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
end end

View File

@ -11,44 +11,45 @@ int main()
assert_eq(P_SUCCESS, p_parse(&context)); assert_eq(P_SUCCESS, p_parse(&context));
Start * start = p_result(&context); Start * start = p_result(&context);
assert(start->pItems1 != NULL); assert(start->pItems1 != NULL);
Items * items = start->pItems1; assert(start->pItems != NULL);
assert(items->pItem1 != NULL); Items * items = start->pItems;
assert(items->pItem1->pToken1 != NULL); assert(items->pItem != NULL);
assert_eq(TOKEN_a, items->pItem1->pToken1->token); assert(items->pItem->pToken1 != NULL);
assert_eq(11, items->pItem1->pToken1->pvalue); assert_eq(TOKEN_a, items->pItem->pToken1->token);
assert(items->pItemsMore2 != NULL); assert_eq(11, items->pItem->pToken1->pvalue);
ItemsMore * itemsmore = items->pItemsMore2; assert(items->pItemsMore != NULL);
assert(itemsmore->pItem2 != NULL); ItemsMore * itemsmore = items->pItemsMore;
assert(itemsmore->pItem2->pItem2 != NULL); assert(itemsmore->pItem != NULL);
assert(itemsmore->pItem2->pItem2->pItem2 != NULL); assert(itemsmore->pItem->pItem != NULL);
assert(itemsmore->pItem2->pItem2->pItem2->pToken1 != NULL); assert(itemsmore->pItem->pItem->pItem != NULL);
assert_eq(TOKEN_b, itemsmore->pItem2->pItem2->pItem2->pToken1->token); assert(itemsmore->pItem->pItem->pItem->pToken1 != NULL);
assert_eq(22, itemsmore->pItem2->pItem2->pItem2->pToken1->pvalue); assert_eq(TOKEN_b, itemsmore->pItem->pItem->pItem->pToken1->token);
assert(itemsmore->pItemsMore3 != NULL); assert_eq(22, itemsmore->pItem->pItem->pItem->pToken1->pvalue);
itemsmore = itemsmore->pItemsMore3; assert(itemsmore->pItemsMore != NULL);
assert(itemsmore->pItem2 != NULL); itemsmore = itemsmore->pItemsMore;
assert(itemsmore->pItem2->pToken1 != NULL); assert(itemsmore->pItem != NULL);
assert_eq(TOKEN_b, itemsmore->pItem2->pToken1->token); assert(itemsmore->pItem->pToken1 != NULL);
assert_eq(22, itemsmore->pItem2->pToken1->pvalue); assert_eq(TOKEN_b, itemsmore->pItem->pToken1->token);
assert(itemsmore->pItemsMore3 == NULL); assert_eq(22, itemsmore->pItem->pToken1->pvalue);
assert(itemsmore->pItemsMore == NULL);
input = ""; input = "";
p_context_init(&context, (uint8_t const *)input, strlen(input)); p_context_init(&context, (uint8_t const *)input, strlen(input));
assert_eq(P_SUCCESS, p_parse(&context)); assert_eq(P_SUCCESS, p_parse(&context));
start = p_result(&context); start = p_result(&context);
assert(start->pItems1 == NULL); assert(start->pItems == NULL);
input = "2 1"; input = "2 1";
p_context_init(&context, (uint8_t const *)input, strlen(input)); p_context_init(&context, (uint8_t const *)input, strlen(input));
assert_eq(P_SUCCESS, p_parse(&context)); assert_eq(P_SUCCESS, p_parse(&context));
start = p_result(&context); start = p_result(&context);
assert(start->pItems1 != NULL); assert(start->pItems != NULL);
assert(start->pItems1->pItem1 != NULL); assert(start->pItems->pItem != NULL);
assert(start->pItems1->pItem1->pDual1 != NULL); assert(start->pItems->pItem->pDual != NULL);
assert(start->pItems1->pItem1->pDual1->pTwo1 != NULL); assert(start->pItems->pItem->pDual->pTwo1 != NULL);
assert(start->pItems1->pItem1->pDual1->pOne2 != NULL); assert(start->pItems->pItem->pDual->pOne2 != NULL);
assert(start->pItems1->pItem1->pDual1->pTwo2 == NULL); assert(start->pItems->pItem->pDual->pTwo2 == NULL);
assert(start->pItems1->pItem1->pDual1->pOne1 == NULL); assert(start->pItems->pItem->pDual->pOne1 == NULL);
return 0; return 0;
} }

View File

@ -15,42 +15,43 @@ unittest
assert_eq(P_SUCCESS, p_parse(&context)); assert_eq(P_SUCCESS, p_parse(&context));
Start * start = p_result(&context); Start * start = p_result(&context);
assert(start.pItems1 !is null); assert(start.pItems1 !is null);
Items * items = start.pItems1; assert(start.pItems !is null);
assert(items.pItem1 !is null); Items * items = start.pItems;
assert(items.pItem1.pToken1 !is null); assert(items.pItem !is null);
assert_eq(TOKEN_a, items.pItem1.pToken1.token); assert(items.pItem.pToken1 !is null);
assert_eq(11, items.pItem1.pToken1.pvalue); assert_eq(TOKEN_a, items.pItem.pToken1.token);
assert(items.pItemsMore2 !is null); assert_eq(11, items.pItem.pToken1.pvalue);
ItemsMore * itemsmore = items.pItemsMore2; assert(items.pItemsMore !is null);
assert(itemsmore.pItem2 !is null); ItemsMore * itemsmore = items.pItemsMore;
assert(itemsmore.pItem2.pItem2 !is null); assert(itemsmore.pItem !is null);
assert(itemsmore.pItem2.pItem2.pItem2 !is null); assert(itemsmore.pItem.pItem !is null);
assert(itemsmore.pItem2.pItem2.pItem2.pToken1 !is null); assert(itemsmore.pItem.pItem.pItem !is null);
assert_eq(TOKEN_b, itemsmore.pItem2.pItem2.pItem2.pToken1.token); assert(itemsmore.pItem.pItem.pItem.pToken1 !is null);
assert_eq(22, itemsmore.pItem2.pItem2.pItem2.pToken1.pvalue); assert_eq(TOKEN_b, itemsmore.pItem.pItem.pItem.pToken1.token);
assert(itemsmore.pItemsMore3 !is null); assert_eq(22, itemsmore.pItem.pItem.pItem.pToken1.pvalue);
itemsmore = itemsmore.pItemsMore3; assert(itemsmore.pItemsMore !is null);
assert(itemsmore.pItem2 !is null); itemsmore = itemsmore.pItemsMore;
assert(itemsmore.pItem2.pToken1 !is null); assert(itemsmore.pItem !is null);
assert_eq(TOKEN_b, itemsmore.pItem2.pToken1.token); assert(itemsmore.pItem.pToken1 !is null);
assert_eq(22, itemsmore.pItem2.pToken1.pvalue); assert_eq(TOKEN_b, itemsmore.pItem.pToken1.token);
assert(itemsmore.pItemsMore3 is null); assert_eq(22, itemsmore.pItem.pToken1.pvalue);
assert(itemsmore.pItemsMore is null);
input = ""; input = "";
p_context_init(&context, input); p_context_init(&context, input);
assert_eq(P_SUCCESS, p_parse(&context)); assert_eq(P_SUCCESS, p_parse(&context));
start = p_result(&context); start = p_result(&context);
assert(start.pItems1 is null); assert(start.pItems is null);
input = "2 1"; input = "2 1";
p_context_init(&context, input); p_context_init(&context, input);
assert_eq(P_SUCCESS, p_parse(&context)); assert_eq(P_SUCCESS, p_parse(&context));
start = p_result(&context); start = p_result(&context);
assert(start.pItems1 !is null); assert(start.pItems !is null);
assert(start.pItems1.pItem1 !is null); assert(start.pItems.pItem !is null);
assert(start.pItems1.pItem1.pDual1 !is null); assert(start.pItems.pItem.pDual !is null);
assert(start.pItems1.pItem1.pDual1.pTwo1 !is null); assert(start.pItems.pItem.pDual.pTwo1 !is null);
assert(start.pItems1.pItem1.pDual1.pOne2 !is null); assert(start.pItems.pItem.pDual.pOne2 !is null);
assert(start.pItems1.pItem1.pDual1.pTwo2 is null); assert(start.pItems.pItem.pDual.pTwo2 is null);
assert(start.pItems1.pItem1.pDual1.pOne1 is null); assert(start.pItems.pItem.pDual.pOne1 is null);
} }