Skip to content

Commit 0383946

Browse files
committed
cleanup !single_line_comment
1 parent 08140c5 commit 0383946

File tree

4 files changed

+121
-78
lines changed

4 files changed

+121
-78
lines changed
Lines changed: 80 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
11
/*
22
* Multiline comment
3+
*/
4+
// Empty comments, or comments with whitespaces:
5+
//
6+
//
7+
//
8+
//a
9+
/*inlinecommentwithnospaces*/
10+
/* This should be kept as a multiline comment */
11+
/*
12+
13+
314
*/
415
/*
516
* Multiline comment with a // single line comment
617
*/
718
// Single line comment
8-
// Single line comment with a multiline /* starting
919
let testPostComment = "";
1020

1121
// let commentedCode = "";
12-
// Test inter-code comments
22+
// Test: inter-code comments
1323
let testMultiline a =>
1424
switch a {
1525
// single line comment
1626
| `Thingy x => {
1727
print_string
18-
// multiline comment should be fine
28+
/* multiline comment should be fine */
1929
"matched thingy x";
2030
let zz = 10;
2131
// post line single line comment
@@ -29,22 +39,24 @@ let testMultiline a =>
2939
};
3040

3141
// single line comment below
32-
// short comment
42+
/* short comment */
3343
let x = ["test"];
3444

35-
// short comment
45+
/* short comment */
3646
let x = {
3747
// /* */
3848
let y = "";
3949
()
4050
};
4151

4252
// /* this is a valid nested comment*/ this is a valid comment
43-
// valid /* this is a valid comment
53+
// valid /* this is also a valid nested comment */
4454
let z = 10;
4555

56+
///////////////////////////////////////////////////////////////////////////////////
4657
// The following tests will test the conversion of /* */ to single line
4758
// comments as well as the wrapping of interleaved comments within short sequences.
59+
///////////////////////////////////////////////////////////////////////////////////
4860
/*
4961
* Test wrapping every form of named arguments where various parts are
5062
* commented.
@@ -53,79 +65,116 @@ let a = 10;
5365

5466
let b = 20;
5567

56-
//A
68+
/*A*/
5769
let named /* a::a */ a::a /* b::b */ b::b => /* a + b */ a + b;
5870

59-
//B
60-
let namedAlias /* a::aa */ a::aa /* b::bb */ b::bb => /* aa + bb */ aa + bb;
71+
/*B*/
72+
let namedAlias
73+
/* a::aa */
74+
a::aa
75+
/* b::bb */
76+
b::bb => /* aa + bb */ aa + bb;
6177

62-
//C
78+
/*C*/
6379
let namedAnnot
6480
/* a::(a: option int) */
6581
a::(a: option int)
6682
/* b::(b: option int) */
6783
b::(b: option int) =>
68-
// 20
84+
/* 20 */
6985
20;
7086

71-
//D
87+
/*D*/
7288
let namedAliasAnnot
7389
/* a::(aa: option int) */
7490
a::(aa: option int)
7591
/* b::(bb: option int) */
7692
b::(bb: option int) =>
77-
// 20
93+
/* 20 */
7894
20;
7995

80-
//E
81-
let optional /* a::a=? */ a::a=? /* b::b=? */ b::b=? /* () */ () =>
82-
// 10
96+
/*E*/
97+
let optional
98+
/* a::a=? */
99+
a::a=?
100+
/* b::b=? */
101+
b::b=?
102+
/* () */
103+
() =>
104+
/* 10 */
83105
10;
84106

85-
//F
86-
let optionalAlias /* a::aa */ a::aa=? /* ?b:bb */ b::bb=? /* () */ () =>
87-
// 10
107+
/*F*/
108+
let optionalAlias
109+
/* a::aa */
110+
a::aa=?
111+
/* ?b:bb */
112+
b::bb=?
113+
/* () */
114+
() =>
115+
/* 10 */
88116
10;
89117

90-
//G
118+
/*G*/
91119
let optionalAnnot
92120
/* a::(a: option int)=? */
93121
a::(a: option int)=?
94122
/* ?b:(b: option int) */
95123
b::(b: option int)=?
96124
/* () */
97125
() =>
98-
// 10
126+
/* 10 */
99127
10;
100128

101-
//H
129+
/*H*/
102130
let optionalAliasAnnot
103131
/* a::(aa: option int)=? */
104132
a::(aa: option int)=?
105133
/* b::(bb: option int)=? */
106134
b::(bb: option int)=?
107135
/* () => */
108136
() =>
109-
// 10
137+
/* 10 */
110138
10;
111139

112-
//I: This one is really annoying? Where's the visual label?
113-
let defOptional /* a::a=10 */ a::a=10 /* b::b=10 */ b::b=10 /* () => */ () =>
114-
// 10
140+
/*I: This one is really annoying? Where's the visual label?*/
141+
let defOptional
142+
/* a::a=10 */
143+
a::a=10
144+
/* b::b=10 */
145+
b::b=10
146+
/* () => */
147+
() =>
148+
/* 10 */
115149
10;
116150

117-
//J
118-
let defOptionalAlias /* a::aa=10 */ a::aa=10 /* b::bb=10 */ b::bb=10 /* () => */ () =>
119-
// 10;
151+
/*J*/
152+
let defOptionalAlias
153+
/* a::aa=10 */
154+
a::aa=10
155+
/* b::bb=10 */
156+
b::bb=10
157+
/* () => */
158+
() =>
159+
/* 10; */
120160
10;
121161

122-
//K
162+
/*K*/
123163
let defOptionalAnnot
124164
/* a::(a:int)=10 */
125165
a::(a: int)=10
126166
/* b::(b:int)=10 */
127167
b::(b: int)=10
128168
/* () => */
129169
() =>
130-
// 10;
170+
/* 10; */
131171
10;
172+
173+
// This tests a short inline comment that should retain it's inline properties when formatted
174+
let x = [/* sh */ "te"];
175+
176+
// This tests an empty /* */ nested comment
177+
// This tests a line comment that should be converted to an inline comment when formatted
178+
let x = [/* sh*/ "te"];
179+
180+
// File ends with a comment

formatTest/unit_tests/input/comments.re

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
//a
1010
/*inlinecommentwithnospaces*/
1111

12+
/* This should be kept as a multiline comment */
1213
/*
1314
1415

src/reason_lexer.mll

Lines changed: 34 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,7 @@ let get_stored_string () =
166166
(* To store the position of the beginning of a string and comment *)
167167
let string_start_loc = ref Location.none;;
168168
let comment_start_loc = ref [];;
169-
let line_comment_start_loc = ref [];;
170-
let single_line_comment = ref false;;
169+
let line_comment_start_loc = ref None;;
171170
let in_comment () = !comment_start_loc <> [];;
172171
let is_in_string = ref false
173172
let in_string () = !is_in_string
@@ -424,8 +423,7 @@ rule token = parse
424423
}
425424
| "//"
426425
{ let start_loc = Location.curr lexbuf in
427-
single_line_comment := true;
428-
line_comment_start_loc := [start_loc];
426+
line_comment_start_loc := Some start_loc;
429427
reset_string_buffer ();
430428
let end_loc = comment lexbuf in
431429
let s = get_stored_string () in
@@ -569,23 +567,24 @@ and comment = parse
569567
}
570568
| "*/"
571569
{
572-
match (!comment_start_loc, !single_line_comment) with
573-
| ([], false) ->
570+
match (!comment_start_loc, !line_comment_start_loc) with
571+
| ([], None) ->
574572
assert false
575-
| ([], true) ->
576-
let loc = Location.curr lexbuf in
577-
let start = List.hd (List.rev !line_comment_start_loc) in
578-
raise (Error (Unmatched_nested_comment loc, start))
579-
| ([_], true) ->
580-
comment_start_loc := [];
581-
store_lexeme lexbuf;
582-
comment lexbuf;
583-
| ([_], false) ->
573+
| ([_], None) ->
584574
comment_start_loc := []; Location.curr lexbuf
585-
| (_ :: l, _) ->
575+
| (_ :: l, None) ->
586576
comment_start_loc := l;
587577
store_lexeme lexbuf;
588578
comment lexbuf;
579+
(* line comment *)
580+
| ([], Some start_loc) ->
581+
let loc = Location.curr lexbuf in
582+
raise (Error (Unmatched_nested_comment loc, start_loc))
583+
(* a multiline comment nested within a line comment *)
584+
| (_, Some _) ->
585+
comment_start_loc := [];
586+
store_lexeme lexbuf;
587+
comment lexbuf;
589588
}
590589
| "\""
591590
{
@@ -644,48 +643,39 @@ and comment = parse
644643
| "'\\" 'x' ['0'-'9' 'a'-'f' 'A'-'F'] ['0'-'9' 'a'-'f' 'A'-'F'] "'"
645644
{ store_lexeme lexbuf; comment lexbuf }
646645
| eof
647-
{ match !comment_start_loc with
648-
| [] ->
649-
(* if the file ends with a single line comment then *)
650-
if !single_line_comment then (
651-
single_line_comment := false;
652-
match (!comment_start_loc, !line_comment_start_loc) with
653-
| ([], []) ->
654-
assert false
655-
| ([], _) ->
656-
line_comment_start_loc := []; Location.curr lexbuf
657-
| (_, _) ->
658-
let start = List.hd (List.rev !comment_start_loc) in
659-
comment_start_loc := [];
660-
raise (Error (Unmatched_nested_comment start, Location.curr lexbuf))
661-
) else assert false
662-
| loc :: _ ->
646+
{ match (!comment_start_loc, !line_comment_start_loc) with
647+
| ([], None) -> assert false
648+
| (loc :: _, None) ->
663649
let start = List.hd (List.rev !comment_start_loc) in
664650
comment_start_loc := [];
665651
raise (Error (Unterminated_comment start, loc))
652+
(* line comment ended with no nested multilines *)
653+
| ([], Some _) ->
654+
line_comment_start_loc := None; Location.curr lexbuf
655+
(* line comment ends with an unfinished multiline *)
656+
| (_, Some _) ->
657+
let start = List.hd (List.rev !comment_start_loc) in
658+
comment_start_loc := [];
659+
raise (Error (Unmatched_nested_comment start, Location.curr lexbuf))
666660
}
667661
| newline
668662
{
669-
if not !single_line_comment then (
670-
update_loc lexbuf None 1 false 0;
671-
store_lexeme lexbuf;
672-
comment lexbuf
673-
)
674-
else (
675663
update_loc lexbuf None 1 false 0;
676664
(* check if there are any unmatched nested comments *)
677665

678-
single_line_comment := false;
679666
match (!comment_start_loc, !line_comment_start_loc) with
680-
| ([], []) ->
667+
| ([], None) ->
681668
assert false
682-
| ([], _) ->
683-
line_comment_start_loc := []; Location.curr lexbuf
684-
| (_, _) ->
669+
| (_, None) ->
670+
update_loc lexbuf None 1 false 0;
671+
store_lexeme lexbuf;
672+
comment lexbuf
673+
| ([], Some _) ->
674+
line_comment_start_loc := None; Location.curr lexbuf
675+
| (_, Some _) ->
685676
let start = List.hd (List.rev !comment_start_loc) in
686677
comment_start_loc := [];
687678
raise (Error (Unmatched_nested_comment start, Location.curr lexbuf))
688-
)
689679
}
690680
| _
691681
{ store_lexeme lexbuf; comment lexbuf }

src/reason_toolchain.ml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,12 @@ module Create_parse_entrypoint (Toolchain_impl: Toolchain_spec) :Toolchain = str
129129
| _ ->
130130
let modified_comments =
131131
List.map (fun (str, is_line_comment, loc) ->
132-
let comment_length = (loc.loc_end.pos_cnum - loc.loc_start.pos_cnum - 4) in
133-
let original_comment_contents = String.sub !chan_input (loc.loc_start.pos_cnum + 2) comment_length in
134-
(original_comment_contents, is_line_comment, loc)
132+
if not is_line_comment then
133+
let comment_length = (loc.loc_end.pos_cnum - loc.loc_start.pos_cnum - 4) in
134+
let original_comment_contents = String.sub !chan_input (loc.loc_start.pos_cnum + 2) comment_length in
135+
(original_comment_contents, is_line_comment, loc)
136+
else
137+
(str, is_line_comment, loc)
135138
)
136139
unmodified_comments
137140
in

0 commit comments

Comments
 (0)