Permalink
Browse files

adjusted the code to remove the error recovery from the run-time when…

… no error recovery rules are included in the user-provided grammar. (The correction was necessary as the run-time has been altered in the previous commits.)
  • Loading branch information...
GerHobbelt committed Jan 6, 2013
1 parent 38d1245 commit 24e36a5e99f8063df1be4401071cb558ae61c97f
Showing with 51 additions and 55 deletions.
  1. +1 −1 gh-pages
  2. +26 −22 lib/jison.js
  3. +12 −16 lib/jison/util/bnf-parser.js
  4. +12 −16 lib/jison/util/lex-parser.js
View
@@ -1037,7 +1037,10 @@ function removeErrorRecovery (fn) {
var ast = Reflect.parse(parseFn);
var labeled = JSONSelect.match(':has(:root > .label > .name:val("_handle_error"))', ast);
- labeled[0].body.consequent.body = [labeled[0].body.consequent.body[0], labeled[0].body.consequent.body[1]];
+ var reduced_code = labeled[0].body.consequent.body[3].consequent.body;
+ reduced_code[0] = labeled[0].body.consequent.body[1]; // remove the line: error_rule_depth = locateNearestErrorRecoveryRule(state);
+ reduced_code[4].expression.arguments[1].properties.pop(); // remove the line: 'recoverable: error_rule_depth !== false'
+ labeled[0].body.consequent.body = reduced_code;
return Reflect.stringify(ast).replace(/_handle_error:\s?/,"").replace(/\\\\n/g,"\\n");
} catch (e) {
@@ -1222,27 +1225,6 @@ parser.parse = function parse (input) {
return token;
}
- // Return the rule stack depth where the nearest error rule can be found.
- // Return FALSE when no error recovery rule was found.
- function locateNearestErrorRecoveryRule(state) {
- var stack_probe = stack.length - 1;
- var depth = 0;
-
- // try to recover from error
- for(;;) {
- // check for error recovery rule in this state
- if ((TERROR.toString()) in table[state]) {
- return depth;
- }
- if (state === 0 || stack_probe < 2) {
- return false; // No suitable error recovery rule available.
- }
- stack_probe -= 2; // popStack(1): [symbol, action]
- state = stack[stack_probe];
- ++depth;
- }
- }
-
var symbol, preErrorSymbol, state, action, a, r, yyval = {}, p, len, newState, expected;
while (true) {
// retreive state number from top of stack
@@ -1264,6 +1246,28 @@ _handle_error:
if (typeof action === 'undefined' || !action.length || !action[0]) {
var error_rule_depth;
var errStr = '';
+
+ // Return the rule stack depth where the nearest error rule can be found.
+ // Return FALSE when no error recovery rule was found.
+ function locateNearestErrorRecoveryRule(state) {
+ var stack_probe = stack.length - 1;
+ var depth = 0;
+
+ // try to recover from error
+ for(;;) {
+ // check for error recovery rule in this state
+ if ((TERROR.toString()) in table[state]) {
+ return depth;
+ }
+ if (state === 0 || stack_probe < 2) {
+ return false; // No suitable error recovery rule available.
+ }
+ stack_probe -= 2; // popStack(1): [symbol, action]
+ state = stack[stack_probe];
+ ++depth;
+ }
+ }
+
if (!recovering) {
// first see if there's any chance at hitting an error recovery rule:
error_rule_depth = locateNearestErrorRecoveryRule(state);
@@ -220,21 +220,6 @@ parse: function parse(input) {
}
return token;
}
- function locateNearestErrorRecoveryRule(state) {
- var stack_probe = stack.length - 1;
- var depth = 0;
- for (;;) {
- if (TERROR.toString() in table[state]) {
- return depth;
- }
- if (state === 0 || stack_probe < 2) {
- return false;
- }
- stack_probe -= 2;
- state = stack[stack_probe];
- ++depth;
- }
- }
var symbol, preErrorSymbol, state, action, a, r, yyval = {}, p, len, newState, expected;
while (true) {
state = stack[stack.length - 1];
@@ -247,8 +232,19 @@ parse: function parse(input) {
action = table[state] && table[state][symbol];
}
if (typeof action === "undefined" || !action.length || !action[0]) {
- var error_rule_depth;
var errStr = "";
+ expected = [];
+ for (p in table[state]) {
+ if (this.terminals_[p] && p > TERROR) {
+ 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) + "'";
+ } 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, token: this.terminals_[symbol] || symbol, line: this.lexer.yylineno, loc: yyloc, expected: expected});
}
if (action[0] instanceof Array && action.length > 1) {
throw new Error("Parse Error: multiple actions possible at state: " + state + ", token: " + symbol);
@@ -242,21 +242,6 @@ parse: function parse(input) {
}
return token;
}
- function locateNearestErrorRecoveryRule(state) {
- var stack_probe = stack.length - 1;
- var depth = 0;
- for (;;) {
- if (TERROR.toString() in table[state]) {
- return depth;
- }
- if (state === 0 || stack_probe < 2) {
- return false;
- }
- stack_probe -= 2;
- state = stack[stack_probe];
- ++depth;
- }
- }
var symbol, preErrorSymbol, state, action, a, r, yyval = {}, p, len, newState, expected;
while (true) {
state = stack[stack.length - 1];
@@ -269,8 +254,19 @@ parse: function parse(input) {
action = table[state] && table[state][symbol];
}
if (typeof action === "undefined" || !action.length || !action[0]) {
- var error_rule_depth;
var errStr = "";
+ expected = [];
+ for (p in table[state]) {
+ if (this.terminals_[p] && p > TERROR) {
+ 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) + "'";
+ } 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, token: this.terminals_[symbol] || symbol, line: this.lexer.yylineno, loc: yyloc, expected: expected});
}
if (action[0] instanceof Array && action.length > 1) {
throw new Error("Parse Error: multiple actions possible at state: " + state + ", token: " + symbol);

0 comments on commit 24e36a5

Please sign in to comment.