parser.ml 17 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 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"
let const   = Grammar.Entry.create gram "scalar constant"
		
34 35
let exp pos e = LocatedExpr (loc_of_pos pos,e)

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

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


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

64

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

70 71
let include_stack = ref []

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

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

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

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

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

96
  uident: [ [ x = IDENT -> parse_ident x ] ];
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 = uident; "="; t = pat -> [ mk loc (TypeDecl (x,t)) ]
105
    | "schema"; name = IDENT; "="; uri = STRING2 ->
106
	protect_op "schema";
107
        let schema_doc = Schema_xml.pxp_tree_of uri in
108 109
        let schema = Schema_parser.parse_schema schema_doc 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 "#reinit_ns" -> [ mk loc (Directive `Reinit_ns) ]
122
    | "include"; s = STRING2 ->
123 124 125 126
	let s = 
	  if Filename.is_relative s 
	  then Filename.concat (Location.current_dir ()) s
	  else s in
127 128
	protect_op "File inclusion";
	(* avoid looping; should issue an error ? *)
129 130
	(* it is possible to have looping with x/../x/../x/.. ....
	   Need to canonicalize filename *)
131 132 133 134
	if List.mem s !include_stack then [] 
	else (
	  include_stack := s :: !include_stack;
	  Location.push_source (`File s);
135 136
	  let saved_enc = !Ulexer.enc in
	  Ulexer.enc := Ulexing.Latin1;
137 138 139
	  protect_exn
	    (fun () ->
	       let chan = open_in s in
140 141 142 143 144
	       protect_exn
		 (fun () ->
		    let input = Stream.of_channel chan in
		    Grammar.Entry.parse prog input)
		 (fun () -> close_in chan))
145
	    (fun () ->
146
	       Ulexer.enc := saved_enc;
147 148
	       Location.pop_source ();
	       include_stack := List.tl !include_stack)
149
	)
150
    ] | 
151
    [ e = expr -> [ mk loc (EvalStatement e) ]
152 153 154 155
    ]
  ];

  debug_directive: [
156 157 158 159 160
    [ 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)
161
    ]
162
  ];
163

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

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

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

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

  ];
300

301
  tag: [ [ a = [ IDENT | keyword ] -> 
302
	     Const_atom (parse_ident a) ] ];
303 304

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

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

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

344
 fun_decl_after_lparen: [
345
(* need an hack to do this, because both productions would
346
   match   [ OPT IDENT; "("; pat ] .... *)
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
   [ 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: [
369
   [ f = OPT uident; "("; (a,b) = fun_decl_after_lparen ->
370
	    (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
  | [ a = IDENT; "::"; x = regexp -> SeqCapture (ident a,x) ]
395 396 397 398
  | [ x = regexp; "*" -> Star x
    | x = regexp; "*?" -> WeakStar x
    | x = regexp; "+" -> Seq (x, Star x)
    | x = regexp; "+?" -> Seq (x, WeakStar x)
399
    | x = regexp; "?" ->  Alt (x, Epsilon)
400 401
    | x = regexp; "??" -> Alt (Epsilon, x) ]
  | [ "("; x = regexp; ")" -> x
402
    | "("; a = IDENT; ":="; c = const; ")" -> 
403
	Elem (mk loc (Constant ((ident a,c))))
404
    | IDENT "PCDATA" -> string_regexp
405
    | i = STRING1; "--"; j = STRING1 ->
406 407
	let i = Chars.V.mk_int (parse_char loc i)
	and j = Chars.V.mk_int (parse_char loc j) in
408
        Elem (mk loc (Internal (Types.char (Chars.char_class i j))))
409
    |  s = STRING1 ->
410
	List.fold_right
411
	  (fun c accu -> 
412
	     let c = Chars.V.mk_int c in
413
	     let c = Chars.atom c in
414 415
	     Seq (Elem (mknoloc (Internal (Types.char c))), accu))
	  (seq_of_string s)
416
	  Epsilon 
417 418 419 420 421
    | e = pat LEVEL "simple" -> Elem e
    ]
  ];

  pat: [ 
422 423
      [ x = pat; IDENT "where"; 
        b = LIST1 [ a = uident; "="; y = pat -> (a,y) ] SEP "and"
424 425
            -> mk loc (Recurs (x,b)) ]
    | RIGHTA [ x = pat; "->"; y = pat -> mk loc (Arrow (x,y)) ]
426
    | "no_arrow" [ x = pat; "|"; y = pat -> mk loc (Or (x,y)) ] 
427
    | "simple" [ x = pat; "&"; y = pat -> mk loc (And (x,y)) 
428
	       | x = pat; "\\"; y = pat -> mk loc (Diff (x,y)) ]
429
    | 
430 431
      [ "{"; r = record_spec; "}" -> mk loc (Record (true,r))
      | "{|"; r = record_spec; "|}" -> mk loc (Record (false,r))
432 433 434 435 436
      | "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))
437 438
      | IDENT "_" -> mk loc (Internal Types.any)
      | "("; a = IDENT; ":="; c = const; ")" -> 
439
	  mk loc (Constant (ident a,c))
440
      | schema = IDENT; "#"; typ = [ IDENT | keyword ];
441 442 443 444 445 446 447 448 449 450
        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))
451 452
      | a = uident -> 
	  mk loc (PatVar a)
453
      | i = INT ; "--"; j = INT -> 
454 455
          let i =  Intervals.V.mk i 
	  and j =  Intervals.V.mk j in
456 457
          mk loc (Internal (Types.interval (Intervals.bounded i j)))
      | i = INT -> 
458
          let i =  Intervals.V.mk i  in
459
          mk loc (Internal (Types.interval (Intervals.atom i)))
460
      | "*"; "--"; j = INT ->
461
	  let j =  Intervals.V.mk j in
462
          mk loc (Internal (Types.interval (Intervals.left j)))
463
      | i = INT; "--"; "*" ->
464
	  let i = Intervals.V.mk i in
465
          mk loc (Internal (Types.interval (Intervals.right i)))
466 467
      | i = char ->
          mk loc (Internal (Types.char (Chars.char_class i i)))
468
      | i = char ; "--"; j = char ->
469
          mk loc (Internal (Types.char (Chars.char_class i j)))
470 471 472
      | "`"; c = tag_type -> c
      | c = const -> 
	  (match c with
473 474
	     | Const_atom l -> mk loc (AtomT l)
	     | Const_internal c -> mk loc (Internal (Types.constant c))
475
	  )
476 477 478
      | "("; l = LIST1 pat SEP ","; ")" -> multi_prod loc l
      | "["; r = [ r = regexp -> r | -> Epsilon ];
             q = [ ";"; q = pat -> q 
479
                 | -> pat_nil ]; 
480
             "]" -> mk loc (Regexp (r,q))
481
      | "<"; t =
482 483
            [ x = tag_type -> x
            | "("; t = pat; ")" -> t ];
484
	a = attrib_spec; ">"; c = pat ->
485
          mk loc (XmlT (t, multi_prod loc [a;c]))
486
      | s = STRING2 ->
487 488 489 490 491 492
	  let s = 
	    List.map 
	      (fun c -> 
		 mknoloc (Internal
			     (Types.char
				(Chars.atom
493
				   (Chars.V.mk_int c))))) 
494 495
	      (seq_of_string s) in
	  let s = s @ [mknoloc (Internal (Sequence.nil_type))] in
496
	  multi_prod loc s
497 498 499 500 501
      ]
    
  ];

  record_spec:
502
    [ [ r = LIST0 [ l = [IDENT | keyword ]; "=";
503
                  o = [ "?" -> true | -> false]; 
504 505
                  x = pat -> 
		    let x = if o then mk loc (Optional x) else x in
506
		    (label l, x)
507
                ] SEP ";" ->
508
	  r
509 510
      ] ];
  
511 512
  char:
    [ 
513
      [ c = STRING1 -> Chars.V.mk_int (parse_char loc c) ]
514 515 516
    ];
     

517 518
  const:
    [ 
519
      [ i = INT -> Const_internal (Types.Integer (Intervals.V.mk i))
520 521
      | "`"; a = tag -> a
      | c = char -> Const_internal (Types.Char c) ]
522 523 524 525
    ];


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

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

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

558
let pat = Grammar.Entry.parse pat
559 560
and expr = Grammar.Entry.parse expr
and prog = Grammar.Entry.parse prog
561 562
and top_phrases = Grammar.Entry.parse top_phrases

563
let sync () = 
564
  match !Ulexer.lexbuf with
565 566 567
    | None -> ()
    | Some lb ->
	let rec aux () =
568
	  match !Ulexer.last_tok with
569 570
	    | ("",";;") | ("EOI","") -> ()
	    | _ -> 
571
		Ulexer.last_tok := Ulexer.token lb; 
572 573 574
		aux ()
	in
	aux ()