]> matita.cs.unibo.it Git - helm.git/blob - matitaB/components/content_pres/cicNotationParser.ml
fcd209307fd73e6c1202e3439a6791ef968d24c2
[helm.git] / matitaB / components / content_pres / cicNotationParser.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 open Printf
29
30 module Ast = NotationPt
31 module Env = NotationEnv
32
33 module StringSet = Set.Make(String)
34
35 (* let prerr_endline _ = () *)
36
37 exception Parse_error of string
38 exception Level_not_found of int
39
40 let min_precedence = 0
41 let max_precedence = 100
42
43 type ('a,'b,'c,'d,'e) grammars = {
44   level1_pattern: 'a Grammar.Entry.e;
45   level2_ast: 'b Grammar.Entry.e;
46   level2_ast_grammar : Grammar.g;
47   term: 'b Grammar.Entry.e;
48   ident: 'e Grammar.Entry.e;
49   sym_attributes: (string option * string option) Grammar.Entry.e;
50   sym_table: ([ `Sym of string | `Kwd of string ] * Stdpp.location Grammar.Entry.e) list;
51   let_defs: 'c Grammar.Entry.e;
52   let_codefs: 'c Grammar.Entry.e;
53   protected_binder_vars: 'd Grammar.Entry.e;
54   level2_meta: 'b Grammar.Entry.e;
55 }
56
57 type checked_l1_pattern = CL1P of NotationPt.term * int
58
59 let refresh_uri_in_checked_l1_pattern ~refresh_uri_in_term
60      ~refresh_uri_in_reference (CL1P (t,n))
61 =
62  CL1P (NotationUtil.refresh_uri_in_term ~refresh_uri_in_term
63  ~refresh_uri_in_reference t, n)
64
65 type binding =
66   | NoBinding
67   | Binding of string * Env.value_type
68   | Env of (string * Env.value_type) list
69
70 type db = {
71   grammars: 
72     (int -> NotationPt.term, 
73     Ast.term,
74     (Ast.term Ast.capture_variable list *
75       Ast.term Ast.capture_variable * Ast.term * int) list, 
76     Ast.term list * Ast.term option, Env.ident_or_var) grammars;
77   keywords: string list;
78   items: (string * Ast.term * (NotationEnv.t -> Ast.location -> Ast.term)) list;
79   loctable: (string option * string option) CicNotationLexer.LocalizeEnv.t ref
80 }
81
82 let int_of_string s =
83   try
84     Pervasives.int_of_string s
85   with Failure _ ->
86     failwith (sprintf "Lexer failure: string_of_int \"%s\" failed" s)
87
88 (** {2 Grammar extension} *)
89
90 let level_of precedence =
91   if precedence < min_precedence || precedence > max_precedence then
92     raise (Level_not_found precedence);
93   string_of_int precedence 
94
95 let add_item_to_grammar_explicit level2_ast_grammar 
96     sym_attributes sym_table item = 
97   let stok, nonterm = match item with
98   | `Sym sy -> Gramext.Stoken ("SYMBOL",sy), "sym" ^ sy
99   | `Kwd i -> Gramext.Stoken("",i), "kwd" ^ i
100   in
101   try
102     let _ = List.assoc item sym_table
103     in sym_table
104   with Not_found -> 
105 (*    let entry = Grammar.Entry.create level2_ast_grammar ("sym" ^ s) in *)
106     let entry = Grammar.Entry.create level2_ast_grammar nonterm in 
107     Grammar.extend
108     [ Grammar.Entry.obj entry,
109       None,
110       [ None,
111         Some (*Gramext.NonA*) Gramext.NonA,
112         [ [stok], (* concrete l1 syntax *) 
113           (Gramext.action (fun _ loc -> (* None, *) loc))
114 (*        ; [Gramext.Stoken ("ATAG","")
115           ;Gramext.Snterm (Grammar.Entry.obj sym_attributes)
116           ;Gramext.Stoken ("SYMBOL","\005")
117           ;stok
118           ;Gramext.Stoken ("ATAGEND","")],
119           (Gramext.action (fun _ uridesc _ _ _ loc -> (Some uridesc),loc)) *)
120         ]]];
121 (*  prerr_endline ("adding to grammar symbol " ^ s); *)
122   (item,entry)::sym_table
123  
124
125 let add_item_to_grammar status item =
126   let sym_attributes = status#notation_parser_db.grammars.sym_attributes in
127   let sym_table = status#notation_parser_db.grammars.sym_table in
128   let level2_ast_grammar =
129     status#notation_parser_db.grammars.level2_ast_grammar
130   in
131   let sym_table = 
132     add_item_to_grammar_explicit level2_ast_grammar sym_attributes sym_table
133       item
134   in
135   let grammars =
136     { status#notation_parser_db.grammars with sym_table = sym_table }
137   in
138   let notation_parser_db =
139     { status#notation_parser_db with grammars = grammars } in
140   status#set_notation_parser_db notation_parser_db
141
142 let gram_symbol status s =
143   let sym_table = status#notation_parser_db.grammars.sym_table in
144   let entry =
145     try List.assoc (`Sym s) sym_table
146     with Not_found ->
147      (let syms = List.map fst (status#notation_parser_db.grammars.sym_table) in
148       let syms = List.map (fun x -> match x with `Sym x | `Kwd x -> "\"" ^ x ^ "\"") syms in
149       prerr_endline ("new symbol/keyword non-terminals: " ^ (String.concat ", " syms));
150       prerr_endline ("unable to find symbol \"" ^ s ^ "\""); 
151       (* XXX: typing error
152        * Gramext.Stoken("SYMBOL", s) *)
153        assert false)
154   in
155   Gramext.Snterm (Grammar.Entry.obj entry)
156
157 let gram_ident status = 
158   Gramext.Snterm (Grammar.Entry.obj
159   (status#notation_parser_db.grammars.ident : 'a Grammar.Entry.e))
160   (*Gramext.Stoken ("IDENT", s)*)
161 let gram_number s = Gramext.Stoken ("NUMBER", s)
162 let gram_keyword status s =
163   let sym_table = status#notation_parser_db.grammars.sym_table in
164     try Gramext.Snterm (Grammar.Entry.obj 
165           (List.assoc (`Kwd s) sym_table))
166     with Not_found ->
167      (let syms = List.map fst (status#notation_parser_db.grammars.sym_table) in
168       let syms = List.map (fun x -> match x with `Sym x | `Kwd x -> "\"" ^ x ^ "\"") syms in
169       (* prerr_endline ("new symbol/keyword non-terminals: " ^ (String.concat ", " syms));
170         prerr_endline ("unable to find keyword \"" ^ s ^ "\""); *)
171       Gramext.Stoken("", s))
172   
173 let gram_term status = function
174   | Ast.Self _ -> Gramext.Sself
175   | Ast.Level precedence ->
176       Gramext.Snterml 
177         (Grammar.Entry.obj 
178           (status#notation_parser_db.grammars.term : 'a Grammar.Entry.e), 
179          level_of precedence)
180 ;;
181
182 let gram_of_literal status =
183   function
184   | `Symbol (s,_) -> gram_symbol status s
185   | `Keyword (s,_) -> gram_keyword status s
186   | `Number (s,_) -> gram_number s
187
188 let make_action status action bindings =
189   let rec aux (vl : NotationEnv.t) =
190     function
191       [] -> Gramext.action (fun (loc: Ast.location) -> action vl loc)
192     | NoBinding :: tl -> 
193         Gramext.action 
194          (fun ((*_,*)(loc: Ast.location)) ->
195            let uri,desc = 
196              try
197                CicNotationLexer.LocalizeEnv.find loc
198                  !(status#notation_parser_db.loctable)
199              with Not_found -> None, None
200            in aux (("",(Env.NoType,
201                Env.DisambiguationValue (loc,uri,desc)))::vl) tl)
202     (* LUCA: DEFCON 3 BEGIN *)
203     | Binding (name, Env.TermType l) :: tl ->
204         Gramext.action
205           (fun (v:Ast.term) ->
206             aux ((name, (Env.TermType l, Env.TermValue v))::vl) tl)
207     | Binding (name, Env.StringType) :: tl ->
208         Gramext.action
209           (fun (v:Env.ident_or_var) ->
210             aux ((name, (Env.StringType, Env.StringValue v)) :: vl) tl)
211     | Binding (name, Env.NumType) :: tl ->
212         Gramext.action
213           (fun (v:string) ->
214             aux ((name, (Env.NumType, Env.NumValue v)) :: vl) tl)
215     | Binding (name, Env.OptType t) :: tl ->
216         Gramext.action
217           (fun (v:'a option) ->
218             aux ((name, (Env.OptType t, Env.OptValue v)) :: vl) tl)
219     | Binding (name, Env.ListType t) :: tl ->
220         Gramext.action
221           (fun (v:'a list) ->
222             aux ((name, (Env.ListType t, Env.ListValue v)) :: vl) tl)
223     | Env _ :: tl ->
224         Gramext.action (fun (v:NotationEnv.t) -> aux (v @ vl) tl)
225     | _ (* Binding (_,NoType) *) -> assert false
226     (* LUCA: DEFCON 3 END *)
227   in
228     aux [] (List.rev bindings)
229
230 let flatten_opt =
231   let rec aux acc =
232     function
233       [] -> List.rev acc
234     | NoBinding :: tl -> aux acc tl
235     | Env names :: tl -> aux (List.rev names @ acc) tl
236     | Binding (name, ty) :: tl -> aux ((name, ty) :: acc) tl
237   in
238   aux []
239
240 (* given a level 1 pattern, adds productions for symbols when needed *)
241 let update_sym_grammar status pattern =
242   let rec aux status = function
243     | Ast.AttributedTerm (_, t) -> aux status t
244     | Ast.Literal l -> aux_literal status l
245     | Ast.Layout l -> aux_layout status l
246     | Ast.Magic m -> aux_magic status m
247     | Ast.Variable v -> aux_variable status v
248     | t ->
249         prerr_endline (NotationPp.pp_term status t);
250         assert false
251   and aux_literal status =
252     function
253     | `Symbol (s,_) -> add_item_to_grammar status (`Sym s)
254     | `Keyword (s,_) ->  
255          if not (List.mem s CicNotationLexer.initial_keywords)
256             then add_item_to_grammar status (`Kwd s)
257             else status
258     | `Number _ -> status
259   and aux_layout status = function
260     | Ast.Sub (p1, p2) -> aux (aux status p1) p2
261     | Ast.Sup (p1, p2) -> aux (aux status p1) p2
262     | Ast.Below (p1, p2) -> aux (aux status p1) p2
263     | Ast.Above (p1, p2) -> aux (aux status p1) p2
264     | Ast.Frac (p1, p2) -> aux (aux status p1) p2
265     | Ast.InfRule (p1, p2, p3) -> aux (aux (aux status p1) p2) p3
266     | Ast.Atop (p1, p2) -> aux (aux status p1) p2
267     | Ast.Over (p1, p2) -> aux (aux status p1) p2
268     | Ast.Root (p1, p2) -> aux (aux status p1) p2
269     | Ast.Sqrt p -> aux status p
270     | Ast.Break -> status
271     | Ast.Box (_, pl) -> List.fold_left aux status pl
272     | Ast.Group pl -> List.fold_left aux status pl
273     | Ast.Mstyle (_,pl) -> List.fold_left aux status pl
274     | Ast.Mpadded (_,pl) -> List.fold_left aux status pl
275     | Ast.Maction l -> List.fold_left aux status l
276   and aux_magic status magic =
277     match magic with
278     | Ast.Opt p -> aux status p
279     | Ast.List0 (p, s)
280     | Ast.List1 (p, s) ->
281         let status = 
282           match s with None -> status | Some s' -> aux_literal status s'
283         in
284         aux status p
285     | _ -> assert false
286   and aux_variable status _ = status
287   in
288   aux status pattern
289
290   (* given a level 1 pattern computes the new RHS of "term" grammar entry *)
291 let extract_term_production status pattern =
292   let rec aux = function
293     | Ast.AttributedTerm (_, t) -> aux t
294     | Ast.Literal l -> aux_literal l
295     | Ast.Layout l -> aux_layout l
296     | Ast.Magic m -> aux_magic m
297     | Ast.Variable v -> aux_variable v
298     | t ->
299         prerr_endline (NotationPp.pp_term status t);
300         assert false
301   and aux_literal =
302     function
303     | `Symbol (s,_) -> [NoBinding, gram_symbol status s]
304     | `Keyword (s,_) ->
305         (* assumption: s will be registered as a keyword with the lexer *)
306         [NoBinding, gram_keyword status s]
307     | `Number (s,_) -> [NoBinding, gram_number s]
308   and aux_layout = function
309     | Ast.Sub (p1, p2) -> aux p1 @ [NoBinding, gram_symbol status "\\sub "] @ aux p2
310     | Ast.Sup (p1, p2) -> aux p1 @ [NoBinding, gram_symbol status "\\sup "] @ aux p2
311     | Ast.Below (p1, p2) -> aux p1 @ [NoBinding, gram_symbol status "\\below "] @ aux p2
312     | Ast.Above (p1, p2) -> aux p1 @ [NoBinding, gram_symbol status "\\above "] @ aux p2
313     | Ast.Frac (p1, p2) -> aux p1 @ [NoBinding, gram_symbol status "\\frac "] @ aux p2
314     | Ast.InfRule (p1, p2, p3) -> [NoBinding, gram_symbol status "\\infrule "] @ aux p1 @ aux p2 @ aux p3
315     | Ast.Atop (p1, p2) -> aux p1 @ [NoBinding, gram_symbol status "\\atop "] @ aux p2
316     | Ast.Over (p1, p2) -> aux p1 @ [NoBinding, gram_symbol status "\\over "] @ aux p2
317     | Ast.Root (p1, p2) ->
318         [NoBinding, gram_symbol status "\\root "] @ aux p2
319         @ [NoBinding, gram_symbol status "\\of "] @ aux p1
320     | Ast.Sqrt p -> [NoBinding, gram_symbol status "\\sqrt "] @ aux p
321     | Ast.Break -> []
322     | Ast.Box (_, pl) -> List.flatten (List.map aux pl)
323     | Ast.Group pl -> List.flatten (List.map aux pl)
324     | Ast.Mstyle (_,pl) -> List.flatten (List.map aux pl)
325     | Ast.Mpadded (_,pl) -> List.flatten (List.map aux pl)
326     | Ast.Maction l -> List.flatten (List.map aux l)
327   and aux_magic magic =
328     match magic with
329     | Ast.Opt p ->
330         let p_bindings, p_atoms, p_names, p_action = inner_pattern p in
331         let action (env_opt : NotationEnv.t option) (loc : Ast.location) =
332           match env_opt with
333           | Some env -> List.map Env.opt_binding_some env
334           | None -> List.map Env.opt_binding_of_name p_names
335         in
336         [ Env (List.map Env.opt_declaration p_names),
337           Gramext.srules
338             [ [ Gramext.Sopt (Gramext.srules [ p_atoms, p_action ]) ],
339               Gramext.action action ] ]
340     | Ast.List0 (p, _)
341     | Ast.List1 (p, _) ->
342         let p_bindings, p_atoms, p_names, p_action = inner_pattern p in
343         let action (env_list : NotationEnv.t list) (loc : Ast.location) =
344           NotationEnv.coalesce_env p_names env_list
345         in
346         let gram_of_list s =
347           match magic with
348           | Ast.List0 (_, None) -> Gramext.Slist0 s
349           | Ast.List1 (_, None) -> Gramext.Slist1 s
350           | Ast.List0 (_, Some l) -> 
351               Gramext.Slist0sep (s, gram_of_literal status l,true)
352           | Ast.List1 (_, Some l) -> 
353               Gramext.Slist1sep (s, gram_of_literal status l,true)
354           | _ -> assert false
355         in
356         [ Env (List.map Env.list_declaration p_names),
357           Gramext.srules
358             [ [ gram_of_list (Gramext.srules [ p_atoms, p_action ]) ],
359               Gramext.action action ] ]
360     | _ -> assert false
361   and aux_variable =
362     function
363     | Ast.NumVar s -> [Binding (s, Env.NumType), gram_number ""]
364     | Ast.TermVar (s,(Ast.Self level|Ast.Level level as lv)) -> 
365         [Binding (s, Env.TermType level), gram_term status lv]
366     | Ast.IdentVar s -> [Binding (s, Env.StringType), gram_ident status]
367     | Ast.Ascription (p, s) -> assert false (* TODO *)
368     | Ast.FreshVar _ -> assert false
369   and inner_pattern p =
370     let p_bindings, p_atoms = List.split (aux p) in
371     let p_names = flatten_opt p_bindings in
372     let action =
373       make_action status 
374         (fun (env : NotationEnv.t) (loc : Ast.location) -> env) p_bindings
375     in
376     p_bindings, p_atoms, p_names, action
377   in
378   aux pattern
379
380 type rule_id = Grammar.token Gramext.g_symbol list
381
382 let compare_rule_id x y =
383   let rec aux = function
384     | [],[] -> 0
385     | [],_ -> ~-1
386     | _,[] -> 1
387     | ((s1::tl1) as x),((s2::tl2) as y) ->
388         if Gramext.eq_symbol s1 s2 then aux (tl1,tl2)
389         else Pervasives.compare x y 
390   in
391     aux (x,y)
392
393
394 let check_l1_pattern level1_pattern pponly level associativity =
395   let variables = ref 0 in
396   let symbols = ref 0 in
397   let rec aux = function
398     | Ast.AttributedTerm (att, t) -> Ast.AttributedTerm (att,aux t)
399     | Ast.Literal _ as l -> incr symbols; l
400     | Ast.Layout l -> Ast.Layout (aux_layout l)
401     | Ast.Magic m -> Ast.Magic (aux_magic m)
402     | Ast.Variable v -> (aux_variable v)
403     | t -> assert false
404   and aux_layout = function
405     | Ast.Sub (p1, p2)   -> let p1 = aux p1 in let p2 = aux p2 in Ast.Sub (p1, p2)
406     | Ast.Sup (p1, p2)   -> let p1 = aux p1 in let p2 = aux p2 in Ast.Sup (p1, p2)
407     | Ast.Below (p1, p2) -> let p1 = aux p1 in let p2 = aux p2 in Ast.Below (p1, p2)
408     | Ast.Above (p1, p2) -> let p1 = aux p1 in let p2 = aux p2 in Ast.Above (p1, p2)
409     | Ast.Frac (p1, p2)  -> let p1 = aux p1 in let p2 = aux p2 in Ast.Frac (p1, p2)
410     | Ast.InfRule (p1, p2, p3)  -> let p1 = aux p1 in let p2 = aux p2 in let p3 = aux p3 in Ast.InfRule (p1, p2, p3)
411     | Ast.Atop (p1, p2)  -> let p1 = aux p1 in let p2 = aux p2 in Ast.Atop (p1, p2)
412     | Ast.Over (p1, p2)  -> let p1 = aux p1 in let p2 = aux p2 in Ast.Over (p1, p2)
413     | Ast.Root (p1, p2)  -> let p1 = aux p1 in let p2 = aux p2 in Ast.Root (p1, p2)
414     | Ast.Sqrt p -> Ast.Sqrt (aux p)
415     | Ast.Break as t -> t 
416     | Ast.Box (b, pl) -> Ast.Box(b, List.map aux pl)
417     | Ast.Group pl -> Ast.Group (List.map aux pl)
418     | Ast.Mstyle (l,pl) -> Ast.Mstyle (l, List.map aux pl)
419     | Ast.Mpadded (l,pl) -> Ast.Mpadded (l, List.map aux pl)
420     | Ast.Maction l as t -> 
421         if not pponly then 
422         raise(Parse_error("Maction can be used only in output notations")) 
423         else t
424   and aux_magic magic =
425     match magic with
426     | Ast.Opt p -> Ast.Opt (aux p)
427     | Ast.List0 (p, x) -> Ast.List0 (aux p, x)
428     | Ast.List1 (p, x) -> Ast.List1 (aux p, x)
429     | _ -> assert false
430   and aux_variable =
431     function
432     | Ast.NumVar _ as t -> Ast.Variable t
433     | Ast.TermVar (s,Ast.Self _) when associativity <> Gramext.NonA -> 
434         incr variables; 
435         if !variables > 2 then
436           raise (Parse_error ("Exactly 2 variables must be specified in an "^
437           "associative notation"));
438         (match !variables, associativity with
439         | 1,Gramext.LeftA -> 
440              Ast.Variable (Ast.TermVar (s, Ast.Self level))
441         | 1,Gramext.RightA -> 
442              Ast.Variable (Ast.TermVar (s, Ast.Self (level+1)))
443         | 2,Gramext.LeftA ->
444              Ast.Variable (Ast.TermVar (s, Ast.Self (level+1)))
445         | 2,Gramext.RightA -> 
446              Ast.Variable (Ast.TermVar (s, Ast.Level (level-1)))
447         | _ -> assert false)
448     | Ast.TermVar (s,Ast.Level _) when associativity <> Gramext.NonA -> 
449           raise (Parse_error ("Variables can not be declared with a " ^ 
450             "precedence in an associative notation"))
451        (*avoid camlp5 divergence due to non-Sself recursion at the same level *)
452     | Ast.TermVar (s,Ast.Level l) when l<=level && !variables=0 && !symbols=0-> 
453        raise(Parse_error("Left recursive rule with precedence not greater " ^
454         "than " ^ string_of_int level ^ " is not allowed to avoid divergence"))
455     | Ast.TermVar _ as t -> incr variables; Ast.Variable t
456     | Ast.IdentVar _ as t -> Ast.Variable t
457     | Ast.Ascription _ -> assert false (* TODO *)
458     | Ast.FreshVar _ -> assert false
459   in
460   if associativity <> Gramext.NonA && level = min_precedence then
461     raise (Parse_error ("You can not specify an associative notation " ^
462     "at level "^string_of_int min_precedence ^ "; increase it"));
463   let cp = aux level1_pattern in
464 (*   prerr_endline ("checked_pattern: " ^ NotationPp.pp_term cp); *)
465   if !variables <> 2 && associativity <> Gramext.NonA then
466     raise (Parse_error ("Exactly 2 variables must be specified in an "^
467      "associative notation"));
468   CL1P (cp,level)
469 ;;
470
471 (** {2 Grammar} *)
472
473 let fold_cluster binder terms ty body =
474   List.fold_right
475     (fun term body -> Ast.Binder (binder, (term, ty), body))
476     terms body  (* terms are names: either Ident or FreshVar *)
477
478 let fold_exists terms ty body =
479   List.fold_right
480     (fun term body ->
481       let lambda = Ast.Binder (`Lambda, (term, ty), body) in
482       Ast.Appl [ Ast.Symbol ("exists", None); lambda ])
483     terms body
484
485 let fold_binder binder pt_names body =
486   List.fold_right
487     (fun (names, ty) body -> fold_cluster binder names ty body)
488     pt_names body
489
490 let return_term loc term = Ast.AttributedTerm (`Loc loc, term)
491 let return_term_of_level loc term l = 
492   Ast.AttributedTerm (`Loc loc, term l)
493
494 (** {2 API implementation} *)
495
496 let exc_located_wrapper f =
497   try
498     f ()
499   with
500   | Stdpp.Exc_located (floc, Stream.Error msg) ->
501       raise (HExtlib.Localized (floc, Parse_error msg))
502   | Stdpp.Exc_located (floc, HExtlib.Localized (_,exn)) ->
503       raise (HExtlib.Localized (floc, (Parse_error (Printexc.to_string exn))))
504   | Stdpp.Exc_located (floc, exn) ->
505       raise (HExtlib.Localized (floc, (Parse_error (Printexc.to_string exn))))
506
507 let parse_level1_pattern grammars precedence lexbuf =
508   exc_located_wrapper
509     (fun () -> Grammar.Entry.parse grammars.level1_pattern (Obj.magic lexbuf) precedence)
510
511 let parse_level2_ast grammars lexbuf =
512   exc_located_wrapper
513     (fun () -> 
514             Grammar.Entry.parse grammars.level2_ast (Obj.magic lexbuf))
515
516 let parse_level2_meta grammars lexbuf =
517   exc_located_wrapper
518     (fun () -> Grammar.Entry.parse grammars.level2_meta (Obj.magic lexbuf))
519
520   (* create empty precedence level for "term" *)
521 let initialize_grammars loctable grammars =
522   let dummy_action =
523     Gramext.action (fun _ ->
524       failwith "internal error, lexer generated a dummy token")
525   in
526   (* Needed since campl4 on "delete_rule" remove the precedence level if it gets
527    * empty after the deletion. The lexer never generate the Stoken below. *)
528   let dummy_prod = [ [ Gramext.Stoken ("DUMMY", "") ], dummy_action ] in
529   let mk_level_list first last =
530     let rec aux acc = function
531       | i when i < first -> acc
532       | i ->
533           aux
534             ((Some (level_of i), Some Gramext.NonA, dummy_prod)
535              :: acc)
536             (i - 1)
537     in
538     aux [] last
539   in
540   Grammar.extend
541     [ Grammar.Entry.obj (grammars.term: 'a Grammar.Entry.e),
542       None,
543       mk_level_list min_precedence max_precedence ];
544 (* {{{ Grammar for concrete syntax patterns, notation level 1 *)
545   begin
546   let level1_pattern = grammars.level1_pattern in
547 EXTEND
548   GLOBAL: level1_pattern;
549
550   level1_pattern: [ 
551     [ p = l1_pattern; EOI -> fun l -> NotationUtil.boxify (p l) ] 
552   ];
553   l1_pattern: [ 
554     [ p = LIST1 l1_simple_pattern -> 
555         fun l -> List.map (fun x -> x l) p ] 
556   ];
557   literal: [
558     [ s = SYMBOL -> `Symbol (s, (None,None))
559     | k = QKEYWORD -> `Keyword (k, (None,None))
560     | n = NUMBER -> `Number (n,(None,None))
561     ]
562   ];
563   sep:       [ [ "sep";      sep = literal -> sep ] ];
564   l1_magic_pattern: [
565     [ "list0"; p = l1_simple_pattern; sep = OPT sep -> 
566             fun l -> Ast.List0 (p l, sep)
567     | "list1"; p = l1_simple_pattern; sep = OPT sep -> 
568             fun l -> Ast.List1 (p l, sep)
569     | "opt";   p = l1_simple_pattern -> fun l -> Ast.Opt (p l)
570     ]
571   ];
572   l1_pattern_variable: [
573     [ "term"; precedence = NUMBER; id = IDENT -> 
574         Ast.TermVar (id, Ast.Level (int_of_string precedence))
575     | "number"; id = IDENT -> Ast.NumVar id
576     | "ident"; id = IDENT -> Ast.IdentVar id
577     ]
578   ];
579   mstyle: [ 
580     [ id = IDENT; 
581       v = [ IDENT | NUMBER | COLOR | FLOATWITHUNIT ] -> id, v]];
582   mpadded: [ 
583     [ id = IDENT; 
584       v = [ PERCENTAGE ] -> id, v]];
585   l1_simple_pattern:
586     [ "layout" LEFTA
587       [ p1 = SELF; SYMBOL "\\sub "; p2 = SELF ->
588           return_term_of_level loc 
589             (fun l -> Ast.Layout (Ast.Sub (p1 l, p2 l)))
590       | p1 = SELF; SYMBOL "\\sup "; p2 = SELF ->
591           return_term_of_level loc 
592             (fun l -> Ast.Layout (Ast.Sup (p1 l, p2 l)))
593       | p1 = SELF; SYMBOL "\\below "; p2 = SELF ->
594           return_term_of_level loc 
595             (fun l -> Ast.Layout (Ast.Below (p1 l, p2 l)))
596       | p1 = SELF; SYMBOL "\\above "; p2 = SELF ->
597           return_term_of_level loc 
598             (fun l -> Ast.Layout (Ast.Above (p1 l, p2 l)))
599       | p1 = SELF; SYMBOL "\\over "; p2 = SELF ->
600           return_term_of_level loc 
601             (fun l -> Ast.Layout (Ast.Over (p1 l, p2 l)))
602       | p1 = SELF; SYMBOL "\\atop "; p2 = SELF ->
603           return_term_of_level loc 
604             (fun l -> Ast.Layout (Ast.Atop (p1 l, p2 l)))
605       | p1 = SELF; SYMBOL "\\frac "; p2 = SELF ->
606           return_term_of_level loc 
607             (fun l -> Ast.Layout (Ast.Frac (p1 l, p2 l)))
608       | SYMBOL "\\infrule "; p1 = SELF; p2 = SELF; p3 = SELF ->
609           return_term_of_level loc 
610             (fun l -> Ast.Layout (Ast.InfRule (p1 l, p2 l, p3 l)))
611       | SYMBOL "\\sqrt "; p = SELF -> 
612           return_term_of_level loc (fun l -> Ast.Layout (Ast.Sqrt p l))
613       | SYMBOL "\\root "; index = SELF; SYMBOL "\\of "; arg = SELF ->
614           return_term_of_level loc 
615             (fun l -> Ast.Layout (Ast.Root (arg l, index l)))
616       | "hbox"; LPAREN; p = l1_pattern; RPAREN ->
617           return_term_of_level loc 
618             (fun l -> Ast.Layout (Ast.Box ((Ast.H, false, false), p l)))
619       | "vbox"; LPAREN; p = l1_pattern; RPAREN ->
620           return_term_of_level loc 
621             (fun l -> Ast.Layout (Ast.Box ((Ast.V, false, false), p l)))
622       | "hvbox"; LPAREN; p = l1_pattern; RPAREN ->
623           return_term_of_level loc 
624             (fun l -> Ast.Layout (Ast.Box ((Ast.HV, false, false), p l)))
625       | "hovbox"; LPAREN; p = l1_pattern; RPAREN ->
626           return_term_of_level loc 
627             (fun l -> Ast.Layout (Ast.Box ((Ast.HOV, false, false), p l)))
628       | "break" -> return_term_of_level loc (fun _ -> Ast.Layout Ast.Break)
629       | "mstyle"; m = LIST1 mstyle ; LPAREN; t = l1_pattern; RPAREN ->
630           return_term_of_level loc 
631             (fun l -> 
632                Ast.Layout (Ast.Mstyle (m, t l)))
633       | "mpadded"; m = LIST1 mpadded ; LPAREN; t = l1_pattern; RPAREN ->
634           return_term_of_level loc 
635             (fun l -> 
636                Ast.Layout (Ast.Mpadded (m, t l)))
637       | "maction"; m = LIST1 [ LPAREN; l = l1_pattern; RPAREN -> l ] ->
638            return_term_of_level loc 
639             (fun l -> Ast.Layout (Ast.Maction (List.map (fun x ->
640               NotationUtil.group (x l)) m)))
641       | LPAREN; p = l1_pattern; RPAREN ->
642           return_term_of_level loc (fun l -> NotationUtil.group (p l))
643       ]
644     | "simple" NONA
645       [ i = IDENT -> 
646          return_term_of_level loc 
647            (fun l -> Ast.Variable (Ast.TermVar (i,Ast.Self l)))
648       | m = l1_magic_pattern -> 
649              return_term_of_level loc (fun l -> Ast.Magic (m l))
650       | v = l1_pattern_variable -> 
651              return_term_of_level loc (fun _ -> Ast.Variable v)
652       | l = literal -> return_term_of_level loc (fun _ -> Ast.Literal l)
653       ]
654     ];
655   END
656   end;
657 (* }}} *)
658 (* {{{ Grammar for ast magics, notation level 2 *)
659   begin
660   let level2_meta = grammars.level2_meta in
661 EXTEND
662   GLOBAL: level2_meta;
663   l2_variable: [
664     [ "term"; precedence = NUMBER; id = IDENT -> 
665         Ast.TermVar (id,Ast.Level (int_of_string precedence))
666     | "number"; id = IDENT -> Ast.NumVar id
667     | "ident"; id = IDENT -> Ast.IdentVar id
668     | "fresh"; id = IDENT -> Ast.FreshVar id
669     | "anonymous" -> Ast.TermVar ("_",Ast.Self 0) (* is the level relevant?*)
670     | id = IDENT -> Ast.TermVar (id,Ast.Self 0)
671     ]
672   ];
673   l2_magic: [
674     [ "fold"; kind = [ "left" -> `Left | "right" -> `Right ];
675       base = level2_meta; "rec"; id = IDENT; recursive = level2_meta ->
676         Ast.Fold (kind, base, [id], recursive)
677     | "default"; some = level2_meta; none = level2_meta ->
678         Ast.Default (some, none)
679     | "if"; p_test = level2_meta;
680       "then"; p_true = level2_meta;
681       "else"; p_false = level2_meta ->
682         Ast.If (p_test, p_true, p_false)
683     | "fail" -> Ast.Fail
684     ]
685   ];
686   level2_meta: [
687     [ magic = l2_magic -> Ast.Magic magic
688     | var = l2_variable -> Ast.Variable var
689     | blob = UNPARSED_AST ->
690         parse_level2_ast grammars (Ulexing.from_utf8_string blob)
691     ]
692   ];
693 END
694   end;
695 (* }}} *)
696 (* {{{ Grammar for ast patterns, notation level 2 *)
697   begin
698   let level2_ast = grammars.level2_ast in
699   let term = grammars.term in
700   let atag_attributes = grammars.sym_attributes in
701   let let_defs = grammars.let_defs in
702   let let_codefs = grammars.let_codefs in
703   let ident = grammars.ident in
704   let protected_binder_vars = grammars.protected_binder_vars in
705 EXTEND
706   GLOBAL: level2_ast term let_defs let_codefs protected_binder_vars ident atag_attributes;
707   level2_ast: [ [ p = term -> p ] ];
708   sort: [
709     [ "Prop" -> `Prop
710     | "Set" -> `Set
711     | "Type"; SYMBOL "["; n = [ NUMBER| IDENT ]; SYMBOL "]" -> `NType n
712     | "CProp"; SYMBOL "["; n = [ NUMBER| IDENT ]; SYMBOL "]" -> `NCProp n
713     ]
714   ];
715   meta_subst: [
716     [ s = SYMBOL "_" -> None
717     | p = term -> Some p ]
718   ];
719   meta_substs: [
720     [ SYMBOL "["; substs = LIST0 meta_subst; SYMBOL "]" -> substs ]
721   ];
722   possibly_typed_name: [
723     [ LPAREN; id = single_arg; SYMBOL ":"; typ = term; RPAREN ->
724         id, Some typ
725     | arg = single_arg -> arg, None
726     | id = PIDENT -> Ast.Ident (id, `Ambiguous), None
727     | SYMBOL "_" -> Ast.Ident ("_", `Ambiguous), None
728     | LPAREN; id = PIDENT; SYMBOL ":"; typ = term; RPAREN ->
729         Ast.Ident (id, `Ambiguous), Some typ
730     | LPAREN; SYMBOL "_"; SYMBOL ":"; typ = term; RPAREN ->
731         Ast.Ident ("_", `Ambiguous), Some typ
732     ]
733   ];
734   match_pattern: [
735     [ SYMBOL "_" -> Ast.Wildcard
736     | id = IDENT -> Ast.Pattern (id, None, [])
737     | LPAREN; id = IDENT; vars = LIST1 possibly_typed_name; RPAREN ->
738        Ast.Pattern (id, None, vars)
739     | id = IDENT; vars = LIST1 possibly_typed_name ->
740        Ast.Pattern (id, None, vars)
741     ]
742   ];
743   binder: [
744     [ SYMBOL <:unicode<Pi>>     (* Π *) -> `Pi
745     | SYMBOL <:unicode<forall>> (* ∀ *) -> `Forall
746     | SYMBOL <:unicode<lambda>> (* λ *) -> `Lambda
747     ]
748   ];
749   gident: [
750     [ id = IDENT ->
751        try
752          let uri,_ = CicNotationLexer.LocalizeEnv.find loc 
753            !loctable in
754          match uri with
755          | Some u -> 
756             prerr_endline ("trovata interpretazione per " ^ id ^ ": " ^ u);
757                          id, `Uri u
758          | None ->
759             prerr_endline ("identificatore ambiguo: " ^ id);
760                          id, `Ambiguous
761        with
762        | Not_found -> 
763             prerr_endline ("identificatore non trovato: " ^ id);
764                        id, `Ambiguous ]];
765   gnum: [
766     [ n = NUMBER ->
767        try
768          match CicNotationLexer.LocalizeEnv.find loc !loctable with
769          | _uri, Some interpr -> n, Some (Some "cic:/fakeuri.def(1)",interpr)
770          | _ -> n,None 
771        with
772        | Not_found -> n,None ]];
773   arg: [
774     [ LPAREN; names = LIST1 gident SEP SYMBOL ",";
775       SYMBOL ":"; ty = term; RPAREN ->
776         List.map (fun (n,u) -> Ast.Ident (n,u)) names, Some ty
777     | (name,uri) = gident -> [Ast.Ident (name,uri)], None
778     | blob = UNPARSED_META ->
779         let meta = parse_level2_meta grammars (Ulexing.from_utf8_string blob) in
780         match meta with
781         | Ast.Variable (Ast.FreshVar _) -> [meta], None
782         | Ast.Variable (Ast.TermVar ("_",_)) -> [Ast.Ident ("_", `Ambiguous)], None
783         | _ -> failwith "Invalid bound name."
784    ]
785   ];
786   single_arg: [
787     [ (name,uri) = gident -> Ast.Ident (name,uri)
788     | blob = UNPARSED_META ->
789         let meta = parse_level2_meta grammars (Ulexing.from_utf8_string blob) in
790         match meta with
791         | Ast.Variable (Ast.FreshVar _)
792         | Ast.Variable (Ast.IdentVar _) -> meta
793         | Ast.Variable (Ast.TermVar ("_",_)) -> Ast.Ident ("_", `Ambiguous)
794         | _ -> failwith "Invalid index name."
795     ]
796   ];
797   ident: [
798     [ name = IDENT -> Env.Ident name
799     | blob = UNPARSED_META ->
800         let meta = parse_level2_meta grammars (Ulexing.from_utf8_string blob) in
801         match meta with
802         | Ast.Variable (Ast.FreshVar _) ->
803            (* it makes sense: extend Env.ident_or_var *)
804             assert false
805         | Ast.Variable (Ast.IdentVar name) -> Env.Var name
806         | Ast.Variable (Ast.TermVar ("_",_)) -> Env.Var "_"
807         | _ -> failwith ("Invalid index name: " ^ blob)
808     ]
809   ];
810   let_defs: [
811     [ defs = LIST1 [
812         name = single_arg;
813         args = LIST1 arg;
814         index_name = OPT [ "on"; id = single_arg -> id ];
815         ty = OPT [ SYMBOL ":" ; p = term -> p ];
816         SYMBOL <:unicode<def>> (* ≝ *); body = term ->
817           let rec position_of name p = function 
818             | [] -> None, p
819             | n :: _ when n = name -> Some p, p
820             | _ :: tl -> position_of name (p + 1) tl
821           in
822           let rec find_arg name n = function 
823             | [] ->
824                 (* CSC: new NCicPp.status is the best I can do here
825                    without changing the return type *)
826                 Ast.fail loc (sprintf "Argument %s not found"
827                   (NotationPp.pp_term (new NCicPp.status None) name))
828             | (l,_) :: tl -> 
829                 (match position_of name 0 l with
830                 | None, len -> find_arg name (n + len) tl
831                 | Some where, len -> n + where)
832           in
833           let index = 
834             match index_name with 
835             | None -> 0 
836             | Some index_name -> find_arg index_name 0 args
837           in
838           let args =
839            List.concat
840             (List.map
841              (function (names,ty) -> List.map (function x -> x,ty) names
842              ) args)
843           in
844            args, (name, ty), body, index
845       ] SEP "and" ->
846         defs
847     ]
848   ];
849   binder_vars: [
850     [ vars = [ l =
851         [ l = LIST1 single_arg SEP SYMBOL "," -> l
852         | l = LIST1 [ PIDENT | SYMBOL "_" ] SEP SYMBOL "," -> 
853             List.map (fun x -> Ast.Ident(x,`Ambiguous)) l
854       ] -> l ];
855       typ = OPT [ SYMBOL ":"; t = term -> t ] -> (vars, typ)
856     ]
857   ];
858   protected_binder_vars: [
859     [ LPAREN; vars = binder_vars; RPAREN -> vars 
860     ]
861   ];
862   maybe_protected_binder_vars: [
863     [ vars = binder_vars -> vars
864     | vars = protected_binder_vars -> vars
865     ]
866   ];
867   term: LEVEL "10"
868   [
869     [ "let"; 
870      var = 
871       [ LPAREN; id = single_arg; SYMBOL ":"; typ = term; RPAREN ->
872           id, Some typ
873       | (id,uri) = gident; ty = OPT [ SYMBOL ":"; typ = term -> typ] ->
874           Ast.Ident (id,uri), ty ];
875       SYMBOL <:unicode<def>> (* ≝ *);
876       p1 = term; "in"; p2 = term ->
877         return_term loc (Ast.LetIn (var, p1, p2))
878     | LETCOREC; defs = let_defs; "in";
879       body = term ->
880         return_term loc (Ast.LetRec (`CoInductive, defs, body))
881     | LETREC; defs = let_defs; "in";
882       body = term ->
883         return_term loc (Ast.LetRec (`Inductive, defs, body))
884     ]
885   ];
886   term: LEVEL "20"
887     [
888       [ b = binder; (vars, typ) = maybe_protected_binder_vars; SYMBOL "."; body = term LEVEL "19" ->
889           return_term loc (fold_cluster b vars typ body)
890       ]
891     ];
892   term: LEVEL "70"
893     [
894       [ p1 = term; p2 = term LEVEL "71" ->
895           let rec aux = function
896             | Ast.Appl (hd :: tl)
897             | Ast.AttributedTerm (_, Ast.Appl (hd :: tl)) ->
898                 aux hd @ tl
899             | term -> [term]
900           in
901           return_term loc (Ast.Appl (aux p1 @ [p2]))
902       ]
903     ];
904   term: LEVEL "90"
905     [
906       [ (id,uri) = gident -> return_term loc (Ast.Ident (id,uri))
907       | s = CSYMBOL -> return_term loc (Ast.Symbol (s, None))
908       | u = URI -> return_term loc (Ast.Ident 
909                      (NUri.name_of_uri (NUri.uri_of_string u), `Uri u))
910       | r = NREF -> return_term loc (Ast.NRef (NReference.reference_of_string r))
911       | (n,interpr) = gnum -> return_term loc (Ast.Num (n, interpr))
912       | IMPLICIT -> return_term loc (Ast.Implicit `JustOne)
913       | SYMBOL <:unicode<ldots>> -> return_term loc (Ast.Implicit `Vector)
914       | PLACEHOLDER -> return_term loc Ast.UserInput
915       | m = META -> return_term loc (Ast.Meta (int_of_string m, []))
916       | m = META; s = meta_substs ->
917           return_term loc (Ast.Meta (int_of_string m, s))
918       | s = sort -> return_term loc (Ast.Sort s)
919       | "match"; t = term;
920         indty_ident = OPT [ "in"; id = IDENT -> id, None ];
921         outtyp = OPT [ "return"; ty = term -> ty ];
922         "with"; SYMBOL "[";
923         patterns = LIST0 [
924           lhs = match_pattern; SYMBOL <:unicode<Rightarrow>> (* ⇒ *);
925           rhs = term ->
926             lhs, rhs
927         ] SEP SYMBOL "|";
928         SYMBOL "]" ->
929           return_term loc (Ast.Case (t, indty_ident, outtyp, patterns))
930       | LPAREN; p1 = term; SYMBOL ":"; p2 = term; RPAREN ->
931           return_term loc (Ast.Cast (p1, p2))
932       | LPAREN; p = term; RPAREN -> p
933       | blob = UNPARSED_META ->
934           parse_level2_meta grammars (Ulexing.from_utf8_string blob)
935       ]
936     ];
937 END
938   end;
939 (* }}} *)
940   grammars
941 ;;
942
943
944 let initial_grammars loctable keywords =
945   let lexers = CicNotationLexer.mk_lexers loctable keywords in
946   let level1_pattern_grammar = 
947     Grammar.gcreate lexers.CicNotationLexer.level1_pattern_lexer in
948   let level2_ast_grammar = 
949     Grammar.gcreate lexers.CicNotationLexer.level2_ast_lexer in
950   let level2_meta_grammar = 
951     Grammar.gcreate lexers.CicNotationLexer.level2_meta_lexer in
952   let level1_pattern =
953     Grammar.Entry.create level1_pattern_grammar "level1_pattern" in
954   let level2_ast = Grammar.Entry.create level2_ast_grammar "level2_ast" in
955   let term = Grammar.Entry.create level2_ast_grammar "term" in
956   let ident = Grammar.Entry.create level2_ast_grammar "ident" in
957   (* unexpanded TeX macros terminated by a space (see comment in
958    * CicNotationLexer) *)
959   let initial_symbols = 
960     ["\\sub ";"\\sup ";"\\below ";"\\above ";"\\frac "
961     ;"\\infrule ";"\\atop ";"\\over ";"\\root ";"\\of ";"\\sqrt "] in
962   let sym_attributes = 
963     Grammar.Entry.create level2_ast_grammar "atag_attributes" in
964   let sym_table = 
965     List.fold_left 
966       (add_item_to_grammar_explicit level2_ast_grammar sym_attributes) 
967       [] (List.map (fun s -> `Sym s) initial_symbols)
968   in
969   let let_defs = Grammar.Entry.create level2_ast_grammar "let_defs" in
970   let let_codefs = Grammar.Entry.create level2_ast_grammar "let_codefs" in
971   let protected_binder_vars = 
972     Grammar.Entry.create level2_ast_grammar "protected_binder_vars" in
973   let level2_meta = Grammar.Entry.create level2_meta_grammar "level2_meta" in
974   initialize_grammars loctable
975   { level1_pattern=level1_pattern;
976     level2_ast=level2_ast;
977     term=term;
978     ident=ident;
979     sym_table=sym_table;
980     sym_attributes=sym_attributes;
981     let_defs=let_defs;
982     let_codefs=let_codefs;
983     protected_binder_vars=protected_binder_vars;
984     level2_meta=level2_meta;
985     level2_ast_grammar=level2_ast_grammar;
986   }
987 ;;
988
989 class type g_status =
990  object
991   method notation_parser_db: db
992  end
993
994 class status0 ~keywords:kwds =
995  object
996   val db = 
997     let lt = ref CicNotationLexer.LocalizeEnv.empty in
998     { grammars = initial_grammars lt kwds; keywords = kwds; 
999       items = []; loctable = lt }
1000   method notation_parser_db = db
1001   method set_notation_parser_db v = {< db = v >}
1002   method set_notation_parser_status
1003    : 'status. #g_status as 'status -> 'self
1004    = fun o -> {< db = o#notation_parser_db >}
1005   method reset_loctable () = 
1006     db.loctable := CicNotationLexer.LocalizeEnv.empty
1007  end
1008
1009 class virtual status uid ~keywords:kwds =
1010  object
1011   inherit NCic.status uid
1012   inherit status0 kwds
1013  end
1014
1015 let extend (status : #status) (CL1P (level1_pattern,precedence)) action =
1016         (* move inside constructor XXX *)
1017   let add1item status (level, level1_pattern, action) =
1018     let status = update_sym_grammar status level1_pattern in 
1019     let p_bindings, p_atoms =
1020       List.split (extract_term_production status level1_pattern) 
1021     in
1022     Grammar.extend
1023       [ Grammar.Entry.obj 
1024         (status#notation_parser_db.grammars.term : 'a Grammar.Entry.e),
1025         Some (Gramext.Level level),
1026         [ None,
1027           Some (*Gramext.NonA*) Gramext.NonA,
1028           [ p_atoms, (* concrete l1 syntax *) 
1029             (make_action status 
1030               (fun (env: NotationEnv.t) (loc: Ast.location) ->
1031                 (action env loc))
1032               p_bindings) ]]];
1033     status
1034   in
1035   let current_item = 
1036     let level = level_of precedence in
1037     level, level1_pattern, action in
1038   let keywords = NotationUtil.keywords_of_term level1_pattern @
1039     status#notation_parser_db.keywords in
1040   let items = current_item :: status#notation_parser_db.items in 
1041   let status = status#set_notation_parser_status (new status0 ~keywords) in
1042   let status = status#set_notation_parser_db 
1043     {status#notation_parser_db with items = items} in
1044   List.fold_left add1item status items
1045 ;;
1046
1047
1048 let parse_level1_pattern status =
1049   parse_level1_pattern status#notation_parser_db.grammars 
1050 let parse_level2_ast status =
1051   parse_level2_ast status#notation_parser_db.grammars 
1052 let parse_level2_meta status =
1053   parse_level2_meta status#notation_parser_db.grammars
1054
1055 let level2_ast_grammar status = 
1056   status#notation_parser_db.grammars.level2_ast_grammar
1057 let term status = status#notation_parser_db.grammars.term
1058 let let_defs status = status#notation_parser_db.grammars.let_defs
1059 let let_codefs status = status#notation_parser_db.grammars.let_codefs
1060 let protected_binder_vars status = 
1061   status#notation_parser_db.grammars.protected_binder_vars
1062
1063 (** {2 Debugging} *)
1064
1065 let print_l2_pattern status =
1066   Grammar.print_entry Format.std_formatter 
1067     (Grammar.Entry.obj status#notation_parser_db.grammars.term);
1068   Format.pp_print_flush Format.std_formatter ();
1069   flush stdout  
1070
1071 (* vim:set encoding=utf8 foldmethod=marker: *)