Remove use of shared_ptr in choice, repeat, and seq factories

This commit is contained in:
Max Brunsfeld 2017-03-17 14:28:13 -07:00
parent d9fb863bea
commit b3edd8f749
34 changed files with 366 additions and 381 deletions

View file

@ -86,7 +86,7 @@ describe("LexItemSet::transitions()", [&]() {
it("handles sequences", [&]() {
LexItemSet item_set({
LexItem(Symbol::non_terminal(1), Seq::build({
LexItem(Symbol::non_terminal(1), Rule::seq({
CharacterSet{{ 'w' }},
CharacterSet{{ 'x' }},
CharacterSet{{ 'y' }},
@ -101,7 +101,7 @@ describe("LexItemSet::transitions()", [&]() {
CharacterSet().include('w'),
Transition{
LexItemSet({
LexItem(Symbol::non_terminal(1), Seq::build({
LexItem(Symbol::non_terminal(1), Rule::seq({
CharacterSet{{ 'x' }},
CharacterSet{{ 'y' }},
CharacterSet{{ 'z' }},
@ -116,10 +116,10 @@ describe("LexItemSet::transitions()", [&]() {
it("handles sequences with nested precedence", [&]() {
LexItemSet item_set({
LexItem(Symbol::non_terminal(1), Seq::build({
Metadata::prec(3, Seq::build({
LexItem(Symbol::non_terminal(1), Rule::seq({
Metadata::prec(3, Rule::seq({
CharacterSet{{ 'v' }},
Metadata::prec(4, Seq::build({
Metadata::prec(4, Rule::seq({
CharacterSet{{ 'w' }},
CharacterSet{{ 'x' }} })),
CharacterSet{{ 'y' }} })),
@ -138,9 +138,9 @@ describe("LexItemSet::transitions()", [&]() {
// The outer precedence is now 'active', because we are within its
// contained rule.
LexItemSet({
LexItem(Symbol::non_terminal(1), Seq::build({
Metadata::active_prec(3, Seq::build({
Metadata::prec(4, Seq::build({
LexItem(Symbol::non_terminal(1), Rule::seq({
Metadata::active_prec(3, Rule::seq({
Metadata::prec(4, Rule::seq({
CharacterSet{{ 'w' }},
CharacterSet{{ 'x' }}
})),
@ -168,8 +168,8 @@ describe("LexItemSet::transitions()", [&]() {
Transition{
// The inner precedence is now 'active'
LexItemSet({
LexItem(Symbol::non_terminal(1), Seq::build({
Metadata::active_prec(3, Seq::build({
LexItem(Symbol::non_terminal(1), Rule::seq({
Metadata::active_prec(3, Rule::seq({
Metadata::active_prec(4, CharacterSet{{'x'}}),
CharacterSet{{'y'}}
})),
@ -194,7 +194,7 @@ describe("LexItemSet::transitions()", [&]() {
CharacterSet().include('x'),
Transition{
LexItemSet({
LexItem(Symbol::non_terminal(1), Seq::build({
LexItem(Symbol::non_terminal(1), Rule::seq({
Metadata::active_prec(3, CharacterSet{{'y'}}),
CharacterSet{{'z'}},
})),
@ -228,8 +228,8 @@ describe("LexItemSet::transitions()", [&]() {
it("handles sequences where the left hand side can be blank", [&]() {
LexItemSet item_set({
LexItem(Symbol::non_terminal(1), Seq::build({
Choice::build({
LexItem(Symbol::non_terminal(1), Rule::seq({
Rule::choice({
CharacterSet{{ 'x' }},
Blank{},
}),
@ -245,7 +245,7 @@ describe("LexItemSet::transitions()", [&]() {
CharacterSet().include('x'),
Transition{
LexItemSet({
LexItem(Symbol::non_terminal(1), Seq::build({
LexItem(Symbol::non_terminal(1), Rule::seq({
CharacterSet{{ 'y' }},
CharacterSet{{ 'z' }},
})),
@ -277,7 +277,7 @@ describe("LexItemSet::transitions()", [&]() {
it("handles repeats", [&]() {
LexItemSet item_set({
LexItem(Symbol::non_terminal(1), Repeat{Seq::build({
LexItem(Symbol::non_terminal(1), Repeat{Rule::seq({
CharacterSet{{ 'a' }},
CharacterSet{{ 'b' }},
})}),
@ -291,9 +291,9 @@ describe("LexItemSet::transitions()", [&]() {
CharacterSet().include('a'),
Transition{
LexItemSet({
LexItem(Symbol::non_terminal(1), Seq::build({
LexItem(Symbol::non_terminal(1), Rule::seq({
CharacterSet{{ 'b' }},
Repeat{Seq::build({
Repeat{Rule::seq({
CharacterSet{{ 'a' }},
CharacterSet{{ 'b' }},
})}
@ -342,12 +342,12 @@ describe("LexItemSet::transitions()", [&]() {
it("handles choices between overlapping character sets", [&]() {
LexItemSet item_set({
LexItem(Symbol::non_terminal(1), Choice::build({
Metadata::active_prec(2, Seq::build({
LexItem(Symbol::non_terminal(1), Rule::choice({
Metadata::active_prec(2, Rule::seq({
CharacterSet{{ 'a', 'b', 'c', 'd' }},
CharacterSet{{ 'x' }},
})),
Metadata::active_prec(3, Seq::build({
Metadata::active_prec(3, Rule::seq({
CharacterSet{{ 'c', 'd', 'e', 'f' }},
CharacterSet{{ 'y' }},
})),
@ -393,12 +393,12 @@ describe("LexItemSet::transitions()", [&]() {
it("handles choices between a subset and a superset of characters", [&]() {
LexItemSet item_set({
LexItem(Symbol::non_terminal(1), Choice::build({
Seq::build({
LexItem(Symbol::non_terminal(1), Rule::choice({
Rule::seq({
CharacterSet{{ 'b', 'c', 'd' }},
CharacterSet{{ 'x' }},
}),
Seq::build({
Rule::seq({
CharacterSet{{ 'a', 'b', 'c', 'd', 'e', 'f' }},
CharacterSet{{ 'y' }},
}),
@ -434,10 +434,10 @@ describe("LexItemSet::transitions()", [&]() {
it("handles choices between whitelisted and blacklisted character sets", [&]() {
LexItemSet item_set({
LexItem(Symbol::non_terminal(1), Seq::build({
Choice::build({
LexItem(Symbol::non_terminal(1), Rule::seq({
Rule::choice({
CharacterSet().include_all().exclude('/'),
Seq::build({
Rule::seq({
CharacterSet{{ '\\' }},
CharacterSet{{ '/' }},
}),
@ -464,7 +464,7 @@ describe("LexItemSet::transitions()", [&]() {
Transition{
LexItemSet({
LexItem(Symbol::non_terminal(1), CharacterSet{{ '/' }}),
LexItem(Symbol::non_terminal(1), Seq::build({ CharacterSet{{ '/' }}, CharacterSet{{ '/' }} })),
LexItem(Symbol::non_terminal(1), Rule::seq({ CharacterSet{{ '/' }}, CharacterSet{{ '/' }} })),
}),
PrecedenceRange(),
false

View file

@ -24,24 +24,24 @@ describe("rule_can_be_blank", [&]() {
});
it("returns true for choices iff one or more sides can be blank", [&]() {
rule = Choice::build({ CharacterSet{{'x'}}, Blank{} });
rule = Rule::choice({ CharacterSet{{'x'}}, Blank{} });
AssertThat(rule_can_be_blank(rule), IsTrue());
rule = Choice::build({ Blank{}, CharacterSet{{'x'}} });
rule = Rule::choice({ Blank{}, CharacterSet{{'x'}} });
AssertThat(rule_can_be_blank(rule), IsTrue());
rule = Choice::build({ CharacterSet{{'x'}}, CharacterSet{{'y'}} });
rule = Rule::choice({ CharacterSet{{'x'}}, CharacterSet{{'y'}} });
AssertThat(rule_can_be_blank(rule), IsFalse());
});
it("returns true for sequences iff both sides can be blank", [&]() {
rule = Seq::build({ Blank{}, CharacterSet{{'x'}} });
rule = Rule::seq({ Blank{}, CharacterSet{{'x'}} });
AssertThat(rule_can_be_blank(rule), IsFalse());
rule = Seq::build({ CharacterSet{{'x'}}, Blank{} });
rule = Rule::seq({ CharacterSet{{'x'}}, Blank{} });
AssertThat(rule_can_be_blank(rule), IsFalse());
rule = Seq::build({ Blank{}, Choice::build({ CharacterSet{{'x'}}, Blank{} }) });
rule = Rule::seq({ Blank{}, Rule::choice({ CharacterSet{{'x'}}, Blank{} }) });
AssertThat(rule_can_be_blank(rule), IsTrue());
});

View file

@ -27,8 +27,8 @@ describe("expand_repeats", []() {
AssertThat(result.variables, Equals(vector<Variable>{
Variable{"rule0", VariableTypeNamed, Symbol::non_terminal(1)},
Variable{"rule0_repeat1", VariableTypeAuxiliary, Choice::build({
Seq::build({ Symbol::non_terminal(1), Symbol::terminal(0) }),
Variable{"rule0_repeat1", VariableTypeAuxiliary, Rule::choice({
Rule::seq({ Symbol::non_terminal(1), Symbol::terminal(0) }),
Symbol::terminal(0),
})},
}));
@ -37,7 +37,7 @@ describe("expand_repeats", []() {
it("replaces repeats inside of sequences", [&]() {
InitialSyntaxGrammar grammar{
{
Variable{"rule0", VariableTypeNamed, Seq::build({
Variable{"rule0", VariableTypeNamed, Rule::seq({
Symbol::terminal(10),
Repeat{Symbol::terminal(11)},
})},
@ -48,12 +48,12 @@ describe("expand_repeats", []() {
auto result = expand_repeats(grammar);
AssertThat(result.variables, Equals(vector<Variable>{
Variable{"rule0", VariableTypeNamed, Seq::build({
Variable{"rule0", VariableTypeNamed, Rule::seq({
Symbol::terminal(10),
Symbol::non_terminal(1),
})},
Variable{"rule0_repeat1", VariableTypeAuxiliary, Choice::build({
Seq::build({ Symbol::non_terminal(1), Symbol::terminal(11) }),
Variable{"rule0_repeat1", VariableTypeAuxiliary, Rule::choice({
Rule::seq({ Symbol::non_terminal(1), Symbol::terminal(11) }),
Symbol::terminal(11)
})},
}));
@ -62,7 +62,7 @@ describe("expand_repeats", []() {
it("replaces repeats inside of choices", [&]() {
InitialSyntaxGrammar grammar{
{
Variable{"rule0", VariableTypeNamed, Choice::build({
Variable{"rule0", VariableTypeNamed, Rule::choice({
Symbol::terminal(10),
Repeat{Symbol::terminal(11)}
})},
@ -73,12 +73,12 @@ describe("expand_repeats", []() {
auto result = expand_repeats(grammar);
AssertThat(result.variables, Equals(vector<Variable>{
Variable{"rule0", VariableTypeNamed, Choice::build({
Variable{"rule0", VariableTypeNamed, Rule::choice({
Symbol::terminal(10),
Symbol::non_terminal(1),
})},
Variable{"rule0_repeat1", VariableTypeAuxiliary, Choice::build({
Seq::build({ Symbol::non_terminal(1), Symbol::terminal(11) }),
Variable{"rule0_repeat1", VariableTypeAuxiliary, Rule::choice({
Rule::seq({ Symbol::non_terminal(1), Symbol::terminal(11) }),
Symbol::terminal(11),
})},
}));
@ -87,11 +87,11 @@ describe("expand_repeats", []() {
it("does not create redundant auxiliary rules", [&]() {
InitialSyntaxGrammar grammar{
{
Variable{"rule0", VariableTypeNamed, Choice::build({
Seq::build({ Symbol::terminal(1), Repeat{Symbol::terminal(4)} }),
Seq::build({ Symbol::terminal(2), Repeat{Symbol::terminal(4)} }),
Variable{"rule0", VariableTypeNamed, Rule::choice({
Rule::seq({ Symbol::terminal(1), Repeat{Symbol::terminal(4)} }),
Rule::seq({ Symbol::terminal(2), Repeat{Symbol::terminal(4)} }),
})},
Variable{"rule1", VariableTypeNamed, Seq::build({
Variable{"rule1", VariableTypeNamed, Rule::seq({
Symbol::terminal(3),
Repeat{Symbol::terminal(4)}
})},
@ -102,16 +102,16 @@ describe("expand_repeats", []() {
auto result = expand_repeats(grammar);
AssertThat(result.variables, Equals(vector<Variable>{
Variable{"rule0", VariableTypeNamed, Choice::build({
Seq::build({ Symbol::terminal(1), Symbol::non_terminal(2) }),
Seq::build({ Symbol::terminal(2), Symbol::non_terminal(2) }),
Variable{"rule0", VariableTypeNamed, Rule::choice({
Rule::seq({ Symbol::terminal(1), Symbol::non_terminal(2) }),
Rule::seq({ Symbol::terminal(2), Symbol::non_terminal(2) }),
})},
Variable{"rule1", VariableTypeNamed, Seq::build({
Variable{"rule1", VariableTypeNamed, Rule::seq({
Symbol::terminal(3),
Symbol::non_terminal(2),
})},
Variable{"rule0_repeat1", VariableTypeAuxiliary, Choice::build({
Seq::build({ Symbol::non_terminal(2), Symbol::terminal(4) }),
Variable{"rule0_repeat1", VariableTypeAuxiliary, Rule::choice({
Rule::seq({ Symbol::non_terminal(2), Symbol::terminal(4) }),
Symbol::terminal(4),
})},
}));
@ -120,7 +120,7 @@ describe("expand_repeats", []() {
it("can replace multiple repeats in the same rule", [&]() {
InitialSyntaxGrammar grammar{
{
Variable{"rule0", VariableTypeNamed, Seq::build({
Variable{"rule0", VariableTypeNamed, Rule::seq({
Repeat{Symbol::terminal(10)},
Repeat{Symbol::terminal(11)},
})},
@ -131,16 +131,16 @@ describe("expand_repeats", []() {
auto result = expand_repeats(grammar);
AssertThat(result.variables, Equals(vector<Variable>{
Variable{"rule0", VariableTypeNamed, Seq::build({
Variable{"rule0", VariableTypeNamed, Rule::seq({
Symbol::non_terminal(1),
Symbol::non_terminal(2),
})},
Variable{"rule0_repeat1", VariableTypeAuxiliary, Choice::build({
Seq::build({ Symbol::non_terminal(1), Symbol::terminal(10) }),
Variable{"rule0_repeat1", VariableTypeAuxiliary, Rule::choice({
Rule::seq({ Symbol::non_terminal(1), Symbol::terminal(10) }),
Symbol::terminal(10),
})},
Variable{"rule0_repeat2", VariableTypeAuxiliary, Choice::build({
Seq::build({ Symbol::non_terminal(2), Symbol::terminal(11) }),
Variable{"rule0_repeat2", VariableTypeAuxiliary, Rule::choice({
Rule::seq({ Symbol::non_terminal(2), Symbol::terminal(11) }),
Symbol::terminal(11),
})},
}));
@ -160,12 +160,12 @@ describe("expand_repeats", []() {
AssertThat(result.variables, Equals(vector<Variable>{
Variable{"rule0", VariableTypeNamed, Symbol::non_terminal(2)},
Variable{"rule1", VariableTypeNamed, Symbol::non_terminal(3)},
Variable{"rule0_repeat1", VariableTypeAuxiliary, Choice::build({
Seq::build({ Symbol::non_terminal(2), Symbol::terminal(10) }),
Variable{"rule0_repeat1", VariableTypeAuxiliary, Rule::choice({
Rule::seq({ Symbol::non_terminal(2), Symbol::terminal(10) }),
Symbol::terminal(10),
})},
Variable{"rule1_repeat1", VariableTypeAuxiliary, Choice::build({
Seq::build({ Symbol::non_terminal(3), Symbol::terminal(11) }),
Variable{"rule1_repeat1", VariableTypeAuxiliary, Rule::choice({
Rule::seq({ Symbol::non_terminal(3), Symbol::terminal(11) }),
Symbol::terminal(11),
})},
}));

View file

@ -17,14 +17,14 @@ describe("expand_tokens", []() {
describe("string rules", [&]() {
it("replaces strings with sequences of character sets", [&]() {
AssertThat(
expand_token(Seq::build({
expand_token(Rule::seq({
String{"a"},
String{"bcd"},
String{"e"}
})).rule,
Equals(*Seq::build({
Equals(Rule::seq({
CharacterSet{{ 'a' }},
Seq::build({
Rule::seq({
CharacterSet{{ 'b' }},
CharacterSet{{ 'c' }},
CharacterSet{{ 'd' }},
@ -36,7 +36,7 @@ describe("expand_tokens", []() {
it("handles strings containing non-ASCII UTF8 characters", [&]() {
AssertThat(
expand_token(String{"\u03B1 \u03B2"}).rule,
Equals(*Seq::build({
Equals(Rule::seq({
CharacterSet{{ 945 }},
CharacterSet{{ ' ' }},
CharacterSet{{ 946 }},
@ -48,12 +48,12 @@ describe("expand_tokens", []() {
describe("regexp rules", [&]() {
it("replaces regexps with the equivalent rule tree", [&]() {
AssertThat(
expand_token(Seq::build({
expand_token(Rule::seq({
String{"a"},
Pattern{"x+"},
String{"b"},
})).rule,
Equals(*Seq::build({
Equals(Rule::seq({
CharacterSet{{'a'}},
Repeat{CharacterSet{{ 'x' }}},
CharacterSet{{'b'}},
@ -72,7 +72,7 @@ describe("expand_tokens", []() {
it("returns an error when the grammar contains an invalid regex", [&]() {
AssertThat(
expand_token(Seq::build({
expand_token(Rule::seq({
Pattern{"("},
String{"xyz"},
Pattern{"["},

View file

@ -9,9 +9,9 @@ using prepare_grammar::extract_choices;
describe("extract_choices", []() {
it("expands rules containing choices into multiple rules", [&]() {
auto rule = Seq::build({
auto rule = Rule::seq({
Symbol::terminal(1),
Choice::build({
Rule::choice({
Symbol::terminal(2),
Symbol::terminal(3),
Symbol::terminal(4)
@ -22,14 +22,14 @@ describe("extract_choices", []() {
auto result = extract_choices(rule);
AssertThat(result, Equals(vector<Rule>({
Seq::build({Symbol::terminal(1), Symbol::terminal(2), Symbol::terminal(5)}),
Seq::build({Symbol::terminal(1), Symbol::terminal(3), Symbol::terminal(5)}),
Seq::build({Symbol::terminal(1), Symbol::terminal(4), Symbol::terminal(5)}),
Rule::seq({Symbol::terminal(1), Symbol::terminal(2), Symbol::terminal(5)}),
Rule::seq({Symbol::terminal(1), Symbol::terminal(3), Symbol::terminal(5)}),
Rule::seq({Symbol::terminal(1), Symbol::terminal(4), Symbol::terminal(5)}),
})));
});
it("handles metadata rules", [&]() {
auto rule = Metadata::prec(5, Choice::build({
auto rule = Metadata::prec(5, Rule::choice({
Symbol::terminal(2),
Symbol::terminal(3),
Symbol::terminal(4)
@ -43,9 +43,9 @@ describe("extract_choices", []() {
});
it("handles nested choices", [&]() {
auto rule = Choice::build({
Seq::build({
Choice::build({
auto rule = Rule::choice({
Rule::seq({
Rule::choice({
Symbol::terminal(1),
Symbol::terminal(2)
}),
@ -55,8 +55,8 @@ describe("extract_choices", []() {
});
AssertThat(extract_choices(rule), Equals(vector<Rule>({
Seq::build({Symbol::terminal(1), Symbol::terminal(3)}),
Seq::build({Symbol::terminal(2), Symbol::terminal(3)}),
Rule::seq({Symbol::terminal(1), Symbol::terminal(3)}),
Rule::seq({Symbol::terminal(2), Symbol::terminal(3)}),
Symbol::terminal(4),
})));
});

View file

@ -21,13 +21,13 @@ describe("extract_tokens", []() {
InternedVariable{
"rule_A",
VariableTypeNamed,
Repeat{Seq::build({
Repeat{Rule::seq({
String{"ab"},
Pattern{"cd+"},
Choice::build({
Rule::choice({
Symbol::non_terminal(1),
Symbol::non_terminal(2),
Metadata::token(Repeat{Choice::build({
Metadata::token(Repeat{Rule::choice({
String{"ef"},
String{"g"}
})}),
@ -42,7 +42,7 @@ describe("extract_tokens", []() {
InternedVariable{
"rule_C",
VariableTypeNamed,
Choice::build({ String{"i"}, Blank{} })
Rule::choice({ String{"i"}, Blank{} })
},
InternedVariable{
"rule_D",
@ -65,7 +65,7 @@ describe("extract_tokens", []() {
InitialSyntaxVariable{
"rule_A",
VariableTypeNamed,
Repeat{Seq::build({
Repeat{Rule::seq({
// This string is now the first token in the lexical grammar.
Symbol::terminal(0),
@ -73,7 +73,7 @@ describe("extract_tokens", []() {
// This pattern is now the second rule in the lexical grammar.
Symbol::terminal(1),
Choice::build({
Rule::choice({
// Rule 1, which this symbol pointed to, has been moved to the
// lexical grammar.
Symbol::terminal(3),
@ -91,7 +91,7 @@ describe("extract_tokens", []() {
InitialSyntaxVariable{
"rule_C",
VariableTypeNamed,
Choice::build({Symbol::terminal(4), Blank{}})
Rule::choice({Symbol::terminal(4), Blank{}})
},
InitialSyntaxVariable{
@ -122,7 +122,7 @@ describe("extract_tokens", []() {
LexicalVariable{
"/(ef|g)+/",
VariableTypeAuxiliary,
Repeat{Choice::build({
Repeat{Rule::choice({
Seq{CharacterSet{{'e'}}, CharacterSet{{'f'}}},
CharacterSet{{'g'}},
})},
@ -153,7 +153,7 @@ describe("extract_tokens", []() {
{
"rule_A",
VariableTypeNamed,
Seq::build({
Rule::seq({
String{"ab"},
Symbol::non_terminal(1),
String{"ab"},
@ -172,7 +172,7 @@ describe("extract_tokens", []() {
InitialSyntaxVariable{
"rule_A",
VariableTypeNamed,
Seq::build({
Rule::seq({
Symbol::terminal(0),
Symbol::non_terminal(1),
Symbol::terminal(0)
@ -195,7 +195,7 @@ describe("extract_tokens", []() {
InternedVariable{
"rule_A",
VariableTypeNamed,
Seq::build({ Symbol::non_terminal(1), String{"ab"} })
Rule::seq({ Symbol::non_terminal(1), String{"ab"} })
},
InternedVariable{
"rule_B",
@ -205,7 +205,7 @@ describe("extract_tokens", []() {
InternedVariable{
"rule_C",
VariableTypeNamed,
Seq::build({ String{"ef"}, String{"cd"} })
Rule::seq({ String{"ef"}, String{"cd"} })
},
}, {}, {}, {}});
@ -216,7 +216,7 @@ describe("extract_tokens", []() {
InitialSyntaxVariable{
"rule_A",
VariableTypeNamed,
Seq::build({ Symbol::non_terminal(1), Symbol::terminal(0) })
Rule::seq({ Symbol::non_terminal(1), Symbol::terminal(0) })
},
InitialSyntaxVariable{
"rule_B",
@ -226,7 +226,7 @@ describe("extract_tokens", []() {
InitialSyntaxVariable{
"rule_C",
VariableTypeNamed,
Seq::build({ Symbol::terminal(2), Symbol::terminal(1) })
Rule::seq({ Symbol::terminal(2), Symbol::terminal(1) })
},
})));
@ -335,7 +335,7 @@ describe("extract_tokens", []() {
InternedVariable{
"rule_A",
VariableTypeNamed,
Seq::build({ String{"w"}, String{"x"}, Symbol::non_terminal(1) })
Rule::seq({ String{"w"}, String{"x"}, Symbol::non_terminal(1) })
},
InternedVariable{
"rule_B",
@ -370,12 +370,12 @@ describe("extract_tokens", []() {
InternedVariable{
"rule_A",
VariableTypeNamed,
Seq::build({ String{"x"}, Symbol::non_terminal(1) })
Rule::seq({ String{"x"}, Symbol::non_terminal(1) })
},
InternedVariable{
"rule_B",
VariableTypeNamed,
Seq::build({ String{"y"}, String{"z"} })
Rule::seq({ String{"y"}, String{"z"} })
},
},
{
@ -398,7 +398,7 @@ describe("extract_tokens", []() {
{"rule_B", VariableTypeNamed, String{"y"}},
},
{
Choice::build({ Symbol::non_terminal(1), Blank{} })
Rule::choice({ Symbol::non_terminal(1), Blank{} })
},
{},
{}
@ -417,12 +417,12 @@ describe("extract_tokens", []() {
{
"rule_A",
VariableTypeNamed,
Seq::build({ String{"x"}, Symbol::non_terminal(1) })
Rule::seq({ String{"x"}, Symbol::non_terminal(1) })
},
{
"rule_B",
VariableTypeNamed,
Seq::build({ String{"y"}, String{"z"} })
Rule::seq({ String{"y"}, String{"z"} })
},
},
{},

View file

@ -14,12 +14,12 @@ describe("flatten_grammar", []() {
SyntaxVariable result = flatten_rule({
"test",
VariableTypeNamed,
Seq::build({
Rule::seq({
Symbol::non_terminal(1),
Metadata::prec_left(101, Seq::build({
Metadata::prec_left(101, Rule::seq({
Symbol::non_terminal(2),
Choice::build({
Metadata::prec_right(102, Seq::build({
Rule::choice({
Metadata::prec_right(102, Rule::seq({
Symbol::non_terminal(3),
Symbol::non_terminal(4)
})),
@ -56,7 +56,7 @@ describe("flatten_grammar", []() {
SyntaxVariable result = flatten_rule({
"test1",
VariableTypeNamed,
Metadata::prec_left(101, Seq::build({
Metadata::prec_left(101, Rule::seq({
Symbol::non_terminal(1),
Symbol::non_terminal(2),
}))
@ -72,7 +72,7 @@ describe("flatten_grammar", []() {
result = flatten_rule({
"test2",
VariableTypeNamed,
Metadata::prec_left(101, Seq::build({
Metadata::prec_left(101, Rule::seq({
Symbol::non_terminal(1),
}))
});

View file

@ -13,7 +13,7 @@ describe("intern_symbols", []() {
it("replaces named symbols with numerically-indexed symbols", [&]() {
InputGrammar grammar{
{
{"x", VariableTypeNamed, Choice::build({ NamedSymbol{"y"}, NamedSymbol{"_z"} })},
{"x", VariableTypeNamed, Rule::choice({ NamedSymbol{"y"}, NamedSymbol{"_z"} })},
{"y", VariableTypeNamed, NamedSymbol{"_z"}},
{"_z", VariableTypeNamed, String{"stuff"}}
}, {}, {}, {}
@ -23,7 +23,7 @@ describe("intern_symbols", []() {
AssertThat(result.second, Equals(CompileError::none()));
AssertThat(result.first.variables, Equals(vector<prepare_grammar::InternedGrammar::Variable>{
{"x", VariableTypeNamed, Choice::build({ Symbol::non_terminal(1), Symbol::non_terminal(2) })},
{"x", VariableTypeNamed, Rule::choice({ Symbol::non_terminal(1), Symbol::non_terminal(2) })},
{"y", VariableTypeNamed, Symbol::non_terminal(2)},
{"_z", VariableTypeHidden, String{"stuff"}},
}));
@ -47,7 +47,7 @@ describe("intern_symbols", []() {
it("translates the grammar's optional 'extra_tokens' to numerical symbols", [&]() {
InputGrammar grammar{
{
{"x", VariableTypeNamed, Choice::build({ NamedSymbol{"y"}, NamedSymbol{"z"} })},
{"x", VariableTypeNamed, Rule::choice({ NamedSymbol{"y"}, NamedSymbol{"z"} })},
{"y", VariableTypeNamed, NamedSymbol{"z"}},
{"z", VariableTypeNamed, String{"stuff"}}
},
@ -67,7 +67,7 @@ describe("intern_symbols", []() {
it("records any rule names that match external token names", [&]() {
InputGrammar grammar{
{
{"x", VariableTypeNamed, Choice::build({ NamedSymbol{"y"}, NamedSymbol{"z"} })},
{"x", VariableTypeNamed, Rule::choice({ NamedSymbol{"y"}, NamedSymbol{"z"} })},
{"y", VariableTypeNamed, NamedSymbol{"z"}},
{"z", VariableTypeNamed, String{"stuff"}},
},

View file

@ -29,7 +29,7 @@ describe("parse_regex", []() {
{
"character classes",
"\\w-\\d-\\s-\\W-\\D-\\S",
Seq::build({
Rule::seq({
CharacterSet{{
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
@ -60,7 +60,7 @@ describe("parse_regex", []() {
{
"choices",
"ab|cd|ef",
Choice::build({
Rule::choice({
Seq{
CharacterSet{{'a'}},
CharacterSet{{'b'}}
@ -79,7 +79,7 @@ describe("parse_regex", []() {
{
"simple sequences",
"abc",
Seq::build({
Rule::seq({
CharacterSet{{'a'}},
CharacterSet{{'b'}},
CharacterSet{{'c'}}
@ -113,12 +113,12 @@ describe("parse_regex", []() {
{
"character groups in sequences",
"x([^x]|\\\\x)*x",
Seq::build({
Rule::seq({
CharacterSet{{'x'}},
Choice::build({
Repeat{Choice::build({
Rule::choice({
Repeat{Rule::choice({
CharacterSet().include_all().exclude('x'),
Seq::build({
Rule::seq({
CharacterSet{{'\\'}},
CharacterSet{{'x'}}
})
@ -132,8 +132,8 @@ describe("parse_regex", []() {
{
"choices in sequences",
"(a|b)cd",
Seq::build({
Choice::build({
Rule::seq({
Rule::choice({
CharacterSet{{'a'}},
CharacterSet{{'b'}} }),
CharacterSet{{'c'}},
@ -143,7 +143,7 @@ describe("parse_regex", []() {
{
"escaped parentheses",
"a\\(b",
Seq::build({
Rule::seq({
CharacterSet{{'a'}},
CharacterSet{{'('}},
CharacterSet{{'b'}},
@ -153,7 +153,7 @@ describe("parse_regex", []() {
{
"escaped periods",
"a\\.",
Seq::build({
Rule::seq({
CharacterSet{{'a'}},
CharacterSet{{'.'}},
})
@ -162,7 +162,7 @@ describe("parse_regex", []() {
{
"escaped characters",
"\\t\\n\\r",
Seq::build({
Rule::seq({
CharacterSet{{'\t'}},
CharacterSet{{'\n'}},
CharacterSet{{'\r'}},
@ -172,22 +172,22 @@ describe("parse_regex", []() {
{
"plus repeats",
"(ab)+(cd)+",
Seq::build({
Repeat{Seq::build({ CharacterSet{{'a'}}, CharacterSet{{'b'}} })},
Repeat{Seq::build({ CharacterSet{{'c'}}, CharacterSet{{'d'}} })},
Rule::seq({
Repeat{Rule::seq({ CharacterSet{{'a'}}, CharacterSet{{'b'}} })},
Repeat{Rule::seq({ CharacterSet{{'c'}}, CharacterSet{{'d'}} })},
})
},
{
"asterix repeats",
"(ab)*(cd)*",
Seq::build({
Choice::build({
Repeat{Seq::build({ CharacterSet{{'a'}}, CharacterSet{{'b'}} })},
Rule::seq({
Rule::choice({
Repeat{Rule::seq({ CharacterSet{{'a'}}, CharacterSet{{'b'}} })},
Blank{},
}),
Choice::build({
Repeat{Seq::build({ CharacterSet{{'c'}}, CharacterSet{{'d'}} })},
Rule::choice({
Repeat{Rule::seq({ CharacterSet{{'c'}}, CharacterSet{{'d'}} })},
Blank{},
}),
})
@ -196,10 +196,10 @@ describe("parse_regex", []() {
{
"optional rules",
"a(bc)?",
Seq::build({
Rule::seq({
CharacterSet{{'a'}},
Choice::build({
Seq::build({
Rule::choice({
Rule::seq({
CharacterSet{{'b'}},
CharacterSet{{'c'}},
}),
@ -211,11 +211,11 @@ describe("parse_regex", []() {
{
"choices containing negated character classes",
"/([^/]|(\\\\/))+/",
Seq::build({
Rule::seq({
CharacterSet{{'/'}},
Repeat{Choice::build({
Repeat{Rule::choice({
CharacterSet().include_all().exclude('/'),
Seq::build({
Rule::seq({
CharacterSet{{'\\'}},
CharacterSet{{'/'}},
}),

View file

@ -8,38 +8,38 @@ START_TEST
describe("Choice", []() {
describe("constructing choices", [&]() {
it("eliminates duplicate members", [&]() {
Rule rule = Choice::build({
Seq::build({ NamedSymbol{"one"}, NamedSymbol{"two"} }),
Rule rule = Rule::choice({
Rule::seq({ NamedSymbol{"one"}, NamedSymbol{"two"} }),
NamedSymbol{"three"},
Seq::build({ NamedSymbol{"one"}, NamedSymbol{"two"} })
Rule::seq({ NamedSymbol{"one"}, NamedSymbol{"two"} })
});
AssertThat(rule, Equals(Rule(Choice{{
Seq::build({ NamedSymbol{"one"}, NamedSymbol{"two"} }),
Rule::seq({ NamedSymbol{"one"}, NamedSymbol{"two"} }),
NamedSymbol{"three"},
}})));
rule = Choice::build({
rule = Rule::choice({
Blank{},
Blank{},
Choice::build({
Rule::choice({
Blank{},
NamedSymbol{"four"}
})
});
AssertThat(rule, Equals(*Choice::build({Blank{}, NamedSymbol{"four"}})));
AssertThat(rule, Equals(Rule::choice({Blank{}, NamedSymbol{"four"}})));
});
it("eliminates duplicates within nested choices", [&]() {
Rule rule = Choice::build({
Seq::build({
Rule rule = Rule::choice({
Rule::seq({
NamedSymbol{"one"},
NamedSymbol{"two"}
}),
Choice::build({
Rule::choice({
NamedSymbol{"three"},
Seq::build({
Rule::seq({
NamedSymbol{"one"},
NamedSymbol{"two"}
})
@ -47,7 +47,7 @@ describe("Choice", []() {
});
AssertThat(rule, Equals(Rule(Choice{{
Seq::build({
Rule::seq({
NamedSymbol{"one"},
NamedSymbol{"two"},
}),
@ -56,9 +56,9 @@ describe("Choice", []() {
});
it("doesn't construct a choice if there's only one unique member", [&]() {
Rule rule = Choice::build({
Rule rule = Rule::choice({
NamedSymbol{"one"},
Choice::build({
Rule::choice({
NamedSymbol{"one"},
})
});

View file

@ -9,8 +9,8 @@ describe("Repeat", []() {
describe("constructing repeats", [&]() {
it("doesn't create redundant repeats", [&]() {
Rule symbol = Symbol::non_terminal(1);
Rule repeat = Repeat::build(Rule(symbol));
Rule outer_repeat = Repeat::build(Rule(repeat));
Rule repeat = Rule::repeat(Rule(symbol));
Rule outer_repeat = Rule::repeat(Rule(repeat));
AssertThat(repeat, !Equals(symbol));
AssertThat(outer_repeat, Equals(repeat));