parser.ml 18.6 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
34
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"
		
35
36
let exp pos e = LocatedExpr (loc_of_pos pos,e)

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

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


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

65

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

71
72
let include_stack = ref []

73
74
75
76
let protect_exn f g =
  try let x = f () in g (); x
  with e -> g (); raise 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
EXTEND
87
  GLOBAL: top_phrases prog expr pat regexp;
88
89

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

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

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

  debug_directive: [
170
171
172
173
174
    [ 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)
175
    | IDENT "explain"; t = pat; e = expr -> `Explain (t,e)
176
    ]
177
  ];
178

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

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

241
    | 
242
    [ e1 = expr; op = ["+" | "-" | "@" ]; e2 = expr -> 
243
	exp loc (Op (op,[e1;e2]))
244
245
246
247
    | 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]))       
248
    | e = expr; "\\"; l = [IDENT | keyword ] -> 
249
	exp loc (RemoveField (e, label l)) 
250
251
    ]
    |
252
    [ e1 = expr; op = ["*"]; e2 = expr -> exp loc (Op (op,[e1;e2]))  
253
254
255
256
    | 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))]))       
257
258
259
260
261
    | 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
262
	let re = Star(Alt(SeqCapture(id_dummy,Elem p), Elem any)) in
263
264
	let ct = mk loc (Regexp (re,any)) in
        let p = mk loc (XmlT (tag, multi_prod loc [att;ct])) in
265
	let b = (p,  Var (Id.value id_dummy)) in
266
	exp loc (Transform (e,[b]))
267
    ]
268
    |
269
    [ e = expr;  "."; l = [IDENT | keyword ] -> 
270
	exp loc (Dot (e, label l)) 
271
    ]  
272
 
273
    |
274
275
276
277
278
279
280
281
282
283
284
    [ 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"
285
	   ]; 
286
      e = expr -> exp loc (Op (op,[e]))
287
    | op = [ IDENT "dump_to_file" | IDENT "dump_to_file_utf8" ];
288
      e1 = expr LEVEL "no_appl"; e2 = expr -> exp loc (Op (op, [e1;e2])) 
289
290
    | e1 = SELF; IDENT "div"; e2 = expr -> exp loc (Op ("/", [e1;e2]))
    | e1 = SELF; IDENT "mod"; e2 = expr -> exp loc (Op ("mod", [e1;e2]))
291
    | e1 = SELF; e2 = expr -> exp loc (Apply (e1,e2))
292
293
    ]

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

  ];
329

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

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

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

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

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

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

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

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

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

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

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

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

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

565
  attrib_spec:
566
    [ [ r = LIST0 [ l = [IDENT | keyword ]; "=";
567
568
569
570
571
572
                  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)) 
573
574
575
576
      | "("; t = pat; ")" -> t 
      | "{"; r = record_spec; "}" -> mk loc (Record (true,r))
      | "{|"; r = record_spec; "|}" -> mk loc (Record (false,r))
      ] ];
577
578
579

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

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

598
599
600
601
602
module Hook = struct
  let expr = expr
  let pat = pat
end

603
let pat = Grammar.Entry.parse pat
604
605
and expr = Grammar.Entry.parse expr
and prog = Grammar.Entry.parse prog
606
607
and top_phrases = Grammar.Entry.parse top_phrases

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

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