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