]> matita.cs.unibo.it Git - helm.git/blob - matita/components/grafite_parser/grafiteParser.ml
Forward compatibility with new releases of Camlp5.
[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 (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 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    ]
257 ];
258   auto_params: [
259     [ params = 
260       LIST0 [
261          i = auto_fixed_param -> i,""
262        | i = auto_fixed_param ; SYMBOL "="; v = [ v = int ->
263               string_of_int v | v = IDENT -> v ] -> i,v ]; 
264       just = OPT [ IDENT "by"; by = 
265         [ univ = LIST0 tactic_term SEP SYMBOL "," -> `Univ univ
266         | SYMBOL "_" -> `Trace ] -> by ] -> just,params
267    ]
268 ];
269
270 (* MATITA 1.0
271   by_continuation: [
272     [ 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)
273     | WEPROVED; ty = tactic_term ; t1 = OPT [IDENT "that" ; IDENT "is" ; IDENT "equivalent" ; "to" ; t2 = tactic_term -> t2] ; 
274             "done" -> BYC_weproved (ty,None,t1)
275     | "done" -> BYC_done
276     | "let" ; id1 = IDENT ; SYMBOL ":" ; t1 = tactic_term ;
277       IDENT "such" ; IDENT "that" ; t2=tactic_term ; LPAREN ; 
278       id2 = IDENT ; RPAREN -> BYC_letsuchthat (id1,t1,id2,t2)
279     | WEHAVE; t1=tactic_term ; LPAREN ; id1=IDENT ; RPAREN ;"and" ; t2=tactic_term ; LPAREN ; id2=IDENT ; RPAREN ->
280               BYC_wehaveand (id1,t1,id2,t2)
281     ]
282 ];
283 *)
284 (* MATITA 1.0
285   rewriting_step_continuation : [
286     [ "done" -> true
287     | -> false
288     ]
289 ];
290 *)
291 (* MATITA 1.0
292   atomic_tactical:
293     [ "sequence" LEFTA
294       [ t1 = SELF; SYMBOL ";"; t2 = SELF ->
295           let ts =
296             match t1 with
297             | G.Seq (_, l) -> l @ [ t2 ]
298             | _ -> [ t1; t2 ]
299           in
300           G.Seq (loc, ts)
301       ]
302     | "then" NONA
303       [ tac = SELF; SYMBOL ";";
304         SYMBOL "["; tacs = LIST0 SELF SEP SYMBOL "|"; SYMBOL "]"->
305           (G.Then (loc, tac, tacs))
306       ]
307     | "loops" RIGHTA
308       [ IDENT "do"; count = int; tac = SELF ->
309           G.Do (loc, count, tac)
310       | IDENT "repeat"; tac = SELF -> G.Repeat (loc, tac)
311       ]
312     | "simple" NONA
313       [ IDENT "first";
314         SYMBOL "["; tacs = LIST0 SELF SEP SYMBOL "|"; SYMBOL "]"->
315           G.First (loc, tacs)
316       | IDENT "try"; tac = SELF -> G.Try (loc, tac)
317       | IDENT "solve";
318         SYMBOL "["; tacs = LIST0 SELF SEP SYMBOL "|"; SYMBOL "]"->
319           G.Solve (loc, tacs)
320       | IDENT "progress"; tac = SELF -> G.Progress (loc, tac)
321       | LPAREN; tac = SELF; RPAREN -> tac
322       | tac = tactic -> tac
323         ]
324       ];
325 *)
326   npunctuation_tactical:
327     [
328       [ SYMBOL "[" -> G.NBranch loc
329       | SYMBOL "|" -> G.NShift loc
330       | i = LIST1 int SEP SYMBOL ","; SYMBOL ":" -> G.NPos (loc, i)
331       | SYMBOL "*"; SYMBOL ":" -> G.NWildcard loc
332       | name = IDENT; SYMBOL ":" -> G.NPosbyname (loc, name)
333       | SYMBOL "]" -> G.NMerge loc
334       | SYMBOL ";" -> G.NSemicolon loc
335       | SYMBOL "." -> G.NDot loc
336       ]
337     ];
338   nnon_punctuation_tactical:
339     [ "simple" NONA
340       [ IDENT "focus"; goals = LIST1 int -> G.NFocus (loc, goals)
341       | IDENT "unfocus" -> G.NUnfocus loc
342       | IDENT "skip" -> G.NSkip loc
343       ]
344       ];
345   ntheorem_flavour: [
346     [ [ IDENT "definition"  ] -> `Definition
347     | [ IDENT "fact"        ] -> `Fact
348     | [ IDENT "lemma"       ] -> `Lemma
349     | [ IDENT "example"     ] -> `Example
350     | [ IDENT "theorem"     ] -> `Theorem
351     | [ IDENT "corollary"   ] -> `Corollary
352     ]
353   ];
354   inductive_spec: [ [
355     fst_name = IDENT; 
356       params = LIST0 protected_binder_vars;
357     SYMBOL ":"; fst_typ = term; SYMBOL <:unicode<def>>; OPT SYMBOL "|";
358     fst_constructors = LIST0 constructor SEP SYMBOL "|";
359     tl = OPT [ "with";
360         types = LIST1 [
361           name = IDENT; SYMBOL ":"; typ = term; SYMBOL <:unicode<def>>;
362          OPT SYMBOL "|"; constructors = LIST0 constructor SEP SYMBOL "|" ->
363             (name, true, typ, constructors) ] SEP "with" -> types
364       ] ->
365         let params =
366           List.fold_right
367             (fun (names, typ) acc ->
368               (List.map (fun name -> (name, typ)) names) @ acc)
369             params []
370         in
371         let fst_ind_type = (fst_name, true, fst_typ, fst_constructors) in
372         let tl_ind_types = match tl with None -> [] | Some types -> types in
373         let ind_types = fst_ind_type :: tl_ind_types in
374         (params, ind_types)
375     ] ];
376     
377     record_spec: [ [
378       name = IDENT; 
379       params = LIST0 protected_binder_vars;
380        SYMBOL ":"; typ = term; SYMBOL <:unicode<def>>; SYMBOL "{" ; 
381        fields = LIST0 [ 
382          name = IDENT ; 
383          coercion = [ 
384              SYMBOL ":" -> false,0 
385            | SYMBOL ":"; SYMBOL ">" -> true,0
386            | SYMBOL ":"; arity = int ; SYMBOL ">" -> true,arity
387          ]; 
388          ty = term -> 
389            let b,n = coercion in 
390            (name,ty,b,n) 
391        ] SEP SYMBOL ";"; SYMBOL "}" -> 
392         let params =
393           List.fold_right
394             (fun (names, typ) acc ->
395               (List.map (fun name -> (name, typ)) names) @ acc)
396             params []
397         in
398         (params,name,typ,fields)
399     ] ];
400
401     alias_spec: [
402       [ IDENT "id"; id = QSTRING; SYMBOL "="; uri = QSTRING ->
403         let alpha = "[a-zA-Z]" in
404         let num = "[0-9]+" in
405         let ident_cont = "\\("^alpha^"\\|"^num^"\\|_\\|\\\\\\)" in
406         let decoration = "\\'" in
407         let ident = "\\("^alpha^ident_cont^"*"^decoration^"*\\|_"^ident_cont^"+"^decoration^"*\\)" in
408         let rex = Str.regexp ("^"^ident^"$") in
409         if Str.string_match rex id 0 then
410           if (try ignore (NReference.reference_of_string uri); true
411               with NReference.IllFormedReference _ -> false)
412           then
413             G.Ident_alias (id, uri)
414           else
415             raise
416              (HExtlib.Localized (loc, CicNotationParser.Parse_error (Printf.sprintf "Not a valid uri: %s" uri)))
417         else
418           raise (HExtlib.Localized (loc, CicNotationParser.Parse_error (
419             Printf.sprintf "Not a valid identifier: %s" id)))
420       | IDENT "symbol"; symbol = QSTRING;
421         instance = OPT [ LPAREN; IDENT "instance"; n = int; RPAREN -> n ];
422         SYMBOL "="; dsc = QSTRING ->
423           let instance =
424             match instance with Some i -> i | None -> 0
425           in
426           G.Symbol_alias (symbol, instance, dsc)
427       | IDENT "num";
428         instance = OPT [ LPAREN; IDENT "instance"; n = int; RPAREN -> n ];
429         SYMBOL "="; dsc = QSTRING ->
430           let instance =
431             match instance with Some i -> i | None -> 0
432           in
433           G.Number_alias (instance, dsc)
434       ]
435      ];
436     argument: [
437       [ l = LIST0 [ SYMBOL <:unicode<eta>> (* η *); SYMBOL "." -> () ];
438         id = IDENT ->
439           N.IdentArg (List.length l, id)
440       ]
441     ];
442     associativity: [
443       [ IDENT "left";  IDENT "associative" -> Gramext.LeftA
444       | IDENT "right"; IDENT "associative" -> Gramext.RightA
445       | IDENT "non"; IDENT "associative" -> Gramext.NonA
446       ]
447     ];
448     precedence: [
449       [ "with"; IDENT "precedence"; n = NUMBER -> int_of_string n ]
450     ];
451     notation: [
452       [ dir = OPT direction; s = QSTRING;
453         assoc = OPT associativity; prec = precedence;
454         IDENT "for";
455         p2 = 
456           [ blob = UNPARSED_AST ->
457               add_raw_attribute ~text:(Printf.sprintf "@{%s}" blob)
458                 (CicNotationParser.parse_level2_ast lstatus
459                   (Ulexing.from_utf8_string blob))
460           | blob = UNPARSED_META ->
461               add_raw_attribute ~text:(Printf.sprintf "${%s}" blob)
462                 (CicNotationParser.parse_level2_meta lstatus
463                   (Ulexing.from_utf8_string blob))
464           ] ->
465             let assoc =
466               match assoc with
467               | None -> default_associativity
468               | Some assoc -> assoc
469             in
470             let p1 =
471               add_raw_attribute ~text:s
472                 (CicNotationParser.parse_level1_pattern lstatus prec
473                   (Ulexing.from_utf8_string s))
474             in
475             (dir, p1, assoc, prec, p2)
476       ]
477     ];
478     level3_term: [
479       [ r = NREF -> N.NRefPattern (NReference.reference_of_string r)
480       | IMPLICIT -> N.ImplicitPattern
481       | id = IDENT -> N.VarPattern id
482       | LPAREN; terms = LIST1 SELF; RPAREN ->
483           (match terms with
484           | [] -> assert false
485           | [term] -> term
486           | terms -> N.ApplPattern terms)
487       ]
488     ];
489     interpretation: [
490       [ s = CSYMBOL; args = LIST0 argument; SYMBOL "="; t = level3_term ->
491           (s, args, t)
492       ]
493     ];
494     
495     include_command: [ [
496         IDENT "include" ; path = QSTRING -> 
497           loc,path,G.WithPreferences
498       | IDENT "include" ; IDENT "alias"; path = QSTRING -> 
499           loc,path,G.OnlyPreferences
500       | IDENT "include'" ; path = QSTRING -> 
501           loc,path,G.WithoutPreferences
502      ]];
503
504   index: [[ b = OPT SYMBOL "-" -> match b with None -> true | _ -> false ]];
505
506   grafite_ncommand: [ [
507       IDENT "qed" ;  i = index -> G.NQed (loc,i)
508     | nflavour = ntheorem_flavour; name = IDENT; SYMBOL ":"; typ = term;
509       body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
510         G.NObj (loc, N.Theorem (nflavour, name, typ, body,`Regular),true)
511     | nflavour = ntheorem_flavour; name = IDENT; SYMBOL <:unicode<def>> (* ≝ *);
512       body = term ->
513         G.NObj (loc, 
514           N.Theorem(nflavour, name, N.Implicit `JustOne, Some body,`Regular),
515           true)
516     | IDENT "axiom"; i = index; name = IDENT; SYMBOL ":"; typ = term ->
517         G.NObj (loc, N.Theorem (`Axiom, name, typ, None, `Regular),i)
518     | IDENT "discriminator" ; indty = tactic_term -> G.NDiscriminator (loc,indty)
519     | IDENT "inverter"; name = IDENT; IDENT "for" ; indty = tactic_term ;
520       paramspec = OPT inverter_param_list ; 
521       outsort = OPT [ SYMBOL ":" ; outsort = term -> outsort ] -> 
522         G.NInverter (loc,name,indty,paramspec,outsort)
523     | LETCOREC ; defs = let_codefs -> 
524         nmk_rec_corec `CoInductive defs loc true
525     | LETREC ; defs = let_defs -> 
526         nmk_rec_corec `Inductive defs loc true
527     | IDENT "inductive"; spec = inductive_spec ->
528         let (params, ind_types) = spec in
529         G.NObj (loc, N.Inductive (params, ind_types),true)
530     | IDENT "coinductive"; spec = inductive_spec ->
531         let (params, ind_types) = spec in
532         let ind_types = (* set inductive flags to false (coinductive) *)
533           List.map (fun (name, _, term, ctors) -> (name, false, term, ctors))
534             ind_types
535         in
536         G.NObj (loc, N.Inductive (params, ind_types),true)
537     | IDENT "universe"; IDENT "constraint"; u1 = tactic_term; 
538         SYMBOL <:unicode<lt>> ; u2 = tactic_term ->
539         let urify = function 
540           | NotationPt.AttributedTerm (_, NotationPt.Sort (`NType i)) ->
541               NUri.uri_of_string ("cic:/matita/pts/Type"^i^".univ")
542           | _ -> raise (Failure "only a Type[…] sort can be constrained")
543         in
544         let u1 = urify u1 in
545         let u2 = urify u2 in
546          G.NUnivConstraint (loc,u1,u2)
547     | IDENT "unification"; IDENT "hint"; n = int; t = tactic_term ->
548         G.UnificationHint (loc, t, n)
549     | IDENT "coercion"; name = IDENT;
550         compose = OPT [ IDENT "nocomposites" -> () ];
551         spec = OPT [ SYMBOL ":"; ty = term; 
552         SYMBOL <:unicode<def>>; t = term; "on"; 
553         id = [ IDENT | PIDENT ]; SYMBOL ":"; source = term;
554         "to"; target = term -> t,ty,(id,source),target ] ->
555           let compose = compose = None in
556           G.NCoercion(loc,name,compose,spec)
557     | IDENT "record" ; (params,name,ty,fields) = record_spec ->
558         G.NObj (loc, N.Record (params,name,ty,fields),true)
559     | IDENT "copy" ; s = IDENT; IDENT "from"; u = URI; "with"; 
560       m = LIST0 [ u1 = URI; SYMBOL <:unicode<mapsto>>; u2 = URI -> u1,u2 ] ->
561         G.NCopy (loc,s,NUri.uri_of_string u,
562           List.map (fun a,b -> NUri.uri_of_string a, NUri.uri_of_string b) m)
563     | lc = lexicon_command -> lc
564   ]];
565
566   lexicon_command: [ [
567       IDENT "alias" ; spec = alias_spec ->
568         G.Alias (loc, spec)
569     | IDENT "notation"; (dir, l1, assoc, prec, l2) = notation ->
570         G.Notation (loc, dir, l1, assoc, prec, l2)
571     | IDENT "interpretation"; id = QSTRING;
572       (symbol, args, l3) = interpretation ->
573         G.Interpretation (loc, id, (symbol, args), l3)
574   ]];
575   executable: [
576     [ ncmd = grafite_ncommand; SYMBOL "." -> G.NCommand (loc, ncmd)
577     | punct = npunctuation_tactical -> G.NTactic (loc, [punct])
578     | tac = nnon_punctuation_tactical(*; punct = npunctuation_tactical*) ->
579           G.NTactic (loc, [tac])
580     | tac = ntactic (*; punct = npunctuation_tactical*) ->
581          tac 
582 (*
583     | tac = nnon_punctuation_tactical; 
584         punct = npunctuation_tactical ->
585           G.NTactic (loc, [tac; punct])
586 *)
587     ]
588   ];
589   comment: [
590     [ BEGINCOMMENT ; ex = executable ; ENDCOMMENT -> 
591        G.Code (loc, ex)
592     | str = NOTE -> 
593        G.Note (loc, str)
594     ]
595   ];
596   statement: [
597     [ ex = executable -> G.Executable (loc, ex)
598     | com = comment -> G.Comment (loc, com)
599     | (iloc,fname,mode) = include_command ; SYMBOL "."  ->
600                G.Executable (loc,G.NCommand (loc,G.Include (iloc,mode,fname)))
601     | EOI -> raise End_of_file
602     ]
603   ];
604   END;
605 (* }}} *)
606   statement
607 ;;
608
609 type db = GrafiteAst.statement Grammar.Entry.e ;;
610
611 class type g_status =
612  object
613   inherit CicNotationParser.g_status
614   method parser_db: db
615  end
616
617 class virtual status =
618  object(self)
619   inherit CicNotationParser.status ~keywords:[]
620   val mutable db = None (* mutable only to initialize it :-( *)
621   method parser_db = match db with None -> assert false | Some x -> x
622   method set_parser_db v = {< db = Some v >}
623   method set_parser_status
624    : 'status. #g_status as 'status -> 'self
625    = fun o -> {< db = Some o#parser_db >}#set_notation_parser_status o
626   initializer
627    let grammar = CicNotationParser.level2_ast_grammar self in
628    db <- Some (mk_parser (Grammar.Entry.create grammar "statement") self)
629  end
630
631 let extend status l1 action = 
632   let status = CicNotationParser.extend status l1 action in
633   let grammar = CicNotationParser.level2_ast_grammar status in
634   status#set_parser_db
635     (mk_parser (Grammar.Entry.create grammar "statement") status)
636 ;;
637
638
639 let parse_statement status = 
640   parse_statement status#parser_db
641
642 (* vim:set foldmethod=marker: *)