From 3548861b314df7e119fd8d60b1b714b00b76a24a Mon Sep 17 00:00:00 2001 From: Zachary Carter Date: Mon, 31 Mar 2014 06:40:11 -0700 Subject: [PATCH] create a new instance of the lexer on each parse --- lib/jison.js | 45 +++++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/lib/jison.js b/lib/jison.js index 798b23dfd..de3212afb 100755 --- a/lib/jison.js +++ b/lib/jison.js @@ -1219,17 +1219,18 @@ parser.parse = function parse (input) { //this.reductionCount = this.shiftCount = 0; - this.lexer.setInput(input); - this.lexer.yy = this.yy; - this.yy.lexer = this.lexer; + var lexer = Object.create(this.lexer); + + lexer.setInput(input, this.yy); + this.yy.lexer = lexer; this.yy.parser = this; - if (typeof this.lexer.yylloc == 'undefined') { - this.lexer.yylloc = {}; + if (typeof lexer.yylloc == 'undefined') { + lexer.yylloc = {}; } - var yyloc = this.lexer.yylloc; + var yyloc = lexer.yylloc; lstack.push(yyloc); - var ranges = this.lexer.options && this.lexer.options.ranges; + var ranges = lexer.options && lexer.options.ranges; if (typeof this.yy.parseError === 'function') { this.parseError = this.yy.parseError; @@ -1245,7 +1246,7 @@ parser.parse = function parse (input) { function lex() { var token; - token = self.lexer.lex() || EOF; // $end = 1 + token = lexer.lex() || EOF; // $end = 1 // if token isn't its numeric value, convert if (typeof token !== 'number') { token = self.symbols_[token] || token; @@ -1307,17 +1308,17 @@ _handle_error: expected.push("'"+this.terminals_[p]+"'"); } } - if (this.lexer.showPosition) { - errStr = 'Parse error on line '+(yylineno+1)+":\n"+this.lexer.showPosition()+"\nExpecting "+expected.join(', ') + ", got '" + (this.terminals_[symbol] || symbol)+ "'"; + if (lexer.showPosition) { + errStr = 'Parse error on line '+(yylineno+1)+":\n"+lexer.showPosition()+"\nExpecting "+expected.join(', ') + ", got '" + (this.terminals_[symbol] || symbol)+ "'"; } else { errStr = 'Parse error on line '+(yylineno+1)+": Unexpected " + (symbol == EOF ? "end of input" : ("'"+(this.terminals_[symbol] || symbol)+"'")); } this.parseError(errStr, { - text: this.lexer.match, + text: lexer.match, token: this.terminals_[symbol] || symbol, - line: this.lexer.yylineno, + line: lexer.yylineno, loc: yyloc, expected: expected, recoverable: (error_rule_depth !== false) @@ -1333,10 +1334,10 @@ _handle_error: } // discard current lookahead and grab another - yyleng = this.lexer.yyleng; - yytext = this.lexer.yytext; - yylineno = this.lexer.yylineno; - yyloc = this.lexer.yylloc; + yyleng = lexer.yyleng; + yytext = lexer.yytext; + yylineno = lexer.yylineno; + yyloc = lexer.yylloc; symbol = lex(); } @@ -1363,15 +1364,15 @@ _handle_error: //this.shiftCount++; stack.push(symbol); - vstack.push(this.lexer.yytext); - lstack.push(this.lexer.yylloc); + vstack.push(lexer.yytext); + lstack.push(lexer.yylloc); stack.push(action[1]); // push state symbol = null; if (!preErrorSymbol) { // normal execution/no error - yyleng = this.lexer.yyleng; - yytext = this.lexer.yytext; - yylineno = this.lexer.yylineno; - yyloc = this.lexer.yylloc; + yyleng = lexer.yyleng; + yytext = lexer.yytext; + yylineno = lexer.yylineno; + yyloc = lexer.yylloc; if (recovering > 0) { recovering--; }