Browse Source

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

Makes the rule names a bit longer but also clearer.
redux
David Majda 8 years ago
committed by David Majda
parent
commit
5c6f4dd38b
  1. 54
      lib/parser.js
  2. 24
      spec/parser.spec.js
  3. 40
      src/parser.pegjs

54
lib/parser.js

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

24
spec/parser.spec.js

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

40
src/parser.pegjs

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

Loading…
Cancel
Save