Debian Codegraph @

ide/rocqide/protocol/xml_lexer.mll

raw
1 {(*
2 * Xml Light, an small Xml parser/printer with DTD support.
3 * Copyright (C) 2003 Nicolas Cannasse ([email protected])
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *)
19
20 open Lexing
21
22 type error =
23 | EUnterminatedComment
24 | EUnterminatedString
25 | EIdentExpected
26 | ECloseExpected
27 | ENodeExpected
28 | EAttributeNameExpected
29 | EAttributeValueExpected
30 | EUnterminatedEntity
31
32 exception Error of error
33
34 type pos = int * int * int * int
35
36 type token =
37 | Tag of string * (string * string) list * bool
38 | PCData of string
39 | Endtag of string
40 | Eof
41
42 let last_pos = ref 0
43 and current_line = ref 0
44 and current_line_start = ref 0
45
46 let tmp = Buffer.create 200
47
48 let idents = Hashtbl.create 0
49
50 let _ = begin
51 Hashtbl.add idents "nbsp;" " ";
52 Hashtbl.add idents "gt;" ">";
53 Hashtbl.add idents "lt;" "<";
54 Hashtbl.add idents "amp;" "&";
55 Hashtbl.add idents "apos;" "'";
56 Hashtbl.add idents "quot;" "\"";
57 end
58
59 let init lexbuf =
60 current_line := 1;
61 current_line_start := lexeme_start lexbuf;
62 last_pos := !current_line_start
63
64 let close lexbuf =
65 Buffer.reset tmp
66
67 let pos lexbuf =
68 !current_line , !current_line_start ,
69 !last_pos ,
70 lexeme_start lexbuf
71
72 let restore (cl,cls,lp,_) =
73 current_line := cl;
74 current_line_start := cls;
75 last_pos := lp
76
77 let newline lexbuf =
78 incr current_line;
79 last_pos := lexeme_end lexbuf;
80 current_line_start := !last_pos
81
82 let error lexbuf e =
83 last_pos := lexeme_start lexbuf;
84 raise (Error e)
85
86 }
87
88 let newline = ['\n']
89 let break = ['\r']
90 let space = [' ' '\t']
91 let identchar = ['A'-'Z' 'a'-'z' '_' '0'-'9' ':' '-' '.']
92 let ident = ['A'-'Z' 'a'-'z' '_' ':'] identchar*
93 let entitychar = ['A'-'Z' 'a'-'z']
94 let pcchar = [^ '\r' '\n' '<' '>' '&']
95
96 rule token = parse
97 | break
98 {
99 PCData "\r"
100 }
101 | newline
102 {
103 newline lexbuf;
104 PCData "\n"
105 }
106 | "<!--"
107 {
108 last_pos := lexeme_start lexbuf;
109 comment lexbuf;
110 token lexbuf
111 }
112 | "<?"
113 {
114 last_pos := lexeme_start lexbuf;
115 header lexbuf;
116 token lexbuf;
117 }
118 | '<' space* '/' space*
119 {
120 last_pos := lexeme_start lexbuf;
121 let tag = ident_name lexbuf in
122 ignore_spaces lexbuf;
123 close_tag lexbuf;
124 Endtag tag
125 }
126 | '<' space*
127 {
128 last_pos := lexeme_start lexbuf;
129 let tag = ident_name lexbuf in
130 ignore_spaces lexbuf;
131 let attribs, closed = attributes lexbuf in
132 Tag(tag, attribs, closed)
133 }
134 | "&#"
135 {
136 last_pos := lexeme_start lexbuf;
137 Buffer.reset tmp;
138 Buffer.add_string tmp (lexeme lexbuf);
139 PCData (pcdata lexbuf)
140 }
141 | '&'
142 {
143 last_pos := lexeme_start lexbuf;
144 Buffer.reset tmp;
145 Buffer.add_string tmp (entity lexbuf);
146 PCData (pcdata lexbuf)
147 }
148 | pcchar+
149 {
150 last_pos := lexeme_start lexbuf;
151 Buffer.reset tmp;
152 Buffer.add_string tmp (lexeme lexbuf);
153 PCData (pcdata lexbuf)
154 }
155 | eof { Eof }
156 | _
157 { error lexbuf ENodeExpected }
158
159 and ignore_spaces = parse
160 | break
161 {
162 ignore_spaces lexbuf
163 }
164 | newline
165 {
166 newline lexbuf;
167 ignore_spaces lexbuf
168 }
169 | space +
170 { ignore_spaces lexbuf }
171 | ""
172 { () }
173
174 and comment = parse
175 | break
176 {
177 comment lexbuf
178 }
179 | newline
180 {
181 newline lexbuf;
182 comment lexbuf
183 }
184 | "-->"
185 { () }
186 | eof
187 { raise (Error EUnterminatedComment) }
188 | _
189 { comment lexbuf }
190
191 and header = parse
192 | break
193 {
194 header lexbuf
195 }
196 | newline
197 {
198 newline lexbuf;
199 header lexbuf
200 }
201 | "?>"
202 { () }
203 | eof
204 { error lexbuf ECloseExpected }
205 | _
206 { header lexbuf }
207
208 and pcdata = parse
209 | break
210 {
211 Buffer.add_char tmp '\r';
212 pcdata lexbuf
213 }
214 | newline
215 {
216 Buffer.add_char tmp '\n';
217 newline lexbuf;
218 pcdata lexbuf
219 }
220 | pcchar+
221 {
222 Buffer.add_string tmp (lexeme lexbuf);
223 pcdata lexbuf
224 }
225 | "&#"
226 {
227 Buffer.add_string tmp (lexeme lexbuf);
228 pcdata lexbuf;
229 }
230 | '&'
231 {
232 Buffer.add_string tmp (entity lexbuf);
233 pcdata lexbuf
234 }
235 | ""
236 { Buffer.contents tmp }
237
238 and entity = parse
239 | entitychar+ ';'
240 {
241 let ident = lexeme lexbuf in
242 try
243 Hashtbl.find idents (String.lowercase_ascii ident)
244 with
245 Not_found -> "&" ^ ident
246 }
247 | _ | eof
248 { raise (Error EUnterminatedEntity) }
249
250 and ident_name = parse
251 | ident
252 { lexeme lexbuf }
253 | _ | eof
254 { error lexbuf EIdentExpected }
255
256 and close_tag = parse
257 | '>'
258 { () }
259 | _ | eof
260 { error lexbuf ECloseExpected }
261
262 and attributes = parse
263 | '>'
264 { [], false }
265 | "/>"
266 { [], true }
267 | "" (* do not read a char ! *)
268 {
269 let key = attribute lexbuf in
270 let data = attribute_data lexbuf in
271 ignore_spaces lexbuf;
272 let others, closed = attributes lexbuf in
273 (key, data) :: others, closed
274 }
275
276 and attribute = parse
277 | ident
278 { lexeme lexbuf }
279 | _ | eof
280 { error lexbuf EAttributeNameExpected }
281
282 and attribute_data = parse
283 | space* '=' space* '"'
284 {
285 Buffer.reset tmp;
286 last_pos := lexeme_end lexbuf;
287 dq_string lexbuf
288 }
289 | space* '=' space* '\''
290 {
291 Buffer.reset tmp;
292 last_pos := lexeme_end lexbuf;
293 q_string lexbuf
294 }
295 | _ | eof
296 { error lexbuf EAttributeValueExpected }
297
298 and dq_string = parse
299 | '"'
300 { Buffer.contents tmp }
301 | '\\' [ '"' '\\' ]
302 {
303 Buffer.add_char tmp (lexeme_char lexbuf 1);
304 dq_string lexbuf
305 }
306 | '&'
307 {
308 Buffer.add_string tmp (entity lexbuf);
309 dq_string lexbuf
310 }
311 | eof
312 { raise (Error EUnterminatedString) }
313 | _
314 {
315 Buffer.add_char tmp (lexeme_char lexbuf 0);
316 dq_string lexbuf
317 }
318
319 and q_string = parse
320 | '\''
321 { Buffer.contents tmp }
322 | '\\' [ '\'' '\\' ]
323 {
324 Buffer.add_char tmp (lexeme_char lexbuf 1);
325 q_string lexbuf
326 }
327 | '&'
328 {
329 Buffer.add_string tmp (entity lexbuf);
330 q_string lexbuf
331 }
332 | eof
333 { raise (Error EUnterminatedString) }
334 | _
335 {
336 Buffer.add_char tmp (lexeme_char lexbuf 0);
337 q_string lexbuf
338 }