]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_notation/grafiteParser.ml
ocaml 3.09 transition
[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 "undo"   ]; steps = OPT NUMBER ->
321         GrafiteAst.Undo (loc, int_opt steps)
322     | [ IDENT "redo"   ]; steps = OPT NUMBER ->
323         GrafiteAst.Redo (loc, int_opt steps) *)
324     | [ IDENT "check"   ]; t = term ->
325         GrafiteAst.Check (loc, t)
326     | [ IDENT "hint" ] -> GrafiteAst.Hint loc
327     | [ IDENT "whelp"; "match" ] ; t = term -> 
328         GrafiteAst.WMatch (loc,t)
329     | [ IDENT "whelp"; IDENT "instance" ] ; t = term -> 
330         GrafiteAst.WInstance (loc,t)
331     | [ IDENT "whelp"; IDENT "locate" ] ; id = IDENT -> 
332         GrafiteAst.WLocate (loc,id)
333     | [ IDENT "whelp"; IDENT "elim" ] ; t = term ->
334         GrafiteAst.WElim (loc, t)
335     | [ IDENT "whelp"; IDENT "hint" ] ; t = term -> 
336         GrafiteAst.WHint (loc,t)
337     | [ IDENT "print" ]; name = QSTRING -> GrafiteAst.Print (loc, name)
338     ]
339   ];
340   alias_spec: [
341     [ IDENT "id"; id = QSTRING; SYMBOL "="; uri = QSTRING ->
342       let alpha = "[a-zA-Z]" in
343       let num = "[0-9]+" in
344       let ident_cont = "\\("^alpha^"\\|"^num^"\\|_\\|\\\\\\)" in
345       let ident = "\\("^alpha^ident_cont^"*\\|_"^ident_cont^"+\\)" in
346       let rex = Str.regexp ("^"^ident^"$") in
347       if Str.string_match rex id 0 then
348         if (try ignore (UriManager.uri_of_string uri); true
349             with UriManager.IllFormedUri _ -> false)
350         then
351           GrafiteAst.Ident_alias (id, uri)
352         else 
353           raise (CicNotationParser.Parse_error (loc,sprintf "Not a valid uri: %s" uri))
354       else
355         raise (CicNotationParser.Parse_error (loc,
356           sprintf "Not a valid identifier: %s" id))
357     | IDENT "symbol"; symbol = QSTRING;
358       instance = OPT [ LPAREN; IDENT "instance"; n = int; RPAREN -> n ];
359       SYMBOL "="; dsc = QSTRING ->
360         let instance =
361           match instance with Some i -> i | None -> 0
362         in
363         GrafiteAst.Symbol_alias (symbol, instance, dsc)
364     | IDENT "num";
365       instance = OPT [ LPAREN; IDENT "instance"; n = int; RPAREN -> n ];
366       SYMBOL "="; dsc = QSTRING ->
367         let instance =
368           match instance with Some i -> i | None -> 0
369         in
370         GrafiteAst.Number_alias (instance, dsc)
371     ]
372   ];
373   argument: [
374     [ l = LIST0 [ SYMBOL <:unicode<eta>> (* η *); SYMBOL "." -> () ];
375       id = IDENT ->
376         Ast.IdentArg (List.length l, id)
377     ]
378   ];
379   associativity: [
380     [ IDENT "left";  IDENT "associative" -> Gramext.LeftA
381     | IDENT "right"; IDENT "associative" -> Gramext.RightA
382     | IDENT "non"; IDENT "associative" -> Gramext.NonA
383     ]
384   ];
385   precedence: [
386     [ "with"; IDENT "precedence"; n = NUMBER -> int_of_string n ]
387   ];
388   notation: [
389     [ dir = OPT direction; s = QSTRING;
390       assoc = OPT associativity; prec = OPT precedence;
391       IDENT "for";
392       p2 = 
393         [ blob = UNPARSED_AST ->
394             add_raw_attribute ~text:(sprintf "@{%s}" blob)
395               (CicNotationParser.parse_level2_ast
396                 (Ulexing.from_utf8_string blob))
397         | blob = UNPARSED_META ->
398             add_raw_attribute ~text:(sprintf "${%s}" blob)
399               (CicNotationParser.parse_level2_meta
400                 (Ulexing.from_utf8_string blob))
401         ] ->
402           let assoc =
403             match assoc with
404             | None -> default_associativity
405             | Some assoc -> assoc
406           in
407           let prec =
408             match prec with
409             | None -> default_precedence
410             | Some prec -> prec
411           in
412           let p1 =
413             add_raw_attribute ~text:s
414               (CicNotationParser.parse_level1_pattern
415                 (Ulexing.from_utf8_string s))
416           in
417           (dir, p1, assoc, prec, p2)
418     ]
419   ];
420   level3_term: [
421     [ u = URI -> Ast.UriPattern (UriManager.uri_of_string u)
422     | id = IDENT -> Ast.VarPattern id
423     | SYMBOL "_" -> Ast.ImplicitPattern
424     | LPAREN; terms = LIST1 SELF; RPAREN ->
425         (match terms with
426         | [] -> assert false
427         | [term] -> term
428         | terms -> Ast.ApplPattern terms)
429     ]
430   ];
431   interpretation: [
432     [ s = CSYMBOL; args = LIST0 argument; SYMBOL "="; t = level3_term ->
433         (s, args, t)
434     ]
435   ];
436   command: [ [
437       IDENT "set"; n = QSTRING; v = QSTRING ->
438         GrafiteAst.Set (loc, n, v)
439     | IDENT "drop" -> GrafiteAst.Drop loc
440     | IDENT "qed" -> GrafiteAst.Qed loc
441     | IDENT "variant" ; name = IDENT; SYMBOL ":"; 
442       typ = term; SYMBOL <:unicode<def>> ; newname = IDENT ->
443         GrafiteAst.Obj (loc, 
444           GrafiteAst.Theorem 
445             (`Variant,name,typ,Some (Ast.Ident (newname, None))))
446     | flavour = theorem_flavour; name = IDENT; SYMBOL ":"; typ = term;
447       body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
448         GrafiteAst.Obj (loc,GrafiteAst.Theorem (flavour, name, typ, body))
449     | flavour = theorem_flavour; name = IDENT; SYMBOL <:unicode<def>> (* ≝ *);
450       body = term ->
451         GrafiteAst.Obj (loc,
452           GrafiteAst.Theorem (flavour, name, Ast.Implicit, Some body))
453     | "let"; ind_kind = [ "corec" -> `CoInductive | "rec"-> `Inductive ];
454         defs = CicNotationParser.let_defs -> 
455           let name,ty = 
456             match defs with
457             | ((Ast.Ident (name, None), Some ty),_,_) :: _ -> name,ty
458             | ((Ast.Ident (name, None), None),_,_) :: _ ->
459                 name, Ast.Implicit
460             | _ -> assert false 
461           in
462           let body = Ast.Ident (name,None) in
463           GrafiteAst.Obj (loc,GrafiteAst.Theorem(`Definition, name, ty,
464             Some (Ast.LetRec (ind_kind, defs, body))))
465     | IDENT "inductive"; spec = inductive_spec ->
466         let (params, ind_types) = spec in
467         GrafiteAst.Obj (loc,GrafiteAst.Inductive (params, ind_types))
468     | IDENT "coinductive"; spec = inductive_spec ->
469         let (params, ind_types) = spec in
470         let ind_types = (* set inductive flags to false (coinductive) *)
471           List.map (fun (name, _, term, ctors) -> (name, false, term, ctors))
472             ind_types
473         in
474         GrafiteAst.Obj (loc,GrafiteAst.Inductive (params, ind_types))
475     | IDENT "coercion" ; name = IDENT -> 
476         GrafiteAst.Coercion (loc, Ast.Ident (name,Some []))
477     | IDENT "coercion" ; name = URI -> 
478         GrafiteAst.Coercion (loc, Ast.Uri (name,Some []))
479     | IDENT "alias" ; spec = alias_spec ->
480         GrafiteAst.Alias (loc, spec)
481     | IDENT "record" ; (params,name,ty,fields) = record_spec ->
482         GrafiteAst.Obj (loc,GrafiteAst.Record (params,name,ty,fields))
483     | IDENT "include" ; path = QSTRING ->
484         GrafiteAst.Include (loc,path)
485     | IDENT "default" ; what = QSTRING ; uris = LIST1 URI ->
486        let uris = List.map UriManager.uri_of_string uris in
487         GrafiteAst.Default (loc,what,uris)
488     | IDENT "notation"; (dir, l1, assoc, prec, l2) = notation ->
489         GrafiteAst.Notation (loc, dir, l1, assoc, prec, l2)
490     | IDENT "interpretation"; id = QSTRING;
491       (symbol, args, l3) = interpretation ->
492         GrafiteAst.Interpretation (loc, id, (symbol, args), l3)
493     | IDENT "metadata"; [ IDENT "dependency" | IDENT "baseuri" ] ; URI ->
494         (** metadata commands lives only in .moo, where they are in marshalled
495          * form *)
496         raise (CicNotationParser.Parse_error (loc, "metadata not allowed here"))
497
498     | IDENT "dump" -> GrafiteAst.Dump loc
499     | IDENT "render"; u = URI ->
500         GrafiteAst.Render (loc, UriManager.uri_of_string u)
501   ]];
502   executable: [
503     [ cmd = command; SYMBOL "." -> GrafiteAst.Command (loc, cmd)
504     | tac = tactical; punct = punctuation_tactical ->
505         GrafiteAst.Tactical (loc, tac, Some punct)
506     | punct = punctuation_tactical -> GrafiteAst.Tactical (loc, punct, None)
507     | mac = macro; SYMBOL "." -> GrafiteAst.Macro (loc, mac)
508     ]
509   ];
510   comment: [
511     [ BEGINCOMMENT ; ex = executable ; ENDCOMMENT -> 
512        GrafiteAst.Code (loc, ex)
513     | str = NOTE -> 
514        GrafiteAst.Note (loc, str)
515     ]
516   ];
517   statement: [
518     [ ex = executable -> GrafiteAst.Executable (loc,ex)
519     | com = comment -> GrafiteAst.Comment (loc, com)
520     | EOI -> raise End_of_file
521     ]
522   ];
523 END
524
525 let exc_located_wrapper f =
526   try
527     f ()
528   with
529   | Stdpp.Exc_located (_, End_of_file) -> raise End_of_file
530   | Stdpp.Exc_located (floc, Stream.Error msg) ->
531       raise (CicNotationParser.Parse_error (floc, msg))
532   | Stdpp.Exc_located (floc, exn) ->
533       raise (CicNotationParser.Parse_error (floc, (Printexc.to_string exn)))
534
535 let parse_statement lexbuf =
536   exc_located_wrapper
537     (fun () -> (Grammar.Entry.parse statement (Obj.magic lexbuf)))
538
539 let parse_dependencies lexbuf = 
540   let tok_stream,_ =
541     CicNotationLexer.level2_ast_lexer.Token.tok_func (Obj.magic lexbuf)
542   in
543   let rec parse acc = 
544     (parser
545     | [< '("URI", u) >] ->
546         parse (GrafiteAst.UriDep (UriManager.uri_of_string u) :: acc)
547     | [< '("IDENT", "include"); '("QSTRING", fname) >] ->
548         parse (GrafiteAst.IncludeDep fname :: acc)
549     | [< '("IDENT", "set"); '("QSTRING", "baseuri"); '("QSTRING", baseuri) >] ->
550         parse (GrafiteAst.BaseuriDep baseuri :: acc)
551     | [< '("EOI", _) >] -> acc
552     | [< 'tok >] -> parse acc
553     | [<  >] -> acc) tok_stream
554   in
555   List.rev (parse [])
556