Skip to content

Commit b235362

Browse files
committed
cleanup !single_line_comment, fix unmatched nested comment edge case
1 parent 35f1604 commit b235362

File tree

2 files changed

+61
-68
lines changed

2 files changed

+61
-68
lines changed

src/reason_lexer.mll

Lines changed: 33 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,7 @@ let get_stored_string () =
167167
(* To store the position of the beginning of a string and comment *)
168168
let string_start_loc = ref Location.none;;
169169
let comment_start_loc = ref [];;
170-
let line_comment_start_loc = ref [];;
171-
let single_line_comment = ref false;;
170+
let line_comment_start_loc = ref None;;
172171
let in_comment () = !comment_start_loc <> [];;
173172
let is_in_string = ref false
174173
let in_string () = !is_in_string
@@ -425,8 +424,7 @@ rule token = parse
425424
}
426425
| "//"
427426
{ let start_loc = Location.curr lexbuf in
428-
single_line_comment := true;
429-
line_comment_start_loc := [start_loc];
427+
line_comment_start_loc := Some start_loc;
430428
reset_string_buffer ();
431429
let end_loc = comment lexbuf in
432430
let s = get_stored_string () in
@@ -570,20 +568,21 @@ and comment = parse
570568
}
571569
| "*/"
572570
{
573-
match (!comment_start_loc, !single_line_comment) with
574-
| ([], false) ->
571+
match (!comment_start_loc, !line_comment_start_loc) with
572+
| ([], None) ->
575573
assert false
576-
| ([], true) ->
577-
let loc = Location.curr lexbuf in
578-
let start = List.hd (List.rev !line_comment_start_loc) in
579-
raise (Error (Unmatched_nested_comment loc, start))
580-
| ([_], true) ->
581-
comment_start_loc := [];
574+
| ([_], None) ->
575+
comment_start_loc := []; Location.curr lexbuf
576+
| (_ :: l, None) ->
577+
comment_start_loc := l;
582578
store_lexeme lexbuf;
583579
comment lexbuf;
584-
| ([_], false) ->
585-
comment_start_loc := []; Location.curr lexbuf
586-
| (_ :: l, _) ->
580+
(* line comment *)
581+
| ([], Some start_loc) ->
582+
let loc = Location.curr lexbuf in
583+
raise (Error (Unmatched_nested_comment loc, start_loc))
584+
(* a multiline comment nested within a line comment *)
585+
| (_ :: l, Some _) ->
587586
comment_start_loc := l;
588587
store_lexeme lexbuf;
589588
comment lexbuf;
@@ -645,48 +644,39 @@ and comment = parse
645644
| "'\\" 'x' ['0'-'9' 'a'-'f' 'A'-'F'] ['0'-'9' 'a'-'f' 'A'-'F'] "'"
646645
{ store_lexeme lexbuf; comment lexbuf }
647646
| eof
648-
{ match !comment_start_loc with
649-
| [] ->
650-
(* if the file ends with a single line comment then *)
651-
if !single_line_comment then (
652-
single_line_comment := false;
653-
match (!comment_start_loc, !line_comment_start_loc) with
654-
| ([], []) ->
655-
assert false
656-
| ([], _) ->
657-
line_comment_start_loc := []; Location.curr lexbuf
658-
| (_, _) ->
659-
let start = List.hd (List.rev !comment_start_loc) in
660-
comment_start_loc := [];
661-
raise (Error (Unmatched_nested_comment start, Location.curr lexbuf))
662-
) else assert false
663-
| loc :: _ ->
647+
{ match (!comment_start_loc, !line_comment_start_loc) with
648+
| ([], None) -> assert false
649+
| (loc :: _, None) ->
664650
let start = List.hd (List.rev !comment_start_loc) in
665651
comment_start_loc := [];
666652
raise (Error (Unterminated_comment start, loc))
653+
(* line comment ended with no nested multilines *)
654+
| ([], Some _) ->
655+
line_comment_start_loc := None; Location.curr lexbuf
656+
(* line comment ends with an unfinished multiline *)
657+
| (_, Some _) ->
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))
667661
}
668662
| newline
669663
{
670-
if not !single_line_comment then (
671-
update_loc lexbuf None 1 false 0;
672-
store_lexeme lexbuf;
673-
comment lexbuf
674-
)
675-
else (
676664
update_loc lexbuf None 1 false 0;
677665
(* check if there are any unmatched nested comments *)
678666

679-
single_line_comment := false;
680667
match (!comment_start_loc, !line_comment_start_loc) with
681-
| ([], []) ->
668+
| ([], None) ->
682669
assert false
683-
| ([], _) ->
684-
line_comment_start_loc := []; Location.curr lexbuf
685-
| (_, _) ->
670+
| (_, None) ->
671+
update_loc lexbuf None 1 false 0;
672+
store_lexeme lexbuf;
673+
comment lexbuf
674+
| ([], Some _) ->
675+
line_comment_start_loc := None; Location.curr lexbuf
676+
| (_, Some _) ->
686677
let start = List.hd (List.rev !comment_start_loc) in
687678
comment_start_loc := [];
688679
raise (Error (Unmatched_nested_comment start, Location.curr lexbuf))
689-
)
690680
}
691681
| _
692682
{ store_lexeme lexbuf; comment lexbuf }

src/reason_toolchain.ml

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -188,31 +188,34 @@ module Create_parse_entrypoint (Toolchain_impl: Toolchain_spec) :Toolchain = str
188188
| _ ->
189189
let modified_and_attached_comments =
190190
List.map (fun (str, is_line_comment, physical_loc) ->
191-
(* When searching for "^" regexp, returns location of newline + 1 *)
192-
let first_char_of_line = Str.search_backward new_line !chan_input physical_loc.loc_start.pos_cnum in
193-
let end_pos_plus_one = physical_loc.loc_end.pos_cnum in
194-
let comment_length = (end_pos_plus_one - physical_loc.loc_start.pos_cnum - 4) in
195-
(* Also, the string contents originally reported are incorrect! *)
196-
let original_comment_contents = String.sub !chan_input (physical_loc.loc_start.pos_cnum + 2) comment_length in
197-
let (com, is_line_comment, attachment_location) =
198-
match Str.search_forward line_content !chan_input first_char_of_line
199-
with
200-
| n ->
201-
(* Recall that all end positions are actually the position of end + 1. *)
202-
let one_greater_than_comment_end = end_pos_plus_one in
203-
(* Str.string_match lets you specify a position one greater than last position *)
204-
let comment_is_last_thing_on_line =
205-
Str.string_match space_before_newline !chan_input one_greater_than_comment_end in
206-
if n < physical_loc.loc_start.pos_cnum && comment_is_last_thing_on_line then (
207-
original_comment_contents,
208-
is_line_comment,
209-
{physical_loc with loc_start = {physical_loc.loc_start with pos_cnum = n}}
210-
)
211-
else
212-
(original_comment_contents, is_line_comment, physical_loc)
213-
| exception Not_found -> (original_comment_contents, is_line_comment, physical_loc)
214-
in
215-
(com, is_line_comment, attachment_location, physical_loc)
191+
if not is_line_comment then
192+
(* When searching for "^" regexp, returns location of newline + 1 *)
193+
let first_char_of_line = Str.search_backward new_line !chan_input physical_loc.loc_start.pos_cnum in
194+
let end_pos_plus_one = physical_loc.loc_end.pos_cnum in
195+
let comment_length = (end_pos_plus_one - physical_loc.loc_start.pos_cnum - 4) in
196+
(* Also, the string contents originally reported are incorrect! *)
197+
let original_comment_contents = String.sub !chan_input (physical_loc.loc_start.pos_cnum + 2) comment_length in
198+
let (com, is_line_comment, attachment_location) =
199+
match Str.search_forward line_content !chan_input first_char_of_line
200+
with
201+
| n ->
202+
(* Recall that all end positions are actually the position of end + 1. *)
203+
let one_greater_than_comment_end = end_pos_plus_one in
204+
(* Str.string_match lets you specify a position one greater than last position *)
205+
let comment_is_last_thing_on_line =
206+
Str.string_match space_before_newline !chan_input one_greater_than_comment_end in
207+
if n < physical_loc.loc_start.pos_cnum && comment_is_last_thing_on_line then (
208+
original_comment_contents,
209+
is_line_comment,
210+
{physical_loc with loc_start = {physical_loc.loc_start with pos_cnum = n}}
211+
)
212+
else
213+
(original_comment_contents, is_line_comment, physical_loc)
214+
| exception Not_found -> (original_comment_contents, is_line_comment, physical_loc)
215+
in
216+
(com, is_line_comment, attachment_location, physical_loc)
217+
else
218+
(str, is_line_comment, physical_loc, physical_loc)
216219
)
217220
unmodified_comments
218221
in

0 commit comments

Comments
 (0)