Clean up parser macros more

This commit is contained in:
Max Brunsfeld 2014-03-26 12:52:31 -07:00
parent 6a0e2c08e6
commit 4454925b5a
7 changed files with 45 additions and 32 deletions

View file

@ -1,6 +1,7 @@
#include "tree_sitter/parser.h"
#define TS_SYMBOL_COUNT 11
STATE_COUNT = 52;
SYMBOL_COUNT = 11;
enum {
ts_sym_expression = 2,
@ -28,7 +29,7 @@ SYMBOL_NAMES = {
"token2",
};
HIDDEN_SYMBOLS(11) = {
HIDDEN_SYMBOLS = {
[ts_aux_sym_token1] = 1,
[ts_aux_sym_token2] = 1,
};
@ -131,7 +132,7 @@ LEX_FN() {
}
}
LEX_STATES(52) = {
LEX_STATES = {
[0] = 8,
[1] = 0,
[2] = 15,
@ -186,7 +187,7 @@ LEX_STATES(52) = {
[51] = 15,
};
PARSE_TABLE(52, 11) = {
PARSE_TABLE = {
[0] = {
[ts_sym_expression] = SHIFT(1),
[ts_sym_factor] = SHIFT(2),

View file

@ -1,6 +1,7 @@
#include "tree_sitter/parser.h"
#define TS_SYMBOL_COUNT 42
STATE_COUNT = 239;
SYMBOL_COUNT = 42;
enum {
ts_sym__else = 2,
@ -90,7 +91,7 @@ SYMBOL_NAMES = {
"token9",
};
HIDDEN_SYMBOLS(42) = {
HIDDEN_SYMBOLS = {
[ts_sym__else] = 1,
[ts_sym__function] = 1,
[ts_sym__if] = 1,
@ -1088,7 +1089,7 @@ LEX_FN() {
}
}
LEX_STATES(239) = {
LEX_STATES = {
[0] = 110,
[1] = 82,
[2] = 84,
@ -1330,7 +1331,7 @@ LEX_STATES(239) = {
[238] = 113,
};
PARSE_TABLE(239, 42) = {
PARSE_TABLE = {
[0] = {
[ts_sym__function] = SHIFT(1),
[ts_sym__if] = SHIFT(207),

View file

@ -1,6 +1,7 @@
#include "tree_sitter/parser.h"
#define TS_SYMBOL_COUNT 18
STATE_COUNT = 60;
SYMBOL_COUNT = 18;
enum {
ts_sym_array = 2,
@ -42,7 +43,7 @@ SYMBOL_NAMES = {
"token6",
};
HIDDEN_SYMBOLS(18) = {
HIDDEN_SYMBOLS = {
[ts_aux_sym_repeat_helper1] = 1,
[ts_aux_sym_repeat_helper2] = 1,
[ts_aux_sym_token1] = 1,
@ -326,7 +327,7 @@ LEX_FN() {
}
}
LEX_STATES(60) = {
LEX_STATES = {
[0] = 8,
[1] = 0,
[2] = 0,
@ -389,7 +390,7 @@ LEX_STATES(60) = {
[59] = 0,
};
PARSE_TABLE(60, 18) = {
PARSE_TABLE = {
[0] = {
[ts_sym_array] = SHIFT(1),
[ts_sym_false] = SHIFT(1),

View file

@ -31,14 +31,20 @@ 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[]
static const char *ts_symbol_names[ts_symbol_count]
#define HIDDEN_SYMBOLS(num_symbols) \
static const int hidden_symbol_flags[num_symbols]
#define HIDDEN_SYMBOLS \
static const int hidden_symbol_flags[ts_symbol_count]
#define LEX_STATES(num_states) \
static state_id ts_lex_states[num_states]
#define LEX_STATES \
static state_id ts_lex_states[ts_state_count]
#define LEX_FN() \
static ts_tree * ts_lex(ts_lexer *lexer, state_id lex_state)
@ -62,15 +68,15 @@ lookahead = ts_lexer_lookahead_char(lexer);
#define LEX_PANIC() \
{ DEBUG_LEX("Lex error: unexpected state %d", LEX_STATE()); return NULL; }
#define PARSE_TABLE(num_states, num_symbols) \
static const ts_parse_action ts_parse_actions[num_states][num_symbols]
#define PARSE_TABLE \
static const ts_parse_action ts_parse_actions[ts_state_count][ts_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(ts_symbol_count, (const ts_parse_action *)ts_parse_actions, ts_lex_states, hidden_symbol_flags), \
.free_fn = NULL \
}; \
}

View file

@ -22,7 +22,9 @@ describe("compiling the example grammars", []() {
auto compile_grammar = [&](Grammar grammar, string language) {
it(("compiles the " + language + " grammar").c_str(), [&]() {
ofstream(example_parser_dir + language + ".c") << compile(grammar, language);
ofstream file(example_parser_dir + language + ".c");
file << compile(grammar, language);
file.close();
});
};

View file

@ -5,7 +5,7 @@ int main(int argc, char *argv[]) {
"",
"--no-color",
"--only="
"compiles the javascript grammar"
""
};
return bandit::run(4, const_cast<char **>(args));
}

View file

@ -59,7 +59,7 @@ namespace tree_sitter {
string code() {
return join({
includes(),
symbol_count(),
state_and_symbol_counts(),
symbol_enum(),
rule_names_list(),
hidden_symbols_list(),
@ -73,10 +73,9 @@ namespace tree_sitter {
private:
string symbol_id(rules::Symbol symbol) {
if (symbol.is_built_in()) {
if (symbol == rules::ERROR)
return "ts_builtin_sym_error";
else
return "ts_builtin_sym_end";
return (symbol == rules::ERROR) ?
"ts_builtin_sym_error" :
"ts_builtin_sym_end";
} else if (symbol.is_auxiliary()) {
return "ts_aux_sym_" + symbol.name;
} else {
@ -176,8 +175,11 @@ namespace tree_sitter {
return _switch("lex_state", body);
}
string symbol_count() {
return "#define TS_SYMBOL_COUNT " + to_string(parse_table.symbols.size());
string state_and_symbol_counts() {
return join({
"STATE_COUNT = " + to_string(parse_table.states.size()) + ";",
"SYMBOL_COUNT = " + to_string(parse_table.symbols.size()) + ";"
});
}
string symbol_enum() {
@ -201,7 +203,7 @@ namespace tree_sitter {
}
string hidden_symbols_list() {
string result = "HIDDEN_SYMBOLS(" + to_string(parse_table.symbols.size()) + ") = {";
string result = "HIDDEN_SYMBOLS = {";
for (auto &symbol : parse_table.symbols)
if (symbol.is_hidden())
result += indent("\n[" + symbol_id(symbol) + "] = 1,");
@ -232,7 +234,7 @@ namespace tree_sitter {
string lex_states_list() {
size_t state_id = 0;
return join({
"LEX_STATES(" + to_string(parse_table.states.size()) + ") = {",
"LEX_STATES = {",
indent(join(map_to_string<ParseState>(parse_table.states, [&](ParseState state) {
return "[" + to_string(state_id++) + "] = " + to_string(state.lex_state_id) + ",";
}))),
@ -243,7 +245,7 @@ namespace tree_sitter {
string parse_table_array() {
size_t state_id = 0;
return join({
"PARSE_TABLE(" + to_string(parse_table.states.size()) + ", " + to_string(parse_table.symbols.size()) + ") = {",
"PARSE_TABLE = {",
indent(join(map_to_string<ParseState>(parse_table.states, [&](ParseState state) {
string result = "[" + to_string(state_id++) + "] = {";
for (auto &pair : state.actions)