parser.ml 17.2 KB
Newer Older
1 2
open Location
open Ast
3
open Ident
4
open Printf
5

6
(*
7
let ()  = Grammar.error_verbose := true
8
*)
9

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

13
let gram    = Grammar.gcreate Ulexer.lex
14

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

20
let parse_ident = U.mk
21

22
let id_dummy = ident (U.mk "$$$")
23

24
let label = parse_ident
25
let ident s = ident (parse_ident s)
26

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

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

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


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

63

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

69 70
let include_stack = ref []

71 72 73 74
let protect_exn f g =
  try let x = f () in g (); x
  with e -> g (); raise e

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

84
EXTEND
85
  GLOBAL: top_phrases prog expr pat regexp;
86 87

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

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

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

  debug_directive: [
154 155 156 157 158
    [ 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)
159
    | IDENT "explain"; t = pat; e = expr -> `Explain (t,e)
160
    ]
161
  ];
162

163 164 165 166 167
  keyword: [
    [ a = 
	[ "map" | "match" | "with" | "try" | "xtransform"
	| "if" | "then"	| "else"
	| "transform" | "fun" | "in"
168
	| "let" | "type" | "debug" | "include"
169
        | "and" | "validate" | "schema" | "namespace" | "ref"
170 171 172 173 174
	]
	-> a
    ]
  ];

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

220
    | 
221
    [ e1 = expr; op = ["+" | "-" | "@" ]; e2 = expr -> 
222
	exp loc (Op (op,[e1;e2]))
223
    | e = expr; "\\"; l = [IDENT | keyword ] -> 
224
	exp loc (RemoveField (e, label l)) 
225 226
    ]
    |
227
    [ e1 = expr; op = ["*"]; e2 = expr -> exp loc (Op (op,[e1;e2]))  
228 229 230 231 232
    | 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
233
	let re = Star(Alt(SeqCapture(id_dummy,Elem p), Elem any)) in
234 235
	let ct = mk loc (Regexp (re,any)) in
        let p = mk loc (XmlT (tag, multi_prod loc [att;ct])) in
236
	let b = (p,  Var id_dummy) in
237
	exp loc (Transform (e,[b]))
238
    ]
239
    |
240
    [ e = expr;  "."; l = [IDENT | keyword ] -> 
241
	exp loc (Dot (e, label l)) 
242
    ]  
243
 
244
    |
245 246 247 248 249 250 251 252 253 254 255
    [ 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"
256
	   ]; 
257
      e = expr -> exp loc (Op (op,[e]))
258
    | op = [ IDENT "dump_to_file" | IDENT "dump_to_file_utf8" ];
259
      e1 = expr LEVEL "no_appl"; e2 = expr -> exp loc (Op (op, [e1;e2])) 
260 261
    | e1 = SELF; IDENT "div"; e2 = expr -> exp loc (Op ("/", [e1;e2]))
    | e1 = SELF; IDENT "mod"; e2 = expr -> exp loc (Op ("mod", [e1;e2]))
262
    | e1 = SELF; e2 = expr -> exp loc (Apply (e1,e2))
263 264
    ]

265
    | "no_appl" 
266 267
    [ 
      "("; l = LIST1 expr SEP ","; ")" -> exp loc (tuple l)
268
    | "[";  l = LIST0 seq_elem; e = OPT [ ";"; e = expr -> e ]; 
269
      loc_end = ["]" -> loc] ->
270
	let e = match e with Some e -> e | None -> cst_nil in
271 272
        let e = exp loc_end e in
        let (_,loc_end) = loc_end in
273 274 275
	let l = List.fold_right 
		  (fun x q ->
		     match x with
276
		       | `String (loc,i,j,s) -> exp loc (String (i,j,s,q))
277
		       | `Elems ((loc,_),x) -> exp (loc,loc_end) (Pair(x,q))
278 279 280 281
		       | `Explode x -> Op ("@",[x;q])
		  ) l e
	in
	exp loc l
282
    | "<"; t = [ "("; e = expr; ")" -> e
283
	       | a = tag -> exp loc a
284
	       ];
285
	a = expr_attrib_spec; ">"; c = expr ->
286
	  exp loc (Xml (t, Pair (a,c)))
287
    | "{"; r = [ expr_record_spec | -> exp loc (RecordLitt []) ]; "}" -> r
288
    | s = STRING2 ->
289
	let s = U.mk s in
290
	exp loc (String (U.start_index s, U.end_index s, s, cst_nil))
291
    | a = IDENT -> exp loc (Var (ident a))
292 293
    | "!"; e = expr ->
	exp loc (Apply (Dot (e, U.mk "get"), cst_nil))
294 295 296
    | i = INT -> exp loc (Integer (Intervals.V.mk i))
    | "`"; a = tag -> a
    | c = char -> exp loc (Char c)
297 298 299
    ]

  ];
300

301
  tag: [ [ a = [ IDENT | keyword ] -> exp loc (Atom (parse_ident a)) ] ];
302 303

  tag_type: [
304
    [ IDENT "_" ->  mk loc (Internal (Types.atom Atoms.any))
305
    | a = [ IDENT | keyword ] -> mk loc (Cst (Atom (parse_ident a)))
306
    | t = ANY_IN_NS -> mk loc (NsT (parse_ident t)) 
307 308 309
    ]
  ];

310
  seq_elem: [
311
    [ x = STRING1 -> 
312
	let s = U.mk x in
313
	`String (loc, U.start_index s, U.end_index s, s)
314
    | e = expr LEVEL "no_appl" -> `Elems (loc,e)
315
    | "!"; e = expr LEVEL "no_appl" -> `Explode e
316 317
    ]
  ];
318 319 320
	
  namespace_binding: [
    [ "namespace"; 
321
	name = [ name = [ IDENT | keyword ]; "=" ->
322 323 324
		   parse_ident name
	       | -> U.mk "" ];
	uri = STRING2 ->
325
	  let ns = Ns.mk (parse_ident uri) in
326 327 328 329 330
	  (name,ns)
    ]
  ];

  
331
  let_binding: [
332 333
    [ "let"; is_fun_decl; OPT "fun"; (f,a,b) = fun_decl ->
	let f = match f with Some x -> x | None -> assert false in
334
	let p = mk loc (PatVar f) in
335
	let abst = { fun_name = Some f; fun_iface = a; fun_body = b } in
336
        let e = exp loc (Abstraction abst) in
337
        (true,p,e)
338 339
    | "let"; p = pat; "="; e = expr -> (false,p,e)
    | "let"; p = pat; ":"; t = pat; "="; e = expr -> (false,p, Forget (e,t))
340 341 342
    ] 
  ];

343
 fun_decl_after_lparen: [
344
(* need an hack to do this, because both productions would
345
   match   [ OPT IDENT; "("; pat ] .... *)
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
   [ 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: [
368 369 370
   [ 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)
371
   ]
372 373
 ];

374
 arrow: [
375
    [ t1 = pat LEVEL "no_arrow"; "->"; t2 = pat -> (t1,t2)]
376 377 378
  ];

  branches: [
379
    [ OPT "|"; l = LIST1 branch SEP "|" -> l ]
380 381 382
  ];

  branch: [
383
    [ p = pat LEVEL "no_arrow"; "->"; e = expr -> (p,e) ]
384 385 386 387
  ];

	  
  regexp: [ 
388 389 390 391 392
    [ x = regexp; "|"; y = regexp -> 
	match (x,y) with
	  | Elem x, Elem y -> Elem (mk loc (Or (x,y)))
	  | _ -> Alt (x,y) 
    ]
393
  | [ x = regexp; y = regexp -> Seq (x,y) ]
394 395 396 397 398
  | [ 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"
    ]
399
  | [ a = IDENT; "::"; x = regexp -> SeqCapture (ident a,x) ]
400 401 402 403
  | [ x = regexp; "*" -> Star x
    | x = regexp; "*?" -> WeakStar x
    | x = regexp; "+" -> Seq (x, Star x)
    | x = regexp; "+?" -> Seq (x, WeakStar x)
404
    | x = regexp; "?" ->  Alt (x, Epsilon)
405
    | x = regexp; "??" -> Alt (Epsilon, x) ]
406
  | [ "("; x = LIST1 regexp SEP ","; ")" ->
407 408 409 410 411 412 413 414 415 416 417
	(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))
418 419
    | "("; a = IDENT; ":="; c = expr; ")" -> 
	Elem (mk loc (Constant ((ident a,c))))
420
    | IDENT "PCDATA" -> string_regexp
421
    | i = STRING1; "--"; j = STRING1 ->
422 423
	let i = Chars.V.mk_int (parse_char loc i)
	and j = Chars.V.mk_int (parse_char loc j) in
424
        Elem (mk loc (Internal (Types.char (Chars.char_class i j))))
425
    |  s = STRING1 ->
426
	List.fold_right
427
	  (fun c accu -> 
428
	     let c = Chars.V.mk_int c in
429
	     let c = Chars.atom c in
430 431
	     Seq (Elem (mknoloc (Internal (Types.char c))), accu))
	  (seq_of_string s)
432 433
	  Epsilon ]
    | [ e = pat LEVEL "simple" -> Elem e
434 435 436 437
    ]
  ];

  pat: [ 
438
      [ x = pat; IDENT "where"; 
439
        b = LIST1 [ a = IDENT; "="; y = pat -> (ident a,y) ] SEP "and"
440 441
            -> mk loc (Recurs (x,b)) ]
    | RIGHTA [ x = pat; "->"; y = pat -> mk loc (Arrow (x,y)) ]
442
    | "no_arrow" [ x = pat; "|"; y = pat -> mk loc (Or (x,y)) ] 
443
    | "simple" [ x = pat; "&"; y = pat -> mk loc (And (x,y)) 
444
	       | x = pat; "\\"; y = pat -> mk loc (Diff (x,y)) ]
445
    | 
446 447
      [ "{"; r = record_spec; "}" -> mk loc (Record (true,r))
      | "{|"; r = record_spec; "|}" -> mk loc (Record (false,r))
448 449 450 451 452
      | "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))
453
      | IDENT "_" -> mk loc (Internal Types.any)
454
      | "("; a = IDENT; ":="; c = expr; ")" -> 
455
	  mk loc (Constant (ident a,c))
456
      | schema = IDENT; "#"; typ = [ IDENT | keyword ];
457 458 459 460 461 462 463 464 465 466
        k = OPT [ "as"; k = [ "element" | "type" | "attribute" ] -> k ] ->
          let kind =
            match k with
            | None -> `Any
            | Some "element" -> `Element
            | Some "type" -> `Type
            | Some "attribute" -> `Attribute
            | _ -> assert false
          in
          mk loc (SchemaVar (kind, schema, typ))
467 468
      | a = IDENT -> 
	  mk loc (PatVar (ident a))
469
      | i = INT ; "--"; j = INT -> 
470 471
          let i =  Intervals.V.mk i 
	  and j =  Intervals.V.mk j in
472 473
          mk loc (Internal (Types.interval (Intervals.bounded i j)))
      | i = INT -> 
474
          let i =  Intervals.V.mk i  in
475
          mk loc (Internal (Types.interval (Intervals.atom i)))
476
      | "*"; "--"; j = INT ->
477
	  let j =  Intervals.V.mk j in
478
          mk loc (Internal (Types.interval (Intervals.left j)))
479
      | i = INT; "--"; "*" ->
480
	  let i = Intervals.V.mk i in
481
          mk loc (Internal (Types.interval (Intervals.right i)))
482 483
      | i = char ->
          mk loc (Internal (Types.char (Chars.char_class i i)))
484
      | i = char ; "--"; j = char ->
485
          mk loc (Internal (Types.char (Chars.char_class i j)))
486
      | "`"; c = tag_type -> c
487 488 489
      | "("; l = LIST1 pat SEP ","; ")" -> multi_prod loc l
      | "["; r = [ r = regexp -> r | -> Epsilon ];
             q = [ ";"; q = pat -> q 
490
                 | -> pat_nil ]; 
491
             "]" -> mk loc (Regexp (r,q))
492
      | "<"; t =
493 494
            [ x = tag_type -> x
            | "("; t = pat; ")" -> t ];
495
	a = attrib_spec; ">"; c = pat ->
496
          mk loc (XmlT (t, multi_prod loc [a;c]))
497
      | s = STRING2 ->
498 499 500 501 502 503
	  let s = 
	    List.map 
	      (fun c -> 
		 mknoloc (Internal
			     (Types.char
				(Chars.atom
504
				   (Chars.V.mk_int c))))) 
505 506
	      (seq_of_string s) in
	  let s = s @ [mknoloc (Internal (Sequence.nil_type))] in
507
	  multi_prod loc s
508 509 510 511 512
      ]
    
  ];

  record_spec:
513
    [ [ r = LIST0 [ l = [IDENT | keyword ]; "=";
514
                  o = [ "?" -> true | -> false]; 
515 516
                  x = pat -> 
		    let x = if o then mk loc (Optional x) else x in
517
		    (label l, x)
518
                ] SEP ";" ->
519
	  r
520 521
      ] ];
  
522 523
  char:
    [ 
524
      [ c = STRING1 -> Chars.V.mk_int (parse_char loc c) ]
525 526 527
    ];
     

528
  attrib_spec:
529
    [ [ r = LIST0 [ l = [IDENT | keyword ]; "=";
530 531 532 533 534 535
                  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)) 
536 537 538 539
      | "("; t = pat; ")" -> t 
      | "{"; r = record_spec; "}" -> mk loc (Record (true,r))
      | "{|"; r = record_spec; "|}" -> mk loc (Record (false,r))
      ] ];
540 541 542

  expr_record_spec:
    [ [ r = LIST1
543
	      [ l = [IDENT | keyword ]; "="; x = expr LEVEL "no_appl" -> 
544
		  (label l,x) ] 
545
	       SEP ";"  ->
546
	  exp loc (RecordLitt r)
547
      ] ];
548

549
  expr_attrib_spec:
550
    [ [ r = LIST1
551
	      [ l = [IDENT | keyword ]; "="; x = expr LEVEL "no_appl"; OPT ";" -> 
552 553 554
		  (label l,x) ] ->
	  exp loc (RecordLitt r)
      ]
555
    | [ e = expr LEVEL "no_appl" -> e 
556
      | -> exp loc (RecordLitt []) 
557 558 559 560
      ] 
    ];
END

561
let pat = Grammar.Entry.parse pat
562 563
and expr = Grammar.Entry.parse expr
and prog = Grammar.Entry.parse prog
564 565
and top_phrases = Grammar.Entry.parse top_phrases

566
let sync () = 
567
  match !Ulexer.lexbuf with
568 569 570
    | None -> ()
    | Some lb ->
	let rec aux () =
571
	  match !Ulexer.last_tok with
572 573
	    | ("",";;") | ("EOI","") -> ()
	    | _ -> 
574
		Ulexer.last_tok := fst (Ulexer.token lb); 
575 576 577
		aux ()
	in
	aux ()
578 579 580 581

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