Split |literal| rule in the PEG.js grammar to |literal| and |string|

This is just a formality now but it will make sense later when literals
(but not strings) will allow "i" flag for case-insensitive matching.
redux
David Majda 13 years ago
parent 950cc80738
commit 1c11e4aaa3

@ -24,7 +24,7 @@ PEG.parser = (function(){
"digit": parse_digit, "digit": parse_digit,
"dot": parse_dot, "dot": parse_dot,
"doubleQuotedCharacter": parse_doubleQuotedCharacter, "doubleQuotedCharacter": parse_doubleQuotedCharacter,
"doubleQuotedLiteral": parse_doubleQuotedLiteral, "doubleQuotedString": parse_doubleQuotedString,
"eol": parse_eol, "eol": parse_eol,
"eolChar": parse_eolChar, "eolChar": parse_eolChar,
"eolEscapeSequence": parse_eolEscapeSequence, "eolEscapeSequence": parse_eolEscapeSequence,
@ -57,9 +57,10 @@ PEG.parser = (function(){
"simpleSingleQuotedCharacter": parse_simpleSingleQuotedCharacter, "simpleSingleQuotedCharacter": parse_simpleSingleQuotedCharacter,
"singleLineComment": parse_singleLineComment, "singleLineComment": parse_singleLineComment,
"singleQuotedCharacter": parse_singleQuotedCharacter, "singleQuotedCharacter": parse_singleQuotedCharacter,
"singleQuotedLiteral": parse_singleQuotedLiteral, "singleQuotedString": parse_singleQuotedString,
"slash": parse_slash, "slash": parse_slash,
"star": parse_star, "star": parse_star,
"string": parse_string,
"suffixed": parse_suffixed, "suffixed": parse_suffixed,
"unicodeEscapeSequence": parse_unicodeEscapeSequence, "unicodeEscapeSequence": parse_unicodeEscapeSequence,
"upperCaseLetter": parse_upperCaseLetter, "upperCaseLetter": parse_upperCaseLetter,
@ -271,7 +272,7 @@ PEG.parser = (function(){
pos1 = pos; pos1 = pos;
result0 = parse_identifier(); result0 = parse_identifier();
if (result0 !== null) { if (result0 !== null) {
result1 = parse_literal(); result1 = parse_string();
if (result1 === null) { if (result1 === null) {
result1 = ""; result1 = "";
} }
@ -797,7 +798,7 @@ PEG.parser = (function(){
pos2 = pos; pos2 = pos;
reportFailures++; reportFailures++;
pos3 = pos; pos3 = pos;
result1 = parse_literal(); result1 = parse_string();
if (result1 === null) { if (result1 === null) {
result1 = ""; result1 = "";
} }
@ -842,19 +843,7 @@ PEG.parser = (function(){
pos = pos0; pos = pos0;
} }
if (result0 === null) { if (result0 === null) {
pos0 = pos;
result0 = parse_literal(); result0 = parse_literal();
if (result0 !== null) {
result0 = (function(value) {
return {
type: "literal",
value: value
};
})(result0);
}
if (result0 === null) {
pos = pos0;
}
if (result0 === null) { if (result0 === null) {
pos0 = pos; pos0 = pos;
result0 = parse_dot(); result0 = parse_dot();
@ -1804,15 +1793,52 @@ PEG.parser = (function(){
return cachedResult.result; 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 result0, result1, result2;
var pos0, pos1, pos2; var pos0, pos1, pos2;
reportFailures++; reportFailures++;
pos0 = pos; pos0 = pos;
pos1 = pos; pos1 = pos;
result0 = parse_doubleQuotedLiteral(); result0 = parse_doubleQuotedString();
if (result0 === null) { if (result0 === null) {
result0 = parse_singleQuotedLiteral(); result0 = parse_singleQuotedString();
} }
if (result0 !== null) { if (result0 !== null) {
result1 = parse___(); result1 = parse___();
@ -1827,14 +1853,14 @@ PEG.parser = (function(){
pos = pos1; pos = pos1;
} }
if (result0 !== null) { if (result0 !== null) {
result0 = (function(literal) { return literal; })(result0[0]); result0 = (function(string) { return string; })(result0[0]);
} }
if (result0 === null) { if (result0 === null) {
pos = pos0; pos = pos0;
} }
reportFailures--; reportFailures--;
if (reportFailures === 0 && result0 === null) { if (reportFailures === 0 && result0 === null) {
matchFailed("literal"); matchFailed("string");
} }
cache[cacheKey] = { cache[cacheKey] = {
@ -1844,8 +1870,8 @@ PEG.parser = (function(){
return result0; return result0;
} }
function parse_doubleQuotedLiteral() { function parse_doubleQuotedString() {
var cacheKey = "doubleQuotedLiteral@" + pos; var cacheKey = "doubleQuotedString@" + pos;
var cachedResult = cache[cacheKey]; var cachedResult = cache[cacheKey];
if (cachedResult) { if (cachedResult) {
pos = cachedResult.nextPos; pos = cachedResult.nextPos;
@ -2025,8 +2051,8 @@ PEG.parser = (function(){
return result0; return result0;
} }
function parse_singleQuotedLiteral() { function parse_singleQuotedString() {
var cacheKey = "singleQuotedLiteral@" + pos; var cacheKey = "singleQuotedString@" + pos;
var cachedResult = cache[cacheKey]; var cachedResult = cache[cacheKey];
if (cachedResult) { if (cachedResult) {
pos = cachedResult.nextPos; pos = cachedResult.nextPos;

@ -20,7 +20,7 @@ initializer
} }
rule rule
= name:identifier displayName:(literal / "") equals expression:expression semicolon? { = name:identifier displayName:(string / "") equals expression:expression semicolon? {
return { return {
type: "rule", type: "rule",
name: name, name: name,
@ -130,18 +130,13 @@ suffixed
/ primary / primary
primary primary
= name:identifier !(( literal / "") equals) { = name:identifier !(( string / "") equals) {
return { return {
type: "rule_ref", type: "rule_ref",
name: name name: name
}; };
} }
/ value:literal { / literal
return {
type: "literal",
value: value
};
}
/ dot { return { type: "any" }; } / dot { return { type: "any" }; }
/ class / class
/ lparen expression:expression rparen { return expression; } / lparen expression:expression rparen { return expression; }
@ -200,9 +195,17 @@ identifier "identifier"
* vaguely). * vaguely).
*/ */
literal "literal" 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(""); } = '"' chars:doubleQuotedCharacter* '"' { return chars.join(""); }
doubleQuotedCharacter doubleQuotedCharacter
@ -216,7 +219,7 @@ doubleQuotedCharacter
simpleDoubleQuotedCharacter simpleDoubleQuotedCharacter
= !('"' / "\\" / eolChar) char_:. { return char_; } = !('"' / "\\" / eolChar) char_:. { return char_; }
singleQuotedLiteral singleQuotedString
= "'" chars:singleQuotedCharacter* "'" { return chars.join(""); } = "'" chars:singleQuotedCharacter* "'" { return chars.join(""); }
singleQuotedCharacter singleQuotedCharacter

@ -334,11 +334,16 @@ test("parses identifier", function() {
/* Canonical literal is "\"abcd\"". */ /* Canonical literal is "\"abcd\"". */
test("parses literal", function() { test("parses literal", function() {
parserParses('start = "abcd"', literalGrammar("abcd")); parserParses('start = "abcd"', literalGrammar("abcd"));
});
/* Canonical string is "\"abcd\"". */
test("parses string", function() {
parserParses('start = "abcd"', literalGrammar("abcd"));
parserParses("start = 'abcd'", literalGrammar("abcd")); parserParses("start = 'abcd'", literalGrammar("abcd"));
}); });
/* Canonical doubleQuotedLiteral is "\"abcd\"". */ /* Canonical doubleQuotedString is "\"abcd\"". */
test("parses doubleQuotedLiteral", function() { test("parses doubleQuotedString", function() {
parserParses('start = ""', literalGrammar("")); parserParses('start = ""', literalGrammar(""));
parserParses('start = "a"', literalGrammar("a")); parserParses('start = "a"', literalGrammar("a"));
parserParses('start = "abc"', literalGrammar("abc")); parserParses('start = "abc"', literalGrammar("abc"));
@ -368,8 +373,8 @@ test("parses simpleDoubleQuotedCharacter", function() {
parserDoesNotParse('start = "\u2029"'); parserDoesNotParse('start = "\u2029"');
}); });
/* Canonical singleQuotedLiteral is "'abcd'". */ /* Canonical singleQuotedString is "'abcd'". */
test("parses singleQuotedLiteral", function() { test("parses singleQuotedString", function() {
parserParses("start = ''", literalGrammar("")); parserParses("start = ''", literalGrammar(""));
parserParses("start = 'a'", literalGrammar("a")); parserParses("start = 'a'", literalGrammar("a"));
parserParses("start = 'abc'", literalGrammar("abc")); parserParses("start = 'abc'", literalGrammar("abc"));

Loading…
Cancel
Save