Generate parse tables and code using pre-processed grammars
This commit is contained in:
parent
2621f06315
commit
2afd8843dc
27 changed files with 272 additions and 285 deletions
|
|
@ -2,15 +2,44 @@
|
|||
#include <functional>
|
||||
|
||||
using namespace tree_sitter::lr;
|
||||
using namespace tree_sitter::rules;
|
||||
|
||||
typedef std::unordered_set<ParseAction> parse_actions;
|
||||
typedef std::unordered_set<LexAction> lex_actions;
|
||||
|
||||
START_TEST
|
||||
|
||||
describe("building parse and lex tables", []() {
|
||||
Grammar grammar = test_grammars::arithmetic();
|
||||
ParseTable table = build_tables(grammar).first;
|
||||
LexTable lex_table = build_tables(grammar).second;
|
||||
Grammar grammar({
|
||||
{ "expression", choice({
|
||||
seq({
|
||||
sym("term"),
|
||||
token("plus-token"),
|
||||
sym("term") }),
|
||||
sym("term") }) },
|
||||
{ "term", choice({
|
||||
sym("variable"),
|
||||
sym("number"),
|
||||
seq({
|
||||
token("left-paren-token"),
|
||||
sym("expression"),
|
||||
token("right-paren-token")
|
||||
}) }) },
|
||||
{ "variable", token("variable-token") },
|
||||
{ "number", token("number-token") }
|
||||
});
|
||||
|
||||
Grammar lex_grammar({
|
||||
{ "plus-token", character('+') },
|
||||
{ "variable-token", pattern("\\w+") },
|
||||
{ "number-token", pattern("\\d+") },
|
||||
{ "left-paren-token", character('(') },
|
||||
{ "right-paren-token", character(')') }
|
||||
});
|
||||
|
||||
pair<ParseTable, LexTable> tables = build_tables(grammar, lex_grammar);
|
||||
ParseTable table = tables.first;
|
||||
LexTable lex_table = tables.second;
|
||||
|
||||
function<ParseState(size_t)> parse_state = [&](size_t index) {
|
||||
return table.states[index];
|
||||
|
|
@ -25,16 +54,18 @@ describe("building parse and lex tables", []() {
|
|||
AssertThat(parse_state(0).actions, Equals(unordered_map<string, parse_actions>({
|
||||
{ "expression", parse_actions({ ParseAction::Shift(1) }) },
|
||||
{ "term", parse_actions({ ParseAction::Shift(2) }) },
|
||||
{ "factor", parse_actions({ ParseAction::Shift(5) }) },
|
||||
{ "variable", parse_actions({ ParseAction::Shift(8) }) },
|
||||
{ "number", parse_actions({ ParseAction::Shift(8) }) },
|
||||
{ "left_paren", parse_actions({ ParseAction::Shift(9) }) }
|
||||
{ "number", parse_actions({ ParseAction::Shift(5) }) },
|
||||
{ "variable", parse_actions({ ParseAction::Shift(5) }) },
|
||||
|
||||
{ "left-paren-token", parse_actions({ ParseAction::Shift(6) }) },
|
||||
{ "variable-token", parse_actions({ ParseAction::Shift(9) }) },
|
||||
{ "number-token", parse_actions({ ParseAction::Shift(10) }) },
|
||||
})));
|
||||
|
||||
AssertThat(lex_state(0).actions, Equals(unordered_map<CharMatch, lex_actions>({
|
||||
{ CharMatchClass(CharClassWord), lex_actions({ LexAction::Advance(1) }) },
|
||||
{ CharMatchClass(CharClassDigit), lex_actions({ LexAction::Advance(4) }) },
|
||||
{ CharMatchSpecific('('), lex_actions({ LexAction::Advance(11) }) }
|
||||
{ CharMatchSpecific('('), lex_actions({ LexAction::Advance(1) }) },
|
||||
{ CharMatchClass(CharClassWord), lex_actions({ LexAction::Advance(2) }) },
|
||||
{ CharMatchClass(CharClassDigit), lex_actions({ LexAction::Advance(3) }) },
|
||||
})));
|
||||
});
|
||||
|
||||
|
|
@ -46,15 +77,7 @@ describe("building parse and lex tables", []() {
|
|||
|
||||
it("has the right next states", [&]() {
|
||||
AssertThat(parse_state(2).actions, Equals(unordered_map<string, parse_actions>({
|
||||
{ "plus", parse_actions({ ParseAction::Shift(3) }) },
|
||||
})));
|
||||
|
||||
AssertThat(parse_state(3).actions, Equals(unordered_map<string, parse_actions>({
|
||||
{ "variable", parse_actions({ ParseAction::Shift(8) }) },
|
||||
{ "factor", parse_actions({ ParseAction::Shift(5) }) },
|
||||
{ "left_paren", parse_actions({ ParseAction::Shift(9) }) },
|
||||
{ "number", parse_actions({ ParseAction::Shift(8) }) },
|
||||
{ "term", parse_actions({ ParseAction::Shift(4) }) },
|
||||
{ "plus-token", parse_actions({ ParseAction::Shift(3) }) },
|
||||
})));
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue