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

Contents of /parser/parser.ml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 26 - (hide annotations)
Tue Jul 10 16:59:08 2007 UTC (5 years, 10 months ago) by abate
File size: 7612 byte(s)
[r2002-10-20 23:34:54 by cvscast] Empty log message

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

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