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