Skip to content

Commit d48e27f

Browse files
Julowhhugo
andauthored
Preserve line endings in delimited string literals (#2628)
This fixes an AST changed error caught by test-branch in the compiler's testsuite. Co-authored-by: hhugo <hugo.heuzard@gmail.com>
1 parent 3ee50c0 commit d48e27f

File tree

7 files changed

+105
-2
lines changed

7 files changed

+105
-2
lines changed

CHANGES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ profile. This started with version 0.26.0.
88

99
### Highlight
1010

11-
- \* Support OCaml 5.2 syntax (#2519, #2544, #2590, #2596, #2621, @Julow, @EmileTrotignon)
11+
- \* Support OCaml 5.2 syntax (#2519, #2544, #2590, #2596, #2621, #2628, @Julow, @EmileTrotignon, @hhugo)
1212
This includes local open in types, raw identifiers, and the new
1313
representation for functions.
1414
This might change the formatting of some functions due to the formatting code

lib/Literal_lexer.mll

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,10 @@ and string_aux mode = parse
8181
string_aux mode lexbuf
8282
}
8383
| newline
84-
{ store_string (Lexing.lexeme lexbuf);
84+
{ (* See store_normalized_newline in vendor/parser-standard/lexer.mll. *)
85+
(match Lexing.lexeme lexbuf with
86+
| "\n" -> store_string "\n"
87+
| s -> store_string (String.sub s 1 (String.length s - 1)));
8588
string_aux mode lexbuf }
8689
| eof
8790
{ raise Parse_error }

test/passing/gen/dune.inc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3487,6 +3487,21 @@
34873487
(alias runtest)
34883488
(action (diff new.ml.err new.ml.stderr)))
34893489

3490+
(rule
3491+
(deps .ocamlformat dune-project)
3492+
(action
3493+
(with-stdout-to newlines.ml.stdout
3494+
(with-stderr-to newlines.ml.stderr
3495+
(run %{bin:ocamlformat} --name newlines.ml --margin-check %{dep:../tests/newlines.ml})))))
3496+
3497+
(rule
3498+
(alias runtest)
3499+
(action (diff newlines.ml.ref newlines.ml.stdout)))
3500+
3501+
(rule
3502+
(alias runtest)
3503+
(action (diff newlines.ml.err newlines.ml.stderr)))
3504+
34903505
(rule
34913506
(deps .ocamlformat dune-project)
34923507
(action

test/passing/refs.default/newlines.ml.ref

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
(* TEST *)
2+
3+
let check ~kind ~input ~result =
4+
if input <> result then
5+
Printf.printf "FAIL: %s %S should normalize to %S\n" kind input result
6+
;;
7+
8+
check ~kind:"string literal" ~input:"\n" ~result:"\n";
9+
check ~kind:"quoted string literal" ~input:{|
10+
|} ~result:"\n";
11+
12+
check ~kind:"string literal" ~input:"\n" ~result:"\n";
13+
check ~kind:"quoted string literal" ~input:{|
14+
|} ~result:"\n";
15+
16+
check ~kind:"string literal" ~input:"\n" ~result:"\r\n";
17+
check ~kind:"quoted string literal" ~input:{|
18+
|} ~result:"\r\n"

test/passing/refs.janestreet/newlines.ml.ref

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
(* TEST *)
2+
3+
let check ~kind ~input ~result =
4+
if input <> result
5+
then Printf.printf "FAIL: %s %S should normalize to %S\n" kind input result
6+
;;
7+
8+
check ~kind:"string literal" ~input:"\n" ~result:"\n";
9+
check
10+
~kind:"quoted string literal"
11+
~input:
12+
{|
13+
|}
14+
~result:"\n";
15+
check ~kind:"string literal" ~input:"\n" ~result:"\n";
16+
check
17+
~kind:"quoted string literal"
18+
~input:
19+
{|
20+
|}
21+
~result:"\n";
22+
check ~kind:"string literal" ~input:"\n" ~result:"\r\n";
23+
check
24+
~kind:"quoted string literal"
25+
~input:
26+
{|
27+
|}
28+
~result:"\r\n"

test/passing/refs.ocamlformat/newlines.ml.ref

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
(* TEST *)
2+
3+
let check ~kind ~input ~result =
4+
if input <> result then
5+
Printf.printf "FAIL: %s %S should normalize to %S\n" kind input result
6+
;;
7+
8+
check ~kind:"string literal" ~input:"\n" ~result:"\n" ;
9+
check ~kind:"quoted string literal" ~input:{|
10+
|} ~result:"\n" ;
11+
check ~kind:"string literal" ~input:"\n" ~result:"\n" ;
12+
check ~kind:"quoted string literal" ~input:{|
13+
|} ~result:"\n" ;
14+
check ~kind:"string literal" ~input:"\n" ~result:"\r\n" ;
15+
check ~kind:"quoted string literal" ~input:{|
16+
|} ~result:"\r\n"

test/passing/tests/newlines.ml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
(* TEST *)
2+
3+
let check ~kind ~input ~result =
4+
if input <> result then
5+
Printf.printf "FAIL: %s %S should normalize to %S
6+
"
7+
kind input result
8+
;;
9+
10+
check ~kind:"string literal" ~input:"
11+
" ~result:"\n";
12+
check ~kind:"quoted string literal" ~input:{|
13+
|} ~result:"\n";
14+
15+
check ~kind:"string literal" ~input:"
16+
" ~result:"\n";
17+
check ~kind:"quoted string literal" ~input:{|
18+
|} ~result:"\n";
19+
20+
check ~kind:"string literal" ~input:"
21+
" ~result:"\r\n";
22+
check ~kind:"quoted string literal" ~input:{|
23+
|} ~result:"\r\n";

0 commit comments

Comments
 (0)