]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_notation/grafiteParser.ml
new tacticals
[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 = term -> path ] ->
71          (id,match path with Some p -> p | None -> Ast.UserInput) ]
72        SEP SYMBOL ";";
73      goal_path = OPT [ SYMBOL <:unicode<vdash>>; term = term -> term ] ->
74       let goal_path =
75        match goal_path, hyp_paths with
76           None, [] -> Ast.UserInput
77         | None, _::_ -> Ast.Implicit
78         | Some goal_path, _ -> goal_path
79       in
80        hyp_paths,goal_path
81    ]
82   ];
83   pattern_spec: [
84     [ res = OPT [
85        "in";
86        wanted_and_sps =
87         [ "match" ; wanted = term ;
88           sps = OPT [ "in"; sps = sequent_pattern_spec -> sps ] ->
89            Some wanted,sps
90         | sps = sequent_pattern_spec ->
91            None,Some sps
92         ] ->
93          let wanted,hyp_paths,goal_path =
94           match wanted_and_sps with
95              wanted,None -> wanted, [], Ast.UserInput
96            | wanted,Some (hyp_paths,goal_path) -> wanted,hyp_paths,goal_path
97          in
98           wanted, hyp_paths, goal_path ] ->
99       match res with
100          None -> None,[],Ast.UserInput
101        | Some ps -> ps]
102   ];
103   direction: [
104     [ SYMBOL ">" -> `LeftToRight
105     | SYMBOL "<" -> `RightToLeft ]
106   ];
107   int: [ [ num = NUMBER -> int_of_string num ] ];
108   intros_spec: [
109     [ num = OPT [ num = int -> num ]; idents = OPT ident_list0 ->
110         let idents = match idents with None -> [] | Some idents -> idents in
111         num, idents
112     ]
113   ];
114   using: [ [ using = OPT [ IDENT "using"; t = tactic_term -> t ] -> using ] ];
115   tactic: [
116     [ IDENT "absurd"; t = tactic_term ->
117         GrafiteAst.Absurd (loc, t)
118     | IDENT "apply"; t = tactic_term ->
119         GrafiteAst.Apply (loc, t)
120     | IDENT "assumption" ->
121         GrafiteAst.Assumption loc
122     | IDENT "auto";
123       depth = OPT [ IDENT "depth"; SYMBOL "="; i = int -> i ];
124       width = OPT [ IDENT "width"; SYMBOL "="; i = int -> i ];
125       paramodulation = OPT [ IDENT "paramodulation" ];
126       full = OPT [ IDENT "full" ] ->  (* ALB *)
127           GrafiteAst.Auto (loc,depth,width,paramodulation,full)
128     | IDENT "clear"; id = IDENT ->
129         GrafiteAst.Clear (loc,id)
130     | IDENT "clearbody"; id = IDENT ->
131         GrafiteAst.ClearBody (loc,id)
132     | IDENT "change"; what = pattern_spec; "with"; t = tactic_term ->
133         GrafiteAst.Change (loc, what, t)
134     | IDENT "compare"; t = tactic_term ->
135         GrafiteAst.Compare (loc,t)
136     | IDENT "constructor"; n = int ->
137         GrafiteAst.Constructor (loc, n)
138     | IDENT "contradiction" ->
139         GrafiteAst.Contradiction loc
140     | IDENT "cut"; t = tactic_term; ident = OPT [ "as"; id = IDENT -> id] ->
141         GrafiteAst.Cut (loc, ident, t)
142     | IDENT "decide"; IDENT "equality" ->
143         GrafiteAst.DecideEquality loc
144     | IDENT "decompose"; types = OPT ident_list0; what = IDENT;
145       (num, idents) = intros_spec ->
146         let types = match types with None -> [] | Some types -> types in
147         let to_spec id = GrafiteAst.Ident id in
148         GrafiteAst.Decompose (loc, List.rev_map to_spec types, what, idents)
149     | IDENT "discriminate"; t = tactic_term ->
150         GrafiteAst.Discriminate (loc, t)
151     | IDENT "elim"; what = tactic_term; using = using;
152       (num, idents) = intros_spec ->
153         GrafiteAst.Elim (loc, what, using, num, idents)
154     | IDENT "elimType"; what = tactic_term; using = using;
155       (num, idents) = intros_spec ->
156         GrafiteAst.ElimType (loc, what, using, num, idents)
157     | IDENT "exact"; t = tactic_term ->
158         GrafiteAst.Exact (loc, t)
159     | IDENT "exists" ->
160         GrafiteAst.Exists loc
161     | IDENT "fail" -> GrafiteAst.Fail loc
162     | IDENT "fold"; kind = reduction_kind; t = tactic_term; p = pattern_spec ->
163         let (pt,_,_) = p in
164           if pt <> None then
165             raise (CicNotationParser.Parse_error
166               (loc, "the pattern cannot specify the term to replace, only its"
167               ^ " paths in the hypotheses and in the conclusion"))
168         else
169          GrafiteAst.Fold (loc, kind, t, p)
170     | IDENT "fourier" ->
171         GrafiteAst.Fourier loc
172     | IDENT "fwd"; hyp = IDENT; idents = OPT ident_list0 ->
173         let idents = match idents with None -> [] | Some idents -> idents in
174         GrafiteAst.FwdSimpl (loc, hyp, idents)
175     | IDENT "generalize"; p=pattern_spec; id = OPT ["as" ; id = IDENT -> id] ->
176        GrafiteAst.Generalize (loc,p,id)
177     | IDENT "goal"; n = int ->
178         GrafiteAst.Goal (loc, n)
179     | IDENT "id" -> GrafiteAst.IdTac loc
180     | IDENT "injection"; t = tactic_term ->
181         GrafiteAst.Injection (loc, t)
182     | IDENT "intro"; ident = OPT IDENT ->
183         let idents = match ident with None -> [] | Some id -> [id] in
184         GrafiteAst.Intros (loc, Some 1, idents)
185     | IDENT "intros"; (num, idents) = intros_spec ->
186         GrafiteAst.Intros (loc, num, idents)
187     | IDENT "lapply"; 
188       depth = OPT [ IDENT "depth"; SYMBOL "="; i = int -> i ];
189       what = tactic_term; 
190       to_what = OPT [ "to" ; t = tactic_term_list1 -> t ];
191       ident = OPT [ IDENT "using" ; ident = IDENT -> ident ] ->
192         let to_what = match to_what with None -> [] | Some to_what -> to_what in
193         GrafiteAst.LApply (loc, depth, to_what, what, ident)
194     | IDENT "left" -> GrafiteAst.Left loc
195     | IDENT "letin"; where = IDENT ; SYMBOL <:unicode<def>> ; t = tactic_term ->
196         GrafiteAst.LetIn (loc, t, where)
197     | kind = reduction_kind; p = pattern_spec ->
198         GrafiteAst.Reduce (loc, kind, p)
199     | IDENT "reflexivity" ->
200         GrafiteAst.Reflexivity loc
201     | IDENT "replace"; p = pattern_spec; "with"; t = tactic_term ->
202         GrafiteAst.Replace (loc, p, t)
203     | IDENT "rewrite" ; d = direction; t = tactic_term ; p = pattern_spec ->
204        let (pt,_,_) = p in
205         if pt <> None then
206          raise
207           (CicNotationParser.Parse_error
208             (loc,"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 "print" ]; name = QSTRING -> GrafiteAst.Print (loc, name)
322 (*     | [ IDENT "undo"   ]; steps = OPT NUMBER ->
323         GrafiteAst.Undo (loc, int_opt steps)
324     | [ IDENT "redo"   ]; steps = OPT NUMBER ->
325         GrafiteAst.Redo (loc, int_opt steps) *)
326     | [ IDENT "check"   ]; t = term ->
327         GrafiteAst.Check (loc, t)
328     | [ IDENT "hint" ] -> GrafiteAst.Hint loc
329     | [ IDENT "whelp"; "match" ] ; t = term -> 
330         GrafiteAst.WMatch (loc,t)
331     | [ IDENT "whelp"; IDENT "instance" ] ; t = term -> 
332         GrafiteAst.WInstance (loc,t)
333     | [ IDENT "whelp"; IDENT "locate" ] ; id = IDENT -> 
334         GrafiteAst.WLocate (loc,id)
335     | [ IDENT "whelp"; IDENT "elim" ] ; t = term ->
336         GrafiteAst.WElim (loc, t)
337     | [ IDENT "whelp"; IDENT "hint" ] ; t = term -> 
338         GrafiteAst.WHint (loc,t)
339     | [ IDENT "print" ]; name = QSTRING -> GrafiteAst.Print (loc, name)
340     ]
341   ];
342   alias_spec: [
343     [ IDENT "id"; id = QSTRING; SYMBOL "="; uri = QSTRING ->
344       let alpha = "[a-zA-Z]" in
345       let num = "[0-9]+" in
346       let ident_cont = "\\("^alpha^"\\|"^num^"\\|_\\|\\\\\\)" in
347       let ident = "\\("^alpha^ident_cont^"*\\|_"^ident_cont^"+\\)" in
348       let rex = Str.regexp ("^"^ident^"$") in
349       if Str.string_match rex id 0 then
350         if (try ignore (UriManager.uri_of_string uri); true
351             with UriManager.IllFormedUri _ -> false)
352         then
353           GrafiteAst.Ident_alias (id, uri)
354         else 
355           raise (CicNotationParser.Parse_error (loc,sprintf "Not a valid uri: %s" uri))
356       else
357         raise (CicNotationParser.Parse_error (loc,
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;
452       body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
453         GrafiteAst.Obj (loc,
454           GrafiteAst.Theorem (flavour, name, Ast.Implicit, 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 (CicNotationParser.Parse_error (loc, "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 (CicNotationParser.Parse_error (floc, msg))
534   | Stdpp.Exc_located (floc, exn) ->
535       raise (CicNotationParser.Parse_error (floc, (Printexc.to_string exn)))
536
537 let parse_statement lexbuf =
538   exc_located_wrapper
539     (fun () -> (Grammar.Entry.parse statement (Obj.magic lexbuf)))
540
541 let parse_dependencies lexbuf = 
542   let tok_stream,_ =
543     CicNotationLexer.level2_ast_lexer.Token.tok_func (Obj.magic lexbuf)
544   in
545   let rec parse acc = 
546     (parser
547     | [< '("URI", u) >] ->
548         parse (GrafiteAst.UriDep (UriManager.uri_of_string u) :: acc)
549     | [< '("IDENT", "include"); '("QSTRING", fname) >] ->
550         parse (GrafiteAst.IncludeDep fname :: acc)
551     | [< '("IDENT", "set"); '("QSTRING", "baseuri"); '("QSTRING", baseuri) >] ->
552         parse (GrafiteAst.BaseuriDep baseuri :: acc)
553     | [< '("EOI", _) >] -> acc
554     | [< 'tok >] -> parse acc
555     | [<  >] -> acc) tok_stream
556   in
557   List.rev (parse [])
558