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 Lexer.lex
40 let term = Grammar.Entry.create grammar "term"
41 (* let tactic = Grammar.Entry.create grammar "tactic" *)
42 (* let tactical = Grammar.Entry.create grammar "tactical" *)
44 let return_term loc term = Ast.LocatedTerm (loc, term)
45 (* let return_term loc term = term *)
48 failwith (Printf.sprintf "Error at characters %d - %d: %s" x y msg)
53 [ s = SYMBOL "_" -> None
54 | t = term -> Some t ]
57 [ SYMBOL <:unicode<lambda>> (* λ *) -> `Lambda
58 | SYMBOL <:unicode<pi>> (* π *) -> `Pi
59 | SYMBOL <:unicode<exists>> (* ∃ *) -> `Exists
60 | SYMBOL <:unicode<forall>> (* ∀ *) -> `Forall
63 substituted_name: [ (* a subs.name is an explicit substitution subject *)
64 [ s = [ IDENT | SYMBOL ];
66 SYMBOL "\subst"; (* to avoid catching frequent "a [1]" cases *)
69 i = IDENT; SYMBOL <:unicode<Assign>> (* ≔ *); t = term -> (i, t)
75 | Some l -> Ast.Ident (s, l)
76 | None -> Ast.Ident (s, []))
79 name: [ (* as substituted_name with no explicit substitution *)
80 [ s = [ IDENT | SYMBOL ] -> s ]
84 | LPAREN "("; names = LIST1 name; RPAREN ")" -> names ]
88 [ t1 = term; SYMBOL <:unicode<to>>; t2 = term ->
89 return_term loc (Ast.Binder (`Pi, Cic.Anonymous, Some t1, t2))
92 [ t1 = term; SYMBOL "="; t2 = term ->
93 return_term loc (Ast.Appl_symbol ("eq", 0, [t1; t2]))
95 | "add" LEFTA [ (* nothing here by default *) ]
96 | "mult" LEFTA [ (* nothing here by default *) ]
97 | "inv" NONA [ (* nothing here by default *) ]
100 (* TODO handle "_" *)
101 b = binder; vars = LIST1 IDENT SEP SYMBOL ",";
102 typ = OPT [ SYMBOL ":"; t = term -> t ];
103 SYMBOL "."; body = term ->
106 (fun var body -> Ast.Binder (b, Cic.Name var, typ, body))
109 return_term loc binder
111 "Prop" -> `Prop | "Set" -> `Set | "Type" -> `Type | "CProp" -> `CProp
114 | n = substituted_name -> return_term loc n
115 | LPAREN "("; head = term; args = LIST1 term; RPAREN ")" ->
116 return_term loc (Ast.Appl (head :: args))
117 | i = NUM -> return_term loc (Ast.Num (i, 0))
118 (* | i = NUM -> return_term loc (Ast.Num (i, Random.int max_int)) *)
121 LPAREN "["; substs = LIST0 meta_subst SEP SYMBOL ";" ; RPAREN "]" ->
126 int_of_string (String.sub m 1 (String.length m - 1))
127 with Failure "int_of_string" ->
128 fail loc ("Invalid meta variable number: " ^ m)
130 return_term loc (Ast.Meta (index, substs))
131 (* actually "in" and "and" are _not_ keywords. Parsing works anyway
132 * since applications are required to be bound by parens *)
133 | "let"; name = IDENT; SYMBOL <:unicode<def>> (* ≝ *); t1 = term;
134 IDENT "in"; t2 = term ->
135 return_term loc (Ast.LetIn (name, t1, t2))
136 | "let"; ind_kind = [ "corec" -> `CoInductive | "rec"-> `Inductive ];
139 index = OPT [ LPAREN "("; index = INT; RPAREN ")" ->
142 typ = OPT [ SYMBOL ":"; typ = term -> typ ];
143 SYMBOL <:unicode<def>> (* ≝ *); t1 = term ->
144 (name, t1, typ, (match index with None -> 1 | Some i -> i))
146 IDENT "in"; body = term ->
147 return_term loc (Ast.LetRec (ind_kind, defs, body))
148 | outtyp = OPT [ LPAREN "["; typ = term; RPAREN "]" -> typ ];
150 SYMBOL ":"; indty = IDENT;
154 p = pattern; SYMBOL <:unicode<Rightarrow>> (* ⇒ *); t = term -> (p, t)
157 return_term loc (Ast.Case (t, indty, outtyp, patterns))
158 | LPAREN "("; t = term; RPAREN ")" -> return_term loc t
163 let parse_term stream =
165 Grammar.Entry.parse term stream
166 with Stdpp.Exc_located ((x, y), exn) ->
167 raise (Parse_error (sprintf "parse error at characters %d-%d: %s" x y
168 (Printexc.to_string exn)))