From 5c6f4dd38be234bf4fa2998ea42040530cecb7bf Mon Sep 17 00:00:00 2001 From: David Majda Date: Wed, 2 Apr 2014 18:28:52 +0200 Subject: [PATCH] PEG.js grammar: Append |Expression| to expression rule names Makes the rule names a bit longer but also clearer. --- lib/parser.js | 54 ++++++++++++++++++++++----------------------- spec/parser.spec.js | 24 ++++++++++---------- src/parser.pegjs | 40 ++++++++++++++++----------------- 3 files changed, 59 insertions(+), 59 deletions(-) diff --git a/lib/parser.js b/lib/parser.js index 48c9f7f..a641ece 100644 --- a/lib/parser.js +++ b/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) { diff --git a/spec/parser.spec.js b/spec/parser.spec.js index e21cfad..b0b1bde 100644 --- a/spec/parser.spec.js +++ b/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]")); diff --git a/src/parser.pegjs b/src/parser.pegjs index 166d9e5..cffb07a 100644 --- a/src/parser.pegjs +++ b/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",