Move lr_parser implementation into a separate .c file

This commit is contained in:
Max Brunsfeld 2014-06-04 13:34:37 -07:00
parent 63cde3967c
commit 9a4889176e
5 changed files with 371 additions and 246 deletions

View file

@ -16,8 +16,6 @@ extern "C" {
#define SYMBOL_NAMES \
static const char *ts_symbol_names[]
SYMBOL_NAMES;
#define HIDDEN_SYMBOLS \
static const int hidden_symbol_flags[SYMBOL_COUNT]
@ -27,9 +25,19 @@ static const int ubiquitous_symbol_flags[SYMBOL_COUNT]
#define LEX_STATES \
static ts_state_id ts_lex_states[STATE_COUNT]
#define PARSE_TABLE \
static const ts_parse_action ts_parse_actions[STATE_COUNT][SYMBOL_COUNT]
#define LEX_FN() \
static ts_tree * ts_lex(ts_lexer *lexer, ts_state_id lex_state)
#ifdef TS_DEBUG_LEX
#include <stdio.h>
#define DEBUG_LEX(...) fprintf(stderr, "\n" __VA_ARGS__)
#else
#define DEBUG_LEX(...)
#endif
#define START_LEXER() \
DEBUG_LEX("LEX %d", lex_state); \
char lookahead; \
@ -52,62 +60,15 @@ ts_lexer_start_token(lexer);
#define LEX_PANIC() \
{ DEBUG_LEX("LEX ERROR: unexpected state %d", lex_state); return NULL; }
#define PARSE_TABLE \
static const ts_parse_action ts_parse_actions[STATE_COUNT][SYMBOL_COUNT]
SYMBOL_NAMES;
#define SHIFT(to_state_value) \
{ .type = ts_parse_action_type_shift, .data = { .to_state = to_state_value } }
#define REDUCE(symbol_val, child_count_val) \
{ .type = ts_parse_action_type_reduce, .data = { .symbol = symbol_val, .child_count = child_count_val } }
#define ACCEPT_INPUT() \
{ .type = ts_parse_action_type_accept }
#ifdef TS_DEBUG_LEX
#include <stdio.h>
#define DEBUG_LEX(...) fprintf(stderr, "\n" __VA_ARGS__)
#else
#define DEBUG_LEX(...)
#endif
#ifdef TS_DEBUG_PARSE
#include <stdio.h>
#define DEBUG_PARSE(...) fprintf(stderr, "\n" __VA_ARGS__)
#else
#define DEBUG_PARSE(...)
#endif
static const ts_tree *
ts_parse(void *data, ts_input input, ts_input_edit *edit) {
static const ts_tree * ts_parse(void *data, ts_input input, ts_input_edit *edit) {
ts_lr_parser *parser = (ts_lr_parser *)data;
ts_lr_parser_initialize(parser, input, edit);
int done = 0;
while (!done) {
ts_parse_action action = ts_lr_parser_next_action(parser);
DEBUG_PARSE("LOOKAHEAD %s", ts_symbol_names[ts_tree_symbol(parser->lookahead)]);
switch (action.type) {
case ts_parse_action_type_shift:
DEBUG_PARSE("SHIFT %d", action.data.to_state);
ts_lr_parser_shift(parser, action.data.to_state);
break;
case ts_parse_action_type_reduce:
DEBUG_PARSE("REDUCE %s %d", ts_symbol_names[action.data.symbol], action.data.child_count);
ts_lr_parser_reduce(parser, action.data.symbol, action.data.child_count);
break;
case ts_parse_action_type_accept:
DEBUG_PARSE("ACCEPT");
done = 1;
break;
case ts_parse_action_type_error:
DEBUG_PARSE("ERROR");
done = !ts_lr_parser_handle_error(parser);
break;
}
for (;;) {
ts_tree *tree = ts_lr_parser_parse(parser, ts_symbol_names);
if (tree) return tree;
}
return ts_lr_parser_tree_root(parser);
}
#define EXPORT_PARSER(constructor_name) \