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)
55 let name_of_string = function
56 | "_" -> Cic.Anonymous
62 [ s = SYMBOL "_" -> None
63 | t = term -> Some t ]
66 [ SYMBOL <:unicode<lambda>> (* λ *) -> `Lambda
67 | SYMBOL <:unicode<pi>> (* π *) -> `Pi
68 | SYMBOL <:unicode<exists>> (* ∃ *) -> `Exists
69 | SYMBOL <:unicode<forall>> (* ∀ *) -> `Forall
73 [ PAREN "("; i = IDENT; SYMBOL ":"; typ = term; PAREN ")" ->
74 (name_of_string i, Some typ)
75 | i = IDENT -> (name_of_string i, None)
78 substituted_name: [ (* a subs.name is an explicit substitution subject *)
79 [ s = [ IDENT | SYMBOL ];
81 SYMBOL "\subst"; (* to avoid catching frequent "a [1]" cases *)
84 i = IDENT; SYMBOL <:unicode<Assign>> (* ≔ *); t = term -> (i, t)
90 | Some l -> CicTextualParser2Ast.Ident (s, l)
91 | None -> CicTextualParser2Ast.Ident (s, []))
94 name: [ (* as substituted_name with no explicit substitution *)
95 [ s = [ IDENT | SYMBOL ] -> s ]
99 | PAREN "("; head = name; vars = LIST1 typed_name; PAREN ")" ->
103 term0: [ [ t = term -> return_term loc t ] ];
106 [ t1 = term; SYMBOL <:unicode<to>>; t2 = term ->
108 (CicTextualParser2Ast.Binder (`Pi, (Cic.Anonymous, Some t1), t2))
112 b = binder; vars = LIST1 IDENT SEP SYMBOL ",";
113 typ = OPT [ SYMBOL ":"; t = term -> t ];
114 SYMBOL "."; body = term ->
118 let name = name_of_string var in
119 CicTextualParser2Ast.Binder (b, (name, typ), body))
122 return_term loc binder
125 [ t1 = term; SYMBOL "="; t2 = term ->
126 return_term loc (CicTextualParser2Ast.Appl_symbol ("eq", 0, [t1; t2]))
128 | "add" LEFTA [ (* nothing here by default *) ]
129 | "mult" LEFTA [ (* nothing here by default *) ]
130 | "inv" NONA [ (* nothing here by default *) ]
134 "Prop" -> `Prop | "Set" -> `Set | "Type" -> `Type | "CProp" -> `CProp
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))
143 PAREN "["; substs = LIST0 meta_subst SEP SYMBOL ";" ; PAREN "]" ->
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)
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>> (* ≝ *); *)
159 IDENT "in"; t2 = term ->
160 return_term loc (CicTextualParser2Ast.LetIn (var, t1, t2))
161 | "let"; ind_kind = [ "corec" -> `CoInductive | "rec"-> `Inductive ];
164 index = OPT [ PAREN "("; index = NUM; PAREN ")" ->
167 (* SYMBOL <:unicode<def>> (* ≝ *); *)
170 (var, t1, (match index with None -> 0 | Some i -> i))
172 IDENT "in"; body = term ->
173 return_term loc (CicTextualParser2Ast.LetRec (ind_kind, defs, body))
174 | outtyp = OPT [ PAREN "["; typ = term; PAREN "]" -> typ ];
176 SYMBOL ":"; indty = IDENT;
180 lhs = pattern; SYMBOL <:unicode<Rightarrow>> (* ⇒ *); rhs = term ->
181 ((lhs: CicTextualParser2Ast.case_pattern), rhs)
185 (CicTextualParser2Ast.Case (t, indty, outtyp, patterns))
186 | PAREN "("; t = term; PAREN ")" -> return_term loc t
191 let parse_term stream =
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)))
200 (** {2 Interface for gTopLevel} *)
202 open DisambiguateTypes
204 module EnvironmentP3 =
210 let aliases_grammar = Grammar.gcreate CicTextualLexer2.cic_lexer
211 let aliases = Grammar.Entry.create aliases_grammar "aliases"
216 (fun domain_item (dsc, _) acc ->
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\""
224 sprintf "alias num (instance %d) = \"%s\"" instance dsc
229 String.concat "\n" (List.sort compare aliases)
233 aliases: [ (* build an environment from an aliases list *)
234 [ aliases = LIST0 alias; EOI ->
236 (fun env (domain_item, codomain_item) ->
237 Environment.add domain_item codomain_item env)
238 Environment.empty aliases
241 alias: [ (* return a pair <domain_item, codomain_item> from an alias *)
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)
252 PAREN "("; IDENT "instance"; instance = NUM; PAREN ")";
253 SYMBOL "="; dsc = QSTRING ->
254 (Num (int_of_string instance),
255 DisambiguateChoices.lookup_num_by_dsc dsc)
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)))