parser.ml 20.1 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 14 15 16 17
let tloc (i,j) = (i.Lexing.pos_cnum,j.Lexing.pos_cnum)
let nopos = (Lexing.dummy_pos, Lexing.dummy_pos)


let mk loc x = Location.mk (tloc loc) x

18 19
exception Error of string
let error (i,j) s = Location.raise_loc i j (Error s)
20
let error loc s = error (tloc loc) s
21

22
let gram    = Grammar.gcreate Ulexer.lex
23

24 25 26 27 28 29 30 31 32 33 34
let id_dummy = U.mk "$$$"

let ident s =
  let b = Buffer.create (String.length s) in
  let rec aux i =
    if (i = String.length s) then Buffer.contents b
    else match s.[i] with
      | '\\' -> assert (s.[i+1] = '.'); Buffer.add_char b '.'; aux (i+2)
      | c -> Buffer.add_char b c; aux (i+1)
  in
  aux 0
35

36 37
let label s = U.mk (ident s)
let ident s = U.mk (ident s)
38

39
let prog    = Grammar.Entry.create gram "prog"
40
let top_phrases   = Grammar.Entry.create gram "toplevel phrases"
41 42 43
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"
44
let keyword = Grammar.Entry.create gram "keyword"
45
		
46 47
let lop pos = loc_of_pos (tloc pos)
let exp pos e = LocatedExpr (lop pos,e)
48

49 50 51 52 53
let rec multi_prod loc = function
  | [ x ] -> x
  | x :: l -> mk loc (Prod (x, multi_prod loc l))
  | [] -> assert false
      
54
let rec tuple = function
55
  | [ x ] -> x
56
  | x :: l -> Pair (x, tuple l)
57
  | [] -> assert false
58 59

let tuple_queue = 
60
  List.fold_right (fun x q -> Pair (x, q))
61 62


63
let char = mknoloc (Internal (Types.char Chars.any))
64
let string_regexp = Star (Elem char)
65
	       
66
let seq_of_string s =
67 68
  let s = Encodings.Utf8.mk s in
  let rec aux i j =
69 70
    if Encodings.Utf8.equal_index i j then []
    else let (c,i) = Encodings.Utf8.next s i in c :: (aux i j)
71
  in
72
  aux (Encodings.Utf8.start_index s) (Encodings.Utf8.end_index s)
73

74

75
let parse_char loc s =
76 77
  match seq_of_string s with
    | [ c ] -> c
78
    | _ -> error loc "Character litteral must have length 1"
79

80 81
let include_stack = ref []

82 83 84 85
let protect_exn f g =
  try let x = f () in g (); x
  with e -> g (); raise e

86 87 88 89
let localize_exn f = 
  try f ()
  with
  | Stdpp.Exc_located (_, (Location _ as e)) -> raise e
90 91
(*  | Stdpp.Exc_located ((i,j), e) -> raise_loc i j e *)
  | Stdpp.Exc_located ((i,j), e) -> raise_loc i.Lexing.pos_cnum j.Lexing.pos_cnum e
92

93 94 95 96
let is_fun_decl =
   Grammar.Entry.of_parser gram "[is_fun_decl]"
     (fun strm ->
       match Stream.npeek 3 strm with
97 98
	 | [ ("", "fun"); ("IDENT", _); ("", "(") ]
	 | [ ("IDENT", _) ; ("", "(") ; _ ] -> ()
99 100 101
	 | _ -> raise Stream.Failure
     )

102 103 104 105 106 107 108 109 110
let is_capture =
   Grammar.Entry.of_parser gram "[is_capture]"
     (fun strm ->
       match Stream.npeek 2 strm with
	 | [ ("IDENT", _) ; ("", "::") ; _ ] -> ()
	 | _ -> raise Stream.Failure
     )


111 112 113 114 115
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
116

117
let apply_op2_noloc op e1 e2 = Apply (Apply (Var (ident op), e1), e2)
118
let apply_op2 loc op e1 e2 = exp loc (apply_op2_noloc op e1 e2)
119
 
120

121
EXTEND
122
  GLOBAL: top_phrases prog expr pat regexp keyword;
123 124

  top_phrases: [
125
    [ l = LIST0 phrase; ";;" -> List.flatten l  ]
126
  ];
127 128

  prog: [
129
    [ l = LIST0 [ p = phrase ; OPT ";;" -> p ]; EOI -> List.flatten l ]
130 131 132
  ];

  phrase: [
133 134 135 136
    [ (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"->
137
        [ mk loc (EvalStatement (exp loc (Match (e1,[p,e2])))) ]
138
    | "type"; x = located_ident; "="; t = pat -> [ mk loc (TypeDecl (x,t)) ]
139 140
    | "using"; name = IDENT; "="; cu = [ IDENT | STRING2 ] ->
	[ mk loc (Using (U.mk name, U.mk cu)) ]
141
    | "schema"; name = IDENT; 
142
      p = OPT [ "("; name = [ IDENT | keyword ]; ")" -> ident name ];
143
      "="; uri = STRING2 ->
144
	protect_op "schema";
145
        [ mk loc (SchemaDecl (U.mk name, uri, p)) ]
146 147 148 149 150
    | (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)) ]
151
    | "debug"; d = debug_directive -> [ mk loc (Directive (`Debug d)) ]
152 153 154 155 156 157 158 159
    | "#"; IDENT "verbose" -> [ mk loc (Directive `Verbose) ]
    | "#"; IDENT "silent" -> [ mk loc (Directive `Silent) ]
    | "#"; IDENT "utf8" -> Ulexer.enc := Ulexing.Utf8; [ ]
    | "#"; IDENT "latin1" -> Ulexer.enc := Ulexing.Latin1; [ ]
    | "#"; IDENT "ascii" -> Ulexer.enc := Ulexing.Ascii; [ ]
    | "#"; IDENT "quit" -> [ mk loc (Directive `Quit) ]
    | "#"; IDENT "env" -> [ mk loc (Directive `Env) ]
    | "#"; IDENT "print_type"; t = pat ->
160
        [ mk loc (Directive (`Print_type t)) ]
161 162 163
    | "#"; IDENT "dump_value"; e = expr -> [ mk loc (Directive (`Dump e)) ]
    | "#"; IDENT "reinit_ns" -> [ mk loc (Directive `Reinit_ns) ]
    | "#"; IDENT "help" -> [ mk loc (Directive `Help) ]
164
    | "include"; s = STRING2 ->
165 166 167 168
	let s = 
	  if Filename.is_relative s 
	  then Filename.concat (Location.current_dir ()) s
	  else s in
169 170
	protect_op "File inclusion";
	(* avoid looping; should issue an error ? *)
171 172
	(* it is possible to have looping with x/../x/../x/.. ....
	   Need to canonicalize filename *)
173 174 175 176
	if List.mem s !include_stack then [] 
	else (
	  include_stack := s :: !include_stack;
	  Location.push_source (`File s);
177 178
	  let saved_enc = !Ulexer.enc in
	  Ulexer.enc := Ulexing.Latin1;
179 180 181
	  protect_exn
	    (fun () ->
	       let chan = open_in s in
182 183 184
	       protect_exn
		 (fun () ->
		    let input = Stream.of_channel chan in
185
		    localize_exn (fun () -> Grammar.Entry.parse prog input))
186
		 (fun () -> close_in chan))
187
	    (fun () ->
188
	       Ulexer.enc := saved_enc;
189 190
	       Location.pop_source ();
	       include_stack := List.tl !include_stack)
191
	)
192
    ] | 
193
    [ e = expr -> [ mk loc (EvalStatement e) ]
194 195 196 197
    ]
  ];

  debug_directive: [
198 199 200 201 202
    [ 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)
203
    | IDENT "explain"; t0 = pat; t = pat; e = expr -> `Explain (t0,t,e)
204 205
    | IDENT "single"; t = pat -> `Single t
    | IDENT "approx"; p = pat; t = pat -> `Approx (p,t)
206
    ]
207
  ];
208

209 210 211 212 213
  keyword: [
    [ a = 
	[ "map" | "match" | "with" | "try" | "xtransform"
	| "if" | "then"	| "else"
	| "transform" | "fun" | "in"
214
	| "let" | "type" | "debug" | "include"
215
        | "and" | "validate" | "schema" | "namespace" | "ref" | "alias"
216
	| "not" | "as" | "where"
217 218 219 220 221
	]
	-> a
    ]
  ];

222 223
  expr: [
    "top" RIGHTA
224 225
    [ "match"; e = SELF; "with"; b = branches -> 
	exp loc (Match (e,b))
226
    | "try"; e = SELF; "with"; b = branches -> 
227
	exp loc (Try (e,b))
228 229 230 231
    | "map"; e = SELF; "with"; b = branches -> 
	exp loc (Map (e,b))
    | "xtransform"; e = SELF; "with"; b = branches -> 
	exp loc (Xtrans (e,b))
232
    | "if"; e = SELF; "then"; e1 = SELF; "else"; e2 = SELF ->
233
	exp loc (if_then_else e e1 e2)
234
    | "transform"; e = SELF; "with"; b = branches -> 
235
	exp loc (Transform (e,b))
236 237
    | "validate"; e = SELF; "with"; (schema, typ) = schema_ref ->
        exp loc (Validate (e, schema, typ))
238
    | "fun"; (f,a,b) = fun_decl ->
239
	exp loc (Abstraction { fun_name = f; fun_iface = a; fun_body = b })
240
    | (_,p,e1) = let_binding; "in"; e2 = expr LEVEL "top"->
241
        exp loc (Match (e1,[p,e2]))
242 243
    | (name,ns) = namespace_binding; "in"; e2 = expr LEVEL "top" ->
	exp loc (NamespaceIn (name, ns, e2))
244
    | e = expr; ":"; p = pat ->
245
	exp loc (Forget (e,p))
246 247
    | e = expr; ":"; "?"; p = pat ->
	exp loc (Check (e,p))
248 249 250 251
    | e1 = expr; ";"; e2 = expr ->
	exp loc (Match (e1, [pat_nil,e2]))
    | "ref"; p = pat; e = expr ->
	exp loc (Ref (e,p))
252
    | "not"; e = expr -> exp loc (logical_not e)
253 254 255
    ]
    |
    [ e1 = expr; ":="; e2 = expr ->
256
	exp loc (Apply (Dot (e1, U.mk "set", []), e2))
257
    ]
258 259 260 261 262 263
    | 
    [ e1 = expr; op = ["=" | "<=" | "<<" | ">>" | ">=" ]; e2 = expr -> 
	let op = match op with
	  | "<<" -> "<"
	  | ">>" -> ">"
	  | s -> s in
264
	apply_op2 loc op e1 e2
265 266
    ]

267
    | 
268
    [ e1 = expr; op = ["+" | "-" | "@" ]; e2 = expr -> apply_op2 loc op e1 e2
269
    | e1 = expr; "||"; e2 = expr -> exp loc (logical_or e1 e2)
270
    | e = expr; "\\"; l = [IDENT | keyword ] -> 
271
	exp loc (RemoveField (e, label l)) 
272 273
    ]
    |
274
    [ e1 = expr; op = ["*"]; e2 = expr -> apply_op2 loc op e1 e2
275
    | e1 = expr; "&&"; e2 = expr -> exp loc (logical_and e1 e2)
276
    | e = expr; op = "/"; p = pat LEVEL "simple" ->
277 278 279
	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
280
	let re = Star(Alt(SeqCapture(noloc,id_dummy,Elem p), Elem any)) in
281
	let ct = mk loc (Regexp re) in
282
        let p = mk loc (XmlT (tag, multi_prod loc [att;ct])) in
283
	let b = (p,  Var id_dummy) in
284
	exp loc (Transform (e,[b]))
285
    ]
286
    |
287 288 289 290
    [ e = expr;  "."; l = [IDENT | keyword ];
      tyargs = [ "with"; "{"; pl = LIST0 pat; "}" -> pl | -> [] ]
      -> 
	exp loc (Dot (e, label l,tyargs)) 
291
    ]  
292 293 294
    | [ 
      e1 = SELF; IDENT "div"; e2 = expr -> apply_op2 loc "/" e1 e2
    | e1 = SELF; IDENT "mod"; e2 = expr -> apply_op2 loc "mod" e1 e2
295
    | e1 = SELF; e2 = expr -> exp loc (Apply (e1,e2))
296 297
    ]

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

  ];
333

334
  tag: [ [ a = [ IDENT | keyword ] -> exp loc (Atom (ident a)) ] ];
335 336

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

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

  
364
  let_binding: [
365 366
    [ "let"; is_fun_decl; OPT "fun"; (f,a,b) = fun_decl ->
	let f = match f with Some x -> x | None -> assert false in
367
	let p = mk loc (PatVar (None, snd f)) in
368
	let abst = { fun_name = Some f; fun_iface = a; fun_body = b } in
369
        let e = exp loc (Abstraction abst) in
370
        (true,p,e)
371 372
    | "let"; p = pat; "="; e = expr -> (false,p,e)
    | "let"; p = pat; ":"; t = pat; "="; e = expr -> (false,p, Forget (e,t))
373 374
    | "let"; p = pat; ":"; "?"; t = pat; "="; e = expr -> 
	(false,p, Check (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
   [ 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) ]; 
387 388 389 390 391 392 393 394 395
	     ")";
	     others = LIST0 
			[ "(";
			  args = 
			    LIST1 
			      [ arg = pat; ":"; targ = pat -> (arg,targ) ]
			      SEP ",";
			  ")" -> args ];
	     ":"; tres = pat ; 
396
	     "="; body = expr ->
397
	       `Compact (targ1,args,others,tres,body)
398 399 400
	   ] ->
       match res with
	 | `Classic (p2,a,b) -> (p1,p2)::a,b
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
	 | `Compact (targ1,args,others,tres,body) ->
	     let mkfun args =
	       multi_prod nopos (List.map snd args),
	       multi_prod nopos (List.map fst args)
	     in
	     let (tres,body) =
	       List.fold_right
		 (fun args (tres,body) ->
		    let (targ,arg) = mkfun args in
		    let e = Abstraction 
			      { fun_name = None; fun_iface = [targ,tres]; 
				fun_body = [arg,body] } in
		    let t = mknoloc (Arrow (targ,tres)) in
		    (t,e)
		 )
		 others (tres,body) in
	     let (targ,arg) = mkfun ((p1,targ1) :: args) in
	     [(targ,tres)],[(arg,body)]
	      ] ];
420 421 422


 fun_decl: [
423
   [ f = OPT located_ident; "("; (a,b) = fun_decl_after_lparen ->
424
       (f,a,b)
425
   ]
426 427
 ];

428
 arrow: [
429
    [ t1 = pat LEVEL "no_arrow"; "->"; t2 = pat -> (t1,t2)]
430 431 432
  ];

  branches: [
433
    [ OPT "|"; l = LIST1 branch SEP "|" -> l ]
434 435 436
  ];

  branch: [
437
    [ p = pat LEVEL "no_arrow"; "->"; e = expr -> (p,e) ]
438 439 440 441
  ];

	  
  regexp: [ 
442 443 444 445 446
    [ x = regexp; "|"; y = regexp -> 
	match (x,y) with
	  | Elem x, Elem y -> Elem (mk loc (Or (x,y)))
	  | _ -> Alt (x,y) 
    ]
447
  | [ x = regexp; y = regexp -> Seq (x,y) ]
448 449 450 451 452
  | [ 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"
    ]
453
  | [ a = IDENT; "::"; x = regexp -> SeqCapture (lop loc,ident a,x) ] 
454 455 456 457
  | [ x = regexp; "*" -> Star x
    | x = regexp; "*?" -> WeakStar x
    | x = regexp; "+" -> Seq (x, Star x)
    | x = regexp; "+?" -> Seq (x, WeakStar x)
458
    | x = regexp; "?" ->  Alt (x, Epsilon)
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
    | x = regexp; "??" -> Alt (Epsilon, x) 
    | x = regexp; "**"; i = INT ->
	let rec aux i accu =
	  if (i = 0) then accu
	  else aux (pred i) (Seq (x, accu))
	in
	let i = 
	  try 
	    let i = int_of_string i in
	    if (i > 1024) then raise Exit else i
	      (* We cannot handle type that huge... *)
	  with Failure _ | Exit -> error loc "Repetition number too large"
	in
	if (i <= 0) then 
	  error loc "Repetition number must be a positive integer";
	aux i Epsilon
    ]
476
  | [ "("; x = LIST1 regexp SEP ","; ")" ->
477 478 479 480 481 482 483 484 485 486 487
	(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))
488 489
    | "("; a = IDENT; ":="; c = expr; ")" -> 
	Elem (mk loc (Constant ((ident a,c))))
490
    | "/"; p = pat LEVEL "simple" -> Guard p
491
    | IDENT "PCDATA" -> string_regexp
492
    | i = STRING1; "--"; j = STRING1 ->
493 494
	let i = Chars.V.mk_int (parse_char loc i)
	and j = Chars.V.mk_int (parse_char loc j) in
495
        Elem (mk loc (Internal (Types.char (Chars.char_class i j))))
496
    |  s = STRING1 ->
497
	List.fold_right
498
	  (fun c accu -> 
499
	     let c = Chars.V.mk_int c in
500
	     let c = Chars.atom c in
501 502
	     Seq (Elem (mknoloc (Internal (Types.char c))), accu))
	  (seq_of_string s)
503 504
	  Epsilon ]
    | [ e = pat LEVEL "simple" -> Elem e
505 506 507
    ]
  ];

508
  schema_ref: [
509
    [ schema = IDENT; "."; typ = [ IDENT | keyword ] -> (U.mk schema, ident typ)
510 511 512
    ]
  ];

513 514
  located_ident: [ [ a = IDENT -> (lop loc,ident a) ] ];

515
  pat: [ 
516
      [ x = pat; "where"; 
517 518
        b = LIST1 [ (la,a) = located_ident; "="; y = pat -> 
		      (la,a,y) ] SEP "and"
519 520
            -> mk loc (Recurs (x,b)) ]
    | RIGHTA [ x = pat; "->"; y = pat -> mk loc (Arrow (x,y)) ]
521
    | "no_arrow" [ x = pat; "|"; y = pat -> mk loc (Or (x,y)) ] 
522
    | "simple" [ x = pat; "&"; y = pat -> mk loc (And (x,y)) 
523
	       | x = pat; "\\"; y = pat -> mk loc (Diff (x,y)) ]
524
    | 
525 526
      [ "{"; r = record_spec; "}" -> mk loc (Record (true,r))
      | "{|"; r = record_spec; "|}" -> mk loc (Record (false,r))
527 528
      | "ref"; p = pat ->
	  let get_fun = mk loc (Arrow (pat_nil, p)) 
529 530 531
	  and set_fun = mk loc (Arrow (p, pat_nil))in
	  let fields = 
	    [ label "get", (get_fun, None); label "set", (set_fun, None) ] in
532
	  mk loc (Record (false, fields))
533
      | IDENT "_" -> mk loc (Internal Types.any)
534
      | "("; a = IDENT; ":="; c = expr; ")" -> 
535
	  mk loc (Constant (ident a,c))
536 537
      | "!"; a = IDENT ->
	  mk loc (Internal (Types.abstract (Types.Abstract.atom a)))
538
      | cu = OPT [ cu = IDENT; "." -> U.mk cu ]; a = IDENT ->
539
	  mk loc (PatVar (cu, ident a))
540
      | i = INT ; "--"; j = INT -> 
541 542
          let i =  Intervals.V.mk i 
	  and j =  Intervals.V.mk j in
543 544
          mk loc (Internal (Types.interval (Intervals.bounded i j)))
      | i = INT -> 
545
          let i =  Intervals.V.mk i  in
546
          mk loc (Internal (Types.interval (Intervals.atom i)))
547
      | "*"; "--"; j = INT ->
548
	  let j =  Intervals.V.mk j in
549
          mk loc (Internal (Types.interval (Intervals.left j)))
550
      | i = INT; "--"; "*" ->
551
	  let i = Intervals.V.mk i in
552
          mk loc (Internal (Types.interval (Intervals.right i)))
553 554
      | i = char ->
          mk loc (Internal (Types.char (Chars.char_class i i)))
555
      | i = char ; "--"; j = char ->
556
          mk loc (Internal (Types.char (Chars.char_class i j)))
557
      | "`"; c = tag_type -> c
558 559
      | "("; l = LIST1 pat SEP ","; ")" -> multi_prod loc l
      | "["; r = [ r = regexp -> r | -> Epsilon ];
560 561 562 563 564 565 566 567 568 569
             q = [ ";"; q = pat -> Some q
                 | -> None ]; 
             "]" -> 
	       let r = match q with
		 | Some q -> 	
		     let any = mk loc (Internal (Types.any)) in
		     Seq(r,Seq(Guard q, Star (Elem any)))
		 | None -> r
	       in
	       mk loc (Regexp r)
570
      | "<"; t =
571 572
            [ x = tag_type -> x
            | "("; t = pat; ")" -> t ];
573
	a = attrib_spec; ">"; c = pat ->
574
          mk loc (XmlT (t, multi_prod loc [a;c]))
575
      | s = STRING2 ->
576 577 578 579 580 581
	  let s = 
	    List.map 
	      (fun c -> 
		 mknoloc (Internal
			     (Types.char
				(Chars.atom
582
				   (Chars.V.mk_int c))))) 
583 584
	      (seq_of_string s) in
	  let s = s @ [mknoloc (Internal (Sequence.nil_type))] in
585
	  multi_prod loc s
586 587 588 589
      ]
    
  ];

590 591
  or_else : [ [ OPT [ "else"; y = pat -> y ]  ] ];

592
  opt_field_pat: [ [ OPT [ "=";
593
                  o = [ "?" -> true | -> false]; 
594 595 596 597 598 599
                  x = pat;  y = or_else -> (o,x,y) ] ] ];

  record_spec:
    [ [ r = LIST0 [ l = [IDENT | keyword ]; f = opt_field_pat ->
		      let (o,x,y) =
			match f with
600
			  | None -> (false, mknoloc (PatVar (None,ident l)), None)
601 602 603 604 605
			  | Some z -> z
		      in
		      let x = if o then mk loc (Optional x) else x in
		      (label l, (x,y))
                  ] SEP ";" ->
606
	  r
607 608
      ] ];
  
609 610
  char:
    [ 
611
      [ c = STRING1 -> Chars.V.mk_int (parse_char loc c) ]
612 613 614
    ];
     

615
  attrib_spec:
616 617 618
    [ [ r = LIST0 [ l = [IDENT | keyword ]; f = opt_field_pat; OPT ";" -> 
		      let (o,x,y) =
			match f with
619
			  | None -> (false, mknoloc (PatVar (None,ident l)), None)
620 621 622 623 624
			  | Some z -> z
		      in
		      let x = if o then mk loc (Optional x) else x in
		      (label l, (x, y))
                  ] ->
625
	  mk loc (Record (true,r)) 
626 627 628 629
      | "("; t = pat; ")" -> t 
      | "{"; r = record_spec; "}" -> mk loc (Record (true,r))
      | "{|"; r = record_spec; "|}" -> mk loc (Record (false,r))
      ] ];
630

631 632
  opt_field_expr: [ [ OPT [ "="; x = expr LEVEL "no_appl" -> x ] ] ];

633 634
  expr_record_spec:
    [ [ r = LIST1
635 636
	      [ l = [IDENT | keyword ]; 
		x = opt_field_expr ->
637
		  let x = match x with Some x -> x | None ->  Var (ident l) in
638
		  (label l,x) ] 
639
	       SEP ";"  ->
640
	  exp loc (RecordLitt r)
641
      ] ];
642

643
  expr_attrib_spec:
644
    [ [ r = LIST1
645 646
	      [ l = [IDENT | keyword ]; 
		x = opt_field_expr; OPT ";" ->
647
		  let x = match x with Some x -> x | None ->  Var (ident l) in
648 649 650
		  (label l,x) ] ->
	  exp loc (RecordLitt r)
      ]
651
    | [ e = expr LEVEL "no_appl" -> e 
652
      | -> exp loc (RecordLitt []) 
653 654 655 656
      ] 
    ];
END

657 658 659
module Hook = struct
  let expr = expr
  let pat = pat
660
  let keyword = keyword
661 662
end

663
let pat = Grammar.Entry.parse pat
664 665
and expr = Grammar.Entry.parse expr
and prog = Grammar.Entry.parse prog
666 667
and top_phrases = Grammar.Entry.parse top_phrases

668
let sync () = 
669
  match !Ulexer.lexbuf with
670 671 672
    | None -> ()
    | Some lb ->
	let rec aux () =
673
	  match !Ulexer.last_tok with
674 675
	    | ("",";;") | ("EOI","") -> ()
	    | _ -> 
676
		Ulexer.last_tok := fst (Ulexer.token lb); 
677 678 679
		aux ()
	in
	aux ()
680 681 682 683

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