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