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