]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_notation/grafiteParser.ml
e7c54213d8f64f1ebc29d7548e7ec75b7ed5e268
[helm.git] / helm / ocaml / cic_notation / grafiteParser.ml
1 (* Copyright (C) 2005, 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 open Printf
27
28 module Ast = CicNotationPt
29
30 type statement =
31   (CicNotationPt.term, CicNotationPt.term, GrafiteAst.reduction,
32    GrafiteAst.obj, string)
33     GrafiteAst.statement
34
35 let grammar = CicNotationParser.level2_ast_grammar
36
37 let term = CicNotationParser.term
38 let statement = Grammar.Entry.create grammar "statement"
39
40 let add_raw_attribute ~text t = Ast.AttributedTerm (`Raw text, t)
41
42 let default_precedence = 50
43 let default_associativity = Gramext.NonA
44
45 EXTEND
46   GLOBAL: term statement;
47   arg: [
48    [ LPAREN; names = LIST1 IDENT SEP SYMBOL ",";
49       SYMBOL ":"; ty = term; RPAREN -> names,ty
50    | name = IDENT -> [name],Ast.Implicit
51    ]
52   ];
53   constructor: [ [ name = IDENT; SYMBOL ":"; typ = term -> (name, typ) ] ];
54   tactic_term: [ [ t = term LEVEL "90N" -> t ] ];
55   ident_list0: [ [ LPAREN; idents = LIST0 IDENT; RPAREN -> idents ] ];
56   tactic_term_list1: [
57     [ tactic_terms = LIST1 tactic_term SEP SYMBOL "," -> tactic_terms ]
58   ];
59   reduction_kind: [
60     [ IDENT "normalize" -> `Normalize
61     | IDENT "reduce" -> `Reduce
62     | IDENT "simplify" -> `Simpl
63     | IDENT "unfold"; t = OPT term -> `Unfold t
64     | IDENT "whd" -> `Whd ]
65   ];
66   sequent_pattern_spec: [
67    [ hyp_paths =
68       LIST0
69        [ id = IDENT ;
70          path = OPT [SYMBOL ":" ; path = tactic_term -> path ] ->
71          (id,match path with Some p -> p | None -> Ast.UserInput) ];
72      goal_path = OPT [ SYMBOL <:unicode<vdash>>; term = tactic_term -> term ] ->
73       let goal_path =
74        match goal_path, hyp_paths with
75           None, [] -> Ast.UserInput
76         | None, _::_ -> Ast.Implicit
77         | Some goal_path, _ -> goal_path
78       in
79        hyp_paths,goal_path
80    ]
81   ];
82   pattern_spec: [
83     [ res = OPT [
84        "in";
85        wanted_and_sps =
86         [ "match" ; wanted = tactic_term ;
87           sps = OPT [ "in"; sps = sequent_pattern_spec -> sps ] ->
88            Some wanted,sps
89         | sps = sequent_pattern_spec ->
90            None,Some sps
91         ] ->
92          let wanted,hyp_paths,goal_path =
93           match wanted_and_sps with
94              wanted,None -> wanted, [], Ast.UserInput
95            | wanted,Some (hyp_paths,goal_path) -> wanted,hyp_paths,goal_path
96          in
97           wanted, hyp_paths, goal_path ] ->
98       match res with
99          None -> None,[],Ast.UserInput
100        | Some ps -> ps]
101   ];
102   direction: [
103     [ SYMBOL ">" -> `LeftToRight
104     | SYMBOL "<" -> `RightToLeft ]
105   ];
106   int: [ [ num = NUMBER -> int_of_string num ] ];
107   intros_spec: [
108     [ num = OPT [ num = int -> num ]; idents = OPT ident_list0 ->
109         let idents = match idents with None -> [] | Some idents -> idents in
110         num, idents
111     ]
112   ];
113   using: [ [ using = OPT [ IDENT "using"; t = tactic_term -> t ] -> using ] ];
114   tactic: [
115     [ IDENT "absurd"; t = tactic_term ->
116         GrafiteAst.Absurd (loc, t)
117     | IDENT "apply"; t = tactic_term ->
118         GrafiteAst.Apply (loc, t)
119     | IDENT "assumption" ->
120         GrafiteAst.Assumption loc
121     | IDENT "auto";
122       depth = OPT [ IDENT "depth"; SYMBOL "="; i = int -> i ];
123       width = OPT [ IDENT "width"; SYMBOL "="; i = int -> i ];
124       paramodulation = OPT [ IDENT "paramodulation" ];
125       full = OPT [ IDENT "full" ] ->  (* ALB *)
126           GrafiteAst.Auto (loc,depth,width,paramodulation,full)
127     | IDENT "clear"; id = IDENT ->
128         GrafiteAst.Clear (loc,id)
129     | IDENT "clearbody"; id = IDENT ->
130         GrafiteAst.ClearBody (loc,id)
131     | IDENT "change"; what = pattern_spec; "with"; t = tactic_term ->
132         GrafiteAst.Change (loc, what, t)
133     | IDENT "compare"; t = tactic_term ->
134         GrafiteAst.Compare (loc,t)
135     | IDENT "constructor"; n = int ->
136         GrafiteAst.Constructor (loc, n)
137     | IDENT "contradiction" ->
138         GrafiteAst.Contradiction loc
139     | IDENT "cut"; t = tactic_term; ident = OPT [ "as"; id = IDENT -> id] ->
140         GrafiteAst.Cut (loc, ident, t)
141     | IDENT "decide"; IDENT "equality" ->
142         GrafiteAst.DecideEquality loc
143     | IDENT "decompose"; types = OPT ident_list0; what = IDENT;
144       (num, idents) = intros_spec ->
145         let types = match types with None -> [] | Some types -> types in
146         let to_spec id = GrafiteAst.Ident id in
147         GrafiteAst.Decompose (loc, List.rev_map to_spec types, what, idents)
148     | IDENT "discriminate"; t = tactic_term ->
149         GrafiteAst.Discriminate (loc, t)
150     | IDENT "elim"; what = tactic_term; using = using;
151       (num, idents) = intros_spec ->
152         GrafiteAst.Elim (loc, what, using, num, idents)
153     | IDENT "elimType"; what = tactic_term; using = using;
154       (num, idents) = intros_spec ->
155         GrafiteAst.ElimType (loc, what, using, num, idents)
156     | IDENT "exact"; t = tactic_term ->
157         GrafiteAst.Exact (loc, t)
158     | IDENT "exists" ->
159         GrafiteAst.Exists loc
160     | IDENT "fail" -> GrafiteAst.Fail loc
161     | IDENT "fold"; kind = reduction_kind; t = tactic_term; p = pattern_spec ->
162         let (pt,_,_) = p in
163           if pt <> None then
164             raise (HExtlib.Localized (loc, CicNotationParser.Parse_error
165               ("the pattern cannot specify the term to replace, only its"
166               ^ " paths in the hypotheses and in the conclusion")))
167         else
168          GrafiteAst.Fold (loc, kind, t, p)
169     | IDENT "fourier" ->
170         GrafiteAst.Fourier loc
171     | IDENT "fwd"; hyp = IDENT; idents = OPT ident_list0 ->
172         let idents = match idents with None -> [] | Some idents -> idents in
173         GrafiteAst.FwdSimpl (loc, hyp, idents)
174     | IDENT "generalize"; p=pattern_spec; id = OPT ["as" ; id = IDENT -> id] ->
175        GrafiteAst.Generalize (loc,p,id)
176     | IDENT "goal"; n = int ->
177         GrafiteAst.Goal (loc, n)
178     | IDENT "id" -> GrafiteAst.IdTac loc
179     | IDENT "injection"; t = tactic_term ->
180         GrafiteAst.Injection (loc, t)
181     | IDENT "intro"; ident = OPT IDENT ->
182         let idents = match ident with None -> [] | Some id -> [id] in
183         GrafiteAst.Intros (loc, Some 1, idents)
184     | IDENT "intros"; (num, idents) = intros_spec ->
185         GrafiteAst.Intros (loc, num, idents)
186     | IDENT "lapply"; 
187       depth = OPT [ IDENT "depth"; SYMBOL "="; i = int -> i ];
188       what = tactic_term; 
189       to_what = OPT [ "to" ; t = tactic_term_list1 -> t ];
190       ident = OPT [ IDENT "using" ; ident = IDENT -> ident ] ->
191         let to_what = match to_what with None -> [] | Some to_what -> to_what in
192         GrafiteAst.LApply (loc, depth, to_what, what, ident)
193     | IDENT "left" -> GrafiteAst.Left loc
194     | IDENT "letin"; where = IDENT ; SYMBOL <:unicode<def>> ; t = tactic_term ->
195         GrafiteAst.LetIn (loc, t, where)
196     | kind = reduction_kind; p = pattern_spec ->
197         GrafiteAst.Reduce (loc, kind, p)
198     | IDENT "reflexivity" ->
199         GrafiteAst.Reflexivity loc
200     | IDENT "replace"; p = pattern_spec; "with"; t = tactic_term ->
201         GrafiteAst.Replace (loc, p, t)
202     | IDENT "rewrite" ; d = direction; t = tactic_term ; p = pattern_spec ->
203        let (pt,_,_) = p in
204         if pt <> None then
205          raise
206           (HExtlib.Localized (loc,
207            (CicNotationParser.Parse_error
208             "the pattern cannot specify the term to rewrite, only its paths in the hypotheses and in the conclusion")))
209         else
210          GrafiteAst.Rewrite (loc, d, t, p)
211     | IDENT "right" ->
212         GrafiteAst.Right loc
213     | IDENT "ring" ->
214         GrafiteAst.Ring loc
215     | IDENT "split" ->
216         GrafiteAst.Split loc
217     | IDENT "symmetry" ->
218         GrafiteAst.Symmetry loc
219     | IDENT "transitivity"; t = tactic_term ->
220         GrafiteAst.Transitivity (loc, t)
221     ]
222   ];
223   atomic_tactical:
224     [ "sequence" LEFTA
225       [ t1 = SELF; SYMBOL ";"; t2 = SELF ->
226           let ts =
227             match t1 with
228             | GrafiteAst.Seq (_, l) -> l @ [ t2 ]
229             | _ -> [ t1; t2 ]
230           in
231           GrafiteAst.Seq (loc, ts)
232       ]
233     | "then" NONA
234       [ tac = SELF; SYMBOL ";";
235         SYMBOL "["; tacs = LIST0 SELF SEP SYMBOL "|"; SYMBOL "]"->
236           (GrafiteAst.Then (loc, tac, tacs))
237       ]
238     | "loops" RIGHTA
239       [ IDENT "do"; count = int; tac = SELF; IDENT "end" ->
240           GrafiteAst.Do (loc, count, tac)
241       | IDENT "repeat"; tac = SELF; IDENT "end" -> GrafiteAst.Repeat (loc, tac)
242       ]
243     | "simple" NONA
244       [ IDENT "first";
245         SYMBOL "["; tacs = LIST0 SELF SEP SYMBOL "|"; SYMBOL "]"->
246           GrafiteAst.First (loc, tacs)
247       | IDENT "try"; tac = SELF -> GrafiteAst.Try (loc, tac)
248       | IDENT "solve";
249         SYMBOL "["; tacs = LIST0 SELF SEP SYMBOL "|"; SYMBOL "]"->
250           GrafiteAst.Solve (loc, tacs)
251       | LPAREN; tac = SELF; RPAREN -> tac
252       | tac = tactic -> GrafiteAst.Tactic (loc, tac)
253       ]
254     ];
255   punctuation_tactical:
256     [
257       [ SYMBOL "[" -> GrafiteAst.Branch loc
258       | SYMBOL "|" -> GrafiteAst.Shift loc
259       | i = int; SYMBOL ":" -> GrafiteAst.Pos (loc, i)
260       | SYMBOL "]" -> GrafiteAst.Merge loc
261       | SYMBOL ";" -> GrafiteAst.Semicolon loc
262       | SYMBOL "." -> GrafiteAst.Dot loc
263       ]
264     ];
265   tactical:
266     [ "simple" NONA
267       [ IDENT "focus"; goals = LIST1 int -> GrafiteAst.Focus (loc, goals)
268       | IDENT "unfocus" -> GrafiteAst.Unfocus loc
269       | IDENT "skip" -> GrafiteAst.Skip loc
270       | tac = atomic_tactical LEVEL "loops" -> tac
271       ]
272     ];
273   theorem_flavour: [
274     [ [ IDENT "definition"  ] -> `Definition
275     | [ IDENT "fact"        ] -> `Fact
276     | [ IDENT "lemma"       ] -> `Lemma
277     | [ IDENT "remark"      ] -> `Remark
278     | [ IDENT "theorem"     ] -> `Theorem
279     ]
280   ];
281   inductive_spec: [ [
282     fst_name = IDENT; params = LIST0 [ arg=arg -> arg ];
283     SYMBOL ":"; fst_typ = term; SYMBOL <:unicode<def>>; OPT SYMBOL "|";
284     fst_constructors = LIST0 constructor SEP SYMBOL "|";
285     tl = OPT [ "with";
286       types = LIST1 [
287         name = IDENT; SYMBOL ":"; typ = term; SYMBOL <:unicode<def>>;
288        OPT SYMBOL "|"; constructors = LIST0 constructor SEP SYMBOL "|" ->
289           (name, true, typ, constructors) ] SEP "with" -> types
290     ] ->
291       let params =
292         List.fold_right
293           (fun (names, typ) acc ->
294             (List.map (fun name -> (name, typ)) names) @ acc)
295           params []
296       in
297       let fst_ind_type = (fst_name, true, fst_typ, fst_constructors) in
298       let tl_ind_types = match tl with None -> [] | Some types -> types in
299       let ind_types = fst_ind_type :: tl_ind_types in
300       (params, ind_types)
301   ] ];
302   
303   record_spec: [ [
304     name = IDENT; params = LIST0 [ arg = arg -> arg ] ;
305      SYMBOL ":"; typ = term; SYMBOL <:unicode<def>>; SYMBOL "{" ; 
306      fields = LIST0 [ 
307        name = IDENT ; SYMBOL ":" ; ty = term -> (name,ty) 
308      ] SEP SYMBOL ";"; SYMBOL "}" -> 
309       let params =
310         List.fold_right
311           (fun (names, typ) acc ->
312             (List.map (fun name -> (name, typ)) names) @ acc)
313           params []
314       in
315       (params,name,typ,fields)
316   ] ];
317   
318   macro: [
319     [ [ IDENT "quit"  ] -> GrafiteAst.Quit loc
320 (*     | [ IDENT "abort" ] -> GrafiteAst.Abort loc *)
321 (*     | [ IDENT "undo"   ]; steps = OPT NUMBER ->
322         GrafiteAst.Undo (loc, int_opt steps)
323     | [ IDENT "redo"   ]; steps = OPT NUMBER ->
324         GrafiteAst.Redo (loc, int_opt steps) *)
325     | [ IDENT "check"   ]; t = term ->
326         GrafiteAst.Check (loc, t)
327     | [ IDENT "hint" ] -> GrafiteAst.Hint loc
328     | [ IDENT "whelp"; "match" ] ; t = term -> 
329         GrafiteAst.WMatch (loc,t)
330     | [ IDENT "whelp"; IDENT "instance" ] ; t = term -> 
331         GrafiteAst.WInstance (loc,t)
332     | [ IDENT "whelp"; IDENT "locate" ] ; id = IDENT -> 
333         GrafiteAst.WLocate (loc,id)
334     | [ IDENT "whelp"; IDENT "elim" ] ; t = term ->
335         GrafiteAst.WElim (loc, t)
336     | [ IDENT "whelp"; IDENT "hint" ] ; t = term -> 
337         GrafiteAst.WHint (loc,t)
338     | [ IDENT "print" ]; name = QSTRING -> GrafiteAst.Print (loc, name)
339     ]
340   ];
341   alias_spec: [
342     [ IDENT "id"; id = QSTRING; SYMBOL "="; uri = QSTRING ->
343       let alpha = "[a-zA-Z]" in
344       let num = "[0-9]+" in
345       let ident_cont = "\\("^alpha^"\\|"^num^"\\|_\\|\\\\\\)" in
346       let ident = "\\("^alpha^ident_cont^"*\\|_"^ident_cont^"+\\)" in
347       let rex = Str.regexp ("^"^ident^"$") in
348       if Str.string_match rex id 0 then
349         if (try ignore (UriManager.uri_of_string uri); true
350             with UriManager.IllFormedUri _ -> false)
351         then
352           GrafiteAst.Ident_alias (id, uri)
353         else 
354           raise
355            (HExtlib.Localized (loc, CicNotationParser.Parse_error (sprintf "Not a valid uri: %s" uri)))
356       else
357         raise (HExtlib.Localized (loc, CicNotationParser.Parse_error (
358           sprintf "Not a valid identifier: %s" id)))
359     | IDENT "symbol"; symbol = QSTRING;
360       instance = OPT [ LPAREN; IDENT "instance"; n = int; RPAREN -> n ];
361       SYMBOL "="; dsc = QSTRING ->
362         let instance =
363           match instance with Some i -> i | None -> 0
364         in
365         GrafiteAst.Symbol_alias (symbol, instance, dsc)
366     | IDENT "num";
367       instance = OPT [ LPAREN; IDENT "instance"; n = int; RPAREN -> n ];
368       SYMBOL "="; dsc = QSTRING ->
369         let instance =
370           match instance with Some i -> i | None -> 0
371         in
372         GrafiteAst.Number_alias (instance, dsc)
373     ]
374   ];
375   argument: [
376     [ l = LIST0 [ SYMBOL <:unicode<eta>> (* η *); SYMBOL "." -> () ];
377       id = IDENT ->
378         Ast.IdentArg (List.length l, id)
379     ]
380   ];
381   associativity: [
382     [ IDENT "left";  IDENT "associative" -> Gramext.LeftA
383     | IDENT "right"; IDENT "associative" -> Gramext.RightA
384     | IDENT "non"; IDENT "associative" -> Gramext.NonA
385     ]
386   ];
387   precedence: [
388     [ "with"; IDENT "precedence"; n = NUMBER -> int_of_string n ]
389   ];
390   notation: [
391     [ dir = OPT direction; s = QSTRING;
392       assoc = OPT associativity; prec = OPT precedence;
393       IDENT "for";
394       p2 = 
395         [ blob = UNPARSED_AST ->
396             add_raw_attribute ~text:(sprintf "@{%s}" blob)
397               (CicNotationParser.parse_level2_ast
398                 (Ulexing.from_utf8_string blob))
399         | blob = UNPARSED_META ->
400             add_raw_attribute ~text:(sprintf "${%s}" blob)
401               (CicNotationParser.parse_level2_meta
402                 (Ulexing.from_utf8_string blob))
403         ] ->
404           let assoc =
405             match assoc with
406             | None -> default_associativity
407             | Some assoc -> assoc
408           in
409           let prec =
410             match prec with
411             | None -> default_precedence
412             | Some prec -> prec
413           in
414           let p1 =
415             add_raw_attribute ~text:s
416               (CicNotationParser.parse_level1_pattern
417                 (Ulexing.from_utf8_string s))
418           in
419           (dir, p1, assoc, prec, p2)
420     ]
421   ];
422   level3_term: [
423     [ u = URI -> Ast.UriPattern (UriManager.uri_of_string u)
424     | id = IDENT -> Ast.VarPattern id
425     | SYMBOL "_" -> Ast.ImplicitPattern
426     | LPAREN; terms = LIST1 SELF; RPAREN ->
427         (match terms with
428         | [] -> assert false
429         | [term] -> term
430         | terms -> Ast.ApplPattern terms)
431     ]
432   ];
433   interpretation: [
434     [ s = CSYMBOL; args = LIST0 argument; SYMBOL "="; t = level3_term ->
435         (s, args, t)
436     ]
437   ];
438   command: [ [
439       IDENT "set"; n = QSTRING; v = QSTRING ->
440         GrafiteAst.Set (loc, n, v)
441     | IDENT "drop" -> GrafiteAst.Drop loc
442     | IDENT "qed" -> GrafiteAst.Qed loc
443     | IDENT "variant" ; name = IDENT; SYMBOL ":"; 
444       typ = term; SYMBOL <:unicode<def>> ; newname = IDENT ->
445         GrafiteAst.Obj (loc, 
446           GrafiteAst.Theorem 
447             (`Variant,name,typ,Some (Ast.Ident (newname, None))))
448     | flavour = theorem_flavour; name = IDENT; SYMBOL ":"; typ = term;
449       body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
450         GrafiteAst.Obj (loc,GrafiteAst.Theorem (flavour, name, typ, body))
451     | flavour = theorem_flavour; name = IDENT; SYMBOL <:unicode<def>> (* ≝ *);
452       body = term ->
453         GrafiteAst.Obj (loc,
454           GrafiteAst.Theorem (flavour, name, Ast.Implicit, Some body))
455     | "let"; ind_kind = [ "corec" -> `CoInductive | "rec"-> `Inductive ];
456         defs = CicNotationParser.let_defs -> 
457           let name,ty = 
458             match defs with
459             | ((Ast.Ident (name, None), Some ty),_,_) :: _ -> name,ty
460             | ((Ast.Ident (name, None), None),_,_) :: _ ->
461                 name, Ast.Implicit
462             | _ -> assert false 
463           in
464           let body = Ast.Ident (name,None) in
465           GrafiteAst.Obj (loc,GrafiteAst.Theorem(`Definition, name, ty,
466             Some (Ast.LetRec (ind_kind, defs, body))))
467     | IDENT "inductive"; spec = inductive_spec ->
468         let (params, ind_types) = spec in
469         GrafiteAst.Obj (loc,GrafiteAst.Inductive (params, ind_types))
470     | IDENT "coinductive"; spec = inductive_spec ->
471         let (params, ind_types) = spec in
472         let ind_types = (* set inductive flags to false (coinductive) *)
473           List.map (fun (name, _, term, ctors) -> (name, false, term, ctors))
474             ind_types
475         in
476         GrafiteAst.Obj (loc,GrafiteAst.Inductive (params, ind_types))
477     | IDENT "coercion" ; name = IDENT -> 
478         GrafiteAst.Coercion (loc, Ast.Ident (name,Some []))
479     | IDENT "coercion" ; name = URI -> 
480         GrafiteAst.Coercion (loc, Ast.Uri (name,Some []))
481     | IDENT "alias" ; spec = alias_spec ->
482         GrafiteAst.Alias (loc, spec)
483     | IDENT "record" ; (params,name,ty,fields) = record_spec ->
484         GrafiteAst.Obj (loc,GrafiteAst.Record (params,name,ty,fields))
485     | IDENT "include" ; path = QSTRING ->
486         GrafiteAst.Include (loc,path)
487     | IDENT "default" ; what = QSTRING ; uris = LIST1 URI ->
488        let uris = List.map UriManager.uri_of_string uris in
489         GrafiteAst.Default (loc,what,uris)
490     | IDENT "notation"; (dir, l1, assoc, prec, l2) = notation ->
491         GrafiteAst.Notation (loc, dir, l1, assoc, prec, l2)
492     | IDENT "interpretation"; id = QSTRING;
493       (symbol, args, l3) = interpretation ->
494         GrafiteAst.Interpretation (loc, id, (symbol, args), l3)
495     | IDENT "metadata"; [ IDENT "dependency" | IDENT "baseuri" ] ; URI ->
496         (** metadata commands lives only in .moo, where they are in marshalled
497          * form *)
498         raise (HExtlib.Localized (loc,CicNotationParser.Parse_error "metadata not allowed here"))
499
500     | IDENT "dump" -> GrafiteAst.Dump loc
501     | IDENT "render"; u = URI ->
502         GrafiteAst.Render (loc, UriManager.uri_of_string u)
503   ]];
504   executable: [
505     [ cmd = command; SYMBOL "." -> GrafiteAst.Command (loc, cmd)
506     | tac = tactical; punct = punctuation_tactical ->
507         GrafiteAst.Tactical (loc, tac, Some punct)
508     | punct = punctuation_tactical -> GrafiteAst.Tactical (loc, punct, None)
509     | mac = macro; SYMBOL "." -> GrafiteAst.Macro (loc, mac)
510     ]
511   ];
512   comment: [
513     [ BEGINCOMMENT ; ex = executable ; ENDCOMMENT -> 
514        GrafiteAst.Code (loc, ex)
515     | str = NOTE -> 
516        GrafiteAst.Note (loc, str)
517     ]
518   ];
519   statement: [
520     [ ex = executable -> GrafiteAst.Executable (loc,ex)
521     | com = comment -> GrafiteAst.Comment (loc, com)
522     | EOI -> raise End_of_file
523     ]
524   ];
525 END
526
527 let exc_located_wrapper f =
528   try
529     f ()
530   with
531   | Stdpp.Exc_located (_, End_of_file) -> raise End_of_file
532   | Stdpp.Exc_located (floc, Stream.Error msg) ->
533       raise (HExtlib.Localized (floc,CicNotationParser.Parse_error msg))
534   | Stdpp.Exc_located (floc, exn) ->
535       raise
536        (HExtlib.Localized (floc,CicNotationParser.Parse_error (Printexc.to_string exn)))
537
538 let parse_statement lexbuf =
539   exc_located_wrapper
540     (fun () -> (Grammar.Entry.parse statement (Obj.magic lexbuf)))
541
542 let parse_dependencies lexbuf = 
543   let tok_stream,_ =
544     CicNotationLexer.level2_ast_lexer.Token.tok_func (Obj.magic lexbuf)
545   in
546   let rec parse acc = 
547     (parser
548     | [< '("URI", u) >] ->
549         parse (GrafiteAst.UriDep (UriManager.uri_of_string u) :: acc)
550     | [< '("IDENT", "include"); '("QSTRING", fname) >] ->
551         parse (GrafiteAst.IncludeDep fname :: acc)
552     | [< '("IDENT", "set"); '("QSTRING", "baseuri"); '("QSTRING", baseuri) >] ->
553         parse (GrafiteAst.BaseuriDep baseuri :: acc)
554     | [< '("EOI", _) >] -> acc
555     | [< 'tok >] -> parse acc
556     | [<  >] -> acc) tok_stream
557   in
558   List.rev (parse [])
559