Skip to content

Commit e656753

Browse files
committed
Fix end of line parsing on Windows
1 parent 04bc092 commit e656753

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

lib/lexer_mdx.mll

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,21 @@ let labels l =
1616
failwith msg
1717
}
1818

19-
let eol = '\n' | eof
20-
let ws = ' ' | '\t'
19+
let eol = '\n' | '\r' '\n' | eof
20+
let ws = [' ' '\t']
21+
22+
let until_eol = [^'\n' '\r']
23+
let until_ws = [^' ' '\t']
24+
let until_ws_or_eol = [^' ' '\t' '\n' '\r']
2125

2226
rule text section = parse
2327
| eof { [] }
24-
| ("#"+ as n) " " ([^'\n']* as str) eol
28+
| ("#"+ as n) " " (until_eol* as str) eol
2529
{ let section = (String.length n, str) in
2630
newline lexbuf;
2731
`Section section :: text (Some section) lexbuf }
28-
| ( "<!--" ws* "$MDX" ws* ([^' ' '\n']* as label_cmt) ws* "-->" ws* eol? )?
29-
"```" ([^' ' '\n']* as h) ws* ([^'\n']* as legacy_labels) eol
32+
| ( "<!--" ws* "$MDX" ws* (until_ws* as label_cmt) ws* "-->" ws* eol? )?
33+
"```" (until_ws_or_eol* as h) ws* (until_eol* as legacy_labels) eol
3034
{ let header = Block.Header.of_string h in
3135
let contents = block lexbuf in
3236
let labels, legacy_labels =
@@ -63,7 +67,7 @@ rule text section = parse
6367
List.iter (fun _ -> newline lexbuf) errors;
6468
newline lexbuf);
6569
`Block block :: text section lexbuf }
66-
| "<!--" ws* "$MDX" ws* ([^' ' '\n']* as label_cmt) ws* "-->" ws* eol
70+
| "<!--" ws* "$MDX" ws* (until_ws* as label_cmt) ws* "-->" ws* eol
6771
{ let labels = labels label_cmt in
6872
newline lexbuf;
6973
let loc = Location.curr lexbuf in
@@ -73,24 +77,24 @@ rule text section = parse
7377
| Error (`Msg msg) -> failwith msg
7478
in
7579
`Block block :: text section lexbuf }
76-
| ([^'\n']* as str) eol
80+
| (until_eol* as str) eol
7781
{ newline lexbuf;
7882
`Text str :: text section lexbuf }
7983

8084
and block = parse
8185
| eof | "```" ws* eol { [] }
82-
| ([^'\n'] * as str) eol { str :: block lexbuf }
86+
| (until_eol* as str) eol { str :: block lexbuf }
8387

8488
and error_block = parse
8589
| "```mdx-error" ws* eol { block lexbuf }
8690

8791
and cram_text section = parse
8892
| eof { [] }
89-
| ("#"+ as n) " " ([^'\n']* as str) eol
93+
| ("#"+ as n) " " (until_eol* as str) eol
9094
{ let section = (String.length n, str) in
9195
newline lexbuf;
9296
`Section section :: cram_text (Some section) lexbuf }
93-
| " " ([^'\n']* as first_line) eol
97+
| " " (until_eol* as first_line) eol
9498
{ let header = Some (Block.Header.Shell `Sh) in
9599
let requires_empty_line, contents = cram_block lexbuf in
96100
let contents = first_line :: contents in
@@ -109,7 +113,7 @@ and cram_text section = parse
109113
in
110114
`Block block
111115
:: (if requires_empty_line then `Text "" :: rest else rest) }
112-
| "<-- non-deterministic" ws* ([^'\n']* as choice) eol
116+
| "<-- non-deterministic" ws* (until_eol* as choice) eol
113117
{ let header = Some (Block.Header.Shell `Sh) in
114118
let requires_empty_line, contents = cram_block lexbuf in
115119
let labels =
@@ -132,14 +136,14 @@ and cram_text section = parse
132136
in
133137
`Block block
134138
:: (if requires_empty_line then `Text "" :: rest else rest) }
135-
| ([^'\n']* as str) eol
139+
| (until_eol* as str) eol
136140
{ newline lexbuf;
137141
`Text str :: cram_text section lexbuf }
138142

139143
and cram_block = parse
140144
| eof { false, [] }
141145
| eol { newline lexbuf; true, [] }
142-
| " " ([^'\n'] * as str) eol
146+
| " " (until_eol* as str) eol
143147
{ let requires_empty_line, lst = cram_block lexbuf in
144148
requires_empty_line, str :: lst }
145149

0 commit comments

Comments
 (0)