aboutsummaryrefslogtreecommitdiffhomepage
path: root/parsing/cLexer.ml4
diff options
context:
space:
mode:
authorGravatar Hugo Herbelin <Hugo.Herbelin@inria.fr>2016-10-08 22:51:09 +0200
committerGravatar Hugo Herbelin <Hugo.Herbelin@inria.fr>2016-10-09 08:13:18 +0200
commitb8ae2de5be242bbd1e34ec974627c81f99b16d23 (patch)
tree6b6c2980e6aac60e53da6097dca7ae93db83d52e /parsing/cLexer.ml4
parent8a8caba36ea6b0fd67e026ee3833d3b5b25af56d (diff)
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.
Diffstat (limited to 'parsing/cLexer.ml4')
-rw-r--r--parsing/cLexer.ml417
1 files changed, 14 insertions, 3 deletions
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