parser.ml 12.1 KB
Newer Older
1
2
open Location
open Ast
3
open Ident
4

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

let gram    = Grammar.gcreate (Wlexer.lexer Wlexer.token Wlexer.latin1_engine)


12
13
14
15
16
17
18
19
20
21
22
23
24
let prog    = Grammar.Entry.create gram "prog"
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"
		
let rec multi_prod loc = function
  | [ x ] -> x
  | x :: l -> mk loc (Prod (x, multi_prod loc l))
  | [] -> assert false
      
let rec tuple loc = function
  | [ x ] -> x
25
  | x :: l -> mk loc (Pair (x, tuple loc l))
26
  | [] -> assert false
27
28
29
30

let tuple_queue = 
  List.fold_right (fun x q -> mk x.loc (Pair (x, q)))

31
32


33
let char = mk noloc (Internal (Types.char Chars.any))
34
let string_regexp = Star (Elem char)
35
36
37
	       
let cst_nil =  mk noloc (Cst (Types.Atom Sequence.nil_atom))
		 
38
39
40
41
42
43
let seq_of_string pos s =
  let (pos,_) = pos in
  let rec aux accu i = 
    if (i = 0) 
    then accu 
    else aux (((pos+i,pos+i+1),s.[i-1])::accu) (i-1) in
44
45
  aux [] (String.length s)

46
47
48
exception Error of string
let error loc s = raise (Location (loc, Error s))

49
50
51
let make_record loc r =
  LabelMap.from_list (fun _ _ -> error loc "Duplicated record field") r

52
53
54
55
56
let parse_char loc s =
  (* TODO: Unicode *)
  if String.length s <> 1 then
    error loc "Character litteral must have length 1";
  s.[0]
57

58
let char_list pos s = 
59
  let s = seq_of_string pos s in
60
  List.map (fun (loc,c) -> mk loc (Cst (Types.Char (Chars.mk_char c)))) s
61
62
63


EXTEND
64
65
66
  GLOBAL: prog expr pat regexp const;

  prog: [
67
    [ l = LIST0 [ p = phrase; ";;" -> mk loc p ]; EOI -> l ]
68
69
70
  ];

  phrase: [
71
72
73
    [ (p,e) = let_binding -> LetDecl (p,e)
    | (p,e1) = let_binding; "in"; e2 = expr LEVEL "top"->
        EvalStatement (mk loc (Match (e1,[p,e2])))
74
75
    | LIDENT "type"; x = UIDENT; "="; t = pat -> TypeDecl (x,t)
    | LIDENT "debug"; d = debug_directive -> Debug d
76
77
    ] | 
    [ e = expr -> EvalStatement e 
78
79
80
81
82
    ]
  ];

  debug_directive: [
    [ LIDENT "filter"; t = pat; p = pat -> `Filter(t,p)
83
    | LIDENT "accept"; p = pat -> `Accept p
84
    | LIDENT "compile"; t = pat; p = LIST1 pat -> `Compile (t,p)
85
    | LIDENT "normal_record"; t = pat -> `Normal_record t
86
    | LIDENT "compile2"; t = pat; p = LIST1 pat -> `Compile2 (t,p)
87
    | LIDENT "subtype"; t1 = pat; t2 = pat -> `Subtype (t1,t2)
88
    ]
89
  ];
90
91
92
93

  expr: [
    "top" RIGHTA
    [ "match"; e = SELF; "with"; b = branches -> mk loc (Match (e,b))
94
95
    | "try"; e = SELF; "with"; b = branches -> 
	let default = 
96
97
	  (mk noloc (Capture (ident "x")),
	   mk noloc (Op ("raise",[mk noloc (Var (ident "x"))]))) in
98
	mk loc (Try (e,b@[default]))
99
    | "map"; e = SELF; "with"; b = branches -> mk loc (Map (e,b))
100
101
102
103
    | "if"; e = SELF; "then"; e1 = SELF; "else"; e2 = SELF ->
	let p1 = mk loc (Internal (Builtin.true_type))
	and p2 = mk loc (Internal (Builtin.false_type)) in
	mk loc (Match (e, [p1,e1; p2,e2]))
104
    | "transform"; e = SELF; "with"; b = branches -> 
105
	let default = mk noloc (Capture (ident "x")), cst_nil in
106
	mk loc (Op ("flatten", [mk loc (Map (e,b@[default]))]))
107
    | "fun"; (f,a,b) = fun_decl ->
108
109
110
	mk loc (Abstraction { fun_name = f; fun_iface = a; fun_body = b })
    | (p,e1) = let_binding; "in"; e2 = expr LEVEL "top"->
        mk loc (Match (e1,[p,e2]))
111
112
    | e = expr; ":"; p = pat ->
	mk loc (Forget (e,p))
113
114
115
    ]

    
116
117
118
119
120
121
122
123
124
    | 
    [ e1 = expr; op = ["=" | "<=" | "<<" | ">>" | ">=" ]; e2 = expr -> 
	let op = match op with
	  | "<<" -> "<"
	  | ">>" -> ">"
	  | s -> s in
	mk loc (Op (op,[e1;e2]))
    ]

125
    | 
126
127
    [ e1 = expr; op = ["+" | "-" | "@"]; e2 = expr -> mk loc (Op (op,[e1;e2]))
    ]
128
    |
129
130
131
132
    [ e1 = expr; "++"; "{"; l = [LIDENT | UIDENT]; "="; e = expr; "}" ->
	assert false
    ]
    |
133
134
135
136
137
138
    [ e1 = expr; op = ["*"]; e2 = expr -> mk loc (Op (op,[e1;e2]))  
    | 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
139
	let re = Star(Alt(SeqCapture(ident "x",Elem p), Elem any)) in
140
141
	let ct = mk loc (Regexp (re,any)) in
        let p = mk loc (XmlT (tag, multi_prod loc [att;ct])) in
142
	let b = (p,  mk loc (Var (ident "x"))) in
143
	mk loc (Op ("flatten", [mk loc (Map (e,[b]))]))
144
    ]
145
    |
146
    [ e = expr;  "."; l = [LIDENT | UIDENT] -> 
147
	mk loc (Dot (e,LabelPool.mk l)) 
148
    ]  
149
 
150
    |
151
152
    [ op = [ LIDENT "flatten"
	   | LIDENT "load_xml"
153
	   | LIDENT "load_html"
154
	   | LIDENT "print_xml"
155
	   | LIDENT "print"
156
157
	   | LIDENT "raise" 
	   | LIDENT "int_of"
158
	   | LIDENT "string_of"
159
160
	   ]; 
      e = expr -> mk loc (Op (op,[e]))
161
162
    | op = [ LIDENT "dump_to_file" ];
      e1 = expr LEVEL "no_appl"; e2 = expr -> mk loc (Op (op, [e1;e2])) 
163
    | e1 = SELF; LIDENT "div"; e2 = expr -> mk loc (Op ("/", [e1;e2]))
164
    | e1 = SELF; LIDENT "mod"; e2 = expr -> mk loc (Op ("mod", [e1;e2]))
165
    | e1 = SELF; e2 = expr -> mk loc (Apply (e1,e2))
166
167
    ]

168
169
170
    | "no_appl" 
    [ c = const -> mk loc (Cst c)
    | "("; l = LIST1 expr SEP ","; ")" -> tuple loc l
171
172
    | "[";  l = LIST0 seq_elem; e = OPT [ ";"; e = expr -> e ]; "]" ->
	let e = match e with Some e -> e | None -> cst_nil in
173
174
175
176
177
178
	List.fold_right 
	  (fun x q ->
	     match x with
	       | `Elems l -> tuple_queue l q
	       | `Explode x -> mk x.loc (Op ("@",[x;q]))
	  ) l e
179
    | t = [ a = TAG -> 
180
	      mk loc (Cst (Types.Atom (Atoms.mk a)))
181
182
	  | "<"; e = expr LEVEL "no_appl" -> e ];
	a = expr_attrib_spec; ">"; c = expr ->
183
	mk loc (Xml (t, mk loc (Pair (a,c))))
184
    | "{"; r = [ expr_record_spec | -> mk loc (RecordLitt LabelMap.empty) ]; "}" -> r
185
    | s = STRING2 ->
186
	tuple loc (char_list loc s @ [cst_nil])
187
    | a = LIDENT -> mk loc (Var (ident a))
188
189
190
    ]

  ];
191
192

  seq_elem: [
193
194
195
    [ x = STRING1 -> `Elems (char_list loc x)
    | e = expr LEVEL "no_appl" -> `Elems [e]
    | "!"; e = expr LEVEL "no_appl" -> `Explode e
196
197
    ]
  ];
198
199
200
	  
  let_binding: [
    [ "let"; p = pat; "="; e = expr -> (p,e)
201
    | "let"; p = pat; ":"; t = pat; "="; e = expr -> (p, mk noloc (Forget (e,t)))
202
203
204
205
206
207
    | "let"; "fun"; (f,a,b) = fun_decl ->
	let p = match f with 
	  | Some x -> mk loc (Capture x)
	  | _ -> failwith "Function name mandatory in let fun declarations"
	in
	let abst = { fun_name = f; fun_iface = a; fun_body = b } in
208
        let e = mk loc (Abstraction abst) in
209
        (p,e);
210
211
212
    ] 
  ];

213
 fun_decl: [
214
215
(* need an hack to do this, because both productions would
   match   [ OPT LIDENT; "("; pat ] .... *)
216
   [ f = OPT [ x = LIDENT -> ident x]; "("; p1 = pat LEVEL "no_arrow";
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
    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) -> f,(p1,p2)::a,b
	| `Compact (targ1,args,tres,body) ->
	    let args = (p1,targ1) :: args in
	    let targ = multi_prod noloc (List.map snd args) in
	    let arg = multi_prod noloc (List.map fst args) in
	    let b = [arg, body] in
	    let a = [targ,tres] in
	    (f,a,b)
235
236
237
  ]
 ];

238
 arrow: [
239
    [ t1 = pat LEVEL "no_arrow"; "->"; t2 = pat -> (t1,t2)]
240
241
242
  ];

  branches: [
243
    [ OPT "|"; l = LIST1 branch SEP "|" -> l ]
244
245
246
  ];

  branch: [
247
    [ p = pat LEVEL "no_arrow"; "->"; e = expr -> (p,e) ]
248
249
250
251
  ];

	  
  regexp: [ 
252
253
254
255
256
    [ x = regexp; "|"; y = regexp -> 
	match (x,y) with
	  | Elem x, Elem y -> Elem (mk loc (Or (x,y)))
	  | _ -> Alt (x,y) 
    ]
257
  | [ x = regexp; y = regexp -> Seq (x,y) ]
258
  | [ a = LIDENT; "::"; x = regexp -> SeqCapture (ident a,x) ]
259
260
261
262
  | [ x = regexp; "*" -> Star x
    | x = regexp; "*?" -> WeakStar x
    | x = regexp; "+" -> Seq (x, Star x)
    | x = regexp; "+?" -> Seq (x, WeakStar x)
263
    | x = regexp; "?" ->  Alt (x, Epsilon)
264
265
    | x = regexp; "??" -> Alt (Epsilon, x) ]
  | [ "("; x = regexp; ")" -> x
266
267
    | "("; a = LIDENT; ":="; c = const; ")" -> 
	Elem (mk loc (Constant ((ident a,c))))
268
    | UIDENT "PCDATA" -> string_regexp
269
    | i = STRING1; "--"; j = STRING1 ->
270
271
	let i = Chars.mk_char (parse_char loc i)
	and j = Chars.mk_char (parse_char loc j) in
272
        Elem (mk loc (Internal (Types.char (Chars.char_class i j))))
273
274
    |  s = STRING1 ->
	let s = seq_of_string loc s in
275
	List.fold_right
276
	  (fun (loc,c) accu -> 
277
	     let c = Chars.mk_char c in
278
279
280
	     let c = Chars.atom c in
	     Seq (Elem (mk loc (Internal (Types.char c))), accu))
	  s
281
	  Epsilon 
282
283
284
285
286
287
288
289
290
    | e = pat LEVEL "simple" -> Elem e
    ]
  ];

  pat: [ 
      [ x = pat; "where"; 
        b = LIST1 [ a = UIDENT; "="; y = pat -> (a,y)] SEP "and"
            -> mk loc (Recurs (x,b)) ]
    | RIGHTA [ x = pat; "->"; y = pat -> mk loc (Arrow (x,y)) ]
291
    | "no_arrow" [ x = pat; "|"; y = pat -> mk loc (Or (x,y)) ] 
292
    | "simple" [ x = pat; "&"; y = pat -> mk loc (And (x,y)) 
293
	       | x = pat; "\\"; y = pat -> mk loc (Diff (x,y)) ]
294
    | 
295
296
      [ "{"; r = record_spec; "}" -> mk loc (Record (true,r))
      | "{|"; r = record_spec; "|}" -> mk loc (Record (false,r))
297
      | LIDENT "_" -> mk loc (Internal Types.any)
298
299
300
      | a = LIDENT -> mk loc (Capture (ident a))
      | "("; a = LIDENT; ":="; c = const; ")" -> 
	  mk loc (Constant (ident a,c))
301
302
      | a = UIDENT -> mk loc (PatVar a)
      | i = INT ; "--"; j = INT -> 
303
304
          let i =  Intervals.mk i 
	  and j =  Intervals.mk j in
305
306
          mk loc (Internal (Types.interval (Intervals.bounded i j)))
      | i = INT -> 
307
          let i =  Intervals.mk i  in
308
          mk loc (Internal (Types.interval (Intervals.atom i)))
309
      | "*"; "--"; j = INT ->
310
	  let j =  Intervals.mk j in
311
          mk loc (Internal (Types.interval (Intervals.left j)))
312
      | i = INT; "--"; "*" ->
313
	  let i = Intervals.mk i in
314
          mk loc (Internal (Types.interval (Intervals.right i)))
315
316
      | i = char ->
          mk loc (Internal (Types.char (Chars.char_class i i)))
317
      | i = char ; "--"; j = char ->
318
          mk loc (Internal (Types.char (Chars.char_class i j)))
319
320
321
322
      | c = const -> mk loc (Internal (Types.constant c))
      | "("; l = LIST1 pat SEP ","; ")" -> multi_prod loc l
      | "["; r = [ r = regexp -> r | -> Epsilon ];
             q = [ ";"; q = pat -> q 
323
                 | -> mk noloc (Internal (Sequence.nil_type)) ]; 
324
             "]" -> mk loc (Regexp (r,q))
325
326
327
328
      | t = [
	  [ "<"; LIDENT "_" -> mk loc (Internal (Types.atom (Atoms.any)))
	  | a = TAG -> 
	      mk loc 
329
	      (Internal (Types.atom (Atoms.atom (Atoms.mk a)))) ]
330
331
332
	| [ "<"; t = pat -> t ]
	];
	a = attrib_spec; ">"; c = pat ->
333
          mk loc (XmlT (t, multi_prod loc [a;c]))
334
335
      | s = STRING2 ->
	  let s = seq_of_string loc s in
336
	  let s = List.map 
337
		    (fun (loc,c) -> 
338
339
340
		       mk loc (Internal
				 (Types.char
				    (Chars.atom
341
				       (Chars.mk_char c))))) s in
342
343
	  let s = s @ [mk loc (Internal (Sequence.nil_type))] in
	  multi_prod loc s
344
345
346
347
348
      ]
    
  ];

  record_spec:
349
350
    [ [ r = LIST0 [ l = [LIDENT | UIDENT]; "=";
                  o = [ "?" -> true | -> false]; 
351
352
                  x = pat -> 
		    let x = if o then mk loc (Optional x) else x in
353
		    (LabelPool.mk l, x)
354
                ] SEP ";" ->
355
	  make_record loc r
356
357
      ] ];
  
358
359
  char:
    [ 
360
361
      [ c = STRING1 -> Chars.mk_char (parse_char loc c)
      | "!"; i = INT -> Chars.mk_int (int_of_string i) ]
362
363
364
    ];
     

365
366
  const:
    [ 
367
368
      [ i = INT -> Types.Integer (Intervals.mk i)
      | "`"; a = [LIDENT | UIDENT] -> Types.Atom (Atoms.mk a)
369
      | c = char -> Types.Char c ]
370
371
372
373
    ];


  attrib_spec:
374
375
    [ [ r = record_spec -> mk loc (Record (true,r)) 
      | "("; t = pat; ")" -> t ] ];
376
377
378

  expr_record_spec:
    [ [ r = LIST1
379
	      [ l = [LIDENT | UIDENT]; "="; x = expr -> 
380
		  (LabelPool.mk l,x) ] 
381
	      SEP ";" ->
382
	  mk loc (RecordLitt (make_record loc r))
383
384
385
386
387
      ] ];
  
  expr_attrib_spec:
    [ [ r = expr_record_spec -> r ]
    | [ e = expr LEVEL "no_appl" -> e 
388
      | -> mk loc (RecordLitt (LabelMap.empty)) 
389
390
391
392
      ] 
    ];
END

393
394
395
396
397
let pat'  = Grammar.Entry.create gram "type/pattern expression"
EXTEND GLOBAL: pat pat';
  pat': [  [ p = pat; EOI -> p ] ];
END

398
let pat = Grammar.Entry.parse pat
399
400
and expr = Grammar.Entry.parse expr
and prog = Grammar.Entry.parse prog
401

402
module From_string = struct
403
  let pat s = Grammar.Entry.parse pat' (Stream.of_string s)
404
405
  let expr s = expr (Stream.of_string s)
end
406