#include "spec_helper.h" #include #include "build_tables/perform.h" using build_tables::perform; using namespace rules; typedef unordered_set parse_actions; typedef unordered_set lex_actions; static unordered_set keys(const unordered_map &map) { unordered_set result; for (auto pair : map) { result.insert(pair.first); } return result; } static unordered_set keys(const unordered_map &map) { unordered_set result; for (auto pair : map) { result.insert(pair.first); } return result; } START_TEST describe("building parse and lex tables", []() { Grammar grammar({ { "expression", choice({ seq({ sym("term"), sym("plus"), sym("term") }), sym("term") }) }, { "term", choice({ sym("variable"), sym("number"), seq({ sym("left-paren"), sym("expression"), sym("right-paren") }) }) } }); Grammar lex_grammar({ { "plus", str("+") }, { "variable", pattern("\\w+") }, { "number", pattern("\\d+") }, { "left-paren", str("(") }, { "right-paren", str(")") } }); ParseTable table; LexTable lex_table; before_each([&]() { pair tables = perform(grammar, lex_grammar); table = tables.first; lex_table = tables.second; }); function parse_state = [&](size_t index) { return table.states[index]; }; function lex_state = [&](size_t parse_state_index) { long index = table.states[parse_state_index].lex_state_index; return lex_table.states[index]; }; it("has the right starting state", [&]() { AssertThat(keys(parse_state(0).actions), Equals(unordered_set({ "expression", "term", "number", "variable", "left-paren", }))); AssertThat(keys(lex_state(0).actions), Equals(unordered_set({ CharMatchSpecific('('), CharMatchClass(CharClassDigit), CharMatchClass(CharClassWord), }))); AssertThat(lex_state(0).expected_inputs(), Equals(unordered_set({ CharMatchSpecific('('), CharMatchClass(CharClassDigit), CharMatchClass(CharClassWord), }))); }); it("accepts when the start symbol is reduced", [&]() { AssertThat(parse_state(1).actions, Equals(unordered_map({ { "__END__", parse_actions({ ParseAction::Accept() }) } }))); }); it("has the right next states", [&]() { AssertThat(parse_state(2).actions, Equals(unordered_map({ { "plus", parse_actions({ ParseAction::Shift(3) }) }, { "__END__", parse_actions({ ParseAction::Reduce("expression", 1) }) }, }))); }); }); END_TEST