Skip to content

Commit

Permalink
Preserve line endings in delimited string literals (#2628)
Browse files Browse the repository at this point in the history
This fixes an AST changed error caught by test-branch in the compiler's
testsuite.

Co-authored-by: hhugo <hugo.heuzard@gmail.com>
  • Loading branch information
Julow and hhugo authored Nov 25, 2024
1 parent 3ee50c0 commit d48e27f
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ profile. This started with version 0.26.0.

### Highlight

- \* Support OCaml 5.2 syntax (#2519, #2544, #2590, #2596, #2621, @Julow, @EmileTrotignon)
- \* Support OCaml 5.2 syntax (#2519, #2544, #2590, #2596, #2621, #2628, @Julow, @EmileTrotignon, @hhugo)
This includes local open in types, raw identifiers, and the new
representation for functions.
This might change the formatting of some functions due to the formatting code
Expand Down
5 changes: 4 additions & 1 deletion lib/Literal_lexer.mll
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ and string_aux mode = parse
string_aux mode lexbuf
}
| newline
{ store_string (Lexing.lexeme lexbuf);
{ (* See store_normalized_newline in vendor/parser-standard/lexer.mll. *)
(match Lexing.lexeme lexbuf with
| "\n" -> store_string "\n"
| s -> store_string (String.sub s 1 (String.length s - 1)));
string_aux mode lexbuf }
| eof
{ raise Parse_error }
Expand Down
15 changes: 15 additions & 0 deletions test/passing/gen/dune.inc
Original file line number Diff line number Diff line change
Expand Up @@ -3487,6 +3487,21 @@
(alias runtest)
(action (diff new.ml.err new.ml.stderr)))

(rule
(deps .ocamlformat dune-project)
(action
(with-stdout-to newlines.ml.stdout
(with-stderr-to newlines.ml.stderr
(run %{bin:ocamlformat} --name newlines.ml --margin-check %{dep:../tests/newlines.ml})))))

(rule
(alias runtest)
(action (diff newlines.ml.ref newlines.ml.stdout)))

(rule
(alias runtest)
(action (diff newlines.ml.err newlines.ml.stderr)))

(rule
(deps .ocamlformat dune-project)
(action
Expand Down
18 changes: 18 additions & 0 deletions test/passing/refs.default/newlines.ml.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
(* TEST *)

let check ~kind ~input ~result =
if input <> result then
Printf.printf "FAIL: %s %S should normalize to %S\n" kind input result
;;

check ~kind:"string literal" ~input:"\n" ~result:"\n";
check ~kind:"quoted string literal" ~input:{|
|} ~result:"\n";

check ~kind:"string literal" ~input:"\n" ~result:"\n";
check ~kind:"quoted string literal" ~input:{|
|} ~result:"\n";

check ~kind:"string literal" ~input:"\n" ~result:"\r\n";
check ~kind:"quoted string literal" ~input:{|
|} ~result:"\r\n"
Expand Down
28 changes: 28 additions & 0 deletions test/passing/refs.janestreet/newlines.ml.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
(* TEST *)

let check ~kind ~input ~result =
if input <> result
then Printf.printf "FAIL: %s %S should normalize to %S\n" kind input result
;;

check ~kind:"string literal" ~input:"\n" ~result:"\n";
check
~kind:"quoted string literal"
~input:
{|
|}
~result:"\n";
check ~kind:"string literal" ~input:"\n" ~result:"\n";
check
~kind:"quoted string literal"
~input:
{|
|}
~result:"\n";
check ~kind:"string literal" ~input:"\n" ~result:"\r\n";
check
~kind:"quoted string literal"
~input:
{|
|}
~result:"\r\n"
Expand Down
16 changes: 16 additions & 0 deletions test/passing/refs.ocamlformat/newlines.ml.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
(* TEST *)

let check ~kind ~input ~result =
if input <> result then
Printf.printf "FAIL: %s %S should normalize to %S\n" kind input result
;;

check ~kind:"string literal" ~input:"\n" ~result:"\n" ;
check ~kind:"quoted string literal" ~input:{|
|} ~result:"\n" ;
check ~kind:"string literal" ~input:"\n" ~result:"\n" ;
check ~kind:"quoted string literal" ~input:{|
|} ~result:"\n" ;
check ~kind:"string literal" ~input:"\n" ~result:"\r\n" ;
check ~kind:"quoted string literal" ~input:{|
|} ~result:"\r\n"
Expand Down
23 changes: 23 additions & 0 deletions test/passing/tests/newlines.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
(* TEST *)

let check ~kind ~input ~result =
if input <> result then
Printf.printf "FAIL: %s %S should normalize to %S
"
kind input result
;;

check ~kind:"string literal" ~input:"
" ~result:"\n";
check ~kind:"quoted string literal" ~input:{|
|} ~result:"\n";

check ~kind:"string literal" ~input:"
" ~result:"\n";
check ~kind:"quoted string literal" ~input:{|
|} ~result:"\n";

check ~kind:"string literal" ~input:"
" ~result:"\r\n";
check ~kind:"quoted string literal" ~input:{|
|} ~result:"\r\n";
Expand Down

0 comments on commit d48e27f

Please sign in to comment.