Browse Source

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 8 years ago
parent
commit
c0df01b092
  1. 637
      lib/parser.js
  2. 44
      spec/parser.spec.js
  3. 26
      src/parser.pegjs

637
lib/parser.js
File diff suppressed because it is too large
View File

44
spec/parser.spec.js

@ -389,35 +389,6 @@ describe("PEG.js grammar parser", function() {
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". */
it("parses Identifier", function() {
expect('start = a:"abcd"').toParseAs(oneRuleGrammar(labeledAbcd));
@ -590,6 +561,21 @@ describe("PEG.js grammar parser", function() {
/* 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. */
/* Token rules are not tested. */

26
src/parser.pegjs

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

Loading…
Cancel
Save