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