From 2fd77b96fc2c26c24c9cf432eb9c93c065e0d163 Mon Sep 17 00:00:00 2001 From: David Majda Date: Fri, 6 May 2016 15:15:38 +0200 Subject: [PATCH] Revert "Use literal raw text in error messages" I no longer think that using raw literal texts in error messages is the right thing to do. The main reason is that it couples error messages with details of the grammar such as use of single or double quotes in literals. A better solution is coming in the next commit. This reverts commit 69a0f769fc1e3cd751affce198a8248cda2859c2. --- lib/compiler/passes/generate-bytecode.js | 4 +- lib/parser.js | 7 +- spec/api/plugin-api.spec.js | 2 +- .../compiler/passes/generate-bytecode.spec.js | 2 +- spec/unit/parser.spec.js | 87 +++++++++---------- src/parser.pegjs | 1 - 6 files changed, 50 insertions(+), 53 deletions(-) diff --git a/lib/compiler/passes/generate-bytecode.js b/lib/compiler/passes/generate-bytecode.js index c0fdeba..6ac09ba 100644 --- a/lib/compiler/passes/generate-bytecode.js +++ b/lib/compiler/passes/generate-bytecode.js @@ -544,7 +544,9 @@ function generateBytecode(ast) { '{', 'type: "literal",', 'value: "' + js.stringEscape(node.value) + '",', - 'description: "' + js.stringEscape(node.rawText) + '"', + 'description: "' + + js.stringEscape('"' + js.stringEscape(node.value) + '"') + + '"', '}' ].join(' ')); diff --git a/lib/parser.js b/lib/parser.js index 7486f5b..15dec04 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -168,13 +168,13 @@ peg$c36 = "\t", peg$c37 = { type: "literal", value: "\t", description: "\"\\t\"" }, peg$c38 = "\x0B", - peg$c39 = { type: "literal", value: "\x0B", description: "\"\\v\"" }, + peg$c39 = { type: "literal", value: "\x0B", description: "\"\\x0B\"" }, peg$c40 = "\f", peg$c41 = { type: "literal", value: "\f", description: "\"\\f\"" }, peg$c42 = " ", peg$c43 = { type: "literal", value: " ", description: "\" \"" }, peg$c44 = "\xA0", - peg$c45 = { type: "literal", value: "\xA0", description: "\"\\u00A0\"" }, + peg$c45 = { type: "literal", value: "\xA0", description: "\"\\xA0\"" }, peg$c46 = "\uFEFF", peg$c47 = { type: "literal", value: "\uFEFF", description: "\"\\uFEFF\"" }, peg$c48 = /^[\n\r\u2028\u2029]/, @@ -217,13 +217,12 @@ type: "literal", value: value, ignoreCase: ignoreCase !== null, - rawText: text(), location: location() }; }, peg$c84 = { type: "other", description: "string" }, peg$c85 = "\"", - peg$c86 = { type: "literal", value: "\"", description: "'\"'" }, + peg$c86 = { type: "literal", value: "\"", description: "\"\\\"\"" }, peg$c87 = function(chars) { return chars.join(""); }, peg$c88 = "'", peg$c89 = { type: "literal", value: "'", description: "\"'\"" }, diff --git a/spec/api/plugin-api.spec.js b/spec/api/plugin-api.spec.js index 77d1fc4..92b2e41 100644 --- a/spec/api/plugin-api.spec.js +++ b/spec/api/plugin-api.spec.js @@ -107,7 +107,7 @@ describe("plugin API", function() { ' {', ' type: "rule",', ' name: "start",', - ' expression: { type: "literal", value: text(), ignoreCase: false, rawText: text() }', + ' expression: { type: "literal", value: text(), ignoreCase: false }', ' }', ' ]', ' };', diff --git a/spec/unit/compiler/passes/generate-bytecode.spec.js b/spec/unit/compiler/passes/generate-bytecode.spec.js index 9432fb4..1995b9c 100644 --- a/spec/unit/compiler/passes/generate-bytecode.spec.js +++ b/spec/unit/compiler/passes/generate-bytecode.spec.js @@ -579,7 +579,7 @@ describe("compiler pass |generateBytecode|", function() { it("defines correct constants", function() { expect(pass).toChangeAST(grammar, constsDetails([ '"a"', - '{ type: "literal", value: "A", description: "\\"A\\"i" }' + '{ type: "literal", value: "A", description: "\\"A\\"" }' ])); }); }); diff --git a/spec/unit/parser.spec.js b/spec/unit/parser.spec.js index 8c08c75..88b20da 100644 --- a/spec/unit/parser.spec.js +++ b/spec/unit/parser.spec.js @@ -3,10 +3,10 @@ "use strict"; describe("PEG.js grammar parser", function() { - var literalAbcd = { type: "literal", value: "abcd", ignoreCase: false, rawText: '"abcd"' }, - literalEfgh = { type: "literal", value: "efgh", ignoreCase: false, rawText: '"efgh"' }, - literalIjkl = { type: "literal", value: "ijkl", ignoreCase: false, rawText: '"ijkl"' }, - literalMnop = { type: "literal", value: "mnop", ignoreCase: false, rawText: '"mnop"' }, + var literalAbcd = { type: "literal", value: "abcd", ignoreCase: false }, + literalEfgh = { type: "literal", value: "efgh", ignoreCase: false }, + literalIjkl = { type: "literal", value: "ijkl", ignoreCase: false }, + literalMnop = { type: "literal", value: "mnop", ignoreCase: false }, semanticAnd = { type: "semantic_and", code: " code " }, semanticNot = { type: "semantic_not", code: " code " }, optional = { type: "optional", expression: literalAbcd }, @@ -73,13 +73,10 @@ describe("PEG.js grammar parser", function() { ); } - function literalGrammar(value, ignoreCase, rawText) { - return oneRuleGrammar({ - type: "literal", - value: value, - ignoreCase: ignoreCase, - rawText: rawText - }); + function literalGrammar(value, ignoreCase) { + return oneRuleGrammar( + { type: "literal", value: value, ignoreCase: ignoreCase } + ); } function classGrammar(parts, inverted, ignoreCase, rawText) { @@ -100,7 +97,7 @@ describe("PEG.js grammar parser", function() { return oneRuleGrammar({ type: "rule_ref", name: name }); } - var trivialGrammar = literalGrammar("abcd", false, '"abcd"'), + var trivialGrammar = literalGrammar("abcd", false), twoRuleGrammar = { type: "grammar", initializer: null, @@ -496,26 +493,26 @@ describe("PEG.js grammar parser", function() { /* Canonical LiteralMatcher is "\"abcd\"". */ it("parses LiteralMatcher", function() { - expect('start = "abcd"' ).toParseAs(literalGrammar("abcd", false, '"abcd"')); - expect('start = "abcd"i').toParseAs(literalGrammar("abcd", true, '"abcd"i')); + expect('start = "abcd"' ).toParseAs(literalGrammar("abcd", false)); + expect('start = "abcd"i').toParseAs(literalGrammar("abcd", true)); }); /* Canonical StringLiteral is "\"abcd\"". */ it("parses StringLiteral", function() { - expect('start = ""' ).toParseAs(literalGrammar("", false, '""')); - expect('start = "a"' ).toParseAs(literalGrammar("a", false, '"a"')); - expect('start = "abc"').toParseAs(literalGrammar("abc", false, '"abc"')); + expect('start = ""' ).toParseAs(literalGrammar("", false)); + expect('start = "a"' ).toParseAs(literalGrammar("a", false)); + expect('start = "abc"').toParseAs(literalGrammar("abc", false)); - expect("start = ''" ).toParseAs(literalGrammar("", false, "''")); - expect("start = 'a'" ).toParseAs(literalGrammar("a", false, "'a'")); - expect("start = 'abc'").toParseAs(literalGrammar("abc", false, "'abc'")); + expect("start = ''" ).toParseAs(literalGrammar("", false)); + expect("start = 'a'" ).toParseAs(literalGrammar("a", false)); + expect("start = 'abc'").toParseAs(literalGrammar("abc", false)); }); /* Canonical DoubleStringCharacter is "a". */ it("parses DoubleStringCharacter", function() { - expect('start = "a"' ).toParseAs(literalGrammar("a", false, '"a"')); - expect('start = "\\n"' ).toParseAs(literalGrammar("\n", false, '"\\n"')); - expect('start = "\\\n"').toParseAs(literalGrammar("", false, '"\\\n"')); + expect('start = "a"' ).toParseAs(literalGrammar("a", false)); + expect('start = "\\n"' ).toParseAs(literalGrammar("\n", false)); + expect('start = "\\\n"').toParseAs(literalGrammar("", false)); expect('start = """' ).toFailToParse(); expect('start = "\\"').toFailToParse(); @@ -524,9 +521,9 @@ describe("PEG.js grammar parser", function() { /* Canonical SingleStringCharacter is "a". */ it("parses SingleStringCharacter", function() { - expect("start = 'a'" ).toParseAs(literalGrammar("a", false, "'a'")); - expect("start = '\\n'" ).toParseAs(literalGrammar("\n", false, "'\\n'")); - expect("start = '\\\n'").toParseAs(literalGrammar("", false, "'\\\n'")); + expect("start = 'a'" ).toParseAs(literalGrammar("a", false)); + expect("start = '\\n'" ).toParseAs(literalGrammar("\n", false)); + expect("start = '\\\n'").toParseAs(literalGrammar("", false)); expect("start = '''" ).toFailToParse(); expect("start = '\\'").toFailToParse(); @@ -597,41 +594,41 @@ describe("PEG.js grammar parser", function() { /* Canonical LineContinuation is "\\\n". */ it("parses LineContinuation", function() { - expect('start = "\\\r\n"').toParseAs(literalGrammar("", false, '"\\\r\n"')); + expect('start = "\\\r\n"').toParseAs(literalGrammar("", false)); }); /* Canonical EscapeSequence is "n". */ it("parses EscapeSequence", function() { - expect('start = "\\n"' ).toParseAs(literalGrammar("\n", false, '"\\n"')); - expect('start = "\\0"' ).toParseAs(literalGrammar("\x00", false, '"\\0"')); - expect('start = "\\xFF"' ).toParseAs(literalGrammar("\xFF", false, '"\\xFF"')); - expect('start = "\\uFFFF"').toParseAs(literalGrammar("\uFFFF", false, '"\\uFFFF"')); + expect('start = "\\n"' ).toParseAs(literalGrammar("\n", false)); + expect('start = "\\0"' ).toParseAs(literalGrammar("\x00", false)); + expect('start = "\\xFF"' ).toParseAs(literalGrammar("\xFF", false)); + expect('start = "\\uFFFF"').toParseAs(literalGrammar("\uFFFF", false)); expect('start = "\\09"').toFailToParse(); }); /* Canonical CharacterEscapeSequence is "n". */ it("parses CharacterEscapeSequence", function() { - expect('start = "\\n"').toParseAs(literalGrammar("\n", false, '"\\n"')); - expect('start = "\\a"').toParseAs(literalGrammar("a", false, '"\\a"')); + expect('start = "\\n"').toParseAs(literalGrammar("\n", false)); + expect('start = "\\a"').toParseAs(literalGrammar("a", false)); }); /* Canonical SingleEscapeCharacter is "n". */ it("parses SingleEscapeCharacter", function() { - expect('start = "\\\'"').toParseAs(literalGrammar("'", false, '"\\\'"')); - expect('start = "\\""' ).toParseAs(literalGrammar('"', false, '"\\""')); - expect('start = "\\\\"').toParseAs(literalGrammar("\\", false, '"\\\\"')); - expect('start = "\\b"' ).toParseAs(literalGrammar("\b", false, '"\\b"')); - expect('start = "\\f"' ).toParseAs(literalGrammar("\f", false, '"\\f"')); - expect('start = "\\n"' ).toParseAs(literalGrammar("\n", false, '"\\n"')); - expect('start = "\\r"' ).toParseAs(literalGrammar("\r", false, '"\\r"')); - expect('start = "\\t"' ).toParseAs(literalGrammar("\t", false, '"\\t"')); - expect('start = "\\v"' ).toParseAs(literalGrammar("\x0B", false, '"\\v"')); // no "\v" in IE + expect('start = "\\\'"').toParseAs(literalGrammar("'", false)); + expect('start = "\\""' ).toParseAs(literalGrammar('"', false)); + expect('start = "\\\\"').toParseAs(literalGrammar("\\", false)); + expect('start = "\\b"' ).toParseAs(literalGrammar("\b", false)); + expect('start = "\\f"' ).toParseAs(literalGrammar("\f", false)); + expect('start = "\\n"' ).toParseAs(literalGrammar("\n", false)); + expect('start = "\\r"' ).toParseAs(literalGrammar("\r", false)); + expect('start = "\\t"' ).toParseAs(literalGrammar("\t", false)); + expect('start = "\\v"' ).toParseAs(literalGrammar("\x0B", false)); // no "\v" in IE }); /* Canonical NonEscapeCharacter is "a". */ it("parses NonEscapeCharacter", function() { - expect('start = "\\a"').toParseAs(literalGrammar("a", false, '"\\a"')); + expect('start = "\\a"').toParseAs(literalGrammar("a", false)); /* * The negative predicate is impossible to test with PEG.js grammar @@ -646,12 +643,12 @@ describe("PEG.js grammar parser", function() { /* Canonical HexEscapeSequence is "xFF". */ it("parses HexEscapeSequence", function() { - expect('start = "\\xFF"').toParseAs(literalGrammar("\xFF", false, '"\\xFF"')); + expect('start = "\\xFF"').toParseAs(literalGrammar("\xFF", false)); }); /* Canonical UnicodeEscapeSequence is "uFFFF". */ it("parses UnicodeEscapeSequence", function() { - expect('start = "\\uFFFF"').toParseAs(literalGrammar("\uFFFF", false, '"\\uFFFF"')); + expect('start = "\\uFFFF"').toParseAs(literalGrammar("\uFFFF", false)); }); /* Digit rules are not tested. */ diff --git a/src/parser.pegjs b/src/parser.pegjs index 87962ef..7393444 100644 --- a/src/parser.pegjs +++ b/src/parser.pegjs @@ -355,7 +355,6 @@ LiteralMatcher "literal" type: "literal", value: value, ignoreCase: ignoreCase !== null, - rawText: text(), location: location() }; }