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