| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open Util |
| 12 |
open Xml_datatype |
| 13 |
|
| 14 |
type 'annotation located = { |
| 15 |
annotation : 'annotation option; |
| 16 |
startpos : int; |
| 17 |
endpos : int |
| 18 |
} |
| 19 |
|
| 20 |
type 'a stack = |
| 21 |
| Leaf |
| 22 |
| Node of string * 'a located gxml list * int * 'a stack |
| 23 |
|
| 24 |
type 'a context = { |
| 25 |
mutable stack : 'a stack; |
| 26 |
|
| 27 |
mutable offset : int; |
| 28 |
|
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
let rich_pp ~width ?depth ppcmds = |
| 40 |
|
| 41 |
let context = { |
| 42 |
stack = Leaf; |
| 43 |
offset = 0; |
| 44 |
} in |
| 45 |
|
| 46 |
let pp_buffer = Buffer.create 180 in |
| 47 |
|
| 48 |
let push_pcdata () = |
| 49 |
|
| 50 |
let len = Buffer.length pp_buffer in |
| 51 |
if len = 0 then () |
| 52 |
else match context.stack with |
| 53 |
| Leaf -> assert false |
| 54 |
| Node (node, child, pos, ctx) -> |
| 55 |
let data = Buffer.contents pp_buffer in |
| 56 |
let () = Buffer.clear pp_buffer in |
| 57 |
let () = context.stack <- Node (node, PCData data :: child, pos, ctx) in |
| 58 |
context.offset <- context.offset + len |
| 59 |
in |
| 60 |
|
| 61 |
let open_xml_tag = function |
| 62 |
| Format.String_tag tag -> |
| 63 |
let () = push_pcdata () in |
| 64 |
context.stack <- Node (tag, [], context.offset, context.stack) |
| 65 |
| _ -> () |
| 66 |
in |
| 67 |
|
| 68 |
let close_xml_tag = function |
| 69 |
| Format.String_tag tag -> |
| 70 |
let () = push_pcdata () in |
| 71 |
(match context.stack with |
| 72 |
| Leaf -> assert false |
| 73 |
| Node (node, child, pos, ctx) -> |
| 74 |
let () = assert (String.equal tag node) in |
| 75 |
let annotation = { |
| 76 |
annotation = Some tag; |
| 77 |
startpos = pos; |
| 78 |
endpos = context.offset; |
| 79 |
} in |
| 80 |
let xml = Element (node, annotation, List.rev child) in |
| 81 |
match ctx with |
| 82 |
| Leaf -> |
| 83 |
|
| 84 |
context.stack <- Node ("", [xml], 0, Leaf) |
| 85 |
| Node (node, child, pos, ctx) -> |
| 86 |
context.stack <- Node (node, xml :: child, pos, ctx)) |
| 87 |
| _ -> () |
| 88 |
in |
| 89 |
|
| 90 |
let open Format in |
| 91 |
|
| 92 |
let ft = formatter_of_buffer pp_buffer in |
| 93 |
|
| 94 |
let tag_functions = { |
| 95 |
mark_open_stag = (fun tag -> let () = open_xml_tag tag in ""); |
| 96 |
mark_close_stag = (fun tag -> let () = close_xml_tag tag in ""); |
| 97 |
print_open_stag = ignore; |
| 98 |
print_close_stag = ignore; |
| 99 |
} in |
| 100 |
|
| 101 |
pp_set_formatter_stag_functions ft tag_functions; |
| 102 |
pp_set_mark_tags ft true; |
| 103 |
|
| 104 |
|
| 105 |
pp_set_margin ft width; |
| 106 |
Option.iter (pp_set_max_boxes ft) depth; |
| 107 |
let m = max (64 * width / 100) (width-30) in |
| 108 |
pp_set_max_indent ft m; |
| 109 |
pp_set_max_boxes ft 50 ; |
| 110 |
pp_set_ellipsis_text ft "..."; |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
pp_open_box ft 0; |
| 115 |
pp_open_stag ft (String_tag "pp"); |
| 116 |
Pp.(pp_with ft ppcmds); |
| 117 |
pp_close_stag ft (); |
| 118 |
pp_close_box ft (); |
| 119 |
|
| 120 |
|
| 121 |
let () = pp_print_flush ft () in |
| 122 |
let () = assert (Buffer.length pp_buffer = 0) in |
| 123 |
match context.stack with |
| 124 |
| Node ("", [xml], 0, Leaf) -> xml |
| 125 |
| _ -> assert false |
| 126 |
|
| 127 |
|
| 128 |
let annotations_positions xml = |
| 129 |
let rec node accu = function |
| 130 |
| Element (_, { annotation = Some annotation; startpos; endpos }, cs) -> |
| 131 |
children ((annotation, (startpos, endpos)) :: accu) cs |
| 132 |
| Element (_, _, cs) -> |
| 133 |
children accu cs |
| 134 |
| _ -> |
| 135 |
accu |
| 136 |
and children accu cs = |
| 137 |
List.fold_left node accu cs |
| 138 |
in |
| 139 |
node [] xml |
| 140 |
|
| 141 |
let xml_of_rich_pp tag_of_annotation attributes_of_annotation xml = |
| 142 |
let rec node = function |
| 143 |
| Element (index, { annotation; startpos; endpos }, cs) -> |
| 144 |
let attributes = |
| 145 |
[ "startpos", string_of_int startpos; |
| 146 |
"endpos", string_of_int endpos |
| 147 |
] |
| 148 |
@ (match annotation with |
| 149 |
| None -> [] |
| 150 |
| Some annotation -> attributes_of_annotation annotation |
| 151 |
) |
| 152 |
in |
| 153 |
let tag = |
| 154 |
match annotation with |
| 155 |
| None -> index |
| 156 |
| Some annotation -> tag_of_annotation annotation |
| 157 |
in |
| 158 |
Element (tag, attributes, List.map node cs) |
| 159 |
| PCData s -> |
| 160 |
PCData s |
| 161 |
in |
| 162 |
node xml |
| 163 |
|
| 164 |
type richpp = xml |
| 165 |
|
| 166 |
let richpp_of_pp ~width ?depth pp = |
| 167 |
let rec drop = function |
| 168 |
| PCData s -> [PCData s] |
| 169 |
| Element (_, annotation, cs) -> |
| 170 |
let cs = List.concat (List.map drop cs) in |
| 171 |
match annotation.annotation with |
| 172 |
| None -> cs |
| 173 |
| Some s -> [Element (s, [], cs)] |
| 174 |
in |
| 175 |
let xml = rich_pp ~width ?depth pp in |
| 176 |
Element ("_", [], drop xml) |