1 (* Copyright (C) 2005, HELM Team.
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.
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.
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.
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,
22 * For details, see the HELM World-Wide-Web page,
23 * http://helm.cs.unibo.it/
30 module Ast = NotationPt
31 module Env = NotationEnv
33 exception Parse_error of string
34 exception Level_not_found of int
36 let min_precedence = 0
37 let max_precedence = 100
39 type ('a,'b,'c,'d,'e) grammars = {
40 level1_pattern: 'a Grammar.Entry.e;
41 level2_ast: 'b Grammar.Entry.e;
42 level2_ast_grammar : Grammar.g;
43 term: 'b Grammar.Entry.e;
44 ident: 'e Grammar.Entry.e;
45 sym_attributes: (string option * string option) Grammar.Entry.e;
46 sym_table: (string * Stdpp.location Grammar.Entry.e) list;
47 let_defs: 'c Grammar.Entry.e;
48 protected_binder_vars: 'd Grammar.Entry.e;
49 level2_meta: 'b Grammar.Entry.e;
52 type checked_l1_pattern = CL1P of NotationPt.term * int
54 let refresh_uri_in_checked_l1_pattern ~refresh_uri_in_term
55 ~refresh_uri_in_reference (CL1P (t,n))
57 CL1P (NotationUtil.refresh_uri_in_term ~refresh_uri_in_term
58 ~refresh_uri_in_reference t, n)
62 | Binding of string * Env.value_type
63 | Env of (string * Env.value_type) list
67 (int -> NotationPt.term,
69 (Ast.term Ast.capture_variable list *
70 Ast.term Ast.capture_variable * Ast.term * int) list,
71 Ast.term list * Ast.term option, Env.ident_or_var) grammars;
72 keywords: string list;
73 items: (string * Ast.term * (NotationEnv.t -> Ast.location -> Ast.term)) list;
74 loctable: (string option * string option) CicNotationLexer.LocalizeEnv.t ref
79 Pervasives.int_of_string s
81 failwith (sprintf "Lexer failure: string_of_int \"%s\" failed" s)
83 (** {2 Grammar extension} *)
85 let level_of precedence =
86 if precedence < min_precedence || precedence > max_precedence then
87 raise (Level_not_found precedence);
88 string_of_int precedence
90 let add_symbol_to_grammar_explicit level2_ast_grammar
91 sym_attributes sym_table s =
93 let _ = List.assoc s sym_table
96 let entry = Grammar.Entry.create level2_ast_grammar ("sym" ^ s) in
98 [ Grammar.Entry.obj entry,
101 Some (*Gramext.NonA*) Gramext.NonA,
102 [ [Gramext.Stoken ("SYMBOL",s)], (* concrete l1 syntax *)
103 (Gramext.action (fun _ loc -> None, loc))
104 ; [Gramext.Stoken ("ATAG","")
105 ;Gramext.Snterm (Grammar.Entry.obj sym_attributes)
106 ;Gramext.Stoken ("SYMBOL",">")
107 ;Gramext.Stoken ("SYMBOL",s)
108 ;Gramext.Stoken ("ATAGEND","")],
109 (Gramext.action (fun _ uridesc _ _ _ loc -> (Some uridesc),loc))
111 (* prerr_endline ("adding to grammar symbol " ^ s); *)
115 let add_symbol_to_grammar status s =
116 let sym_attributes = status#notation_parser_db.grammars.sym_attributes in
117 let sym_table = status#notation_parser_db.grammars.sym_table in
118 let level2_ast_grammar =
119 status#notation_parser_db.grammars.level2_ast_grammar
122 add_symbol_to_grammar_explicit level2_ast_grammar sym_attributes sym_table s
125 { status#notation_parser_db.grammars with sym_table = sym_table }
127 let notation_parser_db =
128 { status#notation_parser_db with grammars = grammars } in
129 status#set_notation_parser_db notation_parser_db
131 let gram_symbol status s =
132 let sym_table = status#notation_parser_db.grammars.sym_table in
134 try List.assoc s sym_table
136 (let syms = List.map fst (status#notation_parser_db.grammars.sym_table) in
137 let syms = List.map (fun x -> "\"" ^ x ^ "\"") syms in
138 prerr_endline ("new symbol non-terminals: " ^ (String.concat ", " syms));
139 prerr_endline ("unable to find symbol \"" ^ s ^ "\""); assert false)
141 Gramext.Snterm (Grammar.Entry.obj entry)
143 let gram_ident status =
144 Gramext.Snterm (Grammar.Entry.obj
145 (status#notation_parser_db.grammars.ident : 'a Grammar.Entry.e))
146 (*Gramext.Stoken ("IDENT", s)*)
147 let gram_number s = Gramext.Stoken ("NUMBER", s)
148 let gram_keyword s = Gramext.Stoken ("", s)
149 let gram_term status = function
150 | Ast.Self _ -> Gramext.Sself
151 | Ast.Level precedence ->
154 (status#notation_parser_db.grammars.term : 'a Grammar.Entry.e),
158 let gram_of_literal status =
160 | `Symbol (s,_) -> gram_symbol status s
161 | `Keyword (s,_) -> gram_keyword s
162 | `Number (s,_) -> gram_number s
164 let make_action status action bindings =
165 let rec aux (vl : NotationEnv.t) =
167 [] -> Gramext.action (fun (loc: Ast.location) -> action vl loc)
170 (fun (_,(loc: Ast.location)) ->
173 CicNotationLexer.LocalizeEnv.find loc
174 !(status#notation_parser_db.loctable)
175 with Not_found -> None, None
176 in aux (("",(Env.NoType,
177 Env.DisambiguationValue (loc,uri,desc)))::vl) tl)
178 (* LUCA: DEFCON 3 BEGIN *)
179 | Binding (name, Env.TermType l) :: tl ->
182 aux ((name, (Env.TermType l, Env.TermValue v))::vl) tl)
183 | Binding (name, Env.StringType) :: tl ->
185 (fun (v:Env.ident_or_var) ->
186 aux ((name, (Env.StringType, Env.StringValue v)) :: vl) tl)
187 | Binding (name, Env.NumType) :: tl ->
190 aux ((name, (Env.NumType, Env.NumValue v)) :: vl) tl)
191 | Binding (name, Env.OptType t) :: tl ->
193 (fun (v:'a option) ->
194 aux ((name, (Env.OptType t, Env.OptValue v)) :: vl) tl)
195 | Binding (name, Env.ListType t) :: tl ->
198 aux ((name, (Env.ListType t, Env.ListValue v)) :: vl) tl)
200 Gramext.action (fun (v:NotationEnv.t) -> aux (v @ vl) tl)
201 | _ (* Binding (_,NoType) *) -> assert false
202 (* LUCA: DEFCON 3 END *)
204 aux [] (List.rev bindings)
210 | NoBinding :: tl -> aux acc tl
211 | Env names :: tl -> aux (List.rev names @ acc) tl
212 | Binding (name, ty) :: tl -> aux ((name, ty) :: acc) tl
216 (* given a level 1 pattern, adds productions for symbols when needed *)
217 let update_sym_grammar status pattern =
218 let rec aux status = function
219 | Ast.AttributedTerm (_, t) -> aux status t
220 | Ast.Literal l -> aux_literal status l
221 | Ast.Layout l -> aux_layout status l
222 | Ast.Magic m -> aux_magic status m
223 | Ast.Variable v -> aux_variable status v
225 prerr_endline (NotationPp.pp_term status t);
227 and aux_literal status =
229 | `Symbol (s,_) -> add_symbol_to_grammar status s
230 | `Keyword _ -> status
231 | `Number _ -> status
232 and aux_layout status = function
233 | Ast.Sub (p1, p2) -> aux (aux status p1) p2
234 | Ast.Sup (p1, p2) -> aux (aux status p1) p2
235 | Ast.Below (p1, p2) -> aux (aux status p1) p2
236 | Ast.Above (p1, p2) -> aux (aux status p1) p2
237 | Ast.Frac (p1, p2) -> aux (aux status p1) p2
238 | Ast.InfRule (p1, p2, p3) -> aux (aux (aux status p1) p2) p3
239 | Ast.Atop (p1, p2) -> aux (aux status p1) p2
240 | Ast.Over (p1, p2) -> aux (aux status p1) p2
241 | Ast.Root (p1, p2) -> aux (aux status p1) p2
242 | Ast.Sqrt p -> aux status p
243 | Ast.Break -> status
244 | Ast.Box (_, pl) -> List.fold_left aux status pl
245 | Ast.Group pl -> List.fold_left aux status pl
246 | Ast.Mstyle (_,pl) -> List.fold_left aux status pl
247 | Ast.Mpadded (_,pl) -> List.fold_left aux status pl
248 | Ast.Maction l -> List.fold_left aux status l
249 and aux_magic status magic =
251 | Ast.Opt p -> aux status p
253 | Ast.List1 (p, s) ->
255 match s with None -> status | Some s' -> aux_literal status s'
259 and aux_variable status _ = status
263 (* given a level 1 pattern computes the new RHS of "term" grammar entry *)
264 let extract_term_production status pattern =
265 let rec aux = function
266 | Ast.AttributedTerm (_, t) -> aux t
267 | Ast.Literal l -> aux_literal l
268 | Ast.Layout l -> aux_layout l
269 | Ast.Magic m -> aux_magic m
270 | Ast.Variable v -> aux_variable v
272 prerr_endline (NotationPp.pp_term status t);
276 | `Symbol (s,_) -> [NoBinding, gram_symbol status s]
278 (* assumption: s will be registered as a keyword with the lexer *)
279 [NoBinding, gram_keyword s]
280 | `Number (s,_) -> [NoBinding, gram_number s]
281 and aux_layout = function
282 | Ast.Sub (p1, p2) -> aux p1 @ [NoBinding, gram_symbol status "\\sub "] @ aux p2
283 | Ast.Sup (p1, p2) -> aux p1 @ [NoBinding, gram_symbol status "\\sup "] @ aux p2
284 | Ast.Below (p1, p2) -> aux p1 @ [NoBinding, gram_symbol status "\\below "] @ aux p2
285 | Ast.Above (p1, p2) -> aux p1 @ [NoBinding, gram_symbol status "\\above "] @ aux p2
286 | Ast.Frac (p1, p2) -> aux p1 @ [NoBinding, gram_symbol status "\\frac "] @ aux p2
287 | Ast.InfRule (p1, p2, p3) -> [NoBinding, gram_symbol status "\\infrule "] @ aux p1 @ aux p2 @ aux p3
288 | Ast.Atop (p1, p2) -> aux p1 @ [NoBinding, gram_symbol status "\\atop "] @ aux p2
289 | Ast.Over (p1, p2) -> aux p1 @ [NoBinding, gram_symbol status "\\over "] @ aux p2
290 | Ast.Root (p1, p2) ->
291 [NoBinding, gram_symbol status "\\root "] @ aux p2
292 @ [NoBinding, gram_symbol status "\\of "] @ aux p1
293 | Ast.Sqrt p -> [NoBinding, gram_symbol status "\\sqrt "] @ aux p
295 | Ast.Box (_, pl) -> List.flatten (List.map aux pl)
296 | Ast.Group pl -> List.flatten (List.map aux pl)
297 | Ast.Mstyle (_,pl) -> List.flatten (List.map aux pl)
298 | Ast.Mpadded (_,pl) -> List.flatten (List.map aux pl)
299 | Ast.Maction l -> List.flatten (List.map aux l)
300 and aux_magic magic =
303 let p_bindings, p_atoms, p_names, p_action = inner_pattern p in
304 let action (env_opt : NotationEnv.t option) (loc : Ast.location) =
306 | Some env -> List.map Env.opt_binding_some env
307 | None -> List.map Env.opt_binding_of_name p_names
309 [ Env (List.map Env.opt_declaration p_names),
311 [ [ Gramext.Sopt (Gramext.srules [ p_atoms, p_action ]) ],
312 Gramext.action action ] ]
314 | Ast.List1 (p, _) ->
315 let p_bindings, p_atoms, p_names, p_action = inner_pattern p in
316 let action (env_list : NotationEnv.t list) (loc : Ast.location) =
317 NotationEnv.coalesce_env p_names env_list
321 | Ast.List0 (_, None) -> Gramext.Slist0 s
322 | Ast.List1 (_, None) -> Gramext.Slist1 s
323 | Ast.List0 (_, Some l) ->
324 Gramext.Slist0sep (s, gram_of_literal status l)
325 | Ast.List1 (_, Some l) ->
326 Gramext.Slist1sep (s, gram_of_literal status l)
329 [ Env (List.map Env.list_declaration p_names),
331 [ [ gram_of_list (Gramext.srules [ p_atoms, p_action ]) ],
332 Gramext.action action ] ]
336 | Ast.NumVar s -> [Binding (s, Env.NumType), gram_number ""]
337 | Ast.TermVar (s,(Ast.Self level|Ast.Level level as lv)) ->
338 [Binding (s, Env.TermType level), gram_term status lv]
339 | Ast.IdentVar s -> [Binding (s, Env.StringType), gram_ident status]
340 | Ast.Ascription (p, s) -> assert false (* TODO *)
341 | Ast.FreshVar _ -> assert false
342 and inner_pattern p =
343 let p_bindings, p_atoms = List.split (aux p) in
344 let p_names = flatten_opt p_bindings in
347 (fun (env : NotationEnv.t) (loc : Ast.location) -> env) p_bindings
349 p_bindings, p_atoms, p_names, action
353 type rule_id = Grammar.token Gramext.g_symbol list
355 let compare_rule_id x y =
356 let rec aux = function
360 | ((s1::tl1) as x),((s2::tl2) as y) ->
361 if Gramext.eq_symbol s1 s2 then aux (tl1,tl2)
362 else Pervasives.compare x y
367 let check_l1_pattern level1_pattern pponly level associativity =
368 let variables = ref 0 in
369 let symbols = ref 0 in
370 let rec aux = function
371 | Ast.AttributedTerm (att, t) -> Ast.AttributedTerm (att,aux t)
372 | Ast.Literal _ as l -> incr symbols; l
373 | Ast.Layout l -> Ast.Layout (aux_layout l)
374 | Ast.Magic m -> Ast.Magic (aux_magic m)
375 | Ast.Variable v -> (aux_variable v)
377 and aux_layout = function
378 | Ast.Sub (p1, p2) -> let p1 = aux p1 in let p2 = aux p2 in Ast.Sub (p1, p2)
379 | Ast.Sup (p1, p2) -> let p1 = aux p1 in let p2 = aux p2 in Ast.Sup (p1, p2)
380 | Ast.Below (p1, p2) -> let p1 = aux p1 in let p2 = aux p2 in Ast.Below (p1, p2)
381 | Ast.Above (p1, p2) -> let p1 = aux p1 in let p2 = aux p2 in Ast.Above (p1, p2)
382 | Ast.Frac (p1, p2) -> let p1 = aux p1 in let p2 = aux p2 in Ast.Frac (p1, p2)
383 | 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)
384 | Ast.Atop (p1, p2) -> let p1 = aux p1 in let p2 = aux p2 in Ast.Atop (p1, p2)
385 | Ast.Over (p1, p2) -> let p1 = aux p1 in let p2 = aux p2 in Ast.Over (p1, p2)
386 | Ast.Root (p1, p2) -> let p1 = aux p1 in let p2 = aux p2 in Ast.Root (p1, p2)
387 | Ast.Sqrt p -> Ast.Sqrt (aux p)
388 | Ast.Break as t -> t
389 | Ast.Box (b, pl) -> Ast.Box(b, List.map aux pl)
390 | Ast.Group pl -> Ast.Group (List.map aux pl)
391 | Ast.Mstyle (l,pl) -> Ast.Mstyle (l, List.map aux pl)
392 | Ast.Mpadded (l,pl) -> Ast.Mpadded (l, List.map aux pl)
393 | Ast.Maction l as t ->
395 raise(Parse_error("Maction can be used only in output notations"))
397 and aux_magic magic =
399 | Ast.Opt p -> Ast.Opt (aux p)
400 | Ast.List0 (p, x) -> Ast.List0 (aux p, x)
401 | Ast.List1 (p, x) -> Ast.List1 (aux p, x)
405 | Ast.NumVar _ as t -> Ast.Variable t
406 | Ast.TermVar (s,Ast.Self _) when associativity <> Gramext.NonA ->
408 if !variables > 2 then
409 raise (Parse_error ("Exactly 2 variables must be specified in an "^
410 "associative notation"));
411 (match !variables, associativity with
413 Ast.Variable (Ast.TermVar (s, Ast.Self level))
414 | 1,Gramext.RightA ->
415 Ast.Variable (Ast.TermVar (s, Ast.Self (level+1)))
417 Ast.Variable (Ast.TermVar (s, Ast.Self (level+1)))
418 | 2,Gramext.RightA ->
419 Ast.Variable (Ast.TermVar (s, Ast.Level (level-1)))
421 | Ast.TermVar (s,Ast.Level _) when associativity <> Gramext.NonA ->
422 raise (Parse_error ("Variables can not be declared with a " ^
423 "precedence in an associative notation"))
424 (*avoid camlp5 divergence due to non-Sself recursion at the same level *)
425 | Ast.TermVar (s,Ast.Level l) when l<=level && !variables=0 && !symbols=0->
426 raise(Parse_error("Left recursive rule with precedence not greater " ^
427 "than " ^ string_of_int level ^ " is not allowed to avoid divergence"))
428 | Ast.TermVar _ as t -> incr variables; Ast.Variable t
429 | Ast.IdentVar _ as t -> Ast.Variable t
430 | Ast.Ascription _ -> assert false (* TODO *)
431 | Ast.FreshVar _ -> assert false
433 if associativity <> Gramext.NonA && level = min_precedence then
434 raise (Parse_error ("You can not specify an associative notation " ^
435 "at level "^string_of_int min_precedence ^ "; increase it"));
436 let cp = aux level1_pattern in
437 (* prerr_endline ("checked_pattern: " ^ NotationPp.pp_term cp); *)
438 if !variables <> 2 && associativity <> Gramext.NonA then
439 raise (Parse_error ("Exactly 2 variables must be specified in an "^
440 "associative notation"));
446 let fold_cluster binder terms ty body =
448 (fun term body -> Ast.Binder (binder, (term, ty), body))
449 terms body (* terms are names: either Ident or FreshVar *)
451 let fold_exists terms ty body =
454 let lambda = Ast.Binder (`Lambda, (term, ty), body) in
455 Ast.Appl [ Ast.Symbol ("exists", None); lambda ])
458 let fold_binder binder pt_names body =
460 (fun (names, ty) body -> fold_cluster binder names ty body)
463 let return_term loc term = Ast.AttributedTerm (`Loc loc, term)
464 let return_term_of_level loc term l =
465 Ast.AttributedTerm (`Loc loc, term l)
467 (** {2 API implementation} *)
469 let exc_located_wrapper f =
473 | Stdpp.Exc_located (floc, Stream.Error msg) ->
474 raise (HExtlib.Localized (floc, Parse_error msg))
475 | Stdpp.Exc_located (floc, HExtlib.Localized (_,exn)) ->
476 raise (HExtlib.Localized (floc, (Parse_error (Printexc.to_string exn))))
477 | Stdpp.Exc_located (floc, exn) ->
478 raise (HExtlib.Localized (floc, (Parse_error (Printexc.to_string exn))))
480 let parse_level1_pattern grammars precedence lexbuf =
482 (fun () -> Grammar.Entry.parse grammars.level1_pattern (Obj.magic lexbuf) precedence)
484 let parse_level2_ast grammars lexbuf =
487 Grammar.Entry.parse grammars.level2_ast (Obj.magic lexbuf))
489 let parse_level2_meta grammars lexbuf =
491 (fun () -> Grammar.Entry.parse grammars.level2_meta (Obj.magic lexbuf))
493 (* create empty precedence level for "term" *)
494 let initialize_grammars loctable grammars =
496 Gramext.action (fun _ ->
497 failwith "internal error, lexer generated a dummy token")
499 (* Needed since campl4 on "delete_rule" remove the precedence level if it gets
500 * empty after the deletion. The lexer never generate the Stoken below. *)
501 let dummy_prod = [ [ Gramext.Stoken ("DUMMY", "") ], dummy_action ] in
502 let mk_level_list first last =
503 let rec aux acc = function
504 | i when i < first -> acc
507 ((Some (level_of i), Some Gramext.NonA, dummy_prod)
514 [ Grammar.Entry.obj (grammars.term: 'a Grammar.Entry.e),
516 mk_level_list min_precedence max_precedence ];
517 (* {{{ Grammar for concrete syntax patterns, notation level 1 *)
519 let level1_pattern = grammars.level1_pattern in
521 GLOBAL: level1_pattern;
524 [ p = l1_pattern; EOI -> fun l -> NotationUtil.boxify (p l) ]
527 [ p = LIST1 l1_simple_pattern ->
528 fun l -> List.map (fun x -> x l) p ]
531 [ s = SYMBOL -> `Symbol (s, (None,None))
532 | k = QKEYWORD -> `Keyword (k, (None,None))
533 | n = NUMBER -> `Number (n,(None,None))
536 sep: [ [ "sep"; sep = literal -> sep ] ];
538 [ "list0"; p = l1_simple_pattern; sep = OPT sep ->
539 fun l -> Ast.List0 (p l, sep)
540 | "list1"; p = l1_simple_pattern; sep = OPT sep ->
541 fun l -> Ast.List1 (p l, sep)
542 | "opt"; p = l1_simple_pattern -> fun l -> Ast.Opt (p l)
545 l1_pattern_variable: [
546 [ "term"; precedence = NUMBER; id = IDENT ->
547 Ast.TermVar (id, Ast.Level (int_of_string precedence))
548 | "number"; id = IDENT -> Ast.NumVar id
549 | "ident"; id = IDENT -> Ast.IdentVar id
554 v = [ IDENT | NUMBER | COLOR | FLOATWITHUNIT ] -> id, v]];
557 v = [ PERCENTAGE ] -> id, v]];
560 [ p1 = SELF; SYMBOL "\\sub "; p2 = SELF ->
561 return_term_of_level loc
562 (fun l -> Ast.Layout (Ast.Sub (p1 l, p2 l)))
563 | p1 = SELF; SYMBOL "\\sup "; p2 = SELF ->
564 return_term_of_level loc
565 (fun l -> Ast.Layout (Ast.Sup (p1 l, p2 l)))
566 | p1 = SELF; SYMBOL "\\below "; p2 = SELF ->
567 return_term_of_level loc
568 (fun l -> Ast.Layout (Ast.Below (p1 l, p2 l)))
569 | p1 = SELF; SYMBOL "\\above "; p2 = SELF ->
570 return_term_of_level loc
571 (fun l -> Ast.Layout (Ast.Above (p1 l, p2 l)))
572 | p1 = SELF; SYMBOL "\\over "; p2 = SELF ->
573 return_term_of_level loc
574 (fun l -> Ast.Layout (Ast.Over (p1 l, p2 l)))
575 | p1 = SELF; SYMBOL "\\atop "; p2 = SELF ->
576 return_term_of_level loc
577 (fun l -> Ast.Layout (Ast.Atop (p1 l, p2 l)))
578 | p1 = SELF; SYMBOL "\\frac "; p2 = SELF ->
579 return_term_of_level loc
580 (fun l -> Ast.Layout (Ast.Frac (p1 l, p2 l)))
581 | SYMBOL "\\infrule "; p1 = SELF; p2 = SELF; p3 = SELF ->
582 return_term_of_level loc
583 (fun l -> Ast.Layout (Ast.InfRule (p1 l, p2 l, p3 l)))
584 | SYMBOL "\\sqrt "; p = SELF ->
585 return_term_of_level loc (fun l -> Ast.Layout (Ast.Sqrt p l))
586 | SYMBOL "\\root "; index = SELF; SYMBOL "\\of "; arg = SELF ->
587 return_term_of_level loc
588 (fun l -> Ast.Layout (Ast.Root (arg l, index l)))
589 | "hbox"; LPAREN; p = l1_pattern; RPAREN ->
590 return_term_of_level loc
591 (fun l -> Ast.Layout (Ast.Box ((Ast.H, false, false), p l)))
592 | "vbox"; LPAREN; p = l1_pattern; RPAREN ->
593 return_term_of_level loc
594 (fun l -> Ast.Layout (Ast.Box ((Ast.V, false, false), p l)))
595 | "hvbox"; LPAREN; p = l1_pattern; RPAREN ->
596 return_term_of_level loc
597 (fun l -> Ast.Layout (Ast.Box ((Ast.HV, false, false), p l)))
598 | "hovbox"; LPAREN; p = l1_pattern; RPAREN ->
599 return_term_of_level loc
600 (fun l -> Ast.Layout (Ast.Box ((Ast.HOV, false, false), p l)))
601 | "break" -> return_term_of_level loc (fun _ -> Ast.Layout Ast.Break)
602 | "mstyle"; m = LIST1 mstyle ; LPAREN; t = l1_pattern; RPAREN ->
603 return_term_of_level loc
605 Ast.Layout (Ast.Mstyle (m, t l)))
606 | "mpadded"; m = LIST1 mpadded ; LPAREN; t = l1_pattern; RPAREN ->
607 return_term_of_level loc
609 Ast.Layout (Ast.Mpadded (m, t l)))
610 | "maction"; m = LIST1 [ LPAREN; l = l1_pattern; RPAREN -> l ] ->
611 return_term_of_level loc
612 (fun l -> Ast.Layout (Ast.Maction (List.map (fun x ->
613 NotationUtil.group (x l)) m)))
614 | LPAREN; p = l1_pattern; RPAREN ->
615 return_term_of_level loc (fun l -> NotationUtil.group (p l))
619 return_term_of_level loc
620 (fun l -> Ast.Variable (Ast.TermVar (i,Ast.Self l)))
621 | m = l1_magic_pattern ->
622 return_term_of_level loc (fun l -> Ast.Magic (m l))
623 | v = l1_pattern_variable ->
624 return_term_of_level loc (fun _ -> Ast.Variable v)
625 | l = literal -> return_term_of_level loc (fun _ -> Ast.Literal l)
631 (* {{{ Grammar for ast magics, notation level 2 *)
633 let level2_meta = grammars.level2_meta in
637 [ "term"; precedence = NUMBER; id = IDENT ->
638 Ast.TermVar (id,Ast.Level (int_of_string precedence))
639 | "number"; id = IDENT -> Ast.NumVar id
640 | "ident"; id = IDENT -> Ast.IdentVar id
641 | "fresh"; id = IDENT -> Ast.FreshVar id
642 | "anonymous" -> Ast.TermVar ("_",Ast.Self 0) (* is the level relevant?*)
643 | id = IDENT -> Ast.TermVar (id,Ast.Self 0)
647 [ "fold"; kind = [ "left" -> `Left | "right" -> `Right ];
648 base = level2_meta; "rec"; id = IDENT; recursive = level2_meta ->
649 Ast.Fold (kind, base, [id], recursive)
650 | "default"; some = level2_meta; none = level2_meta ->
651 Ast.Default (some, none)
652 | "if"; p_test = level2_meta;
653 "then"; p_true = level2_meta;
654 "else"; p_false = level2_meta ->
655 Ast.If (p_test, p_true, p_false)
660 [ magic = l2_magic -> Ast.Magic magic
661 | var = l2_variable -> Ast.Variable var
662 | blob = UNPARSED_AST ->
663 parse_level2_ast grammars (Ulexing.from_utf8_string blob)
669 (* {{{ Grammar for ast patterns, notation level 2 *)
671 let level2_ast = grammars.level2_ast in
672 let term = grammars.term in
673 let atag_attributes = grammars.sym_attributes in
674 let let_defs = grammars.let_defs in
675 let ident = grammars.ident in
676 let protected_binder_vars = grammars.protected_binder_vars in
678 GLOBAL: level2_ast term let_defs protected_binder_vars ident atag_attributes;
679 level2_ast: [ [ p = term -> p ] ];
683 | "Type"; SYMBOL "["; n = [ NUMBER| IDENT ]; SYMBOL "]" -> `NType n
684 | "CProp"; SYMBOL "["; n = [ NUMBER| IDENT ]; SYMBOL "]" -> `NCProp n
688 [ s = SYMBOL "_" -> None
689 | p = term -> Some p ]
692 [ SYMBOL "["; substs = LIST0 meta_subst; SYMBOL "]" -> substs ]
694 possibly_typed_name: [
695 [ LPAREN; id = single_arg; SYMBOL ":"; typ = term; RPAREN ->
697 | arg = single_arg -> arg, None
698 | id = PIDENT -> Ast.Ident (id, `Ambiguous), None
699 | SYMBOL "_" -> Ast.Ident ("_", `Ambiguous), None
700 | LPAREN; id = PIDENT; SYMBOL ":"; typ = term; RPAREN ->
701 Ast.Ident (id, `Ambiguous), Some typ
702 | LPAREN; SYMBOL "_"; SYMBOL ":"; typ = term; RPAREN ->
703 Ast.Ident ("_", `Ambiguous), Some typ
707 [ SYMBOL "_" -> Ast.Wildcard
708 | id = IDENT -> Ast.Pattern (id, None, [])
709 | LPAREN; id = IDENT; vars = LIST1 possibly_typed_name; RPAREN ->
710 Ast.Pattern (id, None, vars)
711 | id = IDENT; vars = LIST1 possibly_typed_name ->
712 Ast.Pattern (id, None, vars)
716 [ SYMBOL <:unicode<Pi>> (* Π *) -> `Pi
717 | SYMBOL <:unicode<forall>> (* ∀ *) -> `Forall
718 | SYMBOL <:unicode<lambda>> (* λ *) -> `Lambda
724 let uri,_ = CicNotationLexer.LocalizeEnv.find loc
727 | Some u -> id, `Uri u
728 | None -> id, `Ambiguous
730 | Not_found -> id, `Ambiguous ]];
732 [ LPAREN; names = LIST1 gident SEP SYMBOL ",";
733 SYMBOL ":"; ty = term; RPAREN ->
734 List.map (fun (n,u) -> Ast.Ident (n,u)) names, Some ty
735 | (name,uri) = gident -> [Ast.Ident (name,uri)], None
736 | blob = UNPARSED_META ->
737 let meta = parse_level2_meta grammars (Ulexing.from_utf8_string blob) in
739 | Ast.Variable (Ast.FreshVar _) -> [meta], None
740 | Ast.Variable (Ast.TermVar ("_",_)) -> [Ast.Ident ("_", `Ambiguous)], None
741 | _ -> failwith "Invalid bound name."
745 [ (name,uri) = gident -> Ast.Ident (name,uri)
746 | blob = UNPARSED_META ->
747 let meta = parse_level2_meta grammars (Ulexing.from_utf8_string blob) in
749 | Ast.Variable (Ast.FreshVar _)
750 | Ast.Variable (Ast.IdentVar _) -> meta
751 | Ast.Variable (Ast.TermVar ("_",_)) -> Ast.Ident ("_", `Ambiguous)
752 | _ -> failwith "Invalid index name."
756 [ name = IDENT -> Env.Ident name
757 | blob = UNPARSED_META ->
758 let meta = parse_level2_meta grammars (Ulexing.from_utf8_string blob) in
760 | Ast.Variable (Ast.FreshVar _) ->
761 (* it makes sense: extend Env.ident_or_var *)
763 | Ast.Variable (Ast.IdentVar name) -> Env.Var name
764 | Ast.Variable (Ast.TermVar ("_",_)) -> Env.Var "_"
765 | _ -> failwith ("Invalid index name: " ^ blob)
772 index_name = OPT [ "on"; id = single_arg -> id ];
773 ty = OPT [ SYMBOL ":" ; p = term -> p ];
774 SYMBOL <:unicode<def>> (* ≝ *); body = term ->
775 let rec position_of name p = function
777 | n :: _ when n = name -> Some p, p
778 | _ :: tl -> position_of name (p + 1) tl
780 let rec find_arg name n = function
782 (* CSC: new NCicPp.status is the best I can do here
783 without changing the return type *)
784 Ast.fail loc (sprintf "Argument %s not found"
785 (NotationPp.pp_term (new NCicPp.status None) name))
787 (match position_of name 0 l with
788 | None, len -> find_arg name (n + len) tl
789 | Some where, len -> n + where)
792 match index_name with
794 | Some index_name -> find_arg index_name 0 args
799 (function (names,ty) -> List.map (function x -> x,ty) names
802 args, (name, ty), body, index
809 [ l = LIST1 single_arg SEP SYMBOL "," -> l
810 | l = LIST1 [ PIDENT | SYMBOL "_" ] SEP SYMBOL "," ->
811 List.map (fun x -> Ast.Ident(x,`Ambiguous)) l
813 typ = OPT [ SYMBOL ":"; t = term -> t ] -> (vars, typ)
816 protected_binder_vars: [
817 [ LPAREN; vars = binder_vars; RPAREN -> vars
820 maybe_protected_binder_vars: [
821 [ vars = binder_vars -> vars
822 | vars = protected_binder_vars -> vars
829 [ LPAREN; id = single_arg; SYMBOL ":"; typ = term; RPAREN ->
831 | (id,uri) = gident; ty = OPT [ SYMBOL ":"; typ = term -> typ] ->
832 Ast.Ident (id,uri), ty ];
833 SYMBOL <:unicode<def>> (* ≝ *);
834 p1 = term; "in"; p2 = term ->
835 return_term loc (Ast.LetIn (var, p1, p2))
836 | LETCOREC; defs = let_defs; "in";
838 return_term loc (Ast.LetRec (`CoInductive, defs, body))
839 | LETREC; defs = let_defs; "in";
841 return_term loc (Ast.LetRec (`Inductive, defs, body))
846 [ b = binder; (vars, typ) = maybe_protected_binder_vars; SYMBOL "."; body = term LEVEL "19" ->
847 return_term loc (fold_cluster b vars typ body)
852 [ p1 = term; p2 = term LEVEL "71" ->
853 let rec aux = function
854 | Ast.Appl (hd :: tl)
855 | Ast.AttributedTerm (_, Ast.Appl (hd :: tl)) ->
859 return_term loc (Ast.Appl (aux p1 @ [p2]))
864 [ (id,uri) = gident -> return_term loc (Ast.Ident (id,uri))
865 | s = CSYMBOL -> return_term loc (Ast.Symbol (s, None))
866 | u = URI -> return_term loc (Ast.Ident
867 (NUri.name_of_uri (NUri.uri_of_string u), `Uri u))
868 | r = NREF -> return_term loc (Ast.NRef (NReference.reference_of_string r))
869 | n = NUMBER -> return_term loc (Ast.Num (n, None))
870 | IMPLICIT -> return_term loc (Ast.Implicit `JustOne)
871 | SYMBOL <:unicode<ldots>> -> return_term loc (Ast.Implicit `Vector)
872 | PLACEHOLDER -> return_term loc Ast.UserInput
873 | m = META -> return_term loc (Ast.Meta (int_of_string m, []))
874 | m = META; s = meta_substs ->
875 return_term loc (Ast.Meta (int_of_string m, s))
876 | s = sort -> return_term loc (Ast.Sort s)
878 indty_ident = OPT [ "in"; id = IDENT -> id, None ];
879 outtyp = OPT [ "return"; ty = term -> ty ];
882 lhs = match_pattern; SYMBOL <:unicode<Rightarrow>> (* ⇒ *);
887 return_term loc (Ast.Case (t, indty_ident, outtyp, patterns))
888 | LPAREN; p1 = term; SYMBOL ":"; p2 = term; RPAREN ->
889 return_term loc (Ast.Cast (p1, p2))
890 | LPAREN; p = term; RPAREN -> p
891 | blob = UNPARSED_META ->
892 parse_level2_meta grammars (Ulexing.from_utf8_string blob)
901 let initial_grammars loctable keywords =
902 let lexers = CicNotationLexer.mk_lexers loctable keywords in
903 let level1_pattern_grammar =
904 Grammar.gcreate lexers.CicNotationLexer.level1_pattern_lexer in
905 let level2_ast_grammar =
906 Grammar.gcreate lexers.CicNotationLexer.level2_ast_lexer in
907 let level2_meta_grammar =
908 Grammar.gcreate lexers.CicNotationLexer.level2_meta_lexer in
910 Grammar.Entry.create level1_pattern_grammar "level1_pattern" in
911 let level2_ast = Grammar.Entry.create level2_ast_grammar "level2_ast" in
912 let term = Grammar.Entry.create level2_ast_grammar "term" in
913 let ident = Grammar.Entry.create level2_ast_grammar "ident" in
914 (* unexpanded TeX macros terminated by a space (see comment in
915 * CicNotationLexer) *)
916 let initial_symbols =
917 ["\\sub ";"\\sup ";"\\below ";"\\above ";"\\frac "
918 ;"\\infrule ";"\\atop ";"\\over ";"\\root ";"\\of ";"\\sqrt "] in
920 Grammar.Entry.create level2_ast_grammar "atag_attributes" in
923 (add_symbol_to_grammar_explicit level2_ast_grammar sym_attributes)
926 let let_defs = Grammar.Entry.create level2_ast_grammar "let_defs" in
927 let protected_binder_vars =
928 Grammar.Entry.create level2_ast_grammar "protected_binder_vars" in
929 let level2_meta = Grammar.Entry.create level2_meta_grammar "level2_meta" in
930 initialize_grammars loctable
931 { level1_pattern=level1_pattern;
932 level2_ast=level2_ast;
936 sym_attributes=sym_attributes;
938 protected_binder_vars=protected_binder_vars;
939 level2_meta=level2_meta;
940 level2_ast_grammar=level2_ast_grammar;
944 class type g_status =
946 method notation_parser_db: db
949 class status0 ~keywords:kwds =
952 let lt = ref CicNotationLexer.LocalizeEnv.empty in
953 { grammars = initial_grammars lt kwds; keywords = kwds;
954 items = []; loctable = lt }
955 method notation_parser_db = db
956 method set_notation_parser_db v = {< db = v >}
957 method set_notation_parser_status
958 : 'status. #g_status as 'status -> 'self
959 = fun o -> {< db = o#notation_parser_db >}
960 method reset_loctable () =
961 db.loctable := CicNotationLexer.LocalizeEnv.empty
964 class virtual status uid ~keywords:kwds =
966 inherit NCic.status uid
970 let extend (status : #status) (CL1P (level1_pattern,precedence)) action =
971 (* move inside constructor XXX *)
972 let add1item status (level, level1_pattern, action) =
973 let status = update_sym_grammar status level1_pattern in
974 let p_bindings, p_atoms =
975 List.split (extract_term_production status level1_pattern)
979 (status#notation_parser_db.grammars.term : 'a Grammar.Entry.e),
980 Some (Gramext.Level level),
982 Some (*Gramext.NonA*) Gramext.NonA,
983 [ p_atoms, (* concrete l1 syntax *)
985 (fun (env: NotationEnv.t) (loc: Ast.location) ->
991 let level = level_of precedence in
992 level, level1_pattern, action in
993 let keywords = NotationUtil.keywords_of_term level1_pattern @
994 status#notation_parser_db.keywords in
995 let items = current_item :: status#notation_parser_db.items in
996 let status = status#set_notation_parser_status (new status0 ~keywords) in
997 let status = status#set_notation_parser_db
998 {status#notation_parser_db with items = items} in
999 List.fold_left add1item status items
1003 let parse_level1_pattern status =
1004 parse_level1_pattern status#notation_parser_db.grammars
1005 let parse_level2_ast status =
1006 parse_level2_ast status#notation_parser_db.grammars
1007 let parse_level2_meta status =
1008 parse_level2_meta status#notation_parser_db.grammars
1010 let level2_ast_grammar status =
1011 status#notation_parser_db.grammars.level2_ast_grammar
1012 let term status = status#notation_parser_db.grammars.term
1013 let let_defs status = status#notation_parser_db.grammars.let_defs
1014 let protected_binder_vars status =
1015 status#notation_parser_db.grammars.protected_binder_vars
1017 (** {2 Debugging} *)
1019 let print_l2_pattern status =
1020 Grammar.print_entry Format.std_formatter
1021 (Grammar.Entry.obj status#notation_parser_db.grammars.term);
1022 Format.pp_print_flush Format.std_formatter ();
1025 (* vim:set encoding=utf8 foldmethod=marker: *)