]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/grafite_parser/grafiteParser.ml
test branch
[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 (* $Id$ *)
27
28 open Printf
29
30 module Ast = CicNotationPt
31
32 type 'a localized_option =
33    LSome of 'a
34  | LNone of Token.flocation
35
36 type statement =
37  include_paths:string list ->
38  LexiconEngine.status ->
39   LexiconEngine.status *
40   (CicNotationPt.term, CicNotationPt.term,
41    CicNotationPt.term GrafiteAst.reduction, CicNotationPt.obj, string)
42     GrafiteAst.statement localized_option
43
44 let grammar = CicNotationParser.level2_ast_grammar
45
46 let term = CicNotationParser.term
47 let statement = Grammar.Entry.create grammar "statement"
48
49 let add_raw_attribute ~text t = Ast.AttributedTerm (`Raw text, t)
50
51 let default_precedence = 50
52 let default_associativity = Gramext.NonA
53
54 EXTEND
55   GLOBAL: term statement;
56   arg: [
57    [ LPAREN; names = LIST1 IDENT SEP SYMBOL ",";
58       SYMBOL ":"; ty = term; RPAREN -> names,ty
59    | name = IDENT -> [name],Ast.Implicit
60    ]
61   ];
62   constructor: [ [ name = IDENT; SYMBOL ":"; typ = term -> (name, typ) ] ];
63   tactic_term: [ [ t = term LEVEL "90N" -> t ] ];
64   ident_list0: [ [ LPAREN; idents = LIST0 IDENT; RPAREN -> idents ] ];
65   tactic_term_list1: [
66     [ tactic_terms = LIST1 tactic_term SEP SYMBOL "," -> tactic_terms ]
67   ];
68   reduction_kind: [
69     [ IDENT "normalize" -> `Normalize
70     | IDENT "reduce" -> `Reduce
71     | IDENT "simplify" -> `Simpl
72     | IDENT "unfold"; t = OPT term -> `Unfold t
73     | IDENT "whd" -> `Whd ]
74   ];
75   sequent_pattern_spec: [
76    [ hyp_paths =
77       LIST0
78        [ id = IDENT ;
79          path = OPT [SYMBOL ":" ; path = tactic_term -> path ] ->
80          (id,match path with Some p -> p | None -> Ast.UserInput) ];
81      goal_path = OPT [ SYMBOL <:unicode<vdash>>; term = tactic_term -> term ] ->
82       let goal_path =
83        match goal_path, hyp_paths with
84           None, [] -> Some Ast.UserInput
85         | None, _::_ -> None
86         | Some goal_path, _ -> Some goal_path
87       in
88        hyp_paths,goal_path
89    ]
90   ];
91   pattern_spec: [
92     [ res = OPT [
93        "in";
94        wanted_and_sps =
95         [ "match" ; wanted = tactic_term ;
96           sps = OPT [ "in"; sps = sequent_pattern_spec -> sps ] ->
97            Some wanted,sps
98         | sps = sequent_pattern_spec ->
99            None,Some sps
100         ] ->
101          let wanted,hyp_paths,goal_path =
102           match wanted_and_sps with
103              wanted,None -> wanted, [], Some Ast.UserInput
104            | wanted,Some (hyp_paths,goal_path) -> wanted,hyp_paths,goal_path
105          in
106           wanted, hyp_paths, goal_path ] ->
107       match res with
108          None -> None,[],Some Ast.UserInput
109        | Some ps -> ps]
110   ];
111   direction: [
112     [ SYMBOL ">" -> `LeftToRight
113     | SYMBOL "<" -> `RightToLeft ]
114   ];
115   int: [ [ num = NUMBER -> int_of_string num ] ];
116   intros_spec: [
117     [ num = OPT [ num = int -> num ]; idents = OPT ident_list0 ->
118         let idents = match idents with None -> [] | Some idents -> idents in
119         num, idents
120     ]
121   ];
122   using: [ [ using = OPT [ IDENT "using"; t = tactic_term -> t ] -> using ] ];
123   tactic: [
124     [ IDENT "absurd"; t = tactic_term ->
125         GrafiteAst.Absurd (loc, t)
126     | IDENT "apply"; t = tactic_term ->
127         GrafiteAst.Apply (loc, t)
128     | IDENT "assumption" ->
129         GrafiteAst.Assumption loc
130     | IDENT "auto";
131       depth = OPT [ IDENT "depth"; SYMBOL "="; i = int -> i ];
132       width = OPT [ IDENT "width"; SYMBOL "="; i = int -> i ];
133       paramodulation = OPT [ IDENT "paramodulation" ];
134       full = OPT [ IDENT "full" ] ->  (* ALB *)
135           GrafiteAst.Auto (loc,depth,width,paramodulation,full)
136     | IDENT "clear"; id = IDENT ->
137         GrafiteAst.Clear (loc,id)
138     | IDENT "clearbody"; id = IDENT ->
139         GrafiteAst.ClearBody (loc,id)
140     | IDENT "change"; what = pattern_spec; "with"; t = tactic_term ->
141         GrafiteAst.Change (loc, what, t)
142     | IDENT "compare"; t = tactic_term ->
143         GrafiteAst.Compare (loc,t)
144     | IDENT "constructor"; n = int ->
145         GrafiteAst.Constructor (loc, n)
146     | IDENT "contradiction" ->
147         GrafiteAst.Contradiction loc
148     | IDENT "cut"; t = tactic_term; ident = OPT [ "as"; id = IDENT -> id] ->
149         GrafiteAst.Cut (loc, ident, t)
150     | IDENT "decide"; IDENT "equality" ->
151         GrafiteAst.DecideEquality loc
152     | IDENT "decompose"; types = OPT ident_list0; what = IDENT;
153       (num, idents) = intros_spec ->
154         let types = match types with None -> [] | Some types -> types 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 ident_list0 ->
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 [ IDENT "using" ; 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 = int; SYMBOL ":" -> GrafiteAst.Pos (loc, i)
271       | SYMBOL "]" -> GrafiteAst.Merge loc
272       | SYMBOL ";" -> GrafiteAst.Semicolon loc
273       | SYMBOL "." -> GrafiteAst.Dot loc
274       ]
275     ];
276   tactical:
277     [ "simple" NONA
278       [ IDENT "focus"; goals = LIST1 int -> GrafiteAst.Focus (loc, goals)
279       | IDENT "unfocus" -> GrafiteAst.Unfocus loc
280       | IDENT "skip" -> GrafiteAst.Skip loc
281       | tac = atomic_tactical LEVEL "loops" -> tac
282       ]
283     ];
284   theorem_flavour: [
285     [ [ IDENT "definition"  ] -> `Definition
286     | [ IDENT "fact"        ] -> `Fact
287     | [ IDENT "lemma"       ] -> `Lemma
288     | [ IDENT "remark"      ] -> `Remark
289     | [ IDENT "theorem"     ] -> `Theorem
290     ]
291   ];
292   inductive_spec: [ [
293     fst_name = IDENT; params = LIST0 [ arg=arg -> arg ];
294     SYMBOL ":"; fst_typ = term; SYMBOL <:unicode<def>>; OPT SYMBOL "|";
295     fst_constructors = LIST0 constructor SEP SYMBOL "|";
296     tl = OPT [ "with";
297       types = LIST1 [
298         name = IDENT; SYMBOL ":"; typ = term; SYMBOL <:unicode<def>>;
299        OPT SYMBOL "|"; constructors = LIST0 constructor SEP SYMBOL "|" ->
300           (name, true, typ, constructors) ] SEP "with" -> types
301     ] ->
302       let params =
303         List.fold_right
304           (fun (names, typ) acc ->
305             (List.map (fun name -> (name, typ)) names) @ acc)
306           params []
307       in
308       let fst_ind_type = (fst_name, true, fst_typ, fst_constructors) in
309       let tl_ind_types = match tl with None -> [] | Some types -> types in
310       let ind_types = fst_ind_type :: tl_ind_types in
311       (params, ind_types)
312   ] ];
313   
314   record_spec: [ [
315     name = IDENT; params = LIST0 [ arg = arg -> arg ] ;
316      SYMBOL ":"; typ = term; SYMBOL <:unicode<def>>; SYMBOL "{" ; 
317      fields = LIST0 [ 
318        name = IDENT ; 
319        coercion = [ SYMBOL ":" -> false | SYMBOL ":"; SYMBOL ">" -> true ] ; 
320        ty = term -> (name,ty,coercion) 
321      ] SEP SYMBOL ";"; SYMBOL "}" -> 
322       let params =
323         List.fold_right
324           (fun (names, typ) acc ->
325             (List.map (fun name -> (name, typ)) names) @ acc)
326           params []
327       in
328       (params,name,typ,fields)
329   ] ];
330   
331   macro: [
332     [ [ IDENT "quit"  ] -> GrafiteAst.Quit loc
333 (*     | [ IDENT "abort" ] -> GrafiteAst.Abort loc *)
334 (*     | [ IDENT "undo"   ]; steps = OPT NUMBER ->
335         GrafiteAst.Undo (loc, int_opt steps)
336     | [ IDENT "redo"   ]; steps = OPT NUMBER ->
337         GrafiteAst.Redo (loc, int_opt steps) *)
338     | [ IDENT "check"   ]; t = term ->
339         GrafiteAst.Check (loc, t)
340     | [ IDENT "hint" ] -> GrafiteAst.Hint loc
341     | [ IDENT "whelp"; "match" ] ; t = term -> 
342         GrafiteAst.WMatch (loc,t)
343     | [ IDENT "whelp"; IDENT "instance" ] ; t = term -> 
344         GrafiteAst.WInstance (loc,t)
345     | [ IDENT "whelp"; IDENT "locate" ] ; id = IDENT -> 
346         GrafiteAst.WLocate (loc,id)
347     | [ IDENT "whelp"; IDENT "elim" ] ; t = term ->
348         GrafiteAst.WElim (loc, t)
349     | [ IDENT "whelp"; IDENT "hint" ] ; t = term -> 
350         GrafiteAst.WHint (loc,t)
351     | [ IDENT "print" ]; name = QSTRING -> GrafiteAst.Print (loc, name)
352     ]
353   ];
354   alias_spec: [
355     [ IDENT "id"; id = QSTRING; SYMBOL "="; uri = QSTRING ->
356       let alpha = "[a-zA-Z]" in
357       let num = "[0-9]+" in
358       let ident_cont = "\\("^alpha^"\\|"^num^"\\|_\\|\\\\\\)" in
359       let ident = "\\("^alpha^ident_cont^"*\\|_"^ident_cont^"+\\)" in
360       let rex = Str.regexp ("^"^ident^"$") in
361       if Str.string_match rex id 0 then
362         if (try ignore (UriManager.uri_of_string uri); true
363             with UriManager.IllFormedUri _ -> false)
364         then
365           LexiconAst.Ident_alias (id, uri)
366         else 
367           raise
368            (HExtlib.Localized (loc, CicNotationParser.Parse_error (sprintf "Not a valid uri: %s" uri)))
369       else
370         raise (HExtlib.Localized (loc, CicNotationParser.Parse_error (
371           sprintf "Not a valid identifier: %s" id)))
372     | IDENT "symbol"; symbol = QSTRING;
373       instance = OPT [ LPAREN; IDENT "instance"; n = int; RPAREN -> n ];
374       SYMBOL "="; dsc = QSTRING ->
375         let instance =
376           match instance with Some i -> i | None -> 0
377         in
378         LexiconAst.Symbol_alias (symbol, instance, dsc)
379     | IDENT "num";
380       instance = OPT [ LPAREN; IDENT "instance"; n = int; RPAREN -> n ];
381       SYMBOL "="; dsc = QSTRING ->
382         let instance =
383           match instance with Some i -> i | None -> 0
384         in
385         LexiconAst.Number_alias (instance, dsc)
386     ]
387   ];
388   argument: [
389     [ l = LIST0 [ SYMBOL <:unicode<eta>> (* η *); SYMBOL "." -> () ];
390       id = IDENT ->
391         Ast.IdentArg (List.length l, id)
392     ]
393   ];
394   associativity: [
395     [ IDENT "left";  IDENT "associative" -> Gramext.LeftA
396     | IDENT "right"; IDENT "associative" -> Gramext.RightA
397     | IDENT "non"; IDENT "associative" -> Gramext.NonA
398     ]
399   ];
400   precedence: [
401     [ "with"; IDENT "precedence"; n = NUMBER -> int_of_string n ]
402   ];
403   notation: [
404     [ dir = OPT direction; s = QSTRING;
405       assoc = OPT associativity; prec = OPT precedence;
406       IDENT "for";
407       p2 = 
408         [ blob = UNPARSED_AST ->
409             add_raw_attribute ~text:(sprintf "@{%s}" blob)
410               (CicNotationParser.parse_level2_ast
411                 (Ulexing.from_utf8_string blob))
412         | blob = UNPARSED_META ->
413             add_raw_attribute ~text:(sprintf "${%s}" blob)
414               (CicNotationParser.parse_level2_meta
415                 (Ulexing.from_utf8_string blob))
416         ] ->
417           let assoc =
418             match assoc with
419             | None -> default_associativity
420             | Some assoc -> assoc
421           in
422           let prec =
423             match prec with
424             | None -> default_precedence
425             | Some prec -> prec
426           in
427           let p1 =
428             add_raw_attribute ~text:s
429               (CicNotationParser.parse_level1_pattern
430                 (Ulexing.from_utf8_string s))
431           in
432           (dir, p1, assoc, prec, p2)
433     ]
434   ];
435   level3_term: [
436     [ u = URI -> Ast.UriPattern (UriManager.uri_of_string u)
437     | id = IDENT -> Ast.VarPattern id
438     | SYMBOL "_" -> Ast.ImplicitPattern
439     | LPAREN; terms = LIST1 SELF; RPAREN ->
440         (match terms with
441         | [] -> assert false
442         | [term] -> term
443         | terms -> Ast.ApplPattern terms)
444     ]
445   ];
446   interpretation: [
447     [ s = CSYMBOL; args = LIST0 argument; SYMBOL "="; t = level3_term ->
448         (s, args, t)
449     ]
450   ];
451   
452   include_command: [ [
453       IDENT "include" ; path = QSTRING -> loc,path
454    ]];
455
456   grafite_command: [ [
457       IDENT "set"; n = QSTRING; v = QSTRING ->
458         GrafiteAst.Set (loc, n, v)
459     | IDENT "drop" -> GrafiteAst.Drop loc
460     | IDENT "qed" -> GrafiteAst.Qed loc
461     | IDENT "variant" ; name = IDENT; SYMBOL ":"; 
462       typ = term; SYMBOL <:unicode<def>> ; newname = IDENT ->
463         GrafiteAst.Obj (loc, 
464           Ast.Theorem 
465             (`Variant,name,typ,Some (Ast.Ident (newname, None))))
466     | flavour = theorem_flavour; name = IDENT; SYMBOL ":"; typ = term;
467       body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
468         GrafiteAst.Obj (loc, Ast.Theorem (flavour, name, typ, body))
469     | flavour = theorem_flavour; name = IDENT; SYMBOL <:unicode<def>> (* ≝ *);
470       body = term ->
471         GrafiteAst.Obj (loc,
472           Ast.Theorem (flavour, name, Ast.Implicit, Some body))
473     | "let"; ind_kind = [ "corec" -> `CoInductive | "rec"-> `Inductive ];
474         defs = CicNotationParser.let_defs -> 
475           let name,ty = 
476             match defs with
477             | ((Ast.Ident (name, None), Some ty),_,_) :: _ -> name,ty
478             | ((Ast.Ident (name, None), None),_,_) :: _ ->
479                 name, Ast.Implicit
480             | _ -> assert false 
481           in
482           let body = Ast.Ident (name,None) in
483           GrafiteAst.Obj (loc, Ast.Theorem(`Definition, name, ty,
484             Some (Ast.LetRec (ind_kind, defs, body))))
485     | IDENT "inductive"; spec = inductive_spec ->
486         let (params, ind_types) = spec in
487         GrafiteAst.Obj (loc, Ast.Inductive (params, ind_types))
488     | IDENT "coinductive"; spec = inductive_spec ->
489         let (params, ind_types) = spec in
490         let ind_types = (* set inductive flags to false (coinductive) *)
491           List.map (fun (name, _, term, ctors) -> (name, false, term, ctors))
492             ind_types
493         in
494         GrafiteAst.Obj (loc, Ast.Inductive (params, ind_types))
495     | IDENT "coercion" ; suri = URI ->
496         GrafiteAst.Coercion (loc, UriManager.uri_of_string suri, true)
497     | IDENT "record" ; (params,name,ty,fields) = record_spec ->
498         GrafiteAst.Obj (loc, Ast.Record (params,name,ty,fields))
499     | IDENT "default" ; what = QSTRING ; uris = LIST1 URI ->
500        let uris = List.map UriManager.uri_of_string uris in
501         GrafiteAst.Default (loc,what,uris)
502   ]];
503   lexicon_command: [ [
504       IDENT "alias" ; spec = alias_spec ->
505         LexiconAst.Alias (loc, spec)
506     | IDENT "notation"; (dir, l1, assoc, prec, l2) = notation ->
507         LexiconAst.Notation (loc, dir, l1, assoc, prec, l2)
508     | IDENT "interpretation"; id = QSTRING;
509       (symbol, args, l3) = interpretation ->
510         LexiconAst.Interpretation (loc, id, (symbol, args), l3)
511   ]];
512   executable: [
513     [ cmd = grafite_command; SYMBOL "." -> GrafiteAst.Command (loc, cmd)
514     | tac = tactical; punct = punctuation_tactical ->
515         GrafiteAst.Tactical (loc, tac, Some punct)
516     | punct = punctuation_tactical -> GrafiteAst.Tactical (loc, punct, None)
517     | mac = macro; SYMBOL "." -> GrafiteAst.Macro (loc, mac)
518     ]
519   ];
520   comment: [
521     [ BEGINCOMMENT ; ex = executable ; ENDCOMMENT -> 
522        GrafiteAst.Code (loc, ex)
523     | str = NOTE -> 
524        GrafiteAst.Note (loc, str)
525     ]
526   ];
527   statement: [
528     [ ex = executable ->
529        fun ~include_paths status -> status,LSome(GrafiteAst.Executable (loc,ex))
530     | com = comment ->
531        fun ~include_paths status -> status,LSome (GrafiteAst.Comment (loc, com))
532     | (iloc,fname) = include_command ; SYMBOL "."  ->
533        fun ~include_paths status ->
534         let path = DependenciesParser.baseuri_of_script ~include_paths fname in
535         let status =
536          LexiconEngine.eval_command status (LexiconAst.Include (iloc,path))
537         in
538          status,
539           LSome
540           (GrafiteAst.Executable
541            (loc,GrafiteAst.Command
542             (loc,GrafiteAst.Include (iloc,path))))
543     | scom = lexicon_command ; SYMBOL "." ->
544        fun ~include_paths status ->
545         let status = LexiconEngine.eval_command status scom in
546          status,LNone loc
547     | EOI -> raise End_of_file
548     ]
549   ];
550 END
551
552 let exc_located_wrapper f =
553   try
554     f ()
555   with
556   | Stdpp.Exc_located (_, End_of_file) -> raise End_of_file
557   | Stdpp.Exc_located (floc, Stream.Error msg) ->
558       raise (HExtlib.Localized (floc,CicNotationParser.Parse_error msg))
559   | Stdpp.Exc_located (floc, exn) ->
560       raise
561        (HExtlib.Localized (floc,CicNotationParser.Parse_error (Printexc.to_string exn)))
562
563 let parse_statement lexbuf =
564   exc_located_wrapper
565     (fun () -> (Grammar.Entry.parse statement (Obj.magic lexbuf)))