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

4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
(* let ()  = Grammar.error_verbose := true *)
let gram    = Grammar.gcreate (Lexer.gmake ())
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
  | x :: l -> mk (x.loc) (Pair (x, tuple loc l))
  | [] -> assert false
      
let char = mk noloc (Internal (Types.char Chars.any))
23
let string_regexp = Star (Elem char)
24
25
26
	       
let cst_nil =  mk noloc (Cst (Types.Atom Sequence.nil_atom))
		 
27
28
29
30
31
32
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
33
34
35
  aux [] (String.length s)


36
37
38
let char_list pos s = 
  let s = seq_of_string pos (Token.eval_string s) in
  List.map (fun (loc,c) -> mk loc (Cst (Types.Char (Chars.Unichar.from_char c)))) s
39
40
41


EXTEND
42
43
44
  GLOBAL: prog expr pat regexp const;

  prog: [
45
    [ l = LIST0 [ p = phrase; ";;" -> mk loc p ]; EOI -> l ]
46
47
48
  ];

  phrase: [
49
50
51
    [ (p,e) = let_binding -> LetDecl (p,e)
    | (p,e1) = let_binding; "in"; e2 = expr LEVEL "top"->
        EvalStatement (mk loc (Match (e1,[p,e2])))
52
53
    | "type"; x = UIDENT; "="; t = pat -> TypeDecl (x,t)
    | "debug"; d = debug_directive -> Debug d
54
55
    ] | 
    [ e = expr -> EvalStatement e 
56
57
58
59
60
    ]
  ];

  debug_directive: [
    [ LIDENT "filter"; t = pat; p = pat -> `Filter(t,p)
61
    | LIDENT "accept"; p = pat -> `Accept p
62
    | LIDENT "compile"; t = pat; p = LIST1 pat -> `Compile (t,p)
63
    | LIDENT "normal_record"; t = pat -> `Normal_record t
64
    ]
65
  ];
66
67
68
69

  expr: [
    "top" RIGHTA
    [ "match"; e = SELF; "with"; b = branches -> mk loc (Match (e,b))
70
71
72
73
74
    | "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]))
75
    | LIDENT "map"; e = SELF; "with"; b = branches -> mk loc (Map (e,b))
76
77
    | "transform"; e = SELF; "with"; b = branches -> 
	mk noloc (Op ("flatten", [mk loc (Map (e,b))]))
78
    | "fun"; (f,a,b) = fun_decl ->
79
80
81
	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]))
82
83
    | e = expr; ":"; p = pat ->
	mk loc (Forget (e,p))
84
85
86
    ]

    
87
    | 
88
89
    [ e1 = expr; op = ["+" | "-" | "@"]; e2 = expr -> mk loc (Op (op,[e1;e2]))
    ]
90
    |
91
92
    [ e1 = expr; op = ["*" | "/"]; e2 = expr -> mk loc (Op (op,[e1;e2]))  
    ]
93
    |
94
95
    [ e = expr;  "."; l = [LIDENT | UIDENT] -> mk loc (Dot (e,Types.label l)) 
    ]  
96
 
97
    |
98
99
100
101
102
103
    [ op = [ LIDENT "flatten"
	   | LIDENT "load_xml"
	   | LIDENT "raise" 
	   | LIDENT "int_of"
	   ]; 
      e = expr -> mk loc (Op (op,[e]))
104
105
106
    | e1 = expr; e2 = expr -> mk loc (Apply (e1,e2))
    ]

107
108
109
    | "no_appl" 
    [ c = const -> mk loc (Cst c)
    | "("; l = LIST1 expr SEP ","; ")" -> tuple loc l
110
111
112
    | "[";  l = LIST0 seq_elem; e = OPT [ ";"; e = expr -> e ]; "]" ->
	let e = match e with Some e -> e | None -> cst_nil in
	let l = List.flatten l in
113
114
115
116
        tuple loc (l @ [e])
    | "<"; t = expr_tag_spec; a = expr_attrib_spec; ">"; c = expr ->
	tuple loc [t;a;c]
    | "{"; r = [ expr_record_spec | -> mk loc (RecordLitt []) ]; "}" -> r
117
118
    | s = STRING ->
	tuple loc (char_list loc s @ [cst_nil])
119
    | "!"; t = pat -> mk loc (DebugTyper t)
120
121
122
123
    | a = LIDENT -> mk loc (Var a)
    ]

  ];
124
125

  seq_elem: [
126
    [ x = CHAR -> char_list loc x
127
128
129
    | e = expr LEVEL "no_appl" -> [e]
    ]
  ];
130
131
132
	  
  let_binding: [
    [ "let"; p = pat; "="; e = expr -> (p,e)
133
    | "let"; p = pat; ":"; t = pat; "="; e = expr -> (p, mk noloc (Forget (e,t)))
134
135
136
137
138
139
    | "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
140
        let e = mk loc (Abstraction abst) in
141
        (p,e);
142
143
144
    ] 
  ];

145
146
147
148
149
150
151
152
153
154
155
 fun_decl: [
  [ f = OPT LIDENT; "("; a = LIST0 arrow SEP ";"; ")"; b = branches -> 
      (f,a,b)
  | f = OPT LIDENT; "("; arg = LIDENT; ":"; targ = pat; ")"; ":"; tres = pat ; 
      "="; body = expr ->
	let b = [mk noloc (Capture arg), body] in
	let a = [targ,tres] in
	(f,a,b)
  ]
 ];

156
  arrow: [
157
    [ t1 = pat LEVEL "no_arrow"; "->"; t2 = pat -> (t1,t2)]
158
159
160
161
162
163
164
  ];

  branches: [
    [ OPT "|"; l = LIST1 branch SEP "|" ; OPT "end" -> l ]
  ];

  branch: [
165
    [ p = pat LEVEL "no_arrow"; "->"; e = expr -> (p,e) ]
166
167
168
169
170
171
172
173
174
175
176
  ];

	  
  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)
177
    | x = regexp; "?" ->  Alt (x, Epsilon)
178
179
    | x = regexp; "??" -> Alt (Epsilon, x) ]
  | [ "("; x = regexp; ")" -> x
180
181
182
183
184
185
186
    | UIDENT "PCDATA" -> string_regexp
    | i = CHAR ; "--"; j = CHAR ->
	let i = Chars.Unichar.from_char (Token.eval_char i)
	and j = Chars.Unichar.from_char (Token.eval_char j) in
        Elem (mk loc (Internal (Types.char (Chars.char_class i j))))
    |  s = CHAR ->
	let s = seq_of_string loc (Token.eval_string s) in
187
	List.fold_right
188
	  (fun (loc,c) accu -> 
189
190
191
192
	     let c = Chars.Unichar.from_char c in
	     let c = Chars.atom c in
	     Seq (Elem (mk loc (Internal (Types.char c))), accu))
	  s
193
	  Epsilon 
194
195
196
197
198
199
200
201
202
    | 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)) ]
203
    | "no_arrow" [ x = pat; "|"; y = pat -> mk loc (Or (x,y)) ] 
204
205
206
    | "simple" [ x = pat; "&"; y = pat -> mk loc (And (x,y,true)) 
(*	       | x = pat; ":"; y = pat -> mk loc (And (x,y,false))   *)
	       | x = pat; "-"; y = pat -> mk loc (Diff (x,y)) ]
207
208
209
210
211
212
213
    | 
      [ "{"; 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 -> 
214
215
          let i = Big_int.big_int_of_string i 
	  and j = Big_int.big_int_of_string j in
216
217
218
219
220
221
222
223
224
225
          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)))
      | "*--"; j = INT ->
	  let j = Big_int.big_int_of_string j in
          mk loc (Internal (Types.interval (Intervals.left j)))
      | i = INT; "--*" ->
	  let i = Big_int.big_int_of_string i in
          mk loc (Internal (Types.interval (Intervals.right i)))
226
227
      | i = char ->
          mk loc (Internal (Types.char (Chars.char_class i i)))
228
      | i = char ; "--"; j = char ->
229
          mk loc (Internal (Types.char (Chars.char_class i j)))
230
231
232
233
      | 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 
234
                 | -> mk noloc (Internal (Sequence.nil_type)) ]; 
235
236
237
             "]" -> mk loc (Regexp (r,q))
      | "<"; t = tag_spec; a = attrib_spec; ">"; c = pat ->
          multi_prod loc [t;a;c]
238
      | s = STRING ->
239
	  let s = seq_of_string loc (Token.eval_string s) in
240
	  let s = List.map 
241
		    (fun (loc,c) -> 
242
243
244
245
246
247
		       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
248
249
250
251
252
253
254
255
256
257
258
      ]
    
  ];

  record_spec:
    [ [ r = LIST0 [ l = [LIDENT | UIDENT]; 
                  o = ["=?" -> true | "=" -> false]; 
                  x = pat ->
                    mk loc (Record (Types.label l,o,x))
                ] SEP ";" ->
        match r with
259
          | [] -> mk loc (Internal Types.Record.any)
260
          | h::t -> List.fold_left (fun t1 t2 -> mk loc (And (t1,t2,true))) h t
261
262
      ] ];
  
263
264
265
266
267
268
269
  char:
    [ 
      [ c = CHAR -> Chars.Unichar.from_char (Token.eval_char c)
      | "!"; i = INT -> Chars.Unichar.from_int (int_of_string i) ]
    ];
     

270
271
  const:
    [ 
272
      [ i = INT -> Types.Integer (Big_int.big_int_of_string i)
273
      | "`"; a = [LIDENT | UIDENT] -> Types.Atom (Types.mk_atom a)
274
      | c = char -> Types.Char c ]
275
276
277
278
    ];

  tag_spec:
    [
279
280
281
      [ LIDENT "_" -> mk loc (Internal (Types.atom (Atoms.any))) ]
    | [ a = [LIDENT | UIDENT] -> 
	  mk loc (Internal (Types.atom (Atoms.atom (Types.mk_atom a)))) ]
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
    | [ t = pat -> t ]
    ];

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

  expr_record_spec:
    [ [ r = LIST1
	      [ l = [LIDENT | UIDENT]; "="; x = expr -> (Types.label l,x) ] 
	      SEP ";" ->
	  mk loc (RecordLitt r)
      ] ];
  
  expr_tag_spec:
    [
      [ a = [LIDENT | UIDENT] -> 
	  mk loc (Cst (Types.Atom (Types.mk_atom a))) ]
    | [ e = expr LEVEL "no_appl" -> e ]
    ];

  expr_attrib_spec:
    [ [ r = expr_record_spec -> r ]
    | [ e = expr LEVEL "no_appl" -> e 
      | -> mk loc (RecordLitt []) 
      ] 
    ];
END

310
311
312
313
314
let pat'  = Grammar.Entry.create gram "type/pattern expression"
EXTEND GLOBAL: pat pat';
  pat': [  [ p = pat; EOI -> p ] ];
END

315
316
317
let pat = Grammar.Entry.parse pat
let expr = Grammar.Entry.parse expr
let prog = Grammar.Entry.parse prog
318

319
module From_string = struct
320
  let pat s = Grammar.Entry.parse pat' (Stream.of_string s)
321
322
  let expr s = expr (Stream.of_string s)
end
323