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