Compare commits
9
Commits
e223d03d7c
..
v3.0.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
243ee0f19f | ||
|
|
8d2efcc19b | ||
|
|
9c787f0e89 | ||
|
|
ef6a0f9552 | ||
|
|
77ec7c9de4 | ||
|
|
6a87bb2d56 | ||
|
|
cb426b4be1 | ||
|
|
addf27d837 | ||
|
|
9c03d20083 |
No files matched your search
@@ -1,3 +1,21 @@
|
||||
## v3.0.0
|
||||
|
||||
### New Features
|
||||
|
||||
- Add support for multiple starting rules (#38)
|
||||
- Add `p_free_tree()` functions to reclaim generated tree memory
|
||||
- Add `free_token_node` grammar statement to reclaim user-allocated memory stored in a Token tree node `pvalue` field
|
||||
- Add valgrind memory leak tests to unit tests
|
||||
- Fix build issues for C++ to officially support C++ target output
|
||||
|
||||
### Improvements
|
||||
|
||||
- Document `p_lex()` and `p_token_info_t` in user guide (#37)
|
||||
|
||||
### Breaking changes
|
||||
|
||||
- Rename AST generation mode to tree generation mode (see [UPGRADING.md]())
|
||||
|
||||
## v2.3.0
|
||||
|
||||
### New Features
|
||||
|
||||
+2
-2
@@ -10,7 +10,7 @@ GEM
|
||||
date
|
||||
stringio
|
||||
rake (13.3.1)
|
||||
rdoc (7.1.0)
|
||||
rdoc (7.2.0)
|
||||
erb
|
||||
psych (>= 4.0.0)
|
||||
tsort
|
||||
@@ -27,7 +27,7 @@ GEM
|
||||
rspec-mocks (3.13.7)
|
||||
diff-lcs (>= 1.2.0, < 2.0)
|
||||
rspec-support (~> 3.13.0)
|
||||
rspec-support (3.13.6)
|
||||
rspec-support (3.13.7)
|
||||
simplecov (0.22.0)
|
||||
docile (~> 1.1)
|
||||
simplecov-html (~> 0.11)
|
||||
|
||||
@@ -7,7 +7,9 @@ Propane is a LALR Parser Generator (LPG) which:
|
||||
* supports UTF-8 lexer inputs
|
||||
* generates a table-driven shift/reduce parser to parse input in linear time
|
||||
* targets C, C++, or D language outputs
|
||||
* optionally supports automatic full AST generation
|
||||
* optionally supports automatic full parse tree generation
|
||||
* supports starting parsing from multiple start rules
|
||||
* tracks input text start and end positions for all matched tokens/rules
|
||||
* is MIT-licensed
|
||||
* is distributable as a standalone Ruby script
|
||||
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
## v3.0.0
|
||||
|
||||
### Grammar changes
|
||||
|
||||
- Rename `ast;` statement to `tree;`.
|
||||
- Rename `ast_prefix;` statement to `tree_prefix;`.
|
||||
- Rename `ast_suffix;` statement to `tree_suffix;`.
|
||||
+116
-43
@@ -638,7 +638,7 @@ typedef struct
|
||||
* reduce action.
|
||||
*/
|
||||
parser_state_id_t n_states;
|
||||
<% if @grammar.ast %>
|
||||
<% if @grammar.tree %>
|
||||
|
||||
/**
|
||||
* Map of rule components to rule set child fields.
|
||||
@@ -646,7 +646,7 @@ typedef struct
|
||||
uint16_t const * rule_set_node_field_index_map;
|
||||
|
||||
/**
|
||||
* Number of rule set AST node fields.
|
||||
* Number of rule set tree node fields.
|
||||
*/
|
||||
uint16_t rule_set_node_field_array_size;
|
||||
|
||||
@@ -688,19 +688,21 @@ typedef struct
|
||||
/** Parser value from this state. */
|
||||
<%= @grammar.prefix %>value_t pvalue;
|
||||
|
||||
<% if @grammar.ast %>
|
||||
/** AST node. */
|
||||
void * ast_node;
|
||||
<% if @grammar.tree %>
|
||||
/** tree node. */
|
||||
void * tree_node;
|
||||
<% end %>
|
||||
} state_value_t;
|
||||
|
||||
/** Common AST node structure. */
|
||||
typedef struct
|
||||
/** Common tree node structure. */
|
||||
typedef struct TreeNode_s
|
||||
{
|
||||
<%= @grammar.prefix %>position_t position;
|
||||
<%= @grammar.prefix %>position_t end_position;
|
||||
void * fields[];
|
||||
} ASTNode;
|
||||
uint16_t n_fields;
|
||||
uint8_t is_token;
|
||||
struct TreeNode_s * fields[];
|
||||
} TreeNode;
|
||||
|
||||
/** Parser shift table. */
|
||||
static const shift_t parser_shift_table[] = {
|
||||
@@ -709,7 +711,7 @@ static const shift_t parser_shift_table[] = {
|
||||
<% end %>
|
||||
};
|
||||
|
||||
<% if @grammar.ast %>
|
||||
<% if @grammar.tree %>
|
||||
<% @grammar.rules.each do |rule| %>
|
||||
<% unless rule.flat_rule_set_node_field_index_map? %>
|
||||
const uint16_t r_<%= rule.name.gsub("$", "_") %><%= rule.id %>_node_field_index_map[<%= rule.rule_set_node_field_index_map.size %>] = {<%= rule.rule_set_node_field_index_map.map {|v| v.to_s}.join(", ") %>};
|
||||
@@ -724,14 +726,14 @@ static const reduce_t parser_reduce_table[] = {
|
||||
<%= reduce[:token_id] %>u, /* Token: <%= reduce[:token] ? reduce[:token].name : "(any)" %> */
|
||||
<%= reduce[:rule_id] %>u, /* Rule ID */
|
||||
<%= reduce[:rule_set_id] %>u, /* Rule set ID (<%= reduce[:rule].rule_set.name %>) */
|
||||
<% if @grammar.ast %>
|
||||
<% if @grammar.tree %>
|
||||
<%= reduce[:n_states] %>u, /* Number of states */
|
||||
<% if reduce[:rule].flat_rule_set_node_field_index_map? %>
|
||||
NULL, /* No rule set node field index map (flat map) */
|
||||
<% else %>
|
||||
&r_<%= reduce[:rule].name.gsub("$", "_") %><%= reduce[:rule].id %>_node_field_index_map[0], /* Rule set node field index map */
|
||||
<% end %>
|
||||
<%= reduce[:rule].rule_set.ast_fields.size %>, /* Number of AST fields */
|
||||
<%= reduce[:rule].rule_set.tree_fields.size %>, /* Number of tree fields */
|
||||
<%= reduce[:propagate_optional_target] %>}, /* Propagate optional target? */
|
||||
<% else %>
|
||||
<%= reduce[:n_states] %>u},
|
||||
@@ -839,7 +841,7 @@ static void state_values_stack_free(state_values_stack_t * stack)
|
||||
free(stack->entries);
|
||||
}
|
||||
|
||||
<% unless @grammar.ast %>
|
||||
<% unless @grammar.tree %>
|
||||
/**
|
||||
* Execute user code associated with a parser rule.
|
||||
*
|
||||
@@ -922,6 +924,8 @@ static size_t check_reduce(size_t state_id, <%= @grammar.prefix %>token_t token)
|
||||
*
|
||||
* @param context
|
||||
* Lexer/parser context structure.
|
||||
* @start_state_id
|
||||
* ID of the state in which to start.
|
||||
*
|
||||
* @retval P_SUCCESS
|
||||
* The parser successfully matched the input text. The parse result value
|
||||
@@ -934,19 +938,20 @@ static size_t check_reduce(size_t state_id, <%= @grammar.prefix %>token_t token)
|
||||
* @reval P_UNEXPECTED_INPUT
|
||||
* Input text does not match any lexer pattern.
|
||||
*/
|
||||
size_t <%= @grammar.prefix %>parse(<%= @grammar.prefix %>context_t * context)
|
||||
static size_t parse_from(<%= @grammar.prefix %>context_t * context, size_t start_state_id)
|
||||
{
|
||||
<%= @grammar.prefix %>token_info_t token_info;
|
||||
<%= @grammar.prefix %>token_t token = INVALID_TOKEN_ID;
|
||||
state_values_stack_t statevalues;
|
||||
size_t reduced_rule_set = INVALID_ID;
|
||||
<% if @grammar.ast %>
|
||||
<% if @grammar.tree %>
|
||||
void * reduced_parser_node;
|
||||
<% else %>
|
||||
<%= @grammar.prefix %>value_t reduced_parser_value;
|
||||
<% end %>
|
||||
state_values_stack_init(&statevalues);
|
||||
state_values_stack_push(&statevalues);
|
||||
state_values_stack_index(&statevalues, -1)->state_id = start_state_id;
|
||||
size_t result;
|
||||
for (;;)
|
||||
{
|
||||
@@ -971,8 +976,8 @@ size_t <%= @grammar.prefix %>parse(<%= @grammar.prefix %>context_t * context)
|
||||
if ((shift_state != INVALID_ID) && (token == TOKEN___EOF))
|
||||
{
|
||||
/* Successful parse. */
|
||||
<% if @grammar.ast %>
|
||||
context->parse_result = (<%= @grammar.ast_prefix %><%= @grammar.start_rule %><%= @grammar.ast_suffix %> *)state_values_stack_index(&statevalues, -1)->ast_node;
|
||||
<% if @grammar.tree %>
|
||||
context->parse_result = state_values_stack_index(&statevalues, -1)->tree_node;
|
||||
<% else %>
|
||||
context->parse_result = state_values_stack_index(&statevalues, -1)->pvalue;
|
||||
<% end %>
|
||||
@@ -988,13 +993,15 @@ size_t <%= @grammar.prefix %>parse(<%= @grammar.prefix %>context_t * context)
|
||||
if (reduced_rule_set == INVALID_ID)
|
||||
{
|
||||
/* We shifted a token, mark it consumed. */
|
||||
<% if @grammar.ast %>
|
||||
<%= @grammar.ast_prefix %>Token<%= @grammar.ast_suffix %> * token_ast_node = (<%= @grammar.ast_prefix %>Token<%= @grammar.ast_suffix %> *)malloc(sizeof(<%= @grammar.ast_prefix %>Token<%= @grammar.ast_suffix %>));
|
||||
token_ast_node->position = token_info.position;
|
||||
token_ast_node->end_position = token_info.end_position;
|
||||
token_ast_node->token = token;
|
||||
token_ast_node->pvalue = token_info.pvalue;
|
||||
state_values_stack_index(&statevalues, -1)->ast_node = token_ast_node;
|
||||
<% if @grammar.tree %>
|
||||
<%= @grammar.tree_prefix %>Token<%= @grammar.tree_suffix %> * token_tree_node = (<%= @grammar.tree_prefix %>Token<%= @grammar.tree_suffix %> *)malloc(sizeof(<%= @grammar.tree_prefix %>Token<%= @grammar.tree_suffix %>));
|
||||
token_tree_node->position = token_info.position;
|
||||
token_tree_node->end_position = token_info.end_position;
|
||||
token_tree_node->n_fields = 0u;
|
||||
token_tree_node->is_token = 1u;
|
||||
token_tree_node->token = token;
|
||||
token_tree_node->pvalue = token_info.pvalue;
|
||||
state_values_stack_index(&statevalues, -1)->tree_node = token_tree_node;
|
||||
<% else %>
|
||||
state_values_stack_index(&statevalues, -1)->pvalue = token_info.pvalue;
|
||||
<% end %>
|
||||
@@ -1003,8 +1010,8 @@ size_t <%= @grammar.prefix %>parse(<%= @grammar.prefix %>context_t * context)
|
||||
else
|
||||
{
|
||||
/* We shifted a RuleSet. */
|
||||
<% if @grammar.ast %>
|
||||
state_values_stack_index(&statevalues, -1)->ast_node = reduced_parser_node;
|
||||
<% if @grammar.tree %>
|
||||
state_values_stack_index(&statevalues, -1)->tree_node = reduced_parser_node;
|
||||
<% else %>
|
||||
state_values_stack_index(&statevalues, -1)->pvalue = reduced_parser_value;
|
||||
<%= @grammar.prefix %>value_t new_parse_result;
|
||||
@@ -1020,39 +1027,38 @@ size_t <%= @grammar.prefix %>parse(<%= @grammar.prefix %>context_t * context)
|
||||
if (reduce_index != INVALID_ID)
|
||||
{
|
||||
/* We have something to reduce. */
|
||||
<% if @grammar.ast %>
|
||||
<% if @grammar.tree %>
|
||||
if (parser_reduce_table[reduce_index].propagate_optional_target)
|
||||
{
|
||||
reduced_parser_node = state_values_stack_index(&statevalues, -1)->ast_node;
|
||||
reduced_parser_node = state_values_stack_index(&statevalues, -1)->tree_node;
|
||||
}
|
||||
else if (parser_reduce_table[reduce_index].n_states > 0)
|
||||
{
|
||||
size_t n_fields = parser_reduce_table[reduce_index].rule_set_node_field_array_size;
|
||||
ASTNode * node = (ASTNode *)malloc(sizeof(ASTNode) + n_fields * sizeof(void *));
|
||||
size_t bytes = sizeof(TreeNode) + n_fields * sizeof(void *);
|
||||
TreeNode * node = (TreeNode *)malloc(bytes);
|
||||
memset(node, 0, bytes);
|
||||
node->position = INVALID_POSITION;
|
||||
node->end_position = INVALID_POSITION;
|
||||
for (size_t i = 0; i < n_fields; i++)
|
||||
{
|
||||
node->fields[i] = NULL;
|
||||
}
|
||||
node->n_fields = n_fields;
|
||||
if (parser_reduce_table[reduce_index].rule_set_node_field_index_map == NULL)
|
||||
{
|
||||
for (size_t i = 0; i < parser_reduce_table[reduce_index].n_states; i++)
|
||||
{
|
||||
node->fields[i] = state_values_stack_index(&statevalues, -(int)parser_reduce_table[reduce_index].n_states + (int)i)->ast_node;
|
||||
node->fields[i] = (TreeNode *)state_values_stack_index(&statevalues, -(int)parser_reduce_table[reduce_index].n_states + (int)i)->tree_node;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (size_t i = 0; i < parser_reduce_table[reduce_index].n_states; i++)
|
||||
{
|
||||
node->fields[parser_reduce_table[reduce_index].rule_set_node_field_index_map[i]] = state_values_stack_index(&statevalues, -(int)parser_reduce_table[reduce_index].n_states + (int)i)->ast_node;
|
||||
node->fields[parser_reduce_table[reduce_index].rule_set_node_field_index_map[i]] = (TreeNode *)state_values_stack_index(&statevalues, -(int)parser_reduce_table[reduce_index].n_states + (int)i)->tree_node;
|
||||
}
|
||||
}
|
||||
bool position_found = false;
|
||||
for (size_t i = 0; i < n_fields; i++)
|
||||
{
|
||||
ASTNode * child = (ASTNode *)node->fields[i];
|
||||
TreeNode * child = node->fields[i];
|
||||
if ((child != NULL) && <%= @grammar.prefix %>position_valid(child->position))
|
||||
{
|
||||
if (!position_found)
|
||||
@@ -1074,6 +1080,7 @@ size_t <%= @grammar.prefix %>parse(<%= @grammar.prefix %>context_t * context)
|
||||
memset(&reduced_parser_value2, 0, sizeof(reduced_parser_value2));
|
||||
if (parser_user_code(&reduced_parser_value2, parser_reduce_table[reduce_index].rule, &statevalues, parser_reduce_table[reduce_index].n_states, context) == P_USER_TERMINATED)
|
||||
{
|
||||
state_values_stack_free(&statevalues);
|
||||
return P_USER_TERMINATED;
|
||||
}
|
||||
reduced_parser_value = reduced_parser_value2;
|
||||
@@ -1097,6 +1104,19 @@ size_t <%= @grammar.prefix %>parse(<%= @grammar.prefix %>context_t * context)
|
||||
return result;
|
||||
}
|
||||
|
||||
size_t <%= @grammar.prefix %>parse(<%= @grammar.prefix %>context_t * context)
|
||||
{
|
||||
return parse_from(context, 0u);
|
||||
}
|
||||
|
||||
<% @grammar.start_rules.each_with_index do |start_rule, i| %>
|
||||
|
||||
size_t <%= @grammar.prefix %>parse_<%= start_rule %>(<%= @grammar.prefix %>context_t * context)
|
||||
{
|
||||
return parse_from(context, <%= i %>u);
|
||||
}
|
||||
<% end %>
|
||||
|
||||
/**
|
||||
* Get the parse result value.
|
||||
*
|
||||
@@ -1105,18 +1125,29 @@ size_t <%= @grammar.prefix %>parse(<%= @grammar.prefix %>context_t * context)
|
||||
*
|
||||
* @return Parse result value.
|
||||
*/
|
||||
<% if @grammar.ast %>
|
||||
<%= @grammar.ast_prefix %><%= @grammar.start_rule %><%= @grammar.ast_suffix %> * <%= @grammar.prefix %>result(<%= @grammar.prefix %>context_t * context)
|
||||
<% if @grammar.tree %>
|
||||
<%= @grammar.tree_prefix %><%= @grammar.start_rules[0] %><%= @grammar.tree_suffix %> * <%= @grammar.prefix %>result(<%= @grammar.prefix %>context_t * context)
|
||||
{
|
||||
return (<%= @grammar.tree_prefix %><%= @grammar.start_rules[0] %><%= @grammar.tree_suffix %> *) context->parse_result;
|
||||
}
|
||||
<% @grammar.start_rules.each_with_index do |start_rule, i| %>
|
||||
<%= @grammar.tree_prefix %><%= start_rule %><%= @grammar.tree_suffix %> * <%= @grammar.prefix %>result_<%= start_rule %>(<%= @grammar.prefix %>context_t * context)
|
||||
{
|
||||
return (<%= @grammar.tree_prefix %><%= start_rule %><%= @grammar.tree_suffix %> *) context->parse_result;
|
||||
}
|
||||
<% end %>
|
||||
<% else %>
|
||||
<%= start_rule_type[1] %> <%= @grammar.prefix %>result(<%= @grammar.prefix %>context_t * context)
|
||||
<% end %>
|
||||
{
|
||||
<% if @grammar.ast %>
|
||||
return context->parse_result;
|
||||
<% else %>
|
||||
return context->parse_result.v_<%= start_rule_type[0] %>;
|
||||
<% end %>
|
||||
}
|
||||
<% @grammar.start_rules.each_with_index do |start_rule, i| %>
|
||||
<%= start_rule_type(i)[1] %> <%= @grammar.prefix %>result_<%= start_rule %>(<%= @grammar.prefix %>context_t * context)
|
||||
{
|
||||
return context->parse_result.v_<%= start_rule_type(i)[0] %>;
|
||||
}
|
||||
<% end %>
|
||||
<% end %>
|
||||
|
||||
/**
|
||||
* Get the current text input position.
|
||||
@@ -1153,3 +1184,45 @@ size_t <%= @grammar.prefix %>user_terminate_code(<%= @grammar.prefix %>context_t
|
||||
{
|
||||
return context->token;
|
||||
}
|
||||
<% if @grammar.tree %>
|
||||
|
||||
static void free_tree_node(TreeNode * node)
|
||||
{
|
||||
if (node->is_token)
|
||||
{
|
||||
<% if @grammar.free_token_node %>
|
||||
<%= @grammar.free_token_node %>((<%= @grammar.tree_prefix %>Token<%= @grammar.tree_suffix %> *) node);
|
||||
<% end %>
|
||||
/* TODO: free value_t */
|
||||
}
|
||||
else if (node->n_fields > 0u)
|
||||
{
|
||||
for (size_t i = 0u; i < node->n_fields; i++)
|
||||
{
|
||||
if (node->fields[i] != NULL)
|
||||
{
|
||||
free_tree_node(node->fields[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
free(node);
|
||||
}
|
||||
|
||||
/**
|
||||
* Free all tree node memory.
|
||||
*/
|
||||
void <%= @grammar.prefix %>free_tree(<%= @grammar.tree_prefix %><%= @grammar.start_rules[0] %><%= @grammar.tree_suffix %> * tree)
|
||||
{
|
||||
free_tree_node((TreeNode *)tree);
|
||||
}
|
||||
<% @grammar.start_rules.each_with_index do |start_rule, i| %>
|
||||
|
||||
/**
|
||||
* Free all tree node memory.
|
||||
*/
|
||||
void <%= @grammar.prefix %>free_tree_<%= start_rule %>(<%= @grammar.tree_prefix %><%= start_rule %><%= @grammar.tree_suffix %> * tree)
|
||||
{
|
||||
free_tree_node((TreeNode *)tree);
|
||||
}
|
||||
<% end %>
|
||||
<% end %>
|
||||
+70
-43
@@ -75,7 +75,7 @@ public struct <%= @grammar.prefix %>position_t
|
||||
}
|
||||
}
|
||||
|
||||
<% if @grammar.ast %>
|
||||
<% if @grammar.tree %>
|
||||
/** Parser values type. */
|
||||
public alias <%= @grammar.prefix %>value_t = <%= @grammar.ptype %>;
|
||||
<% else %>
|
||||
@@ -88,19 +88,19 @@ public union <%= @grammar.prefix %>value_t
|
||||
}
|
||||
<% end %>
|
||||
|
||||
<% if @grammar.ast %>
|
||||
/** Common AST node structure. */
|
||||
private struct ASTNode
|
||||
<% if @grammar.tree %>
|
||||
/** Common tree node structure. */
|
||||
private struct TreeNode
|
||||
{
|
||||
<%= @grammar.prefix %>position_t position;
|
||||
<%= @grammar.prefix %>position_t end_position;
|
||||
void *[0] fields;
|
||||
}
|
||||
|
||||
/** AST node types. @{ */
|
||||
public struct <%= @grammar.ast_prefix %>Token<%= @grammar.ast_suffix %>
|
||||
/** Tree node types. @{ */
|
||||
public struct <%= @grammar.tree_prefix %>Token<%= @grammar.tree_suffix %>
|
||||
{
|
||||
/* ASTNode fields must be present in the same order here. */
|
||||
/* TreeNode fields must be present in the same order here. */
|
||||
<%= @grammar.prefix %>position_t position;
|
||||
<%= @grammar.prefix %>position_t end_position;
|
||||
<%= @grammar.prefix %>token_t token;
|
||||
@@ -110,11 +110,11 @@ public struct <%= @grammar.ast_prefix %>Token<%= @grammar.ast_suffix %>
|
||||
<% @parser.rule_sets.each do |name, rule_set| %>
|
||||
<% next if name.start_with?("$") %>
|
||||
<% next if rule_set.optional? %>
|
||||
public struct <%= @grammar.ast_prefix %><%= name %><%= @grammar.ast_suffix %>
|
||||
public struct <%= @grammar.tree_prefix %><%= name %><%= @grammar.tree_suffix %>
|
||||
{
|
||||
<%= @grammar.prefix %>position_t position;
|
||||
<%= @grammar.prefix %>position_t end_position;
|
||||
<% rule_set.ast_fields.each do |fields| %>
|
||||
<% rule_set.tree_fields.each do |fields| %>
|
||||
union
|
||||
{
|
||||
<% fields.each do |field_name, type| %>
|
||||
@@ -172,8 +172,8 @@ public struct <%= @grammar.prefix %>context_t
|
||||
/* Parser context data. */
|
||||
|
||||
/** Parse result value. */
|
||||
<% if @grammar.ast %>
|
||||
<%= @grammar.ast_prefix %><%= @grammar.start_rule %><%= @grammar.ast_suffix %> * parse_result;
|
||||
<% if @grammar.tree %>
|
||||
void * parse_result;
|
||||
<% else %>
|
||||
<%= @grammar.prefix %>value_t parse_result;
|
||||
<% end %>
|
||||
@@ -797,7 +797,7 @@ private struct reduce_t
|
||||
* reduce action.
|
||||
*/
|
||||
parser_state_id_t n_states;
|
||||
<% if @grammar.ast %>
|
||||
<% if @grammar.tree %>
|
||||
|
||||
/**
|
||||
* Map of rule components to rule set child fields.
|
||||
@@ -805,7 +805,7 @@ private struct reduce_t
|
||||
immutable(ushort) * rule_set_node_field_index_map;
|
||||
|
||||
/**
|
||||
* Number of rule set AST node fields.
|
||||
* Number of rule set tree node fields.
|
||||
*/
|
||||
ushort rule_set_node_field_array_size;
|
||||
|
||||
@@ -847,9 +847,9 @@ private struct state_value_t
|
||||
/** Parser value from this state. */
|
||||
<%= @grammar.prefix %>value_t pvalue;
|
||||
|
||||
<% if @grammar.ast %>
|
||||
/** AST node. */
|
||||
void * ast_node;
|
||||
<% if @grammar.tree %>
|
||||
/** Tree node. */
|
||||
void * tree_node;
|
||||
<% end %>
|
||||
|
||||
this(size_t state_id)
|
||||
@@ -865,7 +865,7 @@ private immutable shift_t[] parser_shift_table = [
|
||||
<% end %>
|
||||
];
|
||||
|
||||
<% if @grammar.ast %>
|
||||
<% if @grammar.tree %>
|
||||
<% @grammar.rules.each do |rule| %>
|
||||
<% unless rule.flat_rule_set_node_field_index_map? %>
|
||||
immutable ushort[<%= rule.rule_set_node_field_index_map.size %>] r_<%= rule.name.gsub("$", "_") %><%= rule.id %>_node_field_index_map = [<%= rule.rule_set_node_field_index_map.map {|v| v.to_s}.join(", ") %>];
|
||||
@@ -880,14 +880,14 @@ private immutable reduce_t[] parser_reduce_table = [
|
||||
<%= reduce[:token_id] %>u, /* Token: <%= reduce[:token] ? reduce[:token].name : "(any)" %> */
|
||||
<%= reduce[:rule_id] %>u, /* Rule ID */
|
||||
<%= reduce[:rule_set_id] %>u, /* Rule set ID (<%= reduce[:rule].rule_set.name %>) */
|
||||
<% if @grammar.ast %>
|
||||
<% if @grammar.tree %>
|
||||
<%= reduce[:n_states] %>u, /* Number of states */
|
||||
<% if reduce[:rule].flat_rule_set_node_field_index_map? %>
|
||||
null, /* No rule set node field index map (flat map) */
|
||||
<% else %>
|
||||
&r_<%= reduce[:rule].name.gsub("$", "_") %><%= reduce[:rule].id %>_node_field_index_map[0], /* Rule set node field index map */
|
||||
<% end %>
|
||||
<%= reduce[:rule].rule_set.ast_fields.size %>, /* Number of AST fields */
|
||||
<%= reduce[:rule].rule_set.tree_fields.size %>, /* Number of tree fields */
|
||||
<%= reduce[:propagate_optional_target] %>), /* Propagate optional target? */
|
||||
<% else %>
|
||||
<%= reduce[:n_states] %>u), /* Number of states */
|
||||
@@ -902,7 +902,7 @@ private immutable parser_state_t[] parser_state_table = [
|
||||
<% end %>
|
||||
];
|
||||
|
||||
<% unless @grammar.ast %>
|
||||
<% unless @grammar.tree %>
|
||||
/**
|
||||
* Execute user code associated with a parser rule.
|
||||
*
|
||||
@@ -985,6 +985,8 @@ private size_t check_reduce(size_t state_id, <%= @grammar.prefix %>token_t token
|
||||
*
|
||||
* @param context
|
||||
* Lexer/parser context structure.
|
||||
* @start_state_id
|
||||
* ID of the state in which to start.
|
||||
*
|
||||
* @retval P_SUCCESS
|
||||
* The parser successfully matched the input text. The parse result value
|
||||
@@ -997,13 +999,14 @@ private size_t check_reduce(size_t state_id, <%= @grammar.prefix %>token_t token
|
||||
* @reval P_UNEXPECTED_INPUT
|
||||
* Input text does not match any lexer pattern.
|
||||
*/
|
||||
public size_t <%= @grammar.prefix %>parse(<%= @grammar.prefix %>context_t * context)
|
||||
private size_t parse_from(<%= @grammar.prefix %>context_t * context, size_t start_state_id)
|
||||
{
|
||||
<%= @grammar.prefix %>token_info_t token_info;
|
||||
<%= @grammar.prefix %>token_t token = INVALID_TOKEN_ID;
|
||||
state_value_t[] statevalues = new state_value_t[](1);
|
||||
statevalues[0].state_id = start_state_id;
|
||||
size_t reduced_rule_set = INVALID_ID;
|
||||
<% if @grammar.ast %>
|
||||
<% if @grammar.tree %>
|
||||
void * reduced_parser_node;
|
||||
<% else %>
|
||||
<%= @grammar.prefix %>value_t reduced_parser_value;
|
||||
@@ -1030,8 +1033,8 @@ public size_t <%= @grammar.prefix %>parse(<%= @grammar.prefix %>context_t * cont
|
||||
if ((shift_state != INVALID_ID) && (token == TOKEN___EOF))
|
||||
{
|
||||
/* Successful parse. */
|
||||
<% if @grammar.ast %>
|
||||
context.parse_result = cast(<%= @grammar.ast_prefix %><%= @grammar.start_rule %><%= @grammar.ast_suffix %> *)statevalues[$-1].ast_node;
|
||||
<% if @grammar.tree %>
|
||||
context.parse_result = statevalues[$-1].tree_node;
|
||||
<% else %>
|
||||
context.parse_result = statevalues[$-1].pvalue;
|
||||
<% end %>
|
||||
@@ -1045,9 +1048,9 @@ public size_t <%= @grammar.prefix %>parse(<%= @grammar.prefix %>context_t * cont
|
||||
if (reduced_rule_set == INVALID_ID)
|
||||
{
|
||||
/* We shifted a token, mark it consumed. */
|
||||
<% if @grammar.ast %>
|
||||
<%= @grammar.ast_prefix %>Token<%= @grammar.ast_suffix %> * token_ast_node = new <%= @grammar.ast_prefix %>Token<%= @grammar.ast_suffix %>(token_info.position, token_info.end_position, token, token_info.pvalue);
|
||||
statevalues[$-1].ast_node = token_ast_node;
|
||||
<% if @grammar.tree %>
|
||||
<%= @grammar.tree_prefix %>Token<%= @grammar.tree_suffix %> * token_tree_node = new <%= @grammar.tree_prefix %>Token<%= @grammar.tree_suffix %>(token_info.position, token_info.end_position, token, token_info.pvalue);
|
||||
statevalues[$-1].tree_node = token_tree_node;
|
||||
<% else %>
|
||||
statevalues[$-1].pvalue = token_info.pvalue;
|
||||
<% end %>
|
||||
@@ -1056,8 +1059,8 @@ public size_t <%= @grammar.prefix %>parse(<%= @grammar.prefix %>context_t * cont
|
||||
else
|
||||
{
|
||||
/* We shifted a RuleSet. */
|
||||
<% if @grammar.ast %>
|
||||
statevalues[$-1].ast_node = reduced_parser_node;
|
||||
<% if @grammar.tree %>
|
||||
statevalues[$-1].tree_node = reduced_parser_node;
|
||||
<% else %>
|
||||
statevalues[$-1].pvalue = reduced_parser_value;
|
||||
<%= @grammar.prefix %>value_t new_parse_result;
|
||||
@@ -1072,16 +1075,16 @@ public size_t <%= @grammar.prefix %>parse(<%= @grammar.prefix %>context_t * cont
|
||||
if (reduce_index != INVALID_ID)
|
||||
{
|
||||
/* We have something to reduce. */
|
||||
<% if @grammar.ast %>
|
||||
<% if @grammar.tree %>
|
||||
if (parser_reduce_table[reduce_index].propagate_optional_target)
|
||||
{
|
||||
reduced_parser_node = statevalues[$ - 1].ast_node;
|
||||
reduced_parser_node = statevalues[$ - 1].tree_node;
|
||||
}
|
||||
else if (parser_reduce_table[reduce_index].n_states > 0)
|
||||
{
|
||||
size_t n_fields = parser_reduce_table[reduce_index].rule_set_node_field_array_size;
|
||||
size_t node_size = ASTNode.sizeof + n_fields * (void *).sizeof;
|
||||
ASTNode * node = cast(ASTNode *)malloc(node_size);
|
||||
size_t node_size = TreeNode.sizeof + n_fields * (void *).sizeof;
|
||||
TreeNode * node = cast(TreeNode *)malloc(node_size);
|
||||
GC.addRange(node, node_size);
|
||||
node.position = <%= @grammar.prefix %>position_t.INVALID;
|
||||
node.end_position = <%= @grammar.prefix %>position_t.INVALID;
|
||||
@@ -1093,20 +1096,20 @@ public size_t <%= @grammar.prefix %>parse(<%= @grammar.prefix %>context_t * cont
|
||||
{
|
||||
foreach (i; 0..parser_reduce_table[reduce_index].n_states)
|
||||
{
|
||||
node.fields[i] = statevalues[$ - parser_reduce_table[reduce_index].n_states + i].ast_node;
|
||||
node.fields[i] = statevalues[$ - parser_reduce_table[reduce_index].n_states + i].tree_node;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (i; 0..parser_reduce_table[reduce_index].n_states)
|
||||
{
|
||||
node.fields[parser_reduce_table[reduce_index].rule_set_node_field_index_map[i]] = statevalues[$ - parser_reduce_table[reduce_index].n_states + i].ast_node;
|
||||
node.fields[parser_reduce_table[reduce_index].rule_set_node_field_index_map[i]] = statevalues[$ - parser_reduce_table[reduce_index].n_states + i].tree_node;
|
||||
}
|
||||
}
|
||||
bool position_found = false;
|
||||
foreach (i; 0..n_fields)
|
||||
{
|
||||
ASTNode * child = cast(ASTNode *)node.fields[i];
|
||||
TreeNode * child = cast(TreeNode *)node.fields[i];
|
||||
if (child && child.position.valid)
|
||||
{
|
||||
if (!position_found)
|
||||
@@ -1147,6 +1150,19 @@ public size_t <%= @grammar.prefix %>parse(<%= @grammar.prefix %>context_t * cont
|
||||
}
|
||||
}
|
||||
|
||||
public size_t <%= @grammar.prefix %>parse(<%= @grammar.prefix %>context_t * context)
|
||||
{
|
||||
return parse_from(context, 0u);
|
||||
}
|
||||
|
||||
<% @grammar.start_rules.each_with_index do |start_rule, i| %>
|
||||
|
||||
public size_t <%= @grammar.prefix %>parse_<%= start_rule %>(<%= @grammar.prefix %>context_t * context)
|
||||
{
|
||||
return parse_from(context, <%= i %>u);
|
||||
}
|
||||
<% end %>
|
||||
|
||||
/**
|
||||
* Get the parse result value.
|
||||
*
|
||||
@@ -1155,18 +1171,29 @@ public size_t <%= @grammar.prefix %>parse(<%= @grammar.prefix %>context_t * cont
|
||||
*
|
||||
* @return Parse result value.
|
||||
*/
|
||||
<% if @grammar.ast %>
|
||||
public <%= @grammar.ast_prefix %><%= @grammar.start_rule %><%= @grammar.ast_suffix %> * <%= @grammar.prefix %>result(<%= @grammar.prefix %>context_t * context)
|
||||
<% if @grammar.tree %>
|
||||
public <%= @grammar.tree_prefix %><%= @grammar.start_rules[0] %><%= @grammar.tree_suffix %> * <%= @grammar.prefix %>result(<%= @grammar.prefix %>context_t * context)
|
||||
{
|
||||
return cast(<%= @grammar.tree_prefix %><%= @grammar.start_rules[0] %><%= @grammar.tree_suffix %> *)context.parse_result;
|
||||
}
|
||||
<% @grammar.start_rules.each_with_index do |start_rule, i| %>
|
||||
public <%= @grammar.tree_prefix %><%= start_rule %><%= @grammar.tree_suffix %> * <%= @grammar.prefix %>result_<%= start_rule %>(<%= @grammar.prefix %>context_t * context)
|
||||
{
|
||||
return cast(<%= @grammar.tree_prefix %><%= start_rule %><%= @grammar.tree_suffix %> *)context.parse_result;
|
||||
}
|
||||
<% end %>
|
||||
<% else %>
|
||||
public <%= start_rule_type[1] %> <%= @grammar.prefix %>result(<%= @grammar.prefix %>context_t * context)
|
||||
<% end %>
|
||||
{
|
||||
<% if @grammar.ast %>
|
||||
return context.parse_result;
|
||||
<% else %>
|
||||
return context.parse_result.v_<%= start_rule_type[0] %>;
|
||||
<% end %>
|
||||
}
|
||||
<% @grammar.start_rules.each_with_index do |start_rule, i| %>
|
||||
public <%= start_rule_type(i)[1] %> <%= @grammar.prefix %>result_<%= start_rule %>(<%= @grammar.prefix %>context_t * context)
|
||||
{
|
||||
return context.parse_result.v_<%= start_rule_type(i)[0] %>;
|
||||
}
|
||||
<% end %>
|
||||
<% end %>
|
||||
|
||||
/**
|
||||
* Get the current text input position.
|
||||
|
||||
+34
-13
@@ -58,7 +58,7 @@ typedef struct
|
||||
/** User header code blocks. */
|
||||
<%= @grammar.code_blocks.fetch("header", "") %>
|
||||
|
||||
<% if @grammar.ast %>
|
||||
<% if @grammar.tree %>
|
||||
/** Parser values type. */
|
||||
typedef <%= @grammar.ptype %> <%= @grammar.prefix %>value_t;
|
||||
<% else %>
|
||||
@@ -71,16 +71,18 @@ typedef union
|
||||
} <%= @grammar.prefix %>value_t;
|
||||
<% end %>
|
||||
|
||||
<% if @grammar.ast %>
|
||||
/** AST node types. @{ */
|
||||
typedef struct <%= @grammar.ast_prefix %>Token<%= @grammar.ast_suffix %>
|
||||
<% if @grammar.tree %>
|
||||
/** Tree node types. @{ */
|
||||
typedef struct <%= @grammar.tree_prefix %>Token<%= @grammar.tree_suffix %>
|
||||
{
|
||||
/* ASTNode fields must be present in the same order here. */
|
||||
<% # TreeNode fields must be present in the same order here. # %>
|
||||
<%= @grammar.prefix %>position_t position;
|
||||
<%= @grammar.prefix %>position_t end_position;
|
||||
uint16_t n_fields;
|
||||
uint8_t is_token;
|
||||
<%= @grammar.prefix %>token_t token;
|
||||
<%= @grammar.prefix %>value_t pvalue;
|
||||
} <%= @grammar.ast_prefix %>Token<%= @grammar.ast_suffix %>;
|
||||
} <%= @grammar.tree_prefix %>Token<%= @grammar.tree_suffix %>;
|
||||
|
||||
<% @parser.rule_sets.each do |name, rule_set| %>
|
||||
<% next if name.start_with?("$") %>
|
||||
@@ -91,11 +93,14 @@ struct <%= name %>;
|
||||
<% @parser.rule_sets.each do |name, rule_set| %>
|
||||
<% next if name.start_with?("$") %>
|
||||
<% next if rule_set.optional? %>
|
||||
typedef struct <%= @grammar.ast_prefix %><%= name %><%= @grammar.ast_suffix %>
|
||||
typedef struct <%= @grammar.tree_prefix %><%= name %><%= @grammar.tree_suffix %>
|
||||
{
|
||||
<% # TreeNode fields must be present in the same order here. # %>
|
||||
<%= @grammar.prefix %>position_t position;
|
||||
<%= @grammar.prefix %>position_t end_position;
|
||||
<% rule_set.ast_fields.each do |fields| %>
|
||||
uint16_t n_fields;
|
||||
uint8_t is_token;
|
||||
<% rule_set.tree_fields.each do |fields| %>
|
||||
union
|
||||
{
|
||||
<% fields.each do |field_name, type| %>
|
||||
@@ -103,7 +108,7 @@ typedef struct <%= @grammar.ast_prefix %><%= name %><%= @grammar.ast_suffix %>
|
||||
<% end %>
|
||||
};
|
||||
<% end %>
|
||||
} <%= @grammar.ast_prefix %><%= name %><%= @grammar.ast_suffix %>;
|
||||
} <%= @grammar.tree_prefix %><%= name %><%= @grammar.tree_suffix %>;
|
||||
|
||||
<% end %>
|
||||
/** @} */
|
||||
@@ -156,8 +161,8 @@ typedef struct
|
||||
/* Parser context data. */
|
||||
|
||||
/** Parse result value. */
|
||||
<% if @grammar.ast %>
|
||||
<%= @grammar.ast_prefix %><%= @grammar.start_rule %><%= @grammar.ast_suffix %> * parse_result;
|
||||
<% if @grammar.tree %>
|
||||
void * parse_result;
|
||||
<% else %>
|
||||
<%= @grammar.prefix %>value_t parse_result;
|
||||
<% end %>
|
||||
@@ -184,11 +189,27 @@ size_t <%= @grammar.prefix %>decode_code_point(uint8_t const * input, size_t inp
|
||||
size_t <%= @grammar.prefix %>lex(<%= @grammar.prefix %>context_t * context, <%= @grammar.prefix %>token_info_t * out_token_info);
|
||||
|
||||
size_t <%= @grammar.prefix %>parse(<%= @grammar.prefix %>context_t * context);
|
||||
<% @grammar.start_rules.each_with_index do |start_rule, i| %>
|
||||
size_t <%= @grammar.prefix %>parse_<%= start_rule %>(<%= @grammar.prefix %>context_t * context);
|
||||
<% end %>
|
||||
|
||||
<% if @grammar.ast %>
|
||||
<%= @grammar.ast_prefix %><%= @grammar.start_rule %><%= @grammar.ast_suffix %> * <%= @grammar.prefix %>result(<%= @grammar.prefix %>context_t * context);
|
||||
<% if @grammar.tree %>
|
||||
<%= @grammar.tree_prefix %><%= @grammar.start_rules[0] %><%= @grammar.tree_suffix %> * <%= @grammar.prefix %>result(<%= @grammar.prefix %>context_t * context);
|
||||
<% @grammar.start_rules.each_with_index do |start_rule, i| %>
|
||||
<%= @grammar.tree_prefix %><%= start_rule %><%= @grammar.tree_suffix %> * <%= @grammar.prefix %>result_<%= start_rule %>(<%= @grammar.prefix %>context_t * context);
|
||||
<% end %>
|
||||
<% else %>
|
||||
<%= start_rule_type[1] %> <%= @grammar.prefix %>result(<%= @grammar.prefix %>context_t * context);
|
||||
<% @grammar.start_rules.each_with_index do |start_rule, i| %>
|
||||
<%= start_rule_type(i)[1] %> <%= @grammar.prefix %>result_<%= start_rule %>(<%= @grammar.prefix %>context_t * context);
|
||||
<% end %>
|
||||
<% end %>
|
||||
|
||||
<% if @grammar.tree %>
|
||||
void <%= @grammar.prefix %>free_tree(<%= @grammar.tree_prefix %><%= @grammar.start_rules[0] %><%= @grammar.tree_suffix %> * tree);
|
||||
<% @grammar.start_rules.each_with_index do |start_rule, i| %>
|
||||
void <%= @grammar.prefix %>free_tree_<%= start_rule %>(<%= @grammar.tree_prefix %><%= start_rule %><%= @grammar.tree_suffix %> * tree);
|
||||
<% end %>
|
||||
<% end %>
|
||||
|
||||
<%= @grammar.prefix %>position_t <%= @grammar.prefix %>position(<%= @grammar.prefix %>context_t * context);
|
||||
|
||||
+130
-31
@@ -14,7 +14,8 @@ Propane is a LALR Parser Generator (LPG) which:
|
||||
* supports UTF-8 lexer inputs
|
||||
* generates a table-driven shift/reduce parser to parse input in linear time
|
||||
* targets C, C++, or D language outputs
|
||||
* optionally supports automatic full AST generation
|
||||
* optionally supports automatic full parse tree generation
|
||||
* supports starting parsing from multiple start rules
|
||||
* tracks input text start and end positions for all matched tokens/rules
|
||||
* is MIT-licensed
|
||||
* is distributable as a standalone Ruby script
|
||||
@@ -189,21 +190,21 @@ rule.
|
||||
Parser values for the rules or tokens in the rule pattern can be accessed
|
||||
positionally with tokens `$1`, `$2`, `$3`, etc...
|
||||
|
||||
Parser rule code blocks are not available in AST generation mode.
|
||||
In AST generation mode, a full parse tree is automatically constructed in
|
||||
Parser rule code blocks are not available in tree generation mode.
|
||||
In tree generation mode, a full parse tree is automatically constructed in
|
||||
memory for user code to traverse after parsing is complete.
|
||||
|
||||
##> AST generation mode - the `ast` statement
|
||||
##> Tree generation mode - the `tree` statement
|
||||
|
||||
To activate AST generation mode, place the `ast` statement in your grammar file:
|
||||
To activate tree generation mode, place the `tree` statement in your grammar file:
|
||||
|
||||
```
|
||||
ast;
|
||||
tree;
|
||||
```
|
||||
|
||||
It is recommended to place this statement early in the grammar.
|
||||
|
||||
In AST generation mode various aspects of propane's behavior are changed:
|
||||
In tree generation mode various aspects of propane's behavior are changed:
|
||||
|
||||
* Only one `ptype` is allowed.
|
||||
* Parser user code blocks are not supported.
|
||||
@@ -214,10 +215,10 @@ In AST generation mode various aspects of propane's behavior are changed:
|
||||
with the `start` grammar statement, the name of the start struct will be
|
||||
given by the user-specified start rule instead of `Start`.
|
||||
|
||||
Example AST generation grammar:
|
||||
Example tree generation grammar:
|
||||
|
||||
```
|
||||
ast;
|
||||
tree;
|
||||
|
||||
ptype int;
|
||||
|
||||
@@ -284,23 +285,23 @@ assert_eq(22, itemsmore.item.pToken1.pvalue);
|
||||
assert(itemsmore.pItemsMore is null);
|
||||
```
|
||||
|
||||
## `ast_prefix` and `ast_suffix` statements
|
||||
## `tree_prefix` and `tree_suffix` statements
|
||||
|
||||
In AST generation mode, structure types are defined and named based on the
|
||||
In tree generation mode, structure types are defined and named based on the
|
||||
rules in the grammar.
|
||||
Additionally, a structure type called `Token` is generated to hold parsed
|
||||
token information.
|
||||
|
||||
These structure names can be modified by using the `ast_prefix` or `ast_suffix`
|
||||
These structure names can be modified by using the `tree_prefix` or `tree_suffix`
|
||||
statements in the grammar file.
|
||||
The field names that point to instances of the structures are not affected by
|
||||
the `ast_prefix` or `ast_suffix` values.
|
||||
the `tree_prefix` or `tree_suffix` values.
|
||||
|
||||
For example, if the following two lines were added to the example above:
|
||||
|
||||
```
|
||||
ast_prefix ABC;
|
||||
ast_suffix XYZ;
|
||||
tree_prefix ABC;
|
||||
tree_suffix XYZ;
|
||||
```
|
||||
|
||||
Then the types would be used as such instead:
|
||||
@@ -326,6 +327,36 @@ assert(itemsmore.pItem.pItem.pItem !is null);
|
||||
assert(itemsmore.pItem.pItem.pItem.pToken1 !is null);
|
||||
```
|
||||
|
||||
## Freeing user-allocated memory in token node `pvalue`: the `free_token_node` statement
|
||||
|
||||
If user lexer code block allocates memory to store in a token node's `pvalue`,
|
||||
the `free_token_node` grammar statement can be used to specify the name of a
|
||||
function which will be called during the `p_free_tree()` call to free the memory
|
||||
associated with a token node.
|
||||
|
||||
Example:
|
||||
|
||||
```
|
||||
<<
|
||||
static void free_token(Token * token)
|
||||
{
|
||||
free(token->pvalue);
|
||||
}
|
||||
>>
|
||||
tree;
|
||||
free_token_node free_token;
|
||||
ptype int *;
|
||||
token a <<
|
||||
$$ = (int *)malloc(sizeof(int));
|
||||
*$$ = 1;
|
||||
>>
|
||||
token b <<
|
||||
$$ = (int *)malloc(sizeof(int));
|
||||
*$$ = 2;
|
||||
>>
|
||||
Start -> a:a b:b;
|
||||
```
|
||||
|
||||
##> Specifying tokens - the `token` statement
|
||||
|
||||
The `token` statement allows defining a lexer token and a pattern to match that
|
||||
@@ -611,7 +642,7 @@ In this example:
|
||||
* a reduced `Values`'s parser value has a type of `Value[]`.
|
||||
* a reduced `KeyValue`'s parser value has a type of `Value[string]`.
|
||||
|
||||
When AST generation mode is active, the `ptype` functionality works differently.
|
||||
When tree generation mode is active, the `ptype` functionality works differently.
|
||||
In this mode, only one `ptype` is used by the parser.
|
||||
Lexer user code blocks may assign a parse value to the generated `Token` node
|
||||
by assigning to `$$` within a lexer code block.
|
||||
@@ -670,8 +701,8 @@ A field can be immediately followed by a `?` character to signify that it is
|
||||
optional.
|
||||
A field can optionally be followed by a `:` and then a field alias name.
|
||||
If present, the field alias name is used to refer to the field value in user
|
||||
code blocks, or if AST mode is active, the field alias name is used as the
|
||||
field name in the generated AST node structure.
|
||||
code blocks, or if tree generation mode is active, the field alias name is used
|
||||
as the field name in the generated tree node structure.
|
||||
An optional and named field must use the format `field?:name`.
|
||||
Example:
|
||||
|
||||
@@ -695,7 +726,7 @@ The `$$` symbol accesses the output parser value for this rule.
|
||||
The above examples demonstrate how the parser values for the rule components
|
||||
can be used to produce the parser value for the accepted rule.
|
||||
|
||||
Parser rule code blocks are not allowed and not used when AST generation mode
|
||||
Parser rule code blocks are not allowed and not used when tree generation mode
|
||||
is active.
|
||||
|
||||
##> Specifying the parser start rule name - the `start` statement
|
||||
@@ -708,6 +739,22 @@ Example:
|
||||
start MyStartRule;
|
||||
```
|
||||
|
||||
Multiple start rules can be specified, either with multiple `start` statements
|
||||
or one `start` statement listing multiple start rules.
|
||||
Example:
|
||||
|
||||
```
|
||||
start Module ModuleItem Statement Expression;
|
||||
```
|
||||
|
||||
When multiple start rules are specified, multiple `p_parse_*()` functions,
|
||||
`p_result_*()`, and `p_free_tree_*()` functions (in tree mode) are generated.
|
||||
A default `p_parse()`, `p_result()`, `p_free_tree()` are generated corresponding
|
||||
to the first start rule.
|
||||
Additionally, each start rule causes the generation of another version of each
|
||||
of these functions, for example `p_parse_Statement()`, `p_result_Statement()`,
|
||||
and `p_free_tree_Statement()`.
|
||||
|
||||
##> Specifying the parser module name - the `module` statement
|
||||
|
||||
The `module` statement can be used to specify the module name for a generated
|
||||
@@ -827,17 +874,17 @@ The `p_token_info_t` structure contains the following fields:
|
||||
* `token` (`p_token_t`) holds the token ID of the lexed token
|
||||
* `pvalue` (`p_value_t`) holds the parser value associated with the token.
|
||||
|
||||
### AST Node Types
|
||||
### Tree Node Types
|
||||
|
||||
If AST generation mode is enabled, a structure type for each rule will be
|
||||
If tree generation mode is enabled, a structure type for each rule will be
|
||||
generated.
|
||||
The name of the structure type is given by the name of the rule.
|
||||
Additionally a structure type called `Token` is generated to represent an
|
||||
AST node which refers to a raw parser token rather than a composite rule.
|
||||
Additionally a structure type called `Token` is generated to represent a
|
||||
tree node which refers to a raw parser token rather than a composite rule.
|
||||
|
||||
#### AST Node Fields
|
||||
#### Tree Node Fields
|
||||
|
||||
All AST nodes have a `position` field specifying the text position of the
|
||||
All tree nodes have a `position` field specifying the text position of the
|
||||
beginning of the matched token or rule, and an `end_position` field specifying
|
||||
the text position of the end of the matched token or rule.
|
||||
Each of these fields are instances of the `p_position_t` structure.
|
||||
@@ -856,7 +903,7 @@ A `Token` node has the following additional fields:
|
||||
* `pvalue` which specifies the parser value for the token. If a lexer user
|
||||
code block assigned to `$$`, the assigned value will be stored here.
|
||||
|
||||
AST node structures for rules contain generated fields based on the
|
||||
Tree node structures for rules contain generated fields based on the
|
||||
right hand side components specified for all rules of a given name.
|
||||
|
||||
In this example:
|
||||
@@ -879,10 +926,10 @@ The `Items` structure will have fields:
|
||||
* `pItemsMore` and `pItemsMore2` which point to the parsed `ItemsMore` structure.
|
||||
|
||||
If a rule can be empty (for example in the second `Items` rule above), then
|
||||
an instance of a pointer to that rule's generated AST node will be null if the
|
||||
an instance of a pointer to that rule's generated tree node will be null if the
|
||||
parser matches the empty rule pattern.
|
||||
|
||||
The non-positional AST node field pointer will not be generated if there are
|
||||
The non-positional tree node field pointer will not be generated if there are
|
||||
multiple positions in which an instance of the node it points to could be
|
||||
present.
|
||||
For example, in the below rules:
|
||||
@@ -902,7 +949,7 @@ If the first rule is matched, then `pOne1` and `pTwo2` will be non-null while
|
||||
If the second rule is matched instead, then the opposite would be the case.
|
||||
|
||||
If a field alias is present in a rule definition, an additional field will be
|
||||
generated in the AST node with the field alias name.
|
||||
generated in the tree node with the field alias name.
|
||||
For example:
|
||||
|
||||
```
|
||||
@@ -988,6 +1035,16 @@ p_context_init(&context, input, input_length);
|
||||
size_t result = p_parse(&context);
|
||||
```
|
||||
|
||||
When multiple start rules are specified, a separate parse function is generated
|
||||
for each which starts parsing at the given rule.
|
||||
For example, if `Statement` is specified as a start rule:
|
||||
|
||||
```
|
||||
size_t result = p_parse_Statement(&context);
|
||||
```
|
||||
|
||||
In this case, the parser will start parsing with the `Statement` rule.
|
||||
|
||||
### `p_position_valid`
|
||||
|
||||
The `p_position_valid()` function is only generated for C targets.
|
||||
@@ -1023,8 +1080,25 @@ if (p_parse(&context) == P_SUCCESS)
|
||||
}
|
||||
```
|
||||
|
||||
If AST generation mode is active, then the `p_result()` function returns a
|
||||
`Start *` pointing to the `Start` AST structure.
|
||||
If tree generation mode is active, then the `p_result()` function returns a
|
||||
`Start *` pointing to the `Start` tree node structure.
|
||||
|
||||
When multiple start rules are specified, a separate result function is generated
|
||||
for each which returns the parse result for the corresponding rule.
|
||||
For example, if `Statement` is specified as a start rule:
|
||||
|
||||
```
|
||||
p_context_t context;
|
||||
p_context_init(&context, input, input_length);
|
||||
size_t result = p_parse(&context);
|
||||
if (p_parse_Statement(&context) == P_SUCCESS)
|
||||
{
|
||||
result = p_result_Statement(&context);
|
||||
}
|
||||
```
|
||||
|
||||
In this case, the parser will start parsing with the `Statement` rule and the
|
||||
parse result from the `Statement` rule will be returned.
|
||||
|
||||
### `p_position`
|
||||
|
||||
@@ -1098,6 +1172,31 @@ assert(code_point == 0x1F9E1u);
|
||||
assert(code_point_length == 4u);
|
||||
```
|
||||
|
||||
### `p_free_tree`
|
||||
|
||||
The `p_free_tree()` function can be used to free the memory used by the tree.
|
||||
It should be passed the same value that is returned by `p_result()`.
|
||||
|
||||
The `p_free_tree()` function is only available for C/C++ output targets.
|
||||
|
||||
Note that if any lexer user code block allocates memory to store in a token's
|
||||
`pvalue`, in order to properly free this memory a `free_token_node` function
|
||||
should be specified in the grammar file.
|
||||
If specified, the `free_token_node` function will be called during the
|
||||
`p_free_tree()` process to allow user code to free any memory associated with
|
||||
a token node's `pvalue`.
|
||||
|
||||
When multiple start rules are specified, a separate `p_free_tree` function is
|
||||
generated for each which frees the tree resulting from parsing the given rule.
|
||||
For example, if `Statement` is specified as a start rule:
|
||||
|
||||
```
|
||||
p_free_tree_Statement(statement_tree);
|
||||
```
|
||||
|
||||
In this case, Propane will free a `Statement` tree structure returned by the
|
||||
`p_parse_Statement(&context)` function.
|
||||
|
||||
##> Data
|
||||
|
||||
### `p_token_names`
|
||||
|
||||
@@ -20,7 +20,7 @@ syn match propaneOperator "->"
|
||||
syn match propaneFieldAlias ":[a-zA-Z0-9_]\+" contains=propaneFieldOperator
|
||||
syn match propaneFieldOperator ":" contained
|
||||
syn match propaneOperator "?"
|
||||
syn keyword propaneKeyword ast ast_prefix ast_suffix drop module prefix ptype start token tokenid
|
||||
syn keyword propaneKeyword drop free_token_node module prefix ptype start token tokenid tree tree_prefix tree_suffix
|
||||
|
||||
syn region propaneRegex start="/" end="/" skip="\v\\\\|\\/"
|
||||
|
||||
|
||||
@@ -71,12 +71,15 @@ class Propane
|
||||
end
|
||||
tokens_by_name[token.name] = token
|
||||
end
|
||||
# Check for user start rule.
|
||||
unless @grammar.rules.find {|rule| rule.name == @grammar.start_rule}
|
||||
raise Error.new("Start rule `#{@grammar.start_rule}` not found")
|
||||
# Create real start rule(s).
|
||||
real_start_rules = @grammar.start_rules.map do |start_rule|
|
||||
unless @grammar.rules.find {|rule| rule.name == start_rule}
|
||||
raise Error.new("Start rule `#{start_rule}` not found")
|
||||
end
|
||||
Rule.new("$#{start_rule}", [start_rule, "$EOF"], nil, nil, nil)
|
||||
end
|
||||
# Add "real" start rule.
|
||||
@grammar.rules.unshift(Rule.new("$Start", [@grammar.start_rule, "$EOF"], nil, nil, nil))
|
||||
# Add real start rules before user-given rules.
|
||||
@grammar.rules = real_start_rules + @grammar.rules
|
||||
# Generate and add rules for optional components.
|
||||
generate_optional_component_rules!(tokens_by_name)
|
||||
# Build rule sets.
|
||||
@@ -295,7 +298,7 @@ class Propane
|
||||
end
|
||||
else
|
||||
code = code.gsub(/\$\$/) do |match|
|
||||
if @grammar.ast
|
||||
if @grammar.tree
|
||||
case @language
|
||||
when "c"
|
||||
"out_token_info->pvalue"
|
||||
@@ -332,9 +335,9 @@ class Propane
|
||||
#
|
||||
# @return [Array<String>]
|
||||
# Start rule parser value type name and type string.
|
||||
def start_rule_type
|
||||
def start_rule_type(start_rule_index = 0)
|
||||
start_rule = @grammar.rules.find do |rule|
|
||||
rule.name == @grammar.start_rule
|
||||
rule.name == @grammar.start_rules[start_rule_index]
|
||||
end
|
||||
[start_rule.ptypename, @grammar.ptypes[start_rule.ptypename]]
|
||||
end
|
||||
|
||||
+47
-34
@@ -5,13 +5,14 @@ class Propane
|
||||
# Reserve identifiers beginning with a double-underscore for internal use.
|
||||
IDENTIFIER_REGEX = /(?:[a-zA-Z]|_[a-zA-Z0-9])[a-zA-Z_0-9]*/
|
||||
|
||||
attr_reader :ast
|
||||
attr_reader :ast_prefix
|
||||
attr_reader :ast_suffix
|
||||
attr_reader :tree
|
||||
attr_reader :tree_prefix
|
||||
attr_reader :tree_suffix
|
||||
attr_reader :free_token_node
|
||||
attr_reader :modulename
|
||||
attr_reader :patterns
|
||||
attr_reader :rules
|
||||
attr_reader :start_rule
|
||||
attr_accessor :rules
|
||||
attr_reader :start_rules
|
||||
attr_reader :tokens
|
||||
attr_reader :code_blocks
|
||||
attr_reader :ptypes
|
||||
@@ -19,7 +20,7 @@ class Propane
|
||||
|
||||
def initialize(input)
|
||||
@patterns = []
|
||||
@start_rule = "Start"
|
||||
@start_rules = []
|
||||
@tokens = []
|
||||
@rules = []
|
||||
@code_blocks = {}
|
||||
@@ -29,10 +30,12 @@ class Propane
|
||||
@input = input.gsub("\r\n", "\n")
|
||||
@ptypes = {"default" => "void *"}
|
||||
@prefix = "p_"
|
||||
@ast = false
|
||||
@ast_prefix = ""
|
||||
@ast_suffix = ""
|
||||
@tree = false
|
||||
@tree_prefix = ""
|
||||
@tree_suffix = ""
|
||||
@free_token_node = nil
|
||||
parse_grammar!
|
||||
@start_rules << "Start" if @start_rules.empty?
|
||||
end
|
||||
|
||||
def ptype
|
||||
@@ -59,9 +62,10 @@ class Propane
|
||||
if parse_white_space!
|
||||
elsif parse_comment_line!
|
||||
elsif @modeline.nil? && parse_mode_label!
|
||||
elsif parse_ast_statement!
|
||||
elsif parse_ast_prefix_statement!
|
||||
elsif parse_ast_suffix_statement!
|
||||
elsif parse_tree_statement!
|
||||
elsif parse_tree_prefix_statement!
|
||||
elsif parse_tree_suffix_statement!
|
||||
elsif parse_free_token_node_statement!
|
||||
elsif parse_module_statement!
|
||||
elsif parse_ptype_statement!
|
||||
elsif parse_pattern_statement!
|
||||
@@ -94,21 +98,27 @@ class Propane
|
||||
consume!(/#.*\n/)
|
||||
end
|
||||
|
||||
def parse_ast_statement!
|
||||
if consume!(/ast\s*;/)
|
||||
@ast = true
|
||||
def parse_tree_statement!
|
||||
if consume!(/tree\s*;/)
|
||||
@tree = true
|
||||
end
|
||||
end
|
||||
|
||||
def parse_ast_prefix_statement!
|
||||
if md = consume!(/ast_prefix\s+(\w+)\s*;/)
|
||||
@ast_prefix = md[1]
|
||||
def parse_tree_prefix_statement!
|
||||
if md = consume!(/tree_prefix\s+(\w+)\s*;/)
|
||||
@tree_prefix = md[1]
|
||||
end
|
||||
end
|
||||
|
||||
def parse_ast_suffix_statement!
|
||||
if md = consume!(/ast_suffix\s+(\w+)\s*;/)
|
||||
@ast_suffix = md[1]
|
||||
def parse_tree_suffix_statement!
|
||||
if md = consume!(/tree_suffix\s+(\w+)\s*;/)
|
||||
@tree_suffix = md[1]
|
||||
end
|
||||
end
|
||||
|
||||
def parse_free_token_node_statement!
|
||||
if md = consume!(/free_token_node\s+(\w+)\s*;/)
|
||||
@free_token_node = md[1]
|
||||
end
|
||||
end
|
||||
|
||||
@@ -126,8 +136,8 @@ class Propane
|
||||
if consume!(/ptype\s+/)
|
||||
name = "default"
|
||||
if md = consume!(/(#{IDENTIFIER_REGEX})\s*=\s*/)
|
||||
if @ast
|
||||
raise Error.new("Multiple ptypes are unsupported in AST mode")
|
||||
if @tree
|
||||
raise Error.new("Multiple ptypes are unsupported in tree mode")
|
||||
end
|
||||
name = md[1]
|
||||
end
|
||||
@@ -141,8 +151,8 @@ class Propane
|
||||
md = consume!(/(#{IDENTIFIER_REGEX})\s*/, "expected token name")
|
||||
name = md[1]
|
||||
if md = consume!(/\((#{IDENTIFIER_REGEX})\)\s*/)
|
||||
if @ast
|
||||
raise Error.new("Multiple ptypes are unsupported in AST mode")
|
||||
if @tree
|
||||
raise Error.new("Multiple ptypes are unsupported in tree mode")
|
||||
end
|
||||
ptypename = md[1]
|
||||
end
|
||||
@@ -165,8 +175,8 @@ class Propane
|
||||
md = consume!(/(#{IDENTIFIER_REGEX})\s*/, "expected token name")
|
||||
name = md[1]
|
||||
if md = consume!(/\((#{IDENTIFIER_REGEX})\)\s*/)
|
||||
if @ast
|
||||
raise Error.new("Multiple ptypes are unsupported in AST mode")
|
||||
if @tree
|
||||
raise Error.new("Multiple ptypes are unsupported in tree mode")
|
||||
end
|
||||
ptypename = md[1]
|
||||
end
|
||||
@@ -195,12 +205,12 @@ class Propane
|
||||
def parse_rule_statement!
|
||||
if md = consume!(/(#{IDENTIFIER_REGEX})\s*(?:\((#{IDENTIFIER_REGEX})\))?\s*->\s*/)
|
||||
rule_name, ptypename = *md[1, 2]
|
||||
if @ast && ptypename
|
||||
raise Error.new("Multiple ptypes are unsupported in AST mode")
|
||||
if @tree && ptypename
|
||||
raise Error.new("Multiple ptypes are unsupported in tree mode")
|
||||
end
|
||||
md = consume!(/((?:#{IDENTIFIER_REGEX}\??(?::#{IDENTIFIER_REGEX})?\s*)*)\s*/, "expected rule component list")
|
||||
components = md[1].strip.split(/\s+/)
|
||||
if @ast
|
||||
if @tree
|
||||
consume!(/;/, "expected `;'")
|
||||
else
|
||||
unless code = parse_code_block!
|
||||
@@ -217,8 +227,8 @@ class Propane
|
||||
if pattern = parse_pattern!
|
||||
consume!(/\s+/)
|
||||
if md = consume!(/\((#{IDENTIFIER_REGEX})\)\s*/)
|
||||
if @ast
|
||||
raise Error.new("Multiple ptypes are unsupported in AST mode")
|
||||
if @tree
|
||||
raise Error.new("Multiple ptypes are unsupported in tree mode")
|
||||
end
|
||||
ptypename = md[1]
|
||||
end
|
||||
@@ -232,8 +242,11 @@ class Propane
|
||||
end
|
||||
|
||||
def parse_start_statement!
|
||||
if md = consume!(/start\s+(\w+)\s*;/)
|
||||
@start_rule = md[1]
|
||||
if md = consume!(/start\s+([\w\s]*);/)
|
||||
start_rules = md[1].split(/\s+/).map(&:strip)
|
||||
start_rules.each do |start_rule|
|
||||
@start_rules << start_rule unless @start_rules.include?(start_rule)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
+12
-3
@@ -16,11 +16,20 @@ class Propane
|
||||
@warnings = Set.new
|
||||
@errors = Set.new
|
||||
@options = options
|
||||
start_item = Item.new(grammar.rules.first, 0)
|
||||
eval_item_sets = Set[ItemSet.new([start_item])]
|
||||
start_items = grammar.rules[0...grammar.start_rules.length].map do |start_rule|
|
||||
Item.new(start_rule, 0)
|
||||
end
|
||||
start_item_sets = start_items.map {|item| ItemSet.new([item])}
|
||||
eval_item_sets = Set[*start_item_sets]
|
||||
|
||||
while eval_item_sets.size > 0
|
||||
item_set = eval_item_sets.first
|
||||
item_set =
|
||||
if start_item_sets.size > 0
|
||||
# Ensure we evaluate start_item_sets first in order
|
||||
start_item_sets.slice!(0)
|
||||
else
|
||||
eval_item_sets.first
|
||||
end
|
||||
eval_item_sets.delete(item_set)
|
||||
unless @item_sets_set.include?(item_set)
|
||||
item_set.id = @item_sets.size
|
||||
|
||||
+1
-1
@@ -36,7 +36,7 @@ class Propane
|
||||
|
||||
# @return [Array<Integer>]
|
||||
# Map this rule's components to their positions in the parent RuleSet's
|
||||
# node field pointer array. This is used for AST construction.
|
||||
# node field pointer array. This is used for tree construction.
|
||||
attr_accessor :rule_set_node_field_index_map
|
||||
|
||||
# Construct a Rule.
|
||||
|
||||
+23
-23
@@ -4,8 +4,8 @@ class Propane
|
||||
class RuleSet
|
||||
|
||||
# @return [Array<Hash>]
|
||||
# AST fields.
|
||||
attr_reader :ast_fields
|
||||
# tree fields.
|
||||
attr_reader :tree_fields
|
||||
|
||||
# @return [Integer]
|
||||
# ID of the RuleSet.
|
||||
@@ -100,28 +100,28 @@ class Propane
|
||||
|
||||
# Finalize a RuleSet after adding all Rules to it.
|
||||
def finalize(grammar)
|
||||
if grammar.ast
|
||||
build_ast_fields(grammar)
|
||||
if grammar.tree
|
||||
build_tree_fields(grammar)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Build the set of AST fields for this RuleSet.
|
||||
# Build the set of tree fields for this RuleSet.
|
||||
#
|
||||
# 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
|
||||
# field location in the tree 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 [void]
|
||||
def build_ast_fields(grammar)
|
||||
field_ast_node_indexes = {}
|
||||
def build_tree_fields(grammar)
|
||||
field_tree_node_indexes = {}
|
||||
field_indexes_across_all_rules = {}
|
||||
# Stores the index into @ast_fields by field alias name.
|
||||
# Stores the index into @tree_fields by field alias name.
|
||||
field_aliases = {}
|
||||
@ast_fields = []
|
||||
@tree_fields = []
|
||||
@rules.each do |rule|
|
||||
rule.components.each_with_index do |component, i|
|
||||
if component.is_a?(RuleSet) && component.optional?
|
||||
@@ -132,25 +132,25 @@ class Propane
|
||||
else
|
||||
node_name = component.name
|
||||
end
|
||||
struct_name = "#{grammar.ast_prefix}#{node_name}#{grammar.ast_suffix}"
|
||||
struct_name = "#{grammar.tree_prefix}#{node_name}#{grammar.tree_suffix}"
|
||||
field_name = "p#{node_name}#{i + 1}"
|
||||
unless field_ast_node_indexes[field_name]
|
||||
field_ast_node_indexes[field_name] = @ast_fields.size
|
||||
@ast_fields << {field_name => struct_name}
|
||||
unless field_tree_node_indexes[field_name]
|
||||
field_tree_node_indexes[field_name] = @tree_fields.size
|
||||
@tree_fields << {field_name => struct_name}
|
||||
end
|
||||
rule.aliases.each do |alias_name, index|
|
||||
if index == i
|
||||
alias_ast_fields_index = field_ast_node_indexes[field_name]
|
||||
if field_aliases[alias_name] && field_aliases[alias_name] != alias_ast_fields_index
|
||||
raise Error.new("Error: conflicting AST node field positions for alias `#{alias_name}` in rule #{rule.name} defined on line #{rule.line_number}")
|
||||
alias_tree_fields_index = field_tree_node_indexes[field_name]
|
||||
if field_aliases[alias_name] && field_aliases[alias_name] != alias_tree_fields_index
|
||||
raise Error.new("Error: conflicting tree node field positions for alias `#{alias_name}` in rule #{rule.name} defined on line #{rule.line_number}")
|
||||
end
|
||||
field_aliases[alias_name] = alias_ast_fields_index
|
||||
@ast_fields[alias_ast_fields_index][alias_name] = @ast_fields[alias_ast_fields_index].first[1]
|
||||
field_aliases[alias_name] = alias_tree_fields_index
|
||||
@tree_fields[alias_tree_fields_index][alias_name] = @tree_fields[alias_tree_fields_index].first[1]
|
||||
end
|
||||
end
|
||||
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]
|
||||
field_indexes_across_all_rules[node_name] << field_tree_node_indexes[field_name]
|
||||
rule.rule_set_node_field_index_map[i] = field_tree_node_indexes[field_name]
|
||||
end
|
||||
end
|
||||
field_indexes_across_all_rules.each do |node_name, indexes_across_all_rules|
|
||||
@@ -158,8 +158,8 @@ class Propane
|
||||
# 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_fields[indexes_across_all_rules.first]["p#{node_name}"] =
|
||||
"#{grammar.ast_prefix}#{node_name}#{grammar.ast_suffix}"
|
||||
@tree_fields[indexes_across_all_rules.first]["p#{node_name}"] =
|
||||
"#{grammar.tree_prefix}#{node_name}#{grammar.tree_suffix}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
class Propane
|
||||
VERSION = "2.3.0"
|
||||
VERSION = "3.0.0"
|
||||
end
|
||||
+163
-76
@@ -78,9 +78,9 @@ EOF
|
||||
end
|
||||
case options[:language]
|
||||
when "c"
|
||||
command = [*%w[gcc -Wall -o spec/run/testparser -Ispec -Ispec/run], *parsers, *test_files, "spec/testutils.c", "-lm"]
|
||||
command = [*%w[gcc -g -Wall -o spec/run/testparser -Ispec -Ispec/run], *parsers, *test_files, "spec/testutils.c", "-lm"]
|
||||
when "cpp"
|
||||
command = [*%w[g++ -x c++ -Wall -o spec/run/testparser -Ispec -Ispec/run], *parsers, *test_files, "spec/testutils.c", "-lm"]
|
||||
command = [*%w[g++ -g -x c++ -Wall -o spec/run/testparser -Ispec -Ispec/run], *parsers, *test_files, "spec/testutils.c", "-lm"]
|
||||
when "d"
|
||||
command = [*%w[ldc2 -g --unittest -of spec/run/testparser -Ispec], *parsers, *test_files, "spec/testutils.d"]
|
||||
end
|
||||
@@ -88,12 +88,24 @@ EOF
|
||||
expect(result).to be_truthy
|
||||
end
|
||||
|
||||
def run_test
|
||||
def run_test(options)
|
||||
stdout, stderr, status = Open3.capture3("spec/run/testparser")
|
||||
File.binwrite("spec/run/.stderr", stderr)
|
||||
File.binwrite("spec/run/.stdout", stdout)
|
||||
stderr.sub!(/^.*modules passed unittests\n/, "")
|
||||
Results.new(stdout, stderr, status)
|
||||
results = Results.new(stdout, stderr, status)
|
||||
if %w[c cpp].include?(options[:language])
|
||||
stdout, stderr, status = Open3.capture3("valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --verbose spec/run/testparser")
|
||||
vgout = stdout + stderr
|
||||
File.binwrite("spec/run/.vgout", vgout)
|
||||
vgout.scan(/lost: (\d+) bytes/) do |match|
|
||||
bytes = $1.to_i
|
||||
if bytes > 0
|
||||
raise "Valgrind detected memory leak"
|
||||
end
|
||||
end
|
||||
end
|
||||
results
|
||||
end
|
||||
|
||||
def lines(str)
|
||||
@@ -233,20 +245,20 @@ EOF
|
||||
expect(results.status).to_not eq 0
|
||||
end
|
||||
|
||||
it "errors when an alias is in different positions for different rules in a rule set when AST mode is enabled" do
|
||||
it "errors when an alias is in different positions for different rules in a rule set when tree mode is enabled" do
|
||||
write_grammar <<EOF
|
||||
ast;
|
||||
tree;
|
||||
token a;
|
||||
token b;
|
||||
Start -> a:foo b;
|
||||
Start -> b b:foo;
|
||||
EOF
|
||||
results = run_propane(extra_args: %w[-w], capture: true)
|
||||
expect(results.stderr).to match %r{Error: conflicting AST node field positions for alias `foo`}
|
||||
expect(results.stderr).to match %r{Error: conflicting tree node field positions for alias `foo`}
|
||||
expect(results.status).to_not eq 0
|
||||
end
|
||||
|
||||
it "does not error when an alias is in different positions for different rules in a rule set when AST mode is not enabled" do
|
||||
it "does not error when an alias is in different positions for different rules in a rule set when tree mode is not enabled" do
|
||||
write_grammar <<EOF
|
||||
token a;
|
||||
token b;
|
||||
@@ -286,7 +298,7 @@ Foo -> plus <<>>
|
||||
EOF
|
||||
run_propane(language: language)
|
||||
compile("spec/test_lexer.#{language}", language: language)
|
||||
results = run_test
|
||||
results = run_test(language: language)
|
||||
expect(results.stderr).to eq ""
|
||||
expect(results.status).to eq 0
|
||||
end
|
||||
@@ -324,7 +336,7 @@ EOF
|
||||
end
|
||||
run_propane(language: language)
|
||||
compile("spec/test_lexer_unknown_character.#{language}", language: language)
|
||||
results = run_test
|
||||
results = run_test(language: language)
|
||||
expect(results.stderr).to eq ""
|
||||
expect(results.status).to eq 0
|
||||
end
|
||||
@@ -418,7 +430,7 @@ EOF
|
||||
end
|
||||
run_propane(language: language)
|
||||
compile("spec/test_basic_math_grammar.#{language}", language: language)
|
||||
results = run_test
|
||||
results = run_test(language: language)
|
||||
expect(results.stderr).to eq ""
|
||||
expect(results.status).to eq 0
|
||||
end
|
||||
@@ -444,7 +456,7 @@ R2 -> a b;
|
||||
EOF
|
||||
run_propane(language: language)
|
||||
compile("spec/test_parser_identical_rules_lookahead.#{language}", language: language)
|
||||
results = run_test
|
||||
results = run_test(language: language)
|
||||
expect(results.status).to eq 0
|
||||
end
|
||||
|
||||
@@ -459,7 +471,7 @@ R1 -> b;
|
||||
EOF
|
||||
run_propane(language: language)
|
||||
compile("spec/test_parser_rule_from_multiple_states.#{language}", language: language)
|
||||
results = run_test
|
||||
results = run_test(language: language)
|
||||
expect(results.status).to eq 0
|
||||
end
|
||||
|
||||
@@ -494,7 +506,7 @@ EOF
|
||||
end
|
||||
run_propane(language: language)
|
||||
compile("spec/test_user_code.#{language}", language: language)
|
||||
results = run_test
|
||||
results = run_test(language: language)
|
||||
expect(results.status).to eq 0
|
||||
verify_lines(results.stdout, [
|
||||
"abc!",
|
||||
@@ -530,7 +542,7 @@ EOF
|
||||
end
|
||||
run_propane(language: language)
|
||||
compile("spec/test_pattern.#{language}", language: language)
|
||||
results = run_test
|
||||
results = run_test(language: language)
|
||||
expect(results.status).to eq 0
|
||||
verify_lines(results.stdout, [
|
||||
"def!",
|
||||
@@ -572,7 +584,7 @@ EOF
|
||||
end
|
||||
run_propane(language: language)
|
||||
compile("spec/test_return_token_from_pattern.#{language}", language: language)
|
||||
results = run_test
|
||||
results = run_test(language: language)
|
||||
expect(results.status).to eq 0
|
||||
verify_lines(results.stdout, [
|
||||
"def!",
|
||||
@@ -630,7 +642,7 @@ EOF
|
||||
end
|
||||
run_propane(language: language)
|
||||
compile("spec/test_lexer_modes.#{language}", language: language)
|
||||
results = run_test
|
||||
results = run_test(language: language)
|
||||
expect(results.status).to eq 0
|
||||
verify_lines(results.stdout, [
|
||||
"begin string mode",
|
||||
@@ -688,7 +700,7 @@ EOF
|
||||
end
|
||||
run_propane(language: language)
|
||||
compile("spec/test_lexer_multiple_modes.#{language}", language: language)
|
||||
results = run_test
|
||||
results = run_test(language: language)
|
||||
expect(results.status).to eq 0
|
||||
verify_lines(results.stdout, [
|
||||
"ident: d",
|
||||
@@ -725,7 +737,7 @@ EOF
|
||||
end
|
||||
run_propane(language: language)
|
||||
compile("spec/test_parser_rule_user_code.#{language}", language: language)
|
||||
results = run_test
|
||||
results = run_test(language: language)
|
||||
expect(results.status).to eq 0
|
||||
verify_lines(results.stdout, [
|
||||
"A!",
|
||||
@@ -744,7 +756,7 @@ As -> As a << $$ = $1 + 1u; >>
|
||||
EOF
|
||||
run_propane(language: language)
|
||||
compile("spec/test_parsing_lists.#{language}", language: language)
|
||||
results = run_test
|
||||
results = run_test(language: language)
|
||||
expect(results.status).to eq 0
|
||||
expect(results.stderr).to eq ""
|
||||
end
|
||||
@@ -798,7 +810,7 @@ EOF
|
||||
end
|
||||
run_propane(language: language)
|
||||
compile("spec/test_lexer_match_text.#{language}", language: language)
|
||||
results = run_test
|
||||
results = run_test(language: language)
|
||||
expect(results.status).to eq 0
|
||||
verify_lines(results.stdout, [
|
||||
"Matched token is identifier_123",
|
||||
@@ -831,7 +843,7 @@ EOF
|
||||
end
|
||||
run_propane(language: language)
|
||||
compile("spec/test_lexer_result_value.#{language}", language: language)
|
||||
results = run_test
|
||||
results = run_test(language: language)
|
||||
expect(results.stderr).to eq ""
|
||||
expect(results.status).to eq 0
|
||||
end
|
||||
@@ -846,7 +858,7 @@ Start -> a num;
|
||||
EOF
|
||||
run_propane(language: language)
|
||||
compile("spec/test_error_positions.#{language}", language: language)
|
||||
results = run_test
|
||||
results = run_test(language: language)
|
||||
expect(results.stderr).to eq ""
|
||||
expect(results.status).to eq 0
|
||||
end
|
||||
@@ -875,7 +887,7 @@ Start -> b c b;
|
||||
EOF
|
||||
run_propane(name: "myp2", language: language)
|
||||
compile("spec/test_multiple_parsers.#{language}", parsers: %w[myp1 myp2], language: language)
|
||||
results = run_test
|
||||
results = run_test(language: language)
|
||||
expect(results.stderr).to eq ""
|
||||
expect(results.status).to eq 0
|
||||
end
|
||||
@@ -894,7 +906,7 @@ Any -> c;
|
||||
EOF
|
||||
run_propane(language: language)
|
||||
compile("spec/test_user_terminate_lexer.#{language}", language: language)
|
||||
results = run_test
|
||||
results = run_test(language: language)
|
||||
expect(results.stderr).to eq ""
|
||||
expect(results.status).to eq 0
|
||||
end
|
||||
@@ -912,7 +924,7 @@ Any -> ;
|
||||
EOF
|
||||
run_propane(language: language)
|
||||
compile("spec/test_user_terminate.#{language}", language: language)
|
||||
results = run_test
|
||||
results = run_test(language: language)
|
||||
expect(results.stderr).to eq ""
|
||||
expect(results.status).to eq 0
|
||||
end
|
||||
@@ -956,7 +968,7 @@ EOF
|
||||
end
|
||||
run_propane(language: language)
|
||||
compile("spec/test_match_backslashes.#{language}", language: language)
|
||||
results = run_test
|
||||
results = run_test(language: language)
|
||||
expect(results.stderr).to eq ""
|
||||
expect(results.status).to eq 0
|
||||
verify_lines(results.stdout, [
|
||||
@@ -984,9 +996,9 @@ EOF
|
||||
run_propane(language: language)
|
||||
end
|
||||
|
||||
it "generates an AST" do
|
||||
it "generates a tree" do
|
||||
write_grammar <<EOF
|
||||
ast;
|
||||
tree;
|
||||
|
||||
ptype int;
|
||||
|
||||
@@ -1020,17 +1032,17 @@ One -> one;
|
||||
Two -> two;
|
||||
EOF
|
||||
run_propane(language: language)
|
||||
compile("spec/test_ast.#{language}", language: language)
|
||||
results = run_test
|
||||
compile("spec/test_tree.#{language}", language: language)
|
||||
results = run_test(language: language)
|
||||
expect(results.stderr).to eq ""
|
||||
expect(results.status).to eq 0
|
||||
end
|
||||
|
||||
it "supports AST node prefix and suffix" do
|
||||
it "supports tree node prefix and suffix" do
|
||||
write_grammar <<EOF
|
||||
ast;
|
||||
ast_prefix P ;
|
||||
ast_suffix S;
|
||||
tree;
|
||||
tree_prefix P ;
|
||||
tree_suffix S;
|
||||
|
||||
ptype int;
|
||||
|
||||
@@ -1064,8 +1076,8 @@ One -> one;
|
||||
Two -> two;
|
||||
EOF
|
||||
run_propane(language: language)
|
||||
compile("spec/test_ast_ps.#{language}", language: language)
|
||||
results = run_test
|
||||
compile("spec/test_tree_ps.#{language}", language: language)
|
||||
results = run_test(language: language)
|
||||
expect(results.stderr).to eq ""
|
||||
expect(results.status).to eq 0
|
||||
end
|
||||
@@ -1080,15 +1092,15 @@ EOF
|
||||
compile("spec/test_start_rule.#{language}", language: language)
|
||||
end
|
||||
|
||||
it "allows specifying a different start rule with AST generation" do
|
||||
it "allows specifying a different start rule with tree generation" do
|
||||
write_grammar <<EOF
|
||||
ast;
|
||||
tree;
|
||||
token hi;
|
||||
start Top;
|
||||
Top -> hi;
|
||||
EOF
|
||||
run_propane(language: language)
|
||||
compile("spec/test_start_rule_ast.#{language}", language: language)
|
||||
compile("spec/test_start_rule_tree.#{language}", language: language)
|
||||
end
|
||||
|
||||
it "allows marking a rule component as optional" do
|
||||
@@ -1139,7 +1151,7 @@ EOF
|
||||
end
|
||||
run_propane(language: language)
|
||||
compile("spec/test_optional_rule_component.#{language}", language: language)
|
||||
results = run_test
|
||||
results = run_test(language: language)
|
||||
expect(results.stderr).to eq ""
|
||||
expect(results.status).to eq 0
|
||||
verify_lines(results.stdout, [
|
||||
@@ -1155,10 +1167,10 @@ EOF
|
||||
])
|
||||
end
|
||||
|
||||
it "allows marking a rule component as optional in AST generation mode" do
|
||||
it "allows marking a rule component as optional in tree generation mode" do
|
||||
if language == "d"
|
||||
write_grammar <<EOF
|
||||
ast;
|
||||
tree;
|
||||
|
||||
<<
|
||||
import std.stdio;
|
||||
@@ -1174,7 +1186,7 @@ R -> d c;
|
||||
EOF
|
||||
else
|
||||
write_grammar <<EOF
|
||||
ast;
|
||||
tree;
|
||||
|
||||
<<
|
||||
#include <stdio.h>
|
||||
@@ -1190,16 +1202,16 @@ R -> d c;
|
||||
EOF
|
||||
end
|
||||
run_propane(language: language)
|
||||
compile("spec/test_optional_rule_component_ast.#{language}", language: language)
|
||||
results = run_test
|
||||
compile("spec/test_optional_rule_component_tree.#{language}", language: language)
|
||||
results = run_test(language: language)
|
||||
expect(results.stderr).to eq ""
|
||||
expect(results.status).to eq 0
|
||||
end
|
||||
|
||||
it "allows naming an optional rule component in AST generation mode" do
|
||||
it "allows naming an optional rule component in tree generation mode" do
|
||||
if language == "d"
|
||||
write_grammar <<EOF
|
||||
ast;
|
||||
tree;
|
||||
|
||||
<<
|
||||
import std.stdio;
|
||||
@@ -1215,7 +1227,7 @@ R -> d c;
|
||||
EOF
|
||||
else
|
||||
write_grammar <<EOF
|
||||
ast;
|
||||
tree;
|
||||
|
||||
<<
|
||||
#include <stdio.h>
|
||||
@@ -1231,15 +1243,15 @@ R -> d c;
|
||||
EOF
|
||||
end
|
||||
run_propane(language: language)
|
||||
compile("spec/test_named_optional_rule_component_ast.#{language}", language: language)
|
||||
results = run_test
|
||||
compile("spec/test_named_optional_rule_component_tree.#{language}", language: language)
|
||||
results = run_test(language: language)
|
||||
expect(results.stderr).to eq ""
|
||||
expect(results.status).to eq 0
|
||||
end
|
||||
|
||||
it "stores token and rule positions in AST nodes" do
|
||||
it "stores token and rule positions in tree nodes" do
|
||||
write_grammar <<EOF
|
||||
ast;
|
||||
tree;
|
||||
|
||||
token a;
|
||||
token bb;
|
||||
@@ -1251,15 +1263,15 @@ T -> bb;
|
||||
T -> c;
|
||||
EOF
|
||||
run_propane(language: language)
|
||||
compile("spec/test_ast_token_positions.#{language}", language: language)
|
||||
results = run_test
|
||||
compile("spec/test_tree_token_positions.#{language}", language: language)
|
||||
results = run_test(language: language)
|
||||
expect(results.stderr).to eq ""
|
||||
expect(results.status).to eq 0
|
||||
end
|
||||
|
||||
it "stores invalid positions for empty rule matches" do
|
||||
write_grammar <<EOF
|
||||
ast;
|
||||
tree;
|
||||
|
||||
token a;
|
||||
token bb;
|
||||
@@ -1271,15 +1283,15 @@ T -> a A;
|
||||
A -> bb? c?;
|
||||
EOF
|
||||
run_propane(language: language)
|
||||
compile("spec/test_ast_invalid_positions.#{language}", language: language)
|
||||
results = run_test
|
||||
compile("spec/test_tree_invalid_positions.#{language}", language: language)
|
||||
results = run_test(language: language)
|
||||
expect(results.stderr).to eq ""
|
||||
expect(results.status).to eq 0
|
||||
end
|
||||
|
||||
it "allows specifying field aliases in AST mode" do
|
||||
it "allows specifying field aliases in tree mode" do
|
||||
write_grammar <<EOF
|
||||
ast;
|
||||
tree;
|
||||
|
||||
token a;
|
||||
token b;
|
||||
@@ -1291,15 +1303,15 @@ T -> b;
|
||||
T -> c;
|
||||
EOF
|
||||
run_propane(language: language)
|
||||
compile("spec/test_ast_field_aliases.#{language}", language: language)
|
||||
results = run_test
|
||||
compile("spec/test_tree_field_aliases.#{language}", language: language)
|
||||
results = run_test(language: language)
|
||||
expect(results.stderr).to eq ""
|
||||
expect(results.status).to eq 0
|
||||
end
|
||||
|
||||
it "aliases the correct field when multiple rules are in a rule set in AST mode" do
|
||||
it "aliases the correct field when multiple rules are in a rule set in tree mode" do
|
||||
write_grammar <<EOF
|
||||
ast;
|
||||
tree;
|
||||
|
||||
token a;
|
||||
token b;
|
||||
@@ -1314,13 +1326,13 @@ T -> b;
|
||||
T -> c;
|
||||
EOF
|
||||
run_propane(language: language)
|
||||
compile("spec/test_ast_field_aliases.#{language}", language: language)
|
||||
results = run_test
|
||||
compile("spec/test_tree_field_aliases.#{language}", language: language)
|
||||
results = run_test(language: language)
|
||||
expect(results.stderr).to eq ""
|
||||
expect(results.status).to eq 0
|
||||
end
|
||||
|
||||
it "allows specifying field aliases when AST mode is not enabled" do
|
||||
it "allows specifying field aliases when tree mode is not enabled" do
|
||||
if language == "d"
|
||||
write_grammar <<EOF
|
||||
<<
|
||||
@@ -1342,7 +1354,7 @@ EOF
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
>>
|
||||
ptype char const *;
|
||||
ptype char *;
|
||||
token id /[a-zA-Z_][a-zA-Z0-9_]*/ <<
|
||||
char * s = (char *)malloc(match_length + 1);
|
||||
strncpy(s, (char const *)match, match_length);
|
||||
@@ -1353,18 +1365,20 @@ drop /\\s+/;
|
||||
Start -> id:first id:second <<
|
||||
printf("first is %s\\n", ${first});
|
||||
printf("second is %s\\n", ${second});
|
||||
free(${first});
|
||||
free(${second});
|
||||
>>
|
||||
EOF
|
||||
end
|
||||
run_propane(language: language)
|
||||
compile("spec/test_field_aliases.#{language}", language: language)
|
||||
results = run_test
|
||||
results = run_test(language: language)
|
||||
expect(results.stderr).to eq ""
|
||||
expect(results.status).to eq 0
|
||||
expect(results.stdout).to match /first is foo1.*second is bar2/m
|
||||
end
|
||||
|
||||
it "aliases the correct field when multiple rules are in a rule set when AST mode is not enabled" do
|
||||
it "aliases the correct field when multiple rules are in a rule set when tree mode is not enabled" do
|
||||
if language == "d"
|
||||
write_grammar <<EOF
|
||||
<<
|
||||
@@ -1389,7 +1403,7 @@ EOF
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
>>
|
||||
ptype char const *;
|
||||
ptype char *;
|
||||
token id /[a-zA-Z_][a-zA-Z0-9_]*/ <<
|
||||
char * s = (char *)malloc(match_length + 1);
|
||||
strncpy(s, (char const *)match, match_length);
|
||||
@@ -1402,27 +1416,100 @@ Start -> Foo;
|
||||
Start -> id:first id:second <<
|
||||
printf("first is %s\\n", ${first});
|
||||
printf("second is %s\\n", ${second});
|
||||
free(${first});
|
||||
free(${second});
|
||||
>>
|
||||
Foo -> ;
|
||||
EOF
|
||||
end
|
||||
run_propane(language: language)
|
||||
compile("spec/test_field_aliases.#{language}", language: language)
|
||||
results = run_test
|
||||
results = run_test(language: language)
|
||||
expect(results.stderr).to eq ""
|
||||
expect(results.status).to eq 0
|
||||
expect(results.stdout).to match /first is foo1.*second is bar2/m
|
||||
end
|
||||
|
||||
it "does not free memory allocated for AST nodes" do
|
||||
it "does not free memory allocated for tree nodes" do
|
||||
ext = language == "cpp" ? "c" : language
|
||||
write_grammar(File.read("spec/ast_node_memory_remains.#{ext}.propane"))
|
||||
write_grammar(File.read("spec/tree_node_memory_remains.#{ext}.propane"))
|
||||
run_propane(language: language)
|
||||
compile("spec/test_ast_node_memory_remains.#{language}", language: language)
|
||||
results = run_test
|
||||
compile("spec/test_tree_node_memory_remains.#{language}", language: language)
|
||||
results = run_test(language: language)
|
||||
expect(results.stderr).to eq ""
|
||||
expect(results.status).to eq 0
|
||||
end
|
||||
|
||||
it "allows multiple starting rules" do
|
||||
write_grammar <<EOF
|
||||
ptype int;
|
||||
token a << $$ = 1; >>
|
||||
token b << $$ = 2; >>
|
||||
token c << $$ = 3; >>
|
||||
Start -> a b R;
|
||||
Start -> Bs:bs << $$ = $1; >>
|
||||
R -> c:c << $$ = $1; >>
|
||||
Bs -> << $$ = 0; >>
|
||||
Bs -> b:b Bs:bs << $$ = $1 + $2; >>
|
||||
start Start R Bs;
|
||||
EOF
|
||||
run_propane(language: language)
|
||||
compile("spec/test_starting_rules.#{language}", language: language)
|
||||
results = run_test(language: language)
|
||||
expect(results.stderr).to eq ""
|
||||
expect(results.status).to eq 0
|
||||
end
|
||||
|
||||
it "allows multiple starting rules in tree mode" do
|
||||
write_grammar <<EOF
|
||||
tree;
|
||||
ptype int;
|
||||
token a << $$ = 1; >>
|
||||
token b << $$ = 2; >>
|
||||
token c << $$ = 3; >>
|
||||
Start -> a b R;
|
||||
Start -> Bs:bs;
|
||||
R -> c:c;
|
||||
Bs -> ;
|
||||
Bs -> b:b Bs:bs;
|
||||
start Start R Bs;
|
||||
EOF
|
||||
run_propane(language: language)
|
||||
compile("spec/test_starting_rules_tree.#{language}", language: language)
|
||||
results = run_test(language: language)
|
||||
expect(results.stderr).to eq ""
|
||||
expect(results.status).to eq 0
|
||||
end
|
||||
|
||||
if %w[c cpp].include?(language)
|
||||
it "allows a user function to free token node memory in tree mode" do
|
||||
write_grammar <<EOF
|
||||
<<
|
||||
static void free_token(Token * token)
|
||||
{
|
||||
free(token->pvalue);
|
||||
}
|
||||
>>
|
||||
tree;
|
||||
free_token_node free_token;
|
||||
ptype int *;
|
||||
token a <<
|
||||
$$ = (int *)malloc(sizeof(int));
|
||||
*$$ = 1;
|
||||
>>
|
||||
token b <<
|
||||
$$ = (int *)malloc(sizeof(int));
|
||||
*$$ = 2;
|
||||
>>
|
||||
Start -> a:a b:b;
|
||||
EOF
|
||||
run_propane(language: language)
|
||||
compile("spec/test_free_tree_token_node_memory.#{language}", language: language)
|
||||
results = run_test(language: language)
|
||||
expect(results.stderr).to eq ""
|
||||
expect(results.status).to eq 0
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,19 @@
|
||||
#include "testparser.h"
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#include "testutils.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
char const * input = "ab";
|
||||
p_context_t context;
|
||||
p_context_init(&context, (uint8_t const *)input, strlen(input));
|
||||
assert_eq(P_SUCCESS, p_parse(&context));
|
||||
Start * start = p_result(&context);
|
||||
assert(start->a != NULL);
|
||||
assert(*start->a->pvalue == 1);
|
||||
assert(start->b != NULL);
|
||||
assert(*start->b->pvalue == 2);
|
||||
|
||||
p_free_tree(start);
|
||||
}
|
||||
+6
@@ -17,6 +17,8 @@ int main()
|
||||
assert(start->pR == NULL);
|
||||
assert(start->r == NULL);
|
||||
|
||||
p_free_tree(start);
|
||||
|
||||
input = "abcd";
|
||||
p_context_init(&context, (uint8_t const *)input, strlen(input));
|
||||
assert(p_parse(&context) == P_SUCCESS);
|
||||
@@ -31,6 +33,8 @@ int main()
|
||||
assert(start->pR == start->r);
|
||||
assert_eq(TOKEN_c, start->pR->pToken1->token);
|
||||
|
||||
p_free_tree(start);
|
||||
|
||||
input = "bdc";
|
||||
p_context_init(&context, (uint8_t const *)input, strlen(input));
|
||||
assert(p_parse(&context) == P_SUCCESS);
|
||||
@@ -40,6 +44,8 @@ int main()
|
||||
assert(start->r != NULL);
|
||||
assert_eq(TOKEN_d, start->pR->pToken1->token);
|
||||
|
||||
p_free_tree(start);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
File renamed without changes.
@@ -16,6 +16,8 @@ int main()
|
||||
assert(start->pR3 == NULL);
|
||||
assert(start->pR == NULL);
|
||||
|
||||
p_free_tree(start);
|
||||
|
||||
input = "abcd";
|
||||
p_context_init(&context, (uint8_t const *)input, strlen(input));
|
||||
assert(p_parse(&context) == P_SUCCESS);
|
||||
@@ -28,6 +30,8 @@ int main()
|
||||
assert(start->pR == start->pR3);
|
||||
assert_eq(TOKEN_c, start->pR->pToken1->token);
|
||||
|
||||
p_free_tree(start);
|
||||
|
||||
input = "bdc";
|
||||
p_context_init(&context, (uint8_t const *)input, strlen(input));
|
||||
assert(p_parse(&context) == P_SUCCESS);
|
||||
@@ -37,6 +41,8 @@ int main()
|
||||
assert(start->pR != NULL);
|
||||
assert_eq(TOKEN_d, start->pR->pToken1->token);
|
||||
|
||||
p_free_tree(start);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
File renamed without changes.
@@ -13,5 +13,7 @@ int main()
|
||||
assert(top->pToken != NULL);
|
||||
assert_eq(TOKEN_hi, top->pToken->token);
|
||||
|
||||
p_free_tree(top);
|
||||
|
||||
return 0;
|
||||
}
|
||||
File renamed without changes.
@@ -0,0 +1,27 @@
|
||||
#include "testparser.h"
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#include "testutils.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
char const * input = "bbbb";
|
||||
p_context_t context;
|
||||
p_context_init(&context, (uint8_t const *)input, strlen(input));
|
||||
assert(p_parse(&context) == P_SUCCESS);
|
||||
int result = p_result(&context);
|
||||
assert_eq(8, result);
|
||||
|
||||
p_context_init(&context, (uint8_t const *)input, strlen(input));
|
||||
assert(p_parse_Bs(&context) == P_SUCCESS);
|
||||
result = p_result_Bs(&context);
|
||||
assert_eq(8, result);
|
||||
|
||||
input = "c";
|
||||
p_context_init(&context, (uint8_t const *)input, strlen(input));
|
||||
assert(p_parse_R(&context) == P_SUCCESS);
|
||||
result = p_result_R(&context);
|
||||
assert_eq(3, result);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import testparser;
|
||||
import std.stdio;
|
||||
import testutils;
|
||||
|
||||
int main()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
unittest
|
||||
{
|
||||
string input = "bbbb";
|
||||
p_context_t context;
|
||||
p_context_init(&context, input);
|
||||
assert(p_parse(&context) == P_SUCCESS);
|
||||
int result = p_result(&context);
|
||||
assert(result == 8);
|
||||
|
||||
p_context_init(&context, input);
|
||||
assert(p_parse_Bs(&context) == P_SUCCESS);
|
||||
result = p_result_Bs(&context);
|
||||
assert(result == 8);
|
||||
|
||||
input = "c";
|
||||
p_context_init(&context, input);
|
||||
assert(p_parse_R(&context) == P_SUCCESS);
|
||||
result = p_result_R(&context);
|
||||
assert(result == 3);
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
#include "testparser.h"
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#include "testutils.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
char const * input = "bbbb";
|
||||
p_context_t context;
|
||||
p_context_init(&context, (uint8_t const *)input, strlen(input));
|
||||
assert(p_parse(&context) == P_SUCCESS);
|
||||
Start * start = p_result(&context);
|
||||
assert_not_null(start->bs);
|
||||
assert_not_null(start->bs->b);
|
||||
assert_not_null(start->bs->bs->b);
|
||||
assert_not_null(start->bs->bs->bs->b);
|
||||
assert_not_null(start->bs->bs->bs->bs->b);
|
||||
p_free_tree(start);
|
||||
|
||||
p_context_init(&context, (uint8_t const *)input, strlen(input));
|
||||
assert(p_parse_Bs(&context) == P_SUCCESS);
|
||||
Bs * bs = p_result_Bs(&context);
|
||||
assert_not_null(bs->b);
|
||||
assert_not_null(bs->bs->b);
|
||||
assert_not_null(bs->bs->bs->b);
|
||||
assert_not_null(bs->bs->bs->bs->b);
|
||||
p_free_tree_Bs(bs);
|
||||
|
||||
input = "c";
|
||||
p_context_init(&context, (uint8_t const *)input, strlen(input));
|
||||
assert(p_parse_R(&context) == P_SUCCESS);
|
||||
R * r = p_result_R(&context);
|
||||
assert_not_null(r->c);
|
||||
p_free_tree_R(r);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
import testparser;
|
||||
import std.stdio;
|
||||
import testutils;
|
||||
|
||||
int main()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
unittest
|
||||
{
|
||||
string input = "bbbb";
|
||||
p_context_t context;
|
||||
p_context_init(&context, input);
|
||||
assert(p_parse(&context) == P_SUCCESS);
|
||||
Start * start = p_result(&context);
|
||||
assert(start.bs);
|
||||
assert(start.bs.b);
|
||||
assert(start.bs.bs.b);
|
||||
assert(start.bs.bs.bs.b);
|
||||
assert(start.bs.bs.bs.bs.b);
|
||||
|
||||
p_context_init(&context, input);
|
||||
assert(p_parse_Bs(&context) == P_SUCCESS);
|
||||
Bs * bs = p_result_Bs(&context);
|
||||
assert(bs.b);
|
||||
assert(bs.bs.b);
|
||||
assert(bs.bs.bs.b);
|
||||
assert(bs.bs.bs.bs.b);
|
||||
|
||||
input = "c";
|
||||
p_context_init(&context, input);
|
||||
assert(p_parse_R(&context) == P_SUCCESS);
|
||||
R * r = p_result_R(&context);
|
||||
assert(r.c);
|
||||
}
|
||||
@@ -33,12 +33,16 @@ int main()
|
||||
assert_eq(22, itemsmore->pItem->pToken1->pvalue);
|
||||
assert(itemsmore->pItemsMore == NULL);
|
||||
|
||||
p_free_tree(start);
|
||||
|
||||
input = "";
|
||||
p_context_init(&context, (uint8_t const *)input, strlen(input));
|
||||
assert_eq(P_SUCCESS, p_parse(&context));
|
||||
start = p_result(&context);
|
||||
assert(start->pItems == NULL);
|
||||
|
||||
p_free_tree(start);
|
||||
|
||||
input = "2 1";
|
||||
p_context_init(&context, (uint8_t const *)input, strlen(input));
|
||||
assert_eq(P_SUCCESS, p_parse(&context));
|
||||
@@ -51,5 +55,7 @@ int main()
|
||||
assert(start->pItems->pItem->pDual->pTwo2 == NULL);
|
||||
assert(start->pItems->pItem->pDual->pOne1 == NULL);
|
||||
|
||||
p_free_tree(start);
|
||||
|
||||
return 0;
|
||||
}
|
||||
File renamed without changes.
@@ -15,5 +15,7 @@ int main()
|
||||
assert_eq(TOKEN_b, start->second->pToken->token);
|
||||
assert_eq(TOKEN_c, start->third->pToken->token);
|
||||
|
||||
p_free_tree(start);
|
||||
|
||||
return 0;
|
||||
}
|
||||
File renamed without changes.
@@ -30,6 +30,8 @@ int main()
|
||||
assert_eq(3, start->end_position.row);
|
||||
assert_eq(8, start->end_position.col);
|
||||
|
||||
p_free_tree(start);
|
||||
|
||||
input = "a\nbb";
|
||||
p_context_init(&context, (uint8_t const *)input, strlen(input));
|
||||
assert(p_parse(&context) == P_SUCCESS);
|
||||
@@ -54,6 +56,8 @@ int main()
|
||||
assert_eq(2, start->end_position.row);
|
||||
assert_eq(2, start->end_position.col);
|
||||
|
||||
p_free_tree(start);
|
||||
|
||||
input = "a\nc\nc";
|
||||
p_context_init(&context, (uint8_t const *)input, strlen(input));
|
||||
assert(p_parse(&context) == P_SUCCESS);
|
||||
@@ -78,6 +82,8 @@ int main()
|
||||
assert_eq(3, start->end_position.row);
|
||||
assert_eq(1, start->end_position.col);
|
||||
|
||||
p_free_tree(start);
|
||||
|
||||
input = "a";
|
||||
p_context_init(&context, (uint8_t const *)input, strlen(input));
|
||||
assert(p_parse(&context) == P_SUCCESS);
|
||||
@@ -98,5 +104,7 @@ int main()
|
||||
assert_eq(1, start->end_position.row);
|
||||
assert_eq(1, start->end_position.col);
|
||||
|
||||
p_free_tree(start);
|
||||
|
||||
return 0;
|
||||
}
|
||||
File renamed without changes.
@@ -411,5 +411,8 @@ int main(int argc, char * argv[])
|
||||
}
|
||||
}
|
||||
|
||||
free(pfds);
|
||||
p_free_tree(pmod);
|
||||
|
||||
return 0;
|
||||
}
|
||||
File renamed without changes.
@@ -33,12 +33,16 @@ int main()
|
||||
assert_eq(22, itemsmore->pItem->pToken1->pvalue);
|
||||
assert(itemsmore->pItemsMore == NULL);
|
||||
|
||||
p_free_tree(start);
|
||||
|
||||
input = "";
|
||||
p_context_init(&context, (uint8_t const *)input, strlen(input));
|
||||
assert_eq(P_SUCCESS, p_parse(&context));
|
||||
start = p_result(&context);
|
||||
assert(start->pItems == NULL);
|
||||
|
||||
p_free_tree(start);
|
||||
|
||||
input = "2 1";
|
||||
p_context_init(&context, (uint8_t const *)input, strlen(input));
|
||||
assert_eq(P_SUCCESS, p_parse(&context));
|
||||
@@ -51,5 +55,7 @@ int main()
|
||||
assert(start->pItems->pItem->pDual->pTwo2 == NULL);
|
||||
assert(start->pItems->pItem->pDual->pOne1 == NULL);
|
||||
|
||||
p_free_tree(start);
|
||||
|
||||
return 0;
|
||||
}
|
||||
File renamed without changes.
@@ -43,6 +43,8 @@ int main()
|
||||
assert_eq(1, start->end_position.row);
|
||||
assert_eq(6, start->end_position.col);
|
||||
|
||||
p_free_tree(start);
|
||||
|
||||
input = "\n\n bb\nc\ncc\n\n a";
|
||||
p_context_init(&context, (uint8_t const *)input, strlen(input));
|
||||
assert(p_parse(&context) == P_SUCCESS);
|
||||
@@ -80,5 +82,7 @@ int main()
|
||||
assert_eq(7, start->end_position.row);
|
||||
assert_eq(6, start->end_position.col);
|
||||
|
||||
p_free_tree(start);
|
||||
|
||||
return 0;
|
||||
}
|
||||
File renamed without changes.
@@ -14,6 +14,24 @@ void assert_eq_size_t_i(size_t expected, size_t actual, char const * file, size_
|
||||
}
|
||||
}
|
||||
|
||||
void assert_ne_size_t_i(size_t expected, size_t actual, char const * file, size_t line)
|
||||
{
|
||||
if (expected == actual)
|
||||
{
|
||||
fprintf(stderr, "%s:%lu: expected not %lu, got %lu\n", file, line, expected, actual);
|
||||
assert(false);
|
||||
}
|
||||
}
|
||||
|
||||
void assert_not_null_i(void * ptr, char const * file, size_t line)
|
||||
{
|
||||
if (ptr == NULL)
|
||||
{
|
||||
fprintf(stderr, "%s:%lu: expected not NULL\n", file, line);
|
||||
assert(false);
|
||||
}
|
||||
}
|
||||
|
||||
void str_init(str_t * str, char const * cs)
|
||||
{
|
||||
size_t length = strlen(cs);
|
||||
|
||||
@@ -5,6 +5,16 @@ void assert_eq_size_t_i(size_t expected, size_t actual, char const * file, size_
|
||||
#define assert_eq(expected, actual) \
|
||||
assert_eq_size_t_i(expected, actual, __FILE__, __LINE__)
|
||||
|
||||
void assert_ne_size_t_i(size_t expected, size_t actual, char const * file, size_t line);
|
||||
|
||||
#define assert_ne(expected, actual) \
|
||||
assert_ne_size_t_i(expected, actual, __FILE__, __LINE__)
|
||||
|
||||
void assert_not_null_i(void * ptr, char const * file, size_t line);
|
||||
|
||||
#define assert_not_null(ptr) \
|
||||
assert_not_null_i(ptr, __FILE__, __LINE__)
|
||||
|
||||
typedef struct
|
||||
{
|
||||
char * cs;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
ast;
|
||||
ast_prefix P;
|
||||
tree;
|
||||
tree_prefix P;
|
||||
|
||||
<<header
|
||||
#include <stdio.h>
|
||||
@@ -1,5 +1,5 @@
|
||||
ast;
|
||||
ast_prefix P;
|
||||
tree;
|
||||
tree_prefix P;
|
||||
|
||||
<<
|
||||
import std.bigint;
|
||||
Reference in new issue
Block a user