]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_disambiguation/cicTextualParser2.ml
handle Cic.Anonymous
[helm.git] / helm / ocaml / cic_disambiguation / cicTextualParser2.ml
1 (* Copyright (C) 2004, HELM Team.
2  * 
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.
6  * 
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.
11  * 
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.
16  *
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,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://helm.cs.unibo.it/
24  *)
25
26 let debug = true
27 let debug_print s =
28   if debug then begin
29     prerr_endline "<NEW_TEXTUAL_PARSER>";
30     prerr_endline s;
31     prerr_endline "</NEW_TEXTUAL_PARSER>"
32   end
33
34 open Printf
35
36 exception Parse_error of string
37
38 let grammar = Grammar.gcreate CicTextualLexer2.lex
39
40 let term = Grammar.Entry.create grammar "term"
41 (* let tactic = Grammar.Entry.create grammar "tactic" *)
42 (* let tactical = Grammar.Entry.create grammar "tactical" *)
43
44 let return_term loc term = CicTextualParser2Ast.LocatedTerm (loc, term)
45 (* let return_term loc term = term *)
46
47 let fail (x, y) msg =
48   failwith (Printf.sprintf "Error at characters %d - %d: %s" x y msg)
49
50 EXTEND
51   GLOBAL: term;
52   meta_subst: [
53     [ s = SYMBOL "_" -> None
54     | t = term -> Some t ]
55   ];
56   binder: [
57     [ SYMBOL <:unicode<lambda>> (* λ *) -> `Lambda
58     | SYMBOL <:unicode<pi>> (* π *) -> `Pi
59     | SYMBOL <:unicode<exists>> (* ∃ *) -> `Exists
60     | SYMBOL <:unicode<forall>> (* ∀ *) -> `Forall
61     ]
62   ];
63   substituted_name: [ (* a subs.name is an explicit substitution subject *)
64     [ s = [ IDENT | SYMBOL ];
65       subst = OPT [
66         SYMBOL "\subst";  (* to avoid catching frequent "a [1]" cases *)
67         LPAREN "[";
68         substs = LIST1 [
69           i = IDENT; SYMBOL <:unicode<Assign>> (* ≔ *); t = term -> (i, t)
70         ] SEP SYMBOL ";";
71         RPAREN "]" ->
72           substs
73       ] ->
74         (match subst with
75         | Some l -> CicTextualParser2Ast.Ident (s, l)
76         | None -> CicTextualParser2Ast.Ident (s, []))
77     ]
78   ];
79   name: [ (* as substituted_name with no explicit substitution *)
80     [ s = [ IDENT | SYMBOL ] -> s ]
81   ];
82   pattern: [
83     [ n = name -> [n]
84     | LPAREN "("; names = LIST1 name; RPAREN ")" -> names ]
85   ];
86   term:
87     [ "arrow" RIGHTA
88       [ t1 = term; SYMBOL <:unicode<to>>; t2 = term ->
89           return_term loc
90             (CicTextualParser2Ast.Binder (`Pi, Cic.Anonymous, Some t1, t2))
91       ]
92     | "eq" LEFTA
93       [ t1 = term; SYMBOL "="; t2 = term ->
94         return_term loc (CicTextualParser2Ast.Appl_symbol ("eq", 0, [t1; t2]))
95       ]
96     | "add" LEFTA     [ (* nothing here by default *) ]
97     | "mult" LEFTA    [ (* nothing here by default *) ]
98     | "inv" NONA      [ (* nothing here by default *) ]
99     | "simple" NONA
100       [
101         b = binder; vars = LIST1 IDENT SEP SYMBOL ",";
102         typ = OPT [ SYMBOL ":"; t = term -> t ];
103         SYMBOL "."; body = term ->
104           let binder =
105             List.fold_right
106               (fun var body ->
107                 let name =
108                   match var with
109                   | "_" -> Cic.Anonymous
110                   | var -> Cic.Name var
111                 in
112                 CicTextualParser2Ast.Binder (b, name, typ, body))
113               vars body
114           in
115           return_term loc binder
116       | sort_kind = [
117           "Prop" -> `Prop | "Set" -> `Set | "Type" -> `Type | "CProp" -> `CProp
118         ] ->
119           CicTextualParser2Ast.Sort sort_kind
120       | n = substituted_name -> return_term loc n
121       | LPAREN "("; head = term; args = LIST1 term; RPAREN ")" ->
122           return_term loc (CicTextualParser2Ast.Appl (head :: args))
123       | i = NUM -> return_term loc (CicTextualParser2Ast.Num (i, 0))
124       | m = META;
125         substs = [
126           LPAREN "["; substs = LIST0 meta_subst SEP SYMBOL ";" ; RPAREN "]" ->
127             substs
128         ] ->
129             let index =
130               try
131                 int_of_string (String.sub m 1 (String.length m - 1))
132               with Failure "int_of_string" ->
133                 fail loc ("Invalid meta variable number: " ^ m)
134             in
135             return_term loc (CicTextualParser2Ast.Meta (index, substs))
136         (* actually "in" and "and" are _not_ keywords. Parsing works anyway
137          * since applications are required to be bound by parens *)
138       | "let"; name = IDENT; SYMBOL <:unicode<def>> (* ≝ *); t1 = term;
139         IDENT "in"; t2 = term ->
140           return_term loc (CicTextualParser2Ast.LetIn (name, t1, t2))
141       | "let"; ind_kind = [ "corec" -> `CoInductive | "rec"-> `Inductive ];
142           defs = LIST1 [
143           name = IDENT;
144           index = OPT [ LPAREN "("; index = INT; RPAREN ")" ->
145             int_of_string index
146           ];
147           typ = OPT [ SYMBOL ":"; typ = term -> typ ];
148           SYMBOL <:unicode<def>> (* ≝ *); t1 = term ->
149             (name, t1, typ, (match index with None -> 1 | Some i -> i))
150         ] SEP (IDENT "and");
151         IDENT "in"; body = term ->
152           return_term loc (CicTextualParser2Ast.LetRec (ind_kind, defs, body))
153       | outtyp = OPT [ LPAREN "["; typ = term; RPAREN "]" -> typ ];
154         "match"; t = term;
155         SYMBOL ":"; indty = IDENT;
156         "with";
157         LPAREN "[";
158         patterns = LIST0 [
159           p = pattern; SYMBOL <:unicode<Rightarrow>> (* ⇒ *); t = term -> (p, t)
160         ] SEP SYMBOL "|";
161         RPAREN "]" ->
162           return_term loc (CicTextualParser2Ast.Case (t, indty, outtyp, patterns))
163       | LPAREN "("; t = term; RPAREN ")" -> return_term loc t
164       ]
165     ];
166 END
167
168 let parse_term stream =
169   try
170     Grammar.Entry.parse term stream
171   with Stdpp.Exc_located ((x, y), exn) ->
172     raise (Parse_error (sprintf "parse error at characters %d-%d: %s" x y
173         (Printexc.to_string exn)))
174