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