]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_disambiguation/cicTextualParser2.ml
\lambda x.x y ----> \lambda x.(x y)
[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 = false
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   (** if set to true each number will have a different insance number and can
35   * thus be interpreted differently than others *)
36 let use_fresh_num_instances = false
37
38   (** does the lexer return COMMENT tokens? *)
39 let return_comments = false
40
41 open Printf
42
43 open DisambiguateTypes
44
45 exception Parse_error of Token.flocation * string
46
47 let cic_lexer = CicTextualLexer2.cic_lexer ~comments:return_comments ()
48
49 let fresh_num_instance =
50   let n = ref 0 in
51   if use_fresh_num_instances then
52     (fun () -> incr n; !n)
53   else
54     (fun () -> 0)
55
56 let choice_of_uri uri =
57   let term = CicUtil.term_of_uri uri in
58   (uri, (fun _ _ _ -> term))
59
60 let grammar = Grammar.gcreate cic_lexer
61
62 let term = Grammar.Entry.create grammar "term"
63 let term0 = Grammar.Entry.create grammar "term0"
64 let tactic = Grammar.Entry.create grammar "tactic"
65 let tactical = Grammar.Entry.create grammar "tactical"
66 let tactical0 = Grammar.Entry.create grammar "tactical0"
67 let command = Grammar.Entry.create grammar "command"
68 let alias_spec = Grammar.Entry.create grammar "alias_spec"
69 let macro = Grammar.Entry.create grammar "macro"
70 let script = Grammar.Entry.create grammar "script"
71 let statement = Grammar.Entry.create grammar "statement"
72
73 let return_term loc term = CicAst.AttributedTerm (`Loc loc, term)
74
75 let fail floc msg =
76   let (x, y) = CicAst.loc_of_floc floc in
77   failwith (Printf.sprintf "Error at characters %d - %d: %s" x y msg)
78
79 let name_of_string = function
80   | "_" -> Cic.Anonymous
81   | s -> Cic.Name s
82
83 let string_of_name = function
84   | Cic.Anonymous -> "_"
85   | Cic.Name s -> s
86   
87 let int_opt = function
88   | None -> None
89   | Some lexeme -> Some (int_of_string lexeme)
90
91 let int_of_string s =
92   try
93     Pervasives.int_of_string s
94   with Failure _ ->
95     failwith (sprintf "Lexer failure: string_of_int \"%s\" failed" s)
96
97   (** the uri of an inductive type (a ".ind" uri) is not meaningful without an
98   * xpointer. Still, it's likely that an user who wrote "cic:/blabla/foo.ind"
99   * actually meant "cic:/blabla/foo.ind#xpointer(1/1)", i.e. the first inductive
100   * type in a block of mutual inductive types.
101   *
102   * This function performs the expansion foo.ind -> foo#xpointer..., if needed
103   *)
104 let ind_expansion uri =
105   let len = String.length uri in
106   if len >= 4 && String.sub uri (len - 4) 4 = ".ind" then
107     uri ^ "#xpointer(1/1)"
108   else
109     uri
110
111 let mk_binder_ast binder typ vars body =
112   List.fold_right
113     (fun var body ->
114        let name = name_of_string var in
115        CicAst.Binder (binder, (name, typ), body))
116     vars body
117
118 EXTEND
119   GLOBAL: term term0 statement;
120   int: [
121     [ num = NUM ->
122         try
123           int_of_string num
124         with Failure _ -> raise (Parse_error (loc, "integer literal expected"))
125     ]
126   ];
127   meta_subst: [
128     [ s = SYMBOL "_" -> None
129     | t = term -> Some t ]
130   ];
131   binder_low: [
132     [ SYMBOL <:unicode<Pi>>     (* Π *) -> `Pi
133     | SYMBOL <:unicode<exists>> (* ∃ *) -> `Exists
134     | SYMBOL <:unicode<forall>> (* ∀ *) -> `Forall ]
135   ];
136   binder_high: [ [ SYMBOL <:unicode<lambda>> (* λ *) -> `Lambda ] ];
137   sort: [
138     [ "Prop" -> `Prop
139     | "Set" -> `Set
140     | "Type" -> `Type
141     | "CProp" -> `CProp ]
142   ];
143   typed_name: [
144     [ PAREN "("; i = IDENT; SYMBOL ":"; typ = term; PAREN ")" ->
145         (Cic.Name i, Some typ)
146     | i = IDENT -> (Cic.Name i, None)
147     ]
148   ];
149   subst: [
150     [ subst = OPT [
151         SYMBOL "\\subst";  (* to avoid catching frequent "a [1]" cases *)
152         PAREN "[";
153         substs = LIST1 [
154           i = IDENT; SYMBOL <:unicode<Assign>> (* ≔ *); t = term -> (i, t)
155         ] SEP SYMBOL ";";
156         PAREN "]" ->
157           substs
158       ] -> subst
159     ]
160   ];
161   substituted_name: [ (* a subs.name is an explicit substitution subject *)
162     [ s = IDENT; subst = subst -> CicAst.Ident (s, subst)
163     | s = URI; subst = subst -> CicAst.Uri (ind_expansion s, subst)
164     ]
165   ];
166   name: [ (* as substituted_name with no explicit substitution *)
167     [ s = [ IDENT | SYMBOL ] -> s ]
168   ];
169   pattern: [
170     [ n = name -> (n, [])
171     | PAREN "("; head = name; vars = LIST1 typed_name; PAREN ")" ->
172         (head, vars)
173     ]
174   ];
175   let_defs:[
176     [ defs = LIST1 [
177         name = IDENT;
178         args = LIST1 [
179           PAREN "(" ; names = LIST1 IDENT SEP SYMBOL ","; SYMBOL ":";
180           ty = term; PAREN ")" ->
181             (names, ty)
182         ];
183         index_name = OPT [ IDENT "on"; idx = IDENT -> idx ];
184         ty = OPT [ SYMBOL ":" ; t = term -> t ];
185         SYMBOL <:unicode<def>> (* ≝ *);
186         t1 = term ->
187           let rec list_of_binder binder ty final_term = function
188             | [] -> final_term
189             | name::tl -> 
190                 CicAst.Binder (binder, (Cic.Name name, Some ty), 
191                   list_of_binder binder ty final_term tl)
192           in
193           let rec binder_of_arg_list binder final_term = function
194             | [] -> final_term
195             | (l,ty)::tl ->  
196                 list_of_binder binder ty 
197                   (binder_of_arg_list binder final_term tl) l
198           in
199           let t1' = binder_of_arg_list `Lambda t1 args in
200           let ty' = 
201             match ty with 
202             | None -> None
203             | Some ty -> Some (binder_of_arg_list `Pi ty args)
204           in
205           let rec get_position_of name n = function 
206             | [] -> (None,n)
207             | nam::tl -> 
208                 if nam = name then 
209                   (Some n,n) 
210                 else 
211                   (get_position_of name (n+1) tl)
212           in
213           let rec find_arg name n = function 
214             | [] -> (fail loc (sprintf "Argument %s not found" name))
215             | (l,_)::tl -> 
216                 let (got,len) = get_position_of name 0 l in
217                 (match got with 
218                 | None -> (find_arg name (n+len) tl)
219                 | Some where -> n + where)
220           in
221           let index = 
222             (match index_name with 
223              | None -> 0 
224              | (Some name) -> find_arg name 0 args)
225           in
226           ((Cic.Name name,ty'), t1', index)
227       ] SEP "and" -> defs
228     ]];
229   constructor: [ [ name = IDENT; SYMBOL ":"; typ = term -> (name, typ) ] ];
230   binder_vars: [
231       [ vars = LIST1 IDENT SEP SYMBOL ",";
232         typ = OPT [ SYMBOL ":"; t = term -> t ] -> (vars, typ)
233       | PAREN "("; vars = LIST1 IDENT SEP SYMBOL ",";
234         typ = OPT [ SYMBOL ":"; t = term -> t ]; PAREN ")" -> (vars, typ)
235       ]
236   ];
237   term0: [ [ t = term; EOI -> return_term loc t ] ];
238   term:
239     [ "letin" NONA
240       [ "let"; var = typed_name;
241         SYMBOL <:unicode<def>> (* ≝ *);
242         t1 = term; "in"; t2 = term ->
243           return_term loc (CicAst.LetIn (var, t1, t2))
244       | "let"; ind_kind = [ "corec" -> `CoInductive | "rec"-> `Inductive ];
245         defs = let_defs; "in"; body = term ->
246             return_term loc (CicAst.LetRec (ind_kind, defs, body))
247       ]
248     | "binder" RIGHTA
249       [
250         b = binder_low; (vars, typ) = binder_vars; SYMBOL "."; body = term ->
251           let binder = mk_binder_ast b typ vars body in
252           return_term loc binder
253       | b = binder_high; (vars, typ) = binder_vars; SYMBOL "."; body = term ->
254           let binder = mk_binder_ast b typ vars body in
255           return_term loc binder
256       | t1 = term; SYMBOL <:unicode<to>> (* → *); t2 = term ->
257           return_term loc (CicAst.Binder (`Pi, (Cic.Anonymous, Some t1), t2))
258       ]
259     | "logic_add" LEFTA   [ (* nothing here by default *) ]
260     | "logic_mult" LEFTA  [ (* nothing here by default *) ]
261     | "logic_inv" NONA    [ (* nothing here by default *) ]
262     | "relop" LEFTA
263       [ t1 = term; SYMBOL "="; t2 = term ->
264         return_term loc (CicAst.Appl [CicAst.Symbol ("eq", 0); t1; t2])
265       ]
266     | "add" LEFTA     [ (* nothing here by default *) ]
267     | "mult" LEFTA    [ (* nothing here by default *) ]
268     | "power" LEFTA   [ (* nothing here by default *) ]
269     | "inv" NONA      [ (* nothing here by default *) ]
270     | "apply" LEFTA
271       [ t1 = term; t2 = term ->
272         let rec aux = function
273           | CicAst.Appl (hd :: tl) -> aux hd @ tl
274           | term -> [term]
275         in
276         CicAst.Appl (aux t1 @ [t2])
277       ]
278     | "simple" NONA
279       [ sort = sort -> CicAst.Sort sort
280       | n = substituted_name -> return_term loc n
281       | i = NUM -> return_term loc (CicAst.Num (i, (fresh_num_instance ())))
282       | IMPLICIT -> return_term loc CicAst.Implicit
283       | m = META;
284         substs = [
285           PAREN "["; substs = LIST0 meta_subst SEP SYMBOL ";" ; PAREN "]" ->
286             substs
287         ] ->
288             let index =
289               try
290                 int_of_string (String.sub m 1 (String.length m - 1))
291               with Failure "int_of_string" ->
292                 fail loc ("Invalid meta variable number: " ^ m)
293             in
294             return_term loc (CicAst.Meta (index, substs))
295       | outtyp = OPT [ PAREN "["; typ = term; PAREN "]" -> typ ];
296         "match"; t = term;
297         indty_ident = OPT [ SYMBOL ":"; id = IDENT -> id ];
298         "with";
299         PAREN "[";
300         patterns = LIST0 [
301           lhs = pattern; SYMBOL <:unicode<Rightarrow>> (* ⇒ *); rhs = term
302           ->
303             ((lhs: CicAst.case_pattern), rhs)
304         ] SEP SYMBOL "|";
305         PAREN "]" ->
306           return_term loc
307             (CicAst.Case (t, indty_ident, outtyp, patterns))
308       | PAREN "("; t1 = term; SYMBOL ":"; t2 = term; PAREN ")" ->
309           return_term loc (CicAst.Appl [CicAst.Symbol ("cast", 0); t1; t2])
310       | PAREN "("; t = term; PAREN ")" -> return_term loc t
311       ]
312     ];
313   tactic_where: [
314     [ where = OPT [ "in"; ident = IDENT -> ident ] -> where ]
315   ];
316   tactic_term: [ [ t = term -> t ] ];
317   ident_list0: [
318     [ PAREN "["; idents = LIST0 IDENT SEP SYMBOL ";"; PAREN "]" -> idents ]
319   ];
320   ident_list1: [
321     [ PAREN "["; idents = LIST1 IDENT SEP SYMBOL ";"; PAREN "]" -> idents ]
322   ];
323   reduction_kind: [
324     [ [ IDENT "reduce" ] -> `Reduce
325     | [ IDENT "simplify" ] -> `Simpl
326     | [ IDENT "whd" ] -> `Whd ]
327   ];
328   tactic: [
329     [ [ IDENT "absurd" ]; t = tactic_term ->
330         TacticAst.Absurd (loc, t)
331     | [ IDENT "apply" ]; t = tactic_term ->
332         TacticAst.Apply (loc, t)
333     | [ IDENT "assumption" ] ->
334         TacticAst.Assumption loc
335     | [ IDENT "auto" ] ; num = OPT [ i = NUM -> int_of_string i ] -> 
336           TacticAst.Auto (loc,num)
337     | [ IDENT "change" ];
338       t1 = tactic_term; "with"; t2 = tactic_term;
339       where = tactic_where ->
340         TacticAst.Change (loc, t1, t2, where)
341     (* TODO Change_pattern *)
342     | [ IDENT "contradiction" ] ->
343         TacticAst.Contradiction loc
344     | [ IDENT "cut" ];
345       t = tactic_term ->
346         TacticAst.Cut (loc, t)
347     | [ IDENT "decompose" ];
348       principles = ident_list1; where = IDENT ->
349         TacticAst.Decompose (loc, where, principles)
350     | [ IDENT "discriminate" ];
351       hyp = IDENT ->
352         TacticAst.Discriminate (loc, hyp)
353     | [ IDENT "elimType" ]; t = tactic_term ->
354         TacticAst.ElimType (loc, t)
355     | [ IDENT "elim" ];
356       t1 = tactic_term;
357       using = OPT [ "using"; using = tactic_term -> using ] ->
358         TacticAst.Elim (loc, t1, using)
359     | [ IDENT "exact" ]; t = tactic_term ->
360         TacticAst.Exact (loc, t)
361     | [ IDENT "exists" ] ->
362         TacticAst.Exists loc
363     | [ IDENT "fold" ];
364       kind = reduction_kind; t = tactic_term ->
365         TacticAst.Fold (loc, kind, t)
366     | [ IDENT "fourier" ] ->
367         TacticAst.Fourier loc
368     | IDENT "goal"; n = NUM -> TacticAst.Goal (loc, int_of_string n)
369     | [ IDENT "injection" ]; ident = IDENT ->
370         TacticAst.Injection (loc, ident)
371     | [ IDENT "intros" ];
372       num = OPT [ num = int -> num ];
373       idents = OPT ident_list0 ->
374         let idents = match idents with None -> [] | Some idents -> idents in
375         TacticAst.Intros (loc, num, idents)
376     | [ IDENT "intro" ] ->
377         TacticAst.Intros (loc, None, [])
378     | [ IDENT "left" ] -> TacticAst.Left loc
379     | [ "let" | "Let" ];
380       t = tactic_term; "in"; where = IDENT ->
381         TacticAst.LetIn (loc, t, where)
382     | kind = reduction_kind;
383       pat = OPT [
384         "in"; pat = [ IDENT "goal" -> `Goal | IDENT "hyp" -> `Everywhere ] ->
385           pat
386       ];
387       terms = LIST0 term SEP SYMBOL "," ->
388         (match (pat, terms) with
389         | None, [] -> TacticAst.Reduce (loc, kind, None)
390         | None, terms -> TacticAst.Reduce (loc, kind, Some (terms, `Goal))
391         | Some pat, [] -> fail loc "Missing term [list]"
392         | Some pat, terms -> TacticAst.Reduce (loc, kind, Some (terms, pat)))
393     | [ IDENT "reflexivity" ] ->
394         TacticAst.Reflexivity loc
395     | [ IDENT "replace" ];
396       t1 = tactic_term; "with"; t2 = tactic_term ->
397         TacticAst.Replace (loc, t1, t2)
398     | [ IDENT "rewrite" ; IDENT "left" ] ; t = term ->
399         TacticAst.Rewrite (loc,`Left, t, None)
400     | [ IDENT "rewrite" ; IDENT "right" ] ; t = term ->
401         TacticAst.Rewrite (loc,`Right, t, None)
402     (* TODO Replace_pattern *)
403     | [ IDENT "right" ] -> TacticAst.Right loc
404     | [ IDENT "ring" ] -> TacticAst.Ring loc
405     | [ IDENT "split" ] -> TacticAst.Split loc
406     | [ IDENT "symmetry" ] ->
407         TacticAst.Symmetry loc
408     | [ IDENT "transitivity" ];
409       t = tactic_term ->
410         TacticAst.Transitivity (loc, t)
411     ]
412   ];
413   tactical:
414     [ "sequence" LEFTA
415       [ tacticals = LIST1 NEXT SEP SYMBOL ";" ->
416           TacticAst.Seq (loc, tacticals)
417       ]
418     | "then" NONA
419       [ tac = tactical;
420         PAREN "["; tacs = LIST0 tactical SEP SYMBOL ";"; PAREN "]" ->
421           (TacticAst.Then (loc, tac, tacs))
422       ]
423     | "loops" RIGHTA
424       [ [ IDENT "do" ]; count = int; tac = tactical ->
425           TacticAst.Do (loc, count, tac)
426       | [ IDENT "repeat" ]; tac = tactical ->
427           TacticAst.Repeat (loc, tac)
428       ]
429     | "simple" NONA
430       [ IDENT "tries";
431         PAREN "["; tacs = LIST0 tactical SEP SYMBOL ";"; PAREN "]" ->
432           TacticAst.Tries (loc, tacs)
433       | IDENT "try"; tac = NEXT ->
434           TacticAst.Try (loc, tac)
435       | IDENT "fail" -> TacticAst.Fail loc
436       | IDENT "id" -> TacticAst.IdTac loc
437       | PAREN "("; tac = tactical; PAREN ")" -> tac
438       | tac = tactic -> TacticAst.Tactic (loc, tac)
439       ]
440     ];
441   theorem_flavour: [
442     [ [ IDENT "definition"  ] -> `Definition
443     | [ IDENT "fact"        ] -> `Fact
444     | [ IDENT "lemma"       ] -> `Lemma
445     | [ IDENT "remark"      ] -> `Remark
446     | [ IDENT "theorem"     ] -> `Theorem
447     ]
448   ];
449   inductive_spec: [ [
450     fst_name = IDENT; params = LIST0 [
451       PAREN "("; names = LIST1 IDENT SEP SYMBOL ","; SYMBOL ":";
452       typ = term; PAREN ")" -> (names, typ) ];
453     SYMBOL ":"; fst_typ = term; SYMBOL <:unicode<def>>; OPT SYMBOL "|";
454     fst_constructors = LIST0 constructor SEP SYMBOL "|";
455     tl = OPT [ "with";
456       types = LIST1 [
457         name = IDENT; SYMBOL ":"; typ = term; SYMBOL <:unicode<def>>;
458        OPT SYMBOL "|"; constructors = LIST0 constructor SEP SYMBOL "|" ->
459           (name, true, typ, constructors) ] SEP "with" -> types
460     ] ->
461       let params =
462         List.fold_right
463           (fun (names, typ) acc ->
464             (List.map (fun name -> (name, typ)) names) @ acc)
465           params []
466       in
467       let fst_ind_type = (fst_name, true, fst_typ, fst_constructors) in
468       let tl_ind_types = match tl with None -> [] | Some types -> types in
469       let ind_types = fst_ind_type :: tl_ind_types in
470       (params, ind_types)
471   ] ];
472
473   macro: [[
474       [ IDENT "abort" ] -> TacticAst.Abort loc
475     | [ IDENT "quit"  ] -> TacticAst.Quit loc
476     | [ IDENT "print" ]; name = QSTRING -> TacticAst.Print (loc, name)
477     | [ IDENT "undo"   ]; steps = OPT NUM ->
478         TacticAst.Undo (loc, int_opt steps)
479     | [ IDENT "redo"   ]; steps = OPT NUM ->
480         TacticAst.Redo (loc, int_opt steps)
481     | [ IDENT "check"   ]; t = term ->
482         TacticAst.Check (loc, t)
483     | [ IDENT "hint" ] -> TacticAst.Hint loc
484     | [ IDENT "whelp"; "match" ] ; t = term -> 
485         TacticAst.WMatch (loc,t)
486     | [ IDENT "whelp"; IDENT "instance" ] ; t = term -> 
487         TacticAst.WInstance (loc,t)
488     | [ IDENT "whelp"; IDENT "locate" ] ; id = IDENT -> 
489         TacticAst.WLocate (loc,id)
490     | [ IDENT "whelp"; IDENT "elim" ] ; t = term ->
491         TacticAst.WElim (loc, t)
492     | [ IDENT "whelp"; IDENT "hint" ] ; t = term -> 
493         TacticAst.WHint (loc,t)
494     | [ IDENT "print" ]; name = QSTRING -> TacticAst.Print (loc, name)
495   ]];
496
497   alias_spec: [
498     [ IDENT "id"; id = QSTRING; SYMBOL "="; uri = QSTRING ->
499       let alpha = "[a-zA-Z]" in
500       let num = "[0-9]+" in
501       let ident_cont = "\\("^alpha^"\\|"^num^"\\|_\\|\\\\\\)" in
502       let ident = "\\("^alpha^ident_cont^"*\\|_"^ident_cont^"+\\)" in
503       let rex = Str.regexp ("^"^ident^"$") in
504       if Str.string_match rex id 0 then
505         let rex = Str.regexp 
506           ("^\\(cic:/\\|theory:/\\)"^ident^
507            "\\(/"^ident^"+\\)*\\(\\."^ident^"\\)+"^
508            "\\(#xpointer("^ num^"\\(/"^num^"\\)+)\\)?$") 
509         in
510         if Str.string_match rex uri 0 then
511           TacticAst.Ident_alias (id, uri)
512         else 
513           raise (Parse_error (loc,sprintf "Not a valid uri: %s" uri))
514       else
515         raise (Parse_error (loc,sprintf "Not a valid identifier: %s" id))
516     | IDENT "symbol"; symbol = QSTRING;
517       instance = OPT [ PAREN "("; IDENT "instance"; n = NUM; PAREN ")" -> n ];
518       SYMBOL "="; dsc = QSTRING ->
519         let instance =
520           match instance with Some i -> int_of_string i | None -> 0
521         in
522         TacticAst.Symbol_alias (symbol, instance, dsc)
523     | IDENT "num";
524       instance = OPT [ PAREN "("; IDENT "instance"; n = NUM; PAREN ")" -> n ];
525       SYMBOL "="; dsc = QSTRING ->
526         let instance =
527           match instance with Some i -> int_of_string i | None -> 0
528         in
529         TacticAst.Number_alias (instance, dsc)
530     ]
531   ];
532   
533   command: [[
534       [ IDENT "set"    ]; n = QSTRING; v = QSTRING ->
535         TacticAst.Set (loc, n, v)
536     | [ IDENT "qed"   ] -> TacticAst.Qed loc
537     | flavour = theorem_flavour; name = OPT IDENT; SYMBOL ":"; typ = term;
538       body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
539         TacticAst.Theorem (loc, flavour, name, typ, body)
540     | "let"; ind_kind = [ "corec" -> `CoInductive | "rec"-> `Inductive ];
541         defs = let_defs -> 
542           let name,ty = 
543             match defs with
544             | ((Cic.Name name,Some ty),_,_) :: _ -> name,ty
545             | ((Cic.Name name,None),_,_) :: _ -> 
546                 fail loc ("No type given for " ^ name)
547             | _ -> assert false 
548           in
549           let body = CicAst.Ident (name,None) in
550           TacticAst.Theorem(loc, `Definition, Some name, ty,
551             Some (CicAst.LetRec (ind_kind, defs, body)))
552           
553     | [ IDENT "inductive" ]; spec = inductive_spec ->
554         let (params, ind_types) = spec in
555         TacticAst.Inductive (loc, params, ind_types)
556     | [ IDENT "coinductive" ]; spec = inductive_spec ->
557         let (params, ind_types) = spec in
558         let ind_types = (* set inductive flags to false (coinductive) *)
559           List.map (fun (name, _, term, ctors) -> (name, false, term, ctors))
560             ind_types
561         in
562         TacticAst.Inductive (loc, params, ind_types)
563     | [ IDENT "coercion" ] ; name = IDENT -> 
564         TacticAst.Coercion (loc, CicAst.Ident (name,Some []))
565     | [ IDENT "coercion" ] ; name = URI -> 
566         TacticAst.Coercion (loc, CicAst.Uri (name,Some []))
567     | [ IDENT "alias"   ]; spec = alias_spec ->
568         TacticAst.Alias (loc, spec)
569   ]];
570
571   executable: [
572     [ cmd = command; SYMBOL "." -> TacticAst.Command (loc, cmd)
573     | tac = tactical; SYMBOL "." -> TacticAst.Tactical (loc, tac)
574     | mac = macro; SYMBOL "." -> TacticAst.Macro (loc, mac)
575     ]
576   ];
577   
578   comment: [
579     [ BEGINCOMMENT ; ex = executable ; ENDCOMMENT -> 
580        TacticAst.Code (loc, ex)
581     | str = NOTE -> 
582        TacticAst.Note (loc, str)
583     ]
584   ];
585   
586   statement: [
587     [ ex = executable -> TacticAst.Executable (loc,ex)
588     | com = comment -> TacticAst.Comment (loc, com)
589     ]
590   ];
591 END
592
593 let exc_located_wrapper f =
594   try
595     f ()
596   with
597   | Stdpp.Exc_located (floc, Stream.Error msg) ->
598       raise (Parse_error (floc, msg))
599   | Stdpp.Exc_located (floc, exn) ->
600       raise (Parse_error (floc, (Printexc.to_string exn)))
601
602 let parse_term stream =
603   exc_located_wrapper (fun () -> (Grammar.Entry.parse term0 stream))
604 let parse_statement stream =
605   exc_located_wrapper (fun () -> (Grammar.Entry.parse statement stream))
606
607 (**/**)
608
609 (** {2 Interface for gTopLevel} *)
610
611 module EnvironmentP3 =
612   struct
613     type t = environment
614
615     let empty = ""
616
617     let aliases_grammar = Grammar.gcreate cic_lexer
618     let aliases = Grammar.Entry.create aliases_grammar "aliases"
619
620     let to_string env =
621       let aliases =
622         Environment.fold
623           (fun domain_item (dsc, _) acc ->
624             let s =
625               match domain_item with
626               | Id id ->
627                   TacticAstPp.pp_alias (TacticAst.Ident_alias (id, dsc)) ^ "."
628               | Symbol (symb, i) ->
629                   TacticAstPp.pp_alias (TacticAst.Symbol_alias (symb, i, dsc))
630                   ^ "."
631               | Num i ->
632                   TacticAstPp.pp_alias (TacticAst.Number_alias (i, dsc)) ^ "."
633             in
634             s :: acc)
635           env []
636       in
637       String.concat "\n" (List.sort compare aliases)
638
639     EXTEND
640       GLOBAL: aliases;
641       aliases: [  (* build an environment from an aliases list *)
642         [ aliases = LIST0 alias; EOI ->
643             List.fold_left
644               (fun env (domain_item, codomain_item) ->
645                 Environment.add domain_item codomain_item env)
646               Environment.empty aliases
647         ]
648       ];
649       alias: [  (* return a pair <domain_item, codomain_item> from an alias *)
650         [ IDENT "alias";
651           choice =
652             [ IDENT "id"; id = IDENT; SYMBOL "="; uri = URI ->
653                 (Id id, choice_of_uri uri)
654             | IDENT "symbol"; symbol = QSTRING;
655               PAREN "("; IDENT "instance"; instance = NUM; PAREN ")";
656               SYMBOL "="; dsc = QSTRING ->
657                 (Symbol (symbol, int_of_string instance),
658                  DisambiguateChoices.lookup_symbol_by_dsc symbol dsc)
659             | IDENT "num";
660               PAREN "("; IDENT "instance"; instance = NUM; PAREN ")";
661               SYMBOL "="; dsc = QSTRING ->
662                 (Num (int_of_string instance),
663                  DisambiguateChoices.lookup_num_by_dsc dsc)
664             ] -> choice ]
665       ];
666     END
667
668     let of_string s =
669       if s = empty then
670         Environment.empty
671       else
672         exc_located_wrapper
673           (fun () -> Grammar.Entry.parse aliases (Stream.of_string s))
674   end
675
676 (* vim:set encoding=utf8: *)