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

Contents of /parser/parser.ml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 13 - (hide annotations)
Tue Jul 10 16:57:42 2007 UTC (5 years, 10 months ago) by abate
File size: 6009 byte(s)
[r2002-10-16 16:18:48 by cvscast] Empty log message

Original author: cvscast
Date: 2002-10-16 16:18:48+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 atom_nil = Types.mk_atom "nil"
12    
13     let rec multi_prod loc = function
14     | [ x ] -> x
15     | x :: l -> mk loc (Prod (x, multi_prod loc l))
16     | [] -> assert false
17    
18     let rec tuple loc = function
19     | [ x ] -> x
20     | x :: l -> mk loc (Pair (x, tuple loc l))
21     | [] -> assert false
22    
23     EXTEND
24 abate 10 GLOBAL: prog expr pat regexp const;
25 abate 4
26 abate 10 prog: [
27 abate 13 [ l = LIST0 [ p = phrase; ";;" -> mk loc p ]; ";;" -> l ]
28 abate 10 ];
29    
30 abate 13 phrase: [
31     [ e = expr -> EvalStatement e
32     | "type"; x = UIDENT; "="; t = pat -> TypeDecl (x,t) ]
33     ];
34    
35 abate 4 expr: [
36     "top" RIGHTA
37     [ "match"; e = SELF; "with"; b = branches -> mk loc (Match (e,b))
38     | "map"; e = SELF; "with"; b = branches -> mk loc (Map (e,b))
39     | "fun"; f = OPT LIDENT; "("; a = LIST1 arrow SEP ";"; ")";
40     b = branches ->
41     mk loc (Abstraction { fun_name = f; fun_iface = a; fun_body = b })
42     | (p,e1) = let_binding; "in"; e2 = expr LEVEL "top"->
43     mk loc (Match (e1,[p,e2]))
44     ]
45    
46     |
47     [ e1 = expr; e2 = expr -> mk loc (Apply (e1,e2))
48     ]
49    
50     | "no_appl"
51     [ c = const -> mk loc (Cst c)
52     | "("; l = LIST1 expr SEP ","; ")" -> tuple loc l
53     | "["; l = LIST0 expr LEVEL "no_appl"; "]" ->
54     tuple loc (l @ [mk noloc (Cst (Types.Atom atom_nil))])
55     | "["; l = LIST0 expr LEVEL "no_appl"; ";"; e = expr; "]" ->
56     tuple loc (l @ [e])
57     | "<"; t = expr_tag_spec; a = expr_attrib_spec; ">"; c = expr ->
58     tuple loc [t;a;c]
59     | "{"; r = [ expr_record_spec | -> mk loc (RecordLitt []) ]; "}" -> r
60     | a = LIDENT -> mk loc (Var a)
61     ]
62    
63     ];
64    
65     let_binding: [
66     [ "let"; p = pat; "="; e = expr -> (p,e)
67     | "let"; "fun"; f = LIDENT; "("; a = LIST0 arrow SEP ";"; ")";
68     b = branches ->
69     let p = mk loc (Capture f) in
70     let abst = { fun_name = Some f; fun_iface = a; fun_body = b } in
71     let e = mk loc (Abstraction abst) in
72     (p,e)
73     ]
74     ];
75    
76     arrow: [
77 abate 9 [ t1 = pat LEVEL "no_arrow"; "->"; t2 = pat -> (t1,t2)]
78 abate 4 ];
79    
80     branches: [
81     [ OPT "|"; l = LIST1 branch SEP "|" ; OPT "end" -> l ]
82     ];
83    
84     branch: [
85 abate 6 [ p = pat LEVEL "no_arrow"; "->"; e = expr -> (p,e) ]
86 abate 4 ];
87    
88    
89     regexp: [
90     [ x = regexp; "|"; y = regexp -> Alt (x,y) ]
91     | [ x = regexp; y = regexp -> Seq (x,y) ]
92     | [ a = LIDENT; "::"; x = regexp -> SeqCapture (a,x) ]
93     | [ x = regexp; "*" -> Star x
94     | x = regexp; "*?" -> WeakStar x
95     | x = regexp; "+" -> Seq (x, Star x)
96     | x = regexp; "+?" -> Seq (x, WeakStar x)
97     | x = regexp; "?" -> Alt (x, Epsilon)
98     | x = regexp; "??" -> Alt (Epsilon, x) ]
99     | [ "("; x = regexp; ")" -> x
100     | e = pat LEVEL "simple" -> Elem e
101     ]
102     ];
103    
104     pat: [
105     [ x = pat; "where";
106     b = LIST1 [ a = UIDENT; "="; y = pat -> (a,y)] SEP "and"
107     -> mk loc (Recurs (x,b)) ]
108     | RIGHTA [ x = pat; "->"; y = pat -> mk loc (Arrow (x,y)) ]
109 abate 6 | "no_arrow" [ x = pat; "|"; y = pat -> mk loc (Or (x,y)) ]
110 abate 4 | "simple" [ x = pat; "&"; y = pat -> mk loc (And (x,y))
111     | x = pat; "-"; y = pat -> mk loc (Diff (x,y)) ]
112     |
113     [ "{"; r = record_spec; "}" -> r
114     | UIDENT "Any" -> mk loc (Internal Types.any)
115     | LIDENT "_" -> mk loc (Internal Types.any)
116     | a = LIDENT -> mk loc (Capture a)
117     | "("; a = LIDENT; ":="; c = const; ")" -> mk loc (Constant (a,c))
118     | a = UIDENT -> mk loc (PatVar a)
119     | i = INT ; "--"; j = INT ->
120     let i = int_of_string i and j = int_of_string j in
121     mk loc (Internal (Types.interval i j))
122 abate 13 | i = char ; "--"; j = char ->
123     mk loc (Internal (Types.char_class i j))
124 abate 4 | c = const -> mk loc (Internal (Types.constant c))
125     | "("; l = LIST1 pat SEP ","; ")" -> multi_prod loc l
126     | "["; r = [ r = regexp -> r | -> Epsilon ];
127     q = [ ";"; q = pat -> q
128     | -> mk noloc (Internal (Types.atom atom_nil)) ];
129     "]" -> mk loc (Regexp (r,q))
130     | "<"; t = tag_spec; a = attrib_spec; ">"; c = pat ->
131     multi_prod loc [t;a;c]
132     ]
133    
134     ];
135    
136     record_spec:
137     [ [ r = LIST0 [ l = [LIDENT | UIDENT];
138     o = ["=?" -> true | "=" -> false];
139     x = pat ->
140     mk loc (Record (Types.label l,o,x))
141     ] SEP ";" ->
142     match r with
143 abate 9 | [] -> mk loc (Internal Types.Record.any)
144     | h::t -> List.fold_left (fun t1 t2 -> mk loc (And (t1,t2))) h t
145 abate 4 ] ];
146    
147 abate 13 char:
148     [
149     [ c = CHAR -> Chars.Unichar.from_char (Token.eval_char c)
150     | "!"; i = INT -> Chars.Unichar.from_int (int_of_string i) ]
151     ];
152    
153    
154 abate 4 const:
155     [
156     [ i = INT -> Types.Integer (int_of_string i)
157     | x = STRING -> Types.String (Token.eval_string x)
158 abate 13 | "`"; a = [LIDENT | UIDENT] -> Types.Atom (Types.mk_atom a)
159     | c = char -> Types.Char c ]
160 abate 4 ];
161    
162     tag_spec:
163     [
164     [ a = [LIDENT | UIDENT] ->
165     mk loc (Internal (Types.atom (Types.mk_atom a))) ]
166     | [ t = pat -> t ]
167     ];
168    
169     attrib_spec:
170     [ [ r = record_spec -> r | "("; t = pat; ")" -> t ] ];
171    
172     expr_record_spec:
173     [ [ r = LIST1
174     [ l = [LIDENT | UIDENT]; "="; x = expr -> (Types.label l,x) ]
175     SEP ";" ->
176     mk loc (RecordLitt r)
177     ] ];
178    
179     expr_tag_spec:
180     [
181     [ a = [LIDENT | UIDENT] ->
182     mk loc (Cst (Types.Atom (Types.mk_atom a))) ]
183     | [ e = expr LEVEL "no_appl" -> e ]
184     ];
185    
186     expr_attrib_spec:
187     [ [ r = expr_record_spec -> r ]
188     | [ e = expr LEVEL "no_appl" -> e
189     | -> mk loc (RecordLitt [])
190     ]
191     ];
192     END
193    
194 abate 10 let pat = Grammar.Entry.parse pat
195     let expr = Grammar.Entry.parse expr
196     let prog = Grammar.Entry.parse prog
197    
198     module From_string = struct
199     let pat s = pat (Stream.of_string s)
200     let expr s = expr (Stream.of_string s)
201 abate 4 end
202    

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