Debian Codegraph @

ide/rocqide/protocol/xml_parser.ml

raw symbols SCIP
1 (*
2 * Xml Light, an small Xml parser/printer with DTD support.
3 * Copyright (C) 2003 Nicolas Cannasse ([email protected])
4 * Copyright (C) 2003 Jacques Garrigue
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *)
20
21 open Printf
22 open Xml_datatype
23
24 type xml = Xml_datatype.xml
25
26 type error_pos = {
27 eline : int;
28 eline_start : int;
29 emin : int;
30 emax : int;
31 }
32
33 type error_msg =
34 | UnterminatedComment
35 | UnterminatedString
36 | UnterminatedEntity
37 | IdentExpected
38 | CloseExpected
39 | NodeExpected
40 | AttributeNameExpected
41 | AttributeValueExpected
42 | EndOfTagExpected of string
43 | EOFExpected
44 | Empty
45
46 type error = error_msg * error_pos
47
48 exception Error of error
49
50 exception File_not_found of string
51
52 type t = {
53 mutable check_eof : bool;
54 mutable concat_pcdata : bool;
55 source : Lexing.lexbuf;
56 stack : Xml_lexer.token Stack.t;
57 }
58
59 type source =
60 | SChannel of in_channel
61 | SString of string
62 | SLexbuf of Lexing.lexbuf
63
64 exception Internal_error of error_msg
65 exception NoMoreData
66
67 let xml_error = ref (fun _ -> assert false)
68 let file_not_found = ref (fun _ -> assert false)
69
70 let is_blank s =
71 let len = String.length s in
72 let break = ref true in
73 let i = ref 0 in
74 while !break && !i < len do
75 let c = s.[!i] in
76 (* no '\r' because we replaced them in the lexer *)
77 if c = ' ' || c = '\n' || c = '\t' then incr i
78 else break := false
79 done;
80 !i = len
81
82 let _raises e f =
83 xml_error := e;
84 file_not_found := f
85
86 let make source =
87 let source = match source with
88 | SChannel chan -> Lexing.from_channel chan
89 | SString s -> Lexing.from_string s
90 | SLexbuf lexbuf -> lexbuf
91 in
92 let () = Xml_lexer.init source in
93 {
94 check_eof = false;
95 concat_pcdata = true;
96 source = source;
97 stack = Stack.create ();
98 }
99
100 let check_eof p v = p.check_eof <- v
101
102 let pop s =
103 try
104 Stack.pop s.stack
105 with
106 Stack.Empty ->
107 Xml_lexer.token s.source
108
109 let push t s =
110 Stack.push t s.stack
111
112 let canonicalize_elts l =
113 let has_elt = List.exists (function Element _ -> true | _ -> false) l in
114 if has_elt then List.filter (function PCData s -> not (is_blank s) | _ -> true) l
115 else l
116
117 let rec read_xml canonicalize s =
118 let rec read_node s =
119 match pop s with
120 | Xml_lexer.PCData s -> PCData s
121 | Xml_lexer.Tag (tag, attr, true) -> Element (tag, attr, [])
122 | Xml_lexer.Tag (tag, attr, false) ->
123 let elements = read_elems tag s in
124 let elements =
125 if canonicalize then canonicalize_elts elements else elements
126 in
127 Element (tag, attr, elements)
128 | t ->
129 push t s;
130 raise NoMoreData
131
132 and read_elems tag s =
133 let elems = ref [] in
134 (try
135 while true do
136 let node = read_node s in
137 match node, !elems with
138 | PCData c , (PCData c2) :: q ->
139 elems := PCData (c2 ^ c) :: q
140 | _, l ->
141 elems := node :: l
142 done
143 with
144 NoMoreData -> ());
145 match pop s with
146 | Xml_lexer.Endtag s when s = tag -> List.rev !elems
147 | t -> raise (Internal_error (EndOfTagExpected tag))
148 in
149 match read_node s with
150 | (Element _) as node ->
151 node
152 | PCData c ->
153 if is_blank c then
154 read_xml canonicalize s
155 else
156 raise (Xml_lexer.Error Xml_lexer.ENodeExpected)
157
158 let convert = function
159 | Xml_lexer.EUnterminatedComment -> UnterminatedComment
160 | Xml_lexer.EUnterminatedString -> UnterminatedString
161 | Xml_lexer.EIdentExpected -> IdentExpected
162 | Xml_lexer.ECloseExpected -> CloseExpected
163 | Xml_lexer.ENodeExpected -> NodeExpected
164 | Xml_lexer.EAttributeNameExpected -> AttributeNameExpected
165 | Xml_lexer.EAttributeValueExpected -> AttributeValueExpected
166 | Xml_lexer.EUnterminatedEntity -> UnterminatedEntity
167
168 let error_of_exn xparser = function
169 | NoMoreData when pop xparser = Xml_lexer.Eof -> Empty
170 | NoMoreData -> NodeExpected
171 | Internal_error e -> e
172 | Xml_lexer.Error e -> convert e
173 | e ->
174 (*let e = Errors.push e in: We do not record backtrace here. *)
175 raise e
176
177 let do_parse canonicalize xparser =
178 try
179 Xml_lexer.init xparser.source;
180 let x = read_xml canonicalize xparser in
181 if xparser.check_eof && pop xparser <> Xml_lexer.Eof then raise (Internal_error EOFExpected);
182 Xml_lexer.close ();
183 x
184 with any ->
185 Xml_lexer.close ();
186 raise (!xml_error (error_of_exn xparser any) xparser.source)
187
188 let parse ?(canonicalize=true) p =
189 do_parse canonicalize p
190
191 let error_msg = function
192 | UnterminatedComment -> "Unterminated comment"
193 | UnterminatedString -> "Unterminated string"
194 | UnterminatedEntity -> "Unterminated entity"
195 | IdentExpected -> "Ident expected"
196 | CloseExpected -> "Element close expected"
197 | NodeExpected -> "Xml node expected"
198 | AttributeNameExpected -> "Attribute name expected"
199 | AttributeValueExpected -> "Attribute value expected"
200 | EndOfTagExpected tag -> sprintf "End of tag expected : '%s'" tag
201 | EOFExpected -> "End of file expected"
202 | Empty -> "Empty"
203
204 let error (msg,pos) =
205 if pos.emin = pos.emax then
206 sprintf "%s line %d character %d" (error_msg msg) pos.eline
207 (pos.emin - pos.eline_start)
208 else
209 sprintf "%s line %d characters %d-%d" (error_msg msg) pos.eline
210 (pos.emin - pos.eline_start) (pos.emax - pos.eline_start)
211
212 let line e = e.eline
213
214 let range e =
215 e.emin - e.eline_start , e.emax - e.eline_start
216
217 let abs_range e =
218 e.emin , e.emax
219
220 let pos source =
221 let line, lstart, min, max = Xml_lexer.pos source in
222 {
223 eline = line;
224 eline_start = lstart;
225 emin = min;
226 emax = max;
227 }
228
229 let () = _raises (fun x p ->
230 (* local cast : Xml.error_msg -> error_msg *)
231 Error (x, pos p))
232 (fun f -> File_not_found f)