tree-sitter/spec/compiler/build_tables/perform_spec.cpp

91 lines
3.1 KiB
C++
Raw Normal View History

2013-12-27 17:31:08 -08:00
#include "spec_helper.h"
#include <functional>
2014-01-11 15:14:17 -08:00
#include "build_tables/perform.h"
2013-12-27 17:31:08 -08:00
2014-01-11 17:08:32 -08:00
using build_tables::perform;
using namespace rules;
2014-01-08 18:35:16 -08:00
typedef unordered_set<ParseAction> parse_actions;
typedef unordered_set<LexAction> lex_actions;
2013-12-27 17:31:08 -08:00
START_TEST
describe("building parse and lex tables", []() {
Grammar grammar({
{ "expression", choice({
seq({
sym("term"),
2014-01-08 18:35:16 -08:00
sym("plus"),
sym("term") }),
sym("term") }) },
{ "term", choice({
sym("variable"),
sym("number"),
seq({
2014-01-08 18:35:16 -08:00
sym("left-paren"),
sym("expression"),
2014-01-08 18:35:16 -08:00
sym("right-paren")
}) }) }
});
Grammar lex_grammar({
2014-01-08 18:35:16 -08:00
{ "plus", str("+") },
{ "variable", pattern("\\w+") },
{ "number", pattern("\\d+") },
{ "left-paren", str("(") },
{ "right-paren", str(")") }
});
2014-01-11 17:08:32 -08:00
pair<ParseTable, LexTable> tables = perform(grammar, lex_grammar);
ParseTable table = tables.first;
LexTable lex_table = tables.second;
2013-12-27 17:31:08 -08:00
function<ParseState(size_t)> parse_state = [&](size_t index) {
2013-12-27 17:31:08 -08:00
return table.states[index];
};
2013-12-27 17:31:08 -08:00
function<LexState(size_t)> lex_state = [&](size_t parse_state_index) {
2013-12-27 17:31:08 -08:00
size_t index = table.states[parse_state_index].lex_state_index;
return lex_table.states[index];
};
2013-12-27 17:31:08 -08:00
it("has the right starting state", [&]() {
2013-12-27 17:31:08 -08:00
AssertThat(parse_state(0).actions, Equals(unordered_map<string, parse_actions>({
{ "expression", parse_actions({ ParseAction::Shift(1) }) },
{ "term", parse_actions({ ParseAction::Shift(2) }) },
{ "number", parse_actions({ ParseAction::Shift(5) }) },
{ "variable", parse_actions({ ParseAction::Shift(5) }) },
2014-01-08 18:35:16 -08:00
{ "left-paren", parse_actions({ ParseAction::Shift(6) }) },
{ "variable", parse_actions({ ParseAction::Shift(9) }) },
{ "number", parse_actions({ ParseAction::Shift(10) }) },
2013-12-27 17:31:08 -08:00
})));
AssertThat(lex_state(0).actions, Equals(unordered_map<CharMatch, lex_actions>({
{ CharMatchSpecific('('), lex_actions({ LexAction::Advance(1) }) },
{ CharMatchClass(CharClassDigit), lex_actions({ LexAction::Advance(2) }) },
{ CharMatchClass(CharClassWord), lex_actions({ LexAction::Advance(3) }) },
2013-12-27 17:31:08 -08:00
})));
2014-01-08 18:35:16 -08:00
AssertThat(lex_state(0).expected_inputs(), Equals(unordered_set<CharMatch>({
CharMatchSpecific('('),
CharMatchClass(CharClassDigit),
CharMatchClass(CharClassWord),
})));
});
2013-12-27 17:31:08 -08:00
it("accepts when the start symbol is reduced", [&]() {
2013-12-27 17:31:08 -08:00
AssertThat(parse_state(1).actions, Equals(unordered_map<string, parse_actions>({
{ ParseTable::END_OF_INPUT, parse_actions({ ParseAction::Accept() }) }
})));
});
2013-12-27 17:31:08 -08:00
it("has the right next states", [&]() {
2013-12-27 17:31:08 -08:00
AssertThat(parse_state(2).actions, Equals(unordered_map<string, parse_actions>({
2014-01-08 18:35:16 -08:00
{ "plus", parse_actions({ ParseAction::Shift(3) }) },
2013-12-27 17:31:08 -08:00
})));
});
});
END_TEST