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))
99 [ t1 = term; SYMBOL "="; t2 = term ->
100 return_term loc (CicTextualParser2Ast.Appl_symbol ("eq", 0, [t1; t2]))
102 | "add" LEFTA [ (* nothing here by default *) ]
103 | "mult" LEFTA [ (* nothing here by default *) ]
104 | "inv" NONA [ (* nothing here by default *) ]
107 b = binder; vars = LIST1 IDENT SEP SYMBOL ",";
108 typ = OPT [ SYMBOL ":"; t = term -> t ];
109 SYMBOL "."; body = term ->
115 | "_" -> Cic.Anonymous
116 | var -> Cic.Name var
118 CicTextualParser2Ast.Binder (b, name, typ, body))
121 return_term loc binder
123 "Prop" -> `Prop | "Set" -> `Set | "Type" -> `Type | "CProp" -> `CProp
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))
132 PAREN "["; substs = LIST0 meta_subst SEP SYMBOL ";" ; PAREN "]" ->
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)
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 ];
150 index = OPT [ PAREN "("; index = INT; PAREN ")" ->
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))
157 IDENT "in"; body = term ->
158 return_term loc (CicTextualParser2Ast.LetRec (ind_kind, defs, body))
159 | outtyp = OPT [ PAREN "["; typ = term; PAREN "]" -> typ ];
161 SYMBOL ":"; indty = IDENT;
165 p = pattern; SYMBOL <:unicode<Rightarrow>> (* ⇒ *); t = term ->
170 (CicTextualParser2Ast.Case (t, indty, outtyp, patterns))
171 | PAREN "("; t = term; PAREN ")" -> return_term loc t
176 let parse_term stream =
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)))
185 (** {2 Interface for gTopLevel} *)
187 open DisambiguateTypes
189 module EnvironmentP3 =
195 let aliases_grammar = Grammar.gcreate CicTextualLexer2.cic_lexer
196 let aliases = Grammar.Entry.create aliases_grammar "aliases"
201 (fun domain_item (dsc, _) acc ->
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\""
209 sprintf "alias num (instance %d) = \"%s\"" instance dsc
214 String.concat "\n" (List.sort compare aliases)
218 aliases: [ (* build an environment from an aliases list *)
219 [ aliases = LIST0 alias; EOI ->
221 (fun env (domain_item, codomain_item) ->
222 Environment.add domain_item codomain_item env)
223 Environment.empty aliases
226 alias: [ (* return a pair <domain_item, codomain_item> from an alias *)
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)
237 PAREN "("; IDENT "instance"; instance = NUM; PAREN ")";
238 SYMBOL "="; dsc = QSTRING ->
239 (Num (int_of_string instance),
240 DisambiguateChoices.lookup_num_by_dsc dsc)
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)))