diff --git a/src/parser.js b/src/parser.js index 030f99c..6b76597 100644 --- a/src/parser.js +++ b/src/parser.js @@ -24,7 +24,7 @@ PEG.parser = (function(){ "digit": parse_digit, "dot": parse_dot, "doubleQuotedCharacter": parse_doubleQuotedCharacter, - "doubleQuotedLiteral": parse_doubleQuotedLiteral, + "doubleQuotedString": parse_doubleQuotedString, "eol": parse_eol, "eolChar": parse_eolChar, "eolEscapeSequence": parse_eolEscapeSequence, @@ -57,9 +57,10 @@ PEG.parser = (function(){ "simpleSingleQuotedCharacter": parse_simpleSingleQuotedCharacter, "singleLineComment": parse_singleLineComment, "singleQuotedCharacter": parse_singleQuotedCharacter, - "singleQuotedLiteral": parse_singleQuotedLiteral, + "singleQuotedString": parse_singleQuotedString, "slash": parse_slash, "star": parse_star, + "string": parse_string, "suffixed": parse_suffixed, "unicodeEscapeSequence": parse_unicodeEscapeSequence, "upperCaseLetter": parse_upperCaseLetter, @@ -271,7 +272,7 @@ PEG.parser = (function(){ pos1 = pos; result0 = parse_identifier(); if (result0 !== null) { - result1 = parse_literal(); + result1 = parse_string(); if (result1 === null) { result1 = ""; } @@ -797,7 +798,7 @@ PEG.parser = (function(){ pos2 = pos; reportFailures++; pos3 = pos; - result1 = parse_literal(); + result1 = parse_string(); if (result1 === null) { result1 = ""; } @@ -842,19 +843,7 @@ PEG.parser = (function(){ pos = pos0; } if (result0 === null) { - pos0 = pos; result0 = parse_literal(); - if (result0 !== null) { - result0 = (function(value) { - return { - type: "literal", - value: value - }; - })(result0); - } - if (result0 === null) { - pos = pos0; - } if (result0 === null) { pos0 = pos; result0 = parse_dot(); @@ -1804,15 +1793,52 @@ PEG.parser = (function(){ return cachedResult.result; } + var result0; + var pos0, pos1; + + reportFailures++; + pos0 = pos; + result0 = parse_string(); + if (result0 !== null) { + result0 = (function(value) { + return { + type: "literal", + value: value + }; + })(result0); + } + if (result0 === null) { + pos = pos0; + } + reportFailures--; + if (reportFailures === 0 && result0 === null) { + matchFailed("literal"); + } + + cache[cacheKey] = { + nextPos: pos, + result: result0 + }; + return result0; + } + + function parse_string() { + var cacheKey = "string@" + pos; + var cachedResult = cache[cacheKey]; + if (cachedResult) { + pos = cachedResult.nextPos; + return cachedResult.result; + } + var result0, result1, result2; var pos0, pos1, pos2; reportFailures++; pos0 = pos; pos1 = pos; - result0 = parse_doubleQuotedLiteral(); + result0 = parse_doubleQuotedString(); if (result0 === null) { - result0 = parse_singleQuotedLiteral(); + result0 = parse_singleQuotedString(); } if (result0 !== null) { result1 = parse___(); @@ -1827,14 +1853,14 @@ PEG.parser = (function(){ pos = pos1; } if (result0 !== null) { - result0 = (function(literal) { return literal; })(result0[0]); + result0 = (function(string) { return string; })(result0[0]); } if (result0 === null) { pos = pos0; } reportFailures--; if (reportFailures === 0 && result0 === null) { - matchFailed("literal"); + matchFailed("string"); } cache[cacheKey] = { @@ -1844,8 +1870,8 @@ PEG.parser = (function(){ return result0; } - function parse_doubleQuotedLiteral() { - var cacheKey = "doubleQuotedLiteral@" + pos; + function parse_doubleQuotedString() { + var cacheKey = "doubleQuotedString@" + pos; var cachedResult = cache[cacheKey]; if (cachedResult) { pos = cachedResult.nextPos; @@ -2025,8 +2051,8 @@ PEG.parser = (function(){ return result0; } - function parse_singleQuotedLiteral() { - var cacheKey = "singleQuotedLiteral@" + pos; + function parse_singleQuotedString() { + var cacheKey = "singleQuotedString@" + pos; var cachedResult = cache[cacheKey]; if (cachedResult) { pos = cachedResult.nextPos; diff --git a/src/parser.pegjs b/src/parser.pegjs index bdfb6b4..81f4a1b 100644 --- a/src/parser.pegjs +++ b/src/parser.pegjs @@ -20,7 +20,7 @@ initializer } rule - = name:identifier displayName:(literal / "") equals expression:expression semicolon? { + = name:identifier displayName:(string / "") equals expression:expression semicolon? { return { type: "rule", name: name, @@ -130,18 +130,13 @@ suffixed / primary primary - = name:identifier !(( literal / "") equals) { + = name:identifier !(( string / "") equals) { return { type: "rule_ref", name: name }; } - / value:literal { - return { - type: "literal", - value: value - }; - } + / literal / dot { return { type: "any" }; } / class / lparen expression:expression rparen { return expression; } @@ -200,9 +195,17 @@ identifier "identifier" * vaguely). */ literal "literal" - = literal:(doubleQuotedLiteral / singleQuotedLiteral) __ { return literal; } + = value:string { + return { + type: "literal", + value: value + }; + } + +string "string" + = string:(doubleQuotedString / singleQuotedString) __ { return string; } -doubleQuotedLiteral +doubleQuotedString = '"' chars:doubleQuotedCharacter* '"' { return chars.join(""); } doubleQuotedCharacter @@ -216,7 +219,7 @@ doubleQuotedCharacter simpleDoubleQuotedCharacter = !('"' / "\\" / eolChar) char_:. { return char_; } -singleQuotedLiteral +singleQuotedString = "'" chars:singleQuotedCharacter* "'" { return chars.join(""); } singleQuotedCharacter diff --git a/test/parser-test.js b/test/parser-test.js index 034a483..5f3510b 100644 --- a/test/parser-test.js +++ b/test/parser-test.js @@ -334,11 +334,16 @@ test("parses identifier", function() { /* Canonical literal is "\"abcd\"". */ test("parses literal", function() { parserParses('start = "abcd"', literalGrammar("abcd")); +}); + +/* Canonical string is "\"abcd\"". */ +test("parses string", function() { + parserParses('start = "abcd"', literalGrammar("abcd")); parserParses("start = 'abcd'", literalGrammar("abcd")); }); -/* Canonical doubleQuotedLiteral is "\"abcd\"". */ -test("parses doubleQuotedLiteral", function() { +/* Canonical doubleQuotedString is "\"abcd\"". */ +test("parses doubleQuotedString", function() { parserParses('start = ""', literalGrammar("")); parserParses('start = "a"', literalGrammar("a")); parserParses('start = "abc"', literalGrammar("abc")); @@ -368,8 +373,8 @@ test("parses simpleDoubleQuotedCharacter", function() { parserDoesNotParse('start = "\u2029"'); }); -/* Canonical singleQuotedLiteral is "'abcd'". */ -test("parses singleQuotedLiteral", function() { +/* Canonical singleQuotedString is "'abcd'". */ +test("parses singleQuotedString", function() { parserParses("start = ''", literalGrammar("")); parserParses("start = 'a'", literalGrammar("a")); parserParses("start = 'abc'", literalGrammar("abc"));