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