File: xml_lexer.mll

package info (click to toggle)
xml-light 2.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 212 kB
  • ctags: 352
  • sloc: ml: 1,691; makefile: 171
file content (677 lines) | stat: -rw-r--r-- 12,489 bytes parent folder | download
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
{(*
 * Xml Light, an small Xml parser/printer with DTD support.
 * Copyright (C) 2003 Nicolas Cannasse (ncannasse@motion-twin.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library has the special exception on linking described in file
 * README.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301 USA
 *)

open Lexing
open Xml_parser
open Dtd

type error =
	| EUnterminatedComment
	| EUnterminatedString
	| EIdentExpected
	| ECloseExpected
	| ENodeExpected
	| EAttributeNameExpected
	| EAttributeValueExpected
	| EUnterminatedEntity

type dtd_error =
	| EInvalidDTDDecl
	| EInvalidDTDTag
	| EDTDItemExpected
	| EInvalidDTDElement
	| EInvalidDTDAttribute

exception Error of error
exception DTDError of dtd_error

type pos = int * int * int * int

type dtd_decl =
	| DTDFile of string
	| DTDData of dtd

type dtd_item_type =
	| TElement
	| TAttribute

type token =
	| Tag of string * (string * string) list * bool
	| PCData of string
	| Endtag of string
	| DocType of (string * dtd_decl)
	| Eof

let last_pos = ref 0
and current_line = ref 0
and current_line_start = ref 0

let tmp = Buffer.create 200


let entities = [
  "gt"  ,62, ">";
  "lt"  ,60, "<";
  "amp" ,38, "&";
  "apos",39, "'";
  "quot",34, "\"";
]

let idents = Hashtbl.create 0
let _ = begin
  List.iter (fun (str,code,res) ->
      Hashtbl.add idents (str^";") res;
      if code > 0
      then Hashtbl.add idents ("#" ^ string_of_int code ^ ";") res
    ) entities
end

let init lexbuf =
	current_line := 1;
	current_line_start := lexeme_start lexbuf;
	last_pos := !current_line_start

let close lexbuf =
	Buffer.reset tmp

let pos lexbuf =
	!current_line ,	!current_line_start ,
	!last_pos ,
	lexeme_start lexbuf

let restore (cl,cls,lp,_) =
	current_line := cl;
	current_line_start := cls;
	last_pos := lp

let newline lexbuf =
	incr current_line;
	last_pos := lexeme_end lexbuf;
	current_line_start := !last_pos

let error lexbuf e =
	last_pos := lexeme_start lexbuf;
	raise (Error e)

let dtd_error lexbuf e =
	last_pos := lexeme_start lexbuf;
	raise (DTDError e)
}

let newline = ['\n']
let break = ['\r']
let space = [' ' '\t']
let identchar =  ['A'-'Z' 'a'-'z' '_' '0'-'9' ':' '-']
let entitychar = ['A'-'Z' 'a'-'z' '0'-'9']
let pcchar = [^ '\r' '\n' '<' '>' '&']
let cdata_start = ['c''C']['d''D']['a''A']['t''T']['a''A']

rule token = parse
	| newline
		{
			newline lexbuf;
			token lexbuf
		}
	| (space | break) +
		{
			last_pos := lexeme_end lexbuf;
			token lexbuf
		}
	| "<!DOCTYPE"
		{
			last_pos := lexeme_start lexbuf;
			ignore_spaces lexbuf;
			let root = ident_name lexbuf in
			ignore_spaces lexbuf;
			let data = dtd_data lexbuf in
			DocType (root, data)
		}
	| "<![" cdata_start '['
		{
			last_pos := lexeme_start lexbuf;
			Buffer.reset tmp;
			PCData (cdata lexbuf)
		}
	| "<!--"
		{
			last_pos := lexeme_start lexbuf;
			comment lexbuf;
			token lexbuf
		}
	| "<?"
		{
			last_pos := lexeme_start lexbuf;
			header lexbuf;
			token lexbuf;
		}
	| '<' space* '/' space*
		{
			last_pos := lexeme_start lexbuf;
			let tag = ident_name lexbuf in
			ignore_spaces lexbuf;
			close_tag lexbuf;
			Endtag tag
		}
	| '<' space*
		{
			last_pos := lexeme_start lexbuf;
			let tag = ident_name lexbuf in
			ignore_spaces lexbuf;
			let attribs, closed = attributes lexbuf in
			Tag(tag, attribs, closed)
		}
	| '&'
		{
			last_pos := lexeme_start lexbuf;
			Buffer.reset tmp;
			Buffer.add_string tmp (entity lexbuf);
			PCData (pcdata lexbuf)
		}
	| space* pcchar+
		{
			last_pos := lexeme_start lexbuf;
			Buffer.reset tmp;
			Buffer.add_string tmp (lexeme lexbuf);
			PCData (pcdata lexbuf)
		}
	| eof { Eof }
	| _
		{ error lexbuf ENodeExpected }

and ignore_spaces = parse
	| newline
		{
			newline lexbuf;
			ignore_spaces lexbuf
		}
	| (space | break) +
		{ ignore_spaces lexbuf }
	| ""
		{ () }

and comment = parse
	| newline
		{
			newline lexbuf;
			comment lexbuf
		}
	| "-->"
		{ () }
	| eof
		{ raise (Error EUnterminatedComment) }
	| _
		{ comment lexbuf }

and header = parse
	| newline
		{
			newline lexbuf;
			header lexbuf
		}
	| "?>"
		{ () }
	| eof
		{ error lexbuf ECloseExpected }
	| _
		{ header lexbuf }

and cdata = parse
	| [^ ']' '\n']+
		{
			Buffer.add_string tmp (lexeme lexbuf);
			cdata lexbuf
		}
	| newline
		{
			newline lexbuf;
			Buffer.add_string tmp (lexeme lexbuf);
			cdata lexbuf
		}
	| "]]>"
		{ Buffer.contents tmp }
	| ']'
		{
			Buffer.add_string tmp (lexeme lexbuf);
			cdata lexbuf
		}
	| eof
		{ error lexbuf ECloseExpected }

and pcdata = parse
	| pcchar+
		{
			Buffer.add_string tmp (lexeme lexbuf);
			pcdata lexbuf
		}
	| '&'
		{
			Buffer.add_string tmp (entity lexbuf);
			pcdata lexbuf
		}
	| ""
		{ Buffer.contents tmp }

and entity = parse
	| entitychar+ ';'
		{
			let ident = lexeme lexbuf in
			try
				Hashtbl.find idents (String.lowercase ident)
			with
				Not_found -> "&" ^ ident
		}
  | '#' ['0'-'9']+ ';'
    {
			let ident = lexeme lexbuf in
			try
				Hashtbl.find idents (String.lowercase ident)
			with
				Not_found -> "&" ^ ident
    }
	| _ | eof
		{ raise (Error EUnterminatedEntity) }

and ident_name = parse
	| identchar+
		{ lexeme lexbuf }
	| _ | eof
		{ error lexbuf EIdentExpected }

and close_tag = parse
	| '>'
		{ () }
	| _ | eof
		{ error lexbuf ECloseExpected }

and attributes = parse
	| '>'
		{ [], false }
	| "/>"
		{ [], true }
	| "" (* do not read a char ! *)
		{
			let key = attribute lexbuf in
			let data = attribute_data lexbuf in
			ignore_spaces lexbuf;
			let others, closed = attributes lexbuf in
			(key, data) :: others, closed
		}

and attribute = parse
	| identchar+
		{ lexeme lexbuf }
	| _ | eof
		{ error lexbuf EAttributeNameExpected }

and attribute_data = parse
	| space* '=' space* '"'
		{
			Buffer.reset tmp;
			last_pos := lexeme_end lexbuf;
			dq_string lexbuf
		}
	| space* '=' space* '\''
		{
			Buffer.reset tmp;
			last_pos := lexeme_end lexbuf;
			q_string lexbuf
		}
	| _ | eof
		{ error lexbuf EAttributeValueExpected }

and dq_string = parse
	| '"'
		{ Buffer.contents tmp }
	| '\\' [ '"' '\\' ]
		{
			Buffer.add_char tmp (lexeme_char lexbuf 1);
			dq_string lexbuf
		}
	| eof
		{ raise (Error EUnterminatedString) }
	| _
		{
			Buffer.add_char tmp (lexeme_char lexbuf 0);
			dq_string lexbuf
		}

and q_string = parse
	| '\''
		{ Buffer.contents tmp }
	| '\\' [ '\'' '\\' ]
		{
			Buffer.add_char tmp (lexeme_char lexbuf 1);
			q_string lexbuf
		}
	| eof
		{ raise (Error EUnterminatedString) }
	| _
		{
			Buffer.add_char tmp (lexeme_char lexbuf 0);
			q_string lexbuf
		}

and dtd_data = parse
	| "PUBLIC"
		{
			ignore_spaces lexbuf;
			(* skipping Public ID *)
			let _ = dtd_file lexbuf in
			let file = dtd_file lexbuf in
			dtd_end_decl lexbuf;
			DTDFile file
		}
	| "SYSTEM"
		{
			ignore_spaces lexbuf;
			let file = dtd_file lexbuf in
			dtd_end_decl lexbuf;
			DTDFile file
		}
	| '['
		{
			ignore_spaces lexbuf;
			let data = dtd_intern lexbuf in
			dtd_end_decl lexbuf;
			DTDData data
		}
	| _ | eof
		{ dtd_error lexbuf EInvalidDTDDecl }

and dtd_file = parse
	| '"'
		{
			Buffer.reset tmp;
			let s = dq_string lexbuf in
			ignore_spaces lexbuf;
			s
		}
	| '\''
		{
			Buffer.reset tmp;
			let s = q_string lexbuf in
			ignore_spaces lexbuf;
			s
		}
	| _ | eof
		{ dtd_error lexbuf EInvalidDTDDecl }

and dtd_intern = parse
	| ']'
		{
			ignore_spaces lexbuf;
			[]
		}
	| ""
		{
			let l = dtd_item lexbuf in
			l @ (dtd_intern lexbuf)
		}

and dtd = parse
	| eof
		{ [] }
	| newline
		{
			newline lexbuf;
			dtd lexbuf
		}
	| (space | break)+
		{ dtd lexbuf }
	| ""
		{
			let l = dtd_item lexbuf in
			l @ (dtd lexbuf)
		}

and dtd_end_decl = parse
	| '>'
		{ ignore_spaces lexbuf }
	| _ | eof
		{ dtd_error lexbuf EInvalidDTDDecl }

and dtd_item = parse
	| "<!--"
		{
			comment lexbuf;
			[];
		}
	| "<!"
		{
			ignore_spaces lexbuf;
			let t = dtd_item_type lexbuf in
			let name = (try ident_name lexbuf with Error EIdentExpected -> raise (DTDError EInvalidDTDDecl)) in
			ignore_spaces lexbuf;
			match t with
			| TElement -> [ DTDElement (name , (dtd_element_type lexbuf)) ]
			| TAttribute -> List.map (fun (attrname,atype,adef) -> DTDAttribute (name, attrname, atype, adef)) (dtd_attributes lexbuf)
		}
	| _ | eof
		{  dtd_error lexbuf EDTDItemExpected }

and dtd_attributes = parse
	| '>'
		{
			ignore_spaces lexbuf;
			[]
		}
	| ""
		{
			let attrname = (try ident_name lexbuf with Error EIdentExpected -> raise (DTDError EInvalidDTDAttribute)) in
			ignore_spaces lexbuf;
			let atype = dtd_attr_type lexbuf in
			let adef = dtd_attr_default lexbuf in
			let a = (attrname, atype, adef) in
			a :: (dtd_attributes lexbuf)
		}

and dtd_item_type = parse
	| "ELEMENT"
		{
			ignore_spaces lexbuf;
			TElement
		}
	| "ATTLIST"
		{
			ignore_spaces lexbuf;
			TAttribute
		}
	| _ | eof
		{ dtd_error lexbuf EInvalidDTDTag }

and dtd_element_type = parse
	| "ANY"
		{
			ignore_spaces lexbuf;
			dtd_end_element lexbuf;
			DTDAny
		}
	| "EMPTY"
		{
			ignore_spaces lexbuf;
			dtd_end_element lexbuf;
			DTDEmpty
		}
	| '('
		{
			try
				let item = Xml_parser.dtd_element dtd_element_token lexbuf in
				ignore_spaces lexbuf;
				DTDChild item
			with
				Parsing.Parse_error -> dtd_error lexbuf EInvalidDTDElement
		}
	| _ | eof
		{ dtd_error lexbuf EInvalidDTDElement }

and dtd_end_element = parse
	| '>'
		{ ignore_spaces lexbuf }
	| _ | eof
		{ dtd_error lexbuf EInvalidDTDElement }

and dtd_end_attribute = parse
	| '>'
		{ ignore_spaces lexbuf }
	| _ | eof
		{ dtd_error lexbuf EInvalidDTDAttribute }

and dtd_element_token = parse
	| newline
		{
			newline lexbuf;
			dtd_element_token lexbuf
		}
	| (space | break) +
		{ dtd_element_token lexbuf }
	| '('
		{ OPEN }
	| ')'
		{ CLOSE }
	| ','
		{ NEXT }
	| '>'
		{ END }
	| '|'
		{ OR }
	| "#PCDATA"
		{ PCDATA }
	| '*'
		{ STAR }
	| '+'
		{ PLUS }
	| '?'
		{ QUESTION }
	| identchar+
		{ IDENT (lexeme lexbuf) }
	| _ | eof
		{ dtd_error lexbuf EInvalidDTDElement }

and dtd_attr_type = parse
	| "CDATA"
		{
			ignore_spaces lexbuf;
			DTDCData
		}
	| "NMTOKEN"
		{
			ignore_spaces lexbuf;
			DTDNMToken
		}
	| "ID"
		{
			ignore_spaces lexbuf;
		 	DTDID
		}
	| "IDREF"
		{
			ignore_spaces lexbuf;
			DTDIDRef
		}
	| '('
		{
			ignore_spaces lexbuf;
			DTDEnum (dtd_attr_enum lexbuf)
		}
	| _ | eof
		{ dtd_error lexbuf EInvalidDTDAttribute }

and dtd_attr_enum = parse
	| identchar+
		{
			let v = lexeme lexbuf in
			ignore_spaces lexbuf;
			v :: (dtd_attr_enum_next lexbuf)
		}
	| _ | eof
		{ dtd_error lexbuf EInvalidDTDAttribute }

and dtd_attr_enum_next = parse
	| ')'
		{
			ignore_spaces lexbuf;
			[]
		}
	| '|'
		{
			ignore_spaces lexbuf;
			dtd_attr_enum lexbuf
		}
	| _ | eof
		{ dtd_error lexbuf EInvalidDTDAttribute }

and dtd_attr_default = parse
	| '"'
		{
			Buffer.reset tmp;
			let v = (try dq_string lexbuf with Error EUnterminatedString -> raise (DTDError EInvalidDTDAttribute)) in
			ignore_spaces lexbuf;
			DTDDefault v
		}
	| '\''
		{
			Buffer.reset tmp;
			let v = (try q_string lexbuf with Error EUnterminatedString -> raise (DTDError EInvalidDTDAttribute)) in
			ignore_spaces lexbuf;
			DTDDefault v
		}
	| "#REQUIRED"
		{
			ignore_spaces lexbuf;
			DTDRequired
		}
	| "#IMPLIED"
		{
			ignore_spaces lexbuf;
			DTDImplied
		}
	| "#FIXED"
		{
			ignore_spaces lexbuf;
			DTDFixed (dtd_attr_string lexbuf)
		}
	| "#DEFAULT"
		{
			ignore_spaces lexbuf;
			DTDDefault (dtd_attr_string lexbuf)
		}
	| _ | eof
		{ dtd_error lexbuf EInvalidDTDAttribute }

and dtd_attr_string = parse
	| '"'
		{
			Buffer.reset tmp;
			let v = (try dq_string lexbuf with Error EUnterminatedString -> raise (DTDError EInvalidDTDAttribute)) in
			ignore_spaces lexbuf;
			v
		}
	| '\''
		{
			Buffer.reset tmp;
			let v = (try q_string lexbuf with Error EUnterminatedString -> raise (DTDError EInvalidDTDAttribute)) in
			ignore_spaces lexbuf;
			v
		}
	| _ | eof
		{ dtd_error lexbuf EInvalidDTDAttribute }