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

Contents of /parser/parser.ml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 24 - (show annotations)
Tue Jul 10 16:58:57 2007 UTC (5 years, 11 months ago) by abate
File size: 7523 byte(s)
[r2002-10-20 20:52:38 by cvscast] Empty log message

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

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