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>"
34 let grammar = Grammar.gcreate Lexer.lex
36 let term = Grammar.Entry.create grammar "term"
37 (* let tactic = Grammar.Entry.create grammar "tactic" *)
38 (* let tactical = Grammar.Entry.create grammar "tactical" *)
40 let return_term loc term = Ast.LocatedTerm (loc, term)
41 (* let return_term loc term = term *)
44 failwith (Printf.sprintf "Error at characters %d - %d: %s" x y msg)
49 [ s = SYMBOL "_" -> None
50 | t = term -> Some t ]
53 [ SYMBOL <:unicode<lambda>> (* λ *) -> `Lambda
54 | SYMBOL <:unicode<pi>> (* π *) -> `Pi
55 | SYMBOL <:unicode<exists>> (* ∃ *) -> `Exists
56 | SYMBOL <:unicode<forall>> (* ∀ *) -> `Forall
59 substituted_name: [ (* a subs.name is an explicit substitution subject *)
60 [ s = [ IDENT | SYMBOL ];
62 SYMBOL "\subst"; (* to avoid catching frequent "a [1]" cases *)
65 i = IDENT; SYMBOL <:unicode<Assign>> (* ≔ *); t = term -> (i, t)
71 | Some l -> Ast.Ident (s, l)
72 | None -> Ast.Ident (s, []))
75 name: [ (* as substituted_name with no explicit substitution *)
76 [ s = [ IDENT | SYMBOL ] -> s ]
80 | LPAREN "("; names = LIST1 name; RPAREN ")" -> names ]
84 [ t1 = term; SYMBOL <:unicode<to>>; t2 = term ->
85 return_term loc (Ast.Binder (`Pi, Cic.Anonymous, Some t1, t2))
88 [ t1 = term; SYMBOL "="; t2 = term ->
89 return_term loc (Ast.Appl_symbol ("eq", 0, [t1; t2]))
91 | "add" LEFTA [ (* nothing here by default *) ]
92 | "mult" LEFTA [ (* nothing here by default *) ]
93 | "inv" NONA [ (* nothing here by default *) ]
97 b = binder; vars = LIST1 IDENT SEP SYMBOL ",";
98 typ = OPT [ SYMBOL ":"; t = term -> t ];
99 SYMBOL "."; body = term ->
102 (fun var body -> Ast.Binder (b, Cic.Name var, typ, body))
105 return_term loc binder
107 "Prop" -> `Prop | "Set" -> `Set | "Type" -> `Type | "CProp" -> `CProp
110 | n = substituted_name -> return_term loc n
111 | LPAREN "("; head = term; args = LIST1 term; RPAREN ")" ->
112 return_term loc (Ast.Appl (head :: args))
113 | i = NUM -> return_term loc (Ast.Num (i, 0))
114 (* | i = NUM -> return_term loc (Ast.Num (i, Random.int max_int)) *)
117 LPAREN "["; substs = LIST0 meta_subst SEP SYMBOL ";" ; RPAREN "]" ->
122 int_of_string (String.sub m 1 (String.length m - 1))
123 with Failure "int_of_string" ->
124 fail loc ("Invalid meta variable number: " ^ m)
126 return_term loc (Ast.Meta (index, substs))
127 (* actually "in" and "and" are _not_ keywords. Parsing works anyway
128 * since applications are required to be bound by parens *)
129 | "let"; name = IDENT; SYMBOL <:unicode<def>> (* ≝ *); t1 = term;
130 IDENT "in"; t2 = term ->
131 return_term loc (Ast.LetIn (name, t1, t2))
132 | "let"; ind_kind = [ "corec" -> `CoInductive | "rec"-> `Inductive ];
135 index = OPT [ LPAREN "("; index = INT; RPAREN ")" ->
138 typ = OPT [ SYMBOL ":"; typ = term -> typ ];
139 SYMBOL <:unicode<def>> (* ≝ *); t1 = term ->
140 (name, t1, typ, (match index with None -> 1 | Some i -> i))
142 IDENT "in"; body = term ->
143 return_term loc (Ast.LetRec (ind_kind, defs, body))
144 | outtyp = OPT [ LPAREN "["; typ = term; RPAREN "]" -> typ ];
146 SYMBOL ":"; indty = IDENT;
150 p = pattern; SYMBOL <:unicode<Rightarrow>> (* ⇒ *); t = term -> (p, t)
153 return_term loc (Ast.Case (t, indty, outtyp, patterns))
154 | LPAREN "("; t = term; RPAREN ")" -> return_term loc t
159 let parse_term = Grammar.Entry.parse term