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