PEG.js grammar: Improve code block handling

* Rename the |Action| rule to |CodeBlock| (it better describes what
    the rule matches).

  * Implement the rule in a simpler way and move it after more basic
    lexical elements.
redux
David Majda 10 years ago
parent 13f72bb19d
commit c0df01b092

File diff suppressed because one or more lines are too long

@ -389,35 +389,6 @@ describe("PEG.js grammar parser", function() {
expect('start =//aaa\n"abcd"').toParseAs(trivialGrammar); expect('start =//aaa\n"abcd"').toParseAs(trivialGrammar);
}); });
/* Canonical Action is "{ code }". */
it("parses Action", function() {
expect('start = "abcd" { code }').toParseAs(actionGrammar(" code "));
expect('start = "abcd" { code }\n').toParseAs(actionGrammar(" code "));
});
/* Canonical Braced is "{ code }". */
it("parses Braced", function() {
expect('start = "abcd" {}' ).toParseAs(actionGrammar(""));
expect('start = "abcd" {{a}}' ).toParseAs(actionGrammar("{a}"));
expect('start = "abcd" {abcd}' ).toParseAs(actionGrammar("abcd"));
expect('start = "abcd" {{a}{b}{c}}').toParseAs(actionGrammar("{a}{b}{c}"));
});
/* Canonical NonBraceCharacters is "abcd". */
it("parses NonBraceCharacters", function() {
expect('start = "abcd" {a}' ).toParseAs(actionGrammar("a"));
expect('start = "abcd" {abc}').toParseAs(actionGrammar("abc"));
});
/* Canonical NonBraceCharacter is "a". */
it("parses NonBraceCharacter", function() {
expect('start = "abcd" {a}').toParseAs(actionGrammar("a"));
expect('start = "abcd" {{}').toFailToParse();
expect('start = "abcd" {}}').toFailToParse();
});
/* Canonical Identifier is "a". */ /* Canonical Identifier is "a". */
it("parses Identifier", function() { it("parses Identifier", function() {
expect('start = a:"abcd"').toParseAs(oneRuleGrammar(labeledAbcd)); expect('start = a:"abcd"').toParseAs(oneRuleGrammar(labeledAbcd));
@ -590,6 +561,21 @@ describe("PEG.js grammar parser", function() {
/* Digit rules are not tested. */ /* Digit rules are not tested. */
/* Canonical CodeBlock is "{ code }". */
it("parses CodeBlock", function() {
expect('start = "abcd" { code }').toParseAs(actionGrammar(" code "));
});
/* Canonical Code is "code". */
it("parses Code", function() {
expect('start = "abcd" {a}' ).toParseAs(actionGrammar("a"));
expect('start = "abcd" {{a}}').toParseAs(actionGrammar("{a}"));
expect('start = "abcd" {aaa}').toParseAs(actionGrammar("aaa"));
expect('start = "abcd" {{}').toFailToParse();
expect('start = "abcd" {}}').toFailToParse();
});
/* Unicode character category rules are not tested. */ /* Unicode character category rules are not tested. */
/* Token rules are not tested. */ /* Token rules are not tested. */

@ -41,7 +41,7 @@ Grammar
} }
Initializer Initializer
= code:Action (__ ";")? { = code:CodeBlock (__ ";")? {
return { return {
type: "initializer", type: "initializer",
code: code code: code
@ -77,7 +77,7 @@ Choice
} }
Sequence Sequence
= first:Labeled rest:(__ Labeled)* __ code:Action { = first:Labeled rest:(__ Labeled)* __ 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;
@ -110,7 +110,7 @@ Prefixed
expression: expression expression: expression
}; };
} }
/ "&" __ code:Action { / "&" __ code:CodeBlock {
return { return {
type: "semantic_and", type: "semantic_and",
code: code code: code
@ -122,7 +122,7 @@ Prefixed
expression: expression expression: expression
}; };
} }
/ "!" __ code:Action { / "!" __ code:CodeBlock {
return { return {
type: "semantic_not", type: "semantic_not",
code: code code: code
@ -203,18 +203,6 @@ MultiLineComment
SingleLineComment SingleLineComment
= "//" (!LineTerminator SourceCharacter)* = "//" (!LineTerminator SourceCharacter)*
Action "action"
= braced:Braced __ { return braced.substr(1, braced.length - 2); }
Braced
= $("{" (Braced / NonBraceCharacters)* "}")
NonBraceCharacters
= NonBraceCharacter+
NonBraceCharacter
= [^{}]
Identifier Identifier
= !ReservedWord name:IdentifierName { return name; } = !ReservedWord name:IdentifierName { return name; }
/ name:IdentifierName { / name:IdentifierName {
@ -406,6 +394,12 @@ DecimalDigit
HexDigit HexDigit
= [0-9a-f]i = [0-9a-f]i
CodeBlock "code block"
= "{" code:Code "}" { return code; }
Code
= $([^{}] / "{" Code "}")*
/* /*
* Unicode Character Categories * Unicode Character Categories
* *

Loading…
Cancel
Save