]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_disambiguation/cicTextualParser2.ml
d32302a131e11c2d67ce292c4744ec6e18d995c1
[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 script = Grammar.Entry.create grammar "script"
69
70 let return_term loc term = CicAst.AttributedTerm (`Loc loc, term)
71 let return_tactic loc tactic = TacticAst.LocatedTactic (loc, tactic)
72 let return_tactical loc tactical = TacticAst.LocatedTactical (loc, tactical)
73 let return_command loc cmd = cmd  (* TODO ZACK FIXME uhm ... why we drop loc? *)
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   (** the uri of an inductive type (a ".ind" uri) is not meaningful without an
92   * xpointer. Still, it's likely that an user who wrote "cic:/blabla/foo.ind"
93   * actually meant "cic:/blabla/foo.ind#xpointer(1/1)", i.e. the first inductive
94   * type in a block of mutual inductive types.
95   *
96   * This function performs the expansion foo.ind -> foo#xpointer..., if needed
97   *)
98 let ind_expansion uri =
99   let len = String.length uri in
100   if len >= 4 && String.sub uri (len - 4) 4 = ".ind" then
101     uri ^ "#xpointer(1/1)"
102   else
103     uri
104
105 let mk_binder_ast binder typ vars body =
106   List.fold_right
107     (fun var body ->
108        let name = name_of_string var in
109        CicAst.Binder (binder, (name, typ), body))
110     vars body
111
112 EXTEND
113   GLOBAL: term term0 tactic tactical tactical0 command script;
114   int: [
115     [ num = NUM ->
116         try
117           int_of_string num
118         with Failure _ -> raise (Parse_error (loc, "integer literal expected"))
119     ]
120   ];
121   meta_subst: [
122     [ s = SYMBOL "_" -> None
123     | t = term -> Some t ]
124   ];
125   binder_low: [
126     [ SYMBOL <:unicode<Pi>>     (* Π *) -> `Pi
127     | SYMBOL <:unicode<exists>> (* ∃ *) -> `Exists
128     | SYMBOL <:unicode<forall>> (* ∀ *) -> `Forall ]
129   ];
130   binder_high: [ [ SYMBOL <:unicode<lambda>> (* λ *) -> `Lambda ] ];
131   sort: [
132     [ "Prop" -> `Prop
133     | "Set" -> `Set
134     | "Type" -> `Type
135     | "CProp" -> `CProp ]
136   ];
137   typed_name: [
138     [ PAREN "("; i = IDENT; SYMBOL ":"; typ = term; PAREN ")" ->
139         (Cic.Name i, Some typ)
140     | i = IDENT -> (Cic.Name i, None)
141     ]
142   ];
143   subst: [
144     [ subst = OPT [
145         SYMBOL "\\subst";  (* to avoid catching frequent "a [1]" cases *)
146         PAREN "[";
147         substs = LIST1 [
148           i = IDENT; SYMBOL <:unicode<Assign>> (* ≔ *); t = term -> (i, t)
149         ] SEP SYMBOL ";";
150         PAREN "]" ->
151           substs
152       ] -> subst
153     ]
154   ];
155   substituted_name: [ (* a subs.name is an explicit substitution subject *)
156     [ s = IDENT; subst = subst -> CicAst.Ident (s, subst)
157     | s = URI; subst = subst -> CicAst.Uri (ind_expansion s, subst)
158     ]
159   ];
160   name: [ (* as substituted_name with no explicit substitution *)
161     [ s = [ IDENT | SYMBOL ] -> s ]
162   ];
163   pattern: [
164     [ n = name -> (n, [])
165     | PAREN "("; head = name; vars = LIST1 typed_name; PAREN ")" ->
166         (head, vars)
167     ]
168   ];
169   let_defs:[
170     [ defs = LIST1 [
171         name = IDENT;
172         args = LIST1 [
173           PAREN "(" ; names = LIST1 IDENT SEP SYMBOL ","; SYMBOL ":";
174           ty = term; PAREN ")" ->
175             (names, ty)
176         ];
177         index_name = OPT [ IDENT "on"; idx = IDENT -> idx ];
178         ty = OPT [ SYMBOL ":" ; t = term -> t ];
179         SYMBOL <:unicode<def>> (* ≝ *);
180         t1 = term ->
181           let rec list_of_binder binder ty final_term = function
182             | [] -> final_term
183             | name::tl -> 
184                 CicAst.Binder (binder, (Cic.Name name, Some ty), 
185                   list_of_binder binder ty final_term tl)
186           in
187           let rec binder_of_arg_list binder final_term = function
188             | [] -> final_term
189             | (l,ty)::tl ->  
190                 list_of_binder binder ty 
191                   (binder_of_arg_list binder final_term tl) l
192           in
193           let t1' = binder_of_arg_list `Lambda t1 args in
194           let ty' = 
195             match ty with 
196             | None -> None
197             | Some ty -> Some (binder_of_arg_list `Pi ty args)
198           in
199           let rec get_position_of name n = function 
200             | [] -> (None,n)
201             | nam::tl -> 
202                 if nam = name then 
203                   (Some n,n) 
204                 else 
205                   (get_position_of name (n+1) tl)
206           in
207           let rec find_arg name n = function 
208             | [] -> (fail loc (sprintf "Argument %s not found" name))
209             | (l,_)::tl -> 
210                 let (got,len) = get_position_of name 0 l in
211                 (match got with 
212                 | None -> (find_arg name (n+len) tl)
213                 | Some where -> n + where)
214           in
215           let index = 
216             (match index_name with 
217              | None -> 0 
218              | (Some name) -> find_arg name 0 args)
219           in
220           ((Cic.Name name,ty'), t1', index)
221       ] SEP "and" -> defs
222     ]];
223   constructor: [ [ name = IDENT; SYMBOL ":"; typ = term -> (name, typ) ] ];
224   binder_vars: [
225       [ vars = LIST1 IDENT SEP SYMBOL ",";
226         typ = OPT [ SYMBOL ":"; t = term -> t ] -> (vars, typ)
227       | PAREN "("; vars = LIST1 IDENT SEP SYMBOL ",";
228         typ = OPT [ SYMBOL ":"; t = term -> t ]; PAREN ")" -> (vars, typ)
229       ]
230   ];
231   term0: [ [ t = term; EOI -> return_term loc t ] ];
232   term:
233     [ "letin" NONA
234       [ "let"; var = typed_name;
235         SYMBOL <:unicode<def>> (* ≝ *);
236         t1 = term; "in"; t2 = term ->
237           return_term loc (CicAst.LetIn (var, t1, t2))
238       | "let"; ind_kind = [ "corec" -> `CoInductive | "rec"-> `Inductive ];
239         defs = let_defs; "in"; body = term ->
240             return_term loc (CicAst.LetRec (ind_kind, defs, body))
241       ]
242     | "binder" RIGHTA
243       [
244         b = binder_low; (vars, typ) = binder_vars; SYMBOL "."; body = term ->
245           let binder = mk_binder_ast b typ vars body in
246           return_term loc binder
247       | t1 = term; SYMBOL <:unicode<to>> (* → *); t2 = term ->
248           return_term loc (CicAst.Binder (`Pi, (Cic.Anonymous, Some t1), t2))
249       ]
250     | "logic_add" LEFTA   [ (* nothing here by default *) ]
251     | "logic_mult" LEFTA  [ (* nothing here by default *) ]
252     | "logic_inv" NONA    [ (* nothing here by default *) ]
253     | "relop" LEFTA
254       [ t1 = term; SYMBOL "="; t2 = term ->
255         return_term loc (CicAst.Appl [CicAst.Symbol ("eq", 0); t1; t2])
256       ]
257     | "add" LEFTA     [ (* nothing here by default *) ]
258     | "mult" LEFTA    [ (* nothing here by default *) ]
259     | "power" LEFTA   [ (* nothing here by default *) ]
260     | "inv" NONA      [ (* nothing here by default *) ]
261     | "apply" LEFTA
262       [ t1 = term; t2 = term ->
263         let rec aux = function
264           | CicAst.Appl (hd :: tl) -> aux hd @ tl
265           | term -> [term]
266         in
267         CicAst.Appl (aux t1 @ [t2])
268       ]
269     | "binder" RIGHTA
270       [
271         b = binder_high; (vars, typ) = binder_vars; SYMBOL "."; body = term ->
272           let binder = mk_binder_ast b typ vars body in
273           return_term loc binder
274       ]
275     | "simple" NONA
276       [ sort = sort -> CicAst.Sort sort
277       | n = substituted_name -> return_term loc n
278       | i = NUM -> return_term loc (CicAst.Num (i, (fresh_num_instance ())))
279       | IMPLICIT -> return_term loc CicAst.Implicit
280       | m = META;
281         substs = [
282           PAREN "["; substs = LIST0 meta_subst SEP SYMBOL ";" ; PAREN "]" ->
283             substs
284         ] ->
285             let index =
286               try
287                 int_of_string (String.sub m 1 (String.length m - 1))
288               with Failure "int_of_string" ->
289                 fail loc ("Invalid meta variable number: " ^ m)
290             in
291             return_term loc (CicAst.Meta (index, substs))
292       | outtyp = OPT [ PAREN "["; typ = term; PAREN "]" -> typ ];
293         "match"; t = term;
294         indty_ident = OPT [ SYMBOL ":"; id = IDENT -> id ];
295         "with";
296         PAREN "[";
297         patterns = LIST0 [
298           lhs = pattern; SYMBOL <:unicode<Rightarrow>> (* ⇒ *); rhs = term
299           ->
300             ((lhs: CicAst.case_pattern), rhs)
301         ] SEP SYMBOL "|";
302         PAREN "]" ->
303           return_term loc
304             (CicAst.Case (t, indty_ident, outtyp, patterns))
305       | PAREN "("; t1 = term; SYMBOL ":"; t2 = term; PAREN ")" ->
306           return_term loc (CicAst.Appl [CicAst.Symbol ("cast", 0); t1; t2])
307       | PAREN "("; t = term; PAREN ")" -> return_term loc t
308       ]
309     ];
310   tactic_where: [
311     [ where = OPT [ "in"; ident = IDENT -> ident ] -> where ]
312   ];
313   tactic_term: [ [ t = term -> t ] ];
314   ident_list0: [
315     [ PAREN "["; idents = LIST0 IDENT SEP SYMBOL ";"; PAREN "]" -> idents ]
316   ];
317   ident_list1: [
318     [ PAREN "["; idents = LIST1 IDENT SEP SYMBOL ";"; PAREN "]" -> idents ]
319   ];
320   reduction_kind: [
321     [ [ IDENT "reduce" | IDENT "Reduce" ] -> `Reduce
322     | [ IDENT "simplify" | IDENT "Simplify" ] -> `Simpl
323     | [ IDENT "whd" | IDENT "Whd" ] -> `Whd ]
324   ];
325   tactic: [
326     [ [ IDENT "absurd" | IDENT "Absurd" ]; t = tactic_term ->
327         return_tactic loc (TacticAst.Absurd t)
328     | [ IDENT "apply" | IDENT "Apply" ]; t = tactic_term ->
329         return_tactic loc (TacticAst.Apply t)
330     | [ IDENT "assumption" | IDENT "Assumption" ] ->
331         return_tactic loc TacticAst.Assumption
332     | [ IDENT "auto" | IDENT "Auto" ] -> return_tactic loc TacticAst.Auto
333     | [ IDENT "change" | IDENT "Change" ];
334       t1 = tactic_term; "with"; t2 = tactic_term;
335       where = tactic_where ->
336         return_tactic loc (TacticAst.Change (t1, t2, where))
337     (* TODO Change_pattern *)
338     | [ IDENT "contradiction" | IDENT "Contradiction" ] ->
339         return_tactic loc TacticAst.Contradiction
340     | [ IDENT "cut" | IDENT "Cut" ];
341       t = tactic_term -> return_tactic loc (TacticAst.Cut t)
342     | [ IDENT "decompose" | IDENT "Decompose" ];
343       principles = ident_list1; where = IDENT ->
344         return_tactic loc (TacticAst.Decompose (where, principles))
345     | [ IDENT "discriminate" | IDENT "Discriminate" ];
346       hyp = IDENT ->
347         return_tactic loc (TacticAst.Discriminate hyp)
348     | [ IDENT "elimType" | IDENT "ElimType" ]; t = tactic_term ->
349         return_tactic loc (TacticAst.ElimType t)
350     | [ IDENT "elim" | IDENT "Elim" ];
351       t1 = tactic_term;
352       using = OPT [ "using"; using = tactic_term -> using ] ->
353         return_tactic loc (TacticAst.Elim (t1, using))
354     | [ IDENT "exact" | IDENT "Exact" ]; t = tactic_term ->
355         return_tactic loc (TacticAst.Exact t)
356     | [ IDENT "exists" | IDENT "Exists" ] ->
357         return_tactic loc TacticAst.Exists
358     | [ IDENT "fold" | IDENT "Fold" ];
359       kind = reduction_kind; t = tactic_term ->
360         return_tactic loc (TacticAst.Fold (kind, t))
361     | [ IDENT "fourier" | IDENT "Fourier" ] ->
362         return_tactic loc TacticAst.Fourier
363     | [ IDENT "hint" | IDENT "Hint" ] -> return_tactic loc TacticAst.Hint
364     | [ IDENT "injection" | IDENT "Injection" ]; ident = IDENT ->
365         return_tactic loc (TacticAst.Injection ident)
366     | [ IDENT "intros" | IDENT "Intros" ];
367       num = OPT [ num = int -> num ];
368       idents = OPT ident_list0 ->
369         let idents = match idents with None -> [] | Some idents -> idents in
370         return_tactic loc (TacticAst.Intros (num, idents))
371     | [ IDENT "intro" | IDENT "Intro" ] ->
372         return_tactic loc (TacticAst.Intros (None, []))
373     | [ IDENT "left" | IDENT "Left" ] -> return_tactic loc TacticAst.Left
374     | [ "let" | "Let" ];
375       t = tactic_term; "in"; where = IDENT ->
376         return_tactic loc (TacticAst.LetIn (t, where))
377     | kind = reduction_kind;
378       pat = OPT [
379         "in"; pat = [ IDENT "goal" -> `Goal | IDENT "hyp" -> `Everywhere ] ->
380           pat
381       ];
382       terms = LIST0 term SEP SYMBOL "," ->
383         let tac =
384           (match (pat, terms) with
385           | None, [] -> TacticAst.Reduce (kind, None)
386           | None, terms -> TacticAst.Reduce (kind, Some (terms, `Goal))
387           | Some pat, [] -> TacticAst.Reduce (kind, Some ([], pat))
388           | Some pat, terms -> TacticAst.Reduce (kind, Some (terms, pat)))
389         in
390         return_tactic loc tac
391     | [ IDENT "reflexivity" | IDENT "Reflexivity" ] ->
392         return_tactic loc TacticAst.Reflexivity
393     | [ IDENT "replace" | IDENT "Replace" ];
394       t1 = tactic_term; "with"; t2 = tactic_term ->
395         return_tactic loc (TacticAst.Replace (t1, t2))
396     (* TODO Rewrite *)
397     (* TODO Replace_pattern *)
398     | [ IDENT "right" | IDENT "Right" ] -> return_tactic loc TacticAst.Right
399     | [ IDENT "ring" | IDENT "Ring" ] -> return_tactic loc TacticAst.Ring
400     | [ IDENT "split" | IDENT "Split" ] -> return_tactic loc TacticAst.Split
401     | [ IDENT "symmetry" | IDENT "Symmetry" ] ->
402         return_tactic loc TacticAst.Symmetry
403     | [ IDENT "transitivity" | IDENT "Transitivity" ];
404       t = tactic_term ->
405         return_tactic loc (TacticAst.Transitivity t)
406     ]
407   ];
408   tactical0: [ [ t = tactical; SYMBOL "." -> return_tactical loc t ] ];
409   tactical:
410     [ "command" NONA
411       [ cmd = command -> return_tactical loc (TacticAst.Command cmd) ]
412     | "sequence" LEFTA
413       [ tactics = LIST1 NEXT SEP SYMBOL ";" ->
414           (match tactics with
415           | [tactic] -> tactic
416           | _ -> return_tactical loc (TacticAst.Seq tactics))
417       ]
418     | "then" NONA
419       [ tac = tactical;
420         PAREN "["; tacs = LIST0 tactical SEP SYMBOL ";"; PAREN "]" ->
421           return_tactical loc (TacticAst.Then (tac, tacs))
422       ]
423     | "loops" RIGHTA
424       [ [ IDENT "do" | IDENT "Do" ]; count = int; tac = tactical ->
425           return_tactical loc (TacticAst.Do (count, tac))
426       | [ IDENT "repeat" | IDENT "Repeat" ]; tac = tactical ->
427           return_tactical loc (TacticAst.Repeat tac)
428       ]
429     | "simple" NONA
430       [ [ IDENT "tries" | IDENT "Tries" ];
431         PAREN "["; tacs = LIST0 tactical SEP SYMBOL ";"; PAREN "]" ->
432           return_tactical loc (TacticAst.Tries tacs)
433       | [ IDENT "try" | IDENT "Try" ]; tac = NEXT ->
434           return_tactical loc (TacticAst.Try tac)
435       | [ IDENT "fail" | IDENT "Fail" ] -> return_tactical loc TacticAst.Fail
436       | [ IDENT "id" | IDENT "Id" ] -> return_tactical loc TacticAst.IdTac
437       | PAREN "("; tac = tactical; PAREN ")" -> return_tactical loc tac
438       | tac = tactic -> return_tactical loc (TacticAst.Tactic tac)
439       ]
440     ];
441   theorem_flavour: [  (* all flavours but Goal *)
442     [ [ IDENT "definition"  | IDENT "Definition"  ] -> `Definition
443     | [ IDENT "fact"        | IDENT "Fact"        ] -> `Fact
444     | [ IDENT "lemma"       | IDENT "Lemma"       ] -> `Lemma
445     | [ IDENT "remark"      | IDENT "Remark"      ] -> `Remark
446     | [ IDENT "theorem"     | 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   print_kind: [
473     [ [ IDENT "Env" | IDENT "env" | IDENT "Environment" | IDENT "environment" ]
474       -> `Env
475     | [ IDENT "Coer" | IDENT "coer" | IDENT "Coercions" | IDENT "coercions" ]
476       -> `Coer
477   ] ];
478
479   command: [
480     [ [ IDENT "abort" | IDENT "Abort" ] -> return_command loc TacticAst.Abort
481     | [ IDENT "proof" | IDENT "Proof" ] -> return_command loc TacticAst.Proof
482     | [ IDENT "quit"  | IDENT "Quit"  ] -> return_command loc TacticAst.Quit
483     | [ IDENT "qed"   | IDENT "Qed"   ] ->
484         return_command loc (TacticAst.Qed None)
485     | [ IDENT "print"   | IDENT "Print" ];
486       print_kind = print_kind ->
487             return_command loc (TacticAst.Print print_kind)
488     | [ IDENT "save"  | IDENT "Save"  ]; name = IDENT ->
489         return_command loc (TacticAst.Qed (Some name))
490     | flavour = theorem_flavour; name = OPT IDENT; SYMBOL ":"; typ = term;
491       body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
492         return_command loc (TacticAst.Theorem (flavour, name, typ, body))
493     | "let"; ind_kind = [ "corec" -> `CoInductive | "rec"-> `Inductive ];
494         defs = let_defs -> 
495           let name,ty = 
496             match defs with
497             | ((Cic.Name name,Some ty),_,_) :: _ -> name,ty
498             | ((Cic.Name name,None),_,_) :: _ -> 
499                 fail loc ("No type given for " ^ name)
500             | _ -> assert false 
501           in
502           let body = CicAst.Ident (name,None) in
503           TacticAst.Theorem(`Definition, Some name, ty,
504             Some (CicAst.LetRec (ind_kind, defs, body)))
505           
506     | [ IDENT "inductive" | IDENT "Inductive" ]; spec = inductive_spec ->
507         let (params, ind_types) = spec in
508         return_command loc (TacticAst.Inductive (params, ind_types))
509     | [ IDENT "coinductive" | IDENT "CoInductive" ]; spec = inductive_spec ->
510         let (params, ind_types) = spec in
511         let ind_types = (* set inductive flags to false (coinductive) *)
512           List.map (fun (name, _, term, ctors) -> (name, false, term, ctors))
513             ind_types
514         in
515         return_command loc (TacticAst.Inductive (params, ind_types))
516     | [ IDENT "coercion" | IDENT "Coercion" ] ; name = IDENT -> 
517         return_command loc (TacticAst.Coercion (CicAst.Ident (name,Some [])))
518     | [ IDENT "coercion" | IDENT "Coercion" ] ; name = URI -> 
519         return_command loc (TacticAst.Coercion (CicAst.Uri (name,Some [])))
520     | [ IDENT "goal" | IDENT "Goal" ]; typ = term;
521       body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
522         return_command loc (TacticAst.Theorem (`Goal, None, typ, body))
523     | [ IDENT "undo"   | IDENT "Undo" ]; steps = OPT NUM ->
524         return_command loc (TacticAst.Undo (int_opt steps))
525     | [ IDENT "redo"   | IDENT "Redo" ]; steps = OPT NUM ->
526         return_command loc (TacticAst.Redo (int_opt steps))
527     | [ IDENT "baseuri"   | IDENT "Baseuri" ]; uri = OPT QSTRING ->
528         return_command loc (TacticAst.Baseuri uri)
529     | [ IDENT "basedir"   | IDENT "Basedir" ]; uri = OPT QSTRING ->
530         return_command loc (TacticAst.Basedir uri)
531     | [ IDENT "check"   | IDENT "Check" ]; t = term ->
532         return_command loc (TacticAst.Check t)
533 (*
534     | [ IDENT "alias"   | IDENT "Alias" ]; spec = alias_spec ->
535         return_command loc (TacticAst.Alias spec)
536 *)
537     ]
538   ];
539   script_entry: [
540     [ cmd = tactical0 -> Command cmd
541 (*     | s = COMMENT -> Comment (loc, s) *)
542     ]
543   ];
544   script: [ [ entries = LIST0 script_entry; EOI -> (loc, entries) ] ];
545 END
546
547 let exc_located_wrapper f =
548   try
549     f ()
550   with
551   | Stdpp.Exc_located (floc, Stream.Error msg) ->
552       raise (Parse_error (floc, msg))
553   | Stdpp.Exc_located (floc, exn) ->
554       raise (Parse_error (floc, (Printexc.to_string exn)))
555
556 let parse_term stream =
557   exc_located_wrapper (fun () -> (Grammar.Entry.parse term0 stream))
558 let parse_tactic stream =
559   exc_located_wrapper (fun () -> (Grammar.Entry.parse tactic stream))
560 let parse_tactical stream =
561   exc_located_wrapper (fun () -> (Grammar.Entry.parse tactical0 stream))
562 let parse_script stream =
563   exc_located_wrapper (fun () -> (Grammar.Entry.parse script stream))
564
565 (**/**)
566
567 (** {2 Interface for gTopLevel} *)
568
569 module EnvironmentP3 =
570   struct
571     type t = environment
572
573     let empty = ""
574
575     let aliases_grammar = Grammar.gcreate cic_lexer
576     let aliases = Grammar.Entry.create aliases_grammar "aliases"
577
578     let to_string env =
579       let aliases =
580         Environment.fold
581           (fun domain_item (dsc, _) acc ->
582             let s =
583               match domain_item with
584               | Id id -> sprintf "alias id %s = %s" id dsc
585               | Symbol (symb, instance) ->
586                   sprintf "alias symbol \"%s\" (instance %d) = \"%s\""
587                     symb instance dsc
588               | Num instance ->
589                   sprintf "alias num (instance %d) = \"%s\"" instance dsc
590             in
591             s :: acc)
592           env []
593       in
594       String.concat "\n" (List.sort compare aliases)
595
596     EXTEND
597       GLOBAL: aliases;
598       aliases: [  (* build an environment from an aliases list *)
599         [ aliases = LIST0 alias; EOI ->
600             List.fold_left
601               (fun env (domain_item, codomain_item) ->
602                 Environment.add domain_item codomain_item env)
603               Environment.empty aliases
604         ]
605       ];
606       alias: [  (* return a pair <domain_item, codomain_item> from an alias *)
607         [ IDENT "alias";
608           choice =
609             [ IDENT "id"; id = IDENT; SYMBOL "="; uri = URI ->
610                 (Id id, choice_of_uri uri)
611             | IDENT "symbol"; symbol = QSTRING;
612               PAREN "("; IDENT "instance"; instance = NUM; PAREN ")";
613               SYMBOL "="; dsc = QSTRING ->
614                 (Symbol (symbol, int_of_string instance),
615                  DisambiguateChoices.lookup_symbol_by_dsc symbol dsc)
616             | IDENT "num";
617               PAREN "("; IDENT "instance"; instance = NUM; PAREN ")";
618               SYMBOL "="; dsc = QSTRING ->
619                 (Num (int_of_string instance),
620                  DisambiguateChoices.lookup_num_by_dsc dsc)
621             ] -> choice ]
622       ];
623     END
624
625     let of_string s =
626       if s = empty then
627         Environment.empty
628       else
629         exc_located_wrapper
630           (fun () -> Grammar.Entry.parse aliases (Stream.of_string s))
631   end
632
633 (* vim:set encoding=utf8: *)