]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_notation/cicNotationParser.ml
snapshot, notably:
[helm.git] / helm / ocaml / cic_notation / cicNotationParser.ml
1 (* Copyright (C) 2005, HELM Team.
2  * 
3  * This file is part of HELM, an Hypertextual, Electronic
4  * Library of Mathematics, developed at the Computer Science
5  * Department, University of Bologna, Italy.
6  * 
7  * HELM is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * 
12  * HELM is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with HELM; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://helm.cs.unibo.it/
24  *)
25
26 open Printf
27
28 exception Parse_error of Token.flocation * string
29
30 module Level1Lexer =
31 struct
32   type te = string * string
33   let lexer = CicNotationLexer.syntax_pattern_lexer
34 end
35 module Level1Parser = Grammar.GMake (Level1Lexer)
36
37 module Level2Lexer =
38 struct
39   type te = string * string
40   let lexer = CicNotationLexer.ast_pattern_lexer
41 end
42 module Level2Parser = Grammar.GMake (Level2Lexer)
43
44 let level1_pattern = Level1Parser.Entry.create "level1_pattern"
45 let level2_pattern = Level2Parser.Entry.create "level2_pattern"
46
47 let return_term loc term = ()
48
49 (*let fail floc msg =*)
50 (*  let (x, y) = CicAst.loc_of_floc floc in*)
51 (*  failwith (sprintf "Error at characters %d - %d: %s" x y msg)*)
52
53 let int_of_string s =
54   try
55     Pervasives.int_of_string s
56   with Failure _ ->
57     failwith (sprintf "Lexer failure: string_of_int \"%s\" failed" s)
58
59 (* {{{ Grammar for concrete syntax patterns, notation level 1 *)
60 GEXTEND Level1Parser
61   GLOBAL: level1_pattern;
62   level1_pattern: [ [ p = pattern -> () ] ];
63   pattern: [ [ p = LIST1 simple_pattern -> () ] ];
64   literal: [
65     [ s = SYMBOL -> ()
66     | k = KEYWORD -> ()
67     ]
68   ];
69   sep:       [ [ SYMBOL "\\SEP";      sep = literal -> () ] ];
70   row_sep:   [ [ SYMBOL "\\ROWSEP";   sep = literal -> () ] ];
71   field_sep: [ [ SYMBOL "\\FIELDSEP"; sep = literal -> () ] ];
72   box_pattern: [
73     [ SYMBOL "\\HBOX"; p = simple_pattern -> ()
74     | SYMBOL "\\VBOX"; p = simple_pattern -> ()
75     | SYMBOL "\\BREAK" -> ()
76     ]
77   ];
78   magic_pattern: [
79     [ SYMBOL "\\LIST0"; p = simple_pattern; sep = OPT sep -> ()
80     | SYMBOL "\\LIST1"; p = simple_pattern; sep = OPT sep -> ()
81     | SYMBOL "\\OPT"; p = simple_pattern -> ()
82     ]
83   ];
84   pattern_variable: [
85     [ id = IDENT -> ()
86     | SYMBOL "\\NUM"; id = IDENT -> ()
87     | SYMBOL "\\IDENT"; id = IDENT -> ()
88     ]
89   ];
90   simple_pattern:
91     [ "layout" LEFTA
92       [ p1 = SELF; SYMBOL "\\SUB"; p2 = SELF -> ()
93       | p1 = SELF; SYMBOL "\\SUP"; p2 = SELF -> ()
94       | p1 = SELF; SYMBOL "\\BELOW"; p2 = SELF -> ()
95       | p1 = SELF; SYMBOL "\\ABOVE"; p2 = SELF -> ()
96       | SYMBOL "["; p1 = pattern; SYMBOL "\\OVER"; p2 = pattern; SYMBOL "]" ->
97           ()
98       | SYMBOL "["; p1 = pattern; SYMBOL "\\ATOP"; p2 = pattern; SYMBOL "]" ->
99           ()
100       | SYMBOL "\\ARRAY"; p = SELF; fsep = OPT field_sep; rsep = OPT row_sep ->
101           ()
102       | SYMBOL "\\FRAC"; p1 = SELF; p2 = SELF -> ()
103       | SYMBOL "\\SQRT"; p = SELF -> ()
104       | SYMBOL "\\ROOT"; arg = pattern; SYMBOL "\\OF"; index = SELF -> ()
105       ]
106     | "simple" NONA
107       [ m = magic_pattern -> ()
108       | v = pattern_variable -> ()
109       | b = box_pattern -> ()
110       | n = NUMBER -> ()
111       | SYMBOL "["; p = pattern; SYMBOL "]" -> ()
112       ]
113     ];
114 END
115 (* }}} *)
116
117 (* {{{ Grammar for ast patterns, notation level 2 *)
118 GEXTEND Level2Parser
119   GLOBAL: level2_pattern;
120   level2_pattern: [ [ p = pattern -> () ] ];
121   sort: [
122     [ SYMBOL "\\PROP" -> ()
123     | SYMBOL "\\SET" -> ()
124     | SYMBOL "\\TYPE" -> ()
125     ]
126   ];
127   explicit_subst: [
128     [ (* TODO explicit substitution *)
129     ]
130   ];
131   meta_subst: [
132     [ (* TODO meta substitution *)
133     ]
134   ];
135   possibly_typed_name: [
136     [ SYMBOL "("; i = IDENT; SYMBOL ":"; typ = pattern; SYMBOL ")" -> ()
137     | i = IDENT -> ()
138     ]
139   ];
140   match_pattern: [
141     [ n = IDENT -> ()
142     | SYMBOL "("; head = IDENT; vars = LIST1 possibly_typed_name; SYMBOL ")" ->
143         ()
144     ]
145   ];
146   binder: [
147     [ SYMBOL <:unicode<Pi>>     (* Π *) -> ()
148     | SYMBOL <:unicode<exists>> (* ∃ *) -> ()
149     | SYMBOL <:unicode<forall>> (* ∀ *) -> ()
150     | SYMBOL <:unicode<lambda>> (* λ *) -> ()
151     ]
152   ];
153   bound_names: [
154     [ vars = LIST1 IDENT SEP SYMBOL ",";
155       typ = OPT [ SYMBOL ":"; p = pattern -> () ] -> ()
156     | LIST1 [
157         SYMBOL "("; vars = LIST1 IDENT SEP SYMBOL ",";
158         typ = OPT [ SYMBOL ":"; p = pattern -> () ]; SYMBOL ")" -> ()
159       ] ->
160         ()
161     ]
162   ];
163   induction_kind: [
164     [ IDENT "rec" -> ()
165     | IDENT "corec" -> ()
166     ]
167   ];
168   let_defs: [
169     [ defs = LIST1 [
170         name = IDENT; args = bound_names;
171         index_name = OPT [ IDENT "on"; idx = IDENT -> () ];
172         ty = OPT [ SYMBOL ":" ; p = pattern -> () ];
173         SYMBOL <:unicode<def>> (* ≝ *); body = pattern ->
174           ()
175       ] SEP IDENT "and" ->
176         ()
177     ]
178   ];
179   pattern_variable: [
180     [ SYMBOL "\\NUM"; id = IDENT -> ()
181     | SYMBOL "\\IDENT"; id = IDENT -> ()
182     | SYMBOL "\\FRESH"; id = IDENT -> ()
183     ]
184   ];
185   magic_pattern: [
186     [ SYMBOL "\\FOLD"; n = OPT NUMBER; [ IDENT "left" | IDENT "right" ];
187       LIST1 IDENT; SYMBOL "."; p1 = pattern;
188       OPT [ SYMBOL "\\LAMBDA"; LIST1 IDENT ]; p2 = pattern ->
189         ()
190     | SYMBOL "\\DEFAULT"; id = IDENT; p1 = pattern; p2 = pattern -> ()
191     ]
192   ];
193   pattern:
194     [ "letin" NONA
195       [ IDENT "let"; var = possibly_typed_name; SYMBOL <:unicode<def>> (* ≝ *);
196         p1 = pattern; "in"; p2 = pattern ->
197           ()
198       | IDENT "let"; k = induction_kind; defs = let_defs; IDENT "in";
199         body = pattern ->
200           ()
201       ]
202     | "binder" RIGHTA
203       [ b = binder; bound_names; SYMBOL "."; body = pattern -> () ]
204     | "extension"
205       [ ]
206     | "apply" LEFTA
207       [ p1 = pattern; p2 = pattern -> () ]
208     | "simple" NONA
209       [ i = IDENT -> ()
210       | i = IDENT; s = explicit_subst -> ()
211       | n = NUMBER -> ()
212       | IMPLICIT -> ()
213       | m = META -> ()
214       | m = META; s = meta_subst -> ()
215       | s = sort -> ()
216       | s = SYMBOL -> ()
217       | u = URI -> ()
218       | outtyp = OPT [ SYMBOL "["; typ = pattern; SYMBOL "]" ];
219         IDENT "match"; t = pattern;
220         indty_ident = OPT [ SYMBOL ":"; id = IDENT ];
221         IDENT "with"; SYMBOL "[";
222         patterns = LIST0 [
223           lhs = match_pattern; SYMBOL <:unicode<Rightarrow>> (* ⇒ *);
224           rhs = pattern ->
225             ()
226         ] SEP SYMBOL "|";
227         SYMBOL "]" ->
228           ()
229       | SYMBOL "("; p1 = pattern; SYMBOL ":"; p2 = pattern; SYMBOL ")" -> ()
230       | SYMBOL "("; p = pattern; SYMBOL ")" -> ()
231       | v = pattern_variable -> ()
232       | m = magic_pattern -> ()
233       ]
234     ];
235 END
236 (* }}} *)
237
238 let exc_located_wrapper f =
239   try
240     f ()
241   with
242   | Stdpp.Exc_located (floc, Stream.Error msg) ->
243       raise (Parse_error (floc, msg))
244   | Stdpp.Exc_located (floc, exn) ->
245       raise (Parse_error (floc, (Printexc.to_string exn)))
246
247 let parse_syntax_pattern stream =
248   exc_located_wrapper
249     (fun () ->
250       (Level1Parser.Entry.parse level1_pattern (Level1Parser.parsable stream)))
251
252 let parse_ast_pattern stream =
253   exc_located_wrapper
254     (fun () ->
255       (Level2Parser.Entry.parse level2_pattern (Level2Parser.parsable stream)))
256
257 (* vim:set encoding=utf8 foldmethod=marker: *)