parser.ml 18.7 KB
Newer Older
1 2
#load "pa_extend.cmo";;

3 4
open Location
open Ast
5
open Ident
6
open Printf
7

8
(*
9
let ()  = Grammar.error_verbose := true
10
*)
11

12 13
exception Error of string
let error (i,j) s = Location.raise_loc i j (Error s)
14

15
let gram    = Grammar.gcreate Ulexer.lex
16

17 18
let true_atom = Atoms.V.mk_ascii "true"
let false_atom = Atoms.V.mk_ascii "false"
19 20
let true_type = Types.atom (Atoms.atom true_atom)
let false_type = Types.atom (Atoms.atom false_atom)
21

22
let parse_ident = U.mk
23

24
let id_dummy = ident (U.mk "$$$")
25

26
let label = parse_ident
27
let ident s = ident (parse_ident s)
28

29
let prog    = Grammar.Entry.create gram "prog"
30
let top_phrases   = Grammar.Entry.create gram "toplevel phrases"
31 32 33
let expr    = Grammar.Entry.create gram "expression"
let pat     = Grammar.Entry.create gram "type/pattern expression"
let regexp  = Grammar.Entry.create gram "type/pattern regexp"
34
let keyword = Grammar.Entry.create gram "keyword"
35
		
36 37
let exp pos e = LocatedExpr (loc_of_pos pos,e)

38 39 40 41 42
let rec multi_prod loc = function
  | [ x ] -> x
  | x :: l -> mk loc (Prod (x, multi_prod loc l))
  | [] -> assert false
      
43
let rec tuple = function
44
  | [ x ] -> x
45
  | x :: l -> Pair (x, tuple l)
46
  | [] -> assert false
47 48

let tuple_queue = 
49
  List.fold_right (fun x q -> Pair (x, q))
50 51


52
let char = mknoloc (Internal (Types.char Chars.any))
53
let string_regexp = Star (Elem char)
54
	       
55
let cst_nil =  Const (Types.Atom (Atoms.V.mk_ascii "nil"))
56
let pat_nil = mknoloc (Internal (Sequence.nil_type))
57
		 
58
let seq_of_string s =
59 60
  let s = Encodings.Utf8.mk s in
  let rec aux i j =
61 62
    if Encodings.Utf8.equal_index i j then []
    else let (c,i) = Encodings.Utf8.next s i in c :: (aux i j)
63
  in
64
  aux (Encodings.Utf8.start_index s) (Encodings.Utf8.end_index s)
65

66

67
let parse_char loc s =
68 69
  match seq_of_string s with
    | [ c ] -> c
70
    | _ -> error loc "Character litteral must have length 1"
71

72 73
let include_stack = ref []

74 75 76 77
let protect_exn f g =
  try let x = f () in g (); x
  with e -> g (); raise e

78 79 80 81
let is_fun_decl =
   Grammar.Entry.of_parser gram "[is_fun_decl]"
     (fun strm ->
       match Stream.npeek 3 strm with
82 83
	 | [ ("", "fun"); ("IDENT", _); ("", "(") ]
	 | [ ("IDENT", _) ; ("", "(") ; _ ] -> ()
84 85 86
	 | _ -> raise Stream.Failure
     )

87
EXTEND
88
  GLOBAL: top_phrases prog expr pat regexp keyword;
89 90

  top_phrases: [
91
    [ l = LIST0 phrase; ";;" -> List.flatten l  ]
92
  ];
93 94

  prog: [
95
    [ l = LIST0 [ p = phrase ; OPT ";;" -> p ]; EOI -> List.flatten l ]
96 97 98
  ];

  phrase: [
99 100 101 102
    [ (f,p,e) = let_binding -> 
	if f then [ mk loc (FunDecl e) ] else
	  [ mk loc (LetDecl (p,e)) ]
    | (_,p,e1) = let_binding; "in"; e2 = expr LEVEL "top"->
103
        [ mk loc (EvalStatement (exp loc (Match (e1,[p,e2])))) ]
104
    | "type"; x = IDENT; "="; t = pat -> [ mk loc (TypeDecl (ident x,t)) ]
105 106
    | "using"; name = IDENT; "="; cu = STRING2 ->
	[ mk loc (Using (U.mk name, Types.CompUnit.mk (U.mk cu))) ]
107
    | "schema"; name = IDENT; "="; uri = STRING2 ->
108
	protect_op "schema";
109 110
        let schema = Schema_parser.schema_of_file uri in
        [ mk loc (SchemaDecl (name, schema)) ]
111 112 113 114 115
    | (name,ns) = namespace_binding ->
	[ mk loc (Namespace (name, ns)) ]
    | (name,ns) = namespace_binding; "in"; e2 = expr LEVEL "top" ->
	let e = exp loc (NamespaceIn (name, ns, e2)) in
        [ mk loc (EvalStatement (exp loc e)) ]
116
    | "debug"; d = debug_directive -> [ mk loc (Debug d) ]
117 118 119
    | DIRECTIVE "#utf8" -> Ulexer.enc := Ulexing.Utf8; [ ]
    | DIRECTIVE "#latin1" -> Ulexer.enc := Ulexing.Latin1; [ ]
    | DIRECTIVE "#ascii" -> Ulexer.enc := Ulexing.Ascii; [ ]
120 121
    | DIRECTIVE "#quit" -> [ mk loc (Directive `Quit) ]
    | DIRECTIVE "#env" -> [ mk loc (Directive `Env) ]
122 123 124 125 126 127 128 129 130 131 132 133
    | DIRECTIVE "#print_schema"; name = IDENT ->
        [ mk loc (Directive (`Print_schema name)) ]
    | DIRECTIVE "#print_type"; name = IDENT;
      schema_part = OPT [
        "#"; typ = [ IDENT | keyword ];
        kind = OPT [ "as"; k = schema_kind -> k] ->
          (kind, typ)
      ] ->
        (match schema_part with
        | None -> [ mk loc (Directive (`Print_type name)) ]
        | Some (kind, typ) ->
            [ mk loc (Directive (`Print_schema_type (kind, name, typ))) ])
134
    | DIRECTIVE "#dump_value"; e = expr -> [ mk loc (Directive (`Dump e)) ]
135
    | DIRECTIVE "#reinit_ns" -> [ mk loc (Directive `Reinit_ns) ]
136
    | DIRECTIVE "#help" -> [ mk loc (Directive `Help) ]
137
    | "include"; s = STRING2 ->
138 139 140 141
	let s = 
	  if Filename.is_relative s 
	  then Filename.concat (Location.current_dir ()) s
	  else s in
142 143
	protect_op "File inclusion";
	(* avoid looping; should issue an error ? *)
144 145
	(* it is possible to have looping with x/../x/../x/.. ....
	   Need to canonicalize filename *)
146 147 148 149
	if List.mem s !include_stack then [] 
	else (
	  include_stack := s :: !include_stack;
	  Location.push_source (`File s);
150 151
	  let saved_enc = !Ulexer.enc in
	  Ulexer.enc := Ulexing.Latin1;
152 153 154
	  protect_exn
	    (fun () ->
	       let chan = open_in s in
155 156 157 158 159
	       protect_exn
		 (fun () ->
		    let input = Stream.of_channel chan in
		    Grammar.Entry.parse prog input)
		 (fun () -> close_in chan))
160
	    (fun () ->
161
	       Ulexer.enc := saved_enc;
162 163
	       Location.pop_source ();
	       include_stack := List.tl !include_stack)
164
	)
165
    ] | 
166
    [ e = expr -> [ mk loc (EvalStatement e) ]
167 168 169 170
    ]
  ];

  debug_directive: [
171 172 173 174 175
    [ IDENT "filter"; t = pat; p = pat -> `Filter(t,p)
    | IDENT "accept"; p = pat -> `Accept p
    | IDENT "compile"; t = pat; p = LIST1 pat -> `Compile (t,p)
    | IDENT "sample"; t = pat -> `Sample t
    | IDENT "subtype"; t1 = pat; t2 = pat -> `Subtype (t1,t2)
176
    | IDENT "explain"; t = pat; e = expr -> `Explain (t,e)
177
    ]
178
  ];
179

180 181 182 183 184
  keyword: [
    [ a = 
	[ "map" | "match" | "with" | "try" | "xtransform"
	| "if" | "then"	| "else"
	| "transform" | "fun" | "in"
185
	| "let" | "type" | "debug" | "include"
186
        | "and" | "validate" | "schema" | "namespace" | "ref" | "using"
187
	| "not"
188 189 190 191 192
	]
	-> a
    ]
  ];

193 194
  expr: [
    "top" RIGHTA
195 196
    [ "match"; e = SELF; "with"; b = branches -> 
	exp loc (Match (e,b))
197
    | "try"; e = SELF; "with"; b = branches -> 
198
	exp loc (Try (e,b))
199 200 201 202
    | "map"; e = SELF; "with"; b = branches -> 
	exp loc (Map (e,b))
    | "xtransform"; e = SELF; "with"; b = branches -> 
	exp loc (Xtrans (e,b))
203
    | "if"; e = SELF; "then"; e1 = SELF; "else"; e2 = SELF ->
204 205
	let p1 = mk loc (Internal true_type)
	and p2 = mk loc (Internal false_type) in
206
	exp loc (Match (e, [p1,e1; p2,e2]))
207
    | "transform"; e = SELF; "with"; b = branches -> 
208
	exp loc (Transform (e,b))
209 210
    | "validate"; e = SELF; "with"; (kind, schema, typ) = schema_ref ->
        exp loc (Validate (e, kind, schema, typ))
211
    | "fun"; (f,a,b) = fun_decl ->
212
	exp loc (Abstraction { fun_name = f; fun_iface = a; fun_body = b })
213
    | (_,p,e1) = let_binding; "in"; e2 = expr LEVEL "top"->
214
        exp loc (Match (e1,[p,e2]))
215 216
    | (name,ns) = namespace_binding; "in"; e2 = expr LEVEL "top" ->
	exp loc (NamespaceIn (name, ns, e2))
217
    | e = expr; ":"; p = pat ->
218
	exp loc (Forget (e,p))
219 220 221 222
    | e1 = expr; ";"; e2 = expr ->
	exp loc (Match (e1, [pat_nil,e2]))
    | "ref"; p = pat; e = expr ->
	exp loc (Ref (e,p))
223 224 225 226 227
    | "not"; e = expr ->
	let p1 = mknoloc (Internal true_type)
	and p2 = mknoloc (Internal false_type) in
	exp loc (Match (e, [p1,Const (Types.Atom (false_atom)); 
			     p2,Const (Types.Atom (true_atom))]))       
228 229 230 231
    ]
    |
    [ e1 = expr; ":="; e2 = expr ->
	exp loc (Apply (Dot (e1, U.mk "set"), e2))
232
    ]
233 234 235 236 237 238
    | 
    [ e1 = expr; op = ["=" | "<=" | "<<" | ">>" | ">=" ]; e2 = expr -> 
	let op = match op with
	  | "<<" -> "<"
	  | ">>" -> ">"
	  | s -> s in
239
	exp loc (Op (op,[e1;e2]))
240 241
    ]

242
    | 
243
    [ e1 = expr; op = ["+" | "-" | "@" ]; e2 = expr -> 
244
	exp loc (Op (op,[e1;e2]))
245 246 247 248
    | e1 = expr; "||"; e2 = expr ->
	let p1 = mknoloc (Internal true_type)
	and p2 = mknoloc (Internal false_type) in
	exp loc (Match (e1, [p1,Const (Types.Atom (true_atom)); p2,e2]))       
249
    | e = expr; "\\"; l = [IDENT | keyword ] -> 
250
	exp loc (RemoveField (e, label l)) 
251 252
    ]
    |
253
    [ e1 = expr; op = ["*"]; e2 = expr -> exp loc (Op (op,[e1;e2]))  
254 255 256 257
    | e1 = expr; "&&"; e2 = expr ->
	let p1 = mknoloc (Internal true_type)
	and p2 = mknoloc (Internal false_type) in
	exp loc (Match (e1, [p1,e2;p2,Const (Types.Atom (false_atom))]))       
258 259 260 261 262
    | e = expr; op = "/"; p = pat ->

	let tag = mk loc (Internal (Types.atom (Atoms.any))) in
	let att = mk loc (Internal Types.Record.any) in
	let any = mk loc (Internal (Types.any)) in
263
	let re = Star(Alt(SeqCapture(id_dummy,Elem p), Elem any)) in
264 265
	let ct = mk loc (Regexp (re,any)) in
        let p = mk loc (XmlT (tag, multi_prod loc [att;ct])) in
266
	let b = (p,  Var (Id.value id_dummy)) in
267
	exp loc (Transform (e,[b]))
268
    ]
269
    |
270
    [ e = expr;  "."; l = [IDENT | keyword ] -> 
271
	exp loc (Dot (e, label l)) 
272
    ]  
273
 
274
    |
275 276 277 278 279 280 281 282 283 284 285
    [ op = [ IDENT "flatten"
	   | IDENT "load_xml"
	   | IDENT "load_file" | IDENT "load_file_utf8"
           | IDENT "getenv"
	   | IDENT "load_html"
	   | IDENT "print_xml" | IDENT "print_xml_utf8"
	   | IDENT "print"
	   | IDENT "int_of"
	   | IDENT "string_of"
	   | IDENT "atom_of"
	   | IDENT "raise"
286
	   ]; 
287
      e = expr -> exp loc (Op (op,[e]))
288
    | op = [ IDENT "dump_to_file" | IDENT "dump_to_file_utf8" ];
289
      e1 = expr LEVEL "no_appl"; e2 = expr -> exp loc (Op (op, [e1;e2])) 
290 291
    | e1 = SELF; IDENT "div"; e2 = expr -> exp loc (Op ("/", [e1;e2]))
    | e1 = SELF; IDENT "mod"; e2 = expr -> exp loc (Op ("mod", [e1;e2]))
292
    | e1 = SELF; e2 = expr -> exp loc (Apply (e1,e2))
293 294
    ]

295
    | "no_appl" 
296 297
    [ 
      "("; l = LIST1 expr SEP ","; ")" -> exp loc (tuple l)
298
    | "[";  l = LIST0 seq_elem; e = OPT [ ";"; e = expr -> e ]; 
299
      loc_end = ["]" -> loc] ->
300
	let e = match e with Some e -> e | None -> cst_nil in
301 302
        let e = exp loc_end e in
        let (_,loc_end) = loc_end in
303 304 305
	let l = List.fold_right 
		  (fun x q ->
		     match x with
306
		       | `String (loc,i,j,s) -> exp loc (String (i,j,s,q))
307
		       | `Elems ((loc,_),x) -> exp (loc,loc_end) (Pair(x,q))
308 309 310 311
		       | `Explode x -> Op ("@",[x;q])
		  ) l e
	in
	exp loc l
312
    | "<"; t = [ "("; e = expr; ")" -> e
313
	       | a = tag -> exp loc a
314
	       ];
315
	a = expr_attrib_spec; ">"; c = expr ->
316
	  exp loc (Xml (t, Pair (a,c)))
317
    | "{"; r = [ expr_record_spec | -> exp loc (RecordLitt []) ]; "}" -> r
318
    | s = STRING2 ->
319
	let s = U.mk s in
320
	exp loc (String (U.start_index s, U.end_index s, s, cst_nil))
321
    | a = IDENT -> exp loc (Var (U.mk a))
322 323
    | "!"; e = expr ->
	exp loc (Apply (Dot (e, U.mk "get"), cst_nil))
324 325 326
    | i = INT -> exp loc (Integer (Intervals.V.mk i))
    | "`"; a = tag -> a
    | c = char -> exp loc (Char c)
327 328 329
    ]

  ];
330

331
  tag: [ [ a = [ IDENT | keyword ] -> exp loc (Atom (parse_ident a)) ] ];
332 333

  tag_type: [
334
    [ IDENT "_" ->  mk loc (Internal (Types.atom Atoms.any))
335
    | a = [ IDENT | keyword ] -> mk loc (Cst (Atom (parse_ident a)))
336
    | t = ANY_IN_NS -> mk loc (NsT (parse_ident t)) 
337 338 339
    ]
  ];

340
  seq_elem: [
341
    [ x = STRING1 -> 
342
	let s = U.mk x in
343
	`String (loc, U.start_index s, U.end_index s, s)
344
    | e = expr LEVEL "no_appl" -> `Elems (loc,e)
345
    | "!"; e = expr LEVEL "no_appl" -> `Explode e
346 347
    ]
  ];
348 349 350
	
  namespace_binding: [
    [ "namespace"; 
351
	name = [ name = [ IDENT | keyword ]; "=" ->
352 353 354
		   parse_ident name
	       | -> U.mk "" ];
	uri = STRING2 ->
355
	  let ns = Ns.mk (parse_ident uri) in
356 357 358 359 360
	  (name,ns)
    ]
  ];

  
361
  let_binding: [
362 363
    [ "let"; is_fun_decl; OPT "fun"; (f,a,b) = fun_decl ->
	let f = match f with Some x -> x | None -> assert false in
364
	let p = mk loc (PatVar (Id.value f)) in
365
	let abst = { fun_name = Some f; fun_iface = a; fun_body = b } in
366
        let e = exp loc (Abstraction abst) in
367
        (true,p,e)
368 369
    | "let"; p = pat; "="; e = expr -> (false,p,e)
    | "let"; p = pat; ":"; t = pat; "="; e = expr -> (false,p, Forget (e,t))
370 371 372
    ] 
  ];

373
 fun_decl_after_lparen: [
374
(* need an hack to do this, because both productions would
375
   match   [ OPT IDENT; "("; pat ] .... *)
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
   [ p1 = pat LEVEL "no_arrow";
     res = [ "->"; p2 = pat;
	     a = [ ";"; a = LIST0 arrow SEP ";" -> a | -> [] ];
	     ")"; b = branches -> `Classic (p2,a,b)
	   | ":"; targ1 = pat;
	     args = LIST0 [ ","; arg = pat; ":"; targ = pat -> (arg,targ) ]; 
	     ")"; ":"; tres = pat ; 
	     "="; body = expr ->
	       `Compact (targ1,args,tres,body)
	   ] ->
       match res with
	 | `Classic (p2,a,b) -> (p1,p2)::a,b
	 | `Compact (targ1,args,tres,body) ->
	     let args = (p1,targ1) :: args in
	     let targ = multi_prod nopos (List.map snd args) in
	     let arg = multi_prod nopos (List.map fst args) in
	     let b = [arg, body] in
	     let a = [targ,tres] in
	     (a,b) ] ];


 fun_decl: [
398 399 400
   [ f = OPT IDENT; "("; (a,b) = fun_decl_after_lparen ->
       let f = match f with Some x -> Some (ident x) | None -> None in
       (f,a,b)
401
   ]
402 403
 ];

404
 arrow: [
405
    [ t1 = pat LEVEL "no_arrow"; "->"; t2 = pat -> (t1,t2)]
406 407 408
  ];

  branches: [
409
    [ OPT "|"; l = LIST1 branch SEP "|" -> l ]
410 411 412
  ];

  branch: [
413
    [ p = pat LEVEL "no_arrow"; "->"; e = expr -> (p,e) ]
414 415 416 417
  ];

	  
  regexp: [ 
418 419 420 421 422
    [ x = regexp; "|"; y = regexp -> 
	match (x,y) with
	  | Elem x, Elem y -> Elem (mk loc (Or (x,y)))
	  | _ -> Alt (x,y) 
    ]
423
  | [ x = regexp; y = regexp -> Seq (x,y) ]
424 425 426 427 428
  | [ x = regexp; "&"; y = regexp ->
	match (x,y) with
	  | Elem x, Elem y -> Elem (mk loc (And (x,y)))
	  | _ -> error loc "Conjunction not allowed in regular expression"
    ]
429
  | [ a = IDENT; "::"; x = regexp -> SeqCapture (ident a,x) ]
430 431 432 433
  | [ x = regexp; "*" -> Star x
    | x = regexp; "*?" -> WeakStar x
    | x = regexp; "+" -> Seq (x, Star x)
    | x = regexp; "+?" -> Seq (x, WeakStar x)
434
    | x = regexp; "?" ->  Alt (x, Epsilon)
435
    | x = regexp; "??" -> Alt (Epsilon, x) ]
436
  | [ "("; x = LIST1 regexp SEP ","; ")" ->
437 438 439 440 441 442 443 444 445 446 447
	(match x with
	  | [ x ] -> x
	  | _ ->
	      let x = 
		List.map 
		  (function
		     | Elem x -> x
		     | _ -> error loc 
			 "Mixing regular expressions and products")
		  x in
	      Elem (multi_prod loc x))
448 449
    | "("; a = IDENT; ":="; c = expr; ")" -> 
	Elem (mk loc (Constant ((ident a,c))))
450
    | IDENT "PCDATA" -> string_regexp
451
    | i = STRING1; "--"; j = STRING1 ->
452 453
	let i = Chars.V.mk_int (parse_char loc i)
	and j = Chars.V.mk_int (parse_char loc j) in
454
        Elem (mk loc (Internal (Types.char (Chars.char_class i j))))
455
    |  s = STRING1 ->
456
	List.fold_right
457
	  (fun c accu -> 
458
	     let c = Chars.V.mk_int c in
459
	     let c = Chars.atom c in
460 461
	     Seq (Elem (mknoloc (Internal (Types.char c))), accu))
	  (seq_of_string s)
462 463
	  Epsilon ]
    | [ e = pat LEVEL "simple" -> Elem e
464 465 466
    ]
  ];

467 468 469 470 471 472 473 474 475
  schema_kind : [
    [ "element" -> `Element
    | "type" -> `Type
    | "attribute" -> `Attribute
    | "attribute_group" -> `Attribute_group
    | "model_group" -> `Model_group
    ]
  ];

476 477 478 479 480 481 482
  schema_ref: [
    [ schema = IDENT; "#"; typ = [ IDENT | keyword ];
      kind = OPT [ "as"; k = schema_kind -> k] ->
        (kind, schema, typ)
    ]
  ];

483
  pat: [ 
484
      [ x = pat; IDENT "where"; 
485
        b = LIST1 [ a = IDENT; "="; y = pat -> (ident a,y) ] SEP "and"
486 487
            -> mk loc (Recurs (x,b)) ]
    | RIGHTA [ x = pat; "->"; y = pat -> mk loc (Arrow (x,y)) ]
488
    | "no_arrow" [ x = pat; "|"; y = pat -> mk loc (Or (x,y)) ] 
489
    | "simple" [ x = pat; "&"; y = pat -> mk loc (And (x,y)) 
490
	       | x = pat; "\\"; y = pat -> mk loc (Diff (x,y)) ]
491
    | 
492 493
      [ "{"; r = record_spec; "}" -> mk loc (Record (true,r))
      | "{|"; r = record_spec; "|}" -> mk loc (Record (false,r))
494 495 496 497 498
      | "ref"; p = pat ->
	  let get_fun = mk loc (Arrow (pat_nil, p)) 
	  and set_fun = mk loc (Arrow (p, pat_nil)) in
	  let fields = [ label "get", get_fun; label "set", set_fun ] in
	  mk loc (Record (false, fields))
499
      | IDENT "_" -> mk loc (Internal Types.any)
500
      | "("; a = IDENT; ":="; c = expr; ")" -> 
501
	  mk loc (Constant (ident a,c))
502
      | schema = IDENT; "#"; typ = [ IDENT | keyword ];
503
        kind = OPT [ "as"; k = schema_kind -> k] ->
504
          mk loc (SchemaVar (kind, schema, typ))
505
      | a = IDENT -> 
506
	  mk loc (PatVar (U.mk a))
507
      | i = INT ; "--"; j = INT -> 
508 509
          let i =  Intervals.V.mk i 
	  and j =  Intervals.V.mk j in
510 511
          mk loc (Internal (Types.interval (Intervals.bounded i j)))
      | i = INT -> 
512
          let i =  Intervals.V.mk i  in
513
          mk loc (Internal (Types.interval (Intervals.atom i)))
514
      | "*"; "--"; j = INT ->
515
	  let j =  Intervals.V.mk j in
516
          mk loc (Internal (Types.interval (Intervals.left j)))
517
      | i = INT; "--"; "*" ->
518
	  let i = Intervals.V.mk i in
519
          mk loc (Internal (Types.interval (Intervals.right i)))
520 521
      | i = char ->
          mk loc (Internal (Types.char (Chars.char_class i i)))
522
      | i = char ; "--"; j = char ->
523
          mk loc (Internal (Types.char (Chars.char_class i j)))
524
      | "`"; c = tag_type -> c
525 526 527
      | "("; l = LIST1 pat SEP ","; ")" -> multi_prod loc l
      | "["; r = [ r = regexp -> r | -> Epsilon ];
             q = [ ";"; q = pat -> q 
528
                 | -> pat_nil ]; 
529
             "]" -> mk loc (Regexp (r,q))
530
      | "<"; t =
531 532
            [ x = tag_type -> x
            | "("; t = pat; ")" -> t ];
533
	a = attrib_spec; ">"; c = pat ->
534
          mk loc (XmlT (t, multi_prod loc [a;c]))
535
      | s = STRING2 ->
536 537 538 539 540 541
	  let s = 
	    List.map 
	      (fun c -> 
		 mknoloc (Internal
			     (Types.char
				(Chars.atom
542
				   (Chars.V.mk_int c))))) 
543 544
	      (seq_of_string s) in
	  let s = s @ [mknoloc (Internal (Sequence.nil_type))] in
545
	  multi_prod loc s
546 547 548 549 550
      ]
    
  ];

  record_spec:
551
    [ [ r = LIST0 [ l = [IDENT | keyword ]; "=";
552
                  o = [ "?" -> true | -> false]; 
553 554
                  x = pat -> 
		    let x = if o then mk loc (Optional x) else x in
555
		    (label l, x)
556
                ] SEP ";" ->
557
	  r
558 559
      ] ];
  
560 561
  char:
    [ 
562
      [ c = STRING1 -> Chars.V.mk_int (parse_char loc c) ]
563 564 565
    ];
     

566
  attrib_spec:
567
    [ [ r = LIST0 [ l = [IDENT | keyword ]; "=";
568 569 570 571 572 573
                  o = [ "?" -> true | -> false]; 
                  x = pat; OPT ";" -> 
		    let x = if o then mk loc (Optional x) else x in
		    (label l, x)
                ] ->
	  mk loc (Record (true,r)) 
574 575 576 577
      | "("; t = pat; ")" -> t 
      | "{"; r = record_spec; "}" -> mk loc (Record (true,r))
      | "{|"; r = record_spec; "|}" -> mk loc (Record (false,r))
      ] ];
578 579 580

  expr_record_spec:
    [ [ r = LIST1
581
	      [ l = [IDENT | keyword ]; "="; x = expr LEVEL "no_appl" -> 
582
		  (label l,x) ] 
583
	       SEP ";"  ->
584
	  exp loc (RecordLitt r)
585
      ] ];
586

587
  expr_attrib_spec:
588
    [ [ r = LIST1
589
	      [ l = [IDENT | keyword ]; "="; x = expr LEVEL "no_appl"; OPT ";" -> 
590 591 592
		  (label l,x) ] ->
	  exp loc (RecordLitt r)
      ]
593
    | [ e = expr LEVEL "no_appl" -> e 
594
      | -> exp loc (RecordLitt []) 
595 596 597 598
      ] 
    ];
END

599 600 601
module Hook = struct
  let expr = expr
  let pat = pat
602
  let keyword = keyword
603 604
end

605
let pat = Grammar.Entry.parse pat
606 607
and expr = Grammar.Entry.parse expr
and prog = Grammar.Entry.parse prog
608 609
and top_phrases = Grammar.Entry.parse top_phrases

610
let sync () = 
611
  match !Ulexer.lexbuf with
612 613 614
    | None -> ()
    | Some lb ->
	let rec aux () =
615
	  match !Ulexer.last_tok with
616 617
	    | ("",";;") | ("EOI","") -> ()
	    | _ -> 
618
		Ulexer.last_tok := fst (Ulexer.token lb); 
619 620 621
		aux ()
	in
	aux ()
622 623 624 625

let sync () =
  try sync ()
  with Ulexing.Error -> ()