]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_disambiguation/cicTextualParser2.ml
renamed modules so that they are more consistent with other cic modules
[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 (CicTextualParser2Ast.Binder (`Pi, Cic.Anonymous, Some t1, t2))
90       ]
91     | "eq" LEFTA
92       [ t1 = term; SYMBOL "="; t2 = term ->
93         return_term loc (CicTextualParser2Ast.Appl_symbol ("eq", 0, [t1; t2]))
94       ]
95     | "add" LEFTA     [ (* nothing here by default *) ]
96     | "mult" LEFTA    [ (* nothing here by default *) ]
97     | "inv" NONA      [ (* nothing here by default *) ]
98     | "simple" NONA
99       [
100         (* TODO handle "_" *)
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 -> CicTextualParser2Ast.Binder (b, Cic.Name var, typ, body))
107               vars body
108           in
109           return_term loc binder
110       | sort_kind = [
111           "Prop" -> `Prop | "Set" -> `Set | "Type" -> `Type | "CProp" -> `CProp
112         ] ->
113           CicTextualParser2Ast.Sort sort_kind
114       | n = substituted_name -> return_term loc n
115       | LPAREN "("; head = term; args = LIST1 term; RPAREN ")" ->
116           return_term loc (CicTextualParser2Ast.Appl (head :: args))
117       | i = NUM -> return_term loc (CicTextualParser2Ast.Num (i, 0))
118 (*       | i = NUM -> return_term loc (CicTextualParser2Ast.Num (i, Random.int max_int)) *)
119       | m = META;
120         substs = [
121           LPAREN "["; substs = LIST0 meta_subst SEP SYMBOL ";" ; RPAREN "]" ->
122             substs
123         ] ->
124             let index =
125               try
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)
129             in
130             return_term loc (CicTextualParser2Ast.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 (CicTextualParser2Ast.LetIn (name, t1, t2))
136       | "let"; ind_kind = [ "corec" -> `CoInductive | "rec"-> `Inductive ];
137           defs = LIST1 [
138           name = IDENT;
139           index = OPT [ LPAREN "("; index = INT; RPAREN ")" ->
140             int_of_string index
141           ];
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))
145         ] SEP (IDENT "and");
146         IDENT "in"; body = term ->
147           return_term loc (CicTextualParser2Ast.LetRec (ind_kind, defs, body))
148       | outtyp = OPT [ LPAREN "["; typ = term; RPAREN "]" -> typ ];
149         "match"; t = term;
150         SYMBOL ":"; indty = IDENT;
151         "with";
152         LPAREN "[";
153         patterns = LIST0 [
154           p = pattern; SYMBOL <:unicode<Rightarrow>> (* ⇒ *); t = term -> (p, t)
155         ] SEP SYMBOL "|";
156         RPAREN "]" ->
157           return_term loc (CicTextualParser2Ast.Case (t, indty, outtyp, patterns))
158       | LPAREN "("; t = term; RPAREN ")" -> return_term loc t
159       ]
160     ];
161 END
162
163 let parse_term stream =
164   try
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)))
169