1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
(************************************************************************)
(* v * The Coq Proof Assistant / The Coq Development Team *)
(* <O___,, * INRIA - CNRS - LIX - LRI - PPS - Copyright 1999-2015 *)
(* \VV/ **************************************************************)
(* // * This file is distributed under the terms of the *)
(* * GNU Lesser General Public License Version 2.1 *)
(************************************************************************)
open Util
open Xml_datatype
type 'annotation located = {
annotation : 'annotation option;
startpos : int;
endpos : int
}
type 'a stack =
| Leaf
| Node of string * 'a located gxml list * int * 'a stack
type 'a context = {
mutable stack : 'a stack;
(** Pending opened nodes *)
mutable offset : int;
(** Quantity of characters printed so far *)
mutable annotations : 'a option Int.Map.t;
(** Map associating annotations to indexes *)
mutable index : int;
(** Current index of annotations *)
}
(** We use Format to introduce tags inside the pretty-printed document.
Each inserted tag is a fresh index that we keep in sync with the contents
of annotations.
We build an XML tree on the fly, by plugging ourselves in Format tag
marking functions. As those functions are called when actually writing to
the device, the resulting tree is correct.
*)
let rich_pp annotate ppcmds =
let context = {
stack = Leaf;
offset = 0;
annotations = Int.Map.empty;
index = (-1);
} in
let pp_tag obj =
let index = context.index + 1 in
let () = context.index <- index in
let obj = annotate obj in
let () = context.annotations <- Int.Map.add index obj context.annotations in
string_of_int index
in
let pp_buffer = Buffer.create 13 in
let push_pcdata () =
(** Push the optional PCData on the above node *)
let len = Buffer.length pp_buffer in
if len = 0 then ()
else match context.stack with
| Leaf -> assert false
| Node (node, child, pos, ctx) ->
let data = Buffer.contents pp_buffer in
let () = Buffer.clear pp_buffer in
let () = context.stack <- Node (node, PCData data :: child, pos, ctx) in
context.offset <- context.offset + len
in
let open_xml_tag tag =
let () = push_pcdata () in
context.stack <- Node (tag, [], context.offset, context.stack)
in
let close_xml_tag tag =
let () = push_pcdata () in
match context.stack with
| Leaf -> assert false
| Node (node, child, pos, ctx) ->
let () = assert (String.equal tag node) in
let annotation =
try Int.Map.find (int_of_string node) context.annotations
with _ -> None
in
let annotation = {
annotation = annotation;
startpos = pos;
endpos = context.offset;
} in
let xml = Element (node, annotation, List.rev child) in
match ctx with
| Leaf ->
(** Final node: we keep the result in a dummy context *)
context.stack <- Node ("", [xml], 0, Leaf)
| Node (node, child, pos, ctx) ->
context.stack <- Node (node, xml :: child, pos, ctx)
in
let open Format in
let ft = formatter_of_buffer pp_buffer in
let tag_functions = {
mark_open_tag = (fun tag -> let () = open_xml_tag tag in "");
mark_close_tag = (fun tag -> let () = close_xml_tag tag in "");
print_open_tag = ignore;
print_close_tag = ignore;
} in
pp_set_formatter_tag_functions ft tag_functions;
pp_set_mark_tags ft true;
(** The whole output must be a valid document. To that
end, we nest the document inside <pp> tags. *)
pp_open_tag ft "pp";
Pp.(pp_with ~pp_tag ft ppcmds);
pp_close_tag ft ();
(** Get the resulting XML tree. *)
let () = pp_print_flush ft () in
let () = assert (Buffer.length pp_buffer = 0) in
match context.stack with
| Node ("", [xml], 0, Leaf) -> xml
| _ -> assert false
let annotations_positions xml =
let rec node accu = function
| Element (_, { annotation = Some annotation; startpos; endpos }, cs) ->
children ((annotation, (startpos, endpos)) :: accu) cs
| Element (_, _, cs) ->
children accu cs
| _ ->
accu
and children accu cs =
List.fold_left node accu cs
in
node [] xml
let xml_of_rich_pp tag_of_annotation attributes_of_annotation xml =
let rec node = function
| Element (index, { annotation; startpos; endpos }, cs) ->
let attributes =
[ "startpos", string_of_int startpos;
"endpos", string_of_int endpos
]
@ (match annotation with
| None -> []
| Some annotation -> attributes_of_annotation annotation
)
in
let tag =
match annotation with
| None -> index
| Some annotation -> tag_of_annotation annotation
in
Element (tag, attributes, List.map node cs)
| PCData s ->
PCData s
in
node xml
type richpp = xml
let repr xml = xml
let richpp_of_xml xml = xml
let richpp_of_string s = PCData s
let richpp_of_pp pp =
let annotate t = match Pp.Tag.prj t Ppstyle.tag with
| None -> None
| Some key -> Some (Ppstyle.repr key)
in
let rec drop = function
| PCData s -> [PCData s]
| Element (_, annotation, cs) ->
let cs = List.concat (List.map drop cs) in
match annotation.annotation with
| None -> cs
| Some s -> [Element (String.concat "." s, [], cs)]
in
let xml = rich_pp annotate pp in
Element ("_", [], drop xml)
let raw_print xml =
let buf = Buffer.create 1024 in
let rec print = function
| PCData s -> Buffer.add_string buf s
| Element (_, _, cs) -> List.iter print cs
in
let () = print xml in
Buffer.contents buf
let of_richpp x = Element ("richpp", [], [x])
let to_richpp xml = match xml with
| Element ("richpp", [], [x]) -> x
| _ -> raise Serialize.Marshal_error
|