parser.ml 10.7 KB
Newer Older
1
2
3
open Location
open Ast

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

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


9
10
11
12
13
14
15
16
17
18
19
20
21
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
22
  | x :: l -> mk loc (Pair (x, tuple loc l))
23
  | [] -> assert false
24
25
26
27

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

28
let char = mk noloc (Internal (Types.char Chars.any))
29
let string_regexp = Star (Elem char)
30
31
32
	       
let cst_nil =  mk noloc (Cst (Types.Atom Sequence.nil_atom))
		 
33
34
35
36
37
38
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
39
40
  aux [] (String.length s)

41
42
43
44
45
46
47
48
exception Error of string
let error loc s = raise (Location (loc, Error s))

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

50
let char_list pos s = 
51
  let s = seq_of_string pos s in
52
  List.map (fun (loc,c) -> mk loc (Cst (Types.Char (Chars.Unichar.from_char c)))) s
53
54
55


EXTEND
56
57
58
  GLOBAL: prog expr pat regexp const;

  prog: [
59
    [ l = LIST0 [ p = phrase; ";;" -> mk loc p ]; EOI -> l ]
60
61
62
  ];

  phrase: [
63
64
65
    [ (p,e) = let_binding -> LetDecl (p,e)
    | (p,e1) = let_binding; "in"; e2 = expr LEVEL "top"->
        EvalStatement (mk loc (Match (e1,[p,e2])))
66
67
    | LIDENT "type"; x = UIDENT; "="; t = pat -> TypeDecl (x,t)
    | LIDENT "debug"; d = debug_directive -> Debug d
68
69
    ] | 
    [ e = expr -> EvalStatement e 
70
71
72
73
74
    ]
  ];

  debug_directive: [
    [ LIDENT "filter"; t = pat; p = pat -> `Filter(t,p)
75
    | LIDENT "accept"; p = pat -> `Accept p
76
    | LIDENT "compile"; t = pat; p = LIST1 pat -> `Compile (t,p)
77
    | LIDENT "normal_record"; t = pat -> `Normal_record t
78
    ]
79
  ];
80
81
82
83

  expr: [
    "top" RIGHTA
    [ "match"; e = SELF; "with"; b = branches -> mk loc (Match (e,b))
84
85
86
87
88
    | "try"; e = SELF; "with"; b = branches -> 
	let default = 
	  (mk noloc (Capture "x"),
	   mk noloc (Op ("raise",[mk noloc (Var "x")]))) in
	mk loc (Try (e,b@[default]))
89
    | "map"; e = SELF; "with"; b = branches -> mk loc (Map (e,b))
90
    | "transform"; e = SELF; "with"; b = branches -> 
91
92
	let default = mk noloc (Capture "x"), cst_nil in
	mk loc (Op ("flatten", [mk loc (Map (e,b@[default]))]))
93
    | "fun"; (f,a,b) = fun_decl ->
94
95
96
	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]))
97
98
    | e = expr; ":"; p = pat ->
	mk loc (Forget (e,p))
99
100
101
    ]

    
102
    | 
103
104
    [ e1 = expr; op = ["+" | "-" | "@"]; e2 = expr -> mk loc (Op (op,[e1;e2]))
    ]
105
    |
106
107
    [ e1 = expr; op = ["*" | "/"]; e2 = expr -> mk loc (Op (op,[e1;e2]))  
    ]
108
    |
109
110
    [ e = expr;  "."; l = [LIDENT | UIDENT] -> 
	mk loc (Dot (e,Types.LabelPool.mk l)) 
111
    ]  
112
 
113
    |
114
115
    [ op = [ LIDENT "flatten"
	   | LIDENT "load_xml"
116
	   | LIDENT "print_xml"
117
	   | LIDENT "print"
118
119
120
121
	   | LIDENT "raise" 
	   | LIDENT "int_of"
	   ]; 
      e = expr -> mk loc (Op (op,[e]))
122
123
    | op = [ LIDENT "dump_to_file" ];
      e1 = expr LEVEL "no_appl"; e2 = expr -> mk loc (Op (op, [e1;e2])) 
124
125
126
    | e1 = expr; e2 = expr -> mk loc (Apply (e1,e2))
    ]

127
128
129
    | "no_appl" 
    [ c = const -> mk loc (Cst c)
    | "("; l = LIST1 expr SEP ","; ")" -> tuple loc l
130
131
    | "[";  l = LIST0 seq_elem; e = OPT [ ";"; e = expr -> e ]; "]" ->
	let e = match e with Some e -> e | None -> cst_nil in
132
133
134
135
136
137
	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
138
139
140
141
    | t = [ a = TAG -> 
	      mk loc (Cst (Types.Atom (Types.AtomPool.mk a)))
	  | "<"; e = expr LEVEL "no_appl" -> e ];
	a = expr_attrib_spec; ">"; c = expr ->
142
	mk loc (Xml (t, mk loc (Pair (a,c))))
143
    | "{"; r = [ expr_record_spec | -> mk loc (RecordLitt []) ]; "}" -> r
144
    | s = STRING2 ->
145
	tuple loc (char_list loc s @ [cst_nil])
146
147
148
149
    | a = LIDENT -> mk loc (Var a)
    ]

  ];
150
151

  seq_elem: [
152
153
154
    [ x = STRING1 -> `Elems (char_list loc x)
    | e = expr LEVEL "no_appl" -> `Elems [e]
    | "!"; e = expr LEVEL "no_appl" -> `Explode e
155
156
    ]
  ];
157
158
159
	  
  let_binding: [
    [ "let"; p = pat; "="; e = expr -> (p,e)
160
    | "let"; p = pat; ":"; t = pat; "="; e = expr -> (p, mk noloc (Forget (e,t)))
161
162
163
164
165
166
    | "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
167
        let e = mk loc (Abstraction abst) in
168
        (p,e);
169
170
171
    ] 
  ];

172
 fun_decl: [
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
(* need an hack to do this, because both productions would
   match   [ OPT LIDENT; "("; pat ] .... *)
   [ f = OPT LIDENT; "("; 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) -> 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)
194
195
196
  ]
 ];

197
 arrow: [
198
    [ t1 = pat LEVEL "no_arrow"; "->"; t2 = pat -> (t1,t2)]
199
200
201
  ];

  branches: [
202
    [ OPT "|"; l = LIST1 branch SEP "|" -> l ]
203
204
205
  ];

  branch: [
206
    [ p = pat LEVEL "no_arrow"; "->"; e = expr -> (p,e) ]
207
208
209
210
211
212
213
214
215
216
217
  ];

	  
  regexp: [ 
    [ x = regexp; "|"; y = regexp -> Alt (x,y) ]
  | [ x = regexp; y = regexp -> Seq (x,y) ]
  | [ a = LIDENT; "::"; x = regexp -> SeqCapture (a,x) ]
  | [ x = regexp; "*" -> Star x
    | x = regexp; "*?" -> WeakStar x
    | x = regexp; "+" -> Seq (x, Star x)
    | x = regexp; "+?" -> Seq (x, WeakStar x)
218
    | x = regexp; "?" ->  Alt (x, Epsilon)
219
220
    | x = regexp; "??" -> Alt (Epsilon, x) ]
  | [ "("; x = regexp; ")" -> x
221
    | "("; a = LIDENT; ":="; c = const; ")" -> Elem (mk loc (Constant (a,c)))
222
    | UIDENT "PCDATA" -> string_regexp
223
224
225
    | i = STRING1; "--"; j = STRING1 ->
	let i = Chars.Unichar.from_char (parse_char loc i)
	and j = Chars.Unichar.from_char (parse_char loc j) in
226
        Elem (mk loc (Internal (Types.char (Chars.char_class i j))))
227
228
    |  s = STRING1 ->
	let s = seq_of_string loc s in
229
	List.fold_right
230
	  (fun (loc,c) accu -> 
231
232
233
234
	     let c = Chars.Unichar.from_char c in
	     let c = Chars.atom c in
	     Seq (Elem (mk loc (Internal (Types.char c))), accu))
	  s
235
	  Epsilon 
236
237
238
239
240
241
242
243
244
    | 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)) ]
245
    | "no_arrow" [ x = pat; "|"; y = pat -> mk loc (Or (x,y)) ] 
246
    | "simple" [ x = pat; "&"; y = pat -> mk loc (And (x,y)) 
247
	       | x = pat; "\\"; y = pat -> mk loc (Diff (x,y)) ]
248
249
250
251
252
253
254
    | 
      [ "{"; r = record_spec; "}" -> r
      | LIDENT "_" -> mk loc (Internal Types.any)
      | a = LIDENT -> mk loc (Capture a)
      | "("; a = LIDENT; ":="; c = const; ")" -> mk loc (Constant (a,c))
      | a = UIDENT -> mk loc (PatVar a)
      | i = INT ; "--"; j = INT -> 
255
256
          let i = Big_int.big_int_of_string i 
	  and j = Big_int.big_int_of_string j in
257
258
259
260
          mk loc (Internal (Types.interval (Intervals.bounded i j)))
      | i = INT -> 
          let i = Big_int.big_int_of_string i  in
          mk loc (Internal (Types.interval (Intervals.atom i)))
261
      | "*"; "--"; j = INT ->
262
263
	  let j = Big_int.big_int_of_string j in
          mk loc (Internal (Types.interval (Intervals.left j)))
264
      | i = INT; "--"; "*" ->
265
266
	  let i = Big_int.big_int_of_string i in
          mk loc (Internal (Types.interval (Intervals.right i)))
267
268
      | i = char ->
          mk loc (Internal (Types.char (Chars.char_class i i)))
269
      | i = char ; "--"; j = char ->
270
          mk loc (Internal (Types.char (Chars.char_class i j)))
271
272
273
274
      | 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 
275
                 | -> mk noloc (Internal (Sequence.nil_type)) ]; 
276
             "]" -> mk loc (Regexp (r,q))
277
278
279
280
281
282
283
284
      | t = [
	  [ "<"; LIDENT "_" -> mk loc (Internal (Types.atom (Atoms.any)))
	  | a = TAG -> 
	      mk loc 
	      (Internal (Types.atom (Atoms.atom (Types.AtomPool.mk a)))) ]
	| [ "<"; t = pat -> t ]
	];
	a = attrib_spec; ">"; c = pat ->
285
          mk loc (XmlT (t, multi_prod loc [a;c]))
286
287
      | s = STRING2 ->
	  let s = seq_of_string loc s in
288
	  let s = List.map 
289
		    (fun (loc,c) -> 
290
291
292
293
294
295
		       mk loc (Internal
				 (Types.char
				    (Chars.atom
				       (Chars.Unichar.from_char c))))) s in
	  let s = s @ [mk loc (Internal (Sequence.nil_type))] in
	  multi_prod loc s
296
297
298
299
300
      ]
    
  ];

  record_spec:
301
302
    [ [ r = LIST0 [ l = [LIDENT | UIDENT]; "=";
                  o = [ "?" -> true | -> false]; 
303
                  x = pat ->
304
                    mk loc (Record (Types.LabelPool.mk l,o,x))
305
306
                ] SEP ";" ->
        match r with
307
          | [] -> mk loc (Internal Types.Record.any)
308
          | h::t -> List.fold_left (fun t1 t2 -> mk loc (And (t1,t2))) h t
309
310
      ] ];
  
311
312
  char:
    [ 
313
      [ c = STRING1 -> Chars.Unichar.from_char (parse_char loc c)
314
315
316
317
      | "!"; i = INT -> Chars.Unichar.from_int (int_of_string i) ]
    ];
     

318
319
  const:
    [ 
320
      [ i = INT -> Types.Integer (Big_int.big_int_of_string i)
321
      | "`"; a = [LIDENT | UIDENT] -> Types.Atom (Types.AtomPool.mk a)
322
      | c = char -> Types.Char c ]
323
324
325
326
327
328
329
330
    ];


  attrib_spec:
    [ [ r = record_spec -> r | "("; t = pat; ")" -> t ] ];

  expr_record_spec:
    [ [ r = LIST1
331
332
	      [ l = [LIDENT | UIDENT]; "="; x = expr -> 
		  (Types.LabelPool.mk l,x) ] 
333
334
335
336
337
338
339
340
341
342
343
344
	      SEP ";" ->
	  mk loc (RecordLitt r)
      ] ];
  
  expr_attrib_spec:
    [ [ r = expr_record_spec -> r ]
    | [ e = expr LEVEL "no_appl" -> e 
      | -> mk loc (RecordLitt []) 
      ] 
    ];
END

345
346
347
348
349
let pat'  = Grammar.Entry.create gram "type/pattern expression"
EXTEND GLOBAL: pat pat';
  pat': [  [ p = pat; EOI -> p ] ];
END

350
let pat = Grammar.Entry.parse pat
351
352
and expr = Grammar.Entry.parse expr
and prog = Grammar.Entry.parse prog
353

354
module From_string = struct
355
  let pat s = Grammar.Entry.parse pat' (Stream.of_string s)
356
357
  let expr s = expr (Stream.of_string s)
end
358