parser.ml 17.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 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
    | DIRECTIVE "#dump_value"; e = expr -> [ mk loc (Directive (`Dump e)) ]
122
    | DIRECTIVE "#reinit_ns" -> [ mk loc (Directive `Reinit_ns) ]
123
    | DIRECTIVE "#help" -> [ mk loc (Directive `Help) ]
124
    | "include"; s = STRING2 ->
125 126 127 128
	let s = 
	  if Filename.is_relative s 
	  then Filename.concat (Location.current_dir ()) s
	  else s in
129 130
	protect_op "File inclusion";
	(* avoid looping; should issue an error ? *)
131 132
	(* it is possible to have looping with x/../x/../x/.. ....
	   Need to canonicalize filename *)
133 134 135 136
	if List.mem s !include_stack then [] 
	else (
	  include_stack := s :: !include_stack;
	  Location.push_source (`File s);
137 138
	  let saved_enc = !Ulexer.enc in
	  Ulexer.enc := Ulexing.Latin1;
139 140 141
	  protect_exn
	    (fun () ->
	       let chan = open_in s in
142 143 144 145 146
	       protect_exn
		 (fun () ->
		    let input = Stream.of_channel chan in
		    Grammar.Entry.parse prog input)
		 (fun () -> close_in chan))
147
	    (fun () ->
148
	       Ulexer.enc := saved_enc;
149 150
	       Location.pop_source ();
	       include_stack := List.tl !include_stack)
151
	)
152
    ] | 
153
    [ e = expr -> [ mk loc (EvalStatement e) ]
154 155 156 157
    ]
  ];

  debug_directive: [
158 159 160 161 162
    [ 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)
163
    | IDENT "explain"; t = pat; e = expr -> `Explain (t,e)
164
    ]
165
  ];
166

167 168 169 170 171
  keyword: [
    [ a = 
	[ "map" | "match" | "with" | "try" | "xtransform"
	| "if" | "then"	| "else"
	| "transform" | "fun" | "in"
172
	| "let" | "type" | "debug" | "include"
173
        | "and" | "validate" | "schema" | "namespace" | "ref" | "using"
174
	| "not"
175 176 177 178 179
	]
	-> a
    ]
  ];

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

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

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

  ];
318

319
  tag: [ [ a = [ IDENT | keyword ] -> exp loc (Atom (parse_ident a)) ] ];
320 321

  tag_type: [
322
    [ IDENT "_" ->  mk loc (Internal (Types.atom Atoms.any))
323
    | a = [ IDENT | keyword ] -> mk loc (Cst (Atom (parse_ident a)))
324
    | t = ANY_IN_NS -> mk loc (NsT (parse_ident t)) 
325 326 327
    ]
  ];

328
  seq_elem: [
329
    [ x = STRING1 -> 
330
	let s = U.mk x in
331
	`String (loc, U.start_index s, U.end_index s, s)
332
    | e = expr LEVEL "no_appl" -> `Elems (loc,e)
333
    | "!"; e = expr LEVEL "no_appl" -> `Explode e
334 335
    ]
  ];
336 337 338
	
  namespace_binding: [
    [ "namespace"; 
339
	name = [ name = [ IDENT | keyword ]; "=" ->
340 341 342
		   parse_ident name
	       | -> U.mk "" ];
	uri = STRING2 ->
343
	  let ns = Ns.mk (parse_ident uri) in
344 345 346 347 348
	  (name,ns)
    ]
  ];

  
349
  let_binding: [
350 351
    [ "let"; is_fun_decl; OPT "fun"; (f,a,b) = fun_decl ->
	let f = match f with Some x -> x | None -> assert false in
352
	let p = mk loc (PatVar (Id.value f)) in
353
	let abst = { fun_name = Some f; fun_iface = a; fun_body = b } in
354
        let e = exp loc (Abstraction abst) in
355
        (true,p,e)
356 357
    | "let"; p = pat; "="; e = expr -> (false,p,e)
    | "let"; p = pat; ":"; t = pat; "="; e = expr -> (false,p, Forget (e,t))
358 359 360
    ] 
  ];

361
 fun_decl_after_lparen: [
362
(* need an hack to do this, because both productions would
363
   match   [ OPT IDENT; "("; pat ] .... *)
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
   [ 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: [
386 387 388
   [ 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)
389
   ]
390 391
 ];

392
 arrow: [
393
    [ t1 = pat LEVEL "no_arrow"; "->"; t2 = pat -> (t1,t2)]
394 395 396
  ];

  branches: [
397
    [ OPT "|"; l = LIST1 branch SEP "|" -> l ]
398 399 400
  ];

  branch: [
401
    [ p = pat LEVEL "no_arrow"; "->"; e = expr -> (p,e) ]
402 403 404 405
  ];

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

455 456 457 458 459 460 461 462 463
  schema_kind : [
    [ "element" -> `Element
    | "type" -> `Type
    | "attribute" -> `Attribute
    | "attribute_group" -> `Attribute_group
    | "model_group" -> `Model_group
    ]
  ];

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

  record_spec:
532
    [ [ r = LIST0 [ l = [IDENT | keyword ]; "=";
533
                  o = [ "?" -> true | -> false]; 
534 535
                  x = pat -> 
		    let x = if o then mk loc (Optional x) else x in
536
		    (label l, x)
537
                ] SEP ";" ->
538
	  r
539 540
      ] ];
  
541 542
  char:
    [ 
543
      [ c = STRING1 -> Chars.V.mk_int (parse_char loc c) ]
544 545 546
    ];
     

547
  attrib_spec:
548
    [ [ r = LIST0 [ l = [IDENT | keyword ]; "=";
549 550 551 552 553 554
                  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)) 
555 556 557 558
      | "("; t = pat; ")" -> t 
      | "{"; r = record_spec; "}" -> mk loc (Record (true,r))
      | "{|"; r = record_spec; "|}" -> mk loc (Record (false,r))
      ] ];
559 560 561

  expr_record_spec:
    [ [ r = LIST1
562
	      [ l = [IDENT | keyword ]; "="; x = expr LEVEL "no_appl" -> 
563
		  (label l,x) ] 
564
	       SEP ";"  ->
565
	  exp loc (RecordLitt r)
566
      ] ];
567

568
  expr_attrib_spec:
569
    [ [ r = LIST1
570
	      [ l = [IDENT | keyword ]; "="; x = expr LEVEL "no_appl"; OPT ";" -> 
571 572 573
		  (label l,x) ] ->
	  exp loc (RecordLitt r)
      ]
574
    | [ e = expr LEVEL "no_appl" -> e 
575
      | -> exp loc (RecordLitt []) 
576 577 578 579
      ] 
    ];
END

580 581 582 583 584
module Hook = struct
  let expr = expr
  let pat = pat
end

585
let pat = Grammar.Entry.parse pat
586 587
and expr = Grammar.Entry.parse expr
and prog = Grammar.Entry.parse prog
588 589
and top_phrases = Grammar.Entry.parse top_phrases

590
let sync () = 
591
  match !Ulexer.lexbuf with
592 593 594
    | None -> ()
    | Some lb ->
	let rec aux () =
595
	  match !Ulexer.last_tok with
596 597
	    | ("",";;") | ("EOI","") -> ()
	    | _ -> 
598
		Ulexer.last_tok := fst (Ulexer.token lb); 
599 600 601
		aux ()
	in
	aux ()
602 603 604 605

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