]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_disambiguation/parser.ml
snapshot, almost working
[helm.git] / helm / ocaml / cic_disambiguation / parser.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 let grammar = Grammar.gcreate Lexer.lex
35
36 let term = Grammar.Entry.create grammar "term"
37 (* let tactic = Grammar.Entry.create grammar "tactic" *)
38 (* let tactical = Grammar.Entry.create grammar "tactical" *)
39
40 let return_term loc term = Ast.LocatedTerm (loc, term)
41 (* let return_term loc term = term *)
42
43 let fail (x, y) msg =
44   failwith (Printf.sprintf "Error at characters %d - %d: %s" x y msg)
45
46 EXTEND
47   GLOBAL: term;
48   meta_subst: [
49     [ s = SYMBOL "_" -> None
50     | t = term -> Some t ]
51   ];
52   binder: [
53     [ SYMBOL <:unicode<lambda>> (* λ *) -> `Lambda
54     | SYMBOL <:unicode<pi>> (* π *) -> `Pi
55     | SYMBOL <:unicode<exists>> (* ∃ *) -> `Exists
56     | SYMBOL <:unicode<forall>> (* ∀ *) -> `Forall
57     ]
58   ];
59   substituted_name: [ (* a subs.name is an explicit substitution subject *)
60     [ s = [ IDENT | SYMBOL ];
61       subst = OPT [
62         SYMBOL "\subst";  (* to avoid catching frequent "a [1]" cases *)
63         LPAREN "[";
64         substs = LIST1 [
65           i = IDENT; SYMBOL <:unicode<Assign>> (* ≔ *); t = term -> (i, t)
66         ] SEP SYMBOL ";";
67         RPAREN "]" ->
68           substs
69       ] ->
70         (match subst with
71         | Some l -> Ast.Ident (s, l)
72         | None -> Ast.Ident (s, []))
73     ]
74   ];
75   name: [ (* as substituted_name with no explicit substitution *)
76     [ s = [ IDENT | SYMBOL ] -> s ]
77   ];
78   pattern: [
79     [ n = name -> [n]
80     | LPAREN "("; names = LIST1 name; RPAREN ")" -> names ]
81   ];
82   term:
83     [ "arrow" RIGHTA
84       [ t1 = term; SYMBOL <:unicode<to>>; t2 = term ->
85           return_term loc (Ast.Binder (`Pi, Cic.Anonymous, Some t1, t2))
86       ]
87     | "eq" LEFTA
88       [ t1 = term; SYMBOL "="; t2 = term ->
89         return_term loc (Ast.Appl_symbol ("eq", 0, [t1; t2]))
90       ]
91     | "add" LEFTA     [ (* nothing here by default *) ]
92     | "mult" LEFTA    [ (* nothing here by default *) ]
93     | "inv" NONA      [ (* nothing here by default *) ]
94     | "simple" NONA
95       [
96         (* TODO handle "_" *)
97         b = binder; vars = LIST1 IDENT SEP SYMBOL ",";
98         typ = OPT [ SYMBOL ":"; t = term -> t ];
99         SYMBOL "."; body = term ->
100           let binder =
101             List.fold_right
102               (fun var body -> Ast.Binder (b, Cic.Name var, typ, body))
103               vars body
104           in
105           return_term loc binder
106       | sort_kind = [
107           "Prop" -> `Prop | "Set" -> `Set | "Type" -> `Type | "CProp" -> `CProp
108         ] ->
109           Ast.Sort sort_kind
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)) *)
115       | m = META;
116         substs = [
117           LPAREN "["; substs = LIST0 meta_subst SEP SYMBOL ";" ; RPAREN "]" ->
118             substs
119         ] ->
120             let index =
121               try
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)
125             in
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 ];
133           defs = LIST1 [
134           name = IDENT;
135           index = OPT [ LPAREN "("; index = INT; RPAREN ")" ->
136             int_of_string index
137           ];
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))
141         ] SEP (IDENT "and");
142         IDENT "in"; body = term ->
143           return_term loc (Ast.LetRec (ind_kind, defs, body))
144       | outtyp = OPT [ LPAREN "["; typ = term; RPAREN "]" -> typ ];
145         "match"; t = term;
146         SYMBOL ":"; indty = IDENT;
147         "with";
148         LPAREN "[";
149         patterns = LIST0 [
150           p = pattern; SYMBOL <:unicode<Rightarrow>> (* ⇒ *); t = term -> (p, t)
151         ] SEP SYMBOL "|";
152         RPAREN "]" ->
153           return_term loc (Ast.Case (t, indty, outtyp, patterns))
154       | LPAREN "("; t = term; RPAREN ")" -> return_term loc t
155       ]
156     ];
157 END
158
159 let parse_term = Grammar.Entry.parse term
160