Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit e94a130

Browse files
IwanKaramazowIwan
andauthored
Handle windows CRLF correct. (#425)
* Handle windows CRLF correct. `\r\n` should be picked up as one line break, not two. * Add comment about CRLF Co-authored-by: Iwan <[email protected]>
1 parent d8441ff commit e94a130

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

.depend

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,4 @@ src/res_scanner.cmi : src/res_token.cmx src/res_diagnostics.cmi
7272
src/res_token.cmx : src/res_comment.cmx
7373
tests/res_test.cmx : src/res_token.cmx src/res_parser.cmx \
7474
src/res_outcome_printer.cmx src/res_multi_printer.cmx src/res_io.cmx \
75-
src/res_driver.cmx
75+
src/res_driver.cmx src/res_core.cmx

src/res_scanner.ml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,12 @@ let _printDebug ~startPos ~endPos scanner token =
9898
let next scanner =
9999
let nextOffset = scanner.offset + 1 in
100100
(match scanner.ch with
101-
| '\n' | '\r' ->
101+
| '\n' ->
102102
scanner.lineOffset <- nextOffset;
103103
scanner.lnum <- scanner.lnum + 1;
104+
(* What about CRLF (\r + \n) on windows?
105+
* \r\n will always be terminated by a \n
106+
* -> we can just bump the line count on \n *)
104107
| _ -> ());
105108
if nextOffset < String.length scanner.src then (
106109
scanner.offset <- nextOffset;

tests/res_test.ml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,32 @@ module ParserApiTest = struct
147147
assert (parser.token = Res_token.Let);
148148
print_endline "✅ Parser make: initializes parser and checking offsets"
149149

150+
let unixLf () =
151+
let src = "let x = 1\nlet y = 2\nlet z = 3" in
152+
let parser = Res_parser.make src "test.res" in
153+
(match Res_core.parseImplementation parser with
154+
| [x; y; z] ->
155+
assert (x.pstr_loc.loc_start.pos_lnum = 1);
156+
assert (y.pstr_loc.loc_start.pos_lnum = 2);
157+
assert (z.pstr_loc.loc_start.pos_lnum = 3)
158+
| _ -> assert false);
159+
print_endline "✅ Parser handles LF correct"
160+
161+
let windowsCrlf () =
162+
let src = "let x = 1\r\nlet y = 2\r\nlet z = 3" in
163+
let parser = Res_parser.make src "test.res" in
164+
(match Res_core.parseImplementation parser with
165+
| [x; y; z] ->
166+
assert (x.pstr_loc.loc_start.pos_lnum = 1);
167+
assert (y.pstr_loc.loc_start.pos_lnum = 2);
168+
assert (z.pstr_loc.loc_start.pos_lnum = 3)
169+
| _ -> assert false);
170+
print_endline "✅ Parser handles CRLF correct"
171+
150172
let run () =
151173
makeDefault();
174+
unixLf();
175+
windowsCrlf()
152176

153177
end
154178

0 commit comments

Comments
 (0)