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