diff --git a/examples/parsers/arithmetic.c b/examples/parsers/arithmetic.c index 8b8fc5c5..5c4c41df 100644 --- a/examples/parsers/arithmetic.c +++ b/examples/parsers/arithmetic.c @@ -1,7 +1,7 @@ #include "tree_sitter/parser.h" -STATE_COUNT = 134; -SYMBOL_COUNT = 21; +#define STATE_COUNT 134 +#define SYMBOL_COUNT 21 enum { ts_sym__operand1 = 2, @@ -549,7 +549,7 @@ PARSE_TABLE = { [1] = { [ts_aux_sym_token3] = SHIFT(2), [ts_aux_sym_token4] = SHIFT(100), - [ts_builtin_sym_end] = REDUCE(ts_sym_sum, 1), + [ts_builtin_sym_end] = REDUCE(ts_sym_difference, 1), }, [2] = { [ts_sym__operand1] = SHIFT(3), @@ -617,7 +617,7 @@ PARSE_TABLE = { [ts_builtin_sym_error] = SHIFT(78), }, [12] = { - [ts_aux_sym_token2] = REDUCE(ts_sym_sum, 1), + [ts_aux_sym_token2] = REDUCE(ts_sym_difference, 1), [ts_aux_sym_token3] = SHIFT(13), [ts_aux_sym_token4] = SHIFT(76), }, @@ -689,7 +689,7 @@ PARSE_TABLE = { [23] = { [ts_aux_sym_token2] = REDUCE(ts_sym_product, 1), [ts_aux_sym_token3] = REDUCE(ts_sym_product, 1), - [ts_aux_sym_token4] = REDUCE(ts_sym_product, 1), + [ts_aux_sym_token4] = REDUCE(ts_sym_quotient, 1), [ts_aux_sym_token5] = SHIFT(24), [ts_aux_sym_token6] = SHIFT(52), }, @@ -1209,10 +1209,10 @@ PARSE_TABLE = { }, [102] = { [ts_aux_sym_token3] = REDUCE(ts_sym_quotient, 1), - [ts_aux_sym_token4] = REDUCE(ts_sym_quotient, 1), + [ts_aux_sym_token4] = REDUCE(ts_sym_product, 1), [ts_aux_sym_token5] = SHIFT(103), [ts_aux_sym_token6] = SHIFT(117), - [ts_builtin_sym_end] = REDUCE(ts_sym_quotient, 1), + [ts_builtin_sym_end] = REDUCE(ts_sym_product, 1), }, [103] = { [ts_sym__operand2] = SHIFT(104), diff --git a/examples/parsers/golang.c b/examples/parsers/golang.c index 42311d0e..498ee540 100644 --- a/examples/parsers/golang.c +++ b/examples/parsers/golang.c @@ -1,7 +1,7 @@ #include "tree_sitter/parser.h" -STATE_COUNT = 196; -SYMBOL_COUNT = 52; +#define STATE_COUNT 196 +#define SYMBOL_COUNT 52 enum { ts_sym__func = 2, diff --git a/examples/parsers/javascript.c b/examples/parsers/javascript.c index d7983d72..4b633d08 100644 --- a/examples/parsers/javascript.c +++ b/examples/parsers/javascript.c @@ -1,7 +1,7 @@ #include "tree_sitter/parser.h" -STATE_COUNT = 604; -SYMBOL_COUNT = 55; +#define STATE_COUNT 604 +#define SYMBOL_COUNT 55 enum { ts_sym__break = 2, diff --git a/examples/parsers/json.c b/examples/parsers/json.c index b5358bec..789c726a 100644 --- a/examples/parsers/json.c +++ b/examples/parsers/json.c @@ -1,7 +1,7 @@ #include "tree_sitter/parser.h" -STATE_COUNT = 60; -SYMBOL_COUNT = 18; +#define STATE_COUNT 60 +#define SYMBOL_COUNT 18 enum { ts_sym_array = 2, diff --git a/include/tree_sitter/parser.h b/include/tree_sitter/parser.h index 1bef260a..a37696fc 100644 --- a/include/tree_sitter/parser.h +++ b/include/tree_sitter/parser.h @@ -31,20 +31,14 @@ extern "C" { #define DEBUG_PARSE(...) #endif -#define SYMBOL_COUNT \ -static const size_t ts_symbol_count - -#define STATE_COUNT \ -static const size_t ts_state_count - #define SYMBOL_NAMES \ -static const char *ts_symbol_names[ts_symbol_count] +static const char *ts_symbol_names[SYMBOL_COUNT] #define HIDDEN_SYMBOLS \ -static const int hidden_symbol_flags[ts_symbol_count] +static const int hidden_symbol_flags[SYMBOL_COUNT] #define LEX_STATES \ -static state_id ts_lex_states[ts_state_count] +static state_id ts_lex_states[STATE_COUNT] #define LEX_FN() \ static ts_tree * ts_lex(ts_lexer *lexer, state_id lex_state) @@ -70,26 +64,26 @@ ts_lexer_start_token(lexer); { DEBUG_LEX("Lex error: unexpected state %d", LEX_STATE()); return NULL; } #define PARSE_TABLE \ -static const ts_parse_action ts_parse_actions[ts_state_count][ts_symbol_count] +static const ts_parse_action ts_parse_actions[STATE_COUNT][SYMBOL_COUNT] #define EXPORT_PARSER(constructor_name) \ ts_parser constructor_name() { \ return (ts_parser) { \ .parse_fn = ts_parse, \ .symbol_names = ts_symbol_names, \ - .data = ts_lr_parser_make(ts_symbol_count, (const ts_parse_action *)ts_parse_actions, ts_lex_states, hidden_symbol_flags), \ + .data = ts_lr_parser_make(SYMBOL_COUNT, (const ts_parse_action *)ts_parse_actions, ts_lex_states, hidden_symbol_flags), \ .free_fn = NULL \ }; \ } #define SHIFT(to_state_value) \ -(ts_parse_action){ .type = ts_parse_action_type_shift, .data = { .to_state = to_state_value } } +{ .type = ts_parse_action_type_shift, .data = { .to_state = to_state_value } } #define REDUCE(symbol_val, child_count_val) \ -(ts_parse_action){ .type = ts_parse_action_type_reduce, .data = { .symbol = symbol_val, .child_count = child_count_val } } +{ .type = ts_parse_action_type_reduce, .data = { .symbol = symbol_val, .child_count = child_count_val } } #define ACCEPT_INPUT() \ -(ts_parse_action){ .type = ts_parse_action_type_accept } +{ .type = ts_parse_action_type_accept } /* @@ -174,7 +168,7 @@ static ts_tree * ts_lexer_build_node(ts_lexer *lexer, ts_symbol symbol) { return ts_tree_make_leaf(symbol, size, offset); } -static const state_id ts_lex_state_error = -1; +#define ts_lex_state_error -1 /* diff --git a/include/tree_sitter/runtime.h b/include/tree_sitter/runtime.h index 52c5fd79..4d9a8ddc 100644 --- a/include/tree_sitter/runtime.h +++ b/include/tree_sitter/runtime.h @@ -8,8 +8,8 @@ extern "C" { #include typedef unsigned int ts_symbol; -static const ts_symbol ts_builtin_sym_error = 0; -static const ts_symbol ts_builtin_sym_end = 1; +#define ts_builtin_sym_error 0 +#define ts_builtin_sym_end 1 typedef struct ts_tree ts_tree; ts_tree * ts_tree_make_leaf(ts_symbol symbol, size_t size, size_t offset); diff --git a/src/compiler/generate_code/c_code.cc b/src/compiler/generate_code/c_code.cc index 40d3a92c..5e290600 100644 --- a/src/compiler/generate_code/c_code.cc +++ b/src/compiler/generate_code/c_code.cc @@ -171,8 +171,8 @@ namespace tree_sitter { string state_and_symbol_counts() { return join({ - "STATE_COUNT = " + to_string(parse_table.states.size()) + ";", - "SYMBOL_COUNT = " + to_string(parse_table.symbols.size()) + ";" + "#define STATE_COUNT " + to_string(parse_table.states.size()), + "#define SYMBOL_COUNT " + to_string(parse_table.symbols.size()) }); }