]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_disambiguation/cicTextualParser2.ml
test for empty string given, if so return Environment.empty without invoking the...
[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     | "eq" LEFTA
99       [ t1 = term; SYMBOL "="; t2 = term ->
100         return_term loc (CicTextualParser2Ast.Appl_symbol ("eq", 0, [t1; t2]))
101       ]
102     | "add" LEFTA     [ (* nothing here by default *) ]
103     | "mult" LEFTA    [ (* nothing here by default *) ]
104     | "inv" NONA      [ (* nothing here by default *) ]
105     | "simple" NONA
106       [
107         b = binder; vars = LIST1 IDENT SEP SYMBOL ",";
108         typ = OPT [ SYMBOL ":"; t = term -> t ];
109         SYMBOL "."; body = term ->
110           let binder =
111             List.fold_right
112               (fun var body ->
113                 let name =
114                   match var with
115                   | "_" -> Cic.Anonymous
116                   | var -> Cic.Name var
117                 in
118                 CicTextualParser2Ast.Binder (b, name, typ, body))
119               vars body
120           in
121           return_term loc binder
122       | sort_kind = [
123           "Prop" -> `Prop | "Set" -> `Set | "Type" -> `Type | "CProp" -> `CProp
124         ] ->
125           CicTextualParser2Ast.Sort sort_kind
126       | n = substituted_name -> return_term loc n
127       | PAREN "("; head = term; args = LIST1 term; PAREN ")" ->
128           return_term loc (CicTextualParser2Ast.Appl (head :: args))
129       | i = NUM -> return_term loc (CicTextualParser2Ast.Num (i, 0))
130       | m = META;
131         substs = [
132           PAREN "["; substs = LIST0 meta_subst SEP SYMBOL ";" ; PAREN "]" ->
133             substs
134         ] ->
135             let index =
136               try
137                 int_of_string (String.sub m 1 (String.length m - 1))
138               with Failure "int_of_string" ->
139                 fail loc ("Invalid meta variable number: " ^ m)
140             in
141             return_term loc (CicTextualParser2Ast.Meta (index, substs))
142         (* actually "in" and "and" are _not_ keywords. Parsing works anyway
143          * since applications are required to be bound by parens *)
144       | "let"; name = IDENT; SYMBOL <:unicode<def>> (* ≝ *); t1 = term;
145         IDENT "in"; t2 = term ->
146           return_term loc (CicTextualParser2Ast.LetIn (name, t1, t2))
147       | "let"; ind_kind = [ "corec" -> `CoInductive | "rec"-> `Inductive ];
148           defs = LIST1 [
149           name = IDENT;
150           index = OPT [ PAREN "("; index = INT; PAREN ")" ->
151             int_of_string index
152           ];
153           typ = OPT [ SYMBOL ":"; typ = term -> typ ];
154           SYMBOL <:unicode<def>> (* ≝ *); t1 = term ->
155             (name, t1, typ, (match index with None -> 1 | Some i -> i))
156         ] SEP (IDENT "and");
157         IDENT "in"; body = term ->
158           return_term loc (CicTextualParser2Ast.LetRec (ind_kind, defs, body))
159       | outtyp = OPT [ PAREN "["; typ = term; PAREN "]" -> typ ];
160         "match"; t = term;
161         SYMBOL ":"; indty = IDENT;
162         "with";
163         PAREN "[";
164         patterns = LIST0 [
165           p = pattern; SYMBOL <:unicode<Rightarrow>> (* ⇒ *); t = term ->
166             (p, t)
167         ] SEP SYMBOL "|";
168         PAREN "]" ->
169           return_term loc
170             (CicTextualParser2Ast.Case (t, indty, outtyp, patterns))
171       | PAREN "("; t = term; PAREN ")" -> return_term loc t
172       ]
173     ];
174 END
175
176 let parse_term stream =
177   try
178     Grammar.Entry.parse term0 stream
179   with Stdpp.Exc_located ((x, y), exn) ->
180     raise (Parse_error (sprintf "parse error at characters %d-%d: %s" x y
181         (Printexc.to_string exn)))
182
183 (**/**)
184
185 (** {2 Interface for gTopLevel} *)
186
187 open DisambiguateTypes
188
189 module EnvironmentP3 =
190   struct
191     type t = environment
192
193     let empty = ""
194
195     let aliases_grammar = Grammar.gcreate CicTextualLexer2.cic_lexer
196     let aliases = Grammar.Entry.create aliases_grammar "aliases"
197
198     let to_string env =
199       let aliases =
200         Environment.fold
201           (fun domain_item (dsc, _) acc ->
202             let s =
203               match domain_item with
204               | Id id -> sprintf "alias id %s = %s" id dsc
205               | Symbol (symb, instance) ->
206                   sprintf "alias symbol \"%s\" (instance %d) = \"%s\""
207                     symb instance dsc
208               | Num instance ->
209                   sprintf "alias num (instance %d) = \"%s\"" instance dsc
210             in
211             s :: acc)
212           env []
213       in
214       String.concat "\n" (List.sort compare aliases)
215
216     EXTEND
217       GLOBAL: aliases;
218       aliases: [  (* build an environment from an aliases list *)
219         [ aliases = LIST0 alias; EOI ->
220             List.fold_left
221               (fun env (domain_item, codomain_item) ->
222                 Environment.add domain_item codomain_item env)
223               Environment.empty aliases
224         ]
225       ];
226       alias: [  (* return a pair <domain_item, codomain_item> from an alias *)
227         [ IDENT "alias";
228           choice =
229             [ IDENT "id"; id = IDENT; SYMBOL "="; uri = URI ->
230                 (Id id, choice_of_uri uri)
231             | IDENT "symbol"; symbol = QSTRING;
232               PAREN "("; IDENT "instance"; instance = NUM; PAREN ")";
233               SYMBOL "="; dsc = QSTRING ->
234                 (Symbol (symbol, int_of_string instance),
235                  DisambiguateChoices.lookup_symbol_by_dsc symbol dsc)
236             | IDENT "num";
237               PAREN "("; IDENT "instance"; instance = NUM; PAREN ")";
238               SYMBOL "="; dsc = QSTRING ->
239                 (Num (int_of_string instance),
240                  DisambiguateChoices.lookup_num_by_dsc dsc)
241             ] -> choice ]
242       ];
243     END
244
245     let of_string s =
246       if s = empty then
247         Environment.empty
248       else
249         try
250           Grammar.Entry.parse aliases (Stream.of_string s)
251         with Stdpp.Exc_located ((x, y), exn) ->
252           raise (Parse_error (sprintf "parse error at characters %d-%d: %s" x y
253           (Printexc.to_string exn)))
254   end
255