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