/[svn]/parser/parser.ml
ViewVC logotype

Contents of /parser/parser.ml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 374 - (show annotations)
Tue Jul 10 17:29:46 2007 UTC (5 years, 10 months ago) by abate
File size: 14380 byte(s)
[r2003-05-20 13:27:25 by cvscast] Unicode support

Original author: cvscast
Date: 2003-05-20 13:27:27+00:00
1 open Location
2 open Ast
3 open Ident
4
5 (*
6 let () = Grammar.error_verbose := true
7 *)
8
9 let gram = Grammar.gcreate (Wlexer.lexer Wlexer.token Wlexer.latin1_engine)
10
11
12 let parse_ident = Encodings.Utf8.mk_latin1
13
14 let label s = LabelPool.mk (parse_ident s)
15
16 let prog = Grammar.Entry.create gram "prog"
17 let expr = Grammar.Entry.create gram "expression"
18 let pat = Grammar.Entry.create gram "type/pattern expression"
19 let regexp = Grammar.Entry.create gram "type/pattern regexp"
20 let const = Grammar.Entry.create gram "scalar constant"
21
22 let exp pos e = LocatedExpr (loc_of_pos pos,e)
23
24 let rec multi_prod loc = function
25 | [ x ] -> x
26 | x :: l -> mk loc (Prod (x, multi_prod loc l))
27 | [] -> assert false
28
29 let rec tuple = function
30 | [ x ] -> x
31 | x :: l -> Pair (x, tuple l)
32 | [] -> assert false
33
34 let tuple_queue =
35 List.fold_right (fun x q -> Pair (x, q))
36
37
38 let char = mknoloc (Internal (Types.char Chars.any))
39 let string_regexp = Star (Elem char)
40
41 let cst_nil = Cst (Types.Atom Sequence.nil_atom)
42
43 let seq_of_string pos s =
44 let s = Encodings.Utf8.mk s in
45 let (pos,_) = pos in
46 let rec aux pos i j =
47 if Encodings.Utf8.equal_index i j then []
48 else
49 let (len,i) = Encodings.Utf8.next s i in
50 let (c,i) = Encodings.Utf8.next s i in
51 ((pos,pos+len),c) :: (aux (pos + len) i j)
52 in
53 aux (pos+1) (Encodings.Utf8.start_index s) (Encodings.Utf8.end_index s)
54
55 let get_string s =
56 let s = Encodings.Utf8.mk s in
57 let b = Buffer.create 32 in
58 let rec aux i j =
59 if Encodings.Utf8.equal_index i j then ()
60 else
61 let (len,i) = Encodings.Utf8.next s i in
62 let (c,i) = Encodings.Utf8.next s i in
63 Encodings.Utf8.store b c;
64 aux i j
65 in
66 aux (Encodings.Utf8.start_index s) (Encodings.Utf8.end_index s);
67 Buffer.contents b
68
69 exception Error of string
70 let error (i,j) s = Location.raise_loc i j (Error s)
71
72 let make_record loc r =
73 LabelMap.from_list (fun _ _ -> error loc "Duplicated record field") r
74
75 let parse_char loc s =
76 let s = seq_of_string loc s in
77 match s with
78 | [ loc,c ] -> c
79 | _ -> error loc "Character litteral must have length 1"
80
81 let char_list loc s =
82 let s = seq_of_string loc s in
83 List.map (fun (loc,c) -> exp loc (Cst (Types.Char (Chars.mk_int c)))) s
84
85
86 let include_stack = ref []
87
88 EXTEND
89 GLOBAL: prog expr pat regexp const;
90
91 prog: [
92 [ l = LIST0 [ p = phrase; ";;" -> p ]; EOI -> List.flatten l ]
93 ];
94
95 phrase: [
96 [ (p,e) = let_binding -> [ mk loc (LetDecl (p,e)) ]
97 | (p,e1) = let_binding; "in"; e2 = expr LEVEL "top"->
98 [ mk loc (EvalStatement (exp loc (Match (e1,[p,e2])))) ]
99 | LIDENT "type"; x = UIDENT; "="; t = pat -> [ mk loc (TypeDecl (x,t)) ]
100 | LIDENT "type"; x = LIDENT -> [ error loc "Type identifiers must be capitalized" ]
101 | LIDENT "debug"; d = debug_directive -> [ mk loc (Debug d) ]
102 | LIDENT "include"; s = STRING2 ->
103 let s = get_string s in
104 protect_op "File inclusion";
105 (* avoid looping; should issue an error ? *)
106 if List.mem s !include_stack then []
107 else (
108 include_stack := s :: !include_stack;
109 let chan = open_in s in
110 Location.push_source (`File s);
111 let input = Stream.of_channel chan in
112 let l = Grammar.Entry.parse prog input in
113 close_in chan;
114 Location.pop_source ();
115 include_stack := List.tl !include_stack;
116 l
117 )
118 ] |
119 [ e = expr -> [ mk loc (EvalStatement e) ]
120 ]
121 ];
122
123 debug_directive: [
124 [ LIDENT "filter"; t = pat; p = pat -> `Filter(t,p)
125 | LIDENT "accept"; p = pat -> `Accept p
126 | LIDENT "compile"; t = pat; p = LIST1 pat -> `Compile (t,p)
127 | LIDENT "normal_record"; t = pat -> `Normal_record t
128 | LIDENT "compile2"; t = pat; p = LIST1 pat -> `Compile2 (t,p)
129 | LIDENT "subtype"; t1 = pat; t2 = pat -> `Subtype (t1,t2)
130 ]
131 ];
132
133 keyword: [
134 [ a =
135 [ "map" | "match" | "with" | "try" | "xtransform"
136 | "if" | "then" | "else"
137 | "transform" | "fun" | "in"
138 | "let"
139 ]
140 -> a
141 ]
142 ];
143
144 expr: [
145 "top" RIGHTA
146 [ "match"; e = SELF; "with"; b = branches -> exp loc (Match (e,b))
147 | "try"; e = SELF; "with"; b = branches ->
148 let id = ident (U.mk "x") in
149 let default =
150 mknoloc (Capture id),
151 Op ("raise",[Var id]) in
152 exp loc (Try (e,b@[default]))
153 | "map"; e = SELF; "with"; b = branches -> exp loc (Map (false,e,b))
154 | "xtransform"; e = SELF; "with"; b = branches -> exp loc (Xtrans (e,b))
155 | "if"; e = SELF; "then"; e1 = SELF; "else"; e2 = SELF ->
156 let p1 = mk loc (Internal (Builtin.true_type))
157 and p2 = mk loc (Internal (Builtin.false_type)) in
158 exp loc (Match (e, [p1,e1; p2,e2]))
159 | "transform"; e = SELF; "with"; b = branches ->
160 exp loc (Op ("flatten", [Map (true,e,b)]))
161 | "fun"; (f,a,b) = fun_decl ->
162 exp loc (Abstraction { fun_name = f; fun_iface = a; fun_body = b })
163 | (p,e1) = let_binding; "in"; e2 = expr LEVEL "top"->
164 exp loc (Match (e1,[p,e2]))
165 | e = expr; ":"; p = pat ->
166 exp loc (Forget (e,p))
167 ]
168
169
170 |
171 [ e1 = expr; op = ["=" | "<=" | "<<" | ">>" | ">=" ]; e2 = expr ->
172 let op = match op with
173 | "<<" -> "<"
174 | ">>" -> ">"
175 | s -> s in
176 exp loc (Op (op,[e1;e2]))
177 ]
178
179 |
180 [ e1 = expr; op = ["+" | "-" | "@" ]; e2 = expr ->
181 exp loc (Op (op,[e1;e2]))
182 | e = expr; "\\"; l = [LIDENT | UIDENT | keyword ] ->
183 exp loc (RemoveField (e, label l))
184 ]
185 |
186 [ e1 = expr; op = ["*"]; e2 = expr -> exp loc (Op (op,[e1;e2]))
187 | e = expr; op = "/"; p = pat ->
188
189 let tag = mk loc (Internal (Types.atom (Atoms.any))) in
190 let att = mk loc (Internal Types.Record.any) in
191 let any = mk loc (Internal (Types.any)) in
192 let id = ident (U.mk "x") in
193 let re = Star(Alt(SeqCapture(id,Elem p), Elem any)) in
194 let ct = mk loc (Regexp (re,any)) in
195 let p = mk loc (XmlT (tag, multi_prod loc [att;ct])) in
196 let b = (p, Var id) in
197 exp loc (Op ("flatten", [Map (false,e,[b])]))
198 ]
199 |
200 [ e = expr; "."; l = [LIDENT | UIDENT | keyword ] ->
201 exp loc (Dot (e, label l))
202 ]
203
204 |
205 [ op = [ LIDENT "flatten"
206 | LIDENT "load_xml"
207 | LIDENT "load_file" | LIDENT "load_file_utf8"
208 | LIDENT "load_html"
209 | LIDENT "print_xml" | LIDENT "print_xml_utf8"
210 | LIDENT "print"
211 | LIDENT "raise"
212 | LIDENT "int_of"
213 | LIDENT "string_of"
214 | LIDENT "atom_of"
215 ];
216 e = expr -> exp loc (Op (op,[e]))
217 | op = [ LIDENT "dump_to_file" | LIDENT "dump_to_file_utf8" ];
218 e1 = expr LEVEL "no_appl"; e2 = expr -> exp loc (Op (op, [e1;e2]))
219 | e1 = SELF; LIDENT "div"; e2 = expr -> exp loc (Op ("/", [e1;e2]))
220 | e1 = SELF; LIDENT "mod"; e2 = expr -> exp loc (Op ("mod", [e1;e2]))
221 | e1 = SELF; e2 = expr -> exp loc (Apply (e1,e2))
222 ]
223
224 | "no_appl"
225 [ c = const -> exp loc (Cst c)
226 | "("; l = LIST1 expr SEP ","; ")" -> exp loc (tuple l)
227 | "["; l = LIST0 seq_elem; e = OPT [ ";"; e = expr -> e ]; "]" ->
228 let e = match e with Some e -> e | None -> cst_nil in
229 let l = List.fold_right
230 (fun x q ->
231 match x with
232 | `Elems l -> tuple_queue l q
233 | `Explode x -> Op ("@",[x;q])
234 ) l e
235 in
236 exp loc l
237 | "<"; t = [ "("; e = expr; ")" -> e
238 | a = [ LIDENT | UIDENT | keyword ] ->
239 let a = parse_ident a in
240 exp loc (Cst (Types.Atom (Atoms.mk a))) ];
241 a = expr_attrib_spec; ">"; c = expr ->
242 exp loc (Xml (t, Pair (a,c)))
243 | "{"; r = [ expr_record_spec | -> exp loc (RecordLitt LabelMap.empty) ]; "}" -> r
244 | s = STRING2 ->
245 exp loc (tuple (char_list loc s @ [cst_nil]))
246 | a = LIDENT -> exp loc (Var (ident (parse_ident a)))
247 ]
248
249 ];
250
251 seq_elem: [
252 [ x = STRING1 -> `Elems (char_list loc x)
253 | e = expr LEVEL "no_appl" -> `Elems [e]
254 | "!"; e = expr LEVEL "no_appl" -> `Explode e
255 ]
256 ];
257
258 let_binding: [
259 [ "let"; p = pat; "="; e = expr -> (p,e)
260 | "let"; p = pat; ":"; t = pat; "="; e = expr -> (p, Forget (e,t))
261 | "let"; "fun"; (f,a,b) = fun_decl ->
262 let p = match f with
263 | Some x -> mk loc (Capture x)
264 | _ -> failwith "Function name mandatory in let fun declarations"
265 in
266 let abst = { fun_name = f; fun_iface = a; fun_body = b } in
267 let e = exp loc (Abstraction abst) in
268 (p,e);
269 ]
270 ];
271
272 fun_decl: [
273 (* need an hack to do this, because both productions would
274 match [ OPT LIDENT; "("; pat ] .... *)
275 [ f = OPT [ x = LIDENT -> ident (parse_ident x)]; "("; p1 = pat LEVEL "no_arrow";
276 res = [ "->"; p2 = pat;
277 a = [ ";"; a = LIST0 arrow SEP ";" -> a | -> [] ];
278 ")"; b = branches -> `Classic (p2,a,b)
279 | ":"; targ1 = pat;
280 args = LIST0 [ ","; arg = pat; ":"; targ = pat -> (arg,targ) ];
281 ")"; ":"; tres = pat ;
282 "="; body = expr ->
283 `Compact (targ1,args,tres,body)
284 ] ->
285 match res with
286 | `Classic (p2,a,b) -> f,(p1,p2)::a,b
287 | `Compact (targ1,args,tres,body) ->
288 let args = (p1,targ1) :: args in
289 let targ = multi_prod nopos (List.map snd args) in
290 let arg = multi_prod nopos (List.map fst args) in
291 let b = [arg, body] in
292 let a = [targ,tres] in
293 (f,a,b)
294 ]
295 ];
296
297 arrow: [
298 [ t1 = pat LEVEL "no_arrow"; "->"; t2 = pat -> (t1,t2)]
299 ];
300
301 branches: [
302 [ OPT "|"; l = LIST1 branch SEP "|" -> l ]
303 ];
304
305 branch: [
306 [ p = pat LEVEL "no_arrow"; "->"; e = expr -> (p,e) ]
307 ];
308
309
310 regexp: [
311 [ x = regexp; "|"; y = regexp ->
312 match (x,y) with
313 | Elem x, Elem y -> Elem (mk loc (Or (x,y)))
314 | _ -> Alt (x,y)
315 ]
316 | [ x = regexp; y = regexp -> Seq (x,y) ]
317 | [ a = LIDENT; "::"; x = regexp -> SeqCapture (ident (parse_ident a),x) ]
318 | [ x = regexp; "*" -> Star x
319 | x = regexp; "*?" -> WeakStar x
320 | x = regexp; "+" -> Seq (x, Star x)
321 | x = regexp; "+?" -> Seq (x, WeakStar x)
322 | x = regexp; "?" -> Alt (x, Epsilon)
323 | x = regexp; "??" -> Alt (Epsilon, x) ]
324 | [ "("; x = regexp; ")" -> x
325 | "("; a = LIDENT; ":="; c = const; ")" ->
326 Elem (mk loc (Constant ((ident (parse_ident a),c))))
327 | UIDENT "PCDATA" -> string_regexp
328 | i = STRING1; "--"; j = STRING1 ->
329 let i = Chars.mk_int (parse_char loc i)
330 and j = Chars.mk_int (parse_char loc j) in
331 Elem (mk loc (Internal (Types.char (Chars.char_class i j))))
332 | s = STRING1 ->
333 let s = seq_of_string loc s in
334 List.fold_right
335 (fun (loc,c) accu ->
336 let c = Chars.mk_int c in
337 let c = Chars.atom c in
338 Seq (Elem (mk loc (Internal (Types.char c))), accu))
339 s
340 Epsilon
341 | e = pat LEVEL "simple" -> Elem e
342 ]
343 ];
344
345 pat: [
346 [ x = pat; LIDENT "where";
347 b = LIST1 [ a = UIDENT; "="; y = pat -> (a,y)
348 | LIDENT -> error loc "Type/pattern identifiers must be capitalized"
349 ] SEP "and"
350 -> mk loc (Recurs (x,b)) ]
351 | RIGHTA [ x = pat; "->"; y = pat -> mk loc (Arrow (x,y)) ]
352 | "no_arrow" [ x = pat; "|"; y = pat -> mk loc (Or (x,y)) ]
353 | "simple" [ x = pat; "&"; y = pat -> mk loc (And (x,y))
354 | x = pat; "\\"; y = pat -> mk loc (Diff (x,y)) ]
355 |
356 [ "{"; r = record_spec; "}" -> mk loc (Record (true,r))
357 | "{|"; r = record_spec; "|}" -> mk loc (Record (false,r))
358 | LIDENT "_" -> mk loc (Internal Types.any)
359 | a = LIDENT -> mk loc (Capture (ident (parse_ident a)))
360 | "("; a = LIDENT; ":="; c = const; ")" ->
361 mk loc (Constant (ident (parse_ident a),c))
362 | a = UIDENT -> mk loc (PatVar a)
363 | i = INT ; "--"; j = INT ->
364 let i = Intervals.mk i
365 and j = Intervals.mk j in
366 mk loc (Internal (Types.interval (Intervals.bounded i j)))
367 | i = INT ->
368 let i = Intervals.mk i in
369 mk loc (Internal (Types.interval (Intervals.atom i)))
370 | "*"; "--"; j = INT ->
371 let j = Intervals.mk j in
372 mk loc (Internal (Types.interval (Intervals.left j)))
373 | i = INT; "--"; "*" ->
374 let i = Intervals.mk i in
375 mk loc (Internal (Types.interval (Intervals.right i)))
376 | i = char ->
377 mk loc (Internal (Types.char (Chars.char_class i i)))
378 | i = char ; "--"; j = char ->
379 mk loc (Internal (Types.char (Chars.char_class i j)))
380 | c = const -> mk loc (Internal (Types.constant c))
381 | "("; l = LIST1 pat SEP ","; ")" -> multi_prod loc l
382 | "["; r = [ r = regexp -> r | -> Epsilon ];
383 q = [ ";"; q = pat -> q
384 | -> mknoloc (Internal (Sequence.nil_type)) ];
385 "]" -> mk loc (Regexp (r,q))
386 | "<"; t =
387 [ x = [ LIDENT | UIDENT | keyword ] ->
388 let a = if x = "_" then Atoms.any else Atoms.atom (Atoms.mk (parse_ident x)) in
389 mk loc (Internal (Types.atom a))
390 | "("; t = pat; ")" -> t ];
391 a = attrib_spec; ">"; c = pat ->
392 mk loc (XmlT (t, multi_prod loc [a;c]))
393 | s = STRING2 ->
394 let s = seq_of_string loc s in
395 let s = List.map
396 (fun (loc,c) ->
397 mk loc (Internal
398 (Types.char
399 (Chars.atom
400 (Chars.mk_int c))))) s in
401 let s = s @ [mk loc (Internal (Sequence.nil_type))] in
402 multi_prod loc s
403 ]
404
405 ];
406
407 record_spec:
408 [ [ r = LIST0 [ l = [LIDENT | UIDENT | keyword ]; "=";
409 o = [ "?" -> true | -> false];
410 x = pat ->
411 let x = if o then mk loc (Optional x) else x in
412 (label l, x)
413 ] SEP ";" ->
414 make_record loc r
415 ] ];
416
417 char:
418 [
419 [ c = STRING1 -> Chars.mk_int (parse_char loc c) ]
420 ];
421
422
423 const:
424 [
425 [ i = INT -> Types.Integer (Intervals.mk i)
426 | "`"; a = [LIDENT | UIDENT | keyword ] -> Types.Atom (Atoms.mk (parse_ident a))
427 | c = char -> Types.Char c ]
428 ];
429
430
431 attrib_spec:
432 [ [ r = record_spec -> mk loc (Record (true,r))
433 | "("; t = pat; ")" -> t
434 | "{"; r = record_spec; "}" -> mk loc (Record (true,r))
435 | "{|"; r = record_spec; "|}" -> mk loc (Record (false,r))
436 ] ];
437
438 expr_record_spec:
439 [ [ r = LIST1
440 [ l = [LIDENT | UIDENT | keyword ]; "="; x = expr ->
441 (label l,x) ]
442 SEP ";" ->
443 exp loc (RecordLitt (make_record loc r))
444 ] ];
445
446 expr_attrib_spec:
447 [ [ r = expr_record_spec -> r ]
448 | [ e = expr LEVEL "no_appl" -> e
449 | -> exp loc (RecordLitt (LabelMap.empty))
450 ]
451 ];
452 END
453
454 let pat' = Grammar.Entry.create gram "type/pattern expression"
455 EXTEND GLOBAL: pat pat';
456 pat': [ [ p = pat; EOI -> p ] ];
457 END
458
459 let pat = Grammar.Entry.parse pat
460 and expr = Grammar.Entry.parse expr
461 and prog = Grammar.Entry.parse prog
462
463
464 module From_string = struct
465 let pat s = Grammar.Entry.parse pat' (Stream.of_string s)
466 let expr s = expr (Stream.of_string s)
467 end
468

CVS Admin">CVS Admin
ViewVC Help
Powered by ViewVC 1.1.5