]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_notation/grafiteParser.ml
Syntax change:
[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 (CicNotationParser.Parse_error
165               (loc, "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           (CicNotationParser.Parse_error
207             (loc,"the pattern cannot specify the term to rewrite, only its paths in the hypotheses and in the conclusion"))
208         else
209          GrafiteAst.Rewrite (loc, d, t, p)
210     | IDENT "right" ->
211         GrafiteAst.Right loc
212     | IDENT "ring" ->
213         GrafiteAst.Ring loc
214     | IDENT "split" ->
215         GrafiteAst.Split loc
216     | IDENT "symmetry" ->
217         GrafiteAst.Symmetry loc
218     | IDENT "transitivity"; t = tactic_term ->
219         GrafiteAst.Transitivity (loc, t)
220     ]
221   ];
222   atomic_tactical:
223     [ "sequence" LEFTA
224       [ t1 = SELF; SYMBOL ";"; t2 = SELF ->
225           let ts =
226             match t1 with
227             | GrafiteAst.Seq (_, l) -> l @ [ t2 ]
228             | _ -> [ t1; t2 ]
229           in
230           GrafiteAst.Seq (loc, ts)
231       ]
232     | "then" NONA
233       [ tac = SELF; SYMBOL ";";
234         SYMBOL "["; tacs = LIST0 SELF SEP SYMBOL "|"; SYMBOL "]"->
235           (GrafiteAst.Then (loc, tac, tacs))
236       ]
237     | "loops" RIGHTA
238       [ IDENT "do"; count = int; tac = SELF; IDENT "end" ->
239           GrafiteAst.Do (loc, count, tac)
240       | IDENT "repeat"; tac = SELF; IDENT "end" -> GrafiteAst.Repeat (loc, tac)
241       ]
242     | "simple" NONA
243       [ IDENT "first";
244         SYMBOL "["; tacs = LIST0 SELF SEP SYMBOL "|"; SYMBOL "]"->
245           GrafiteAst.First (loc, tacs)
246       | IDENT "try"; tac = SELF -> GrafiteAst.Try (loc, tac)
247       | IDENT "solve";
248         SYMBOL "["; tacs = LIST0 SELF SEP SYMBOL "|"; SYMBOL "]"->
249           GrafiteAst.Solve (loc, tacs)
250       | LPAREN; tac = SELF; RPAREN -> tac
251       | tac = tactic -> GrafiteAst.Tactic (loc, tac)
252       ]
253     ];
254   punctuation_tactical:
255     [
256       [ SYMBOL "[" -> GrafiteAst.Branch loc
257       | SYMBOL "|" -> GrafiteAst.Shift loc
258       | i = int; SYMBOL ":" -> GrafiteAst.Pos (loc, i)
259       | SYMBOL "]" -> GrafiteAst.Merge loc
260       | SYMBOL ";" -> GrafiteAst.Semicolon loc
261       | SYMBOL "." -> GrafiteAst.Dot loc
262       ]
263     ];
264   tactical:
265     [ "simple" NONA
266       [ IDENT "focus"; goals = LIST1 int -> GrafiteAst.Focus (loc, goals)
267       | IDENT "unfocus" -> GrafiteAst.Unfocus loc
268       | IDENT "skip" -> GrafiteAst.Skip loc
269       | tac = atomic_tactical LEVEL "loops" -> tac
270       ]
271     ];
272   theorem_flavour: [
273     [ [ IDENT "definition"  ] -> `Definition
274     | [ IDENT "fact"        ] -> `Fact
275     | [ IDENT "lemma"       ] -> `Lemma
276     | [ IDENT "remark"      ] -> `Remark
277     | [ IDENT "theorem"     ] -> `Theorem
278     ]
279   ];
280   inductive_spec: [ [
281     fst_name = IDENT; params = LIST0 [ arg=arg -> arg ];
282     SYMBOL ":"; fst_typ = term; SYMBOL <:unicode<def>>; OPT SYMBOL "|";
283     fst_constructors = LIST0 constructor SEP SYMBOL "|";
284     tl = OPT [ "with";
285       types = LIST1 [
286         name = IDENT; SYMBOL ":"; typ = term; SYMBOL <:unicode<def>>;
287        OPT SYMBOL "|"; constructors = LIST0 constructor SEP SYMBOL "|" ->
288           (name, true, typ, constructors) ] SEP "with" -> types
289     ] ->
290       let params =
291         List.fold_right
292           (fun (names, typ) acc ->
293             (List.map (fun name -> (name, typ)) names) @ acc)
294           params []
295       in
296       let fst_ind_type = (fst_name, true, fst_typ, fst_constructors) in
297       let tl_ind_types = match tl with None -> [] | Some types -> types in
298       let ind_types = fst_ind_type :: tl_ind_types in
299       (params, ind_types)
300   ] ];
301   
302   record_spec: [ [
303     name = IDENT; params = LIST0 [ arg = arg -> arg ] ;
304      SYMBOL ":"; typ = term; SYMBOL <:unicode<def>>; SYMBOL "{" ; 
305      fields = LIST0 [ 
306        name = IDENT ; SYMBOL ":" ; ty = term -> (name,ty) 
307      ] SEP SYMBOL ";"; SYMBOL "}" -> 
308       let params =
309         List.fold_right
310           (fun (names, typ) acc ->
311             (List.map (fun name -> (name, typ)) names) @ acc)
312           params []
313       in
314       (params,name,typ,fields)
315   ] ];
316   
317   macro: [
318     [ [ IDENT "quit"  ] -> GrafiteAst.Quit loc
319 (*     | [ IDENT "abort" ] -> GrafiteAst.Abort loc *)
320     | [ IDENT "print" ]; name = QSTRING -> GrafiteAst.Print (loc, name)
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 (CicNotationParser.Parse_error (loc,sprintf "Not a valid uri: %s" uri))
355       else
356         raise (CicNotationParser.Parse_error (loc,
357           sprintf "Not a valid identifier: %s" id))
358     | IDENT "symbol"; symbol = QSTRING;
359       instance = OPT [ LPAREN; IDENT "instance"; n = int; RPAREN -> n ];
360       SYMBOL "="; dsc = QSTRING ->
361         let instance =
362           match instance with Some i -> i | None -> 0
363         in
364         GrafiteAst.Symbol_alias (symbol, instance, dsc)
365     | IDENT "num";
366       instance = OPT [ LPAREN; IDENT "instance"; n = int; RPAREN -> n ];
367       SYMBOL "="; dsc = QSTRING ->
368         let instance =
369           match instance with Some i -> i | None -> 0
370         in
371         GrafiteAst.Number_alias (instance, dsc)
372     ]
373   ];
374   argument: [
375     [ l = LIST0 [ SYMBOL <:unicode<eta>> (* η *); SYMBOL "." -> () ];
376       id = IDENT ->
377         Ast.IdentArg (List.length l, id)
378     ]
379   ];
380   associativity: [
381     [ IDENT "left";  IDENT "associative" -> Gramext.LeftA
382     | IDENT "right"; IDENT "associative" -> Gramext.RightA
383     | IDENT "non"; IDENT "associative" -> Gramext.NonA
384     ]
385   ];
386   precedence: [
387     [ "with"; IDENT "precedence"; n = NUMBER -> int_of_string n ]
388   ];
389   notation: [
390     [ dir = OPT direction; s = QSTRING;
391       assoc = OPT associativity; prec = OPT precedence;
392       IDENT "for";
393       p2 = 
394         [ blob = UNPARSED_AST ->
395             add_raw_attribute ~text:(sprintf "@{%s}" blob)
396               (CicNotationParser.parse_level2_ast
397                 (Ulexing.from_utf8_string blob))
398         | blob = UNPARSED_META ->
399             add_raw_attribute ~text:(sprintf "${%s}" blob)
400               (CicNotationParser.parse_level2_meta
401                 (Ulexing.from_utf8_string blob))
402         ] ->
403           let assoc =
404             match assoc with
405             | None -> default_associativity
406             | Some assoc -> assoc
407           in
408           let prec =
409             match prec with
410             | None -> default_precedence
411             | Some prec -> prec
412           in
413           let p1 =
414             add_raw_attribute ~text:s
415               (CicNotationParser.parse_level1_pattern
416                 (Ulexing.from_utf8_string s))
417           in
418           (dir, p1, assoc, prec, p2)
419     ]
420   ];
421   level3_term: [
422     [ u = URI -> Ast.UriPattern (UriManager.uri_of_string u)
423     | id = IDENT -> Ast.VarPattern id
424     | SYMBOL "_" -> Ast.ImplicitPattern
425     | LPAREN; terms = LIST1 SELF; RPAREN ->
426         (match terms with
427         | [] -> assert false
428         | [term] -> term
429         | terms -> Ast.ApplPattern terms)
430     ]
431   ];
432   interpretation: [
433     [ s = CSYMBOL; args = LIST0 argument; SYMBOL "="; t = level3_term ->
434         (s, args, t)
435     ]
436   ];
437   command: [ [
438       IDENT "set"; n = QSTRING; v = QSTRING ->
439         GrafiteAst.Set (loc, n, v)
440     | IDENT "drop" -> GrafiteAst.Drop loc
441     | IDENT "qed" -> GrafiteAst.Qed loc
442     | IDENT "variant" ; name = IDENT; SYMBOL ":"; 
443       typ = term; SYMBOL <:unicode<def>> ; newname = IDENT ->
444         GrafiteAst.Obj (loc, 
445           GrafiteAst.Theorem 
446             (`Variant,name,typ,Some (Ast.Ident (newname, None))))
447     | flavour = theorem_flavour; name = IDENT; SYMBOL ":"; typ = term;
448       body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
449         GrafiteAst.Obj (loc,GrafiteAst.Theorem (flavour, name, typ, body))
450     | flavour = theorem_flavour; name = IDENT;
451       body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
452         GrafiteAst.Obj (loc,
453           GrafiteAst.Theorem (flavour, name, Ast.Implicit, body))
454     | "let"; ind_kind = [ "corec" -> `CoInductive | "rec"-> `Inductive ];
455         defs = CicNotationParser.let_defs -> 
456           let name,ty = 
457             match defs with
458             | ((Ast.Ident (name, None), Some ty),_,_) :: _ -> name,ty
459             | ((Ast.Ident (name, None), None),_,_) :: _ ->
460                 name, Ast.Implicit
461             | _ -> assert false 
462           in
463           let body = Ast.Ident (name,None) in
464           GrafiteAst.Obj (loc,GrafiteAst.Theorem(`Definition, name, ty,
465             Some (Ast.LetRec (ind_kind, defs, body))))
466     | [ IDENT "inductive" ]; spec = inductive_spec ->
467         let (params, ind_types) = spec in
468         GrafiteAst.Obj (loc,GrafiteAst.Inductive (params, ind_types))
469     | [ IDENT "coinductive" ]; spec = inductive_spec ->
470         let (params, ind_types) = spec in
471         let ind_types = (* set inductive flags to false (coinductive) *)
472           List.map (fun (name, _, term, ctors) -> (name, false, term, ctors))
473             ind_types
474         in
475         GrafiteAst.Obj (loc,GrafiteAst.Inductive (params, ind_types))
476     | IDENT "coercion" ; name = IDENT -> 
477         GrafiteAst.Coercion (loc, Ast.Ident (name,Some []))
478     | IDENT "coercion" ; name = URI -> 
479         GrafiteAst.Coercion (loc, Ast.Uri (name,Some []))
480     | IDENT "alias" ; spec = alias_spec ->
481         GrafiteAst.Alias (loc, spec)
482     | IDENT "record" ; (params,name,ty,fields) = record_spec ->
483         GrafiteAst.Obj (loc,GrafiteAst.Record (params,name,ty,fields))
484     | IDENT "include" ; path = QSTRING ->
485         GrafiteAst.Include (loc,path)
486     | IDENT "default" ; what = QSTRING ; uris = LIST1 URI ->
487        let uris = List.map UriManager.uri_of_string uris in
488         GrafiteAst.Default (loc,what,uris)
489     | IDENT "notation"; (dir, l1, assoc, prec, l2) = notation ->
490         GrafiteAst.Notation (loc, dir, l1, assoc, prec, l2)
491     | IDENT "interpretation"; id = QSTRING;
492       (symbol, args, l3) = interpretation ->
493         GrafiteAst.Interpretation (loc, id, (symbol, args), l3)
494     | IDENT "metadata"; [IDENT "dependency" | IDENT "baseuri" ] ; URI ->
495         (** metadata commands lives only in .moo, where they are in marshalled
496          * form *)
497         raise (CicNotationParser.Parse_error (loc, "metadata not allowed here"))
498
499     | IDENT "dump" -> GrafiteAst.Dump loc
500     | IDENT "render"; u = URI ->
501         GrafiteAst.Render (loc, UriManager.uri_of_string u)
502   ]];
503   executable: [
504     [ cmd = command; SYMBOL "." -> GrafiteAst.Command (loc, cmd)
505     | tac = tactical; punct = punctuation_tactical ->
506         GrafiteAst.Tactical (loc, tac, Some punct)
507     | punct = punctuation_tactical -> GrafiteAst.Tactical (loc, punct, None)
508     | mac = macro; SYMBOL "." -> GrafiteAst.Macro (loc, mac)
509     ]
510   ];
511   comment: [
512     [ BEGINCOMMENT ; ex = executable ; ENDCOMMENT -> 
513        GrafiteAst.Code (loc, ex)
514     | str = NOTE -> 
515        GrafiteAst.Note (loc, str)
516     ]
517   ];
518   statement: [
519     [ ex = executable -> GrafiteAst.Executable (loc,ex)
520     | com = comment -> GrafiteAst.Comment (loc, com)
521     | EOI -> raise End_of_file
522     ]
523   ];
524 END
525
526 let exc_located_wrapper f =
527   try
528     f ()
529   with
530   | Stdpp.Exc_located (_, End_of_file) -> raise End_of_file
531   | Stdpp.Exc_located (floc, Stream.Error msg) ->
532       raise (CicNotationParser.Parse_error (floc, msg))
533   | Stdpp.Exc_located (floc, exn) ->
534       raise (CicNotationParser.Parse_error (floc, (Printexc.to_string exn)))
535
536 let parse_statement lexbuf =
537   exc_located_wrapper
538     (fun () -> (Grammar.Entry.parse statement (Obj.magic lexbuf)))
539
540 let parse_dependencies lexbuf = 
541   let tok_stream,_ =
542     CicNotationLexer.level2_ast_lexer.Token.tok_func (Obj.magic lexbuf)
543   in
544   let rec parse acc = 
545     (parser
546     | [< '("URI", u) >] ->
547         parse (GrafiteAst.UriDep (UriManager.uri_of_string u) :: acc)
548     | [< '("IDENT", "include"); '("QSTRING", fname) >] ->
549         parse (GrafiteAst.IncludeDep fname :: acc)
550     | [< '("IDENT", "set"); '("QSTRING", "baseuri"); '("QSTRING", baseuri) >] ->
551         parse (GrafiteAst.BaseuriDep baseuri :: acc)
552     | [< '("EOI", _) >] -> acc
553     | [< 'tok >] -> parse acc
554     | [<  >] -> acc) tok_stream
555   in
556   List.rev (parse [])
557