]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_disambiguation/cicTextualParser2.ml
030fa593836bb97f549d8c1b7cb77682b13165b2
[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 term0 = Grammar.Entry.create grammar "term0"
42 (* let tactic = Grammar.Entry.create grammar "tactic" *)
43 (* let tactical = Grammar.Entry.create grammar "tactical" *)
44
45 let return_term loc term = CicTextualParser2Ast.LocatedTerm (loc, term)
46 (* let return_term loc term = term *)
47
48 let fail (x, y) msg =
49   failwith (Printf.sprintf "Error at characters %d - %d: %s" x y msg)
50
51 EXTEND
52   GLOBAL: term term0;
53   meta_subst: [
54     [ s = SYMBOL "_" -> None
55     | t = term -> Some t ]
56   ];
57   binder: [
58     [ SYMBOL <:unicode<lambda>> (* λ *) -> `Lambda
59     | SYMBOL <:unicode<pi>> (* π *) -> `Pi
60     | SYMBOL <:unicode<exists>> (* ∃ *) -> `Exists
61     | SYMBOL <:unicode<forall>> (* ∀ *) -> `Forall
62     ]
63   ];
64   substituted_name: [ (* a subs.name is an explicit substitution subject *)
65     [ s = [ IDENT | SYMBOL ];
66       subst = OPT [
67         SYMBOL "\subst";  (* to avoid catching frequent "a [1]" cases *)
68         PAREN "[";
69         substs = LIST1 [
70           i = IDENT; SYMBOL <:unicode<Assign>> (* ≔ *); t = term -> (i, t)
71         ] SEP SYMBOL ";";
72         PAREN "]" ->
73           substs
74       ] ->
75         (match subst with
76         | Some l -> CicTextualParser2Ast.Ident (s, l)
77         | None -> CicTextualParser2Ast.Ident (s, []))
78     ]
79   ];
80   name: [ (* as substituted_name with no explicit substitution *)
81     [ s = [ IDENT | SYMBOL ] -> s ]
82   ];
83   pattern: [
84     [ n = name -> [n]
85     | PAREN "("; names = LIST1 name; PAREN ")" -> names ]
86   ];
87   term0: [ [ t = term -> return_term loc t ] ];
88   term:
89     [ "arrow" RIGHTA
90       [ t1 = term; SYMBOL <:unicode<to>>; t2 = term ->
91           return_term loc
92             (CicTextualParser2Ast.Binder (`Pi, Cic.Anonymous, Some t1, t2))
93       ]
94     | "eq" LEFTA
95       [ t1 = term; SYMBOL "="; t2 = term ->
96         return_term loc (CicTextualParser2Ast.Appl_symbol ("eq", 0, [t1; t2]))
97       ]
98     | "add" LEFTA     [ (* nothing here by default *) ]
99     | "mult" LEFTA    [ (* nothing here by default *) ]
100     | "inv" NONA      [ (* nothing here by default *) ]
101     | "simple" NONA
102       [
103         b = binder; vars = LIST1 IDENT SEP SYMBOL ",";
104         typ = OPT [ SYMBOL ":"; t = term -> t ];
105         SYMBOL "."; body = term ->
106           let binder =
107             List.fold_right
108               (fun var body ->
109                 let name =
110                   match var with
111                   | "_" -> Cic.Anonymous
112                   | var -> Cic.Name var
113                 in
114                 CicTextualParser2Ast.Binder (b, name, typ, body))
115               vars body
116           in
117           return_term loc binder
118       | sort_kind = [
119           "Prop" -> `Prop | "Set" -> `Set | "Type" -> `Type | "CProp" -> `CProp
120         ] ->
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))
126       | m = META;
127         substs = [
128           PAREN "["; substs = LIST0 meta_subst SEP SYMBOL ";" ; PAREN "]" ->
129             substs
130         ] ->
131             let index =
132               try
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)
136             in
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 ];
144           defs = LIST1 [
145           name = IDENT;
146           index = OPT [ PAREN "("; index = INT; PAREN ")" ->
147             int_of_string index
148           ];
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))
152         ] SEP (IDENT "and");
153         IDENT "in"; body = term ->
154           return_term loc (CicTextualParser2Ast.LetRec (ind_kind, defs, body))
155       | outtyp = OPT [ PAREN "["; typ = term; PAREN "]" -> typ ];
156         "match"; t = term;
157         SYMBOL ":"; indty = IDENT;
158         "with";
159         PAREN "[";
160         patterns = LIST0 [
161           p = pattern; SYMBOL <:unicode<Rightarrow>> (* ⇒ *); t = term ->
162             (p, t)
163         ] SEP SYMBOL "|";
164         PAREN "]" ->
165           return_term loc
166             (CicTextualParser2Ast.Case (t, indty, outtyp, patterns))
167       | PAREN "("; t = term; PAREN ")" -> return_term loc t
168       ]
169     ];
170 END
171
172 let parse_term stream =
173   try
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)))
178