parser.ml 18.9 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
let parse_ident = U.mk
18

19
let id_dummy = ident (U.mk "$$$")
20

21
let label = parse_ident
22
let ident s = ident (parse_ident s)
23

24
let prog    = Grammar.Entry.create gram "prog"
25
let top_phrases   = Grammar.Entry.create gram "toplevel phrases"
26
27
28
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"
29
let keyword = Grammar.Entry.create gram "keyword"
30
		
31
32
let exp pos e = LocatedExpr (loc_of_pos pos,e)

33
34
35
36
37
let rec multi_prod loc = function
  | [ x ] -> x
  | x :: l -> mk loc (Prod (x, multi_prod loc l))
  | [] -> assert false
      
38
let rec tuple = function
39
  | [ x ] -> x
40
  | x :: l -> Pair (x, tuple l)
41
  | [] -> assert false
42
43

let tuple_queue = 
44
  List.fold_right (fun x q -> Pair (x, q))
45
46


47
let char = mknoloc (Internal (Types.char Chars.any))
48
let string_regexp = Star (Elem char)
49
	       
50
let seq_of_string s =
51
52
  let s = Encodings.Utf8.mk s in
  let rec aux i j =
53
54
    if Encodings.Utf8.equal_index i j then []
    else let (c,i) = Encodings.Utf8.next s i in c :: (aux i j)
55
  in
56
  aux (Encodings.Utf8.start_index s) (Encodings.Utf8.end_index s)
57

58

59
let parse_char loc s =
60
61
  match seq_of_string s with
    | [ c ] -> c
62
    | _ -> error loc "Character litteral must have length 1"
63

64
65
let include_stack = ref []

66
67
68
69
let protect_exn f g =
  try let x = f () in g (); x
  with e -> g (); raise e

70
71
72
73
74
75
76
let localize_exn f = 
  try f ()
  with
  | Stdpp.Exc_located (_, (Location _ as e)) -> raise e
  | Stdpp.Exc_located ((i,j), e) -> raise_loc i j e
  

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

86
87
88
89
90
91
let if_then_else cond e1 e2 = Match (cond, [pat_true,e1; pat_false,e2])
 
let logical_and e1 e2 = if_then_else e1 e2 cst_false
let logical_or e1 e2 = if_then_else e1 cst_true e2
let logical_not e = if_then_else e cst_false cst_true
 
92

93
EXTEND
94
  GLOBAL: top_phrases prog expr pat regexp keyword;
95
96

  top_phrases: [
97
    [ l = LIST0 phrase; ";;" -> List.flatten l  ]
98
  ];
99
100

  prog: [
101
    [ l = LIST0 [ p = phrase ; OPT ";;" -> p ]; EOI -> List.flatten l ]
102
103
104
  ];

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

  debug_directive: [
180
181
182
183
184
    [ 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)
185
    | IDENT "explain"; t = pat; e = expr -> `Explain (t,e)
186
    ]
187
  ];
188

189
190
191
192
193
  keyword: [
    [ a = 
	[ "map" | "match" | "with" | "try" | "xtransform"
	| "if" | "then"	| "else"
	| "transform" | "fun" | "in"
194
	| "let" | "type" | "debug" | "include"
195
        | "and" | "validate" | "schema" | "namespace" | "ref" | "using"
196
	| "not"
197
198
199
200
201
	]
	-> a
    ]
  ];

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

245
    | 
246
    [ e1 = expr; op = ["+" | "-" | "@" ]; e2 = expr -> 
247
	exp loc (Op (op,[e1;e2]))
248
    | e1 = expr; "||"; e2 = expr -> exp loc (logical_or e1 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
    | e1 = expr; "&&"; e2 = expr -> exp loc (logical_and e1 e2)
255
256
257
    | e = expr; "/" ;
      t = [a = pat ->
        let tag = mk loc (Internal (Types.atom (Atoms.any))) in
258
259
	let att = mk loc (Internal Types.Record.any) in
	let any = mk loc (Internal (Types.any)) in
260
	let re = Star(Alt(SeqCapture(id_dummy,Elem a), Elem any)) in
261
262
	let ct = mk loc (Regexp (re,any)) in
        let p = mk loc (XmlT (tag, multi_prod loc [att;ct])) in
263
264
265
266
267
268
269
270
271
272
	(p,  Var (Id.value id_dummy))
       | "@";	  a = [IDENT|keyword]-> (* projection sur 1 attribut *)
        let tag = mk loc (Internal(Types.atom Atoms.any)) in
        let any = mk loc (Internal(Types.any)) in
        let att = mk loc (Record(true,[(label a,mk loc (PatVar(U.mk "$$$")))]))in
        let ct= mk loc (Regexp(Elem any , any)) in 
        let p = mk loc(XmlT (tag,multi_prod loc[att;ct])) in
        (p, Pair(Var (Id.value id_dummy),cst_nil)) 
       ] -> exp loc (Transform (e,[t]))
 
273
    ]
274
    |
275
    [ e = expr;  "."; l = [IDENT | keyword ] -> 
276
	exp loc (Dot (e, label l)) 
277
    ]  
278
 
279
    |
280
281
282
283
284
285
286
287
288
289
290
    [ 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"
291
	   ]; 
292
      e = expr -> exp loc (Op (op,[e]))
293
    | op = [ IDENT "dump_to_file" | IDENT "dump_to_file_utf8" ];
294
      e1 = expr LEVEL "no_appl"; e2 = expr -> exp loc (Op (op, [e1;e2])) 
295
296
    | e1 = SELF; IDENT "div"; e2 = expr -> exp loc (Op ("/", [e1;e2]))
    | e1 = SELF; IDENT "mod"; e2 = expr -> exp loc (Op ("mod", [e1;e2]))
297
    | e1 = SELF; e2 = expr -> exp loc (Apply (e1,e2))
298
299
    ]

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

  ];
335

336
  tag: [ [ a = [ IDENT | keyword ] -> exp loc (Atom (parse_ident a)) ] ];
337
338

  tag_type: [
339
    [ IDENT "_" ->  mk loc (Internal (Types.atom Atoms.any))
340
    | a = [ IDENT | keyword ] -> mk loc (Cst (Atom (parse_ident a)))
341
    | t = ANY_IN_NS -> mk loc (NsT (parse_ident t)) 
342
343
344
    ]
  ];

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

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

378
 fun_decl_after_lparen: [
379
(* need an hack to do this, because both productions would
380
   match   [ OPT IDENT; "("; pat ] .... *)
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
   [ 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: [
403
404
405
   [ 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)
406
   ]
407
408
 ];

409
 arrow: [
410
    [ t1 = pat LEVEL "no_arrow"; "->"; t2 = pat -> (t1,t2)]
411
412
413
  ];

  branches: [
414
    [ OPT "|"; l = LIST1 branch SEP "|" -> l ]
415
416
417
  ];

  branch: [
418
    [ p = pat LEVEL "no_arrow"; "->"; e = expr -> (p,e) ]
419
420
421
422
  ];

	  
  regexp: [ 
423
424
425
426
427
    [ x = regexp; "|"; y = regexp -> 
	match (x,y) with
	  | Elem x, Elem y -> Elem (mk loc (Or (x,y)))
	  | _ -> Alt (x,y) 
    ]
428
  | [ x = regexp; y = regexp -> Seq (x,y) ]
429
430
431
432
433
  | [ 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"
    ]
434
  | [ a = IDENT; "::"; x = regexp -> SeqCapture (ident a,x) ]
435
436
437
438
  | [ x = regexp; "*" -> Star x
    | x = regexp; "*?" -> WeakStar x
    | x = regexp; "+" -> Seq (x, Star x)
    | x = regexp; "+?" -> Seq (x, WeakStar x)
439
    | x = regexp; "?" ->  Alt (x, Epsilon)
440
    | x = regexp; "??" -> Alt (Epsilon, x) ]
441
  | [ "("; x = LIST1 regexp SEP ","; ")" ->
442
443
444
445
446
447
448
449
450
451
452
	(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))
453
454
    | "("; a = IDENT; ":="; c = expr; ")" -> 
	Elem (mk loc (Constant ((ident a,c))))
455
    | IDENT "PCDATA" -> string_regexp
456
    | i = STRING1; "--"; j = STRING1 ->
457
458
	let i = Chars.V.mk_int (parse_char loc i)
	and j = Chars.V.mk_int (parse_char loc j) in
459
        Elem (mk loc (Internal (Types.char (Chars.char_class i j))))
460
    |  s = STRING1 ->
461
	List.fold_right
462
	  (fun c accu -> 
463
	     let c = Chars.V.mk_int c in
464
	     let c = Chars.atom c in
465
466
	     Seq (Elem (mknoloc (Internal (Types.char c))), accu))
	  (seq_of_string s)
467
468
	  Epsilon ]
    | [ e = pat LEVEL "simple" -> Elem e
469
470
471
    ]
  ];

472
473
474
475
476
477
478
479
480
  schema_kind : [
    [ "element" -> `Element
    | "type" -> `Type
    | "attribute" -> `Attribute
    | "attribute_group" -> `Attribute_group
    | "model_group" -> `Model_group
    ]
  ];

481
482
483
  schema_ref: [
    [ schema = IDENT; "#"; typ = [ IDENT | keyword ];
      kind = OPT [ "as"; k = schema_kind -> k] ->
484
        (kind, U.mk schema, U.mk typ)
485
486
487
    ]
  ];

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

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

571
  attrib_spec:
572
    [ [ r = LIST0 [ l = [IDENT | keyword ]; "=";
573
574
575
576
577
578
                  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)) 
579
580
581
582
      | "("; t = pat; ")" -> t 
      | "{"; r = record_spec; "}" -> mk loc (Record (true,r))
      | "{|"; r = record_spec; "|}" -> mk loc (Record (false,r))
      ] ];
583
584
585

  expr_record_spec:
    [ [ r = LIST1
586
	      [ l = [IDENT | keyword ]; "="; x = expr LEVEL "no_appl" -> 
587
		  (label l,x) ] 
588
	       SEP ";"  ->
589
	  exp loc (RecordLitt r)
590
      ] ];
591

592
  expr_attrib_spec:
593
    [ [ r = LIST1
594
	      [ l = [IDENT | keyword ]; "="; x = expr LEVEL "no_appl"; OPT ";" -> 
595
596
597
		  (label l,x) ] ->
	  exp loc (RecordLitt r)
      ]
598
    | [ e = expr LEVEL "no_appl" -> e 
599
      | -> exp loc (RecordLitt []) 
600
601
602
603
      ] 
    ];
END

604
605
606
module Hook = struct
  let expr = expr
  let pat = pat
607
  let keyword = keyword
608
609
end

610
let pat = Grammar.Entry.parse pat
611
612
and expr = Grammar.Entry.parse expr
and prog = Grammar.Entry.parse prog
613
614
and top_phrases = Grammar.Entry.parse top_phrases

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

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