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

Contents of /parser/parser.ml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 52 - (hide annotations)
Tue Jul 10 17:01:20 2007 UTC (5 years, 10 months ago) by abate
File size: 8399 byte(s)
[r2002-10-26 20:45:22 by cvscast] Empty log message

Original author: cvscast
Date: 2002-10-26 20:45:22+00:00
1 abate 4 open Location
2     open Ast
3    
4 abate 38 (* let () = Grammar.error_verbose := true *)
5     let gram = Grammar.gcreate (Lexer.gmake ())
6     let prog = Grammar.Entry.create gram "prog"
7     let expr = Grammar.Entry.create gram "expression"
8     let pat = Grammar.Entry.create gram "type/pattern expression"
9     let regexp = Grammar.Entry.create gram "type/pattern regexp"
10     let const = Grammar.Entry.create gram "scalar constant"
11    
12     let rec multi_prod loc = function
13     | [ x ] -> x
14     | x :: l -> mk loc (Prod (x, multi_prod loc l))
15     | [] -> assert false
16    
17     let rec tuple loc = function
18     | [ x ] -> x
19     | x :: l -> mk (x.loc) (Pair (x, tuple loc l))
20     | [] -> assert false
21    
22     let char = mk noloc (Internal (Types.char Chars.any))
23     let string = Star (Elem char)
24    
25     let cst_nil = mk noloc (Cst (Types.Atom Sequence.nil_atom))
26    
27     let seq_of_string s =
28     let rec aux accu i = if (i = 0) then accu else aux (s.[i-1]::accu) (i-1) in
29     aux [] (String.length s)
30 abate 4
31 abate 18
32 abate 38 let char_list loc s =
33     let s = seq_of_string (Token.eval_string s) in
34     List.map (fun c -> mk loc (Cst (Types.Char (Chars.Unichar.from_char c)))) s
35 abate 18
36    
37 abate 38 EXTEND
38 abate 10 GLOBAL: prog expr pat regexp const;
39 abate 4
40 abate 10 prog: [
41 abate 14 [ l = LIST0 [ p = phrase; ";;" -> mk loc p ]; EOI -> l ]
42 abate 10 ];
43    
44 abate 13 phrase: [
45     [ e = expr -> EvalStatement e
46 abate 43 | "type"; x = UIDENT; "="; t = pat -> TypeDecl (x,t)
47     | "debug"; d = debug_directive -> Debug d
48     ]
49 abate 13 ];
50    
51 abate 43 debug_directive: [
52     [ LIDENT "filter"; t = pat; p = pat -> `Filter(t,p)
53     | LIDENT "accept"; p = pat -> `Accept p;
54     | LIDENT "compile"; t = pat; p = LIST1 pat -> `Compile (t,p)
55     ]
56     ];
57    
58 abate 4 expr: [
59     "top" RIGHTA
60     [ "match"; e = SELF; "with"; b = branches -> mk loc (Match (e,b))
61     | "map"; e = SELF; "with"; b = branches -> mk loc (Map (e,b))
62 abate 17 | "transform"; e = SELF; "with"; b = branches ->
63     mk noloc (Op ("flatten", [mk loc (Map (e,b))]))
64 abate 48 | "fun"; (f,a,b) = fun_decl ->
65 abate 4 mk loc (Abstraction { fun_name = f; fun_iface = a; fun_body = b })
66     | (p,e1) = let_binding; "in"; e2 = expr LEVEL "top"->
67     mk loc (Match (e1,[p,e2]))
68     ]
69    
70    
71 abate 15 |
72 abate 51 [ e1 = expr; op = ["+" | "-" | "@"]; e2 = expr -> mk loc (Op (op,[e1;e2]))
73     ]
74 abate 16 |
75 abate 51 [ e1 = expr; op = ["*" | "/"]; e2 = expr -> mk loc (Op (op,[e1;e2]))
76     ]
77 abate 26 |
78 abate 51 [ e = expr; "."; l = [LIDENT | UIDENT] -> mk loc (Dot (e,Types.label l))
79     ]
80 abate 26
81 abate 52 |
82     [ LIDENT "flatten"; e = expr -> mk loc (Op ("flatten",[e]))
83     | e1 = expr; e2 = expr -> mk loc (Apply (e1,e2))
84     ]
85    
86 abate 4 | "no_appl"
87     [ c = const -> mk loc (Cst c)
88     | "("; l = LIST1 expr SEP ","; ")" -> tuple loc l
89 abate 18 | "["; l = LIST0 seq_elem; e = OPT [ ";"; e = expr -> e ]; "]" ->
90     let e = match e with Some e -> e | None -> cst_nil in
91     let l = List.flatten l in
92 abate 4 tuple loc (l @ [e])
93     | "<"; t = expr_tag_spec; a = expr_attrib_spec; ">"; c = expr ->
94     tuple loc [t;a;c]
95     | "{"; r = [ expr_record_spec | -> mk loc (RecordLitt []) ]; "}" -> r
96 abate 38 | s = STRING ->
97     tuple loc (char_list loc s @ [cst_nil])
98 abate 18 | "!"; t = pat -> mk loc (DebugTyper t)
99 abate 4 | a = LIDENT -> mk loc (Var a)
100     ]
101    
102     ];
103 abate 18
104     seq_elem: [
105 abate 38 [ x = [CHAR | STRING] -> char_list loc x
106 abate 18 | e = expr LEVEL "no_appl" -> [e]
107     ]
108     ];
109 abate 4
110     let_binding: [
111     [ "let"; p = pat; "="; e = expr -> (p,e)
112 abate 48 | "let"; "fun"; (f,a,b) = fun_decl ->
113     let p = match f with
114     | Some x -> mk loc (Capture x)
115     | _ -> failwith "Function name mandatory in let fun declarations"
116     in
117     let abst = { fun_name = f; fun_iface = a; fun_body = b } in
118 abate 4 let e = mk loc (Abstraction abst) in
119 abate 48 (p,e);
120 abate 4 ]
121     ];
122    
123 abate 48 fun_decl: [
124     [ f = OPT LIDENT; "("; a = LIST0 arrow SEP ";"; ")"; b = branches ->
125     (f,a,b)
126     | f = OPT LIDENT; "("; arg = LIDENT; ":"; targ = pat; ")"; ":"; tres = pat ;
127     "="; body = expr ->
128     let b = [mk noloc (Capture arg), body] in
129     let a = [targ,tres] in
130     (f,a,b)
131     ]
132     ];
133    
134 abate 4 arrow: [
135 abate 9 [ t1 = pat LEVEL "no_arrow"; "->"; t2 = pat -> (t1,t2)]
136 abate 4 ];
137    
138     branches: [
139     [ OPT "|"; l = LIST1 branch SEP "|" ; OPT "end" -> l ]
140     ];
141    
142     branch: [
143 abate 6 [ p = pat LEVEL "no_arrow"; "->"; e = expr -> (p,e) ]
144 abate 4 ];
145    
146    
147     regexp: [
148     [ x = regexp; "|"; y = regexp -> Alt (x,y) ]
149     | [ x = regexp; y = regexp -> Seq (x,y) ]
150     | [ a = LIDENT; "::"; x = regexp -> SeqCapture (a,x) ]
151     | [ x = regexp; "*" -> Star x
152     | x = regexp; "*?" -> WeakStar x
153     | x = regexp; "+" -> Seq (x, Star x)
154     | x = regexp; "+?" -> Seq (x, WeakStar x)
155     | x = regexp; "?" -> Alt (x, Epsilon)
156     | x = regexp; "??" -> Alt (Epsilon, x) ]
157     | [ "("; x = regexp; ")" -> x
158 abate 18 | UIDENT "String" -> string
159 abate 38 | s = CHAR ->
160 abate 18 let s = seq_of_string (Token.eval_string s) in
161     List.fold_right
162     (fun c accu ->
163     let c = Chars.Unichar.from_char c in
164     let c = Chars.atom c in
165     Seq (Elem (mk loc (Internal (Types.char c))), accu))
166     s
167     Epsilon
168 abate 4 | e = pat LEVEL "simple" -> Elem e
169     ]
170     ];
171    
172     pat: [
173     [ x = pat; "where";
174     b = LIST1 [ a = UIDENT; "="; y = pat -> (a,y)] SEP "and"
175     -> mk loc (Recurs (x,b)) ]
176     | RIGHTA [ x = pat; "->"; y = pat -> mk loc (Arrow (x,y)) ]
177 abate 6 | "no_arrow" [ x = pat; "|"; y = pat -> mk loc (Or (x,y)) ]
178 abate 4 | "simple" [ x = pat; "&"; y = pat -> mk loc (And (x,y))
179     | x = pat; "-"; y = pat -> mk loc (Diff (x,y)) ]
180     |
181     [ "{"; r = record_spec; "}" -> r
182     | LIDENT "_" -> mk loc (Internal Types.any)
183     | a = LIDENT -> mk loc (Capture a)
184     | "("; a = LIDENT; ":="; c = const; ")" -> mk loc (Constant (a,c))
185     | a = UIDENT -> mk loc (PatVar a)
186     | i = INT ; "--"; j = INT ->
187 abate 15 let i = Big_int.big_int_of_string i
188     and j = Big_int.big_int_of_string j in
189 abate 52 mk loc (Internal (Types.interval (Intervals.bounded i j)))
190     | i = INT ->
191     let i = Big_int.big_int_of_string i in
192     mk loc (Internal (Types.interval (Intervals.atom i)))
193     | "*--"; j = INT ->
194     let j = Big_int.big_int_of_string j in
195     mk loc (Internal (Types.interval (Intervals.left j)))
196     | i = INT; "--*" ->
197     let i = Big_int.big_int_of_string i in
198     mk loc (Internal (Types.interval (Intervals.right i)))
199 abate 19 | i = char ->
200     mk loc (Internal (Types.char (Chars.char_class i i)))
201 abate 13 | i = char ; "--"; j = char ->
202 abate 18 mk loc (Internal (Types.char (Chars.char_class i j)))
203 abate 4 | c = const -> mk loc (Internal (Types.constant c))
204     | "("; l = LIST1 pat SEP ","; ")" -> multi_prod loc l
205     | "["; r = [ r = regexp -> r | -> Epsilon ];
206     q = [ ";"; q = pat -> q
207 abate 18 | -> mk noloc (Internal (Sequence.nil_type)) ];
208 abate 4 "]" -> mk loc (Regexp (r,q))
209     | "<"; t = tag_spec; a = attrib_spec; ">"; c = pat ->
210     multi_prod loc [t;a;c]
211     ]
212    
213     ];
214    
215     record_spec:
216     [ [ r = LIST0 [ l = [LIDENT | UIDENT];
217     o = ["=?" -> true | "=" -> false];
218     x = pat ->
219     mk loc (Record (Types.label l,o,x))
220     ] SEP ";" ->
221     match r with
222 abate 9 | [] -> mk loc (Internal Types.Record.any)
223     | h::t -> List.fold_left (fun t1 t2 -> mk loc (And (t1,t2))) h t
224 abate 4 ] ];
225    
226 abate 13 char:
227     [
228     [ c = CHAR -> Chars.Unichar.from_char (Token.eval_char c)
229     | "!"; i = INT -> Chars.Unichar.from_int (int_of_string i) ]
230     ];
231    
232    
233 abate 4 const:
234     [
235 abate 15 [ i = INT -> Types.Integer (Big_int.big_int_of_string i)
236 abate 18 | "`"; a = [LIDENT | UIDENT] -> Types.Atom (Types.mk_atom a)
237 abate 13 | c = char -> Types.Char c ]
238 abate 4 ];
239    
240     tag_spec:
241     [
242 abate 18 [ LIDENT "_" -> mk loc (Internal (Types.atom (Atoms.any))) ]
243     | [ a = [LIDENT | UIDENT] ->
244     mk loc (Internal (Types.atom (Atoms.atom (Types.mk_atom a)))) ]
245 abate 4 | [ t = pat -> t ]
246     ];
247    
248     attrib_spec:
249     [ [ r = record_spec -> r | "("; t = pat; ")" -> t ] ];
250    
251     expr_record_spec:
252     [ [ r = LIST1
253     [ l = [LIDENT | UIDENT]; "="; x = expr -> (Types.label l,x) ]
254     SEP ";" ->
255     mk loc (RecordLitt r)
256     ] ];
257    
258     expr_tag_spec:
259     [
260     [ a = [LIDENT | UIDENT] ->
261     mk loc (Cst (Types.Atom (Types.mk_atom a))) ]
262     | [ e = expr LEVEL "no_appl" -> e ]
263     ];
264    
265     expr_attrib_spec:
266     [ [ r = expr_record_spec -> r ]
267     | [ e = expr LEVEL "no_appl" -> e
268     | -> mk loc (RecordLitt [])
269     ]
270     ];
271     END
272    
273 abate 10 let pat = Grammar.Entry.parse pat
274     let expr = Grammar.Entry.parse expr
275     let prog = Grammar.Entry.parse prog
276    
277     module From_string = struct
278     let pat s = pat (Stream.of_string s)
279     let expr s = expr (Stream.of_string s)
280 abate 4 end
281    

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