Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix this.lexer undefined, fixes #246 #267

Merged
merged 1 commit into from
Aug 18, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/jison.js
Original file line number Diff line number Diff line change
Expand Up @@ -1338,6 +1338,7 @@ lrGeneratorMixin.createParser = function createParser () {
}

// backwards compatability
p.lexer = this.lexer;
p.generate = bind('generate');
p.generateAMDModule = bind('generateAMDModule');
p.generateModule = bind('generateModule');
Expand Down
33 changes: 33 additions & 0 deletions tests/parser/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ var Jison = require("../setup").Jison,
Lexer = require("../setup").Lexer,
assert = require("assert");

var fs = require('fs');
var path = require('path');

exports["test amd module generator"] = function() {
var lexData = {
rules: [
Expand Down Expand Up @@ -346,3 +349,33 @@ exports["test module include with each generator type"] = function () {
assert.ok(/TEST_VAR/.test(source), type + " supports module include");
});
};

// test for issue #246
exports["test compiling a parser/lexer"] = function () {
var grammar =
'// Simple "happy happy joy joy" parser, written by Nolan Lawson\n' +
'// Based on the song of the same name.\n\n' +
'%lex\n%%\n\n\\s+ /* skip whitespace */\n' +
'("happy") return \'happy\'\n' +
'("joy") return \'joy\'\n' +
'<<EOF>> return \'EOF\'\n\n' +
'/lex\n\n%start expressions\n\n' +
'%ebnf\n\n%%\n\n' +
'expressions\n : e EOF\n {return $1;}\n ;\n\n' +
'e\n : phrase+ \'joy\'? -> $1 + \' \' + yytext \n ;\n\n' +
'phrase\n : \'happy\' \'happy\' \'joy\' \'joy\' ' +
' -> [$1, $2, $3, $4].join(\' \'); \n ;';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I actually wrote this just for the test. Fun exercise. :)


var parser = new Jison.Parser(grammar);
var generated = parser.generate();

var tmpFile = path.resolve(__dirname, 'tmp-parser.js');
fs.writeFileSync(tmpFile, generated);
var parser2 = require('./tmp-parser');

assert.ok(parser.parse('happy happy joy joy joy') === 'happy happy joy joy joy',
'original parser works');
assert.ok(parser2.parse('happy happy joy joy joy') === 'happy happy joy joy joy',
'generated parser works');
fs.unlinkSync(tmpFile);
};