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