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