From 277fb23411477936adcb369d11564d9ef566d74d Mon Sep 17 00:00:00 2001 From: David Majda Date: Sun, 28 Oct 2012 19:17:37 +0100 Subject: [PATCH] Setup prototype chain for |SyntaxError| in generated parsers correctly --- src/compiler/passes/generate-code.js | 9 ++++++++- src/utils.js | 4 ++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/compiler/passes/generate-code.js b/src/compiler/passes/generate-code.js index 7bf6b7b..92d3a3c 100644 --- a/src/compiler/passes/generate-code.js +++ b/src/compiler/passes/generate-code.js @@ -273,6 +273,13 @@ PEG.compiler.passes.generateCode = function(ast, options) { ' * http://pegjs.majda.cz/', ' */', ' ', + /* This needs to be in sync with |subclass| in utils.js. */ + ' function subclass(child, parent) {', + ' function ctor() { this.constructor = child; }', + ' ctor.prototype = parent.prototype;', + ' child.prototype = new ctor();', + ' }', + ' ', /* This needs to be in sync with |quote| in utils.js. */ ' function quote(s) {', ' /*', @@ -546,7 +553,7 @@ PEG.compiler.passes.generateCode = function(ast, options) { ' this.column = column;', ' };', ' ', - ' result.SyntaxError.prototype = Error.prototype;', + ' subclass(result.SyntaxError, Error);', ' ', ' return result;', '})()' diff --git a/src/utils.js b/src/utils.js index 967168f..e97a3da 100644 --- a/src/utils.js +++ b/src/utils.js @@ -87,6 +87,10 @@ function defaults(object, defaults) { } } +/* + * The code needs to be in sync with the code template in the compilation + * function for "action" nodes. + */ function subclass(child, parent) { function ctor() { this.constructor = child; } ctor.prototype = parent.prototype;