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