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