Skip to content

Commit

Permalink
zaach#349: add YYRECOVERING macro support as per https://www.gnu.org/…
Browse files Browse the repository at this point in the history
  • Loading branch information
GerHobbelt committed Mar 27, 2017
1 parent 1142438 commit 6eb48eb
Showing 1 changed file with 44 additions and 10 deletions.
54 changes: 44 additions & 10 deletions lib/jison.js
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,7 @@ generator.constructor = function Jison_Generator(grammar, optionalLexerSection,
parseActionsUseYYLOC: this.actionsUseYYLOC,
parseActionsUseParseError: this.actionsUseParseError,
parseActionsUseYYERROR: this.actionsUseYYERROR,
parseActionsUseYYRECOVERING: this.actionsUseYYRECOVERING,
parseActionsUseYYERROK: this.actionsUseYYERROK,
parseActionsUseYYCLEARIN: this.actionsUseYYCLEARIN,
parseActionsUseValueTracking: this.actionsUseValueTracking,
Expand Down Expand Up @@ -1407,9 +1408,10 @@ generator.buildProductionActions = function buildProductionActions() {
.replace(/\byyvstack\b/g, '\x01\x16')
.replace(/\byylstack\b/g, '\x01\x17')
.replace(/\byyerror\b/g, '\x01\x18')
.replace(/\byyerrok\b/g, '\x01\x19')
.replace(/\byyclearin\b/g, '\x01\x1A')
.replace(/\byysp\b/g, '\x01\x1B');
.replace(/\bYYRECOVERING\b/g, '\x01\x19')
.replace(/\byyerrok\b/g, '\x01\x1A')
.replace(/\byyclearin\b/g, '\x01\x1B')
.replace(/\byysp\b/g, '\x01\x1C');

return cmt;
}
Expand Down Expand Up @@ -1536,9 +1538,10 @@ generator.buildProductionActions = function buildProductionActions() {
.replace(/\x01\x16/g, 'yyvstack')
.replace(/\x01\x17/g, 'yylstack')
.replace(/\x01\x18/g, 'yyerror')
.replace(/\x01\x19/g, 'yyerrok')
.replace(/\x01\x1A/g, 'yyclearin')
.replace(/\x01\x1B/g, 'yysp');
.replace(/\x01\x19/g, 'YYRECOVERING')
.replace(/\x01\x1A/g, 'yyerrok')
.replace(/\x01\x1B/g, 'yyclearin')
.replace(/\x01\x1C/g, 'yysp');

// And a final, minimal, fixup for the semicolon-lovers -- like me! ;-)
//
Expand Down Expand Up @@ -1590,6 +1593,7 @@ generator.buildProductionActions = function buildProductionActions() {
this.actionsUseYYLOC = analyzeFeatureUsage(this.performAction, /\byyloc\b/g, 1);
this.actionsUseParseError = analyzeFeatureUsage(this.performAction, /\.parseError\b/g, 0);
this.actionsUseYYERROR = analyzeFeatureUsage(this.performAction, /\byyerror\b/g, 0);
this.actionsUseYYRECOVERING = analyzeFeatureUsage(this.performAction, /\bYYRECOVERING\b/g, 0);
this.actionsUseYYERROK = analyzeFeatureUsage(this.performAction, /\byyerrok\b/g, 0);
this.actionsUseYYCLEARIN = analyzeFeatureUsage(this.performAction, /\byyclearin\b/g, 0);
// At this point in time, we have already expanded `$name`, `$$` and `$n` to its `$$[n]` index expression.
Expand All @@ -1614,6 +1618,7 @@ generator.buildProductionActions = function buildProductionActions() {

this.performAction = this.performAction
.replace(/\byyerror\b/g, 'yy.parser.yyError')
.replace(/\bYYRECOVERING\b(?:\s*\(\s*\))?/g, 'yy.parser.yyRecovering()')
.replace(/\byyerrok\b(?:\s*\(\s*\))?/g, 'yy.parser.yyErrOk()')
.replace(/\byyclearin\b(?:\s*\(\s*\))?/g, 'yy.parser.yyClearIn()');

Expand Down Expand Up @@ -1659,6 +1664,7 @@ generator.buildProductionActions = function buildProductionActions() {
actionsUseYYLOC: this.actionsUseYYLOC,
actionsUseParseError: this.actionsUseParseError,
actionsUseYYERROR: this.actionsUseYYERROR,
actionsUseYYRECOVERING: this.actionsUseYYRECOVERING,
actionsUseYYERROK: this.actionsUseYYERROK,
actionsUseYYCLEARIN: this.actionsUseYYCLEARIN,
actionsUseValueTracking: this.actionsUseValueTracking,
Expand Down Expand Up @@ -2957,6 +2963,7 @@ generatorMixin.generateGenericHeaderComment = function generateGenericHeaderComm
*
* parseError: function(str, hash, ExceptionClass),
* yyError: function(str, ...),
* yyRecovering: function(),
* yyErrOk: function(),
* yyClearIn: function(),
*
Expand Down Expand Up @@ -3541,15 +3548,15 @@ function removeUnusedKernelFeatures(parseFn, info) {
.replace(/\n\s+if\s+\(yydebug\)\s+yydebug\([^]+?\}\);[^\r\n]*?/g, '\n\n\n\n\n\n\n\n\n');
}

if (!info.actionsUseYYERROK && !info.actionsUseYYCLEARIN && !info.actionsUseYYERROR) {
if (!info.actionsUseYYERROK && !info.actionsUseYYRECOVERING && !info.actionsUseYYCLEARIN && !info.actionsUseYYERROR) {
/*
* Kill long multi-line comment about yyerror + yyerrok + yyclearin before this code:
* Kill long multi-line comment about yyerror + YYRECOVERING + yyerrok + yyclearin before this code:
*
* if (this.yyError) {
* ...
*/
parseFn = parseFn
.replace(/\s+\/\/.*setup `yyError`, `yyErrOk` and `yyClearIn` functions[^\0]+?\n\s+if \(/g, '\n\n\n\n\n if (');
.replace(/\s+\/\/.*setup `yyError`, `YYRECOVERING`, `yyErrOk` and `yyClearIn` functions[^\0]+?\n\s+if \(/g, '\n\n\n\n\n if (');
}

if (!info.actionsUseYYERROR) {
Expand All @@ -3566,6 +3573,20 @@ function removeUnusedKernelFeatures(parseFn, info) {
.replace(/\s+if \(this\.yyError\) \{[^\0]+?\};\n\s+\}\n/g, '\n\n\n\n\n');
}

if (!info.actionsUseYYRECOVERING) {
/*
* Kill this code:
*
* if (this.yyRecovering) {
* this.yyRecovering = function yyRecovering() {
* return recovering;
* };
* }
*/
parseFn = parseFn
.replace(/\s+if \(this\.yyRecovering\) \{[^\0]+?\};\n\s+\}\n/g, '\n\n\n\n\n');
}

if (!info.actionsUseYYERROK) {
/*
* Kill this code:
Expand Down Expand Up @@ -4291,6 +4312,7 @@ lrGeneratorMixin.generateModule_ = function generateModule_() {
// uses yylloc: ..................... ${this.actionsUseYYLOC}
// uses ParseError API: ............. ${this.actionsUseParseError}
// uses YYERROR: .................... ${this.actionsUseYYERROR}
// uses YYRECOVERING: ............... ${this.actionsUseYYRECOVERING}
// uses YYERROK: .................... ${this.actionsUseYYERROK}
// uses YYCLEARIN: .................. ${this.actionsUseYYCLEARIN}
// tracks rule values: .............. ${this.actionsUseValueTracking}
Expand Down Expand Up @@ -4340,6 +4362,10 @@ lrGeneratorMixin.generateModule_ = function generateModule_() {
this.actionsUseYYERROR ?
'yyError: 1' :
[]
).concat(
this.actionsUseYYRECOVERING ?
'yyRecovering: 1' :
[]
).concat(
this.actionsUseYYERROK ?
'yyErrOk: 1' :
Expand Down Expand Up @@ -5235,6 +5261,7 @@ function define_parser_APIs_1() {
__error_infos: [], // INTERNAL USE ONLY: the set of parseErrorInfo objects created since the last cleanup

// APIs which will be set up depending on user action code analysis:
//yyRecovering: 0,
//yyErrOk: 0,
//yyClearIn: 0,

Expand Down Expand Up @@ -5434,7 +5461,7 @@ parser.parse = function parse(input, parseParams) {
};
}

// *Always* setup `yyError`, `yyErrOk` and `yyClearIn` functions as it is paramount
// *Always* setup `yyError`, `YYRECOVERING`, `yyErrOk` and `yyClearIn` functions as it is paramount
// to have *their* closure match ours -- if we only set them up once,
// any subsequent `parse()` runs will fail in very obscure ways when
// these functions are invoked in the user action code block(s) as
Expand All @@ -5450,6 +5477,13 @@ parser.parse = function parse(input, parseParams) {
};
}

if (this.yyRecovering) {
this.yyRecovering = function yyRecovering() {
if (yydebug) yydebug('yyrecovering: ', { symbol: symbol, state: state, newState: newState, recovering: recovering, action: action });
return recovering;
};
}

if (this.yyErrOk) {
this.yyErrOk = function yyErrOk() {
if (yydebug) yydebug('yyerrok: ', { symbol: symbol, state: state, newState: newState, recovering: recovering, action: action });
Expand Down

0 comments on commit 6eb48eb

Please sign in to comment.