]> matita.cs.unibo.it Git - helm.git/blob - matita/components/grafite_parser/grafiteParser.ml
notation ast updated to comply with the toplevel let rec of ng_kernel
[helm.git] / matita / 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 module N  = NotationPt
29 module G  = GrafiteAst
30
31 let exc_located_wrapper f =
32   try
33     f ()
34   with
35   | Ploc.Exc (_, End_of_file) -> raise End_of_file
36   | Ploc.Exc (floc, Stream.Error msg) ->
37       raise (HExtlib.Localized (floc,CicNotationParser.Parse_error msg))
38   | Ploc.Exc (floc, HExtlib.Localized(_,exn)) ->
39       raise (HExtlib.Localized 
40         (floc,CicNotationParser.Parse_error (Printexc.to_string exn)))
41   | Ploc.Exc (floc, exn) ->
42       raise (HExtlib.Localized 
43         (floc,CicNotationParser.Parse_error (Printexc.to_string exn)))
44
45 type parsable = Grammar.parsable * Ulexing.lexbuf
46
47 let parsable_statement status buf =
48  let grammar = CicNotationParser.level2_ast_grammar status in
49   Grammar.parsable grammar (Obj.magic buf), buf
50
51 let parse_statement grafite_parser parsable =
52   exc_located_wrapper
53     (fun () -> (Grammar.Entry.parse_parsable grafite_parser (fst parsable)))
54
55 let strm_of_parsable (_,buf) = buf
56
57 let add_raw_attribute ~text t = N.AttributedTerm (`Raw text, t)
58
59 let default_associativity = Gramext.NonA
60         
61 let mk_rec_corec ind_kind defs loc = 
62  let attrs = `Provided, `Definition, `Regular in
63   (loc, N.LetRec (ind_kind, defs, attrs))
64
65 let nmk_rec_corec ind_kind defs loc index = 
66  let loc,t = mk_rec_corec ind_kind defs loc in
67   G.NObj (loc,t,index)
68
69 (*
70 let nnon_punct_of_punct = function
71   | G.Skip loc -> G.NSkip loc
72   | G.Unfocus loc -> G.NUnfocus loc
73   | G.Focus (loc,l) -> G.NFocus (loc,l)
74 ;; *)
75
76 type by_continuation =
77    BYC_done
78  | BYC_weproved of N.term * string option * N.term option
79  | BYC_letsuchthat of string * N.term * string * N.term
80  | BYC_wehaveand of string * N.term * string * N.term
81
82 let mk_parser statement lstatus =
83 (*   let grammar = CicNotationParser.level2_ast_grammar lstatus in *)
84   let term = CicNotationParser.term lstatus in
85   let let_defs = CicNotationParser.let_defs lstatus in
86   let let_codefs = CicNotationParser.let_codefs lstatus in
87   let protected_binder_vars = CicNotationParser.protected_binder_vars lstatus in
88   (* {{{ parser initialization *)
89 EXTEND
90   GLOBAL: term statement;
91   constructor: [ [ name = IDENT; SYMBOL ":"; typ = term -> (name, typ) ] ];
92   tactic_term: [ [ t = term LEVEL "90" -> t ] ];
93   ident_list1: [ [ LPAREN; idents = LIST1 IDENT; RPAREN -> idents ] ];
94   nreduction_kind: [
95     [ IDENT "normalize" ; delta = OPT [ IDENT "nodelta" -> () ] ->
96        let delta = match delta with None -> true | _ -> false in
97         `Normalize delta
98     | IDENT "whd" ; delta = OPT [ IDENT "nodelta" -> () ] ->
99        let delta = match delta with None -> true | _ -> false in
100         `Whd delta]
101   ];
102   sequent_pattern_spec: [
103    [ hyp_paths =
104       LIST0
105        [ id = IDENT ;
106          path = OPT [SYMBOL ":" ; path = tactic_term -> path ] ->
107          (id,match path with Some p -> p | None -> N.UserInput) ];
108      goal_path = OPT [ SYMBOL <:unicode<vdash>>; term = tactic_term -> term ] ->
109       let goal_path =
110        match goal_path, hyp_paths with
111           None, [] -> Some N.UserInput
112         | None, _::_ -> None
113         | Some goal_path, _ -> Some goal_path
114       in
115        hyp_paths,goal_path
116    ]
117   ];
118   pattern_spec: [
119     [ res = OPT [
120        "in" ;
121        wanted_and_sps =
122         [ "match" ; wanted = tactic_term ;
123           sps = OPT [ "in"; sps = sequent_pattern_spec -> sps ] ->
124            Some wanted,sps
125         | sps = sequent_pattern_spec ->
126            None,Some sps
127         ];
128        SYMBOL ";" ->
129          let wanted,hyp_paths,goal_path =
130           match wanted_and_sps with
131              wanted,None -> wanted, [], Some N.UserInput
132            | wanted,Some (hyp_paths,goal_path) -> wanted,hyp_paths,goal_path
133          in
134           wanted, hyp_paths, goal_path ] ->
135       match res with
136          None -> None,[],Some N.UserInput
137        | Some ps -> ps]
138   ];
139   inverter_param_list: [ 
140     [ params = tactic_term -> 
141       let deannotate = function
142         | N.AttributedTerm (_,t) | t -> t
143       in match deannotate params with
144       | N.Implicit _ -> [false]
145       | N.UserInput -> [true]
146       | N.Appl l -> 
147          List.map (fun x -> match deannotate x with  
148            | N.Implicit _ -> false
149            | N.UserInput -> true
150            | _ -> raise (Invalid_argument "malformed target parameter list 1")) l
151       | _ ->
152        (*CSC: new NCicPp.status is the best I can do here without changing the
153          result type *)
154        raise (Invalid_argument ("malformed target parameter list 2\n" ^ NotationPp.pp_term (new NCicPp.status) params)) ]
155   ];
156   direction: [
157     [ SYMBOL ">" -> `LeftToRight
158     | SYMBOL "<" -> `RightToLeft ]
159   ];
160   int: [ [ num = NUMBER -> int_of_string num ] ];
161   ntactic: [
162     [ SYMBOL "@"; t = tactic_term -> G.NTactic(loc,[G.NApply (loc, t)])
163     | IDENT "applyS"; t = tactic_term -> G.NTactic(loc,[G.NSmartApply(loc, t)])
164     | IDENT "assert";
165        seqs = LIST0 [
166         hyps = LIST0
167          [ id = IDENT ; SYMBOL ":" ; ty = tactic_term -> id,`Decl ty
168          | id = IDENT ; SYMBOL ":" ; ty = tactic_term ;
169                         SYMBOL <:unicode<def>> ; bo = tactic_term ->
170             id,`Def (bo,ty)];
171         SYMBOL <:unicode<vdash>>;
172         concl = tactic_term -> (List.rev hyps,concl) ] ->
173          G.NTactic(loc,[G.NAssert (loc, seqs)])
174     | SYMBOL "/"; num = OPT NUMBER ; 
175        just_and_params = auto_params; SYMBOL "/" ->
176        let just,params = just_and_params in
177        let depth = match num with Some n -> n | None -> "1" in
178        (match just with
179        | None -> 
180                  G.NTactic(loc,
181             [G.NAuto(loc,(None,["depth",depth]@params))])
182        | Some (`Univ univ) ->
183                  G.NTactic(loc,
184             [G.NAuto(loc,(Some univ,["depth",depth]@params))])
185        | Some `Trace ->
186                  G.NMacro(loc,
187              G.NAutoInteractive (loc, (None,["depth",depth]@params))))
188     | SYMBOL "#"; SYMBOL "#" -> G.NMacro (loc, G.NIntroGuess loc)
189     | IDENT "check"; t = tactic_term -> G.NMacro(loc,G.NCheck (loc,t))
190     | IDENT "screenshot"; fname = QSTRING -> 
191         G.NMacro(loc,G.Screenshot (loc, fname))
192     | IDENT "cases"; what = tactic_term ; where = pattern_spec ->
193         G.NTactic(loc,[G.NCases (loc, what, where)])
194     | IDENT "change";  "with"; with_what = tactic_term; what = pattern_spec -> 
195         G.NTactic(loc,[G.NChange (loc, what, with_what)])
196     | SYMBOL "-"; id = IDENT ->
197         G.NTactic(loc,[G.NClear (loc, [id])])
198     | PLACEHOLDER; num = OPT NUMBER; 
199         l = OPT [ SYMBOL "{"; l = LIST1 tactic_term; SYMBOL "}" -> l ] -> 
200         G.NTactic(loc,[G.NConstructor (loc, (match num with None -> None | Some x -> Some (int_of_string x)),match l with None -> [] | Some l -> l)])
201     | IDENT "cut"; t = tactic_term -> G.NTactic(loc,[G.NCut (loc, t)])
202     | IDENT "destruct"; just = OPT [ dom = ident_list1 -> dom ];
203       exclude = OPT [ IDENT "skip"; skip = ident_list1 -> skip ]
204         -> let exclude' = match exclude with None -> [] | Some l -> l in
205            G.NTactic(loc,[G.NDestruct (loc,just,exclude')])
206     | IDENT "elim"; what = tactic_term ; where = pattern_spec ->
207         G.NTactic(loc,[G.NElim (loc, what, where)])
208     | IDENT "generalize"; p=pattern_spec ->
209         G.NTactic(loc,[G.NGeneralize (loc, p)])
210     | IDENT "inversion"; what = tactic_term ; where = pattern_spec ->
211         G.NTactic(loc,[G.NInversion (loc, what, where)])
212     | IDENT "lapply"; t = tactic_term -> G.NTactic(loc,[G.NLApply (loc, t)])
213     | IDENT "letin"; name = IDENT ; SYMBOL <:unicode<def>> ; t = tactic_term;
214         where = pattern_spec ->
215         G.NTactic(loc,[G.NLetIn (loc,where,t,name)])
216     | kind = nreduction_kind; p = pattern_spec ->
217         G.NTactic(loc,[G.NReduce (loc, kind, p)])
218     | dir = direction; what = tactic_term ; where = pattern_spec ->     
219         G.NTactic(loc,[G.NRewrite (loc, dir, what, where)])
220     | IDENT "try"; tac = SELF -> 
221         let tac = match tac with G.NTactic(_,[t]) -> t | _ -> assert false in
222         G.NTactic(loc,[ G.NTry (loc,tac)])
223     | IDENT "repeat"; tac = SELF -> 
224         let tac = match tac with G.NTactic(_,[t]) -> t | _ -> assert false in
225         G.NTactic(loc,[ G.NRepeat (loc,tac)])
226     | LPAREN; l = LIST1 SELF; RPAREN -> 
227         let l = 
228           List.flatten 
229             (List.map (function G.NTactic(_,t) -> t | _ -> assert false) l) in
230         G.NTactic(loc,[G.NBlock (loc,l)])
231     | IDENT "assumption" -> G.NTactic(loc,[ G.NAssumption loc])
232     | SYMBOL "#"; ns=IDENT -> G.NTactic(loc,[ G.NIntros (loc,[ns])])
233     | SYMBOL "#"; SYMBOL "_" -> G.NTactic(loc,[ G.NIntro (loc,"_")])
234     | SYMBOL "*" -> G.NTactic(loc,[ G.NCase1 (loc,"_")])
235     | SYMBOL "*"; "as"; n=IDENT -> G.NTactic(loc,[ G.NCase1 (loc,n)])
236     ]
237   ];
238   auto_fixed_param: [
239    [ IDENT "demod"
240    | IDENT "fast_paramod"
241    | IDENT "paramod"
242    | IDENT "width"
243    | IDENT "size"
244    | IDENT "nohyps"
245 (*   | IDENT "timeout" *)
246    ]
247 ];
248   auto_params: [
249     [ params = 
250       LIST0 [
251          i = auto_fixed_param -> i,""
252        | i = auto_fixed_param ; SYMBOL "="; v = [ v = int ->
253               string_of_int v | v = IDENT -> v ] -> i,v ]; 
254       just = OPT [ IDENT "by"; by = 
255         [ univ = LIST0 tactic_term SEP SYMBOL "," -> `Univ univ
256         | SYMBOL "_" -> `Trace ] -> by ] -> just,params
257    ]
258 ];
259
260 (* MATITA 1.0
261   by_continuation: [
262     [ WEPROVED; ty = tactic_term ; LPAREN ; id = IDENT ; RPAREN ; t1 = OPT [IDENT "that" ; IDENT "is" ; IDENT "equivalent" ; "to" ; t2 = tactic_term -> t2] -> BYC_weproved (ty,Some id,t1)
263     | WEPROVED; ty = tactic_term ; t1 = OPT [IDENT "that" ; IDENT "is" ; IDENT "equivalent" ; "to" ; t2 = tactic_term -> t2] ; 
264             "done" -> BYC_weproved (ty,None,t1)
265     | "done" -> BYC_done
266     | "let" ; id1 = IDENT ; SYMBOL ":" ; t1 = tactic_term ;
267       IDENT "such" ; IDENT "that" ; t2=tactic_term ; LPAREN ; 
268       id2 = IDENT ; RPAREN -> BYC_letsuchthat (id1,t1,id2,t2)
269     | WEHAVE; t1=tactic_term ; LPAREN ; id1=IDENT ; RPAREN ;"and" ; t2=tactic_term ; LPAREN ; id2=IDENT ; RPAREN ->
270               BYC_wehaveand (id1,t1,id2,t2)
271     ]
272 ];
273 *)
274 (* MATITA 1.0
275   rewriting_step_continuation : [
276     [ "done" -> true
277     | -> false
278     ]
279 ];
280 *)
281 (* MATITA 1.0
282   atomic_tactical:
283     [ "sequence" LEFTA
284       [ t1 = SELF; SYMBOL ";"; t2 = SELF ->
285           let ts =
286             match t1 with
287             | G.Seq (_, l) -> l @ [ t2 ]
288             | _ -> [ t1; t2 ]
289           in
290           G.Seq (loc, ts)
291       ]
292     | "then" NONA
293       [ tac = SELF; SYMBOL ";";
294         SYMBOL "["; tacs = LIST0 SELF SEP SYMBOL "|"; SYMBOL "]"->
295           (G.Then (loc, tac, tacs))
296       ]
297     | "loops" RIGHTA
298       [ IDENT "do"; count = int; tac = SELF ->
299           G.Do (loc, count, tac)
300       | IDENT "repeat"; tac = SELF -> G.Repeat (loc, tac)
301       ]
302     | "simple" NONA
303       [ IDENT "first";
304         SYMBOL "["; tacs = LIST0 SELF SEP SYMBOL "|"; SYMBOL "]"->
305           G.First (loc, tacs)
306       | IDENT "try"; tac = SELF -> G.Try (loc, tac)
307       | IDENT "solve";
308         SYMBOL "["; tacs = LIST0 SELF SEP SYMBOL "|"; SYMBOL "]"->
309           G.Solve (loc, tacs)
310       | IDENT "progress"; tac = SELF -> G.Progress (loc, tac)
311       | LPAREN; tac = SELF; RPAREN -> tac
312       | tac = tactic -> tac
313         ]
314       ];
315 *)
316   npunctuation_tactical:
317     [
318       [ SYMBOL "[" -> G.NBranch loc
319       | SYMBOL "|" -> G.NShift loc
320       | i = LIST1 int SEP SYMBOL ","; SYMBOL ":" -> G.NPos (loc, i)
321       | SYMBOL "*"; SYMBOL ":" -> G.NWildcard loc
322       | name = IDENT; SYMBOL ":" -> G.NPosbyname (loc, name)
323       | SYMBOL "]" -> G.NMerge loc
324       | SYMBOL ";" -> G.NSemicolon loc
325       | SYMBOL "." -> G.NDot loc
326       ]
327     ];
328   nnon_punctuation_tactical:
329     [ "simple" NONA
330       [ IDENT "focus"; goals = LIST1 int -> G.NFocus (loc, goals)
331       | IDENT "unfocus" -> G.NUnfocus loc
332       | IDENT "skip" -> G.NSkip loc
333       ]
334       ];
335   ntheorem_flavour: [
336     [ [ IDENT "definition"  ] -> `Definition
337     | [ IDENT "fact"        ] -> `Fact
338     | [ IDENT "lemma"       ] -> `Lemma
339     | [ IDENT "example"     ] -> `Example
340     | [ IDENT "theorem"     ] -> `Theorem
341     | [ IDENT "corollary"   ] -> `Corollary
342     ]
343   ];
344   inductive_spec: [ [
345     fst_name = IDENT; 
346       params = LIST0 protected_binder_vars;
347     SYMBOL ":"; fst_typ = term; SYMBOL <:unicode<def>>; OPT SYMBOL "|";
348     fst_constructors = LIST0 constructor SEP SYMBOL "|";
349     tl = OPT [ "with";
350         types = LIST1 [
351           name = IDENT; SYMBOL ":"; typ = term; SYMBOL <:unicode<def>>;
352          OPT SYMBOL "|"; constructors = LIST0 constructor SEP SYMBOL "|" ->
353             (name, true, typ, constructors) ] SEP "with" -> types
354       ] ->
355         let params =
356           List.fold_right
357             (fun (names, typ) acc ->
358               (List.map (fun name -> (name, typ)) names) @ acc)
359             params []
360         in
361         let fst_ind_type = (fst_name, true, fst_typ, fst_constructors) in
362         let tl_ind_types = match tl with None -> [] | Some types -> types in
363         let ind_types = fst_ind_type :: tl_ind_types in
364         (params, ind_types)
365     ] ];
366     
367     record_spec: [ [
368       name = IDENT; 
369       params = LIST0 protected_binder_vars;
370        SYMBOL ":"; typ = term; SYMBOL <:unicode<def>>; SYMBOL "{" ; 
371        fields = LIST0 [ 
372          name = IDENT ; 
373          coercion = [ 
374              SYMBOL ":" -> false,0 
375            | SYMBOL ":"; SYMBOL ">" -> true,0
376            | SYMBOL ":"; arity = int ; SYMBOL ">" -> true,arity
377          ]; 
378          ty = term -> 
379            let b,n = coercion in 
380            (name,ty,b,n) 
381        ] SEP SYMBOL ";"; SYMBOL "}" -> 
382         let params =
383           List.fold_right
384             (fun (names, typ) acc ->
385               (List.map (fun name -> (name, typ)) names) @ acc)
386             params []
387         in
388         (params,name,typ,fields)
389     ] ];
390
391     alias_spec: [
392       [ IDENT "id"; id = QSTRING; SYMBOL "="; uri = QSTRING ->
393         let alpha = "[a-zA-Z]" in
394         let num = "[0-9]+" in
395         let ident_cont = "\\("^alpha^"\\|"^num^"\\|_\\|\\\\\\)" in
396         let decoration = "\\'" in
397         let ident = "\\("^alpha^ident_cont^"*"^decoration^"*\\|_"^ident_cont^"+"^decoration^"*\\)" in
398         let rex = Str.regexp ("^"^ident^"$") in
399         if Str.string_match rex id 0 then
400           if (try ignore (NReference.reference_of_string uri); true
401               with NReference.IllFormedReference _ -> false)
402           then
403             G.Ident_alias (id, uri)
404           else
405             raise
406              (HExtlib.Localized (loc, CicNotationParser.Parse_error (Printf.sprintf "Not a valid uri: %s" uri)))
407         else
408           raise (HExtlib.Localized (loc, CicNotationParser.Parse_error (
409             Printf.sprintf "Not a valid identifier: %s" id)))
410       | IDENT "symbol"; symbol = QSTRING;
411         instance = OPT [ LPAREN; IDENT "instance"; n = int; RPAREN -> n ];
412         SYMBOL "="; dsc = QSTRING ->
413           let instance =
414             match instance with Some i -> i | None -> 0
415           in
416           G.Symbol_alias (symbol, instance, dsc)
417       | IDENT "num";
418         instance = OPT [ LPAREN; IDENT "instance"; n = int; RPAREN -> n ];
419         SYMBOL "="; dsc = QSTRING ->
420           let instance =
421             match instance with Some i -> i | None -> 0
422           in
423           G.Number_alias (instance, dsc)
424       ]
425      ];
426     argument: [
427       [ l = LIST0 [ SYMBOL <:unicode<eta>> (* η *); SYMBOL "." -> () ];
428         id = IDENT ->
429           N.IdentArg (List.length l, id)
430       ]
431     ];
432     associativity: [
433       [ IDENT "left";  IDENT "associative" -> Gramext.LeftA
434       | IDENT "right"; IDENT "associative" -> Gramext.RightA
435       | IDENT "non"; IDENT "associative" -> Gramext.NonA
436       ]
437     ];
438     precedence: [
439       [ "with"; IDENT "precedence"; n = NUMBER -> int_of_string n ]
440     ];
441     notation: [
442       [ dir = OPT direction; s = QSTRING;
443         assoc = OPT associativity; prec = precedence;
444         IDENT "for";
445         p2 = 
446           [ blob = UNPARSED_AST ->
447               add_raw_attribute ~text:(Printf.sprintf "@{%s}" blob)
448                 (CicNotationParser.parse_level2_ast lstatus
449                   (Ulexing.from_utf8_string blob))
450           | blob = UNPARSED_META ->
451               add_raw_attribute ~text:(Printf.sprintf "${%s}" blob)
452                 (CicNotationParser.parse_level2_meta lstatus
453                   (Ulexing.from_utf8_string blob))
454           ] ->
455             let assoc =
456               match assoc with
457               | None -> default_associativity
458               | Some assoc -> assoc
459             in
460             let p1 =
461               add_raw_attribute ~text:s
462                 (CicNotationParser.parse_level1_pattern lstatus prec
463                   (Ulexing.from_utf8_string s))
464             in
465             (dir, p1, assoc, prec, p2)
466       ]
467     ];
468     level3_term: [
469       [ r = NREF -> N.NRefPattern (NReference.reference_of_string r)
470       | IMPLICIT -> N.ImplicitPattern
471       | id = IDENT -> N.VarPattern id
472       | LPAREN; terms = LIST1 SELF; RPAREN ->
473           (match terms with
474           | [] -> assert false
475           | [term] -> term
476           | terms -> N.ApplPattern terms)
477       ]
478     ];
479     interpretation: [
480       [ s = CSYMBOL; args = LIST0 argument; SYMBOL "="; t = level3_term ->
481           (s, args, t)
482       ]
483     ];
484     
485     include_command: [ [
486         IDENT "include" ; path = QSTRING -> 
487           loc,path,G.WithPreferences
488       | IDENT "include" ; IDENT "alias"; path = QSTRING -> 
489           loc,path,G.OnlyPreferences
490       | IDENT "include'" ; path = QSTRING -> 
491           loc,path,G.WithoutPreferences
492      ]];
493
494   index: [[ b = OPT SYMBOL "-" -> match b with None -> true | _ -> false ]];
495
496   grafite_ncommand: [ [
497       IDENT "qed" ;  i = index -> G.NQed (loc,i)
498     | IDENT "defined" ;  i = index -> G.NQed (loc,i) (* FG: presentational qed for definitions *)
499     | nflavour = ntheorem_flavour; name = IDENT; SYMBOL ":"; typ = term;
500       body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
501         let attrs = `Provided, nflavour, `Regular in
502         G.NObj (loc, N.Theorem (name, typ, body, attrs),true)
503     | nflavour = ntheorem_flavour; name = IDENT; SYMBOL <:unicode<def>> (* ≝ *);
504       body = term ->
505         let attrs = `Provided, nflavour, `Regular in
506         G.NObj (loc, 
507           N.Theorem(name, N.Implicit `JustOne, Some body, attrs),
508           true)
509     | IDENT "axiom"; i = index; name = IDENT; SYMBOL ":"; typ = term ->
510         let attrs = `Provided, `Axiom, `Regular in
511         G.NObj (loc, N.Theorem (name, typ, None, attrs),i)
512     | IDENT "discriminator" ; indty = tactic_term -> G.NDiscriminator (loc,indty)
513     | IDENT "inverter"; name = IDENT; IDENT "for" ; indty = tactic_term ;
514       paramspec = OPT inverter_param_list ; 
515       outsort = OPT [ SYMBOL ":" ; outsort = term -> outsort ] -> 
516         G.NInverter (loc,name,indty,paramspec,outsort)
517     | LETCOREC ; defs = let_codefs -> 
518         nmk_rec_corec `CoInductive defs loc true
519     | LETREC ; defs = let_defs -> 
520         nmk_rec_corec `Inductive defs loc true
521     | IDENT "inductive"; spec = inductive_spec ->
522         let (params, ind_types) = spec in
523         G.NObj (loc, N.Inductive (params, ind_types),true)
524     | IDENT "coinductive"; spec = inductive_spec ->
525         let (params, ind_types) = spec in
526         let ind_types = (* set inductive flags to false (coinductive) *)
527           List.map (fun (name, _, term, ctors) -> (name, false, term, ctors))
528             ind_types
529         in
530         G.NObj (loc, N.Inductive (params, ind_types),true)
531     | IDENT "universe"; IDENT "constraint"; u1 = tactic_term; 
532         SYMBOL <:unicode<lt>> ; u2 = tactic_term ->
533         let urify = function 
534           | NotationPt.AttributedTerm (_, NotationPt.Sort (`NType i)) ->
535               NUri.uri_of_string ("cic:/matita/pts/Type"^i^".univ")
536           | _ -> raise (Failure "only a Type[…] sort can be constrained")
537         in
538         let u1 = urify u1 in
539         let u2 = urify u2 in
540          G.NUnivConstraint (loc,u1,u2)
541     | IDENT "unification"; IDENT "hint"; n = int; t = tactic_term ->
542         G.UnificationHint (loc, t, n)
543     | IDENT "coercion"; name = IDENT;
544         compose = OPT [ IDENT "nocomposites" -> () ];
545         spec = OPT [ SYMBOL ":"; ty = term; 
546         SYMBOL <:unicode<def>>; t = term; "on"; 
547         id = [ IDENT | PIDENT ]; SYMBOL ":"; source = term;
548         "to"; target = term -> t,ty,(id,source),target ] ->
549           let compose = compose = None in
550           G.NCoercion(loc,name,compose,spec)
551     | IDENT "record" ; (params,name,ty,fields) = record_spec ->
552         G.NObj (loc, N.Record (params,name,ty,fields),true)
553     | IDENT "copy" ; s = IDENT; IDENT "from"; u = URI; "with"; 
554       m = LIST0 [ u1 = URI; SYMBOL <:unicode<mapsto>>; u2 = URI -> u1,u2 ] ->
555         G.NCopy (loc,s,NUri.uri_of_string u,
556           List.map (fun a,b -> NUri.uri_of_string a, NUri.uri_of_string b) m)
557     | lc = lexicon_command -> lc
558   ]];
559
560   lexicon_command: [ [
561       IDENT "alias" ; spec = alias_spec ->
562         G.Alias (loc, spec)
563     | IDENT "notation"; (dir, l1, assoc, prec, l2) = notation ->
564         G.Notation (loc, dir, l1, assoc, prec, l2)
565     | IDENT "interpretation"; id = QSTRING;
566       (symbol, args, l3) = interpretation ->
567         G.Interpretation (loc, id, (symbol, args), l3)
568   ]];
569   executable: [
570     [ ncmd = grafite_ncommand; SYMBOL "." -> G.NCommand (loc, ncmd)
571     | punct = npunctuation_tactical -> G.NTactic (loc, [punct])
572     | tac = nnon_punctuation_tactical(*; punct = npunctuation_tactical*) ->
573           G.NTactic (loc, [tac])
574     | tac = ntactic (*; punct = npunctuation_tactical*) ->
575          tac 
576 (*
577     | tac = nnon_punctuation_tactical; 
578         punct = npunctuation_tactical ->
579           G.NTactic (loc, [tac; punct])
580 *)
581     ]
582   ];
583   comment: [
584     [ BEGINCOMMENT ; ex = executable ; ENDCOMMENT -> 
585        G.Code (loc, ex)
586     | str = NOTE -> 
587        G.Note (loc, str)
588     ]
589   ];
590   statement: [
591     [ ex = executable -> G.Executable (loc, ex)
592     | com = comment -> G.Comment (loc, com)
593     | (iloc,fname,mode) = include_command ; SYMBOL "."  ->
594                G.Executable (loc,G.NCommand (loc,G.Include (iloc,mode,fname)))
595     | EOI -> raise End_of_file
596     ]
597   ];
598   END;
599 (* }}} *)
600   statement
601 ;;
602
603 type db = GrafiteAst.statement Grammar.Entry.e ;;
604
605 class type g_status =
606  object
607   inherit CicNotationParser.g_status
608   method parser_db: db
609  end
610
611 class virtual status =
612  object(self)
613   inherit CicNotationParser.status ~keywords:[]
614   val mutable db = None (* mutable only to initialize it :-( *)
615   method parser_db = match db with None -> assert false | Some x -> x
616   method set_parser_db v = {< db = Some v >}
617   method set_parser_status
618    : 'status. #g_status as 'status -> 'self
619    = fun o -> {< db = Some o#parser_db >}#set_notation_parser_status o
620   initializer
621    let grammar = CicNotationParser.level2_ast_grammar self in
622    db <- Some (mk_parser (Grammar.Entry.create grammar "statement") self)
623  end
624
625 let extend status l1 action = 
626   let status = CicNotationParser.extend status l1 action in
627   let grammar = CicNotationParser.level2_ast_grammar status in
628   status#set_parser_db
629     (mk_parser (Grammar.Entry.create grammar "statement") status)
630 ;;
631
632
633 let parse_statement status = 
634   parse_statement status#parser_db
635
636 (* vim:set foldmethod=marker: *)