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 grammar = Grammar.gcreate CicTextualLexer2.lex
40 let term = Grammar.Entry.create grammar "term"
41 let term0 = Grammar.Entry.create grammar "term0"
42 (* let tactic = Grammar.Entry.create grammar "tactic" *)
43 (* let tactical = Grammar.Entry.create grammar "tactical" *)
45 let return_term loc term = CicTextualParser2Ast.LocatedTerm (loc, term)
46 (* let return_term loc term = term *)
49 failwith (Printf.sprintf "Error at characters %d - %d: %s" x y msg)
54 [ s = SYMBOL "_" -> None
55 | t = term -> Some t ]
58 [ SYMBOL <:unicode<lambda>> (* λ *) -> `Lambda
59 | SYMBOL <:unicode<pi>> (* π *) -> `Pi
60 | SYMBOL <:unicode<exists>> (* ∃ *) -> `Exists
61 | SYMBOL <:unicode<forall>> (* ∀ *) -> `Forall
64 substituted_name: [ (* a subs.name is an explicit substitution subject *)
65 [ s = [ IDENT | SYMBOL ];
67 SYMBOL "\subst"; (* to avoid catching frequent "a [1]" cases *)
70 i = IDENT; SYMBOL <:unicode<Assign>> (* ≔ *); t = term -> (i, t)
76 | Some l -> CicTextualParser2Ast.Ident (s, l)
77 | None -> CicTextualParser2Ast.Ident (s, []))
80 name: [ (* as substituted_name with no explicit substitution *)
81 [ s = [ IDENT | SYMBOL ] -> s ]
85 | PAREN "("; names = LIST1 name; PAREN ")" -> names ]
87 term0: [ [ t = term -> return_term loc t ] ];
90 [ t1 = term; SYMBOL <:unicode<to>>; t2 = term ->
92 (CicTextualParser2Ast.Binder (`Pi, Cic.Anonymous, Some t1, t2))
95 [ t1 = term; SYMBOL "="; t2 = term ->
96 return_term loc (CicTextualParser2Ast.Appl_symbol ("eq", 0, [t1; t2]))
98 | "add" LEFTA [ (* nothing here by default *) ]
99 | "mult" LEFTA [ (* nothing here by default *) ]
100 | "inv" NONA [ (* nothing here by default *) ]
103 b = binder; vars = LIST1 IDENT SEP SYMBOL ",";
104 typ = OPT [ SYMBOL ":"; t = term -> t ];
105 SYMBOL "."; body = term ->
111 | "_" -> Cic.Anonymous
112 | var -> Cic.Name var
114 CicTextualParser2Ast.Binder (b, name, typ, body))
117 return_term loc binder
119 "Prop" -> `Prop | "Set" -> `Set | "Type" -> `Type | "CProp" -> `CProp
121 CicTextualParser2Ast.Sort sort_kind
122 | n = substituted_name -> return_term loc n
123 | PAREN "("; head = term; args = LIST1 term; PAREN ")" ->
124 return_term loc (CicTextualParser2Ast.Appl (head :: args))
125 | i = NUM -> return_term loc (CicTextualParser2Ast.Num (i, 0))
128 PAREN "["; substs = LIST0 meta_subst SEP SYMBOL ";" ; PAREN "]" ->
133 int_of_string (String.sub m 1 (String.length m - 1))
134 with Failure "int_of_string" ->
135 fail loc ("Invalid meta variable number: " ^ m)
137 return_term loc (CicTextualParser2Ast.Meta (index, substs))
138 (* actually "in" and "and" are _not_ keywords. Parsing works anyway
139 * since applications are required to be bound by parens *)
140 | "let"; name = IDENT; SYMBOL <:unicode<def>> (* ≝ *); t1 = term;
141 IDENT "in"; t2 = term ->
142 return_term loc (CicTextualParser2Ast.LetIn (name, t1, t2))
143 | "let"; ind_kind = [ "corec" -> `CoInductive | "rec"-> `Inductive ];
146 index = OPT [ PAREN "("; index = INT; PAREN ")" ->
149 typ = OPT [ SYMBOL ":"; typ = term -> typ ];
150 SYMBOL <:unicode<def>> (* ≝ *); t1 = term ->
151 (name, t1, typ, (match index with None -> 1 | Some i -> i))
153 IDENT "in"; body = term ->
154 return_term loc (CicTextualParser2Ast.LetRec (ind_kind, defs, body))
155 | outtyp = OPT [ PAREN "["; typ = term; PAREN "]" -> typ ];
157 SYMBOL ":"; indty = IDENT;
161 p = pattern; SYMBOL <:unicode<Rightarrow>> (* ⇒ *); t = term ->
166 (CicTextualParser2Ast.Case (t, indty, outtyp, patterns))
167 | PAREN "("; t = term; PAREN ")" -> return_term loc t
172 let parse_term stream =
174 Grammar.Entry.parse term0 stream
175 with Stdpp.Exc_located ((x, y), exn) ->
176 raise (Parse_error (sprintf "parse error at characters %d-%d: %s" x y
177 (Printexc.to_string exn)))