PEG.js grammar: Append |Expression| to expression rule names

Makes the rule names a bit longer but also clearer.
redux
David Majda 10 years ago committed by David Majda
parent 27c2d26203
commit 5c6f4dd38b

@ -705,7 +705,7 @@ module.exports = (function() {
if (s4 !== peg$FAILED) { if (s4 !== peg$FAILED) {
s5 = peg$parse__(); s5 = peg$parse__();
if (s5 !== peg$FAILED) { if (s5 !== peg$FAILED) {
s6 = peg$parseChoice(); s6 = peg$parseChoiceExpression();
if (s6 !== peg$FAILED) { if (s6 !== peg$FAILED) {
s7 = peg$parseEOS(); s7 = peg$parseEOS();
if (s7 !== peg$FAILED) { if (s7 !== peg$FAILED) {
@ -744,11 +744,11 @@ module.exports = (function() {
return s0; return s0;
} }
function peg$parseChoice() { function peg$parseChoiceExpression() {
var s0, s1, s2, s3, s4, s5, s6, s7; var s0, s1, s2, s3, s4, s5, s6, s7;
s0 = peg$currPos; s0 = peg$currPos;
s1 = peg$parseSequence(); s1 = peg$parseSequenceExpression();
if (s1 !== peg$FAILED) { if (s1 !== peg$FAILED) {
s2 = []; s2 = [];
s3 = peg$currPos; s3 = peg$currPos;
@ -764,7 +764,7 @@ module.exports = (function() {
if (s5 !== peg$FAILED) { if (s5 !== peg$FAILED) {
s6 = peg$parse__(); s6 = peg$parse__();
if (s6 !== peg$FAILED) { if (s6 !== peg$FAILED) {
s7 = peg$parseSequence(); s7 = peg$parseSequenceExpression();
if (s7 !== peg$FAILED) { if (s7 !== peg$FAILED) {
s4 = [s4, s5, s6, s7]; s4 = [s4, s5, s6, s7];
s3 = s4; s3 = s4;
@ -799,7 +799,7 @@ module.exports = (function() {
if (s5 !== peg$FAILED) { if (s5 !== peg$FAILED) {
s6 = peg$parse__(); s6 = peg$parse__();
if (s6 !== peg$FAILED) { if (s6 !== peg$FAILED) {
s7 = peg$parseSequence(); s7 = peg$parseSequenceExpression();
if (s7 !== peg$FAILED) { if (s7 !== peg$FAILED) {
s4 = [s4, s5, s6, s7]; s4 = [s4, s5, s6, s7];
s3 = s4; s3 = s4;
@ -836,17 +836,17 @@ module.exports = (function() {
return s0; return s0;
} }
function peg$parseSequence() { function peg$parseSequenceExpression() {
var s0, s1, s2, s3, s4, s5; var s0, s1, s2, s3, s4, s5;
s0 = peg$currPos; s0 = peg$currPos;
s1 = peg$parseLabeled(); s1 = peg$parseLabeledExpression();
if (s1 !== peg$FAILED) { if (s1 !== peg$FAILED) {
s2 = []; s2 = [];
s3 = peg$currPos; s3 = peg$currPos;
s4 = peg$parse__(); s4 = peg$parse__();
if (s4 !== peg$FAILED) { if (s4 !== peg$FAILED) {
s5 = peg$parseLabeled(); s5 = peg$parseLabeledExpression();
if (s5 !== peg$FAILED) { if (s5 !== peg$FAILED) {
s4 = [s4, s5]; s4 = [s4, s5];
s3 = s4; s3 = s4;
@ -863,7 +863,7 @@ module.exports = (function() {
s3 = peg$currPos; s3 = peg$currPos;
s4 = peg$parse__(); s4 = peg$parse__();
if (s4 !== peg$FAILED) { if (s4 !== peg$FAILED) {
s5 = peg$parseLabeled(); s5 = peg$parseLabeledExpression();
if (s5 !== peg$FAILED) { if (s5 !== peg$FAILED) {
s4 = [s4, s5]; s4 = [s4, s5];
s3 = s4; s3 = s4;
@ -902,13 +902,13 @@ module.exports = (function() {
} }
if (s0 === peg$FAILED) { if (s0 === peg$FAILED) {
s0 = peg$currPos; s0 = peg$currPos;
s1 = peg$parseLabeled(); s1 = peg$parseLabeledExpression();
if (s1 !== peg$FAILED) { if (s1 !== peg$FAILED) {
s2 = []; s2 = [];
s3 = peg$currPos; s3 = peg$currPos;
s4 = peg$parse__(); s4 = peg$parse__();
if (s4 !== peg$FAILED) { if (s4 !== peg$FAILED) {
s5 = peg$parseLabeled(); s5 = peg$parseLabeledExpression();
if (s5 !== peg$FAILED) { if (s5 !== peg$FAILED) {
s4 = [s4, s5]; s4 = [s4, s5];
s3 = s4; s3 = s4;
@ -925,7 +925,7 @@ module.exports = (function() {
s3 = peg$currPos; s3 = peg$currPos;
s4 = peg$parse__(); s4 = peg$parse__();
if (s4 !== peg$FAILED) { if (s4 !== peg$FAILED) {
s5 = peg$parseLabeled(); s5 = peg$parseLabeledExpression();
if (s5 !== peg$FAILED) { if (s5 !== peg$FAILED) {
s4 = [s4, s5]; s4 = [s4, s5];
s3 = s4; s3 = s4;
@ -955,7 +955,7 @@ module.exports = (function() {
return s0; return s0;
} }
function peg$parseLabeled() { function peg$parseLabeledExpression() {
var s0, s1, s2, s3, s4, s5; var s0, s1, s2, s3, s4, s5;
s0 = peg$currPos; s0 = peg$currPos;
@ -973,7 +973,7 @@ module.exports = (function() {
if (s3 !== peg$FAILED) { if (s3 !== peg$FAILED) {
s4 = peg$parse__(); s4 = peg$parse__();
if (s4 !== peg$FAILED) { if (s4 !== peg$FAILED) {
s5 = peg$parsePrefixed(); s5 = peg$parsePrefixedExpression();
if (s5 !== peg$FAILED) { if (s5 !== peg$FAILED) {
peg$reportedPos = s0; peg$reportedPos = s0;
s1 = peg$c15(s1, s5); s1 = peg$c15(s1, s5);
@ -999,13 +999,13 @@ module.exports = (function() {
s0 = peg$c0; s0 = peg$c0;
} }
if (s0 === peg$FAILED) { if (s0 === peg$FAILED) {
s0 = peg$parsePrefixed(); s0 = peg$parsePrefixedExpression();
} }
return s0; return s0;
} }
function peg$parsePrefixed() { function peg$parsePrefixedExpression() {
var s0, s1, s2, s3; var s0, s1, s2, s3;
s0 = peg$currPos; s0 = peg$currPos;
@ -1019,7 +1019,7 @@ module.exports = (function() {
if (s1 !== peg$FAILED) { if (s1 !== peg$FAILED) {
s2 = peg$parse__(); s2 = peg$parse__();
if (s2 !== peg$FAILED) { if (s2 !== peg$FAILED) {
s3 = peg$parseSuffixed(); s3 = peg$parseSuffixedExpression();
if (s3 !== peg$FAILED) { if (s3 !== peg$FAILED) {
peg$reportedPos = s0; peg$reportedPos = s0;
s1 = peg$c18(s3); s1 = peg$c18(s3);
@ -1077,7 +1077,7 @@ module.exports = (function() {
if (s1 !== peg$FAILED) { if (s1 !== peg$FAILED) {
s2 = peg$parse__(); s2 = peg$parse__();
if (s2 !== peg$FAILED) { if (s2 !== peg$FAILED) {
s3 = peg$parseSuffixed(); s3 = peg$parseSuffixedExpression();
if (s3 !== peg$FAILED) { if (s3 !== peg$FAILED) {
peg$reportedPos = s0; peg$reportedPos = s0;
s1 = peg$c22(s3); s1 = peg$c22(s3);
@ -1135,7 +1135,7 @@ module.exports = (function() {
if (s1 !== peg$FAILED) { if (s1 !== peg$FAILED) {
s2 = peg$parse__(); s2 = peg$parse__();
if (s2 !== peg$FAILED) { if (s2 !== peg$FAILED) {
s3 = peg$parseSuffixed(); s3 = peg$parseSuffixedExpression();
if (s3 !== peg$FAILED) { if (s3 !== peg$FAILED) {
peg$reportedPos = s0; peg$reportedPos = s0;
s1 = peg$c26(s3); s1 = peg$c26(s3);
@ -1153,7 +1153,7 @@ module.exports = (function() {
s0 = peg$c0; s0 = peg$c0;
} }
if (s0 === peg$FAILED) { if (s0 === peg$FAILED) {
s0 = peg$parseSuffixed(); s0 = peg$parseSuffixedExpression();
} }
} }
} }
@ -1163,11 +1163,11 @@ module.exports = (function() {
return s0; return s0;
} }
function peg$parseSuffixed() { function peg$parseSuffixedExpression() {
var s0, s1, s2, s3; var s0, s1, s2, s3;
s0 = peg$currPos; s0 = peg$currPos;
s1 = peg$parsePrimary(); s1 = peg$parsePrimaryExpression();
if (s1 !== peg$FAILED) { if (s1 !== peg$FAILED) {
s2 = peg$parse__(); s2 = peg$parse__();
if (s2 !== peg$FAILED) { if (s2 !== peg$FAILED) {
@ -1196,7 +1196,7 @@ module.exports = (function() {
} }
if (s0 === peg$FAILED) { if (s0 === peg$FAILED) {
s0 = peg$currPos; s0 = peg$currPos;
s1 = peg$parsePrimary(); s1 = peg$parsePrimaryExpression();
if (s1 !== peg$FAILED) { if (s1 !== peg$FAILED) {
s2 = peg$parse__(); s2 = peg$parse__();
if (s2 !== peg$FAILED) { if (s2 !== peg$FAILED) {
@ -1225,7 +1225,7 @@ module.exports = (function() {
} }
if (s0 === peg$FAILED) { if (s0 === peg$FAILED) {
s0 = peg$currPos; s0 = peg$currPos;
s1 = peg$parsePrimary(); s1 = peg$parsePrimaryExpression();
if (s1 !== peg$FAILED) { if (s1 !== peg$FAILED) {
s2 = peg$parse__(); s2 = peg$parse__();
if (s2 !== peg$FAILED) { if (s2 !== peg$FAILED) {
@ -1253,7 +1253,7 @@ module.exports = (function() {
s0 = peg$c0; s0 = peg$c0;
} }
if (s0 === peg$FAILED) { if (s0 === peg$FAILED) {
s0 = peg$parsePrimary(); s0 = peg$parsePrimaryExpression();
} }
} }
} }
@ -1261,7 +1261,7 @@ module.exports = (function() {
return s0; return s0;
} }
function peg$parsePrimary() { function peg$parsePrimaryExpression() {
var s0, s1, s2, s3, s4, s5, s6, s7; var s0, s1, s2, s3, s4, s5, s6, s7;
s0 = peg$currPos; s0 = peg$currPos;
@ -1350,7 +1350,7 @@ module.exports = (function() {
if (s1 !== peg$FAILED) { if (s1 !== peg$FAILED) {
s2 = peg$parse__(); s2 = peg$parse__();
if (s2 !== peg$FAILED) { if (s2 !== peg$FAILED) {
s3 = peg$parseChoice(); s3 = peg$parseChoiceExpression();
if (s3 !== peg$FAILED) { if (s3 !== peg$FAILED) {
s4 = peg$parse__(); s4 = peg$parse__();
if (s4 !== peg$FAILED) { if (s4 !== peg$FAILED) {

@ -232,8 +232,8 @@ describe("PEG.js grammar parser", function() {
); );
}); });
/* Canonical Choice is "\"abcd\" / \"efgh\" / \"ijkl\"". */ /* Canonical ChoiceExpression is "\"abcd\" / \"efgh\" / \"ijkl\"". */
it("parses Choice", function() { it("parses ChoiceExpression", function() {
expect('start = "abcd" "efgh" "ijkl"').toParseAs( expect('start = "abcd" "efgh" "ijkl"').toParseAs(
oneRuleGrammar(sequenceOfLiterals) oneRuleGrammar(sequenceOfLiterals)
); );
@ -257,8 +257,8 @@ describe("PEG.js grammar parser", function() {
})); }));
}); });
/* Canonical Sequence is "\"abcd\" \"efgh\" \"ijkl\"". */ /* Canonical SequenceExpression is "\"abcd\" \"efgh\" \"ijkl\"". */
it("parses Sequence", function() { it("parses SequenceExpression", function() {
expect('start = a:"abcd" { code }').toParseAs( expect('start = a:"abcd" { code }').toParseAs(
oneRuleGrammar({ type: "action", expression: labeledAbcd, code: " code " }) oneRuleGrammar({ type: "action", expression: labeledAbcd, code: " code " })
); );
@ -291,16 +291,16 @@ describe("PEG.js grammar parser", function() {
); );
}); });
/* Canonical Labeled is "label:\"abcd\"". */ /* Canonical LabeledExpression is "label:\"abcd\"". */
it("parses Labeled", function() { it("parses LabeledExpression", function() {
expect('start = label:!"abcd"' ).toParseAs(oneRuleGrammar(labeledSimpleNotLiteral)); expect('start = label:!"abcd"' ).toParseAs(oneRuleGrammar(labeledSimpleNotLiteral));
expect('start = label\n:!"abcd"').toParseAs(oneRuleGrammar(labeledSimpleNotLiteral)); expect('start = label\n:!"abcd"').toParseAs(oneRuleGrammar(labeledSimpleNotLiteral));
expect('start = label:\n!"abcd"').toParseAs(oneRuleGrammar(labeledSimpleNotLiteral)); expect('start = label:\n!"abcd"').toParseAs(oneRuleGrammar(labeledSimpleNotLiteral));
expect('start = !"abcd"' ).toParseAs(oneRuleGrammar(simpleNotLiteral)); expect('start = !"abcd"' ).toParseAs(oneRuleGrammar(simpleNotLiteral));
}); });
/* Canonical Prefixed is "!\"abcd\"". */ /* Canonical PrefixedExpression is "!\"abcd\"". */
it("parses Prefixed", function() { it("parses PrefixedExpression", function() {
expect('start = $"abcd"?' ).toParseAs(oneRuleGrammar(textOptionalLiteral)); expect('start = $"abcd"?' ).toParseAs(oneRuleGrammar(textOptionalLiteral));
expect('start = $\n"abcd"?' ).toParseAs(oneRuleGrammar(textOptionalLiteral)); expect('start = $\n"abcd"?' ).toParseAs(oneRuleGrammar(textOptionalLiteral));
expect('start = &{ code }' ).toParseAs(oneRuleGrammar(semanticAnd)); expect('start = &{ code }' ).toParseAs(oneRuleGrammar(semanticAnd));
@ -314,8 +314,8 @@ describe("PEG.js grammar parser", function() {
expect('start = "abcd"?' ).toParseAs(oneRuleGrammar(optionalLiteral)); expect('start = "abcd"?' ).toParseAs(oneRuleGrammar(optionalLiteral));
}); });
/* Canonical Suffixed is "\"abcd\"?". */ /* Canonical SuffixedExpression is "\"abcd\"?". */
it("parses Suffixed", function() { it("parses SuffixedExpression", function() {
expect('start = "abcd"?' ).toParseAs(oneRuleGrammar(optionalLiteral)); expect('start = "abcd"?' ).toParseAs(oneRuleGrammar(optionalLiteral));
expect('start = "abcd"\n?').toParseAs(oneRuleGrammar(optionalLiteral)); expect('start = "abcd"\n?').toParseAs(oneRuleGrammar(optionalLiteral));
expect('start = "abcd"*' ).toParseAs(oneRuleGrammar(zeroOrMoreLiteral)); expect('start = "abcd"*' ).toParseAs(oneRuleGrammar(zeroOrMoreLiteral));
@ -325,8 +325,8 @@ describe("PEG.js grammar parser", function() {
expect('start = "abcd"' ).toParseAs(literalGrammar("abcd")); expect('start = "abcd"' ).toParseAs(literalGrammar("abcd"));
}); });
/* Canonical Primary is "\"abcd\"". */ /* Canonical PrimaryExpression is "\"abcd\"". */
it("parses Primary", function() { it("parses PrimaryExpression", function() {
expect('start = a' ).toParseAs(ruleRefGrammar("a")); expect('start = a' ).toParseAs(ruleRefGrammar("a"));
expect('start = "abcd"' ).toParseAs(literalGrammar("abcd")); expect('start = "abcd"' ).toParseAs(literalGrammar("abcd"));
expect('start = [a-d]' ).toParseAs(classGrammar([["a", "d"]], "[a-d]")); expect('start = [a-d]' ).toParseAs(classGrammar([["a", "d"]], "[a-d]"));

@ -67,17 +67,17 @@ Rule
} }
Expression Expression
= Choice = ChoiceExpression
Choice ChoiceExpression
= first:Sequence rest:(__ "/" __ Sequence)* { = first:SequenceExpression rest:(__ "/" __ SequenceExpression)* {
return rest.length > 0 return rest.length > 0
? { type: "choice", alternatives: buildList(first, rest, 3) } ? { type: "choice", alternatives: buildList(first, rest, 3) }
: first; : first;
} }
Sequence SequenceExpression
= first:Labeled rest:(__ Labeled)* __ code:CodeBlock { = first:LabeledExpression rest:(__ LabeledExpression)* __ code:CodeBlock {
var expression = rest.length > 0 var expression = rest.length > 0
? { type: "sequence", elements: buildList(first, rest, 1) } ? { type: "sequence", elements: buildList(first, rest, 1) }
: first; : first;
@ -87,24 +87,24 @@ Sequence
code: code code: code
}; };
} }
/ first:Labeled rest:(__ Labeled)* { / first:LabeledExpression rest:(__ LabeledExpression)* {
return rest.length > 0 return rest.length > 0
? { type: "sequence", elements: buildList(first, rest, 1) } ? { type: "sequence", elements: buildList(first, rest, 1) }
: first; : first;
} }
Labeled LabeledExpression
= label:Identifier __ ":" __ expression:Prefixed { = label:Identifier __ ":" __ expression:PrefixedExpression {
return { return {
type: "labeled", type: "labeled",
label: label, label: label,
expression: expression expression: expression
}; };
} }
/ Prefixed / PrefixedExpression
Prefixed PrefixedExpression
= "$" __ expression:Suffixed { = "$" __ expression:SuffixedExpression {
return { return {
type: "text", type: "text",
expression: expression expression: expression
@ -116,7 +116,7 @@ Prefixed
code: code code: code
}; };
} }
/ "&" __ expression:Suffixed { / "&" __ expression:SuffixedExpression {
return { return {
type: "simple_and", type: "simple_and",
expression: expression expression: expression
@ -128,36 +128,36 @@ Prefixed
code: code code: code
}; };
} }
/ "!" __ expression:Suffixed { / "!" __ expression:SuffixedExpression {
return { return {
type: "simple_not", type: "simple_not",
expression: expression expression: expression
}; };
} }
/ Suffixed / SuffixedExpression
Suffixed SuffixedExpression
= expression:Primary __ "?" { = expression:PrimaryExpression __ "?" {
return { return {
type: "optional", type: "optional",
expression: expression expression: expression
}; };
} }
/ expression:Primary __ "*" { / expression:PrimaryExpression __ "*" {
return { return {
type: "zero_or_more", type: "zero_or_more",
expression: expression expression: expression
}; };
} }
/ expression:Primary __ "+" { / expression:PrimaryExpression __ "+" {
return { return {
type: "one_or_more", type: "one_or_more",
expression: expression expression: expression
}; };
} }
/ Primary / PrimaryExpression
Primary PrimaryExpression
= name:IdentifierName !(__ (StringLiteral __)? "=") { = name:IdentifierName !(__ (StringLiteral __)? "=") {
return { return {
type: "rule_ref", type: "rule_ref",

Loading…
Cancel
Save