]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_disambiguation/cicTextualParser2.ml
4bfc7c4457ba7ea6f9be44469e1811bf071052d9
[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     | "binder" RIGHTA
229       [
230         b = binder;
231         (vars, typ) =
232           [ vars = LIST1 IDENT SEP SYMBOL ",";
233             typ = OPT [ SYMBOL ":"; t = term -> t ] -> (vars, typ)
234           | PAREN "("; vars = LIST1 IDENT SEP SYMBOL ",";
235             typ = OPT [ SYMBOL ":"; t = term -> t ]; PAREN ")" -> (vars, typ)
236           ];
237         SYMBOL "."; body = term ->
238           let binder =
239             List.fold_right
240               (fun var body ->
241                 let name = name_of_string var in
242                 CicAst.Binder (b, (name, typ), body))
243               vars body
244           in
245           return_term loc binder
246       | t1 = term; SYMBOL <:unicode<to>> (* → *); t2 = term ->
247             return_term loc
248               (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     | "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 (Some 1, []))
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           return_tactical loc (TacticAst.Seq tactics)
409       ]
410     | "then" NONA
411       [ tac = tactical;
412         PAREN "["; tacs = LIST0 tactical SEP SYMBOL ";"; PAREN "]" ->
413           return_tactical loc (TacticAst.Then (tac, tacs))
414       ]
415     | "loops" RIGHTA
416       [ [ IDENT "do" | IDENT "Do" ]; count = int; tac = tactical ->
417           return_tactical loc (TacticAst.Do (count, tac))
418       | [ IDENT "repeat" | IDENT "Repeat" ]; tac = tactical ->
419           return_tactical loc (TacticAst.Repeat tac)
420       ]
421     | "simple" NONA
422       [ [ IDENT "tries" | IDENT "Tries" ];
423         PAREN "["; tacs = LIST0 tactical SEP SYMBOL ";"; PAREN "]" ->
424           return_tactical loc (TacticAst.Tries tacs)
425       | [ IDENT "try" | IDENT "Try" ]; tac = NEXT ->
426           return_tactical loc (TacticAst.Try tac)
427       | [ IDENT "fail" | IDENT "Fail" ] -> return_tactical loc TacticAst.Fail
428       | [ IDENT "id" | IDENT "Id" ] -> return_tactical loc TacticAst.IdTac
429       | PAREN "("; tac = tactical; PAREN ")" -> return_tactical loc tac
430       | tac = tactic -> return_tactical loc (TacticAst.Tactic tac)
431       ]
432     ];
433   theorem_flavour: [  (* all flavours but Goal *)
434     [ [ IDENT "definition"  | IDENT "Definition"  ] -> `Definition
435     | [ IDENT "fact"        | IDENT "Fact"        ] -> `Fact
436     | [ IDENT "lemma"       | IDENT "Lemma"       ] -> `Lemma
437     | [ IDENT "remark"      | IDENT "Remark"      ] -> `Remark
438     | [ IDENT "theorem"     | IDENT "Theorem"     ] -> `Theorem
439     ]
440   ];
441   inductive_spec: [ [
442     fst_name = IDENT; params = LIST0 [
443       PAREN "("; names = LIST1 IDENT SEP SYMBOL ","; SYMBOL ":";
444       typ = term; PAREN ")" -> (names, typ) ];
445     SYMBOL ":"; fst_typ = term; SYMBOL <:unicode<def>>; OPT SYMBOL "|";
446     fst_constructors = LIST0 constructor SEP SYMBOL "|";
447     tl = OPT [ "with";
448       types = LIST1 [
449         name = IDENT; SYMBOL ":"; typ = term; SYMBOL <:unicode<def>>;
450        OPT SYMBOL "|"; constructors = LIST0 constructor SEP SYMBOL "|" ->
451           (name, true, typ, constructors) ] SEP "with" -> types
452     ] ->
453       let params =
454         List.fold_right
455           (fun (names, typ) acc ->
456             (List.map (fun name -> (name, typ)) names) @ acc)
457           params []
458       in
459       let fst_ind_type = (fst_name, true, fst_typ, fst_constructors) in
460       let tl_ind_types = match tl with None -> [] | Some types -> types in
461       let ind_types = fst_ind_type :: tl_ind_types in
462       (params, ind_types)
463   ] ];
464   print_kind: [
465     [ [ IDENT "Env" | IDENT "env" | IDENT "Environment" | IDENT "environment" ]
466       -> `Env
467     | [ IDENT "Coer" | IDENT "coer" | IDENT "Coercions" | IDENT "coercions" ]
468       -> `Coer
469   ] ];
470
471   command: [
472     [ [ IDENT "abort" | IDENT "Abort" ] -> return_command loc TacticAst.Abort
473     | [ IDENT "proof" | IDENT "Proof" ] -> return_command loc TacticAst.Proof
474     | [ IDENT "quit"  | IDENT "Quit"  ] -> return_command loc TacticAst.Quit
475     | [ IDENT "qed"   | IDENT "Qed"   ] ->
476         return_command loc (TacticAst.Qed None)
477     | [ IDENT "print"   | IDENT "Print" ];
478       print_kind = print_kind ->
479             return_command loc (TacticAst.Print print_kind)
480     | [ IDENT "save"  | IDENT "Save"  ]; name = IDENT ->
481         return_command loc (TacticAst.Qed (Some name))
482     | flavour = theorem_flavour; name = OPT IDENT; SYMBOL ":"; typ = term;
483       body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
484         return_command loc (TacticAst.Theorem (flavour, name, typ, body))
485     | "let"; ind_kind = [ "corec" -> `CoInductive | "rec"-> `Inductive ];
486         defs = let_defs -> 
487           let name,ty = 
488             match defs with
489             | ((Cic.Name name,Some ty),_,_) :: _ -> name,ty
490             | ((Cic.Name name,None),_,_) :: _ -> 
491                 fail loc ("No type given for " ^ name)
492             | _ -> assert false 
493           in
494           let body = CicAst.Ident (name,None) in
495           TacticAst.Theorem(`Definition, Some name, ty,
496             Some (CicAst.LetRec (ind_kind, defs, body)))
497           
498     | [ IDENT "inductive" | IDENT "Inductive" ]; spec = inductive_spec ->
499         let (params, ind_types) = spec in
500         return_command loc (TacticAst.Inductive (params, ind_types))
501     | [ IDENT "coinductive" | IDENT "CoInductive" ]; spec = inductive_spec ->
502         let (params, ind_types) = spec in
503         let ind_types = (* set inductive flags to false (coinductive) *)
504           List.map (fun (name, _, term, ctors) -> (name, false, term, ctors))
505             ind_types
506         in
507         return_command loc (TacticAst.Inductive (params, ind_types))
508     | [ IDENT "coercion" | IDENT "Coercion" ] ; name = IDENT -> 
509         return_command loc (TacticAst.Coercion (CicAst.Ident (name,Some [])))
510     | [ IDENT "coercion" | IDENT "Coercion" ] ; name = URI -> 
511         return_command loc (TacticAst.Coercion (CicAst.Uri (name,Some [])))
512     | [ IDENT "goal" | IDENT "Goal" ]; typ = term;
513       body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
514         return_command loc (TacticAst.Theorem (`Goal, None, typ, body))
515     | [ IDENT "undo"   | IDENT "Undo" ]; steps = OPT NUM ->
516         return_command loc (TacticAst.Undo (int_opt steps))
517     | [ IDENT "redo"   | IDENT "Redo" ]; steps = OPT NUM ->
518         return_command loc (TacticAst.Redo (int_opt steps))
519     | [ IDENT "baseuri"   | IDENT "Baseuri" ]; uri = OPT QSTRING ->
520         return_command loc (TacticAst.Baseuri uri)
521     | [ IDENT "basedir"   | IDENT "Basedir" ]; uri = OPT QSTRING ->
522         return_command loc (TacticAst.Basedir uri)
523     | [ IDENT "check"   | IDENT "Check" ]; t = term ->
524         return_command loc (TacticAst.Check t)
525 (*
526     | [ IDENT "alias"   | IDENT "Alias" ]; spec = alias_spec ->
527         return_command loc (TacticAst.Alias spec)
528 *)
529     ]
530   ];
531   script_entry: [
532     [ cmd = tactical0 -> Command cmd
533 (*     | s = COMMENT -> Comment (loc, s) *)
534     ]
535   ];
536   script: [ [ entries = LIST0 script_entry; EOI -> (loc, entries) ] ];
537 END
538
539 let exc_located_wrapper f =
540   try
541     f ()
542   with
543   | Stdpp.Exc_located (floc, Stream.Error msg) ->
544       raise (Parse_error (floc, msg))
545   | Stdpp.Exc_located (floc, exn) ->
546       raise (Parse_error (floc, (Printexc.to_string exn)))
547
548 let parse_term stream =
549   exc_located_wrapper (fun () -> (Grammar.Entry.parse term0 stream))
550 let parse_tactic stream =
551   exc_located_wrapper (fun () -> (Grammar.Entry.parse tactic stream))
552 let parse_tactical stream =
553   exc_located_wrapper (fun () -> (Grammar.Entry.parse tactical0 stream))
554 let parse_script stream =
555   exc_located_wrapper (fun () -> (Grammar.Entry.parse script stream))
556
557 (**/**)
558
559 (** {2 Interface for gTopLevel} *)
560
561 module EnvironmentP3 =
562   struct
563     type t = environment
564
565     let empty = ""
566
567     let aliases_grammar = Grammar.gcreate cic_lexer
568     let aliases = Grammar.Entry.create aliases_grammar "aliases"
569
570     let to_string env =
571       let aliases =
572         Environment.fold
573           (fun domain_item (dsc, _) acc ->
574             let s =
575               match domain_item with
576               | Id id -> sprintf "alias id %s = %s" id dsc
577               | Symbol (symb, instance) ->
578                   sprintf "alias symbol \"%s\" (instance %d) = \"%s\""
579                     symb instance dsc
580               | Num instance ->
581                   sprintf "alias num (instance %d) = \"%s\"" instance dsc
582             in
583             s :: acc)
584           env []
585       in
586       String.concat "\n" (List.sort compare aliases)
587
588     EXTEND
589       GLOBAL: aliases;
590       aliases: [  (* build an environment from an aliases list *)
591         [ aliases = LIST0 alias; EOI ->
592             List.fold_left
593               (fun env (domain_item, codomain_item) ->
594                 Environment.add domain_item codomain_item env)
595               Environment.empty aliases
596         ]
597       ];
598       alias: [  (* return a pair <domain_item, codomain_item> from an alias *)
599         [ IDENT "alias";
600           choice =
601             [ IDENT "id"; id = IDENT; SYMBOL "="; uri = URI ->
602                 (Id id, choice_of_uri uri)
603             | IDENT "symbol"; symbol = QSTRING;
604               PAREN "("; IDENT "instance"; instance = NUM; PAREN ")";
605               SYMBOL "="; dsc = QSTRING ->
606                 (Symbol (symbol, int_of_string instance),
607                  DisambiguateChoices.lookup_symbol_by_dsc symbol dsc)
608             | IDENT "num";
609               PAREN "("; IDENT "instance"; instance = NUM; PAREN ")";
610               SYMBOL "="; dsc = QSTRING ->
611                 (Num (int_of_string instance),
612                  DisambiguateChoices.lookup_num_by_dsc dsc)
613             ] -> choice ]
614       ];
615     END
616
617     let of_string s =
618       if s = empty then
619         Environment.empty
620       else
621         exc_located_wrapper
622           (fun () -> Grammar.Entry.parse aliases (Stream.of_string s))
623   end
624
625 (* vim:set encoding=utf8: *)