Tweak format for example grammars
This commit is contained in:
parent
324f55f1ce
commit
13c4e6e648
23 changed files with 1616 additions and 1537 deletions
|
|
@ -68,7 +68,7 @@ describe("computing FIRST sets", []() {
|
|||
})));
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe("when there are left-recursive rules", [&]() {
|
||||
it("terminates", [&]() {
|
||||
Grammar grammar({
|
||||
|
|
@ -77,7 +77,7 @@ describe("computing FIRST sets", []() {
|
|||
sym("y"),
|
||||
}) },
|
||||
});
|
||||
|
||||
|
||||
AssertThat(first_set(sym("expression"), grammar), Equals(set<Symbol>({
|
||||
Symbol("y")
|
||||
})));
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ describe("computing closures of item sets", []() {
|
|||
ParseItemSet item_set = item_set_closure(ParseItemSet({
|
||||
ParseItem(Symbol("E"), grammar.rule(Symbol("E")), 0, Symbol("__END__"))
|
||||
}), grammar);
|
||||
|
||||
|
||||
AssertThat(item_set, Equals(ParseItemSet({
|
||||
ParseItem(Symbol("F"), grammar.rule(Symbol("F")), 0, Symbol("__END__")),
|
||||
ParseItem(Symbol("F"), grammar.rule(Symbol("F")), 0, Symbol("+")),
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ describe("checking if rules can be blank", [&]() {
|
|||
AssertThat(rule_can_be_blank(str("x")), IsFalse());
|
||||
AssertThat(rule_can_be_blank(pattern("x")), IsFalse());
|
||||
});
|
||||
|
||||
|
||||
it("returns true for blanks", [&]() {
|
||||
AssertThat(rule_can_be_blank(blank()), IsTrue());
|
||||
});
|
||||
|
|
@ -23,7 +23,7 @@ describe("checking if rules can be blank", [&]() {
|
|||
it("returns true for repeats", [&]() {
|
||||
AssertThat(rule_can_be_blank(repeat(str("x"))), IsTrue());
|
||||
});
|
||||
|
||||
|
||||
it("returns true for choices iff one or more sides can be blank", [&]() {
|
||||
rule = choice({ sym("x"), blank() });
|
||||
AssertThat(rule_can_be_blank(rule), IsTrue());
|
||||
|
|
@ -34,7 +34,7 @@ describe("checking if rules can be blank", [&]() {
|
|||
rule = choice({ sym("x"), sym("y") });
|
||||
AssertThat(rule_can_be_blank(rule), IsFalse());
|
||||
});
|
||||
|
||||
|
||||
it("returns true for sequences iff both sides can be blank", [&]() {
|
||||
rule = seq({ blank(), str("x") });
|
||||
AssertThat(rule_can_be_blank(rule), IsFalse());
|
||||
|
|
@ -45,7 +45,7 @@ describe("checking if rules can be blank", [&]() {
|
|||
rule = seq({ blank(), choice({ sym("x"), blank() }) });
|
||||
AssertThat(rule_can_be_blank(rule), IsTrue());
|
||||
});
|
||||
|
||||
|
||||
describe("checking recursively (by expanding non-terminals)", [&]() {
|
||||
PreparedGrammar grammar({
|
||||
{ "A", choice({
|
||||
|
|
@ -55,12 +55,12 @@ describe("checking if rules can be blank", [&]() {
|
|||
seq({ sym("B"), sym("y") }),
|
||||
sym("z") }) },
|
||||
}, {});
|
||||
|
||||
|
||||
it("terminates for left-recursive rules that can be blank", [&]() {
|
||||
rule = sym("A");
|
||||
AssertThat(rule_can_be_blank(rule, grammar), IsTrue());
|
||||
});
|
||||
|
||||
|
||||
it("terminates for left-recursive rules that can't be blank", [&]() {
|
||||
rule = sym("B");
|
||||
AssertThat(rule_can_be_blank(rule, grammar), IsFalse());
|
||||
|
|
|
|||
|
|
@ -7,12 +7,10 @@ static string src_dir() {
|
|||
return dir;
|
||||
}
|
||||
|
||||
namespace tree_sitter {
|
||||
namespace examples {
|
||||
Grammar arithmetic();
|
||||
Grammar javascript();
|
||||
Grammar json();
|
||||
}
|
||||
namespace tree_sitter_examples {
|
||||
extern const Grammar arithmetic;
|
||||
extern const Grammar javascript;
|
||||
extern const Grammar json;
|
||||
}
|
||||
|
||||
START_TEST
|
||||
|
|
@ -20,7 +18,7 @@ START_TEST
|
|||
describe("compiling the example grammars", []() {
|
||||
string example_parser_dir = src_dir() + "/examples/parsers/";
|
||||
|
||||
auto compile_grammar = [&](Grammar grammar, string language) {
|
||||
auto compile_grammar = [&](const Grammar &grammar, string language) {
|
||||
it(("compiles the " + language + " grammar").c_str(), [&]() {
|
||||
ofstream file(example_parser_dir + language + ".c");
|
||||
file << compile(grammar, language);
|
||||
|
|
@ -28,9 +26,9 @@ describe("compiling the example grammars", []() {
|
|||
});
|
||||
};
|
||||
|
||||
compile_grammar(examples::arithmetic(), "arithmetic");
|
||||
compile_grammar(examples::json(), "json");
|
||||
compile_grammar(examples::javascript(), "javascript");
|
||||
compile_grammar(tree_sitter_examples::arithmetic, "arithmetic");
|
||||
compile_grammar(tree_sitter_examples::json, "json");
|
||||
compile_grammar(tree_sitter_examples::javascript, "javascript");
|
||||
});
|
||||
|
||||
END_TEST
|
||||
|
|
|
|||
|
|
@ -46,7 +46,7 @@ namespace std {
|
|||
}
|
||||
return stream << ">";
|
||||
}
|
||||
|
||||
|
||||
template<typename TKey, typename TValue>
|
||||
inline std::ostream& operator<<(std::ostream &stream, const std::map<TKey, TValue> &map) {
|
||||
stream << std::string("#<map: ");
|
||||
|
|
@ -60,7 +60,7 @@ namespace std {
|
|||
}
|
||||
return stream << ">";
|
||||
}
|
||||
|
||||
|
||||
template<typename TKey, typename TValue>
|
||||
inline std::ostream& operator<<(std::ostream &stream, const std::unordered_map<TKey, TValue> &map) {
|
||||
stream << std::string("#<map: ");
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ describe("assigning user-visible names to symbols", [&]() {
|
|||
{ "some_generated_string_name", str("the-string") },
|
||||
{ "some_generated_pattern_name", pattern("the-pattern") },
|
||||
});
|
||||
|
||||
|
||||
describe("for symbols that are not in the lexical grammar (syntactic rules)", [&]() {
|
||||
it("uses the symbol's normal name", [&]() {
|
||||
auto symbol = Symbol("some_syntactic_symbol");
|
||||
|
|
@ -23,7 +23,7 @@ describe("assigning user-visible names to symbols", [&]() {
|
|||
})));
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe("for symbols that are in the lexical grammar", [&]() {
|
||||
it("uses symbols' normal names when they are given by the user", [&]() {
|
||||
auto symbol = Symbol("some_given_name");
|
||||
|
|
@ -31,14 +31,14 @@ describe("assigning user-visible names to symbols", [&]() {
|
|||
{ symbol, "some_given_name" }
|
||||
})));
|
||||
});
|
||||
|
||||
|
||||
it("assigns names to string rules based on their string value", [&]() {
|
||||
auto symbol = Symbol("some_generated_string_name", rules::SymbolTypeAuxiliary);
|
||||
AssertThat(name_symbols::name_symbols({ symbol }, lexical_grammar), Equals(map<Symbol, string>({
|
||||
{ symbol, "'the-string'" }
|
||||
})));
|
||||
});
|
||||
|
||||
|
||||
it("assigns names to pattern rules based on their pattern value", [&]() {
|
||||
auto symbol = Symbol("some_generated_pattern_name", rules::SymbolTypeAuxiliary);
|
||||
AssertThat(name_symbols::name_symbols({ symbol }, lexical_grammar), Equals(map<Symbol, string>({
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue