Skip to content

Commit d4d4ded

Browse files
committed
fix windows line endings support
1 parent b2f16ac commit d4d4ded

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

lex.l

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
NAME [a-zA-Z_][a-zA-Z0-9_-]*
3+
BR \r\n|\n|\r
34

45
%s indented trail rules
56
%x code start_condition options conditions action
@@ -20,32 +21,32 @@ NAME [a-zA-Z_][a-zA-Z0-9_-]*
2021
<conditions>"," return ','
2122
<conditions>"*" return '*'
2223

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

2829
<options>{NAME} yy.options[yytext] = true
29-
<options>\n+ this.begin('INITIAL')
30-
<options>\s+\n+ this.begin('INITIAL')
30+
<options>{BR}+ this.begin('INITIAL')
31+
<options>\s+{BR}+ this.begin('INITIAL')
3132
<options>\s+ /* empty */
3233

3334
<start_condition>{NAME} return 'START_COND'
34-
<start_condition>\n+ this.begin('INITIAL')
35-
<start_condition>\s+\n+ this.begin('INITIAL')
35+
<start_condition>{BR}+ this.begin('INITIAL')
36+
<start_condition>\s+{BR}+ this.begin('INITIAL')
3637
<start_condition>\s+ /* empty */
3738

38-
<trail>.*\n+ this.begin('rules')
39+
<trail>.*{BR}+ this.begin('rules')
3940

4041
<indented>"{" yy.depth = 0; this.begin('action'); return '{'
41-
<indented>"%{"(.|\n)*?"%}" this.begin('trail'); yytext = yytext.substr(2, yytext.length-4);return 'ACTION'
42-
"%{"(.|\n)*?"%}" yytext = yytext.substr(2, yytext.length-4); return 'ACTION'
42+
<indented>"%{"(.|{BR})*?"%}" this.begin('trail'); yytext = yytext.substr(2, yytext.length-4);return 'ACTION'
43+
"%{"(.|{BR})*?"%}" yytext = yytext.substr(2, yytext.length-4); return 'ACTION'
4344
<indented>.+ this.begin('rules'); return 'ACTION'
4445
4546
"/*"(.|\n|\r)*?"*/" /* ignore */
4647
"//".* /* ignore */
4748
48-
\n+ /* */
49+
{BR}+ /* */
4950
\s+ /* */
5051
{NAME} return 'NAME'
5152
\"("\\\\"|'\"'|[^"])*\" yytext = yytext.replace(/\\"/g,'"');return 'STRING_LIT'
@@ -81,7 +82,7 @@ NAME [a-zA-Z_][a-zA-Z0-9_-]*
8182
. /* ignore bad characters */
8283
<*><<EOF>> return 'EOF'
8384
84-
<code>(.|\n)+ return 'CODE'
85+
<code>(.|{BR})+ return 'CODE'
8586
8687
%%
8788

tests/all-tests.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,5 +345,16 @@ exports["test rules with trailing escapes"] = function () {
345345
assert.deepEqual(lex.parse(lexgrammar), expected, "grammar should be parsed correctly");
346346
};
347347

348+
exports["test windows line endings"] = function () {
349+
var lexgrammar = '%%\r\n"["[^\\]]"]" %{\r\nreturn true;\r\n%}\r\n';
350+
var expected = {
351+
rules: [
352+
["\\[[^\\]]\\]", "\r\nreturn true;\r\n"]
353+
]
354+
};
355+
356+
assert.deepEqual(lex.parse(lexgrammar), expected, "grammar should be parsed correctly");
357+
};
358+
348359
if (require.main === module)
349360
require("test").run(exports);

0 commit comments

Comments
 (0)