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