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