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 undefined start conditions error : https://github.com/zaach/jison-le... #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
15 changes: 15 additions & 0 deletions regexp-lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ function prepareRules(rules, macros, actions, tokens, startConditions, caseless)
// Add to explicit start conditions
conditions = rules[i].shift();
for (k=0;k<conditions.length;k++) {
if (!startConditions.hasOwnProperty(conditions[k])) {
startConditions[conditions[k]] = {
rules: [], inclusive: false
};
console.warn('Lexer Warning : "' + conditions[k] + '" start condition should be defined as %s or %x');
}
startConditions[conditions[k]].rules.push(i);
}
}
Expand Down Expand Up @@ -170,6 +176,15 @@ RegExpLexer.prototype = {
// consumes and returns one char from the input
input: function () {
var ch = this._input[0];
if ( ch == '\r' && this._input[1] == '\n' ) {
ch += '\n';
this.yyleng++;
this.offset++;
this._input = this._input.slice(1);
if (this.options.ranges) {
this.yylloc.range[1]++;
}
}
this.yytext += ch;
this.yyleng++;
this.offset++;
Expand Down
69 changes: 67 additions & 2 deletions tests/regexplexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ exports["test yytext overwrite"] = function() {
assert.equal(lexer.yytext, "hi der");
};

exports["test yylineno"] = function() {
exports["test yylineno with test_match"] = function() {
var dict = {
rules: [
["\\s+", "/* skip whitespace */" ],
Expand All @@ -257,7 +257,6 @@ exports["test yylineno"] = function() {
};

var input = "x\nxy\n\n\nx";

var lexer = new RegExpLexer(dict, input);
assert.equal(lexer.yylineno, 0);
assert.equal(lexer.lex(), "x");
Expand All @@ -269,6 +268,48 @@ exports["test yylineno"] = function() {
assert.equal(lexer.yylineno, 4);
};

exports["test yylineno with input"] = function() {
var dict = {
rules: [
["\\s+", "/* skip whitespace */" ],
["x", "return 'x';" ],
["y", "return 'y';" ]
]
};

// windows style
var input = "a\r\nb";
var lexer = new RegExpLexer(dict, input);
assert.equal(lexer.yylineno, 0);
assert.equal(lexer.input(), "a");
assert.equal(lexer.input(), "\r\n");
assert.equal(lexer.yylineno, 1);
assert.equal(lexer.input(), "b");
assert.equal(lexer.yylineno, 1);

// linux style
var input = "a\nb";
var lexer = new RegExpLexer(dict, input);
assert.equal(lexer.yylineno, 0);
assert.equal(lexer.input(), "a");
assert.equal(lexer.input(), "\n");
assert.equal(lexer.yylineno, 1);
assert.equal(lexer.input(), "b");
assert.equal(lexer.yylineno, 1);

// mac style
var input = "a\rb";
var lexer = new RegExpLexer(dict, input);
assert.equal(lexer.yylineno, 0);
assert.equal(lexer.input(), "a");
assert.equal(lexer.input(), "\r");
assert.equal(lexer.yylineno, 1);
assert.equal(lexer.input(), "b");
assert.equal(lexer.yylineno, 1);

};


exports["test yylloc"] = function() {
var dict = {
rules: [
Expand Down Expand Up @@ -718,6 +759,30 @@ exports["test start condition constants"] = function() {
assert.equal(lexer.lex(), "EOF");
};

exports["test start condition & warning"] = function() {
var dict = {
startConditions: {
"INITIAL": 0,
},
rules: [
["\\/\\/", "this.begin('EAT');" ],
[["EAT"], ".", "if (YYSTATE==='EAT') return 'E';" ],
["x", "if (YY_START==='INITIAL') return 'X';" ],
["y", "return 'Y';" ],
[["*"],"$", "return 'EOF';" ]
]
};
var input = "xy//y";

var lexer = new RegExpLexer(dict);
lexer.setInput(input);

assert.equal(lexer.lex(), "X");
assert.equal(lexer.lex(), "Y");
assert.equal(lexer.lex(), "E");
assert.equal(lexer.lex(), "EOF");
};

exports["test unicode encoding"] = function() {
var dict = {
rules: [
Expand Down