Browse Source

PEG.js grammar: Extract the |RuleReferenceExpression| rule

This makes the |Primary| rule a bit more tidy.
redux
David Majda 8 years ago
parent
commit
da18f6a729
  1. 243
      lib/parser.js
  2. 7
      spec/parser.spec.js
  3. 14
      src/parser.pegjs

243
lib/parser.js

@ -149,18 +149,15 @@ module.exports = (function() {
expression: expression
};
},
peg$c36 = void 0,
peg$c37 = function(name) {
return {
type: "rule_ref",
name: name
};
peg$c36 = "(",
peg$c37 = { type: "literal", value: "(", description: "\"(\"" },
peg$c38 = ")",
peg$c39 = { type: "literal", value: ")", description: "\")\"" },
peg$c40 = function(expression) { return expression; },
peg$c41 = void 0,
peg$c42 = function(name) {
return { type: "rule_ref", name: name }
},
peg$c38 = "(",
peg$c39 = { type: "literal", value: "(", description: "\"(\"" },
peg$c40 = ")",
peg$c41 = { type: "literal", value: ")", description: "\")\"" },
peg$c42 = function(expression) { return expression; },
peg$c43 = { type: "any", description: "any character" },
peg$c44 = { type: "other", description: "whitespace" },
peg$c45 = "\t",
@ -1234,6 +1231,71 @@ module.exports = (function() {
}
function peg$parsePrimaryExpression() {
var s0, s1, s2, s3, s4, s5;
s0 = peg$parseLiteralMatcher();
if (s0 === peg$FAILED) {
s0 = peg$parseCharacterClassMatcher();
if (s0 === peg$FAILED) {
s0 = peg$parseAnyMatcher();
if (s0 === peg$FAILED) {
s0 = peg$parseRuleReferenceExpression();
if (s0 === peg$FAILED) {
s0 = peg$currPos;
if (input.charCodeAt(peg$currPos) === 40) {
s1 = peg$c36;
peg$currPos++;
} else {
s1 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$c37); }
}
if (s1 !== peg$FAILED) {
s2 = peg$parse__();
if (s2 !== peg$FAILED) {
s3 = peg$parseChoiceExpression();
if (s3 !== peg$FAILED) {
s4 = peg$parse__();
if (s4 !== peg$FAILED) {
if (input.charCodeAt(peg$currPos) === 41) {
s5 = peg$c38;
peg$currPos++;
} else {
s5 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$c39); }
}
if (s5 !== peg$FAILED) {
peg$reportedPos = s0;
s1 = peg$c40(s3);
s0 = s1;
} else {
peg$currPos = s0;
s0 = peg$c0;
}
} else {
peg$currPos = s0;
s0 = peg$c0;
}
} else {
peg$currPos = s0;
s0 = peg$c0;
}
} else {
peg$currPos = s0;
s0 = peg$c0;
}
} else {
peg$currPos = s0;
s0 = peg$c0;
}
}
}
}
}
return s0;
}
function peg$parseRuleReferenceExpression() {
var s0, s1, s2, s3, s4, s5, s6, s7;
s0 = peg$currPos;
@ -1287,14 +1349,14 @@ module.exports = (function() {
}
peg$silentFails--;
if (s3 === peg$FAILED) {
s2 = peg$c36;
s2 = peg$c41;
} else {
peg$currPos = s2;
s2 = peg$c0;
}
if (s2 !== peg$FAILED) {
peg$reportedPos = s0;
s1 = peg$c37(s1);
s1 = peg$c42(s1);
s0 = s1;
} else {
peg$currPos = s0;
@ -1304,63 +1366,6 @@ module.exports = (function() {
peg$currPos = s0;
s0 = peg$c0;
}
if (s0 === peg$FAILED) {
s0 = peg$parseLiteralMatcher();
if (s0 === peg$FAILED) {
s0 = peg$parseCharacterClassMatcher();
if (s0 === peg$FAILED) {
s0 = peg$parseAnyMatcher();
if (s0 === peg$FAILED) {
s0 = peg$currPos;
if (input.charCodeAt(peg$currPos) === 40) {
s1 = peg$c38;
peg$currPos++;
} else {
s1 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$c39); }
}
if (s1 !== peg$FAILED) {
s2 = peg$parse__();
if (s2 !== peg$FAILED) {
s3 = peg$parseChoiceExpression();
if (s3 !== peg$FAILED) {
s4 = peg$parse__();
if (s4 !== peg$FAILED) {
if (input.charCodeAt(peg$currPos) === 41) {
s5 = peg$c40;
peg$currPos++;
} else {
s5 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$c41); }
}
if (s5 !== peg$FAILED) {
peg$reportedPos = s0;
s1 = peg$c42(s3);
s0 = s1;
} else {
peg$currPos = s0;
s0 = peg$c0;
}
} else {
peg$currPos = s0;
s0 = peg$c0;
}
} else {
peg$currPos = s0;
s0 = peg$c0;
}
} else {
peg$currPos = s0;
s0 = peg$c0;
}
} else {
peg$currPos = s0;
s0 = peg$c0;
}
}
}
}
}
return s0;
}
@ -1559,7 +1564,7 @@ module.exports = (function() {
}
peg$silentFails--;
if (s5 === peg$FAILED) {
s4 = peg$c36;
s4 = peg$c41;
} else {
peg$currPos = s4;
s4 = peg$c0;
@ -1591,7 +1596,7 @@ module.exports = (function() {
}
peg$silentFails--;
if (s5 === peg$FAILED) {
s4 = peg$c36;
s4 = peg$c41;
} else {
peg$currPos = s4;
s4 = peg$c0;
@ -1665,7 +1670,7 @@ module.exports = (function() {
}
peg$silentFails--;
if (s5 === peg$FAILED) {
s4 = peg$c36;
s4 = peg$c41;
} else {
peg$currPos = s4;
s4 = peg$c0;
@ -1700,7 +1705,7 @@ module.exports = (function() {
}
peg$silentFails--;
if (s5 === peg$FAILED) {
s4 = peg$c36;
s4 = peg$c41;
} else {
peg$currPos = s4;
s4 = peg$c0;
@ -1765,7 +1770,7 @@ module.exports = (function() {
s5 = peg$parseLineTerminator();
peg$silentFails--;
if (s5 === peg$FAILED) {
s4 = peg$c36;
s4 = peg$c41;
} else {
peg$currPos = s4;
s4 = peg$c0;
@ -1791,7 +1796,7 @@ module.exports = (function() {
s5 = peg$parseLineTerminator();
peg$silentFails--;
if (s5 === peg$FAILED) {
s4 = peg$c36;
s4 = peg$c41;
} else {
peg$currPos = s4;
s4 = peg$c0;
@ -1834,7 +1839,7 @@ module.exports = (function() {
s2 = peg$parseReservedWord();
peg$silentFails--;
if (s2 === peg$FAILED) {
s1 = peg$c36;
s1 = peg$c41;
} else {
peg$currPos = s1;
s1 = peg$c0;
@ -2314,7 +2319,7 @@ module.exports = (function() {
}
peg$silentFails--;
if (s2 === peg$FAILED) {
s1 = peg$c36;
s1 = peg$c41;
} else {
peg$currPos = s1;
s1 = peg$c0;
@ -2391,7 +2396,7 @@ module.exports = (function() {
}
peg$silentFails--;
if (s2 === peg$FAILED) {
s1 = peg$c36;
s1 = peg$c41;
} else {
peg$currPos = s1;
s1 = peg$c0;
@ -2591,7 +2596,7 @@ module.exports = (function() {
}
peg$silentFails--;
if (s2 === peg$FAILED) {
s1 = peg$c36;
s1 = peg$c41;
} else {
peg$currPos = s1;
s1 = peg$c0;
@ -2689,7 +2694,7 @@ module.exports = (function() {
s3 = peg$parseDecimalDigit();
peg$silentFails--;
if (s3 === peg$FAILED) {
s2 = peg$c36;
s2 = peg$c41;
} else {
peg$currPos = s2;
s2 = peg$c0;
@ -2862,7 +2867,7 @@ module.exports = (function() {
}
peg$silentFails--;
if (s2 === peg$FAILED) {
s1 = peg$c36;
s1 = peg$c41;
} else {
peg$currPos = s1;
s1 = peg$c0;
@ -3388,7 +3393,7 @@ module.exports = (function() {
s3 = peg$parseIdentifierPart();
peg$silentFails--;
if (s3 === peg$FAILED) {
s2 = peg$c36;
s2 = peg$c41;
} else {
peg$currPos = s2;
s2 = peg$c0;
@ -3425,7 +3430,7 @@ module.exports = (function() {
s3 = peg$parseIdentifierPart();
peg$silentFails--;
if (s3 === peg$FAILED) {
s2 = peg$c36;
s2 = peg$c41;
} else {
peg$currPos = s2;
s2 = peg$c0;
@ -3462,7 +3467,7 @@ module.exports = (function() {
s3 = peg$parseIdentifierPart();
peg$silentFails--;
if (s3 === peg$FAILED) {
s2 = peg$c36;
s2 = peg$c41;
} else {
peg$currPos = s2;
s2 = peg$c0;
@ -3499,7 +3504,7 @@ module.exports = (function() {
s3 = peg$parseIdentifierPart();
peg$silentFails--;
if (s3 === peg$FAILED) {
s2 = peg$c36;
s2 = peg$c41;
} else {
peg$currPos = s2;
s2 = peg$c0;
@ -3536,7 +3541,7 @@ module.exports = (function() {
s3 = peg$parseIdentifierPart();
peg$silentFails--;
if (s3 === peg$FAILED) {
s2 = peg$c36;
s2 = peg$c41;
} else {
peg$currPos = s2;
s2 = peg$c0;
@ -3573,7 +3578,7 @@ module.exports = (function() {
s3 = peg$parseIdentifierPart();
peg$silentFails--;
if (s3 === peg$FAILED) {
s2 = peg$c36;
s2 = peg$c41;
} else {
peg$currPos = s2;
s2 = peg$c0;
@ -3610,7 +3615,7 @@ module.exports = (function() {
s3 = peg$parseIdentifierPart();
peg$silentFails--;
if (s3 === peg$FAILED) {
s2 = peg$c36;
s2 = peg$c41;
} else {
peg$currPos = s2;
s2 = peg$c0;
@ -3647,7 +3652,7 @@ module.exports = (function() {
s3 = peg$parseIdentifierPart();
peg$silentFails--;
if (s3 === peg$FAILED) {
s2 = peg$c36;
s2 = peg$c41;
} else {
peg$currPos = s2;
s2 = peg$c0;
@ -3684,7 +3689,7 @@ module.exports = (function() {
s3 = peg$parseIdentifierPart();
peg$silentFails--;
if (s3 === peg$FAILED) {
s2 = peg$c36;
s2 = peg$c41;
} else {
peg$currPos = s2;
s2 = peg$c0;
@ -3721,7 +3726,7 @@ module.exports = (function() {
s3 = peg$parseIdentifierPart();
peg$silentFails--;
if (s3 === peg$FAILED) {
s2 = peg$c36;
s2 = peg$c41;
} else {
peg$currPos = s2;
s2 = peg$c0;
@ -3758,7 +3763,7 @@ module.exports = (function() {
s3 = peg$parseIdentifierPart();
peg$silentFails--;
if (s3 === peg$FAILED) {
s2 = peg$c36;
s2 = peg$c41;
} else {
peg$currPos = s2;
s2 = peg$c0;
@ -3795,7 +3800,7 @@ module.exports = (function() {
s3 = peg$parseIdentifierPart();
peg$silentFails--;
if (s3 === peg$FAILED) {
s2 = peg$c36;
s2 = peg$c41;
} else {
peg$currPos = s2;
s2 = peg$c0;
@ -3832,7 +3837,7 @@ module.exports = (function() {
s3 = peg$parseIdentifierPart();
peg$silentFails--;
if (s3 === peg$FAILED) {
s2 = peg$c36;
s2 = peg$c41;
} else {
peg$currPos = s2;
s2 = peg$c0;
@ -3869,7 +3874,7 @@ module.exports = (function() {
s3 = peg$parseIdentifierPart();
peg$silentFails--;
if (s3 === peg$FAILED) {
s2 = peg$c36;
s2 = peg$c41;
} else {
peg$currPos = s2;
s2 = peg$c0;
@ -3906,7 +3911,7 @@ module.exports = (function() {
s3 = peg$parseIdentifierPart();
peg$silentFails--;
if (s3 === peg$FAILED) {
s2 = peg$c36;
s2 = peg$c41;
} else {
peg$currPos = s2;
s2 = peg$c0;
@ -3943,7 +3948,7 @@ module.exports = (function() {
s3 = peg$parseIdentifierPart();
peg$silentFails--;
if (s3 === peg$FAILED) {
s2 = peg$c36;
s2 = peg$c41;
} else {
peg$currPos = s2;
s2 = peg$c0;
@ -3980,7 +3985,7 @@ module.exports = (function() {
s3 = peg$parseIdentifierPart();
peg$silentFails--;
if (s3 === peg$FAILED) {
s2 = peg$c36;
s2 = peg$c41;
} else {
peg$currPos = s2;
s2 = peg$c0;
@ -4017,7 +4022,7 @@ module.exports = (function() {
s3 = peg$parseIdentifierPart();
peg$silentFails--;
if (s3 === peg$FAILED) {
s2 = peg$c36;
s2 = peg$c41;
} else {
peg$currPos = s2;
s2 = peg$c0;
@ -4054,7 +4059,7 @@ module.exports = (function() {
s3 = peg$parseIdentifierPart();
peg$silentFails--;
if (s3 === peg$FAILED) {
s2 = peg$c36;
s2 = peg$c41;
} else {
peg$currPos = s2;
s2 = peg$c0;
@ -4091,7 +4096,7 @@ module.exports = (function() {
s3 = peg$parseIdentifierPart();
peg$silentFails--;
if (s3 === peg$FAILED) {
s2 = peg$c36;
s2 = peg$c41;
} else {
peg$currPos = s2;
s2 = peg$c0;
@ -4128,7 +4133,7 @@ module.exports = (function() {
s3 = peg$parseIdentifierPart();
peg$silentFails--;
if (s3 === peg$FAILED) {
s2 = peg$c36;
s2 = peg$c41;
} else {
peg$currPos = s2;
s2 = peg$c0;
@ -4165,7 +4170,7 @@ module.exports = (function() {
s3 = peg$parseIdentifierPart();
peg$silentFails--;
if (s3 === peg$FAILED) {
s2 = peg$c36;
s2 = peg$c41;
} else {
peg$currPos = s2;
s2 = peg$c0;
@ -4202,7 +4207,7 @@ module.exports = (function() {
s3 = peg$parseIdentifierPart();
peg$silentFails--;
if (s3 === peg$FAILED) {
s2 = peg$c36;
s2 = peg$c41;
} else {
peg$currPos = s2;
s2 = peg$c0;
@ -4239,7 +4244,7 @@ module.exports = (function() {
s3 = peg$parseIdentifierPart();
peg$silentFails--;
if (s3 === peg$FAILED) {
s2 = peg$c36;
s2 = peg$c41;
} else {
peg$currPos = s2;
s2 = peg$c0;
@ -4276,7 +4281,7 @@ module.exports = (function() {
s3 = peg$parseIdentifierPart();
peg$silentFails--;
if (s3 === peg$FAILED) {
s2 = peg$c36;
s2 = peg$c41;
} else {
peg$currPos = s2;
s2 = peg$c0;
@ -4313,7 +4318,7 @@ module.exports = (function() {
s3 = peg$parseIdentifierPart();
peg$silentFails--;
if (s3 === peg$FAILED) {
s2 = peg$c36;
s2 = peg$c41;
} else {
peg$currPos = s2;
s2 = peg$c0;
@ -4350,7 +4355,7 @@ module.exports = (function() {
s3 = peg$parseIdentifierPart();
peg$silentFails--;
if (s3 === peg$FAILED) {
s2 = peg$c36;
s2 = peg$c41;
} else {
peg$currPos = s2;
s2 = peg$c0;
@ -4387,7 +4392,7 @@ module.exports = (function() {
s3 = peg$parseIdentifierPart();
peg$silentFails--;
if (s3 === peg$FAILED) {
s2 = peg$c36;
s2 = peg$c41;
} else {
peg$currPos = s2;
s2 = peg$c0;
@ -4424,7 +4429,7 @@ module.exports = (function() {
s3 = peg$parseIdentifierPart();
peg$silentFails--;
if (s3 === peg$FAILED) {
s2 = peg$c36;
s2 = peg$c41;
} else {
peg$currPos = s2;
s2 = peg$c0;
@ -4461,7 +4466,7 @@ module.exports = (function() {
s3 = peg$parseIdentifierPart();
peg$silentFails--;
if (s3 === peg$FAILED) {
s2 = peg$c36;
s2 = peg$c41;
} else {
peg$currPos = s2;
s2 = peg$c0;
@ -4498,7 +4503,7 @@ module.exports = (function() {
s3 = peg$parseIdentifierPart();
peg$silentFails--;
if (s3 === peg$FAILED) {
s2 = peg$c36;
s2 = peg$c41;
} else {
peg$currPos = s2;
s2 = peg$c0;
@ -4535,7 +4540,7 @@ module.exports = (function() {
s3 = peg$parseIdentifierPart();
peg$silentFails--;
if (s3 === peg$FAILED) {
s2 = peg$c36;
s2 = peg$c41;
} else {
peg$currPos = s2;
s2 = peg$c0;
@ -4572,7 +4577,7 @@ module.exports = (function() {
s3 = peg$parseIdentifierPart();
peg$silentFails--;
if (s3 === peg$FAILED) {
s2 = peg$c36;
s2 = peg$c41;
} else {
peg$currPos = s2;
s2 = peg$c0;
@ -4609,7 +4614,7 @@ module.exports = (function() {
s3 = peg$parseIdentifierPart();
peg$silentFails--;
if (s3 === peg$FAILED) {
s2 = peg$c36;
s2 = peg$c41;
} else {
peg$currPos = s2;
s2 = peg$c0;
@ -4646,7 +4651,7 @@ module.exports = (function() {
s3 = peg$parseIdentifierPart();
peg$silentFails--;
if (s3 === peg$FAILED) {
s2 = peg$c36;
s2 = peg$c41;
} else {
peg$currPos = s2;
s2 = peg$c0;
@ -4683,7 +4688,7 @@ module.exports = (function() {
s3 = peg$parseIdentifierPart();
peg$silentFails--;
if (s3 === peg$FAILED) {
s2 = peg$c36;
s2 = peg$c41;
} else {
peg$currPos = s2;
s2 = peg$c0;
@ -4832,7 +4837,7 @@ module.exports = (function() {
}
peg$silentFails--;
if (s1 === peg$FAILED) {
s0 = peg$c36;
s0 = peg$c41;
} else {
peg$currPos = s0;
s0 = peg$c0;

7
spec/parser.spec.js

@ -318,15 +318,20 @@ describe("PEG.js grammar parser", 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]"));
expect('start = .' ).toParseAs(oneRuleGrammar({ type: "any" }));
expect('start = a' ).toParseAs(ruleRefGrammar("a"));
expect('start = ("abcd")' ).toParseAs(literalGrammar("abcd"));
expect('start = (\n"abcd")').toParseAs(literalGrammar("abcd"));
expect('start = ("abcd"\n)').toParseAs(literalGrammar("abcd"));
});
/* Canonical RuleReferenceExpression is "\"abcd\"". */
it("parses RuleReferenceExpression", function() {
expect('start = a').toParseAs(ruleRefGrammar("a"));
});
/* The SourceCharacter rule is not tested. */
/* Canonical WhiteSpace is " ". */

14
src/parser.pegjs

@ -155,17 +155,17 @@ SuffixedExpression
/ PrimaryExpression
PrimaryExpression
= name:IdentifierName !(__ (StringLiteral __)? "=") {
return {
type: "rule_ref",
name: name
};
}
/ LiteralMatcher
= LiteralMatcher
/ CharacterClassMatcher
/ AnyMatcher
/ RuleReferenceExpression
/ "(" __ expression:Expression __ ")" { return expression; }
RuleReferenceExpression
= name:IdentifierName !(__ (StringLiteral __)? "=") {
return { type: "rule_ref", name: name }
}
/* "Lexical" elements */
SourceCharacter

Loading…
Cancel
Save