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
let parse_ident = U.mk
25

26
let id_dummy = ident (U.mk "$$$")
27

28
let label = parse_ident
29
let ident s = ident (parse_ident s)
30

31
let prog    = Grammar.Entry.create gram "prog"
32
let top_phrases   = Grammar.Entry.create gram "toplevel phrases"
33
34
35
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"
36
let keyword = Grammar.Entry.create gram "keyword"
37
		
38
let exp pos e = LocatedExpr (loc_of_pos (tloc pos),e)
39

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

let tuple_queue = 
51
  List.fold_right (fun x q -> Pair (x, q))
52
53


54
let char = mknoloc (Internal (Types.char Chars.any))
55
let string_regexp = Star (Elem char)
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 localize_exn f = 
  try f ()
  with
  | Stdpp.Exc_located (_, (Location _ as e)) -> raise e
81
82
(*  | 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
83

84
85
86
87
let is_fun_decl =
   Grammar.Entry.of_parser gram "[is_fun_decl]"
     (fun strm ->
       match Stream.npeek 3 strm with
88
89
	 | [ ("", "fun"); ("IDENT", _); ("", "(") ]
	 | [ ("IDENT", _) ; ("", "(") ; _ ] -> ()
90
91
92
	 | _ -> raise Stream.Failure
     )

93
94
95
96
97
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
98
99
100

let apply_op2_noloc op e1 e2 = Apply (Apply (Var (parse_ident op), e1), e2)
let apply_op2 loc op e1 e2 = exp loc (apply_op2_noloc op e1 e2)
101
 
102

103
EXTEND
104
  GLOBAL: top_phrases prog expr pat regexp keyword;
105
106

  top_phrases: [
107
    [ l = LIST0 phrase; ";;" -> List.flatten l  ]
108
  ];
109
110

  prog: [
111
    [ l = LIST0 [ p = phrase ; OPT ";;" -> p ]; EOI -> List.flatten l ]
112
113
114
  ];

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

  debug_directive: [
180
181
182
183
184
    [ 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)
185
    | IDENT "explain"; t0 = pat; t = pat; e = expr -> `Explain (t0,t,e)
186
187
    | IDENT "single"; t = pat -> `Single t
    | IDENT "approx"; p = pat; t = pat -> `Approx (p,t)
188
    ]
189
  ];
190

191
192
193
194
195
  keyword: [
    [ a = 
	[ "map" | "match" | "with" | "try" | "xtransform"
	| "if" | "then"	| "else"
	| "transform" | "fun" | "in"
196
	| "let" | "type" | "debug" | "include"
197
        | "and" | "validate" | "schema" | "namespace" | "ref" | "alias"
198
	| "not" | "as" | "where" | "external"
199
200
201
202
203
	]
	-> a
    ]
  ];

204
205
  expr: [
    "top" RIGHTA
206
207
    [ "match"; e = SELF; "with"; b = branches -> 
	exp loc (Match (e,b))
208
    | "try"; e = SELF; "with"; b = branches -> 
209
	exp loc (Try (e,b))
210
211
212
213
    | "map"; e = SELF; "with"; b = branches -> 
	exp loc (Map (e,b))
    | "xtransform"; e = SELF; "with"; b = branches -> 
	exp loc (Xtrans (e,b))
214
    | "if"; e = SELF; "then"; e1 = SELF; "else"; e2 = SELF ->
215
	exp loc (if_then_else e e1 e2)
216
    | "transform"; e = SELF; "with"; b = branches -> 
217
	exp loc (Transform (e,b))
218
219
    | "validate"; e = SELF; "with"; (kind, schema, typ) = schema_ref ->
        exp loc (Validate (e, kind, schema, typ))
220
    | "fun"; (f,a,b) = fun_decl ->
221
	exp loc (Abstraction { fun_name = f; fun_iface = a; fun_body = b })
222
    | "external"; s = STRING2 ->
223
	exp loc (External (s,[]))
224
    | "external"; "{"; s = STRING2; pl = LIST0 pat; "}" ->
225
	exp loc (External (s,pl))
226
    | (_,p,e1) = let_binding; "in"; e2 = expr LEVEL "top"->
227
        exp loc (Match (e1,[p,e2]))
228
229
    | (name,ns) = namespace_binding; "in"; e2 = expr LEVEL "top" ->
	exp loc (NamespaceIn (name, ns, e2))
230
    | e = expr; ":"; p = pat ->
231
	exp loc (Forget (e,p))
232
233
    | e = expr; ":"; "?"; p = pat ->
	exp loc (Check (e,p))
234
235
236
237
    | e1 = expr; ";"; e2 = expr ->
	exp loc (Match (e1, [pat_nil,e2]))
    | "ref"; p = pat; e = expr ->
	exp loc (Ref (e,p))
238
    | "not"; e = expr -> exp loc (logical_not e)
239
240
241
242
    ]
    |
    [ e1 = expr; ":="; e2 = expr ->
	exp loc (Apply (Dot (e1, U.mk "set"), e2))
243
    ]
244
245
246
247
248
249
    | 
    [ e1 = expr; op = ["=" | "<=" | "<<" | ">>" | ">=" ]; e2 = expr -> 
	let op = match op with
	  | "<<" -> "<"
	  | ">>" -> ">"
	  | s -> s in
250
	apply_op2 loc op e1 e2
251
252
    ]

253
    | 
254
    [ e1 = expr; op = ["+" | "-" | "@" ]; e2 = expr -> apply_op2 loc op e1 e2
255
    | e1 = expr; "||"; e2 = expr -> exp loc (logical_or e1 e2)
256
    | e = expr; "\\"; l = [IDENT | keyword ] -> 
257
	exp loc (RemoveField (e, label l)) 
258
259
    ]
    |
260
    [ e1 = expr; op = ["*"]; e2 = expr -> apply_op2 loc op e1 e2
261
    | e1 = expr; "&&"; e2 = expr -> exp loc (logical_and e1 e2)
262
    | e = expr; op = "/"; p = pat LEVEL "simple" ->
263
264
265
266
	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
	let re = Star(Alt(SeqCapture(id_dummy,Elem p), Elem any)) in
267
	let ct = mk loc (Regexp re) in
268
269
270
        let p = mk loc (XmlT (tag, multi_prod loc [att;ct])) in
	let b = (p,  Var (Id.value id_dummy)) in
	exp loc (Transform (e,[b]))
271
    ]
272
    |
273
    [ e = expr;  "."; l = [IDENT | keyword ] -> 
274
	exp loc (Dot (e, label l)) 
275
    ]  
276
277
278
    | [ 
      e1 = SELF; IDENT "div"; e2 = expr -> apply_op2 loc "/" e1 e2
    | e1 = SELF; IDENT "mod"; e2 = expr -> apply_op2 loc "mod" e1 e2
279
    | e1 = SELF; e2 = expr -> exp loc (Apply (e1,e2))
280
281
    ]

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

  ];
317

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

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

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

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

362
 fun_decl_after_lparen: [
363
(* need an hack to do this, because both productions would
364
   match   [ OPT IDENT; "("; pat ] .... *)
365
366
367
368
369
370
   [ 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) ]; 
371
372
373
374
375
376
377
378
379
	     ")";
	     others = LIST0 
			[ "(";
			  args = 
			    LIST1 
			      [ arg = pat; ":"; targ = pat -> (arg,targ) ]
			      SEP ",";
			  ")" -> args ];
	     ":"; tres = pat ; 
380
	     "="; body = expr ->
381
	       `Compact (targ1,args,others,tres,body)
382
383
384
	   ] ->
       match res with
	 | `Classic (p2,a,b) -> (p1,p2)::a,b
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
	 | `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)]
	      ] ];
404
405
406


 fun_decl: [
407
408
409
   [ 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)
410
   ]
411
412
 ];

413
 arrow: [
414
    [ t1 = pat LEVEL "no_arrow"; "->"; t2 = pat -> (t1,t2)]
415
416
417
  ];

  branches: [
418
    [ OPT "|"; l = LIST1 branch SEP "|" -> l ]
419
420
421
  ];

  branch: [
422
    [ p = pat LEVEL "no_arrow"; "->"; e = expr -> (p,e) ]
423
424
425
426
  ];

	  
  regexp: [ 
427
428
429
430
431
    [ x = regexp; "|"; y = regexp -> 
	match (x,y) with
	  | Elem x, Elem y -> Elem (mk loc (Or (x,y)))
	  | _ -> Alt (x,y) 
    ]
432
  | [ x = regexp; y = regexp -> Seq (x,y) ]
433
434
435
436
437
  | [ 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"
    ]
438
  | [ a = IDENT; "::"; x = regexp -> SeqCapture (ident a,x) ]
439
440
441
442
  | [ x = regexp; "*" -> Star x
    | x = regexp; "*?" -> WeakStar x
    | x = regexp; "+" -> Seq (x, Star x)
    | x = regexp; "+?" -> Seq (x, WeakStar x)
443
    | x = regexp; "?" ->  Alt (x, Epsilon)
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
    | 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
    ]
461
  | [ "("; x = LIST1 regexp SEP ","; ")" ->
462
463
464
465
466
467
468
469
470
471
472
	(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))
473
474
    | "("; a = IDENT; ":="; c = expr; ")" -> 
	Elem (mk loc (Constant ((ident a,c))))
475
    | "/"; p = pat LEVEL "simple" -> Guard p
476
    | IDENT "PCDATA" -> string_regexp
477
    | i = STRING1; "--"; j = STRING1 ->
478
479
	let i = Chars.V.mk_int (parse_char loc i)
	and j = Chars.V.mk_int (parse_char loc j) in
480
        Elem (mk loc (Internal (Types.char (Chars.char_class i j))))
481
    |  s = STRING1 ->
482
	List.fold_right
483
	  (fun c accu -> 
484
	     let c = Chars.V.mk_int c in
485
	     let c = Chars.atom c in
486
487
	     Seq (Elem (mknoloc (Internal (Types.char c))), accu))
	  (seq_of_string s)
488
489
	  Epsilon ]
    | [ e = pat LEVEL "simple" -> Elem e
490
491
492
    ]
  ];

493
  schema_kind : [
494
    [ IDENT "element" -> `Element
495
    | "type" -> `Type
496
497
498
    | IDENT "attribute" -> `Attribute
    | IDENT "attribute_group" -> `Attribute_group
    | IDENT "model_group" -> `Model_group
499
500
501
    ]
  ];

502
503
504
  schema_ref: [
    [ schema = IDENT; "#"; typ = [ IDENT | keyword ];
      kind = OPT [ "as"; k = schema_kind -> k] ->
505
        (kind, U.mk schema, U.mk typ)
506
507
508
    ]
  ];

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

586
587
  or_else : [ [ OPT [ "else"; y = pat -> y ]  ] ];

588
  opt_field_pat: [ [ OPT [ "=";
589
                  o = [ "?" -> true | -> false]; 
590
591
592
593
594
595
596
597
598
599
600
601
                  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
			  | None -> (false, mknoloc (PatVar (U.mk l)), None)
			  | Some z -> z
		      in
		      let x = if o then mk loc (Optional x) else x in
		      (label l, (x,y))
                  ] SEP ";" ->
602
	  r
603
604
      ] ];
  
605
606
  char:
    [ 
607
      [ c = STRING1 -> Chars.V.mk_int (parse_char loc c) ]
608
609
610
    ];
     

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

627
628
  opt_field_expr: [ [ OPT [ "="; x = expr LEVEL "no_appl" -> x ] ] ];

629
630
  expr_record_spec:
    [ [ r = LIST1
631
632
633
	      [ l = [IDENT | keyword ]; 
		x = opt_field_expr ->
		  let x = match x with Some x -> x | None ->  Var (U.mk l) in
634
		  (label l,x) ] 
635
	       SEP ";"  ->
636
	  exp loc (RecordLitt r)
637
      ] ];
638

639
  expr_attrib_spec:
640
    [ [ r = LIST1
641
642
643
	      [ l = [IDENT | keyword ]; 
		x = opt_field_expr; OPT ";" ->
		  let x = match x with Some x -> x | None ->  Var (U.mk l) in
644
645
646
		  (label l,x) ] ->
	  exp loc (RecordLitt r)
      ]
647
    | [ e = expr LEVEL "no_appl" -> e 
648
      | -> exp loc (RecordLitt []) 
649
650
651
652
      ] 
    ];
END

653
654
655
module Hook = struct
  let expr = expr
  let pat = pat
656
  let keyword = keyword
657
658
end

659
let pat = Grammar.Entry.parse pat
660
661
and expr = Grammar.Entry.parse expr
and prog = Grammar.Entry.parse prog
662
663
and top_phrases = Grammar.Entry.parse top_phrases

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

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