Skip to content

Commit

Permalink
create a new instance of the lexer on each parse
Browse files Browse the repository at this point in the history
  • Loading branch information
zaach committed Mar 31, 2014
1 parent 9e0cc65 commit 3548861
Showing 1 changed file with 23 additions and 22 deletions.
45 changes: 23 additions & 22 deletions lib/jison.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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)
Expand All @@ -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();
}

Expand All @@ -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--;
}
Expand Down

0 comments on commit 3548861

Please sign in to comment.