Skip to content

Commit

Permalink
refactor generation so that in-memory and to-source code paths are th…
Browse files Browse the repository at this point in the history
…e same
  • Loading branch information
zaach committed Mar 31, 2014
1 parent 2d151e5 commit 9e0cc65
Showing 1 changed file with 13 additions and 22 deletions.
35 changes: 13 additions & 22 deletions lib/jison.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,13 +253,7 @@ generator.buildProductions = function buildProductions(bnf, productions, nonterm
var parameters = "yytext, yyleng, yylineno, yy, yystate /* action[1] */, $$ /* vstack */, _$ /* lstack */";
if (this.parseParams) parameters += ', ' + this.parseParams.join(', ');

// first try to create the performAction function the old way,
// but this will break for some legal constructs in the user action code:
try {
this.performAction = Function(parameters, actions);
} catch (e) {
this.performAction = "function anonymous(" + parameters + ") {\n" + actions + "\n}";
}
this.performAction = "function anonymous(" + parameters + ") {\n" + actions + "\n}";

function buildProduction (handle) {
var r, rhs, i;
Expand Down Expand Up @@ -1054,7 +1048,17 @@ lrGeneratorMixin.generateModule = function generateModule (opt) {
+ " recoverable: (boolean: TRUE when the parser has a error recovery rule available for this particular error)\n"
+ " }\n"
+ "*/\n";
out += (moduleName.match(/\./) ? moduleName : "var "+moduleName)+" = (function(){";
out += (moduleName.match(/\./) ? moduleName : "var "+moduleName) +
" = " + this.generateModuleExpr();

return out;
};


lrGeneratorMixin.generateModuleExpr = function generateModuleExpr () {
var out = '';

out += "(function(){";
out += "\nvar parser = "+this.generateModule_();
out += "\n"+this.moduleInclude;
if (this.lexer && this.lexer.generateModule) {
Expand Down Expand Up @@ -1157,16 +1161,8 @@ var lrGeneratorDebug = {
var parser = typal.beget();

lrGeneratorMixin.createParser = function createParser () {
var p = parser.beget();
p.yy = {};

p.init(this);

// don't throw if grammar recovers from errors
if (this.hasErrorRecovery) {
p.parseError = traceParseError;
p.recover = true;
}
var p = eval(this.generateModuleExpr());

// for debugging
p.productions = this.productions;
Expand All @@ -1180,16 +1176,11 @@ lrGeneratorMixin.createParser = function createParser () {
}

// backwards compatability
p.lexer = this.lexer;
p.generate = bind('generate');
p.generateAMDModule = bind('generateAMDModule');
p.generateModule = bind('generateModule');
p.generateCommonJSModule = bind('generateCommonJSModule');

p.Parser = function () {
return self.createParser();
};

return p;
};

Expand Down

0 comments on commit 9e0cc65

Please sign in to comment.