1 (* Copyright (C) 2004, HELM Team.
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.
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.
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.
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,
22 * For details, see the HELM World-Wide-Web page,
23 * http://helm.cs.unibo.it/
29 prerr_endline "<NEW_TEXTUAL_PARSER>";
31 prerr_endline "</NEW_TEXTUAL_PARSER>"
36 exception Parse_error of string
38 let choice_of_uri (uri: string) =
39 let cic = HelmLibraryObjects.term_of_uri (UriManager.uri_of_string uri) in
40 (uri, (fun _ _ _ -> cic))
42 let grammar = Grammar.gcreate CicTextualLexer2.cic_lexer
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" *)
49 let return_term loc term = CicTextualParser2Ast.LocatedTerm (loc, term)
50 (* let return_term loc term = term *)
53 failwith (Printf.sprintf "Error at characters %d - %d: %s" x y msg)
58 [ s = SYMBOL "_" -> None
59 | t = term -> Some t ]
62 [ SYMBOL <:unicode<lambda>> (* λ *) -> `Lambda
63 | SYMBOL <:unicode<pi>> (* π *) -> `Pi
64 | SYMBOL <:unicode<exists>> (* ∃ *) -> `Exists
65 | SYMBOL <:unicode<forall>> (* ∀ *) -> `Forall
68 substituted_name: [ (* a subs.name is an explicit substitution subject *)
69 [ s = [ IDENT | SYMBOL ];
71 SYMBOL "\subst"; (* to avoid catching frequent "a [1]" cases *)
74 i = IDENT; SYMBOL <:unicode<Assign>> (* ≔ *); t = term -> (i, t)
80 | Some l -> CicTextualParser2Ast.Ident (s, l)
81 | None -> CicTextualParser2Ast.Ident (s, []))
84 name: [ (* as substituted_name with no explicit substitution *)
85 [ s = [ IDENT | SYMBOL ] -> s ]
89 | PAREN "("; names = LIST1 name; PAREN ")" -> names ]
91 term0: [ [ t = term -> return_term loc t ] ];
94 [ t1 = term; SYMBOL <:unicode<to>>; t2 = term ->
96 (CicTextualParser2Ast.Binder (`Pi, Cic.Anonymous, Some t1, t2))
100 b = binder; vars = LIST1 IDENT SEP SYMBOL ",";
101 typ = OPT [ SYMBOL ":"; t = term -> t ];
102 SYMBOL "."; body = term ->
108 | "_" -> Cic.Anonymous
109 | var -> Cic.Name var
111 CicTextualParser2Ast.Binder (b, name, typ, body))
114 return_term loc binder
117 [ t1 = term; SYMBOL "="; t2 = term ->
118 return_term loc (CicTextualParser2Ast.Appl_symbol ("eq", 0, [t1; t2]))
120 | "add" LEFTA [ (* nothing here by default *) ]
121 | "mult" LEFTA [ (* nothing here by default *) ]
122 | "inv" NONA [ (* nothing here by default *) ]
126 "Prop" -> `Prop | "Set" -> `Set | "Type" -> `Type | "CProp" -> `CProp
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))
135 PAREN "["; substs = LIST0 meta_subst SEP SYMBOL ";" ; PAREN "]" ->
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)
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>> (* ≝ *); *)
151 IDENT "in"; t2 = term ->
152 return_term loc (CicTextualParser2Ast.LetIn (name, t1, t2))
153 | "let"; ind_kind = [ "corec" -> `CoInductive | "rec"-> `Inductive ];
156 index = OPT [ PAREN "("; index = NUM; PAREN ")" ->
159 typ = OPT [ SYMBOL ":"; typ = term -> typ ];
160 (* SYMBOL <:unicode<def>> (* ≝ *); *)
163 (name, t1, typ, (match index with None -> 1 | Some i -> i))
165 IDENT "in"; body = term ->
166 return_term loc (CicTextualParser2Ast.LetRec (ind_kind, defs, body))
167 | outtyp = OPT [ PAREN "["; typ = term; PAREN "]" -> typ ];
169 SYMBOL ":"; indty = IDENT;
173 p = pattern; SYMBOL <:unicode<Rightarrow>> (* ⇒ *); t = term ->
178 (CicTextualParser2Ast.Case (t, indty, outtyp, patterns))
179 | PAREN "("; t = term; PAREN ")" -> return_term loc t
184 let parse_term stream =
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)))
193 (** {2 Interface for gTopLevel} *)
195 open DisambiguateTypes
197 module EnvironmentP3 =
203 let aliases_grammar = Grammar.gcreate CicTextualLexer2.cic_lexer
204 let aliases = Grammar.Entry.create aliases_grammar "aliases"
209 (fun domain_item (dsc, _) acc ->
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\""
217 sprintf "alias num (instance %d) = \"%s\"" instance dsc
222 String.concat "\n" (List.sort compare aliases)
226 aliases: [ (* build an environment from an aliases list *)
227 [ aliases = LIST0 alias; EOI ->
229 (fun env (domain_item, codomain_item) ->
230 Environment.add domain_item codomain_item env)
231 Environment.empty aliases
234 alias: [ (* return a pair <domain_item, codomain_item> from an alias *)
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)
245 PAREN "("; IDENT "instance"; instance = NUM; PAREN ")";
246 SYMBOL "="; dsc = QSTRING ->
247 (Num (int_of_string instance),
248 DisambiguateChoices.lookup_num_by_dsc dsc)
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)))