From b8ae2de5be242bbd1e34ec974627c81f99b16d23 Mon Sep 17 00:00:00 2001 From: Hugo Herbelin Date: Sat, 8 Oct 2016 22:51:09 +0200 Subject: Moving Pp.comments to CLexer so that Pp is purer (no more side-effect done by the Ppcmd_comment token) and so that lexing/parsing side-effects are collected at the same place, i.e. in CLexer. --- parsing/cLexer.ml4 | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'parsing/cLexer.ml4') diff --git a/parsing/cLexer.ml4 b/parsing/cLexer.ml4 index 777cdc9bf..181c4b7fd 100644 --- a/parsing/cLexer.ml4 +++ b/parsing/cLexer.ml4 @@ -340,18 +340,29 @@ let comm_loc bp = match !comment_begin with | None -> comment_begin := Some bp | _ -> () +let comments = ref [] let current_comment = Buffer.create 8192 let between_commands = ref true +let rec split_comments comacc acc pos = function + [] -> comments := List.rev acc; comacc + | ((b,e),c as com)::coms -> + (* Take all comments that terminates before pos, or begin exactly + at pos (used to print comments attached after an expression) *) + if e<=pos || pos=b then split_comments (c::comacc) acc pos coms + else split_comments comacc (com::acc) pos coms + +let extract_comments pos = split_comments [] [] pos !comments + type comments_state = int option * string * bool * ((int * int) * string) list let restore_comments_state (o,s,b,c) = comment_begin := o; Buffer.clear current_comment; Buffer.add_string current_comment s; between_commands := b; - Pp.comments := c + comments := c let default_comments_state = (None,"",true,[]) let comments_state () = - let s = (!comment_begin, Buffer.contents current_comment, !between_commands, !Pp.comments) in + let s = (!comment_begin, Buffer.contents current_comment, !between_commands, !comments) in restore_comments_state default_comments_state; s let real_push_char c = Buffer.add_char current_comment c @@ -390,7 +401,7 @@ let comment_stop ep = ++ str current_s ++str"' ending at " ++ int ep); ep-1 in - Pp.comments := ((bp,ep),current_s) :: !Pp.comments); + comments := ((bp,ep),current_s) :: !comments); Buffer.clear current_comment; comment_begin := None; between_commands := false -- cgit v1.2.3