]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_disambiguation/cicTextualParser2.ml
- typo bugfix: INT token no longer exists
[helm.git] / helm / ocaml / cic_disambiguation / cicTextualParser2.ml
1 (* Copyright (C) 2004, 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 let debug = true
27 let debug_print s =
28   if debug then begin
29     prerr_endline "<NEW_TEXTUAL_PARSER>";
30     prerr_endline s;
31     prerr_endline "</NEW_TEXTUAL_PARSER>"
32   end
33
34 open Printf
35
36 exception Parse_error of string
37
38 let choice_of_uri (uri: string) =
39   let cic = HelmLibraryObjects.term_of_uri (UriManager.uri_of_string uri) in
40   (uri, (fun _ _ _ -> cic))
41
42 let grammar = Grammar.gcreate CicTextualLexer2.cic_lexer
43
44 let term = Grammar.Entry.create grammar "term"
45 let term0 = Grammar.Entry.create grammar "term0"
46 (* let tactic = Grammar.Entry.create grammar "tactic" *)
47 (* let tactical = Grammar.Entry.create grammar "tactical" *)
48
49 let return_term loc term = CicTextualParser2Ast.LocatedTerm (loc, term)
50 (* let return_term loc term = term *)
51
52 let fail (x, y) msg =
53   failwith (Printf.sprintf "Error at characters %d - %d: %s" x y msg)
54
55 EXTEND
56   GLOBAL: term term0;
57   meta_subst: [
58     [ s = SYMBOL "_" -> None
59     | t = term -> Some t ]
60   ];
61   binder: [
62     [ SYMBOL <:unicode<lambda>> (* λ *) -> `Lambda
63     | SYMBOL <:unicode<pi>> (* π *) -> `Pi
64     | SYMBOL <:unicode<exists>> (* ∃ *) -> `Exists
65     | SYMBOL <:unicode<forall>> (* ∀ *) -> `Forall
66     ]
67   ];
68   substituted_name: [ (* a subs.name is an explicit substitution subject *)
69     [ s = [ IDENT | SYMBOL ];
70       subst = OPT [
71         SYMBOL "\subst";  (* to avoid catching frequent "a [1]" cases *)
72         PAREN "[";
73         substs = LIST1 [
74           i = IDENT; SYMBOL <:unicode<Assign>> (* ≔ *); t = term -> (i, t)
75         ] SEP SYMBOL ";";
76         PAREN "]" ->
77           substs
78       ] ->
79         (match subst with
80         | Some l -> CicTextualParser2Ast.Ident (s, l)
81         | None -> CicTextualParser2Ast.Ident (s, []))
82     ]
83   ];
84   name: [ (* as substituted_name with no explicit substitution *)
85     [ s = [ IDENT | SYMBOL ] -> s ]
86   ];
87   pattern: [
88     [ n = name -> [n]
89     | PAREN "("; names = LIST1 name; PAREN ")" -> names ]
90   ];
91   term0: [ [ t = term -> return_term loc t ] ];
92   term:
93     [ "arrow" RIGHTA
94       [ t1 = term; SYMBOL <:unicode<to>>; t2 = term ->
95           return_term loc
96             (CicTextualParser2Ast.Binder (`Pi, Cic.Anonymous, Some t1, t2))
97       ]
98     | "binder" RIGHTA
99       [
100         b = binder; vars = LIST1 IDENT SEP SYMBOL ",";
101         typ = OPT [ SYMBOL ":"; t = term -> t ];
102         SYMBOL "."; body = term ->
103           let binder =
104             List.fold_right
105               (fun var body ->
106                 let name =
107                   match var with
108                   | "_" -> Cic.Anonymous
109                   | var -> Cic.Name var
110                 in
111                 CicTextualParser2Ast.Binder (b, name, typ, body))
112               vars body
113           in
114           return_term loc binder
115       ]
116     | "eq" LEFTA
117       [ t1 = term; SYMBOL "="; t2 = term ->
118         return_term loc (CicTextualParser2Ast.Appl_symbol ("eq", 0, [t1; t2]))
119       ]
120     | "add" LEFTA     [ (* nothing here by default *) ]
121     | "mult" LEFTA    [ (* nothing here by default *) ]
122     | "inv" NONA      [ (* nothing here by default *) ]
123     | "simple" NONA
124       [
125         sort_kind = [
126           "Prop" -> `Prop | "Set" -> `Set | "Type" -> `Type | "CProp" -> `CProp
127         ] ->
128           CicTextualParser2Ast.Sort sort_kind
129       | n = substituted_name -> return_term loc n
130       | PAREN "("; head = term; args = LIST1 term; PAREN ")" ->
131           return_term loc (CicTextualParser2Ast.Appl (head :: args))
132       | i = NUM -> return_term loc (CicTextualParser2Ast.Num (i, 0))
133       | m = META;
134         substs = [
135           PAREN "["; substs = LIST0 meta_subst SEP SYMBOL ";" ; PAREN "]" ->
136             substs
137         ] ->
138             let index =
139               try
140                 int_of_string (String.sub m 1 (String.length m - 1))
141               with Failure "int_of_string" ->
142                 fail loc ("Invalid meta variable number: " ^ m)
143             in
144             return_term loc (CicTextualParser2Ast.Meta (index, substs))
145         (* actually "in" and "and" are _not_ keywords. Parsing works anyway
146          * since applications are required to be bound by parens *)
147       | "let"; name = IDENT;
148 (*         SYMBOL <:unicode<def>> (* ≝ *); *)
149         SYMBOL "=";
150         t1 = term;
151         IDENT "in"; t2 = term ->
152           return_term loc (CicTextualParser2Ast.LetIn (name, t1, t2))
153       | "let"; ind_kind = [ "corec" -> `CoInductive | "rec"-> `Inductive ];
154           defs = LIST1 [
155             name = IDENT;
156             index = OPT [ PAREN "("; index = NUM; PAREN ")" ->
157               int_of_string index
158             ];
159             typ = OPT [ SYMBOL ":"; typ = term -> typ ];
160 (*             SYMBOL <:unicode<def>> (* ≝ *); *)
161             SYMBOL "=";
162             t1 = term ->
163               (name, t1, typ, (match index with None -> 1 | Some i -> i))
164           ] SEP (IDENT "and");
165           IDENT "in"; body = term ->
166             return_term loc (CicTextualParser2Ast.LetRec (ind_kind, defs, body))
167       | outtyp = OPT [ PAREN "["; typ = term; PAREN "]" -> typ ];
168         "match"; t = term;
169         SYMBOL ":"; indty = IDENT;
170         "with";
171         PAREN "[";
172         patterns = LIST0 [
173           p = pattern; SYMBOL <:unicode<Rightarrow>> (* ⇒ *); t = term ->
174             (p, t)
175         ] SEP SYMBOL "|";
176         PAREN "]" ->
177           return_term loc
178             (CicTextualParser2Ast.Case (t, indty, outtyp, patterns))
179       | PAREN "("; t = term; PAREN ")" -> return_term loc t
180       ]
181     ];
182 END
183
184 let parse_term stream =
185   try
186     Grammar.Entry.parse term0 stream
187   with Stdpp.Exc_located ((x, y), exn) ->
188     raise (Parse_error (sprintf "parse error at characters %d-%d: %s" x y
189         (Printexc.to_string exn)))
190
191 (**/**)
192
193 (** {2 Interface for gTopLevel} *)
194
195 open DisambiguateTypes
196
197 module EnvironmentP3 =
198   struct
199     type t = environment
200
201     let empty = ""
202
203     let aliases_grammar = Grammar.gcreate CicTextualLexer2.cic_lexer
204     let aliases = Grammar.Entry.create aliases_grammar "aliases"
205
206     let to_string env =
207       let aliases =
208         Environment.fold
209           (fun domain_item (dsc, _) acc ->
210             let s =
211               match domain_item with
212               | Id id -> sprintf "alias id %s = %s" id dsc
213               | Symbol (symb, instance) ->
214                   sprintf "alias symbol \"%s\" (instance %d) = \"%s\""
215                     symb instance dsc
216               | Num instance ->
217                   sprintf "alias num (instance %d) = \"%s\"" instance dsc
218             in
219             s :: acc)
220           env []
221       in
222       String.concat "\n" (List.sort compare aliases)
223
224     EXTEND
225       GLOBAL: aliases;
226       aliases: [  (* build an environment from an aliases list *)
227         [ aliases = LIST0 alias; EOI ->
228             List.fold_left
229               (fun env (domain_item, codomain_item) ->
230                 Environment.add domain_item codomain_item env)
231               Environment.empty aliases
232         ]
233       ];
234       alias: [  (* return a pair <domain_item, codomain_item> from an alias *)
235         [ IDENT "alias";
236           choice =
237             [ IDENT "id"; id = IDENT; SYMBOL "="; uri = URI ->
238                 (Id id, choice_of_uri uri)
239             | IDENT "symbol"; symbol = QSTRING;
240               PAREN "("; IDENT "instance"; instance = NUM; PAREN ")";
241               SYMBOL "="; dsc = QSTRING ->
242                 (Symbol (symbol, int_of_string instance),
243                  DisambiguateChoices.lookup_symbol_by_dsc symbol dsc)
244             | IDENT "num";
245               PAREN "("; IDENT "instance"; instance = NUM; PAREN ")";
246               SYMBOL "="; dsc = QSTRING ->
247                 (Num (int_of_string instance),
248                  DisambiguateChoices.lookup_num_by_dsc dsc)
249             ] -> choice ]
250       ];
251     END
252
253     let of_string s =
254       if s = empty then
255         Environment.empty
256       else
257         try
258           Grammar.Entry.parse aliases (Stream.of_string s)
259         with Stdpp.Exc_located ((x, y), exn) ->
260           raise (Parse_error (sprintf "parse error at characters %d-%d: %s" x y
261           (Printexc.to_string exn)))
262   end
263