Skip to content

Commit ef5de68

Browse files
GerHobbeltzaach
authored andcommitted
SHA-1: 34233d9a278fba3bf65e427336a33ac50d188e5d
* Added fix for when grammar file leaves some (invisible) whitespace on the empty lines between lexer rules: previously, the lexer would, depending on the exact input, barf an inexplicable hairball or (worse!) fail silently. Unit tests to verify the success of the fix have been included. Conflicts: lex.l tests/all-tests.js
1 parent dd1d4e5 commit ef5de68

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

lex.l

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ BR \r\n|\n|\r
2323
<conditions>"*" return '*';
2424

2525
<rules>{BR}+ /* */
26+
<rules>\s+{BR}+ /* */
2627
<rules>\s+ this.begin('indented')
2728
<rules>"%%" this.begin('code'); return '%%'
2829
<rules>[a-zA-Z0-9_]+ return 'CHARACTER_LIT'

lex.y

+2-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ range_regex
204204

205205
string
206206
: STRING_LIT
207-
{ $$ = prepareString(yytext.substr(1, yytext.length-2)); }
207+
{ $$ = prepareString(yytext.substr(1, yytext.length - 2)); }
208208
| CHARACTER_LIT
209209
;
210210

@@ -220,3 +220,4 @@ function prepareString (s) {
220220
s = encodeRE(s);
221221
return s;
222222
};
223+

tests/all-tests.js

+25
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,18 @@ exports["test rules with trailing escapes"] = function () {
356356
assert.deepEqual(lex.parse(lexgrammar), expected, "grammar should be parsed correctly");
357357
};
358358

359+
exports["test no brace action with surplus whitespace between rules"] = function () {
360+
var lexgrammar = '%%\n"a" return true;\n \n"b" return 1;\n \n';
361+
var expected = {
362+
rules: [
363+
["a\\b", "return true;"],
364+
["b\\b", "return 1;"]
365+
]
366+
};
367+
368+
assert.deepEqual(lex.parse(lexgrammar), expected, "grammar should be parsed correctly");
369+
};
370+
359371
exports["test windows line endings"] = function () {
360372
var lexgrammar = '%%\r\n"["[^\\]]"]" %{\r\nreturn true;\r\n%}\r\n';
361373
var expected = {
@@ -367,5 +379,18 @@ exports["test windows line endings"] = function () {
367379
assert.deepEqual(lex.parse(lexgrammar), expected, "grammar should be parsed correctly");
368380
};
369381

382+
exports["test braced action with surplus whitespace between rules"] = function () {
383+
var lexgrammar = '%%\n"a" %{ \nreturn true;\n%} \n \n"b" %{ return 1;\n%} \n \n';
384+
var expected = {
385+
rules: [
386+
["a\\b", " \nreturn true;\n"],
387+
["b\\b", " return 1;\n"]
388+
]
389+
};
390+
391+
assert.deepEqual(lex.parse(lexgrammar), expected, "grammar should be parsed correctly");
392+
};
393+
394+
370395
if (require.main === module)
371396
require("test").run(exports);

0 commit comments

Comments
 (0)