From ed45a1808efd90bcf52332b36ff231d44f17269d Mon Sep 17 00:00:00 2001 From: David Majda Date: Sun, 4 Mar 2012 11:32:18 +0100 Subject: [PATCH] Fix |quote| visibility in generated parsers |quote| is used outside of the |parse| function so it must be defined in more outer scope. Fixes a problem (introduced in e9d8dc8ebaeda081a6617bb261733bf1a18f2152) where construction of some error messages could throw an error. --- src/emitter.js | 48 ++++++++++++++++++++++++------------------------ src/parser.js | 46 +++++++++++++++++++++++----------------------- 2 files changed, 47 insertions(+), 47 deletions(-) diff --git a/src/emitter.js b/src/emitter.js index f800076..1012fe1 100644 --- a/src/emitter.js +++ b/src/emitter.js @@ -248,6 +248,30 @@ PEG.compiler.emitter = function(ast) { '(function(){', ' /* Generated by PEG.js @VERSION (http://pegjs.majda.cz/). */', ' ', + /* This needs to be in sync with |quote| in utils.js. */ + ' function quote(s) {', + ' /*', + ' * ECMA-262, 5th ed., 7.8.4: All characters may appear literally in a', + ' * string literal except for the closing quote character, backslash,', + ' * carriage return, line separator, paragraph separator, and line feed.', + ' * Any character may appear in the form of an escape sequence.', + ' *', + ' * For portability, we also escape escape all control and non-ASCII', + ' * characters. Note that "\\0" and "\\v" escape sequences are not used', + ' * because JSHint does not like the first and IE the second.', + ' */', + ' return \'"\' + s', + ' .replace(/\\\\/g, \'\\\\\\\\\') // backslash', + ' .replace(/"/g, \'\\\\"\') // closing quote character', + ' .replace(/\\x08/g, \'\\\\b\') // backspace', + ' .replace(/\\t/g, \'\\\\t\') // horizontal tab', + ' .replace(/\\n/g, \'\\\\n\') // line feed', + ' .replace(/\\f/g, \'\\\\f\') // form feed', + ' .replace(/\\r/g, \'\\\\r\') // carriage return', + ' .replace(/[\\x00-\\x07\\x0B\\x0E-\\x1F\\x80-\\uFFFF]/g, escape)', + ' + \'"\';', + ' }', + ' ', ' var result = {', ' /*', ' * Parses the input with a generated parser. If the parsing is successfull,', @@ -303,30 +327,6 @@ PEG.compiler.emitter = function(ast) { ' return \'\\\\\' + escapeChar + padLeft(charCode.toString(16).toUpperCase(), \'0\', length);', ' }', ' ', - /* This needs to be in sync with |quote| in utils.js. */ - ' function quote(s) {', - ' /*', - ' * ECMA-262, 5th ed., 7.8.4: All characters may appear literally in a', - ' * string literal except for the closing quote character, backslash,', - ' * carriage return, line separator, paragraph separator, and line feed.', - ' * Any character may appear in the form of an escape sequence.', - ' *', - ' * For portability, we also escape escape all control and non-ASCII', - ' * characters. Note that "\\0" and "\\v" escape sequences are not used', - ' * because JSHint does not like the first and IE the second.', - ' */', - ' return \'"\' + s', - ' .replace(/\\\\/g, \'\\\\\\\\\') // backslash', - ' .replace(/"/g, \'\\\\"\') // closing quote character', - ' .replace(/\\x08/g, \'\\\\b\') // backspace', - ' .replace(/\\t/g, \'\\\\t\') // horizontal tab', - ' .replace(/\\n/g, \'\\\\n\') // line feed', - ' .replace(/\\f/g, \'\\\\f\') // form feed', - ' .replace(/\\r/g, \'\\\\r\') // carriage return', - ' .replace(/[\\x00-\\x07\\x0B\\x0E-\\x1F\\x80-\\uFFFF]/g, escape)', - ' + \'"\';', - ' }', - ' ', ' function matchFailed(failure) {', ' if (pos < rightmostFailuresPos) {', ' return;', diff --git a/src/parser.js b/src/parser.js index 8e9bc81..fa1cb56 100644 --- a/src/parser.js +++ b/src/parser.js @@ -1,6 +1,29 @@ PEG.parser = (function(){ /* Generated by PEG.js 0.6.2 (http://pegjs.majda.cz/). */ + function quote(s) { + /* + * ECMA-262, 5th ed., 7.8.4: All characters may appear literally in a + * string literal except for the closing quote character, backslash, + * carriage return, line separator, paragraph separator, and line feed. + * Any character may appear in the form of an escape sequence. + * + * For portability, we also escape escape all control and non-ASCII + * characters. Note that "\0" and "\v" escape sequences are not used + * because JSHint does not like the first and IE the second. + */ + return '"' + s + .replace(/\\/g, '\\\\') // backslash + .replace(/"/g, '\\"') // closing quote character + .replace(/\x08/g, '\\b') // backspace + .replace(/\t/g, '\\t') // horizontal tab + .replace(/\n/g, '\\n') // line feed + .replace(/\f/g, '\\f') // form feed + .replace(/\r/g, '\\r') // carriage return + .replace(/[\x00-\x07\x0B\x0E-\x1F\x80-\uFFFF]/g, escape) + + '"'; + } + var result = { /* * Parses the input with a generated parser. If the parsing is successfull, @@ -109,29 +132,6 @@ PEG.parser = (function(){ return '\\' + escapeChar + padLeft(charCode.toString(16).toUpperCase(), '0', length); } - function quote(s) { - /* - * ECMA-262, 5th ed., 7.8.4: All characters may appear literally in a - * string literal except for the closing quote character, backslash, - * carriage return, line separator, paragraph separator, and line feed. - * Any character may appear in the form of an escape sequence. - * - * For portability, we also escape escape all control and non-ASCII - * characters. Note that "\0" and "\v" escape sequences are not used - * because JSHint does not like the first and IE the second. - */ - return '"' + s - .replace(/\\/g, '\\\\') // backslash - .replace(/"/g, '\\"') // closing quote character - .replace(/\x08/g, '\\b') // backspace - .replace(/\t/g, '\\t') // horizontal tab - .replace(/\n/g, '\\n') // line feed - .replace(/\f/g, '\\f') // form feed - .replace(/\r/g, '\\r') // carriage return - .replace(/[\x00-\x07\x0B\x0E-\x1F\x80-\uFFFF]/g, escape) - + '"'; - } - function matchFailed(failure) { if (pos < rightmostFailuresPos) { return;