diff --git a/lib/parser.js b/lib/parser.js index 1ed8054..f793263 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -182,7 +182,7 @@ module.exports = (function() { peg$c103 = function(inverted, parts, ignoreCase) { return { type: "class", - parts: parts, + parts: filterEmptyStrings(parts), inverted: inverted !== null, ignoreCase: ignoreCase !== null, rawText: text() @@ -4743,6 +4743,18 @@ module.exports = (function() { "!": "semantic_not" }; + function filterEmptyStrings(array) { + var result = [], i; + + for (i = 0; i < array.length; i++) { + if (array[i] !== "") { + result.push(array[i]); + } + } + + return result; + } + function extractOptional(optional, index) { return optional ? optional[index] : null; } diff --git a/spec/parser.spec.js b/spec/parser.spec.js index e5a1fc0..fcdc745 100644 --- a/spec/parser.spec.js +++ b/spec/parser.spec.js @@ -485,6 +485,10 @@ describe("PEG.js grammar parser", function() { expect('start = [a-d]i').toParseAs( classGrammar([["a", "d"]], false, true, "[a-d]i") ); + + expect('start = [\\\n]').toParseAs( + classGrammar([], false, false, "[\\\n]") + ); }); /* Canonical ClassCharacterRange is "a-d". */ @@ -510,7 +514,7 @@ describe("PEG.js grammar parser", function() { classGrammar(["\n"], false, false, "[\\n]") ); expect('start = [\\\n]').toParseAs( - classGrammar([''], false, false, "[\\\n]") + classGrammar([], false, false, "[\\\n]") ); expect('start = []]' ).toFailToParse(); diff --git a/src/parser.pegjs b/src/parser.pegjs index 493951d..e9d82bb 100644 --- a/src/parser.pegjs +++ b/src/parser.pegjs @@ -41,6 +41,18 @@ "!": "semantic_not" }; + function filterEmptyStrings(array) { + var result = [], i; + + for (i = 0; i < array.length; i++) { + if (array[i] !== "") { + result.push(array[i]); + } + } + + return result; + } + function extractOptional(optional, index) { return optional ? optional[index] : null; } @@ -323,7 +335,7 @@ CharacterClassMatcher "character class" { return { type: "class", - parts: parts, + parts: filterEmptyStrings(parts), inverted: inverted !== null, ignoreCase: ignoreCase !== null, rawText: text()