Compare commits
35 Commits
Author | SHA1 | Date | |
---|---|---|---|
c24f323ff0 | |||
fec2c28693 | |||
61339aeae9 | |||
95b3dc6550 | |||
74d94fef72 | |||
588c5e21c7 | |||
5f1c306273 | |||
343e8a7f9e | |||
b3a134bf8d | |||
4a71dc74fb | |||
a7348be95d | |||
9746b3f2bf | |||
c5b8fc28bd | |||
092fce61eb | |||
e647248e34 | |||
f4ae1b8601 | |||
eae2e17f41 | |||
87d6d29d60 | |||
3aced70356 | |||
2dd89445fc | |||
4ae5ab79b3 | |||
69cc8fa67d | |||
7f3eb8f315 | |||
d76e12fea1 | |||
911e9505b7 | |||
aaeb0c4db1 | |||
fd89c5c6b3 | |||
1468946735 | |||
2bccf3303e | |||
0d1ee74ca6 | |||
985b180f62 | |||
f3e4941ad8 | |||
494afb7307 | |||
508dabe760 | |||
153f9d28f8 |
31
CHANGELOG.md
31
CHANGELOG.md
@ -1,3 +1,34 @@
|
|||||||
|
## v1.5.1
|
||||||
|
|
||||||
|
### Improvements
|
||||||
|
|
||||||
|
- Improve performance (#28)
|
||||||
|
|
||||||
|
## v1.5.0
|
||||||
|
|
||||||
|
### New Features
|
||||||
|
|
||||||
|
- Track start and end text positions for tokens and rules in AST node structures (#27)
|
||||||
|
- Add warnings for shift/reduce conflicts to log file (#25)
|
||||||
|
- Add -w command line switch to treat warnings as errors and output to stderr (#26)
|
||||||
|
- Add rule field aliases (#24)
|
||||||
|
|
||||||
|
### Improvements
|
||||||
|
|
||||||
|
- Show line numbers of rules on conflict (#23)
|
||||||
|
|
||||||
|
## v1.4.0
|
||||||
|
|
||||||
|
### New Features
|
||||||
|
|
||||||
|
- Allow user to specify AST node name prefix or suffix
|
||||||
|
- Allow specifying the start rule name
|
||||||
|
- Allow rule terms to be marked as optional
|
||||||
|
|
||||||
|
### Improvements
|
||||||
|
|
||||||
|
- Give a better error message when a referenced ptype has not been declared
|
||||||
|
|
||||||
## v1.3.0
|
## v1.3.0
|
||||||
|
|
||||||
### New Features
|
### New Features
|
||||||
|
11
README.md
11
README.md
@ -31,9 +31,14 @@ Propane is typically invoked from the command-line as `./propane`.
|
|||||||
|
|
||||||
Usage: ./propane [options] <input-file> <output-file>
|
Usage: ./propane [options] <input-file> <output-file>
|
||||||
Options:
|
Options:
|
||||||
--log LOG Write log file
|
-h, --help Show this usage and exit.
|
||||||
--version Show program version and exit
|
--log LOG Write log file. This will show all parser states and their
|
||||||
-h, --help Show this usage and exit
|
associated shifts and reduces. It can be helpful when
|
||||||
|
debugging a grammar.
|
||||||
|
--version Show program version and exit.
|
||||||
|
-w Treat warnings as errors. This option will treat shift/reduce
|
||||||
|
conflicts as fatal errors and will print them to stderr in
|
||||||
|
addition to the log file.
|
||||||
|
|
||||||
The user must specify the path to a Propane input grammar file and a path to an
|
The user must specify the path to a Propane input grammar file and a path to an
|
||||||
output file.
|
output file.
|
||||||
|
@ -226,7 +226,10 @@ typedef struct
|
|||||||
/** Number of bytes of input text used to match. */
|
/** Number of bytes of input text used to match. */
|
||||||
size_t length;
|
size_t length;
|
||||||
|
|
||||||
/** Input text position delta. */
|
/** Input text position delta to end of token. */
|
||||||
|
<%= @grammar.prefix %>position_t end_delta_position;
|
||||||
|
|
||||||
|
/** Input text position delta to next code point after token end. */
|
||||||
<%= @grammar.prefix %>position_t delta_position;
|
<%= @grammar.prefix %>position_t delta_position;
|
||||||
|
|
||||||
/** Accepting lexer state from the match. */
|
/** Accepting lexer state from the match. */
|
||||||
@ -358,6 +361,7 @@ static size_t find_longest_match(<%= @grammar.prefix %>context_t * context,
|
|||||||
if (transition_state != INVALID_LEXER_STATE_ID)
|
if (transition_state != INVALID_LEXER_STATE_ID)
|
||||||
{
|
{
|
||||||
attempt_match.length += code_point_length;
|
attempt_match.length += code_point_length;
|
||||||
|
attempt_match.end_delta_position = attempt_match.delta_position;
|
||||||
if (code_point == '\n')
|
if (code_point == '\n')
|
||||||
{
|
{
|
||||||
attempt_match.delta_position.row++;
|
attempt_match.delta_position.row++;
|
||||||
@ -444,7 +448,6 @@ static size_t attempt_lex_token(<%= @grammar.prefix %>context_t * context, <%= @
|
|||||||
<%= @grammar.prefix %>token_info_t token_info = {0};
|
<%= @grammar.prefix %>token_info_t token_info = {0};
|
||||||
token_info.position = context->text_position;
|
token_info.position = context->text_position;
|
||||||
token_info.token = INVALID_TOKEN_ID;
|
token_info.token = INVALID_TOKEN_ID;
|
||||||
*out_token_info = token_info; // TODO: remove
|
|
||||||
lexer_match_info_t match_info;
|
lexer_match_info_t match_info;
|
||||||
size_t unexpected_input_length;
|
size_t unexpected_input_length;
|
||||||
size_t result = find_longest_match(context, &match_info, &unexpected_input_length);
|
size_t result = find_longest_match(context, &match_info, &unexpected_input_length);
|
||||||
@ -491,11 +494,22 @@ static size_t attempt_lex_token(<%= @grammar.prefix %>context_t * context, <%= @
|
|||||||
}
|
}
|
||||||
token_info.token = token_to_accept;
|
token_info.token = token_to_accept;
|
||||||
token_info.length = match_info.length;
|
token_info.length = match_info.length;
|
||||||
|
if (match_info.end_delta_position.row != 0u)
|
||||||
|
{
|
||||||
|
token_info.end_position.row = token_info.position.row + match_info.end_delta_position.row;
|
||||||
|
token_info.end_position.col = match_info.end_delta_position.col;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
token_info.end_position.row = token_info.position.row;
|
||||||
|
token_info.end_position.col = token_info.position.col + match_info.end_delta_position.col;
|
||||||
|
}
|
||||||
*out_token_info = token_info;
|
*out_token_info = token_info;
|
||||||
return P_SUCCESS;
|
return P_SUCCESS;
|
||||||
|
|
||||||
case P_EOF:
|
case P_EOF:
|
||||||
token_info.token = TOKEN___EOF;
|
token_info.token = TOKEN___EOF;
|
||||||
|
token_info.end_position = token_info.position;
|
||||||
*out_token_info = token_info;
|
*out_token_info = token_info;
|
||||||
return P_SUCCESS;
|
return P_SUCCESS;
|
||||||
|
|
||||||
@ -552,6 +566,9 @@ size_t <%= @grammar.prefix %>lex(<%= @grammar.prefix %>context_t * context, <%=
|
|||||||
* Parser
|
* Parser
|
||||||
*************************************************************************/
|
*************************************************************************/
|
||||||
|
|
||||||
|
/** Invalid position value. */
|
||||||
|
#define INVALID_POSITION (<%= @grammar.prefix %>position_t){0xFFFFFFFFu, 0xFFFFFFFFu}
|
||||||
|
|
||||||
/** Reduce ID type. */
|
/** Reduce ID type. */
|
||||||
typedef <%= get_type_for(@parser.reduce_table.size) %> reduce_id_t;
|
typedef <%= get_type_for(@parser.reduce_table.size) %> reduce_id_t;
|
||||||
|
|
||||||
@ -622,6 +639,13 @@ typedef struct
|
|||||||
* Number of rule set AST node fields.
|
* Number of rule set AST node fields.
|
||||||
*/
|
*/
|
||||||
uint16_t rule_set_node_field_array_size;
|
uint16_t rule_set_node_field_array_size;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether this rule was a generated optional rule that matched the
|
||||||
|
* optional target. In this case, propagate the matched target node up
|
||||||
|
* instead of making a new node for this rule.
|
||||||
|
*/
|
||||||
|
bool propagate_optional_target;
|
||||||
<% end %>
|
<% end %>
|
||||||
} reduce_t;
|
} reduce_t;
|
||||||
|
|
||||||
@ -660,10 +684,18 @@ typedef struct
|
|||||||
<% end %>
|
<% end %>
|
||||||
} state_value_t;
|
} state_value_t;
|
||||||
|
|
||||||
|
/** Common AST node structure. */
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
<%= @grammar.prefix %>position_t position;
|
||||||
|
<%= @grammar.prefix %>position_t end_position;
|
||||||
|
void * fields[];
|
||||||
|
} ASTNode;
|
||||||
|
|
||||||
/** Parser shift table. */
|
/** Parser shift table. */
|
||||||
static const shift_t parser_shift_table[] = {
|
static const shift_t parser_shift_table[] = {
|
||||||
<% @parser.shift_table.each do |shift| %>
|
<% @parser.shift_table.each do |shift| %>
|
||||||
{<%= shift[:symbol_id] %>u, <%= shift[:state_id] %>u},
|
{<%= shift[:symbol].id %>u, <%= shift[:state_id] %>u},
|
||||||
<% end %>
|
<% end %>
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -686,6 +718,7 @@ static const reduce_t parser_reduce_table[] = {
|
|||||||
, &r_<%= reduce[:rule].name.gsub("$", "_") %><%= reduce[:rule].id %>_node_field_index_map[0]
|
, &r_<%= reduce[:rule].name.gsub("$", "_") %><%= reduce[:rule].id %>_node_field_index_map[0]
|
||||||
<% end %>
|
<% end %>
|
||||||
, <%= reduce[:rule].rule_set.ast_fields.size %>
|
, <%= reduce[:rule].rule_set.ast_fields.size %>
|
||||||
|
, <%= reduce[:propagate_optional_target] %>
|
||||||
<% end %>
|
<% end %>
|
||||||
},
|
},
|
||||||
<% end %>
|
<% end %>
|
||||||
@ -924,7 +957,7 @@ size_t <%= @grammar.prefix %>parse(<%= @grammar.prefix %>context_t * context)
|
|||||||
{
|
{
|
||||||
/* Successful parse. */
|
/* Successful parse. */
|
||||||
<% if @grammar.ast %>
|
<% if @grammar.ast %>
|
||||||
context->parse_result = (Start *)state_values_stack_index(&statevalues, -1)->ast_node;
|
context->parse_result = (<%= @grammar.ast_prefix %><%= @grammar.start_rule %><%= @grammar.ast_suffix %> *)state_values_stack_index(&statevalues, -1)->ast_node;
|
||||||
<% else %>
|
<% else %>
|
||||||
context->parse_result = state_values_stack_index(&statevalues, -1)->pvalue;
|
context->parse_result = state_values_stack_index(&statevalues, -1)->pvalue;
|
||||||
<% end %>
|
<% end %>
|
||||||
@ -941,7 +974,9 @@ size_t <%= @grammar.prefix %>parse(<%= @grammar.prefix %>context_t * context)
|
|||||||
{
|
{
|
||||||
/* We shifted a token, mark it consumed. */
|
/* We shifted a token, mark it consumed. */
|
||||||
<% if @grammar.ast %>
|
<% if @grammar.ast %>
|
||||||
Token * token_ast_node = malloc(sizeof(Token));
|
<%= @grammar.ast_prefix %>Token<%= @grammar.ast_suffix %> * token_ast_node = 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->token = token;
|
||||||
token_ast_node->pvalue = token_info.pvalue;
|
token_ast_node->pvalue = token_info.pvalue;
|
||||||
state_values_stack_index(&statevalues, -1)->ast_node = token_ast_node;
|
state_values_stack_index(&statevalues, -1)->ast_node = token_ast_node;
|
||||||
@ -970,24 +1005,49 @@ size_t <%= @grammar.prefix %>parse(<%= @grammar.prefix %>context_t * context)
|
|||||||
{
|
{
|
||||||
/* We have something to reduce. */
|
/* We have something to reduce. */
|
||||||
<% if @grammar.ast %>
|
<% if @grammar.ast %>
|
||||||
if (parser_reduce_table[reduce_index].n_states > 0)
|
if (parser_reduce_table[reduce_index].propagate_optional_target)
|
||||||
{
|
{
|
||||||
void ** node_fields = calloc(parser_reduce_table[reduce_index].rule_set_node_field_array_size, sizeof(void *));
|
reduced_parser_node = state_values_stack_index(&statevalues, -1)->ast_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 *));
|
||||||
|
node->position = INVALID_POSITION;
|
||||||
|
node->end_position = INVALID_POSITION;
|
||||||
|
for (size_t i = 0; i < n_fields; i++)
|
||||||
|
{
|
||||||
|
node->fields[i] = NULL;
|
||||||
|
}
|
||||||
if (parser_reduce_table[reduce_index].rule_set_node_field_index_map == NULL)
|
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++)
|
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] = state_values_stack_index(&statevalues, -(int)parser_reduce_table[reduce_index].n_states + (int)i)->ast_node;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
for (size_t i = 0; i < parser_reduce_table[reduce_index].n_states; i++)
|
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]] = state_values_stack_index(&statevalues, -(int)parser_reduce_table[reduce_index].n_states + (int)i)->ast_node;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
reduced_parser_node = node_fields;
|
bool position_found = false;
|
||||||
|
for (size_t i = 0; i < n_fields; i++)
|
||||||
|
{
|
||||||
|
ASTNode * child = (ASTNode *)node->fields[i];
|
||||||
|
if ((child != NULL) && <%= @grammar.prefix %>position_valid(child->position))
|
||||||
|
{
|
||||||
|
if (!position_found)
|
||||||
|
{
|
||||||
|
node->position = child->position;
|
||||||
|
position_found = true;
|
||||||
|
}
|
||||||
|
node->end_position = child->end_position;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
reduced_parser_node = node;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -1029,7 +1089,7 @@ size_t <%= @grammar.prefix %>parse(<%= @grammar.prefix %>context_t * context)
|
|||||||
* @return Parse result value.
|
* @return Parse result value.
|
||||||
*/
|
*/
|
||||||
<% if @grammar.ast %>
|
<% if @grammar.ast %>
|
||||||
Start * <%= @grammar.prefix %>result(<%= @grammar.prefix %>context_t * context)
|
<%= @grammar.ast_prefix %><%= @grammar.start_rule %><%= @grammar.ast_suffix %> * <%= @grammar.prefix %>result(<%= @grammar.prefix %>context_t * context)
|
||||||
<% else %>
|
<% else %>
|
||||||
<%= start_rule_type[1] %> <%= @grammar.prefix %>result(<%= @grammar.prefix %>context_t * context)
|
<%= start_rule_type[1] %> <%= @grammar.prefix %>result(<%= @grammar.prefix %>context_t * context)
|
||||||
<% end %>
|
<% end %>
|
||||||
|
@ -8,6 +8,8 @@
|
|||||||
module <%= @grammar.modulename %>;
|
module <%= @grammar.modulename %>;
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
||||||
|
import core.stdc.stdlib : malloc;
|
||||||
|
|
||||||
/**************************************************************************
|
/**************************************************************************
|
||||||
* User code blocks
|
* User code blocks
|
||||||
*************************************************************************/
|
*************************************************************************/
|
||||||
@ -49,6 +51,29 @@ public enum : <%= @grammar.prefix %>token_t
|
|||||||
/** Code point type. */
|
/** Code point type. */
|
||||||
public alias <%= @grammar.prefix %>code_point_t = uint;
|
public alias <%= @grammar.prefix %>code_point_t = uint;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A structure to keep track of input position.
|
||||||
|
*
|
||||||
|
* This is useful for reporting errors, etc...
|
||||||
|
*/
|
||||||
|
public struct <%= @grammar.prefix %>position_t
|
||||||
|
{
|
||||||
|
/** Input text row (0-based). */
|
||||||
|
uint row;
|
||||||
|
|
||||||
|
/** Input text column (0-based). */
|
||||||
|
uint col;
|
||||||
|
|
||||||
|
/** Invalid position value. */
|
||||||
|
enum INVALID = <%= @grammar.prefix %>position_t(0xFFFF_FFFF, 0xFFFF_FFFF);
|
||||||
|
|
||||||
|
/** Return whether the position is valid. */
|
||||||
|
public @property bool valid()
|
||||||
|
{
|
||||||
|
return row != 0xFFFF_FFFFu;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
<% if @grammar.ast %>
|
<% if @grammar.ast %>
|
||||||
/** Parser values type. */
|
/** Parser values type. */
|
||||||
public alias <%= @grammar.prefix %>value_t = <%= @grammar.ptype %>;
|
public alias <%= @grammar.prefix %>value_t = <%= @grammar.ptype %>;
|
||||||
@ -63,17 +88,31 @@ public union <%= @grammar.prefix %>value_t
|
|||||||
<% end %>
|
<% end %>
|
||||||
|
|
||||||
<% if @grammar.ast %>
|
<% if @grammar.ast %>
|
||||||
/** AST node types. @{ */
|
/** Common AST node structure. */
|
||||||
public struct Token
|
private struct ASTNode
|
||||||
{
|
{
|
||||||
|
<%= @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 %>
|
||||||
|
{
|
||||||
|
/* ASTNode 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;
|
<%= @grammar.prefix %>token_t token;
|
||||||
<%= @grammar.prefix %>value_t pvalue;
|
<%= @grammar.prefix %>value_t pvalue;
|
||||||
}
|
}
|
||||||
|
|
||||||
<% @parser.rule_sets.each do |name, rule_set| %>
|
<% @parser.rule_sets.each do |name, rule_set| %>
|
||||||
<% next if name.start_with?("$") %>
|
<% next if name.start_with?("$") %>
|
||||||
public struct <%= name %>
|
<% next if rule_set.optional? %>
|
||||||
|
public struct <%= @grammar.ast_prefix %><%= name %><%= @grammar.ast_suffix %>
|
||||||
{
|
{
|
||||||
|
<%= @grammar.prefix %>position_t position;
|
||||||
|
<%= @grammar.prefix %>position_t end_position;
|
||||||
<% rule_set.ast_fields.each do |fields| %>
|
<% rule_set.ast_fields.each do |fields| %>
|
||||||
union
|
union
|
||||||
{
|
{
|
||||||
@ -88,26 +127,15 @@ public struct <%= name %>
|
|||||||
/** @} */
|
/** @} */
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
||||||
/**
|
|
||||||
* A structure to keep track of parser position.
|
|
||||||
*
|
|
||||||
* This is useful for reporting errors, etc...
|
|
||||||
*/
|
|
||||||
public struct <%= @grammar.prefix %>position_t
|
|
||||||
{
|
|
||||||
/** Input text row (0-based). */
|
|
||||||
uint row;
|
|
||||||
|
|
||||||
/** Input text column (0-based). */
|
|
||||||
uint col;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Lexed token information. */
|
/** Lexed token information. */
|
||||||
public struct <%= @grammar.prefix %>token_info_t
|
public struct <%= @grammar.prefix %>token_info_t
|
||||||
{
|
{
|
||||||
/** Text position where the token was found. */
|
/** Text position of first code point in token. */
|
||||||
<%= @grammar.prefix %>position_t position;
|
<%= @grammar.prefix %>position_t position;
|
||||||
|
|
||||||
|
/** Text position of last code point in token. */
|
||||||
|
<%= @grammar.prefix %>position_t end_position;
|
||||||
|
|
||||||
/** Number of input bytes used by the token. */
|
/** Number of input bytes used by the token. */
|
||||||
size_t length;
|
size_t length;
|
||||||
|
|
||||||
@ -144,7 +172,7 @@ public struct <%= @grammar.prefix %>context_t
|
|||||||
|
|
||||||
/** Parse result value. */
|
/** Parse result value. */
|
||||||
<% if @grammar.ast %>
|
<% if @grammar.ast %>
|
||||||
Start * parse_result;
|
<%= @grammar.ast_prefix %><%= @grammar.start_rule %><%= @grammar.ast_suffix %> * parse_result;
|
||||||
<% else %>
|
<% else %>
|
||||||
<%= @grammar.prefix %>value_t parse_result;
|
<%= @grammar.prefix %>value_t parse_result;
|
||||||
<% end %>
|
<% end %>
|
||||||
@ -371,7 +399,10 @@ private struct lexer_match_info_t
|
|||||||
/** Number of bytes of input text used to match. */
|
/** Number of bytes of input text used to match. */
|
||||||
size_t length;
|
size_t length;
|
||||||
|
|
||||||
/** Input text position delta. */
|
/** Input text position delta to end of token. */
|
||||||
|
<%= @grammar.prefix %>position_t end_delta_position;
|
||||||
|
|
||||||
|
/** Input text position delta to next code point after token end. */
|
||||||
<%= @grammar.prefix %>position_t delta_position;
|
<%= @grammar.prefix %>position_t delta_position;
|
||||||
|
|
||||||
/** Accepting lexer state from the match. */
|
/** Accepting lexer state from the match. */
|
||||||
@ -499,6 +530,7 @@ private size_t find_longest_match(<%= @grammar.prefix %>context_t * context,
|
|||||||
if (transition_state != INVALID_LEXER_STATE_ID)
|
if (transition_state != INVALID_LEXER_STATE_ID)
|
||||||
{
|
{
|
||||||
attempt_match.length += code_point_length;
|
attempt_match.length += code_point_length;
|
||||||
|
attempt_match.end_delta_position = attempt_match.delta_position;
|
||||||
if (code_point == '\n')
|
if (code_point == '\n')
|
||||||
{
|
{
|
||||||
attempt_match.delta_position.row++;
|
attempt_match.delta_position.row++;
|
||||||
@ -585,7 +617,6 @@ private size_t attempt_lex_token(<%= @grammar.prefix %>context_t * context, <%=
|
|||||||
<%= @grammar.prefix %>token_info_t token_info;
|
<%= @grammar.prefix %>token_info_t token_info;
|
||||||
token_info.position = context.text_position;
|
token_info.position = context.text_position;
|
||||||
token_info.token = INVALID_TOKEN_ID;
|
token_info.token = INVALID_TOKEN_ID;
|
||||||
*out_token_info = token_info; // TODO: remove
|
|
||||||
lexer_match_info_t match_info;
|
lexer_match_info_t match_info;
|
||||||
size_t unexpected_input_length;
|
size_t unexpected_input_length;
|
||||||
size_t result = find_longest_match(context, &match_info, &unexpected_input_length);
|
size_t result = find_longest_match(context, &match_info, &unexpected_input_length);
|
||||||
@ -632,11 +663,22 @@ private size_t attempt_lex_token(<%= @grammar.prefix %>context_t * context, <%=
|
|||||||
}
|
}
|
||||||
token_info.token = token_to_accept;
|
token_info.token = token_to_accept;
|
||||||
token_info.length = match_info.length;
|
token_info.length = match_info.length;
|
||||||
|
if (match_info.end_delta_position.row != 0u)
|
||||||
|
{
|
||||||
|
token_info.end_position.row = token_info.position.row + match_info.end_delta_position.row;
|
||||||
|
token_info.end_position.col = match_info.end_delta_position.col;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
token_info.end_position.row = token_info.position.row;
|
||||||
|
token_info.end_position.col = token_info.position.col + match_info.end_delta_position.col;
|
||||||
|
}
|
||||||
*out_token_info = token_info;
|
*out_token_info = token_info;
|
||||||
return P_SUCCESS;
|
return P_SUCCESS;
|
||||||
|
|
||||||
case P_EOF:
|
case P_EOF:
|
||||||
token_info.token = TOKEN___EOF;
|
token_info.token = TOKEN___EOF;
|
||||||
|
token_info.end_position = token_info.position;
|
||||||
*out_token_info = token_info;
|
*out_token_info = token_info;
|
||||||
return P_SUCCESS;
|
return P_SUCCESS;
|
||||||
|
|
||||||
@ -763,6 +805,13 @@ private struct reduce_t
|
|||||||
* Number of rule set AST node fields.
|
* Number of rule set AST node fields.
|
||||||
*/
|
*/
|
||||||
ushort rule_set_node_field_array_size;
|
ushort rule_set_node_field_array_size;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether this rule was a generated optional rule that matched the
|
||||||
|
* optional target. In this case, propagate the matched target node up
|
||||||
|
* instead of making a new node for this rule.
|
||||||
|
*/
|
||||||
|
bool propagate_optional_target;
|
||||||
<% end %>
|
<% end %>
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -809,7 +858,7 @@ private struct state_value_t
|
|||||||
/** Parser shift table. */
|
/** Parser shift table. */
|
||||||
private immutable shift_t[] parser_shift_table = [
|
private immutable shift_t[] parser_shift_table = [
|
||||||
<% @parser.shift_table.each do |shift| %>
|
<% @parser.shift_table.each do |shift| %>
|
||||||
shift_t(<%= shift[:symbol_id] %>u, <%= shift[:state_id] %>u),
|
shift_t(<%= shift[:symbol].id %>u, <%= shift[:state_id] %>u),
|
||||||
<% end %>
|
<% end %>
|
||||||
];
|
];
|
||||||
|
|
||||||
@ -832,6 +881,7 @@ private immutable reduce_t[] parser_reduce_table = [
|
|||||||
, &r_<%= reduce[:rule].name.gsub("$", "_") %><%= reduce[:rule].id %>_node_field_index_map[0]
|
, &r_<%= reduce[:rule].name.gsub("$", "_") %><%= reduce[:rule].id %>_node_field_index_map[0]
|
||||||
<% end %>
|
<% end %>
|
||||||
, <%= reduce[:rule].rule_set.ast_fields.size %>
|
, <%= reduce[:rule].rule_set.ast_fields.size %>
|
||||||
|
, <%= reduce[:propagate_optional_target] %>
|
||||||
<% end %>
|
<% end %>
|
||||||
),
|
),
|
||||||
<% end %>
|
<% end %>
|
||||||
@ -973,7 +1023,7 @@ public size_t <%= @grammar.prefix %>parse(<%= @grammar.prefix %>context_t * cont
|
|||||||
{
|
{
|
||||||
/* Successful parse. */
|
/* Successful parse. */
|
||||||
<% if @grammar.ast %>
|
<% if @grammar.ast %>
|
||||||
context.parse_result = cast(Start *)statevalues[$-1].ast_node;
|
context.parse_result = cast(<%= @grammar.ast_prefix %><%= @grammar.start_rule %><%= @grammar.ast_suffix %> *)statevalues[$-1].ast_node;
|
||||||
<% else %>
|
<% else %>
|
||||||
context.parse_result = statevalues[$-1].pvalue;
|
context.parse_result = statevalues[$-1].pvalue;
|
||||||
<% end %>
|
<% end %>
|
||||||
@ -988,7 +1038,7 @@ public size_t <%= @grammar.prefix %>parse(<%= @grammar.prefix %>context_t * cont
|
|||||||
{
|
{
|
||||||
/* We shifted a token, mark it consumed. */
|
/* We shifted a token, mark it consumed. */
|
||||||
<% if @grammar.ast %>
|
<% if @grammar.ast %>
|
||||||
Token * token_ast_node = new Token(token, token_info.pvalue);
|
<%= @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;
|
statevalues[$-1].ast_node = token_ast_node;
|
||||||
<% else %>
|
<% else %>
|
||||||
statevalues[$-1].pvalue = token_info.pvalue;
|
statevalues[$-1].pvalue = token_info.pvalue;
|
||||||
@ -1015,28 +1065,49 @@ public size_t <%= @grammar.prefix %>parse(<%= @grammar.prefix %>context_t * cont
|
|||||||
{
|
{
|
||||||
/* We have something to reduce. */
|
/* We have something to reduce. */
|
||||||
<% if @grammar.ast %>
|
<% if @grammar.ast %>
|
||||||
if (parser_reduce_table[reduce_index].n_states > 0)
|
if (parser_reduce_table[reduce_index].propagate_optional_target)
|
||||||
{
|
{
|
||||||
void *[] node_fields = new void *[parser_reduce_table[reduce_index].rule_set_node_field_array_size];
|
reduced_parser_node = statevalues[$ - 1].ast_node;
|
||||||
foreach (i; 0..parser_reduce_table[reduce_index].rule_set_node_field_array_size)
|
}
|
||||||
|
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 = cast(ASTNode *)malloc(ASTNode.sizeof + n_fields * (void *).sizeof);
|
||||||
|
node.position = <%= @grammar.prefix %>position_t.INVALID;
|
||||||
|
node.end_position = <%= @grammar.prefix %>position_t.INVALID;
|
||||||
|
foreach (i; 0..n_fields)
|
||||||
{
|
{
|
||||||
node_fields[i] = null;
|
node.fields[i] = null;
|
||||||
}
|
}
|
||||||
if (parser_reduce_table[reduce_index].rule_set_node_field_index_map is null)
|
if (parser_reduce_table[reduce_index].rule_set_node_field_index_map is null)
|
||||||
{
|
{
|
||||||
foreach (i; 0..parser_reduce_table[reduce_index].n_states)
|
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].ast_node;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
foreach (i; 0..parser_reduce_table[reduce_index].n_states)
|
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].ast_node;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
reduced_parser_node = node_fields.ptr;
|
bool position_found = false;
|
||||||
|
foreach (i; 0..n_fields)
|
||||||
|
{
|
||||||
|
ASTNode * child = cast(ASTNode *)node.fields[i];
|
||||||
|
if (child && child.position.valid)
|
||||||
|
{
|
||||||
|
if (!position_found)
|
||||||
|
{
|
||||||
|
node.position = child.position;
|
||||||
|
position_found = true;
|
||||||
|
}
|
||||||
|
node.end_position = child.end_position;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
reduced_parser_node = node;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -1075,7 +1146,7 @@ public size_t <%= @grammar.prefix %>parse(<%= @grammar.prefix %>context_t * cont
|
|||||||
* @return Parse result value.
|
* @return Parse result value.
|
||||||
*/
|
*/
|
||||||
<% if @grammar.ast %>
|
<% if @grammar.ast %>
|
||||||
public Start * <%= @grammar.prefix %>result(<%= @grammar.prefix %>context_t * context)
|
public <%= @grammar.ast_prefix %><%= @grammar.start_rule %><%= @grammar.ast_suffix %> * <%= @grammar.prefix %>result(<%= @grammar.prefix %>context_t * context)
|
||||||
<% else %>
|
<% else %>
|
||||||
public <%= start_rule_type[1] %> <%= @grammar.prefix %>result(<%= @grammar.prefix %>context_t * context)
|
public <%= start_rule_type[1] %> <%= @grammar.prefix %>result(<%= @grammar.prefix %>context_t * context)
|
||||||
<% end %>
|
<% end %>
|
||||||
|
@ -38,6 +38,23 @@ typedef <%= get_type_for(@grammar.terminate_token_id) %> <%= @grammar.prefix %>t
|
|||||||
/** Code point type. */
|
/** Code point type. */
|
||||||
typedef uint32_t <%= @grammar.prefix %>code_point_t;
|
typedef uint32_t <%= @grammar.prefix %>code_point_t;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A structure to keep track of input position.
|
||||||
|
*
|
||||||
|
* This is useful for reporting errors, etc...
|
||||||
|
*/
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
/** Input text row (0-based). */
|
||||||
|
uint32_t row;
|
||||||
|
|
||||||
|
/** Input text column (0-based). */
|
||||||
|
uint32_t col;
|
||||||
|
} <%= @grammar.prefix %>position_t;
|
||||||
|
|
||||||
|
/** Return whether the position is valid. */
|
||||||
|
#define <%= @grammar.prefix %>position_valid(p) ((p).row != 0xFFFFFFFFu)
|
||||||
|
|
||||||
/** User header code blocks. */
|
/** User header code blocks. */
|
||||||
<%= @grammar.code_blocks.fetch("header", "") %>
|
<%= @grammar.code_blocks.fetch("header", "") %>
|
||||||
|
|
||||||
@ -56,21 +73,28 @@ typedef union
|
|||||||
|
|
||||||
<% if @grammar.ast %>
|
<% if @grammar.ast %>
|
||||||
/** AST node types. @{ */
|
/** AST node types. @{ */
|
||||||
typedef struct Token
|
typedef struct <%= @grammar.ast_prefix %>Token<%= @grammar.ast_suffix %>
|
||||||
{
|
{
|
||||||
|
/* ASTNode 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;
|
<%= @grammar.prefix %>token_t token;
|
||||||
<%= @grammar.prefix %>value_t pvalue;
|
<%= @grammar.prefix %>value_t pvalue;
|
||||||
} Token;
|
} <%= @grammar.ast_prefix %>Token<%= @grammar.ast_suffix %>;
|
||||||
|
|
||||||
<% @parser.rule_sets.each do |name, rule_set| %>
|
<% @parser.rule_sets.each do |name, rule_set| %>
|
||||||
<% next if name.start_with?("$") %>
|
<% next if name.start_with?("$") %>
|
||||||
|
<% next if rule_set.optional? %>
|
||||||
struct <%= name %>;
|
struct <%= name %>;
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
||||||
<% @parser.rule_sets.each do |name, rule_set| %>
|
<% @parser.rule_sets.each do |name, rule_set| %>
|
||||||
<% next if name.start_with?("$") %>
|
<% next if name.start_with?("$") %>
|
||||||
typedef struct <%= name %>
|
<% next if rule_set.optional? %>
|
||||||
|
typedef struct <%= @grammar.ast_prefix %><%= name %><%= @grammar.ast_suffix %>
|
||||||
{
|
{
|
||||||
|
<%= @grammar.prefix %>position_t position;
|
||||||
|
<%= @grammar.prefix %>position_t end_position;
|
||||||
<% rule_set.ast_fields.each do |fields| %>
|
<% rule_set.ast_fields.each do |fields| %>
|
||||||
union
|
union
|
||||||
{
|
{
|
||||||
@ -79,32 +103,21 @@ typedef struct <%= name %>
|
|||||||
<% end %>
|
<% end %>
|
||||||
};
|
};
|
||||||
<% end %>
|
<% end %>
|
||||||
} <%= name %>;
|
} <%= @grammar.ast_prefix %><%= name %><%= @grammar.ast_suffix %>;
|
||||||
|
|
||||||
<% end %>
|
<% end %>
|
||||||
/** @} */
|
/** @} */
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
||||||
/**
|
|
||||||
* A structure to keep track of parser position.
|
|
||||||
*
|
|
||||||
* This is useful for reporting errors, etc...
|
|
||||||
*/
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
/** Input text row (0-based). */
|
|
||||||
uint32_t row;
|
|
||||||
|
|
||||||
/** Input text column (0-based). */
|
|
||||||
uint32_t col;
|
|
||||||
} <%= @grammar.prefix %>position_t;
|
|
||||||
|
|
||||||
/** Lexed token information. */
|
/** Lexed token information. */
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
/** Text position where the token was found. */
|
/** Text position of first code point in token. */
|
||||||
<%= @grammar.prefix %>position_t position;
|
<%= @grammar.prefix %>position_t position;
|
||||||
|
|
||||||
|
/** Text position of last code point in token. */
|
||||||
|
<%= @grammar.prefix %>position_t end_position;
|
||||||
|
|
||||||
/** Number of input bytes used by the token. */
|
/** Number of input bytes used by the token. */
|
||||||
size_t length;
|
size_t length;
|
||||||
|
|
||||||
@ -144,7 +157,7 @@ typedef struct
|
|||||||
|
|
||||||
/** Parse result value. */
|
/** Parse result value. */
|
||||||
<% if @grammar.ast %>
|
<% if @grammar.ast %>
|
||||||
Start * parse_result;
|
<%= @grammar.ast_prefix %><%= @grammar.start_rule %><%= @grammar.ast_suffix %> * parse_result;
|
||||||
<% else %>
|
<% else %>
|
||||||
<%= @grammar.prefix %>value_t parse_result;
|
<%= @grammar.prefix %>value_t parse_result;
|
||||||
<% end %>
|
<% end %>
|
||||||
@ -173,7 +186,7 @@ size_t <%= @grammar.prefix %>lex(<%= @grammar.prefix %>context_t * context, <%=
|
|||||||
size_t <%= @grammar.prefix %>parse(<%= @grammar.prefix %>context_t * context);
|
size_t <%= @grammar.prefix %>parse(<%= @grammar.prefix %>context_t * context);
|
||||||
|
|
||||||
<% if @grammar.ast %>
|
<% if @grammar.ast %>
|
||||||
Start * <%= @grammar.prefix %>result(<%= @grammar.prefix %>context_t * context);
|
<%= @grammar.ast_prefix %><%= @grammar.start_rule %><%= @grammar.ast_suffix %> * <%= @grammar.prefix %>result(<%= @grammar.prefix %>context_t * context);
|
||||||
<% else %>
|
<% else %>
|
||||||
<%= start_rule_type[1] %> <%= @grammar.prefix %>result(<%= @grammar.prefix %>context_t * context);
|
<%= start_rule_type[1] %> <%= @grammar.prefix %>result(<%= @grammar.prefix %>context_t * context);
|
||||||
<% end %>
|
<% end %>
|
||||||
|
@ -15,6 +15,7 @@ Propane is a LALR Parser Generator (LPG) which:
|
|||||||
* generates a table-driven shift/reduce parser to parse input in linear time
|
* generates a table-driven shift/reduce parser to parse input in linear time
|
||||||
* targets C or D language outputs
|
* targets C or D language outputs
|
||||||
* optionally supports automatic full AST generation
|
* optionally supports automatic full AST generation
|
||||||
|
* tracks input text start and end positions for all matched tokens/rules
|
||||||
* is MIT-licensed
|
* is MIT-licensed
|
||||||
* is distributable as a standalone Ruby script
|
* is distributable as a standalone Ruby script
|
||||||
|
|
||||||
@ -35,9 +36,14 @@ Propane is typically invoked from the command-line as `./propane`.
|
|||||||
|
|
||||||
Usage: ./propane [options] <input-file> <output-file>
|
Usage: ./propane [options] <input-file> <output-file>
|
||||||
Options:
|
Options:
|
||||||
--log LOG Write log file
|
-h, --help Show this usage and exit.
|
||||||
--version Show program version and exit
|
--log LOG Write log file. This will show all parser states and their
|
||||||
-h, --help Show this usage and exit
|
associated shifts and reduces. It can be helpful when
|
||||||
|
debugging a grammar.
|
||||||
|
--version Show program version and exit.
|
||||||
|
-w Treat warnings as errors. This option will treat shift/reduce
|
||||||
|
conflicts as fatal errors and will print them to stderr in
|
||||||
|
addition to the log file.
|
||||||
|
|
||||||
The user must specify the path to a Propane input grammar file and a path to an
|
The user must specify the path to a Propane input grammar file and a path to an
|
||||||
output file.
|
output file.
|
||||||
@ -203,8 +209,10 @@ In AST generation mode various aspects of propane's behavior are changed:
|
|||||||
* Parser user code blocks are not supported.
|
* Parser user code blocks are not supported.
|
||||||
* Structure types are generated to represent the parsed tokens and rules as
|
* Structure types are generated to represent the parsed tokens and rules as
|
||||||
defined in the grammar.
|
defined in the grammar.
|
||||||
* The parse result from `p_result()` points to a `Start` structure containing
|
* The parse result from `p_result()` points to a `Start` struct containing
|
||||||
the entire parse tree for the input.
|
the entire parse tree for the input. If the user has changed the start rule
|
||||||
|
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 AST generation grammar:
|
||||||
|
|
||||||
@ -226,15 +234,15 @@ drop /\\s+/;
|
|||||||
|
|
||||||
Start -> Items;
|
Start -> Items;
|
||||||
|
|
||||||
Items -> Item ItemsMore;
|
Items -> Item:item ItemsMore;
|
||||||
Items -> ;
|
Items -> ;
|
||||||
|
|
||||||
ItemsMore -> comma Item ItemsMore;
|
ItemsMore -> comma Item:item ItemsMore;
|
||||||
ItemsMore -> ;
|
ItemsMore -> ;
|
||||||
|
|
||||||
Item -> a;
|
Item -> a;
|
||||||
Item -> b;
|
Item -> b;
|
||||||
Item -> lparen Item rparen;
|
Item -> lparen Item:item rparen;
|
||||||
Item -> Dual;
|
Item -> Dual;
|
||||||
|
|
||||||
Dual -> One Two;
|
Dual -> One Two;
|
||||||
@ -255,25 +263,67 @@ Start * start = p_result(&context);
|
|||||||
assert(start.pItems1 !is null);
|
assert(start.pItems1 !is null);
|
||||||
assert(start.pItems !is null);
|
assert(start.pItems !is null);
|
||||||
Items * items = start.pItems;
|
Items * items = start.pItems;
|
||||||
|
assert(items.item !is null);
|
||||||
|
assert(items.item.pToken1 !is null);
|
||||||
|
assert_eq(TOKEN_a, items.item.pToken1.token);
|
||||||
|
assert_eq(11, items.item.pToken1.pvalue);
|
||||||
|
assert(items.pItemsMore !is null);
|
||||||
|
ItemsMore * itemsmore = items.pItemsMore;
|
||||||
|
assert(itemsmore.item !is null);
|
||||||
|
assert(itemsmore.item.item !is null);
|
||||||
|
assert(itemsmore.item.item.item !is null);
|
||||||
|
assert(itemsmore.item.item.item.pToken1 !is null);
|
||||||
|
assert_eq(TOKEN_b, itemsmore.item.item.item.pToken1.token);
|
||||||
|
assert_eq(22, itemsmore.item.item.item.pToken1.pvalue);
|
||||||
|
assert(itemsmore.pItemsMore !is null);
|
||||||
|
itemsmore = itemsmore.pItemsMore;
|
||||||
|
assert(itemsmore.item !is null);
|
||||||
|
assert(itemsmore.item.pToken1 !is null);
|
||||||
|
assert_eq(TOKEN_b, itemsmore.item.pToken1.token);
|
||||||
|
assert_eq(22, itemsmore.item.pToken1.pvalue);
|
||||||
|
assert(itemsmore.pItemsMore is null);
|
||||||
|
```
|
||||||
|
|
||||||
|
## `ast_prefix` and `ast_suffix` statements
|
||||||
|
|
||||||
|
In AST 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`
|
||||||
|
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.
|
||||||
|
|
||||||
|
For example, if the following two lines were added to the example above:
|
||||||
|
|
||||||
|
```
|
||||||
|
ast_prefix ABC;
|
||||||
|
ast_suffix XYZ;
|
||||||
|
```
|
||||||
|
|
||||||
|
Then the types would be used as such instead:
|
||||||
|
|
||||||
|
```
|
||||||
|
string input = "a, ((b)), b";
|
||||||
|
p_context_t context;
|
||||||
|
p_context_init(&context, input);
|
||||||
|
assert_eq(P_SUCCESS, p_parse(&context));
|
||||||
|
ABCStartXYZ * start = p_result(&context);
|
||||||
|
assert(start.pItems1 !is null);
|
||||||
|
assert(start.pItems !is null);
|
||||||
|
ABCItemsXYZ * items = start.pItems;
|
||||||
assert(items.pItem !is null);
|
assert(items.pItem !is null);
|
||||||
assert(items.pItem.pToken1 !is null);
|
assert(items.pItem.pToken1 !is null);
|
||||||
assert_eq(TOKEN_a, items.pItem.pToken1.token);
|
assert_eq(TOKEN_a, items.pItem.pToken1.token);
|
||||||
assert_eq(11, items.pItem.pToken1.pvalue);
|
assert_eq(11, items.pItem.pToken1.pvalue);
|
||||||
assert(items.pItemsMore !is null);
|
assert(items.pItemsMore !is null);
|
||||||
ItemsMore * itemsmore = items.pItemsMore;
|
ABCItemsMoreXYZ * itemsmore = items.pItemsMore;
|
||||||
assert(itemsmore.pItem !is null);
|
assert(itemsmore.pItem !is null);
|
||||||
assert(itemsmore.pItem.pItem !is null);
|
assert(itemsmore.pItem.pItem !is null);
|
||||||
assert(itemsmore.pItem.pItem.pItem !is null);
|
assert(itemsmore.pItem.pItem.pItem !is null);
|
||||||
assert(itemsmore.pItem.pItem.pItem.pToken1 !is null);
|
assert(itemsmore.pItem.pItem.pItem.pToken1 !is null);
|
||||||
assert_eq(TOKEN_b, itemsmore.pItem.pItem.pItem.pToken1.token);
|
|
||||||
assert_eq(22, itemsmore.pItem.pItem.pItem.pToken1.pvalue);
|
|
||||||
assert(itemsmore.pItemsMore !is null);
|
|
||||||
itemsmore = itemsmore.pItemsMore;
|
|
||||||
assert(itemsmore.pItem !is null);
|
|
||||||
assert(itemsmore.pItem.pToken1 !is null);
|
|
||||||
assert_eq(TOKEN_b, itemsmore.pItem.pToken1.token);
|
|
||||||
assert_eq(22, itemsmore.pItem.pToken1.pvalue);
|
|
||||||
assert(itemsmore.pItemsMore is null);
|
|
||||||
```
|
```
|
||||||
|
|
||||||
##> Specifying tokens - the `token` statement
|
##> Specifying tokens - the `token` statement
|
||||||
@ -458,7 +508,7 @@ tokenid str;
|
|||||||
mystringvalue = "";
|
mystringvalue = "";
|
||||||
$mode(string);
|
$mode(string);
|
||||||
>>
|
>>
|
||||||
string: /[^"]+/ << mystringvalue += match; >>
|
string: /[^"]+/ << mystringvalue ~= match; >>
|
||||||
string: /"/ <<
|
string: /"/ <<
|
||||||
$mode(default);
|
$mode(default);
|
||||||
return $token(str);
|
return $token(str);
|
||||||
@ -552,21 +602,28 @@ Rules with the same name define a rule set for that name and act as
|
|||||||
alternatives that the parser can accept when attempting to match a reference to
|
alternatives that the parser can accept when attempting to match a reference to
|
||||||
that rule.
|
that rule.
|
||||||
|
|
||||||
The grammar file must define a rule with the name `Start` which will be used as
|
The default start rule name is `Start`.
|
||||||
the top-level starting rule that the parser attempts to reduce.
|
This can be changed with the `start` statement.
|
||||||
|
The grammar file must define a rule with the name of the start rule name which
|
||||||
|
will be used as the top-level starting rule that the parser attempts to reduce.
|
||||||
|
|
||||||
|
Rule statements are composed of the name of the rule, a `->` token, the fields
|
||||||
|
defining the rule pattern that must be matched, and a terminating semicolon or
|
||||||
|
user code block.
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
```
|
```
|
||||||
ptype ulong;
|
ptype ulong;
|
||||||
|
start Top;
|
||||||
token word /[a-z]+/ << $$ = match.length; >>
|
token word /[a-z]+/ << $$ = match.length; >>
|
||||||
Start -> word << $$ = $1; >>
|
Top -> word << $$ = $1; >>
|
||||||
```
|
```
|
||||||
|
|
||||||
In the above example the `Start` rule is defined to match a single `word`
|
In the above example the `Top` rule is defined to match a single `word`
|
||||||
token.
|
token.
|
||||||
|
|
||||||
Example:
|
Another example:
|
||||||
|
|
||||||
```
|
```
|
||||||
Start -> E1 << $$ = $1; >>
|
Start -> E1 << $$ = $1; >>
|
||||||
@ -580,12 +637,34 @@ E4 -> integer << $$ = $1; >>
|
|||||||
E4 -> lparen E1 rparen << $$ = $2; >>
|
E4 -> lparen E1 rparen << $$ = $2; >>
|
||||||
```
|
```
|
||||||
|
|
||||||
A parser rule has zero or more terms on the right side of its definition.
|
This example uses the default start rule name of `Start`.
|
||||||
Each of these terms is either a token name or a rule name.
|
|
||||||
|
|
||||||
In a parser rule code block, parser values for the right side terms are
|
A parser rule has zero or more fields on the right side of its definition.
|
||||||
accessible as `$1` for the first term's parser value, `$2` for the second
|
Each of these fields is either a token name or a rule name.
|
||||||
term's parser value, etc...
|
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.
|
||||||
|
A field can be immediately followed by a `?` character to signify that it is
|
||||||
|
optional.
|
||||||
|
Another example:
|
||||||
|
|
||||||
|
```
|
||||||
|
token public;
|
||||||
|
token private;
|
||||||
|
token int;
|
||||||
|
token ident /[a-zA-Z_][a-zA-Z_0-9]*/;
|
||||||
|
token semicolon /;/;
|
||||||
|
IntegerDeclaration -> Visibility? int ident:name semicolon;
|
||||||
|
Visibility -> public;
|
||||||
|
Visibility -> private;
|
||||||
|
```
|
||||||
|
|
||||||
|
In a parser rule code block, parser values for the right side fields are
|
||||||
|
accessible as `$1` for the first field's parser value, `$2` for the second
|
||||||
|
field's parser value, etc...
|
||||||
|
For the `IntegerDeclaration` rule, the third field value can also be referred
|
||||||
|
to as `${name}`.
|
||||||
The `$$` symbol accesses the output parser value for this rule.
|
The `$$` symbol accesses the output parser value for this rule.
|
||||||
The above examples demonstrate how the parser values for the rule components
|
The above examples demonstrate how the parser values for the rule components
|
||||||
can be used to produce the parser value for the accepted rule.
|
can be used to produce the parser value for the accepted rule.
|
||||||
@ -593,6 +672,16 @@ 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 AST generation mode
|
||||||
is active.
|
is active.
|
||||||
|
|
||||||
|
##> Specifying the parser start rule name - the `start` statement
|
||||||
|
|
||||||
|
The start rule can be changed from the default of `Start` by using the `start`
|
||||||
|
statement.
|
||||||
|
Example:
|
||||||
|
|
||||||
|
```
|
||||||
|
start MyStartRule;
|
||||||
|
```
|
||||||
|
|
||||||
##> Specifying the parser module name - the `module` statement
|
##> Specifying the parser module name - the `module` statement
|
||||||
|
|
||||||
The `module` statement can be used to specify the module name for a generated
|
The `module` statement can be used to specify the module name for a generated
|
||||||
@ -689,6 +778,13 @@ A pointer to this instance is passed to the generated functions.
|
|||||||
The `p_position_t` structure contains two fields `row` and `col`.
|
The `p_position_t` structure contains two fields `row` and `col`.
|
||||||
These fields contain the 0-based row and column describing a parser position.
|
These fields contain the 0-based row and column describing a parser position.
|
||||||
|
|
||||||
|
For D targets, the `p_position_t` structure can be checked for validity by
|
||||||
|
querying the `valid` property.
|
||||||
|
|
||||||
|
For C targets, the `p_position_t` structure can be checked for validity by
|
||||||
|
calling `p_position_valid(pos)` where `pos` is a `p_position_t` structure
|
||||||
|
instance.
|
||||||
|
|
||||||
### AST Node Types
|
### AST Node Types
|
||||||
|
|
||||||
If AST generation mode is enabled, a structure type for each rule will be
|
If AST generation mode is enabled, a structure type for each rule will be
|
||||||
@ -699,13 +795,26 @@ AST node which refers to a raw parser token rather than a composite rule.
|
|||||||
|
|
||||||
#### AST Node Fields
|
#### AST Node Fields
|
||||||
|
|
||||||
A `Token` node has two fields:
|
All AST 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.
|
||||||
|
|
||||||
|
A `Token` node will always have a valid `position` and `end_position`.
|
||||||
|
A rule node may not have valid positions if the rule allows for an empty match.
|
||||||
|
In this case the `position` structure should be checked for validity before
|
||||||
|
using it.
|
||||||
|
For C targets this can be accomplished with
|
||||||
|
`if (p_position_valid(node->position))` and for D targets this can be
|
||||||
|
accomplished with `if (node.position.valid)`.
|
||||||
|
|
||||||
|
A `Token` node has the following additional fields:
|
||||||
|
|
||||||
* `token` which specifies which token was parsed (one of `TOKEN_*`)
|
* `token` which specifies which token was parsed (one of `TOKEN_*`)
|
||||||
* `pvalue` which specifies the parser value for the token. If a lexer user
|
* `pvalue` which specifies the parser value for the token. If a lexer user
|
||||||
code block assigned to `$$`, the assigned value will be stored here.
|
code block assigned to `$$`, the assigned value will be stored here.
|
||||||
|
|
||||||
The other generated AST node structures have fields generated based on the
|
AST node structures for rules contain generated fields based on the
|
||||||
right hand side components specified for all rules of a given name.
|
right hand side components specified for all rules of a given name.
|
||||||
|
|
||||||
In this example:
|
In this example:
|
||||||
@ -729,7 +838,7 @@ The `Items` structure will have fields:
|
|||||||
|
|
||||||
If a rule can be empty (for example in the second `Items` rule above), then
|
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 AST node will be null if the
|
||||||
parser matches the empty rule definition.
|
parser matches the empty rule pattern.
|
||||||
|
|
||||||
The non-positional AST node field pointer will not be generated if there are
|
The non-positional AST node field pointer will not be generated if there are
|
||||||
multiple positions in which an instance of the node it points to could be
|
multiple positions in which an instance of the node it points to could be
|
||||||
@ -750,6 +859,19 @@ If the first rule is matched, then `pOne1` and `pTwo2` will be non-null while
|
|||||||
`pTwo1` and `pOne2` will be null.
|
`pTwo1` and `pOne2` will be null.
|
||||||
If the second rule is matched instead, then the opposite would be the case.
|
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.
|
||||||
|
For example:
|
||||||
|
|
||||||
|
```
|
||||||
|
Exp -> Exp:left plus ExpB:right;
|
||||||
|
```
|
||||||
|
|
||||||
|
In the generated `Exp` structure, the fields `pExp`, `pExp1`, and `left` will
|
||||||
|
all point to the same child node (an instance of the `Exp` structure), and the
|
||||||
|
fields `pExpB`, `pExpB3`, and `right` will all point to the same child node
|
||||||
|
(an instance of the `ExpB` structure).
|
||||||
|
|
||||||
##> Functions
|
##> Functions
|
||||||
|
|
||||||
### `p_context_init`
|
### `p_context_init`
|
||||||
@ -786,6 +908,24 @@ p_context_init(&context, input, input_length);
|
|||||||
size_t result = p_parse(&context);
|
size_t result = p_parse(&context);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### `p_position_valid`
|
||||||
|
|
||||||
|
The `p_position_valid()` function is only generated for C targets.
|
||||||
|
it is used to determine whether or not a `p_position_t` structure is valid.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
```
|
||||||
|
if (p_position_valid(node->position))
|
||||||
|
{
|
||||||
|
....
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
For D targets, rather than using `p_position_valid()`, the `valid` property
|
||||||
|
function of the `p_position_t` structure can be queried
|
||||||
|
(e.g. `if (node.position.valid)`).
|
||||||
|
|
||||||
### `p_result`
|
### `p_result`
|
||||||
|
|
||||||
The `p_result()` function can be used to retrieve the final parse value after
|
The `p_result()` function can be used to retrieve the final parse value after
|
||||||
|
1
extra/vim/ftdetect/propane.vim
Normal file
1
extra/vim/ftdetect/propane.vim
Normal file
@ -0,0 +1 @@
|
|||||||
|
au BufNewFile,BufRead *.propane set filetype=propane
|
33
extra/vim/syntax/propane.vim
Normal file
33
extra/vim/syntax/propane.vim
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
" Vim syntax file for Propane
|
||||||
|
" Language: propane
|
||||||
|
" Maintainer: Josh Holtrop
|
||||||
|
" URL: https://github.com/holtrop/propane
|
||||||
|
|
||||||
|
if exists("b:current_syntax")
|
||||||
|
finish
|
||||||
|
endif
|
||||||
|
|
||||||
|
if !exists("b:propane_subtype")
|
||||||
|
let b:propane_subtype = "d"
|
||||||
|
endif
|
||||||
|
|
||||||
|
exe "syn include @propaneTarget syntax/".b:propane_subtype.".vim"
|
||||||
|
|
||||||
|
syn region propaneTarget matchgroup=propaneDelimiter start="<<" end=">>$" contains=@propaneTarget keepend
|
||||||
|
|
||||||
|
syn match propaneComment "#.*"
|
||||||
|
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 region propaneRegex start="/" end="/" skip="\\/"
|
||||||
|
|
||||||
|
hi def link propaneComment Comment
|
||||||
|
hi def link propaneKeyword Keyword
|
||||||
|
hi def link propaneRegex String
|
||||||
|
hi def link propaneOperator Operator
|
||||||
|
hi def link propaneFieldOperator Operator
|
||||||
|
hi def link propaneDelimiter Delimiter
|
||||||
|
hi def link propaneFieldAlias Identifier
|
@ -31,10 +31,10 @@ class Propane
|
|||||||
|
|
||||||
class << self
|
class << self
|
||||||
|
|
||||||
def run(input_file, output_file, log_file)
|
def run(input_file, output_file, log_file, options)
|
||||||
begin
|
begin
|
||||||
grammar = Grammar.new(File.read(input_file))
|
grammar = Grammar.new(File.read(input_file))
|
||||||
generator = Generator.new(grammar, output_file, log_file)
|
generator = Generator.new(grammar, output_file, log_file, options)
|
||||||
generator.generate
|
generator.generate
|
||||||
rescue Error => e
|
rescue Error => e
|
||||||
$stderr.puts e.message
|
$stderr.puts e.message
|
||||||
|
@ -4,15 +4,21 @@ class Propane
|
|||||||
USAGE = <<EOF
|
USAGE = <<EOF
|
||||||
Usage: #{$0} [options] <input-file> <output-file>
|
Usage: #{$0} [options] <input-file> <output-file>
|
||||||
Options:
|
Options:
|
||||||
--log LOG Write log file
|
-h, --help Show this usage and exit.
|
||||||
--version Show program version and exit
|
--log LOG Write log file. This will show all parser states and their
|
||||||
-h, --help Show this usage and exit
|
associated shifts and reduces. It can be helpful when
|
||||||
|
debugging a grammar.
|
||||||
|
--version Show program version and exit.
|
||||||
|
-w Treat warnings as errors. This option will treat shift/reduce
|
||||||
|
conflicts as fatal errors and will print them to stderr in
|
||||||
|
addition to the log file.
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
|
|
||||||
def run(args)
|
def run(args)
|
||||||
params = []
|
params = []
|
||||||
|
options = {}
|
||||||
log_file = nil
|
log_file = nil
|
||||||
i = 0
|
i = 0
|
||||||
while i < args.size
|
while i < args.size
|
||||||
@ -29,6 +35,8 @@ EOF
|
|||||||
when "-h", "--help"
|
when "-h", "--help"
|
||||||
puts USAGE
|
puts USAGE
|
||||||
return 0
|
return 0
|
||||||
|
when "-w"
|
||||||
|
options[:warnings_as_errors] = true
|
||||||
when /^-/
|
when /^-/
|
||||||
$stderr.puts "Error: unknown option #{arg}"
|
$stderr.puts "Error: unknown option #{arg}"
|
||||||
return 1
|
return 1
|
||||||
@ -45,7 +53,7 @@ EOF
|
|||||||
$stderr.puts "Error: cannot read #{params[0]}"
|
$stderr.puts "Error: cannot read #{params[0]}"
|
||||||
return 2
|
return 2
|
||||||
end
|
end
|
||||||
Propane.run(*params, log_file)
|
Propane.run(*params, log_file, options)
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
@ -2,7 +2,7 @@ class Propane
|
|||||||
|
|
||||||
class Generator
|
class Generator
|
||||||
|
|
||||||
def initialize(grammar, output_file, log_file)
|
def initialize(grammar, output_file, log_file, options)
|
||||||
@grammar = grammar
|
@grammar = grammar
|
||||||
@output_file = output_file
|
@output_file = output_file
|
||||||
if log_file
|
if log_file
|
||||||
@ -16,6 +16,7 @@ class Propane
|
|||||||
else
|
else
|
||||||
"d"
|
"d"
|
||||||
end
|
end
|
||||||
|
@options = options
|
||||||
process_grammar!
|
process_grammar!
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -51,6 +52,7 @@ class Propane
|
|||||||
unless found_default
|
unless found_default
|
||||||
raise Error.new("No patterns found for default mode")
|
raise Error.new("No patterns found for default mode")
|
||||||
end
|
end
|
||||||
|
check_ptypes!
|
||||||
# Add EOF token.
|
# Add EOF token.
|
||||||
@grammar.tokens << Token.new("$EOF", nil, nil)
|
@grammar.tokens << Token.new("$EOF", nil, nil)
|
||||||
tokens_by_name = {}
|
tokens_by_name = {}
|
||||||
@ -66,11 +68,14 @@ class Propane
|
|||||||
tokens_by_name[token.name] = token
|
tokens_by_name[token.name] = token
|
||||||
end
|
end
|
||||||
# Check for user start rule.
|
# Check for user start rule.
|
||||||
unless @grammar.rules.find {|rule| rule.name == "Start"}
|
unless @grammar.rules.find {|rule| rule.name == @grammar.start_rule}
|
||||||
raise Error.new("Start rule not found")
|
raise Error.new("Start rule `#{@grammar.start_rule}` not found")
|
||||||
end
|
end
|
||||||
# Add "real" start rule.
|
# Add "real" start rule.
|
||||||
@grammar.rules.unshift(Rule.new("$Start", ["Start", "$EOF"], nil, nil, nil))
|
@grammar.rules.unshift(Rule.new("$Start", [@grammar.start_rule, "$EOF"], nil, nil, nil))
|
||||||
|
# Generate and add rules for optional components.
|
||||||
|
generate_optional_component_rules!(tokens_by_name)
|
||||||
|
# Build rule sets.
|
||||||
rule_sets = {}
|
rule_sets = {}
|
||||||
rule_set_id = @grammar.tokens.size
|
rule_set_id = @grammar.tokens.size
|
||||||
@grammar.rules.each_with_index do |rule, rule_id|
|
@grammar.rules.each_with_index do |rule, rule_id|
|
||||||
@ -120,12 +125,54 @@ class Propane
|
|||||||
end
|
end
|
||||||
determine_possibly_empty_rulesets!(rule_sets)
|
determine_possibly_empty_rulesets!(rule_sets)
|
||||||
rule_sets.each do |name, rule_set|
|
rule_sets.each do |name, rule_set|
|
||||||
rule_set.finalize
|
rule_set.finalize(@grammar)
|
||||||
end
|
end
|
||||||
# Generate the lexer.
|
# Generate the lexer.
|
||||||
@lexer = Lexer.new(@grammar)
|
@lexer = Lexer.new(@grammar)
|
||||||
# Generate the parser.
|
# Generate the parser.
|
||||||
@parser = Parser.new(@grammar, rule_sets, @log)
|
@parser = Parser.new(@grammar, rule_sets, @log, @options)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Check that any referenced ptypes have been defined.
|
||||||
|
def check_ptypes!
|
||||||
|
(@grammar.patterns + @grammar.tokens + @grammar.rules).each do |potor|
|
||||||
|
if potor.ptypename
|
||||||
|
unless @grammar.ptypes.include?(potor.ptypename)
|
||||||
|
raise Error.new("Error: Line #{potor.line_number}: ptype #{potor.ptypename} not declared. Declare with `ptype` statement.")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Generate and add rules for any optional components.
|
||||||
|
def generate_optional_component_rules!(tokens_by_name)
|
||||||
|
optional_rules_added = Set.new
|
||||||
|
@grammar.rules.each do |rule|
|
||||||
|
rule.components.each do |component|
|
||||||
|
if component =~ /^(.*)\?$/
|
||||||
|
c = $1
|
||||||
|
unless optional_rules_added.include?(component)
|
||||||
|
# Create two rules for the optional component: one empty and
|
||||||
|
# one just matching the component.
|
||||||
|
# We need to find the ptypename for the optional component in
|
||||||
|
# order to copy it to the generated rules.
|
||||||
|
if tokens_by_name[c]
|
||||||
|
# The optional component is a token.
|
||||||
|
ptypename = tokens_by_name[c].ptypename
|
||||||
|
else
|
||||||
|
# The optional component must be a rule, so find any instance
|
||||||
|
# of that rule that specifies a ptypename.
|
||||||
|
ptypename = @grammar.rules.reduce(nil) do |result, rule|
|
||||||
|
rule.name == c && rule.ptypename ? rule.ptypename : result
|
||||||
|
end
|
||||||
|
end
|
||||||
|
@grammar.rules << Rule.new(component, [], nil, ptypename, rule.line_number)
|
||||||
|
@grammar.rules << Rule.new(component, [c], "$$ = $1;\n", ptypename, rule.line_number)
|
||||||
|
optional_rules_added << component
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Determine which grammar rules could expand to empty sequences.
|
# Determine which grammar rules could expand to empty sequences.
|
||||||
@ -229,6 +276,19 @@ class Propane
|
|||||||
"statevalues[$-1-n_states+#{index}].pvalue.v_#{rule.components[index - 1].ptypename}"
|
"statevalues[$-1-n_states+#{index}].pvalue.v_#{rule.components[index - 1].ptypename}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
code = code.gsub(/\$\{(\w+)\}/) do |match|
|
||||||
|
aliasname = $1
|
||||||
|
if index = rule.aliases[aliasname]
|
||||||
|
case @language
|
||||||
|
when "c"
|
||||||
|
"state_values_stack_index(statevalues, -(int)n_states + #{index})->pvalue.v_#{rule.components[index].ptypename}"
|
||||||
|
when "d"
|
||||||
|
"statevalues[$-n_states+#{index}].pvalue.v_#{rule.components[index].ptypename}"
|
||||||
|
end
|
||||||
|
else
|
||||||
|
raise Error.new("Field alias '#{aliasname}' not found")
|
||||||
|
end
|
||||||
|
end
|
||||||
else
|
else
|
||||||
code = code.gsub(/\$\$/) do |match|
|
code = code.gsub(/\$\$/) do |match|
|
||||||
if @grammar.ast
|
if @grammar.ast
|
||||||
@ -270,7 +330,7 @@ class Propane
|
|||||||
# Start rule parser value type name and type string.
|
# Start rule parser value type name and type string.
|
||||||
def start_rule_type
|
def start_rule_type
|
||||||
start_rule = @grammar.rules.find do |rule|
|
start_rule = @grammar.rules.find do |rule|
|
||||||
rule.name == "Start"
|
rule.name == @grammar.start_rule
|
||||||
end
|
end
|
||||||
[start_rule.ptypename, @grammar.ptypes[start_rule.ptypename]]
|
[start_rule.ptypename, @grammar.ptypes[start_rule.ptypename]]
|
||||||
end
|
end
|
||||||
|
@ -6,9 +6,12 @@ class Propane
|
|||||||
IDENTIFIER_REGEX = /(?:[a-zA-Z]|_[a-zA-Z0-9])[a-zA-Z_0-9]*/
|
IDENTIFIER_REGEX = /(?:[a-zA-Z]|_[a-zA-Z0-9])[a-zA-Z_0-9]*/
|
||||||
|
|
||||||
attr_reader :ast
|
attr_reader :ast
|
||||||
|
attr_reader :ast_prefix
|
||||||
|
attr_reader :ast_suffix
|
||||||
attr_reader :modulename
|
attr_reader :modulename
|
||||||
attr_reader :patterns
|
attr_reader :patterns
|
||||||
attr_reader :rules
|
attr_reader :rules
|
||||||
|
attr_reader :start_rule
|
||||||
attr_reader :tokens
|
attr_reader :tokens
|
||||||
attr_reader :code_blocks
|
attr_reader :code_blocks
|
||||||
attr_reader :ptypes
|
attr_reader :ptypes
|
||||||
@ -16,6 +19,7 @@ class Propane
|
|||||||
|
|
||||||
def initialize(input)
|
def initialize(input)
|
||||||
@patterns = []
|
@patterns = []
|
||||||
|
@start_rule = "Start"
|
||||||
@tokens = []
|
@tokens = []
|
||||||
@rules = []
|
@rules = []
|
||||||
@code_blocks = {}
|
@code_blocks = {}
|
||||||
@ -26,6 +30,8 @@ class Propane
|
|||||||
@ptypes = {"default" => "void *"}
|
@ptypes = {"default" => "void *"}
|
||||||
@prefix = "p_"
|
@prefix = "p_"
|
||||||
@ast = false
|
@ast = false
|
||||||
|
@ast_prefix = ""
|
||||||
|
@ast_suffix = ""
|
||||||
parse_grammar!
|
parse_grammar!
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -54,9 +60,12 @@ class Propane
|
|||||||
elsif parse_comment_line!
|
elsif parse_comment_line!
|
||||||
elsif @mode.nil? && parse_mode_label!
|
elsif @mode.nil? && parse_mode_label!
|
||||||
elsif parse_ast_statement!
|
elsif parse_ast_statement!
|
||||||
|
elsif parse_ast_prefix_statement!
|
||||||
|
elsif parse_ast_suffix_statement!
|
||||||
elsif parse_module_statement!
|
elsif parse_module_statement!
|
||||||
elsif parse_ptype_statement!
|
elsif parse_ptype_statement!
|
||||||
elsif parse_pattern_statement!
|
elsif parse_pattern_statement!
|
||||||
|
elsif parse_start_statement!
|
||||||
elsif parse_token_statement!
|
elsif parse_token_statement!
|
||||||
elsif parse_tokenid_statement!
|
elsif parse_tokenid_statement!
|
||||||
elsif parse_drop_statement!
|
elsif parse_drop_statement!
|
||||||
@ -91,6 +100,18 @@ class Propane
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def parse_ast_prefix_statement!
|
||||||
|
if md = consume!(/ast_prefix\s+(\w+)\s*;/)
|
||||||
|
@ast_prefix = md[1]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def parse_ast_suffix_statement!
|
||||||
|
if md = consume!(/ast_suffix\s+(\w+)\s*;/)
|
||||||
|
@ast_suffix = md[1]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def parse_module_statement!
|
def parse_module_statement!
|
||||||
if consume!(/module\s+/)
|
if consume!(/module\s+/)
|
||||||
md = consume!(/([\w.]+)\s*/, "expected module name")
|
md = consume!(/([\w.]+)\s*/, "expected module name")
|
||||||
@ -177,7 +198,7 @@ class Propane
|
|||||||
if @ast && ptypename
|
if @ast && ptypename
|
||||||
raise Error.new("Multiple ptypes are unsupported in AST mode")
|
raise Error.new("Multiple ptypes are unsupported in AST mode")
|
||||||
end
|
end
|
||||||
md = consume!(/((?:#{IDENTIFIER_REGEX}\s*)*)\s*/, "expected rule component list")
|
md = consume!(/((?:#{IDENTIFIER_REGEX}(?::#{IDENTIFIER_REGEX})?\??\s*)*)\s*/, "expected rule component list")
|
||||||
components = md[1].strip.split(/\s+/)
|
components = md[1].strip.split(/\s+/)
|
||||||
if @ast
|
if @ast
|
||||||
consume!(/;/, "expected `;'")
|
consume!(/;/, "expected `;'")
|
||||||
@ -210,6 +231,12 @@ class Propane
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def parse_start_statement!
|
||||||
|
if md = consume!(/start\s+(\w+)\s*;/)
|
||||||
|
@start_rule = md[1]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def parse_code_block_statement!
|
def parse_code_block_statement!
|
||||||
if md = consume!(/<<([a-z]*)(.*?)>>\n/m)
|
if md = consume!(/<<([a-z]*)(.*?)>>\n/m)
|
||||||
name, code = md[1..2]
|
name, code = md[1..2]
|
||||||
|
@ -7,12 +7,14 @@ class Propane
|
|||||||
attr_reader :reduce_table
|
attr_reader :reduce_table
|
||||||
attr_reader :rule_sets
|
attr_reader :rule_sets
|
||||||
|
|
||||||
def initialize(grammar, rule_sets, log)
|
def initialize(grammar, rule_sets, log, options)
|
||||||
@grammar = grammar
|
@grammar = grammar
|
||||||
@rule_sets = rule_sets
|
@rule_sets = rule_sets
|
||||||
@log = log
|
@log = log
|
||||||
@item_sets = []
|
@item_sets = []
|
||||||
@item_sets_set = {}
|
@item_sets_set = {}
|
||||||
|
@warnings = Set.new
|
||||||
|
@options = options
|
||||||
start_item = Item.new(grammar.rules.first, 0)
|
start_item = Item.new(grammar.rules.first, 0)
|
||||||
eval_item_sets = Set[ItemSet.new([start_item])]
|
eval_item_sets = Set[ItemSet.new([start_item])]
|
||||||
|
|
||||||
@ -23,10 +25,10 @@ class Propane
|
|||||||
item_set.id = @item_sets.size
|
item_set.id = @item_sets.size
|
||||||
@item_sets << item_set
|
@item_sets << item_set
|
||||||
@item_sets_set[item_set] = item_set
|
@item_sets_set[item_set] = item_set
|
||||||
item_set.following_symbols.each do |following_symbol|
|
item_set.next_symbols.each do |next_symbol|
|
||||||
unless following_symbol.name == "$EOF"
|
unless next_symbol.name == "$EOF"
|
||||||
following_set = item_set.build_following_item_set(following_symbol)
|
next_item_set = item_set.build_next_item_set(next_symbol)
|
||||||
eval_item_sets << following_set
|
eval_item_sets << next_item_set
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -37,8 +39,11 @@ class Propane
|
|||||||
end
|
end
|
||||||
|
|
||||||
build_reduce_actions!
|
build_reduce_actions!
|
||||||
write_log!
|
|
||||||
build_tables!
|
build_tables!
|
||||||
|
write_log!
|
||||||
|
if @warnings.size > 0 && @options[:warnings_as_errors]
|
||||||
|
raise Error.new("Fatal errors (-w):\n" + @warnings.join("\n"))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
@ -48,27 +53,37 @@ class Propane
|
|||||||
@shift_table = []
|
@shift_table = []
|
||||||
@reduce_table = []
|
@reduce_table = []
|
||||||
@item_sets.each do |item_set|
|
@item_sets.each do |item_set|
|
||||||
shift_entries = item_set.following_symbols.map do |following_symbol|
|
shift_entries = item_set.next_symbols.map do |next_symbol|
|
||||||
state_id =
|
state_id =
|
||||||
if following_symbol.name == "$EOF"
|
if next_symbol.name == "$EOF"
|
||||||
0
|
0
|
||||||
else
|
else
|
||||||
item_set.following_item_set[following_symbol].id
|
item_set.next_item_set[next_symbol].id
|
||||||
end
|
end
|
||||||
{
|
{
|
||||||
symbol_id: following_symbol.id,
|
symbol: next_symbol,
|
||||||
state_id: state_id,
|
state_id: state_id,
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
unless item_set.reduce_rules.empty?
|
||||||
|
shift_entries.each do |shift_entry|
|
||||||
|
token = shift_entry[:symbol]
|
||||||
|
if get_lookahead_reduce_actions_for_item_set(item_set).include?(token)
|
||||||
|
rule = item_set.reduce_actions[token]
|
||||||
|
@warnings << "Shift/Reduce conflict (state #{item_set.id}) between token #{token.name} and rule #{rule.name} (defined on line #{rule.line_number})"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
reduce_entries =
|
reduce_entries =
|
||||||
case ra = item_set.reduce_actions
|
if rule = item_set.reduce_rule
|
||||||
when Rule
|
[{token_id: @grammar.invalid_token_id, rule_id: rule.id, rule: rule,
|
||||||
[{token_id: @grammar.invalid_token_id, rule_id: ra.id, rule: ra,
|
rule_set_id: rule.rule_set.id, n_states: rule.components.size,
|
||||||
rule_set_id: ra.rule_set.id, n_states: ra.components.size}]
|
propagate_optional_target: rule.optional? && rule.components.size == 1}]
|
||||||
when Hash
|
elsif reduce_actions = item_set.reduce_actions
|
||||||
ra.map do |token, rule|
|
reduce_actions.map do |token, rule|
|
||||||
{token_id: token.id, rule_id: rule.id, rule: rule,
|
{token_id: token.id, rule_id: rule.id, rule: rule,
|
||||||
rule_set_id: rule.rule_set.id, n_states: rule.components.size}
|
rule_set_id: rule.rule_set.id, n_states: rule.components.size,
|
||||||
|
propagate_optional_target: rule.optional? && rule.components.size == 1}
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
[]
|
[]
|
||||||
@ -85,11 +100,11 @@ class Propane
|
|||||||
end
|
end
|
||||||
|
|
||||||
def process_item_set(item_set)
|
def process_item_set(item_set)
|
||||||
item_set.following_symbols.each do |following_symbol|
|
item_set.next_symbols.each do |next_symbol|
|
||||||
unless following_symbol.name == "$EOF"
|
unless next_symbol.name == "$EOF"
|
||||||
following_set = @item_sets_set[item_set.build_following_item_set(following_symbol)]
|
next_item_set = @item_sets_set[item_set.build_next_item_set(next_symbol)]
|
||||||
item_set.following_item_set[following_symbol] = following_set
|
item_set.next_item_set[next_symbol] = next_item_set
|
||||||
following_set.in_sets << item_set
|
next_item_set.in_sets << item_set
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -99,7 +114,7 @@ class Propane
|
|||||||
# @return [void]
|
# @return [void]
|
||||||
def build_reduce_actions!
|
def build_reduce_actions!
|
||||||
@item_sets.each do |item_set|
|
@item_sets.each do |item_set|
|
||||||
item_set.reduce_actions = build_reduce_actions_for_item_set(item_set)
|
build_reduce_actions_for_item_set(item_set)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -108,38 +123,55 @@ class Propane
|
|||||||
# @param item_set [ItemSet]
|
# @param item_set [ItemSet]
|
||||||
# ItemSet (parser state)
|
# ItemSet (parser state)
|
||||||
#
|
#
|
||||||
# @return [nil, Rule, Hash]
|
# @return [void]
|
||||||
# If no reduce actions are possible for the given item set, nil.
|
|
||||||
# If only one reduce action is possible for the given item set, the Rule
|
|
||||||
# to reduce.
|
|
||||||
# Otherwise, a mapping of lookahead Tokens to the Rules to reduce.
|
|
||||||
def build_reduce_actions_for_item_set(item_set)
|
def build_reduce_actions_for_item_set(item_set)
|
||||||
# To build the reduce actions, we start by looking at any
|
# To build the reduce actions, we start by looking at any
|
||||||
# "complete" items, i.e., items where the parse position is at the
|
# "complete" items, i.e., items where the parse position is at the
|
||||||
# end of a rule. These are the only rules that are candidates for
|
# end of a rule. These are the only rules that are candidates for
|
||||||
# reduction in the current ItemSet.
|
# reduction in the current ItemSet.
|
||||||
reduce_rules = Set.new(item_set.items.select(&:complete?).map(&:rule))
|
item_set.reduce_rules = Set.new(item_set.items.select(&:complete?).map(&:rule))
|
||||||
|
|
||||||
# If there are no rules to reduce for this ItemSet, we're done here.
|
if item_set.reduce_rules.size == 1
|
||||||
return nil if reduce_rules.size == 0
|
item_set.reduce_rule = item_set.reduce_rules.first
|
||||||
|
end
|
||||||
|
|
||||||
# If there is exactly one rule to reduce for this ItemSet, then do not
|
if item_set.reduce_rules.size > 1
|
||||||
# figure out the lookaheads; just reduce it.
|
# Force item_set.reduce_actions to be built to store the lookahead
|
||||||
return reduce_rules.first if reduce_rules.size == 1
|
# tokens for the possible reduce rules if there is more than one.
|
||||||
|
get_lookahead_reduce_actions_for_item_set(item_set)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# Otherwise, we have more than one possible rule to reduce.
|
# Get the reduce actions for a single item set (parser state).
|
||||||
|
#
|
||||||
|
# @param item_set [ItemSet]
|
||||||
|
# ItemSet (parser state)
|
||||||
|
#
|
||||||
|
# @return [Hash]
|
||||||
|
# Mapping of lookahead Tokens to the Rules to reduce.
|
||||||
|
def get_lookahead_reduce_actions_for_item_set(item_set)
|
||||||
|
item_set.reduce_actions ||= build_lookahead_reduce_actions_for_item_set(item_set)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Build the reduce actions for a single item set (parser state).
|
||||||
|
#
|
||||||
|
# @param item_set [ItemSet]
|
||||||
|
# ItemSet (parser state)
|
||||||
|
#
|
||||||
|
# @return [Hash]
|
||||||
|
# Mapping of lookahead Tokens to the Rules to reduce.
|
||||||
|
def build_lookahead_reduce_actions_for_item_set(item_set)
|
||||||
# We will be looking for all possible tokens that can follow instances of
|
# We will be looking for all possible tokens that can follow instances of
|
||||||
# these rules. Rather than looking through the entire grammar for the
|
# these rules. Rather than looking through the entire grammar for the
|
||||||
# possible following tokens, we will only look in the item sets leading
|
# possible following tokens, we will only look in the item sets leading
|
||||||
# up to this one. This restriction gives us a more precise lookahead set,
|
# up to this one. This restriction gives us a more precise lookahead set,
|
||||||
# and allows us to parse LALR grammars.
|
# and allows us to parse LALR grammars.
|
||||||
item_sets = Set[item_set] + item_set.leading_item_sets
|
item_sets = Set[item_set] + item_set.leading_item_sets
|
||||||
reduce_rules.reduce({}) do |reduce_actions, reduce_rule|
|
item_set.reduce_rules.reduce({}) do |reduce_actions, reduce_rule|
|
||||||
lookahead_tokens_for_rule = build_lookahead_tokens_to_reduce(reduce_rule, item_sets)
|
lookahead_tokens_for_rule = build_lookahead_tokens_to_reduce(reduce_rule, item_sets)
|
||||||
lookahead_tokens_for_rule.each do |lookahead_token|
|
lookahead_tokens_for_rule.each do |lookahead_token|
|
||||||
if existing_reduce_rule = reduce_actions[lookahead_token]
|
if existing_reduce_rule = reduce_actions[lookahead_token]
|
||||||
raise Error.new("Error: reduce/reduce conflict between rule #{existing_reduce_rule.id} (#{existing_reduce_rule.name}) and rule #{reduce_rule.id} (#{reduce_rule.name})")
|
raise Error.new("Error: reduce/reduce conflict (state #{item_set.id}) between rule #{existing_reduce_rule.name}##{existing_reduce_rule.id} (defined on line #{existing_reduce_rule.line_number}) and rule #{reduce_rule.name}##{reduce_rule.id} (defined on line #{reduce_rule.line_number})")
|
||||||
end
|
end
|
||||||
reduce_actions[lookahead_token] = reduce_rule
|
reduce_actions[lookahead_token] = reduce_rule
|
||||||
end
|
end
|
||||||
@ -181,9 +213,9 @@ class Propane
|
|||||||
# tokens to form the lookahead token set.
|
# tokens to form the lookahead token set.
|
||||||
item_sets.each do |item_set|
|
item_sets.each do |item_set|
|
||||||
item_set.items.each do |item|
|
item_set.items.each do |item|
|
||||||
if item.following_symbol == rule_set
|
if item.next_symbol == rule_set
|
||||||
(1..).each do |offset|
|
(1..).each do |offset|
|
||||||
case symbol = item.following_symbol(offset)
|
case symbol = item.next_symbol(offset)
|
||||||
when nil
|
when nil
|
||||||
rule_set = item.rule.rule_set
|
rule_set = item.rule.rule_set
|
||||||
unless checked_rule_sets.include?(rule_set)
|
unless checked_rule_sets.include?(rule_set)
|
||||||
@ -240,20 +272,26 @@ class Propane
|
|||||||
@log.puts
|
@log.puts
|
||||||
@log.puts " Incoming states: #{incoming_ids.join(", ")}"
|
@log.puts " Incoming states: #{incoming_ids.join(", ")}"
|
||||||
@log.puts " Outgoing states:"
|
@log.puts " Outgoing states:"
|
||||||
item_set.following_item_set.each do |following_symbol, following_item_set|
|
item_set.next_item_set.each do |next_symbol, next_item_set|
|
||||||
@log.puts " #{following_symbol.name} => #{following_item_set.id}"
|
@log.puts " #{next_symbol.name} => #{next_item_set.id}"
|
||||||
end
|
end
|
||||||
@log.puts
|
@log.puts
|
||||||
@log.puts " Reduce actions:"
|
@log.puts " Reduce actions:"
|
||||||
case item_set.reduce_actions
|
if item_set.reduce_rule
|
||||||
when Rule
|
@log.puts " * => rule #{item_set.reduce_rule.id}, rule set #{@rule_sets[item_set.reduce_rule.name].id} (#{item_set.reduce_rule.name})"
|
||||||
@log.puts " * => rule #{item_set.reduce_actions.id}, rule set #{@rule_sets[item_set.reduce_actions.name].id} (#{item_set.reduce_actions.name})"
|
elsif item_set.reduce_actions
|
||||||
when Hash
|
|
||||||
item_set.reduce_actions.each do |token, rule|
|
item_set.reduce_actions.each do |token, rule|
|
||||||
@log.puts " lookahead #{token.name} => #{rule.name} (#{rule.id}), rule set ##{rule.rule_set.id}"
|
@log.puts " lookahead #{token.name} => #{rule.name} (#{rule.id}), rule set ##{rule.rule_set.id}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
if @warnings.size > 0
|
||||||
|
@log.puts
|
||||||
|
@log.puts "Warnings:"
|
||||||
|
@warnings.each do |warning|
|
||||||
|
@log.puts " #{warning}"
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
@ -56,7 +56,7 @@ class Propane
|
|||||||
|
|
||||||
# Return the set of Items obtained by "closing" the current item.
|
# Return the set of Items obtained by "closing" the current item.
|
||||||
#
|
#
|
||||||
# If the following symbol for the current item is another Rule name, then
|
# If the next symbol for the current item is another Rule name, then
|
||||||
# this method will return all Items for that Rule with a position of 0.
|
# this method will return all Items for that Rule with a position of 0.
|
||||||
# Otherwise, an empty Array is returned.
|
# Otherwise, an empty Array is returned.
|
||||||
#
|
#
|
||||||
@ -81,17 +81,17 @@ class Propane
|
|||||||
@position == @rule.components.size
|
@position == @rule.components.size
|
||||||
end
|
end
|
||||||
|
|
||||||
# Get the following symbol for the Item.
|
# Get the next symbol for the Item.
|
||||||
#
|
#
|
||||||
# That is, the symbol which follows the parse position marker in the
|
# That is, the symbol which is after the parse position marker in the
|
||||||
# current Item.
|
# current Item.
|
||||||
#
|
#
|
||||||
# @param offset [Integer]
|
# @param offset [Integer]
|
||||||
# Offset from current parse position to examine.
|
# Offset from current parse position to examine.
|
||||||
#
|
#
|
||||||
# @return [Token, RuleSet, nil]
|
# @return [Token, RuleSet, nil]
|
||||||
# Following symbol for the Item.
|
# Next symbol for the Item.
|
||||||
def following_symbol(offset = 0)
|
def next_symbol(offset = 0)
|
||||||
@rule.components[@position + offset]
|
@rule.components[@position + offset]
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -108,25 +108,25 @@ class Propane
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Get whether this Item is followed by the provided symbol.
|
# Get whether this Item's next symbol is the given symbol.
|
||||||
#
|
#
|
||||||
# @param symbol [Token, RuleSet]
|
# @param symbol [Token, RuleSet]
|
||||||
# Symbol to query.
|
# Symbol to query.
|
||||||
#
|
#
|
||||||
# @return [Boolean]
|
# @return [Boolean]
|
||||||
# Whether this Item is followed by the provided symbol.
|
# Whether this Item's next symbol is the given symbol.
|
||||||
def followed_by?(symbol)
|
def next_symbol?(symbol)
|
||||||
following_symbol == symbol
|
next_symbol == symbol
|
||||||
end
|
end
|
||||||
|
|
||||||
# Get the following item for this Item.
|
# Get the next item for this Item.
|
||||||
#
|
#
|
||||||
# That is, the Item formed by moving the parse position marker one place
|
# That is, the Item formed by moving the parse position marker one place
|
||||||
# forward from its position in this Item.
|
# forward from its position in this Item.
|
||||||
#
|
#
|
||||||
# @return [Item]
|
# @return [Item]
|
||||||
# The following item for this Item.
|
# The next item for this Item.
|
||||||
def following_item
|
def next_item
|
||||||
Item.new(@rule, @position + 1)
|
Item.new(@rule, @position + 1)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -2,7 +2,7 @@ class Propane
|
|||||||
class Parser
|
class Parser
|
||||||
|
|
||||||
# Represent a parser "item set", which is a set of possible items that the
|
# Represent a parser "item set", which is a set of possible items that the
|
||||||
# parser could currently be parsing.
|
# parser could currently be parsing. This is equivalent to a parser state.
|
||||||
class ItemSet
|
class ItemSet
|
||||||
|
|
||||||
# @return [Set<Item>]
|
# @return [Set<Item>]
|
||||||
@ -14,15 +14,24 @@ class Propane
|
|||||||
attr_accessor :id
|
attr_accessor :id
|
||||||
|
|
||||||
# @return [Hash]
|
# @return [Hash]
|
||||||
# Maps a following symbol to its ItemSet.
|
# Maps a next symbol to its ItemSet.
|
||||||
attr_reader :following_item_set
|
attr_reader :next_item_set
|
||||||
|
|
||||||
# @return [Set<ItemSet>]
|
# @return [Set<ItemSet>]
|
||||||
# ItemSets leading to this item set.
|
# ItemSets leading to this item set.
|
||||||
attr_reader :in_sets
|
attr_reader :in_sets
|
||||||
|
|
||||||
# @return [nil, Rule, Hash]
|
# @return [nil, Rule]
|
||||||
# Reduce actions, mapping lookahead tokens to rules.
|
# Rule to reduce if there is only one possibility.
|
||||||
|
attr_accessor :reduce_rule
|
||||||
|
|
||||||
|
# @return [Set<Rule>]
|
||||||
|
# Set of rules that could be reduced in this parser state.
|
||||||
|
attr_accessor :reduce_rules
|
||||||
|
|
||||||
|
# @return [nil, Hash]
|
||||||
|
# Reduce actions, mapping lookahead tokens to rules, if there is
|
||||||
|
# more than one rule that could be reduced.
|
||||||
attr_accessor :reduce_actions
|
attr_accessor :reduce_actions
|
||||||
|
|
||||||
# Build an ItemSet.
|
# Build an ItemSet.
|
||||||
@ -31,28 +40,28 @@ class Propane
|
|||||||
# Items in this ItemSet.
|
# Items in this ItemSet.
|
||||||
def initialize(items)
|
def initialize(items)
|
||||||
@items = Set.new(items)
|
@items = Set.new(items)
|
||||||
@following_item_set = {}
|
@next_item_set = {}
|
||||||
@in_sets = Set.new
|
@in_sets = Set.new
|
||||||
close!
|
close!
|
||||||
end
|
end
|
||||||
|
|
||||||
# Get the set of following symbols for all Items in this ItemSet.
|
# Get the set of next symbols for all Items in this ItemSet.
|
||||||
#
|
#
|
||||||
# @return [Set<Token, RuleSet>]
|
# @return [Set<Token, RuleSet>]
|
||||||
# Set of following symbols for all Items in this ItemSet.
|
# Set of next symbols for all Items in this ItemSet.
|
||||||
def following_symbols
|
def next_symbols
|
||||||
Set.new(@items.map(&:following_symbol).compact)
|
@_next_symbols ||= Set.new(@items.map(&:next_symbol).compact)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Build a following ItemSet for the given following symbol.
|
# Build a next ItemSet for the given next symbol.
|
||||||
#
|
#
|
||||||
# @param symbol [Token, RuleSet]
|
# @param symbol [Token, RuleSet]
|
||||||
# Following symbol to build the following ItemSet for.
|
# Next symbol to build the next ItemSet for.
|
||||||
#
|
#
|
||||||
# @return [ItemSet]
|
# @return [ItemSet]
|
||||||
# Following ItemSet for the given following symbol.
|
# Next ItemSet for the given next symbol.
|
||||||
def build_following_item_set(symbol)
|
def build_next_item_set(symbol)
|
||||||
ItemSet.new(items_followed_by(symbol).map(&:following_item))
|
ItemSet.new(items_with_next(symbol).map(&:next_item))
|
||||||
end
|
end
|
||||||
|
|
||||||
# Hash function.
|
# Hash function.
|
||||||
@ -90,21 +99,24 @@ class Propane
|
|||||||
# @return [Set<ItemSet>]
|
# @return [Set<ItemSet>]
|
||||||
# Set of all ItemSets that lead up to this ItemSet.
|
# Set of all ItemSets that lead up to this ItemSet.
|
||||||
def leading_item_sets
|
def leading_item_sets
|
||||||
result = Set.new
|
@_leading_item_sets ||=
|
||||||
eval_sets = Set[self]
|
begin
|
||||||
evaled = Set.new
|
result = Set.new
|
||||||
while eval_sets.size > 0
|
eval_sets = Set[self]
|
||||||
eval_set = eval_sets.first
|
evaled = Set.new
|
||||||
eval_sets.delete(eval_set)
|
while eval_sets.size > 0
|
||||||
evaled << eval_set
|
eval_set = eval_sets.first
|
||||||
eval_set.in_sets.each do |in_set|
|
eval_sets.delete(eval_set)
|
||||||
result << in_set
|
evaled << eval_set
|
||||||
unless evaled.include?(in_set)
|
eval_set.in_sets.each do |in_set|
|
||||||
eval_sets << in_set
|
result << in_set
|
||||||
|
unless evaled.include?(in_set)
|
||||||
|
eval_sets << in_set
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
result
|
||||||
end
|
end
|
||||||
end
|
|
||||||
result
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Represent the ItemSet as a String.
|
# Represent the ItemSet as a String.
|
||||||
@ -137,16 +149,16 @@ class Propane
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Get the Items followed by the given following symbol.
|
# Get the Items with the given next symbol.
|
||||||
#
|
#
|
||||||
# @param symbol [Token, RuleSet]
|
# @param symbol [Token, RuleSet]
|
||||||
# Following symbol.
|
# Next symbol.
|
||||||
#
|
#
|
||||||
# @return [Array<Item>]
|
# @return [Array<Item>]
|
||||||
# Items followed by the given following symbol.
|
# Items with the given next symbol.
|
||||||
def items_followed_by(symbol)
|
def items_with_next(symbol)
|
||||||
@items.select do |item|
|
@items.select do |item|
|
||||||
item.followed_by?(symbol)
|
item.next_symbol?(symbol)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -6,6 +6,10 @@ class Propane
|
|||||||
# Rule components.
|
# Rule components.
|
||||||
attr_reader :components
|
attr_reader :components
|
||||||
|
|
||||||
|
# @return [Hash]
|
||||||
|
# Field aliases.
|
||||||
|
attr_reader :aliases
|
||||||
|
|
||||||
# @return [String]
|
# @return [String]
|
||||||
# User code associated with the rule.
|
# User code associated with the rule.
|
||||||
attr_reader :code
|
attr_reader :code
|
||||||
@ -49,7 +53,19 @@ class Propane
|
|||||||
# Line number where the rule was defined in the input grammar.
|
# Line number where the rule was defined in the input grammar.
|
||||||
def initialize(name, components, code, ptypename, line_number)
|
def initialize(name, components, code, ptypename, line_number)
|
||||||
@name = name
|
@name = name
|
||||||
@components = components
|
@aliases = {}
|
||||||
|
@components = components.each_with_index.map do |component, i|
|
||||||
|
if component =~ /(\S+):(\S+)/
|
||||||
|
c, aliasname = $1, $2
|
||||||
|
if @aliases[aliasname]
|
||||||
|
raise Error.new("Error: duplicate field alias `#{aliasname}` for rule #{name} defined on line #{line_number}")
|
||||||
|
end
|
||||||
|
@aliases[aliasname] = i
|
||||||
|
c
|
||||||
|
else
|
||||||
|
component
|
||||||
|
end
|
||||||
|
end
|
||||||
@rule_set_node_field_index_map = components.map {0}
|
@rule_set_node_field_index_map = components.map {0}
|
||||||
@code = code
|
@code = code
|
||||||
@ptypename = ptypename
|
@ptypename = ptypename
|
||||||
@ -66,6 +82,14 @@ class Propane
|
|||||||
@components.empty?
|
@components.empty?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Return whether this is an optional Rule.
|
||||||
|
#
|
||||||
|
# @return [Boolean]
|
||||||
|
# Whether this is an optional Rule.
|
||||||
|
def optional?
|
||||||
|
@name.end_with?("?")
|
||||||
|
end
|
||||||
|
|
||||||
# Represent the Rule as a String.
|
# Represent the Rule as a String.
|
||||||
#
|
#
|
||||||
# @return [String]
|
# @return [String]
|
||||||
|
@ -3,6 +3,10 @@ class Propane
|
|||||||
# A RuleSet collects all grammar rules of the same name.
|
# A RuleSet collects all grammar rules of the same name.
|
||||||
class RuleSet
|
class RuleSet
|
||||||
|
|
||||||
|
# @return [Array<Hash>]
|
||||||
|
# AST fields.
|
||||||
|
attr_reader :ast_fields
|
||||||
|
|
||||||
# @return [Integer]
|
# @return [Integer]
|
||||||
# ID of the RuleSet.
|
# ID of the RuleSet.
|
||||||
attr_reader :id
|
attr_reader :id
|
||||||
@ -52,6 +56,24 @@ class Propane
|
|||||||
@could_be_empty
|
@could_be_empty
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Return whether this is an optional RuleSet.
|
||||||
|
#
|
||||||
|
# @return [Boolean]
|
||||||
|
# Whether this is an optional RuleSet.
|
||||||
|
def optional?
|
||||||
|
@name.end_with?("?")
|
||||||
|
end
|
||||||
|
|
||||||
|
# For optional rule sets, return the underlying component that is optional.
|
||||||
|
def option_target
|
||||||
|
@rules.each do |rule|
|
||||||
|
if rule.components.size > 0
|
||||||
|
return rule.components[0]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
raise "Optional rule target not found"
|
||||||
|
end
|
||||||
|
|
||||||
# Build the start token set for the RuleSet.
|
# Build the start token set for the RuleSet.
|
||||||
#
|
#
|
||||||
# @return [Set<Token>]
|
# @return [Set<Token>]
|
||||||
@ -76,6 +98,15 @@ class Propane
|
|||||||
@_start_token_set
|
@_start_token_set
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Finalize a RuleSet after adding all Rules to it.
|
||||||
|
def finalize(grammar)
|
||||||
|
if grammar.ast
|
||||||
|
build_ast_fields(grammar)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
# Build the set of AST fields for this RuleSet.
|
# Build the set of AST fields for this RuleSet.
|
||||||
#
|
#
|
||||||
# This is an Array of Hashes. Each entry in the Array corresponds to a
|
# This is an Array of Hashes. Each entry in the Array corresponds to a
|
||||||
@ -84,46 +115,53 @@ class Propane
|
|||||||
# a key. It may also have the field name without the positional suffix if
|
# 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.
|
# that field only exists in one position across all Rules in the RuleSet.
|
||||||
#
|
#
|
||||||
# @return [Array<Hash>]
|
# @return [void]
|
||||||
# AST fields.
|
def build_ast_fields(grammar)
|
||||||
def ast_fields
|
field_ast_node_indexes = {}
|
||||||
@_ast_fields ||=
|
field_indexes_across_all_rules = {}
|
||||||
begin
|
@ast_fields = []
|
||||||
field_ast_node_indexes = {}
|
@rules.each do |rule|
|
||||||
field_indexes_across_all_rules = {}
|
rule.components.each_with_index do |component, i|
|
||||||
ast_node_fields = []
|
if component.is_a?(RuleSet) && component.optional?
|
||||||
@rules.each do |rule|
|
component = component.option_target
|
||||||
rule.components.each_with_index do |component, i|
|
|
||||||
if component.is_a?(Token)
|
|
||||||
node_name = "Token"
|
|
||||||
else
|
|
||||||
node_name = component.name
|
|
||||||
end
|
|
||||||
field_name = "p#{node_name}#{i + 1}"
|
|
||||||
unless field_ast_node_indexes[field_name]
|
|
||||||
field_ast_node_indexes[field_name] = ast_node_fields.size
|
|
||||||
ast_node_fields << {field_name => node_name}
|
|
||||||
end
|
|
||||||
field_indexes_across_all_rules[node_name] ||= Set.new
|
|
||||||
field_indexes_across_all_rules[node_name] << field_ast_node_indexes[field_name]
|
|
||||||
rule.rule_set_node_field_index_map[i] = field_ast_node_indexes[field_name]
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
field_indexes_across_all_rules.each do |node_name, indexes_across_all_rules|
|
if component.is_a?(Token)
|
||||||
if indexes_across_all_rules.size == 1
|
node_name = "Token"
|
||||||
# If this field was only seen in one position across all rules,
|
else
|
||||||
# then add an alias to the positional field name that does not
|
node_name = component.name
|
||||||
# include the position.
|
|
||||||
ast_node_fields[indexes_across_all_rules.first]["p#{node_name}"] = node_name
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
ast_node_fields
|
struct_name = "#{grammar.ast_prefix}#{node_name}#{grammar.ast_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}
|
||||||
|
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]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
field_indexes_across_all_rules.each do |node_name, indexes_across_all_rules|
|
||||||
# Finalize a RuleSet after adding all Rules to it.
|
if indexes_across_all_rules.size == 1
|
||||||
def finalize
|
# If this field was only seen in one position across all rules,
|
||||||
ast_fields
|
# 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}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
# Now merge in the field aliases as given by the user in the
|
||||||
|
# grammar.
|
||||||
|
field_aliases = {}
|
||||||
|
@rules.each do |rule|
|
||||||
|
rule.aliases.each do |alias_name, index|
|
||||||
|
if field_aliases[alias_name] && field_aliases[alias_name] != index
|
||||||
|
raise Error.new("Error: conflicting AST node field positions for alias `#{alias_name}`")
|
||||||
|
end
|
||||||
|
field_aliases[alias_name] = index
|
||||||
|
@ast_fields[index][alias_name] = @ast_fields[index].first[1]
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
class Propane
|
class Propane
|
||||||
VERSION = "1.3.0"
|
VERSION = "1.5.1"
|
||||||
end
|
end
|
||||||
|
@ -54,6 +54,7 @@ EOF
|
|||||||
else
|
else
|
||||||
command += %W[spec/run/testparser#{options[:name]}.propane spec/run/testparser#{options[:name]}.#{options[:language]} --log spec/run/testparser#{options[:name]}.log]
|
command += %W[spec/run/testparser#{options[:name]}.propane spec/run/testparser#{options[:name]}.#{options[:language]} --log spec/run/testparser#{options[:name]}.log]
|
||||||
end
|
end
|
||||||
|
command += (options[:extra_args] || [])
|
||||||
if (options[:capture])
|
if (options[:capture])
|
||||||
stdout, stderr, status = Open3.capture3(*command)
|
stdout, stderr, status = Open3.capture3(*command)
|
||||||
Results.new(stdout, stderr, status)
|
Results.new(stdout, stderr, status)
|
||||||
@ -148,6 +149,106 @@ EOF
|
|||||||
expect(results.status).to_not eq 0
|
expect(results.status).to_not eq 0
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "raises an error when a pattern referenced ptype has not been defined" do
|
||||||
|
write_grammar <<EOF
|
||||||
|
ptype yes = int;
|
||||||
|
/foo/ (yes) <<
|
||||||
|
>>
|
||||||
|
/bar/ (no) <<
|
||||||
|
>>
|
||||||
|
EOF
|
||||||
|
results = run_propane(capture: true)
|
||||||
|
expect(results.stderr).to match /Error: Line 4: ptype no not declared\. Declare with `ptype` statement\./
|
||||||
|
expect(results.status).to_not eq 0
|
||||||
|
end
|
||||||
|
|
||||||
|
it "raises an error when a token referenced ptype has not been defined" do
|
||||||
|
write_grammar <<EOF
|
||||||
|
ptype yes = int;
|
||||||
|
token foo (yes);
|
||||||
|
token bar (no);
|
||||||
|
EOF
|
||||||
|
results = run_propane(capture: true)
|
||||||
|
expect(results.stderr).to match /Error: Line 3: ptype no not declared\. Declare with `ptype` statement\./
|
||||||
|
expect(results.status).to_not eq 0
|
||||||
|
end
|
||||||
|
|
||||||
|
it "raises an error when a rule referenced ptype has not been defined" do
|
||||||
|
write_grammar <<EOF
|
||||||
|
ptype yes = int;
|
||||||
|
token xyz;
|
||||||
|
foo (yes) -> bar;
|
||||||
|
bar (no) -> xyz;
|
||||||
|
EOF
|
||||||
|
results = run_propane(capture: true)
|
||||||
|
expect(results.stderr).to match /Error: Line 4: ptype no not declared\. Declare with `ptype` statement\./
|
||||||
|
expect(results.status).to_not eq 0
|
||||||
|
end
|
||||||
|
|
||||||
|
it "warns on shift/reduce conflicts" do
|
||||||
|
write_grammar <<EOF
|
||||||
|
token a;
|
||||||
|
token b;
|
||||||
|
Start -> As? b?;
|
||||||
|
As -> a As2?;
|
||||||
|
As2 -> b a As2?;
|
||||||
|
EOF
|
||||||
|
results = run_propane(capture: true)
|
||||||
|
expect(results.stderr).to eq ""
|
||||||
|
expect(results.status).to eq 0
|
||||||
|
expect(File.binread("spec/run/testparser.log")).to match %r{Shift/Reduce conflict \(state \d+\) between token b and rule As2\? \(defined on line 4\)}
|
||||||
|
end
|
||||||
|
|
||||||
|
it "errors on shift/reduce conflicts with -w" do
|
||||||
|
write_grammar <<EOF
|
||||||
|
token a;
|
||||||
|
token b;
|
||||||
|
Start -> As? b?;
|
||||||
|
As -> a As2?;
|
||||||
|
As2 -> b a As2?;
|
||||||
|
EOF
|
||||||
|
results = run_propane(extra_args: %w[-w], capture: true)
|
||||||
|
expect(results.stderr).to match %r{Shift/Reduce conflict \(state \d+\) between token b and rule As2\? \(defined on line 4\)}m
|
||||||
|
expect(results.status).to_not eq 0
|
||||||
|
expect(File.binread("spec/run/testparser.log")).to match %r{Shift/Reduce conflict \(state \d+\) between token b and rule As2\? \(defined on line 4\)}
|
||||||
|
end
|
||||||
|
|
||||||
|
it "errors on duplicate field aliases in a rule" do
|
||||||
|
write_grammar <<EOF
|
||||||
|
token a;
|
||||||
|
token b;
|
||||||
|
Start -> a:foo b:foo;
|
||||||
|
EOF
|
||||||
|
results = run_propane(extra_args: %w[-w], capture: true)
|
||||||
|
expect(results.stderr).to match %r{Error: duplicate field alias `foo` for rule Start defined on line 3}
|
||||||
|
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
|
||||||
|
write_grammar <<EOF
|
||||||
|
ast;
|
||||||
|
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.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
|
||||||
|
write_grammar <<EOF
|
||||||
|
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 eq ""
|
||||||
|
expect(results.status).to eq 0
|
||||||
|
end
|
||||||
|
|
||||||
%w[d c].each do |language|
|
%w[d c].each do |language|
|
||||||
|
|
||||||
context "#{language.upcase} language" do
|
context "#{language.upcase} language" do
|
||||||
@ -587,7 +688,7 @@ F -> e;
|
|||||||
EOF
|
EOF
|
||||||
results = run_propane(capture: true, language: language)
|
results = run_propane(capture: true, language: language)
|
||||||
expect(results.status).to_not eq 0
|
expect(results.status).to_not eq 0
|
||||||
expect(results.stderr).to match %r{reduce/reduce conflict.*\(E\).*\(F\)}
|
expect(results.stderr).to match %r{Error: reduce/reduce conflict \(state \d+\) between rule E#\d+ \(defined on line 10\) and rule F#\d+ \(defined on line 11\)}
|
||||||
end
|
end
|
||||||
|
|
||||||
it "provides matched text to user code blocks" do
|
it "provides matched text to user code blocks" do
|
||||||
@ -845,6 +946,280 @@ EOF
|
|||||||
expect(results.stderr).to eq ""
|
expect(results.stderr).to eq ""
|
||||||
expect(results.status).to eq 0
|
expect(results.status).to eq 0
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "supports AST node prefix and suffix" do
|
||||||
|
write_grammar <<EOF
|
||||||
|
ast;
|
||||||
|
ast_prefix P ;
|
||||||
|
ast_suffix S;
|
||||||
|
|
||||||
|
ptype int;
|
||||||
|
|
||||||
|
token a << $$ = 11; >>
|
||||||
|
token b << $$ = 22; >>
|
||||||
|
token one /1/;
|
||||||
|
token two /2/;
|
||||||
|
token comma /,/ <<
|
||||||
|
$$ = 42;
|
||||||
|
>>
|
||||||
|
token lparen /\\(/;
|
||||||
|
token rparen /\\)/;
|
||||||
|
drop /\\s+/;
|
||||||
|
|
||||||
|
Start -> Items;
|
||||||
|
|
||||||
|
Items -> Item ItemsMore;
|
||||||
|
Items -> ;
|
||||||
|
|
||||||
|
ItemsMore -> comma Item ItemsMore;
|
||||||
|
ItemsMore -> ;
|
||||||
|
|
||||||
|
Item -> a;
|
||||||
|
Item -> b;
|
||||||
|
Item -> lparen Item rparen;
|
||||||
|
Item -> Dual;
|
||||||
|
|
||||||
|
Dual -> One Two;
|
||||||
|
Dual -> Two One;
|
||||||
|
One -> one;
|
||||||
|
Two -> two;
|
||||||
|
EOF
|
||||||
|
run_propane(language: language)
|
||||||
|
compile("spec/test_ast_ps.#{language}", language: language)
|
||||||
|
results = run_test
|
||||||
|
expect(results.stderr).to eq ""
|
||||||
|
expect(results.status).to eq 0
|
||||||
|
end
|
||||||
|
|
||||||
|
it "allows specifying a different start rule" do
|
||||||
|
write_grammar <<EOF
|
||||||
|
token hi;
|
||||||
|
start Top;
|
||||||
|
Top -> hi;
|
||||||
|
EOF
|
||||||
|
run_propane(language: language)
|
||||||
|
compile("spec/test_start_rule.#{language}", language: language)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "allows specifying a different start rule with AST generation" do
|
||||||
|
write_grammar <<EOF
|
||||||
|
ast;
|
||||||
|
token hi;
|
||||||
|
start Top;
|
||||||
|
Top -> hi;
|
||||||
|
EOF
|
||||||
|
run_propane(language: language)
|
||||||
|
compile("spec/test_start_rule_ast.#{language}", language: language)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "allows marking a rule component as optional" do
|
||||||
|
if language == "d"
|
||||||
|
write_grammar <<EOF
|
||||||
|
<<
|
||||||
|
import std.stdio;
|
||||||
|
>>
|
||||||
|
|
||||||
|
ptype int;
|
||||||
|
ptype float = float;
|
||||||
|
ptype string = string;
|
||||||
|
|
||||||
|
token a (float) << $$ = 1.5; >>
|
||||||
|
token b << $$ = 2; >>
|
||||||
|
token c << $$ = 3; >>
|
||||||
|
token d << $$ = 4; >>
|
||||||
|
Start -> a? b R? <<
|
||||||
|
writeln("a: ", $1);
|
||||||
|
writeln("b: ", $2);
|
||||||
|
writeln("R: ", $3);
|
||||||
|
>>
|
||||||
|
R -> c d << $$ = "cd"; >>
|
||||||
|
R (string) -> d c << $$ = "dc"; >>
|
||||||
|
EOF
|
||||||
|
else
|
||||||
|
write_grammar <<EOF
|
||||||
|
<<
|
||||||
|
#include <stdio.h>
|
||||||
|
>>
|
||||||
|
|
||||||
|
ptype int;
|
||||||
|
ptype float = float;
|
||||||
|
ptype string = char *;
|
||||||
|
|
||||||
|
token a (float) << $$ = 1.5; >>
|
||||||
|
token b << $$ = 2; >>
|
||||||
|
token c << $$ = 3; >>
|
||||||
|
token d << $$ = 4; >>
|
||||||
|
Start -> a? b R? <<
|
||||||
|
printf("a: %.1f\\n", $1);
|
||||||
|
printf("b: %d\\n", $2);
|
||||||
|
printf("R: %s\\n", $3 == NULL ? "" : $3);
|
||||||
|
>>
|
||||||
|
R -> c d << $$ = "cd"; >>
|
||||||
|
R (string) -> d c << $$ = "dc"; >>
|
||||||
|
EOF
|
||||||
|
end
|
||||||
|
run_propane(language: language)
|
||||||
|
compile("spec/test_optional_rule_component.#{language}", language: language)
|
||||||
|
results = run_test
|
||||||
|
expect(results.stderr).to eq ""
|
||||||
|
expect(results.status).to eq 0
|
||||||
|
verify_lines(results.stdout, [
|
||||||
|
"a: 0#{language == "d" ? "" : ".0"}",
|
||||||
|
"b: 2",
|
||||||
|
"R: ",
|
||||||
|
"a: 1.5",
|
||||||
|
"b: 2",
|
||||||
|
"R: cd",
|
||||||
|
"a: 1.5",
|
||||||
|
"b: 2",
|
||||||
|
"R: dc",
|
||||||
|
])
|
||||||
|
end
|
||||||
|
|
||||||
|
it "allows marking a rule component as optional in AST generation mode" do
|
||||||
|
if language == "d"
|
||||||
|
write_grammar <<EOF
|
||||||
|
ast;
|
||||||
|
|
||||||
|
<<
|
||||||
|
import std.stdio;
|
||||||
|
>>
|
||||||
|
|
||||||
|
token a;
|
||||||
|
token b;
|
||||||
|
token c;
|
||||||
|
token d;
|
||||||
|
Start -> a? b R?;
|
||||||
|
R -> c d;
|
||||||
|
R -> d c;
|
||||||
|
EOF
|
||||||
|
else
|
||||||
|
write_grammar <<EOF
|
||||||
|
ast;
|
||||||
|
|
||||||
|
<<
|
||||||
|
#include <stdio.h>
|
||||||
|
>>
|
||||||
|
|
||||||
|
token a;
|
||||||
|
token b;
|
||||||
|
token c;
|
||||||
|
token d;
|
||||||
|
Start -> a? b R?;
|
||||||
|
R -> c d;
|
||||||
|
R -> d c;
|
||||||
|
EOF
|
||||||
|
end
|
||||||
|
run_propane(language: language)
|
||||||
|
compile("spec/test_optional_rule_component_ast.#{language}", language: language)
|
||||||
|
results = run_test
|
||||||
|
expect(results.stderr).to eq ""
|
||||||
|
expect(results.status).to eq 0
|
||||||
|
end
|
||||||
|
|
||||||
|
it "stores token and rule positions in AST nodes" do
|
||||||
|
write_grammar <<EOF
|
||||||
|
ast;
|
||||||
|
|
||||||
|
token a;
|
||||||
|
token bb;
|
||||||
|
token c /c(.|\\n)*c/;
|
||||||
|
drop /\\s+/;
|
||||||
|
Start -> T T T;
|
||||||
|
T -> a;
|
||||||
|
T -> bb;
|
||||||
|
T -> c;
|
||||||
|
EOF
|
||||||
|
run_propane(language: language)
|
||||||
|
compile("spec/test_ast_token_positions.#{language}", language: language)
|
||||||
|
results = run_test
|
||||||
|
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;
|
||||||
|
|
||||||
|
token a;
|
||||||
|
token bb;
|
||||||
|
token c /c(.|\\n)*c/;
|
||||||
|
drop /\\s+/;
|
||||||
|
Start -> T Start;
|
||||||
|
Start -> ;
|
||||||
|
T -> a A;
|
||||||
|
A -> bb? c?;
|
||||||
|
EOF
|
||||||
|
run_propane(language: language)
|
||||||
|
compile("spec/test_ast_invalid_positions.#{language}", language: language)
|
||||||
|
results = run_test
|
||||||
|
expect(results.stderr).to eq ""
|
||||||
|
expect(results.status).to eq 0
|
||||||
|
end
|
||||||
|
|
||||||
|
it "allows specifying field aliases in AST mode" do
|
||||||
|
write_grammar <<EOF
|
||||||
|
ast;
|
||||||
|
|
||||||
|
token a;
|
||||||
|
token b;
|
||||||
|
token c;
|
||||||
|
drop /\\s+/;
|
||||||
|
Start -> T:first T:second T:third;
|
||||||
|
T -> a;
|
||||||
|
T -> b;
|
||||||
|
T -> c;
|
||||||
|
EOF
|
||||||
|
run_propane(language: language)
|
||||||
|
compile("spec/test_ast_field_aliases.#{language}", language: language)
|
||||||
|
results = run_test
|
||||||
|
expect(results.stderr).to eq ""
|
||||||
|
expect(results.status).to eq 0
|
||||||
|
end
|
||||||
|
|
||||||
|
it "allows specifying field aliases when AST mode is not enabled" do
|
||||||
|
if language == "d"
|
||||||
|
write_grammar <<EOF
|
||||||
|
<<
|
||||||
|
import std.stdio;
|
||||||
|
>>
|
||||||
|
ptype string;
|
||||||
|
token id /[a-zA-Z_][a-zA-Z0-9_]*/ <<
|
||||||
|
$$ = match;
|
||||||
|
>>
|
||||||
|
drop /\\s+/;
|
||||||
|
Start -> id:first id:second <<
|
||||||
|
writeln("first is ", ${first});
|
||||||
|
writeln("second is ", ${second});
|
||||||
|
>>
|
||||||
|
EOF
|
||||||
|
else
|
||||||
|
write_grammar <<EOF
|
||||||
|
<<
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
>>
|
||||||
|
ptype char const *;
|
||||||
|
token id /[a-zA-Z_][a-zA-Z0-9_]*/ <<
|
||||||
|
char * s = malloc(match_length + 1);
|
||||||
|
strncpy(s, (char const *)match, match_length);
|
||||||
|
s[match_length] = 0;
|
||||||
|
$$ = s;
|
||||||
|
>>
|
||||||
|
drop /\\s+/;
|
||||||
|
Start -> id:first id:second <<
|
||||||
|
printf("first is %s\\n", ${first});
|
||||||
|
printf("second is %s\\n", ${second});
|
||||||
|
>>
|
||||||
|
EOF
|
||||||
|
end
|
||||||
|
run_propane(language: language)
|
||||||
|
compile("spec/test_field_aliases.#{language}", language: language)
|
||||||
|
results = run_test
|
||||||
|
expect(results.stderr).to eq ""
|
||||||
|
expect(results.status).to eq 0
|
||||||
|
expect(results.stdout).to match /first is foo1.*second is bar2/m
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
19
spec/test_ast_field_aliases.c
Normal file
19
spec/test_ast_field_aliases.c
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
#include "testparser.h"
|
||||||
|
#include <assert.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "testutils.h"
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
char const * input = "\na\nb\nc";
|
||||||
|
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_eq(TOKEN_a, start->first->pToken->token);
|
||||||
|
assert_eq(TOKEN_b, start->second->pToken->token);
|
||||||
|
assert_eq(TOKEN_c, start->third->pToken->token);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
21
spec/test_ast_field_aliases.d
Normal file
21
spec/test_ast_field_aliases.d
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
import testparser;
|
||||||
|
import std.stdio;
|
||||||
|
import testutils;
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
unittest
|
||||||
|
{
|
||||||
|
string input = "\na\nb\nc";
|
||||||
|
p_context_t context;
|
||||||
|
p_context_init(&context, input);
|
||||||
|
assert(p_parse(&context) == P_SUCCESS);
|
||||||
|
Start * start = p_result(&context);
|
||||||
|
|
||||||
|
assert_eq(TOKEN_a, start.first.pToken.token);
|
||||||
|
assert_eq(TOKEN_b, start.second.pToken.token);
|
||||||
|
assert_eq(TOKEN_c, start.third.pToken.token);
|
||||||
|
}
|
102
spec/test_ast_invalid_positions.c
Normal file
102
spec/test_ast_invalid_positions.c
Normal file
@ -0,0 +1,102 @@
|
|||||||
|
#include "testparser.h"
|
||||||
|
#include <assert.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "testutils.h"
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
char const * input = "\na\n bb ccc";
|
||||||
|
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_eq(1, start->pT1->pToken->position.row);
|
||||||
|
assert_eq(0, start->pT1->pToken->position.col);
|
||||||
|
assert_eq(1, start->pT1->pToken->end_position.row);
|
||||||
|
assert_eq(0, start->pT1->pToken->end_position.col);
|
||||||
|
assert(p_position_valid(start->pT1->pA->position));
|
||||||
|
assert_eq(2, start->pT1->pA->position.row);
|
||||||
|
assert_eq(2, start->pT1->pA->position.col);
|
||||||
|
assert_eq(2, start->pT1->pA->end_position.row);
|
||||||
|
assert_eq(7, start->pT1->pA->end_position.col);
|
||||||
|
assert_eq(1, start->pT1->position.row);
|
||||||
|
assert_eq(0, start->pT1->position.col);
|
||||||
|
assert_eq(2, start->pT1->end_position.row);
|
||||||
|
assert_eq(7, start->pT1->end_position.col);
|
||||||
|
|
||||||
|
assert_eq(1, start->position.row);
|
||||||
|
assert_eq(0, start->position.col);
|
||||||
|
assert_eq(2, start->end_position.row);
|
||||||
|
assert_eq(7, start->end_position.col);
|
||||||
|
|
||||||
|
input = "a\nbb";
|
||||||
|
p_context_init(&context, (uint8_t const *)input, strlen(input));
|
||||||
|
assert(p_parse(&context) == P_SUCCESS);
|
||||||
|
start = p_result(&context);
|
||||||
|
|
||||||
|
assert_eq(0, start->pT1->pToken->position.row);
|
||||||
|
assert_eq(0, start->pT1->pToken->position.col);
|
||||||
|
assert_eq(0, start->pT1->pToken->end_position.row);
|
||||||
|
assert_eq(0, start->pT1->pToken->end_position.col);
|
||||||
|
assert(p_position_valid(start->pT1->pA->position));
|
||||||
|
assert_eq(1, start->pT1->pA->position.row);
|
||||||
|
assert_eq(0, start->pT1->pA->position.col);
|
||||||
|
assert_eq(1, start->pT1->pA->end_position.row);
|
||||||
|
assert_eq(1, start->pT1->pA->end_position.col);
|
||||||
|
assert_eq(0, start->pT1->position.row);
|
||||||
|
assert_eq(0, start->pT1->position.col);
|
||||||
|
assert_eq(1, start->pT1->end_position.row);
|
||||||
|
assert_eq(1, start->pT1->end_position.col);
|
||||||
|
|
||||||
|
assert_eq(0, start->position.row);
|
||||||
|
assert_eq(0, start->position.col);
|
||||||
|
assert_eq(1, start->end_position.row);
|
||||||
|
assert_eq(1, start->end_position.col);
|
||||||
|
|
||||||
|
input = "a\nc\nc";
|
||||||
|
p_context_init(&context, (uint8_t const *)input, strlen(input));
|
||||||
|
assert(p_parse(&context) == P_SUCCESS);
|
||||||
|
start = p_result(&context);
|
||||||
|
|
||||||
|
assert_eq(0, start->pT1->pToken->position.row);
|
||||||
|
assert_eq(0, start->pT1->pToken->position.col);
|
||||||
|
assert_eq(0, start->pT1->pToken->end_position.row);
|
||||||
|
assert_eq(0, start->pT1->pToken->end_position.col);
|
||||||
|
assert(p_position_valid(start->pT1->pA->position));
|
||||||
|
assert_eq(1, start->pT1->pA->position.row);
|
||||||
|
assert_eq(0, start->pT1->pA->position.col);
|
||||||
|
assert_eq(2, start->pT1->pA->end_position.row);
|
||||||
|
assert_eq(0, start->pT1->pA->end_position.col);
|
||||||
|
assert_eq(0, start->pT1->position.row);
|
||||||
|
assert_eq(0, start->pT1->position.col);
|
||||||
|
assert_eq(2, start->pT1->end_position.row);
|
||||||
|
assert_eq(0, start->pT1->end_position.col);
|
||||||
|
|
||||||
|
assert_eq(0, start->position.row);
|
||||||
|
assert_eq(0, start->position.col);
|
||||||
|
assert_eq(2, start->end_position.row);
|
||||||
|
assert_eq(0, start->end_position.col);
|
||||||
|
|
||||||
|
input = "a";
|
||||||
|
p_context_init(&context, (uint8_t const *)input, strlen(input));
|
||||||
|
assert(p_parse(&context) == P_SUCCESS);
|
||||||
|
start = p_result(&context);
|
||||||
|
|
||||||
|
assert_eq(0, start->pT1->pToken->position.row);
|
||||||
|
assert_eq(0, start->pT1->pToken->position.col);
|
||||||
|
assert_eq(0, start->pT1->pToken->end_position.row);
|
||||||
|
assert_eq(0, start->pT1->pToken->end_position.col);
|
||||||
|
assert(!p_position_valid(start->pT1->pA->position));
|
||||||
|
assert_eq(0, start->pT1->position.row);
|
||||||
|
assert_eq(0, start->pT1->position.col);
|
||||||
|
assert_eq(0, start->pT1->end_position.row);
|
||||||
|
assert_eq(0, start->pT1->end_position.col);
|
||||||
|
|
||||||
|
assert_eq(0, start->position.row);
|
||||||
|
assert_eq(0, start->position.col);
|
||||||
|
assert_eq(0, start->end_position.row);
|
||||||
|
assert_eq(0, start->end_position.col);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
104
spec/test_ast_invalid_positions.d
Normal file
104
spec/test_ast_invalid_positions.d
Normal file
@ -0,0 +1,104 @@
|
|||||||
|
import testparser;
|
||||||
|
import std.stdio;
|
||||||
|
import testutils;
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
unittest
|
||||||
|
{
|
||||||
|
string input = "\na\n bb ccc";
|
||||||
|
p_context_t context;
|
||||||
|
p_context_init(&context, input);
|
||||||
|
assert(p_parse(&context) == P_SUCCESS);
|
||||||
|
Start * start = p_result(&context);
|
||||||
|
|
||||||
|
assert_eq(1, start.pT1.pToken.position.row);
|
||||||
|
assert_eq(0, start.pT1.pToken.position.col);
|
||||||
|
assert_eq(1, start.pT1.pToken.end_position.row);
|
||||||
|
assert_eq(0, start.pT1.pToken.end_position.col);
|
||||||
|
assert(start.pT1.pA.position.valid);
|
||||||
|
assert_eq(2, start.pT1.pA.position.row);
|
||||||
|
assert_eq(2, start.pT1.pA.position.col);
|
||||||
|
assert_eq(2, start.pT1.pA.end_position.row);
|
||||||
|
assert_eq(7, start.pT1.pA.end_position.col);
|
||||||
|
assert_eq(1, start.pT1.position.row);
|
||||||
|
assert_eq(0, start.pT1.position.col);
|
||||||
|
assert_eq(2, start.pT1.end_position.row);
|
||||||
|
assert_eq(7, start.pT1.end_position.col);
|
||||||
|
|
||||||
|
assert_eq(1, start.position.row);
|
||||||
|
assert_eq(0, start.position.col);
|
||||||
|
assert_eq(2, start.end_position.row);
|
||||||
|
assert_eq(7, start.end_position.col);
|
||||||
|
|
||||||
|
input = "a\nbb";
|
||||||
|
p_context_init(&context, input);
|
||||||
|
assert(p_parse(&context) == P_SUCCESS);
|
||||||
|
start = p_result(&context);
|
||||||
|
|
||||||
|
assert_eq(0, start.pT1.pToken.position.row);
|
||||||
|
assert_eq(0, start.pT1.pToken.position.col);
|
||||||
|
assert_eq(0, start.pT1.pToken.end_position.row);
|
||||||
|
assert_eq(0, start.pT1.pToken.end_position.col);
|
||||||
|
assert(start.pT1.pA.position.valid);
|
||||||
|
assert_eq(1, start.pT1.pA.position.row);
|
||||||
|
assert_eq(0, start.pT1.pA.position.col);
|
||||||
|
assert_eq(1, start.pT1.pA.end_position.row);
|
||||||
|
assert_eq(1, start.pT1.pA.end_position.col);
|
||||||
|
assert_eq(0, start.pT1.position.row);
|
||||||
|
assert_eq(0, start.pT1.position.col);
|
||||||
|
assert_eq(1, start.pT1.end_position.row);
|
||||||
|
assert_eq(1, start.pT1.end_position.col);
|
||||||
|
|
||||||
|
assert_eq(0, start.position.row);
|
||||||
|
assert_eq(0, start.position.col);
|
||||||
|
assert_eq(1, start.end_position.row);
|
||||||
|
assert_eq(1, start.end_position.col);
|
||||||
|
|
||||||
|
input = "a\nc\nc";
|
||||||
|
p_context_init(&context, input);
|
||||||
|
assert(p_parse(&context) == P_SUCCESS);
|
||||||
|
start = p_result(&context);
|
||||||
|
|
||||||
|
assert_eq(0, start.pT1.pToken.position.row);
|
||||||
|
assert_eq(0, start.pT1.pToken.position.col);
|
||||||
|
assert_eq(0, start.pT1.pToken.end_position.row);
|
||||||
|
assert_eq(0, start.pT1.pToken.end_position.col);
|
||||||
|
assert(start.pT1.pA.position.valid);
|
||||||
|
assert_eq(1, start.pT1.pA.position.row);
|
||||||
|
assert_eq(0, start.pT1.pA.position.col);
|
||||||
|
assert_eq(2, start.pT1.pA.end_position.row);
|
||||||
|
assert_eq(0, start.pT1.pA.end_position.col);
|
||||||
|
assert_eq(0, start.pT1.position.row);
|
||||||
|
assert_eq(0, start.pT1.position.col);
|
||||||
|
assert_eq(2, start.pT1.end_position.row);
|
||||||
|
assert_eq(0, start.pT1.end_position.col);
|
||||||
|
|
||||||
|
assert_eq(0, start.position.row);
|
||||||
|
assert_eq(0, start.position.col);
|
||||||
|
assert_eq(2, start.end_position.row);
|
||||||
|
assert_eq(0, start.end_position.col);
|
||||||
|
|
||||||
|
input = "a";
|
||||||
|
p_context_init(&context, input);
|
||||||
|
assert(p_parse(&context) == P_SUCCESS);
|
||||||
|
start = p_result(&context);
|
||||||
|
|
||||||
|
assert_eq(0, start.pT1.pToken.position.row);
|
||||||
|
assert_eq(0, start.pT1.pToken.position.col);
|
||||||
|
assert_eq(0, start.pT1.pToken.end_position.row);
|
||||||
|
assert_eq(0, start.pT1.pToken.end_position.col);
|
||||||
|
assert(!start.pT1.pA.position.valid);
|
||||||
|
assert_eq(0, start.pT1.position.row);
|
||||||
|
assert_eq(0, start.pT1.position.col);
|
||||||
|
assert_eq(0, start.pT1.end_position.row);
|
||||||
|
assert_eq(0, start.pT1.end_position.col);
|
||||||
|
|
||||||
|
assert_eq(0, start.position.row);
|
||||||
|
assert_eq(0, start.position.col);
|
||||||
|
assert_eq(0, start.end_position.row);
|
||||||
|
assert_eq(0, start.end_position.col);
|
||||||
|
}
|
55
spec/test_ast_ps.c
Normal file
55
spec/test_ast_ps.c
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
#include "testparser.h"
|
||||||
|
#include <assert.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "testutils.h"
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
char const * input = "a, ((b)), b";
|
||||||
|
p_context_t context;
|
||||||
|
p_context_init(&context, (uint8_t const *)input, strlen(input));
|
||||||
|
assert_eq(P_SUCCESS, p_parse(&context));
|
||||||
|
PStartS * start = p_result(&context);
|
||||||
|
assert(start->pItems1 != NULL);
|
||||||
|
assert(start->pItems != NULL);
|
||||||
|
PItemsS * items = start->pItems;
|
||||||
|
assert(items->pItem != NULL);
|
||||||
|
assert(items->pItem->pToken1 != NULL);
|
||||||
|
assert_eq(TOKEN_a, items->pItem->pToken1->token);
|
||||||
|
assert_eq(11, items->pItem->pToken1->pvalue);
|
||||||
|
assert(items->pItemsMore != NULL);
|
||||||
|
PItemsMoreS * itemsmore = items->pItemsMore;
|
||||||
|
assert(itemsmore->pItem != NULL);
|
||||||
|
assert(itemsmore->pItem->pItem != NULL);
|
||||||
|
assert(itemsmore->pItem->pItem->pItem != NULL);
|
||||||
|
assert(itemsmore->pItem->pItem->pItem->pToken1 != NULL);
|
||||||
|
assert_eq(TOKEN_b, itemsmore->pItem->pItem->pItem->pToken1->token);
|
||||||
|
assert_eq(22, itemsmore->pItem->pItem->pItem->pToken1->pvalue);
|
||||||
|
assert(itemsmore->pItemsMore != NULL);
|
||||||
|
itemsmore = itemsmore->pItemsMore;
|
||||||
|
assert(itemsmore->pItem != NULL);
|
||||||
|
assert(itemsmore->pItem->pToken1 != NULL);
|
||||||
|
assert_eq(TOKEN_b, itemsmore->pItem->pToken1->token);
|
||||||
|
assert_eq(22, itemsmore->pItem->pToken1->pvalue);
|
||||||
|
assert(itemsmore->pItemsMore == NULL);
|
||||||
|
|
||||||
|
input = "";
|
||||||
|
p_context_init(&context, (uint8_t const *)input, strlen(input));
|
||||||
|
assert_eq(P_SUCCESS, p_parse(&context));
|
||||||
|
start = p_result(&context);
|
||||||
|
assert(start->pItems == NULL);
|
||||||
|
|
||||||
|
input = "2 1";
|
||||||
|
p_context_init(&context, (uint8_t const *)input, strlen(input));
|
||||||
|
assert_eq(P_SUCCESS, p_parse(&context));
|
||||||
|
start = p_result(&context);
|
||||||
|
assert(start->pItems != NULL);
|
||||||
|
assert(start->pItems->pItem != NULL);
|
||||||
|
assert(start->pItems->pItem->pDual != NULL);
|
||||||
|
assert(start->pItems->pItem->pDual->pTwo1 != NULL);
|
||||||
|
assert(start->pItems->pItem->pDual->pOne2 != NULL);
|
||||||
|
assert(start->pItems->pItem->pDual->pTwo2 == NULL);
|
||||||
|
assert(start->pItems->pItem->pDual->pOne1 == NULL);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
57
spec/test_ast_ps.d
Normal file
57
spec/test_ast_ps.d
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
import testparser;
|
||||||
|
import std.stdio;
|
||||||
|
import testutils;
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
unittest
|
||||||
|
{
|
||||||
|
string input = "a, ((b)), b";
|
||||||
|
p_context_t context;
|
||||||
|
p_context_init(&context, input);
|
||||||
|
assert_eq(P_SUCCESS, p_parse(&context));
|
||||||
|
PStartS * start = p_result(&context);
|
||||||
|
assert(start.pItems1 !is null);
|
||||||
|
assert(start.pItems !is null);
|
||||||
|
PItemsS * items = start.pItems;
|
||||||
|
assert(items.pItem !is null);
|
||||||
|
assert(items.pItem.pToken1 !is null);
|
||||||
|
assert_eq(TOKEN_a, items.pItem.pToken1.token);
|
||||||
|
assert_eq(11, items.pItem.pToken1.pvalue);
|
||||||
|
assert(items.pItemsMore !is null);
|
||||||
|
PItemsMoreS * itemsmore = items.pItemsMore;
|
||||||
|
assert(itemsmore.pItem !is null);
|
||||||
|
assert(itemsmore.pItem.pItem !is null);
|
||||||
|
assert(itemsmore.pItem.pItem.pItem !is null);
|
||||||
|
assert(itemsmore.pItem.pItem.pItem.pToken1 !is null);
|
||||||
|
assert_eq(TOKEN_b, itemsmore.pItem.pItem.pItem.pToken1.token);
|
||||||
|
assert_eq(22, itemsmore.pItem.pItem.pItem.pToken1.pvalue);
|
||||||
|
assert(itemsmore.pItemsMore !is null);
|
||||||
|
itemsmore = itemsmore.pItemsMore;
|
||||||
|
assert(itemsmore.pItem !is null);
|
||||||
|
assert(itemsmore.pItem.pToken1 !is null);
|
||||||
|
assert_eq(TOKEN_b, itemsmore.pItem.pToken1.token);
|
||||||
|
assert_eq(22, itemsmore.pItem.pToken1.pvalue);
|
||||||
|
assert(itemsmore.pItemsMore is null);
|
||||||
|
|
||||||
|
input = "";
|
||||||
|
p_context_init(&context, input);
|
||||||
|
assert_eq(P_SUCCESS, p_parse(&context));
|
||||||
|
start = p_result(&context);
|
||||||
|
assert(start.pItems is null);
|
||||||
|
|
||||||
|
input = "2 1";
|
||||||
|
p_context_init(&context, input);
|
||||||
|
assert_eq(P_SUCCESS, p_parse(&context));
|
||||||
|
start = p_result(&context);
|
||||||
|
assert(start.pItems !is null);
|
||||||
|
assert(start.pItems.pItem !is null);
|
||||||
|
assert(start.pItems.pItem.pDual !is null);
|
||||||
|
assert(start.pItems.pItem.pDual.pTwo1 !is null);
|
||||||
|
assert(start.pItems.pItem.pDual.pOne2 !is null);
|
||||||
|
assert(start.pItems.pItem.pDual.pTwo2 is null);
|
||||||
|
assert(start.pItems.pItem.pDual.pOne1 is null);
|
||||||
|
}
|
84
spec/test_ast_token_positions.c
Normal file
84
spec/test_ast_token_positions.c
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
#include "testparser.h"
|
||||||
|
#include <assert.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "testutils.h"
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
char const * input = "abbccc";
|
||||||
|
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_eq(0, start->pT1->pToken->position.row);
|
||||||
|
assert_eq(0, start->pT1->pToken->position.col);
|
||||||
|
assert_eq(0, start->pT1->pToken->end_position.row);
|
||||||
|
assert_eq(0, start->pT1->pToken->end_position.col);
|
||||||
|
assert_eq(0, start->pT1->position.row);
|
||||||
|
assert_eq(0, start->pT1->position.col);
|
||||||
|
assert_eq(0, start->pT1->end_position.row);
|
||||||
|
assert_eq(0, start->pT1->end_position.col);
|
||||||
|
|
||||||
|
assert_eq(0, start->pT2->pToken->position.row);
|
||||||
|
assert_eq(1, start->pT2->pToken->position.col);
|
||||||
|
assert_eq(0, start->pT2->pToken->end_position.row);
|
||||||
|
assert_eq(2, start->pT2->pToken->end_position.col);
|
||||||
|
assert_eq(0, start->pT2->position.row);
|
||||||
|
assert_eq(1, start->pT2->position.col);
|
||||||
|
assert_eq(0, start->pT2->end_position.row);
|
||||||
|
assert_eq(2, start->pT2->end_position.col);
|
||||||
|
|
||||||
|
assert_eq(0, start->pT3->pToken->position.row);
|
||||||
|
assert_eq(3, start->pT3->pToken->position.col);
|
||||||
|
assert_eq(0, start->pT3->pToken->end_position.row);
|
||||||
|
assert_eq(5, start->pT3->pToken->end_position.col);
|
||||||
|
assert_eq(0, start->pT3->position.row);
|
||||||
|
assert_eq(3, start->pT3->position.col);
|
||||||
|
assert_eq(0, start->pT3->end_position.row);
|
||||||
|
assert_eq(5, start->pT3->end_position.col);
|
||||||
|
|
||||||
|
assert_eq(0, start->position.row);
|
||||||
|
assert_eq(0, start->position.col);
|
||||||
|
assert_eq(0, start->end_position.row);
|
||||||
|
assert_eq(5, start->end_position.col);
|
||||||
|
|
||||||
|
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);
|
||||||
|
start = p_result(&context);
|
||||||
|
|
||||||
|
assert_eq(2, start->pT1->pToken->position.row);
|
||||||
|
assert_eq(2, start->pT1->pToken->position.col);
|
||||||
|
assert_eq(2, start->pT1->pToken->end_position.row);
|
||||||
|
assert_eq(3, start->pT1->pToken->end_position.col);
|
||||||
|
assert_eq(2, start->pT1->position.row);
|
||||||
|
assert_eq(2, start->pT1->position.col);
|
||||||
|
assert_eq(2, start->pT1->end_position.row);
|
||||||
|
assert_eq(3, start->pT1->end_position.col);
|
||||||
|
|
||||||
|
assert_eq(3, start->pT2->pToken->position.row);
|
||||||
|
assert_eq(0, start->pT2->pToken->position.col);
|
||||||
|
assert_eq(4, start->pT2->pToken->end_position.row);
|
||||||
|
assert_eq(1, start->pT2->pToken->end_position.col);
|
||||||
|
assert_eq(3, start->pT2->position.row);
|
||||||
|
assert_eq(0, start->pT2->position.col);
|
||||||
|
assert_eq(4, start->pT2->end_position.row);
|
||||||
|
assert_eq(1, start->pT2->end_position.col);
|
||||||
|
|
||||||
|
assert_eq(6, start->pT3->pToken->position.row);
|
||||||
|
assert_eq(5, start->pT3->pToken->position.col);
|
||||||
|
assert_eq(6, start->pT3->pToken->end_position.row);
|
||||||
|
assert_eq(5, start->pT3->pToken->end_position.col);
|
||||||
|
assert_eq(6, start->pT3->position.row);
|
||||||
|
assert_eq(5, start->pT3->position.col);
|
||||||
|
assert_eq(6, start->pT3->end_position.row);
|
||||||
|
assert_eq(5, start->pT3->end_position.col);
|
||||||
|
|
||||||
|
assert_eq(2, start->position.row);
|
||||||
|
assert_eq(2, start->position.col);
|
||||||
|
assert_eq(6, start->end_position.row);
|
||||||
|
assert_eq(5, start->end_position.col);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
86
spec/test_ast_token_positions.d
Normal file
86
spec/test_ast_token_positions.d
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
import testparser;
|
||||||
|
import std.stdio;
|
||||||
|
import testutils;
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
unittest
|
||||||
|
{
|
||||||
|
string input = "abbccc";
|
||||||
|
p_context_t context;
|
||||||
|
p_context_init(&context, input);
|
||||||
|
assert(p_parse(&context) == P_SUCCESS);
|
||||||
|
Start * start = p_result(&context);
|
||||||
|
|
||||||
|
assert_eq(0, start.pT1.pToken.position.row);
|
||||||
|
assert_eq(0, start.pT1.pToken.position.col);
|
||||||
|
assert_eq(0, start.pT1.pToken.end_position.row);
|
||||||
|
assert_eq(0, start.pT1.pToken.end_position.col);
|
||||||
|
assert_eq(0, start.pT1.position.row);
|
||||||
|
assert_eq(0, start.pT1.position.col);
|
||||||
|
assert_eq(0, start.pT1.end_position.row);
|
||||||
|
assert_eq(0, start.pT1.end_position.col);
|
||||||
|
|
||||||
|
assert_eq(0, start.pT2.pToken.position.row);
|
||||||
|
assert_eq(1, start.pT2.pToken.position.col);
|
||||||
|
assert_eq(0, start.pT2.pToken.end_position.row);
|
||||||
|
assert_eq(2, start.pT2.pToken.end_position.col);
|
||||||
|
assert_eq(0, start.pT2.position.row);
|
||||||
|
assert_eq(1, start.pT2.position.col);
|
||||||
|
assert_eq(0, start.pT2.end_position.row);
|
||||||
|
assert_eq(2, start.pT2.end_position.col);
|
||||||
|
|
||||||
|
assert_eq(0, start.pT3.pToken.position.row);
|
||||||
|
assert_eq(3, start.pT3.pToken.position.col);
|
||||||
|
assert_eq(0, start.pT3.pToken.end_position.row);
|
||||||
|
assert_eq(5, start.pT3.pToken.end_position.col);
|
||||||
|
assert_eq(0, start.pT3.position.row);
|
||||||
|
assert_eq(3, start.pT3.position.col);
|
||||||
|
assert_eq(0, start.pT3.end_position.row);
|
||||||
|
assert_eq(5, start.pT3.end_position.col);
|
||||||
|
|
||||||
|
assert_eq(0, start.position.row);
|
||||||
|
assert_eq(0, start.position.col);
|
||||||
|
assert_eq(0, start.end_position.row);
|
||||||
|
assert_eq(5, start.end_position.col);
|
||||||
|
|
||||||
|
input = "\n\n bb\nc\ncc\n\n a";
|
||||||
|
p_context_init(&context, input);
|
||||||
|
assert(p_parse(&context) == P_SUCCESS);
|
||||||
|
start = p_result(&context);
|
||||||
|
|
||||||
|
assert_eq(2, start.pT1.pToken.position.row);
|
||||||
|
assert_eq(2, start.pT1.pToken.position.col);
|
||||||
|
assert_eq(2, start.pT1.pToken.end_position.row);
|
||||||
|
assert_eq(3, start.pT1.pToken.end_position.col);
|
||||||
|
assert_eq(2, start.pT1.position.row);
|
||||||
|
assert_eq(2, start.pT1.position.col);
|
||||||
|
assert_eq(2, start.pT1.end_position.row);
|
||||||
|
assert_eq(3, start.pT1.end_position.col);
|
||||||
|
|
||||||
|
assert_eq(3, start.pT2.pToken.position.row);
|
||||||
|
assert_eq(0, start.pT2.pToken.position.col);
|
||||||
|
assert_eq(4, start.pT2.pToken.end_position.row);
|
||||||
|
assert_eq(1, start.pT2.pToken.end_position.col);
|
||||||
|
assert_eq(3, start.pT2.position.row);
|
||||||
|
assert_eq(0, start.pT2.position.col);
|
||||||
|
assert_eq(4, start.pT2.end_position.row);
|
||||||
|
assert_eq(1, start.pT2.end_position.col);
|
||||||
|
|
||||||
|
assert_eq(6, start.pT3.pToken.position.row);
|
||||||
|
assert_eq(5, start.pT3.pToken.position.col);
|
||||||
|
assert_eq(6, start.pT3.pToken.end_position.row);
|
||||||
|
assert_eq(5, start.pT3.pToken.end_position.col);
|
||||||
|
assert_eq(6, start.pT3.position.row);
|
||||||
|
assert_eq(5, start.pT3.position.col);
|
||||||
|
assert_eq(6, start.pT3.end_position.row);
|
||||||
|
assert_eq(5, start.pT3.end_position.col);
|
||||||
|
|
||||||
|
assert_eq(2, start.position.row);
|
||||||
|
assert_eq(2, start.position.col);
|
||||||
|
assert_eq(6, start.end_position.row);
|
||||||
|
assert_eq(5, start.end_position.col);
|
||||||
|
}
|
13
spec/test_field_aliases.c
Normal file
13
spec/test_field_aliases.c
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
#include "testparser.h"
|
||||||
|
#include <assert.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "testutils.h"
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
char const * input = "foo1\nbar2";
|
||||||
|
p_context_t context;
|
||||||
|
p_context_init(&context, (uint8_t const *)input, strlen(input));
|
||||||
|
assert(p_parse(&context) == P_SUCCESS);
|
||||||
|
return 0;
|
||||||
|
}
|
15
spec/test_field_aliases.d
Normal file
15
spec/test_field_aliases.d
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
import testparser;
|
||||||
|
import std.stdio;
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
unittest
|
||||||
|
{
|
||||||
|
string input = "foo1\nbar2";
|
||||||
|
p_context_t context;
|
||||||
|
p_context_init(&context, input);
|
||||||
|
assert(p_parse(&context) == P_SUCCESS);
|
||||||
|
}
|
@ -43,41 +43,57 @@ int main()
|
|||||||
assert(p_lex(&context, &token_info) == P_SUCCESS);
|
assert(p_lex(&context, &token_info) == P_SUCCESS);
|
||||||
assert(token_info.position.row == 0u);
|
assert(token_info.position.row == 0u);
|
||||||
assert(token_info.position.col == 0u);
|
assert(token_info.position.col == 0u);
|
||||||
|
assert(token_info.end_position.row == 0u);
|
||||||
|
assert(token_info.end_position.col == 0u);
|
||||||
assert(token_info.length == 1u);
|
assert(token_info.length == 1u);
|
||||||
assert(token_info.token == TOKEN_int);
|
assert(token_info.token == TOKEN_int);
|
||||||
assert(p_lex(&context, &token_info) == P_SUCCESS);
|
assert(p_lex(&context, &token_info) == P_SUCCESS);
|
||||||
assert(token_info.position.row == 0u);
|
assert(token_info.position.row == 0u);
|
||||||
assert(token_info.position.col == 2u);
|
assert(token_info.position.col == 2u);
|
||||||
|
assert(token_info.end_position.row == 0u);
|
||||||
|
assert(token_info.end_position.col == 2u);
|
||||||
assert(token_info.length == 1u);
|
assert(token_info.length == 1u);
|
||||||
assert(token_info.token == TOKEN_plus);
|
assert(token_info.token == TOKEN_plus);
|
||||||
assert(p_lex(&context, &token_info) == P_SUCCESS);
|
assert(p_lex(&context, &token_info) == P_SUCCESS);
|
||||||
assert(token_info.position.row == 0u);
|
assert(token_info.position.row == 0u);
|
||||||
assert(token_info.position.col == 4u);
|
assert(token_info.position.col == 4u);
|
||||||
|
assert(token_info.end_position.row == 0u);
|
||||||
|
assert(token_info.end_position.col == 4u);
|
||||||
assert(token_info.length == 1u);
|
assert(token_info.length == 1u);
|
||||||
assert(token_info.token == TOKEN_int);
|
assert(token_info.token == TOKEN_int);
|
||||||
assert(p_lex(&context, &token_info) == P_SUCCESS);
|
assert(p_lex(&context, &token_info) == P_SUCCESS);
|
||||||
assert(token_info.position.row == 0u);
|
assert(token_info.position.row == 0u);
|
||||||
assert(token_info.position.col == 6u);
|
assert(token_info.position.col == 6u);
|
||||||
|
assert(token_info.end_position.row == 0u);
|
||||||
|
assert(token_info.end_position.col == 6u);
|
||||||
assert(token_info.length == 1u);
|
assert(token_info.length == 1u);
|
||||||
assert(token_info.token == TOKEN_times);
|
assert(token_info.token == TOKEN_times);
|
||||||
assert(p_lex(&context, &token_info) == P_SUCCESS);
|
assert(p_lex(&context, &token_info) == P_SUCCESS);
|
||||||
assert(token_info.position.row == 1u);
|
assert(token_info.position.row == 1u);
|
||||||
assert(token_info.position.col == 0u);
|
assert(token_info.position.col == 0u);
|
||||||
|
assert(token_info.end_position.row == 1u);
|
||||||
|
assert(token_info.end_position.col == 2u);
|
||||||
assert(token_info.length == 3u);
|
assert(token_info.length == 3u);
|
||||||
assert(token_info.token == TOKEN_int);
|
assert(token_info.token == TOKEN_int);
|
||||||
assert(p_lex(&context, &token_info) == P_SUCCESS);
|
assert(p_lex(&context, &token_info) == P_SUCCESS);
|
||||||
assert(token_info.position.row == 1u);
|
assert(token_info.position.row == 1u);
|
||||||
assert(token_info.position.col == 4u);
|
assert(token_info.position.col == 4u);
|
||||||
|
assert(token_info.end_position.row == 1u);
|
||||||
|
assert(token_info.end_position.col == 4u);
|
||||||
assert(token_info.length == 1u);
|
assert(token_info.length == 1u);
|
||||||
assert(token_info.token == TOKEN_plus);
|
assert(token_info.token == TOKEN_plus);
|
||||||
assert(p_lex(&context, &token_info) == P_SUCCESS);
|
assert(p_lex(&context, &token_info) == P_SUCCESS);
|
||||||
assert(token_info.position.row == 1u);
|
assert(token_info.position.row == 1u);
|
||||||
assert(token_info.position.col == 6u);
|
assert(token_info.position.col == 6u);
|
||||||
|
assert(token_info.end_position.row == 1u);
|
||||||
|
assert(token_info.end_position.col == 8u);
|
||||||
assert(token_info.length == 3u);
|
assert(token_info.length == 3u);
|
||||||
assert(token_info.token == TOKEN_int);
|
assert(token_info.token == TOKEN_int);
|
||||||
assert(p_lex(&context, &token_info) == P_SUCCESS);
|
assert(p_lex(&context, &token_info) == P_SUCCESS);
|
||||||
assert(token_info.position.row == 1u);
|
assert(token_info.position.row == 1u);
|
||||||
assert(token_info.position.col == 9u);
|
assert(token_info.position.col == 9u);
|
||||||
|
assert(token_info.end_position.row == 1u);
|
||||||
|
assert(token_info.end_position.col == 9u);
|
||||||
assert(token_info.length == 0u);
|
assert(token_info.length == 0u);
|
||||||
assert(token_info.token == TOKEN___EOF);
|
assert(token_info.token == TOKEN___EOF);
|
||||||
|
|
||||||
@ -85,6 +101,8 @@ int main()
|
|||||||
assert(p_lex(&context, &token_info) == P_SUCCESS);
|
assert(p_lex(&context, &token_info) == P_SUCCESS);
|
||||||
assert(token_info.position.row == 0u);
|
assert(token_info.position.row == 0u);
|
||||||
assert(token_info.position.col == 0u);
|
assert(token_info.position.col == 0u);
|
||||||
|
assert(token_info.end_position.row == 0u);
|
||||||
|
assert(token_info.end_position.col == 0u);
|
||||||
assert(token_info.length == 0u);
|
assert(token_info.length == 0u);
|
||||||
assert(token_info.token == TOKEN___EOF);
|
assert(token_info.token == TOKEN___EOF);
|
||||||
|
|
||||||
|
@ -47,23 +47,23 @@ unittest
|
|||||||
p_context_t context;
|
p_context_t context;
|
||||||
p_context_init(&context, input);
|
p_context_init(&context, input);
|
||||||
assert(p_lex(&context, &token_info) == P_SUCCESS);
|
assert(p_lex(&context, &token_info) == P_SUCCESS);
|
||||||
assert(token_info == p_token_info_t(p_position_t(0, 0), 1, TOKEN_int));
|
assert(token_info == p_token_info_t(p_position_t(0, 0), p_position_t(0, 0), 1, TOKEN_int));
|
||||||
assert(p_lex(&context, &token_info) == P_SUCCESS);
|
assert(p_lex(&context, &token_info) == P_SUCCESS);
|
||||||
assert(token_info == p_token_info_t(p_position_t(0, 2), 1, TOKEN_plus));
|
assert(token_info == p_token_info_t(p_position_t(0, 2), p_position_t(0, 2), 1, TOKEN_plus));
|
||||||
assert(p_lex(&context, &token_info) == P_SUCCESS);
|
assert(p_lex(&context, &token_info) == P_SUCCESS);
|
||||||
assert(token_info == p_token_info_t(p_position_t(0, 4), 1, TOKEN_int));
|
assert(token_info == p_token_info_t(p_position_t(0, 4), p_position_t(0, 4), 1, TOKEN_int));
|
||||||
assert(p_lex(&context, &token_info) == P_SUCCESS);
|
assert(p_lex(&context, &token_info) == P_SUCCESS);
|
||||||
assert(token_info == p_token_info_t(p_position_t(0, 6), 1, TOKEN_times));
|
assert(token_info == p_token_info_t(p_position_t(0, 6), p_position_t(0, 6), 1, TOKEN_times));
|
||||||
assert(p_lex(&context, &token_info) == P_SUCCESS);
|
assert(p_lex(&context, &token_info) == P_SUCCESS);
|
||||||
assert(token_info == p_token_info_t(p_position_t(1, 0), 3, TOKEN_int));
|
assert(token_info == p_token_info_t(p_position_t(1, 0), p_position_t(1, 2), 3, TOKEN_int));
|
||||||
assert(p_lex(&context, &token_info) == P_SUCCESS);
|
assert(p_lex(&context, &token_info) == P_SUCCESS);
|
||||||
assert(token_info == p_token_info_t(p_position_t(1, 4), 1, TOKEN_plus));
|
assert(token_info == p_token_info_t(p_position_t(1, 4), p_position_t(1, 4), 1, TOKEN_plus));
|
||||||
assert(p_lex(&context, &token_info) == P_SUCCESS);
|
assert(p_lex(&context, &token_info) == P_SUCCESS);
|
||||||
assert(token_info == p_token_info_t(p_position_t(1, 6), 3, TOKEN_int));
|
assert(token_info == p_token_info_t(p_position_t(1, 6), p_position_t(1, 8), 3, TOKEN_int));
|
||||||
assert(p_lex(&context, &token_info) == P_SUCCESS);
|
assert(p_lex(&context, &token_info) == P_SUCCESS);
|
||||||
assert(token_info == p_token_info_t(p_position_t(1, 9), 0, TOKEN___EOF));
|
assert(token_info == p_token_info_t(p_position_t(1, 9), p_position_t(1, 9), 0, TOKEN___EOF));
|
||||||
|
|
||||||
p_context_init(&context, "");
|
p_context_init(&context, "");
|
||||||
assert(p_lex(&context, &token_info) == P_SUCCESS);
|
assert(p_lex(&context, &token_info) == P_SUCCESS);
|
||||||
assert(token_info == p_token_info_t(p_position_t(0, 0), 0, TOKEN___EOF));
|
assert(token_info == p_token_info_t(p_position_t(0, 0), p_position_t(0, 0), 0, TOKEN___EOF));
|
||||||
}
|
}
|
||||||
|
22
spec/test_optional_rule_component.c
Normal file
22
spec/test_optional_rule_component.c
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
#include "testparser.h"
|
||||||
|
#include <assert.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
char const * input = "b";
|
||||||
|
p_context_t context;
|
||||||
|
p_context_init(&context, (uint8_t const *)input, strlen(input));
|
||||||
|
assert(p_parse(&context) == P_SUCCESS);
|
||||||
|
|
||||||
|
input = "abcd";
|
||||||
|
p_context_init(&context, (uint8_t const *)input, strlen(input));
|
||||||
|
assert(p_parse(&context) == P_SUCCESS);
|
||||||
|
|
||||||
|
input = "abdc";
|
||||||
|
p_context_init(&context, (uint8_t const *)input, strlen(input));
|
||||||
|
assert(p_parse(&context) == P_SUCCESS);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
23
spec/test_optional_rule_component.d
Normal file
23
spec/test_optional_rule_component.d
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
import testparser;
|
||||||
|
import std.stdio;
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
unittest
|
||||||
|
{
|
||||||
|
string input = "b";
|
||||||
|
p_context_t context;
|
||||||
|
p_context_init(&context, input);
|
||||||
|
assert(p_parse(&context) == P_SUCCESS);
|
||||||
|
|
||||||
|
input = "abcd";
|
||||||
|
p_context_init(&context, input);
|
||||||
|
assert(p_parse(&context) == P_SUCCESS);
|
||||||
|
|
||||||
|
input = "abdc";
|
||||||
|
p_context_init(&context, input);
|
||||||
|
assert(p_parse(&context) == P_SUCCESS);
|
||||||
|
}
|
42
spec/test_optional_rule_component_ast.c
Normal file
42
spec/test_optional_rule_component_ast.c
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
#include "testparser.h"
|
||||||
|
#include <assert.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "testutils.h"
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
char const * input = "b";
|
||||||
|
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(start->pToken1 == NULL);
|
||||||
|
assert(start->pToken2 != NULL);
|
||||||
|
assert_eq(TOKEN_b, start->pToken2->token);
|
||||||
|
assert(start->pR3 == NULL);
|
||||||
|
assert(start->pR == NULL);
|
||||||
|
|
||||||
|
input = "abcd";
|
||||||
|
p_context_init(&context, (uint8_t const *)input, strlen(input));
|
||||||
|
assert(p_parse(&context) == P_SUCCESS);
|
||||||
|
start = p_result(&context);
|
||||||
|
assert(start->pToken1 != NULL);
|
||||||
|
assert_eq(TOKEN_a, start->pToken1->token);
|
||||||
|
assert(start->pToken2 != NULL);
|
||||||
|
assert(start->pR3 != NULL);
|
||||||
|
assert(start->pR != NULL);
|
||||||
|
assert(start->pR == start->pR3);
|
||||||
|
assert_eq(TOKEN_c, start->pR->pToken1->token);
|
||||||
|
|
||||||
|
input = "bdc";
|
||||||
|
p_context_init(&context, (uint8_t const *)input, strlen(input));
|
||||||
|
assert(p_parse(&context) == P_SUCCESS);
|
||||||
|
start = p_result(&context);
|
||||||
|
assert(start->pToken1 == NULL);
|
||||||
|
assert(start->pToken2 != NULL);
|
||||||
|
assert(start->pR != NULL);
|
||||||
|
assert_eq(TOKEN_d, start->pR->pToken1->token);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
43
spec/test_optional_rule_component_ast.d
Normal file
43
spec/test_optional_rule_component_ast.d
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
import testparser;
|
||||||
|
import std.stdio;
|
||||||
|
import testutils;
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
unittest
|
||||||
|
{
|
||||||
|
string input = "b";
|
||||||
|
p_context_t context;
|
||||||
|
p_context_init(&context, input);
|
||||||
|
assert(p_parse(&context) == P_SUCCESS);
|
||||||
|
Start * start = p_result(&context);
|
||||||
|
assert(start.pToken1 is null);
|
||||||
|
assert(start.pToken2 !is null);
|
||||||
|
assert_eq(TOKEN_b, start.pToken2.token);
|
||||||
|
assert(start.pR3 is null);
|
||||||
|
assert(start.pR is null);
|
||||||
|
|
||||||
|
input = "abcd";
|
||||||
|
p_context_init(&context, input);
|
||||||
|
assert(p_parse(&context) == P_SUCCESS);
|
||||||
|
start = p_result(&context);
|
||||||
|
assert(start.pToken1 != null);
|
||||||
|
assert_eq(TOKEN_a, start.pToken1.token);
|
||||||
|
assert(start.pToken2 != null);
|
||||||
|
assert(start.pR3 != null);
|
||||||
|
assert(start.pR != null);
|
||||||
|
assert(start.pR == start.pR3);
|
||||||
|
assert_eq(TOKEN_c, start.pR.pToken1.token);
|
||||||
|
|
||||||
|
input = "bdc";
|
||||||
|
p_context_init(&context, input);
|
||||||
|
assert(p_parse(&context) == P_SUCCESS);
|
||||||
|
start = p_result(&context);
|
||||||
|
assert(start.pToken1 is null);
|
||||||
|
assert(start.pToken2 !is null);
|
||||||
|
assert(start.pR !is null);
|
||||||
|
assert_eq(TOKEN_d, start.pR.pToken1.token);
|
||||||
|
}
|
9
spec/test_start_rule.c
Normal file
9
spec/test_start_rule.c
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
#include "testparser.h"
|
||||||
|
#include <assert.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "testutils.h"
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
8
spec/test_start_rule.d
Normal file
8
spec/test_start_rule.d
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
import testparser;
|
||||||
|
import std.stdio;
|
||||||
|
import testutils;
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
17
spec/test_start_rule_ast.c
Normal file
17
spec/test_start_rule_ast.c
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
#include "testparser.h"
|
||||||
|
#include <assert.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "testutils.h"
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
char const * input = "hi";
|
||||||
|
p_context_t context;
|
||||||
|
p_context_init(&context, (uint8_t const *)input, strlen(input));
|
||||||
|
assert_eq(P_SUCCESS, p_parse(&context));
|
||||||
|
Top * top = p_result(&context);
|
||||||
|
assert(top->pToken != NULL);
|
||||||
|
assert_eq(TOKEN_hi, top->pToken->token);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
19
spec/test_start_rule_ast.d
Normal file
19
spec/test_start_rule_ast.d
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
import testparser;
|
||||||
|
import std.stdio;
|
||||||
|
import testutils;
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
unittest
|
||||||
|
{
|
||||||
|
string input = "hi";
|
||||||
|
p_context_t context;
|
||||||
|
p_context_init(&context, input);
|
||||||
|
assert_eq(P_SUCCESS, p_parse(&context));
|
||||||
|
Top * top = p_result(&context);
|
||||||
|
assert(top.pToken !is null);
|
||||||
|
assert_eq(TOKEN_hi, top.pToken.token);
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user