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