@@ -167,8 +167,7 @@ let get_stored_string () =
167167(* To store the position of the beginning of a string and comment *)
168168let string_start_loc = ref Location. none;;
169169let 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 ;;
172171let in_comment () = ! comment_start_loc <> [] ;;
173172let is_in_string = ref false
174173let 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 }
0 commit comments