From d91bc718a019d1024a491182ecd8e52778076b01 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Sun, 4 May 2014 23:28:53 -0700 Subject: [PATCH] Add basic test for parse table builder --- .../build_tables/build_parse_table_spec.cc | 54 +++++++++++++++++++ .../build_tables/conflict_manager_spec.cc | 1 - 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 spec/compiler/build_tables/build_parse_table_spec.cc diff --git a/spec/compiler/build_tables/build_parse_table_spec.cc b/spec/compiler/build_tables/build_parse_table_spec.cc new file mode 100644 index 00000000..ba9d80b9 --- /dev/null +++ b/spec/compiler/build_tables/build_parse_table_spec.cc @@ -0,0 +1,54 @@ +#include "compiler_spec_helper.h" +#include "compiler/build_tables/build_parse_table.h" +#include "compiler/parse_table.h" +#include "compiler/prepared_grammar.h" +#include "compiler/rules/built_in_symbols.h" + +using namespace rules; +using namespace build_tables; + +START_TEST + +describe("building parse tables", []() { + PreparedGrammar parse_grammar({ + { "rule0", choice({ i_sym(1), i_sym(2) }) }, + { "rule1", i_token(0) }, + { "rule2", i_token(1) }, + }, {}); + + PreparedGrammar lex_grammar({ + { "token0", pattern("[a-c]") }, + { "token1", pattern("[b-d]") }, + }, {}); + + it("first looks for the start rule and its item set closure", [&]() { + auto result = build_parse_table(parse_grammar, lex_grammar); + + AssertThat(result.first.states[0].actions, Equals(map({ + { Symbol(0), ParseAction::Shift(1, { 0 }) }, + { Symbol(1), ParseAction::Shift(2, { 0 }) }, + { Symbol(2), ParseAction::Shift(2, { 0 }) }, + { Symbol(0, SymbolOptionToken), ParseAction::Shift(3, { 0 }) }, + { Symbol(1, SymbolOptionToken), ParseAction::Shift(4, { 0 }) }, + }))); + }); + + it("accepts the input when EOF occurs after the start rule", [&]() { + auto result = build_parse_table(parse_grammar, lex_grammar); + + AssertThat(result.first.states[1].actions, Equals(map({ + { END_OF_INPUT(), ParseAction::Accept() }, + }))); + }); + + it("reduces a rule once it has been consumed", [&]() { + auto result = build_parse_table(parse_grammar, lex_grammar); + + AssertThat(result.first.states[2].actions, Equals(map({ + { END_OF_INPUT(), ParseAction::Reduce(Symbol(1), 1, 0) }, + }))); + }); +}); + + +END_TEST diff --git a/spec/compiler/build_tables/conflict_manager_spec.cc b/spec/compiler/build_tables/conflict_manager_spec.cc index 50af5f9a..dfe49c36 100644 --- a/spec/compiler/build_tables/conflict_manager_spec.cc +++ b/spec/compiler/build_tables/conflict_manager_spec.cc @@ -28,7 +28,6 @@ describe("resolving parse conflicts", []() { LexConflictManager *manager; - before_each([&]() { manager = new LexConflictManager(lex_grammar); });