diff --git a/CHANGES.md b/CHANGES.md index 815173e66a..4b0887f300 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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 diff --git a/lib/Literal_lexer.mll b/lib/Literal_lexer.mll index 95ed7eecfe..704fcf9bc4 100644 --- a/lib/Literal_lexer.mll +++ b/lib/Literal_lexer.mll @@ -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 } diff --git a/test/passing/gen/dune.inc b/test/passing/gen/dune.inc index 27454cae96..2643a7c96d 100644 --- a/test/passing/gen/dune.inc +++ b/test/passing/gen/dune.inc @@ -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 diff --git a/test/passing/refs.default/newlines.ml.ref b/test/passing/refs.default/newlines.ml.ref new file mode 100644 index 0000000000..4c565d44bd --- /dev/null +++ b/test/passing/refs.default/newlines.ml.ref @@ -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" diff --git a/test/passing/refs.janestreet/newlines.ml.ref b/test/passing/refs.janestreet/newlines.ml.ref new file mode 100644 index 0000000000..c019eb7c6a --- /dev/null +++ b/test/passing/refs.janestreet/newlines.ml.ref @@ -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" diff --git a/test/passing/refs.ocamlformat/newlines.ml.ref b/test/passing/refs.ocamlformat/newlines.ml.ref new file mode 100644 index 0000000000..8f51b3b9b5 --- /dev/null +++ b/test/passing/refs.ocamlformat/newlines.ml.ref @@ -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" diff --git a/test/passing/tests/newlines.ml b/test/passing/tests/newlines.ml new file mode 100644 index 0000000000..c940b04faa --- /dev/null +++ b/test/passing/tests/newlines.ml @@ -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";