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