Skip to content

Commit

Permalink
Handle CRLF files correctly in let-syntax
Browse files Browse the repository at this point in the history
Previously a CRLF source file would be re-emitted with \r\n translated
to \r\r\n which affected the fragments in src/preprocessing.ml. The
lexer is now altered so that \r is never written to the output channel
so on Windows the result will always be CRLF encoded and on Unix the
result will always be LF encoded, regardless of the input.

Signed-off-by: David Allsopp <[email protected]>
  • Loading branch information
dra27 authored and jeremiedimino committed Sep 13, 2018
1 parent 67fbbe4 commit d7bd6c6
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/let-syntax/lexer.mll
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,21 @@ let fail lb msg =
exit 1

let ps t s =
String.iter s ~f:(function
| '\n' -> t.line <- t.line + 1
| _ -> ());
output_string t.oc s
let len = String.length s in
if len > 0 then
if s.[len - 1] = '\n' then begin
assert (String.index s '\n' = len - 1);
t.line <- t.line + 1;
if len > 1 && s.[len - 2] = '\r' then begin
output_substring t.oc s 0 (len - 2);
output_char t.oc '\n'
end else
output_string t.oc s
end else
output_string t.oc s
let pc t = function
| '\n' -> t.line <- t.line + 1; output_char t.oc '\n'
| '\r' -> assert false
| c -> output_char t.oc c
let npc t n c = for _ = 1 to n do pc t c done
let pf t fmt = ksprintf (ps t) fmt
Expand Down Expand Up @@ -179,8 +188,8 @@ and lhs t = parse
| eof
{ ()
}
| newline as s
{ Buffer.add_string t.buf s;
| newline
{ Buffer.add_char t.buf '\n';
Lexing.new_line lexbuf;
lhs t lexbuf
}
Expand Down

0 comments on commit d7bd6c6

Please sign in to comment.