tree-sitter/spec/compiler/build_tables/first_set_spec.cpp
Max Brunsfeld 1bf216b796 Rename next_{terminals,non_terminals} to first_set and follow_sets
This is to prepare for keeping track of lookahead symbols as part
of computing follow sets
2014-01-19 01:49:56 -08:00

53 lines
No EOL
1.3 KiB
C++

#include "spec_helper.h"
#include "build_tables/first_set.h"
#include "grammar.h"
#include "rules.h"
using std::set;
using namespace build_tables;
using namespace rules;
START_TEST
describe("computing FIRST sets", []() {
Grammar grammar({
{ "A", choice({
seq({
sym("B"),
sym("x"),
sym("B") }),
sym("B") }) },
{ "B", choice({
seq({
sym("y"),
sym("z"),
sym("y") }),
sym("y") }) },
{ "C", seq({
choice({
sym("x"),
blank() }),
sym("y") }) }
});
describe("for a rule starting with a non-terminal B", [&]() {
it("includes FIRST(B)", [&]() {
auto terminals = first_set(grammar.rules.find("A")->second, grammar);
AssertThat(terminals, Equals(set<Symbol>({
Symbol("y")
})));
});
});
describe("for a sequence xy", [&]() {
it("includes FIRST(y) when x can be blank", [&]() {
auto terminals = first_set(grammar.rules.find("C")->second, grammar);
AssertThat(terminals, Equals(set<Symbol>({
Symbol("x"),
Symbol("y")
})));
});
});
});
END_TEST