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/
27 mmodule Ast = CicNotationPtmmodule Env = CicNotationEnv
28 eexception Parse_error of Token.flocation * stringeexception Level_not_found of int
29 llet level1_pattern_grammar =
30 Grammar.gcreate CicNotationLexer.level1_pattern_lexerllet level2_ast_grammar = Grammar.gcreate CicNotationLexer.level2_ast_lexerllet level2_meta_grammar = Grammar.gcreate CicNotationLexer.level2_meta_lexer
31 llet min_precedence = 0llet max_precedence = 100
33 Grammar.Entry.create level1_pattern_grammar "level1_pattern"llet level2_ast = Grammar.Entry.create level2_ast_grammar "level2_ast"llet term = Grammar.Entry.create level2_ast_grammar "term"llet let_defs = Grammar.Entry.create level2_ast_grammar "let_defs"llet level2_meta = Grammar.Entry.create level2_meta_grammar "level2_meta"
34 llet return_term loc term = ()
35 llet int_of_string s =
36 try Pervasives.int_of_string s with
38 failwith (sprintf "Lexer failure: string_of_int \"%s\" failed" s)
39 (** {2 Grammar extension} *)
41 llet gram_symbol s = Gramext.Stoken ("SYMBOL", s)llet gram_ident s = Gramext.Stoken ("IDENT", s)llet gram_number s = Gramext.Stoken ("NUMBER", s)llet gram_keyword s = Gramext.Stoken ("", s)llet gram_term = Gramext.Sself
42 llet gram_of_literal =
44 `Symbol s -> gram_symbol s
45 | `Keyword s -> gram_keyword s
46 | `Number s -> gram_number s
49 | Binding of string * Env.value_type
50 | Env of (string * Env.value_type) list
51 llet make_action action bindings =
52 let rec aux (vl : CicNotationEnv.t) =
54 [] -> Gramext.action (fun (loc : Ast.location) -> action vl loc)
55 | NoBinding :: tl -> Gramext.action (fun _ -> aux vl tl)
56 | Binding (name, Env.TermType) :: tl ->
58 (fun (v : Ast.term) ->
59 aux ((name, (Env.TermType, Env.TermValue v)) :: vl) tl)
60 | Binding (name, Env.StringType) :: tl ->
63 aux ((name, (Env.StringType, Env.StringValue v)) :: vl) tl)
64 | Binding (name, Env.NumType) :: tl ->
67 aux ((name, (Env.NumType, Env.NumValue v)) :: vl) tl)
68 | Binding (name, Env.OptType t) :: tl ->
70 (fun (v : 'a option) ->
71 aux ((name, (Env.OptType t, Env.OptValue v)) :: vl) tl)
72 | Binding (name, Env.ListType t) :: tl ->
75 aux ((name, (Env.ListType t, Env.ListValue v)) :: vl) tl)
77 Gramext.action (fun (v : CicNotationEnv.t) -> aux (v @ vl) tl)
79 aux [] (List.rev bindings)
84 | NoBinding :: tl -> aux acc tl
85 | Env names :: tl -> aux (List.rev names @ acc) tl
86 | Binding (name, ty) :: tl -> aux ((name, ty) :: acc) tl
89 (* given a level 1 pattern computes the new RHS of "term" grammar entry *)
90 llet extract_term_production pattern =
93 Ast.AttributedTerm (_, t) -> aux t
94 | Ast.Literal l -> aux_literal l
95 | Ast.Layout l -> aux_layout l
96 | Ast.Magic m -> aux_magic m
97 | Ast.Variable v -> aux_variable v
98 | t -> prerr_endline (CicNotationPp.pp_term t); assert false
101 `Symbol s -> [NoBinding, gram_symbol s]
102 | `Keyword s -> [NoBinding, gram_keyword s]
103 | `Number s -> [NoBinding, gram_number s]
106 Ast.Sub (p1, p2) -> aux p1 @ [NoBinding, gram_symbol "\\sub"] @ aux p2
107 | Ast.Sup (p1, p2) -> aux p1 @ [NoBinding, gram_symbol "\\sup"] @ aux p2
108 | Ast.Below (p1, p2) ->
109 aux p1 @ [NoBinding, gram_symbol "\\below"] @ aux p2
110 | Ast.Above (p1, p2) ->
111 aux p1 @ [NoBinding, gram_symbol "\\above"] @ aux p2
112 | Ast.Frac (p1, p2) -> aux p1 @ [NoBinding, gram_symbol "\\frac"] @ aux p2
113 | Ast.Atop (p1, p2) -> aux p1 @ [NoBinding, gram_symbol "\\atop"] @ aux p2
114 | Ast.Over (p1, p2) -> aux p1 @ [NoBinding, gram_symbol "\\over"] @ aux p2
115 | Ast.Root (p1, p2) ->
116 [NoBinding, gram_symbol "\\root"] @ aux p2 @
117 [NoBinding, gram_symbol "\\of"] @ aux p1
118 | Ast.Sqrt p -> [NoBinding, gram_symbol "\\sqrt"] @ aux p
120 | Ast.Box (_, pl) -> List.flatten (List.map aux pl)
121 | Ast.Group pl -> List.flatten (List.map aux pl)
122 and aux_magic magic =
125 let (p_bindings, p_atoms, p_names, p_action) = inner_pattern p in
126 let action (env_opt : CicNotationEnv.t option) (loc : Ast.location) =
128 Some env -> List.map Env.opt_binding_some env
129 | None -> List.map Env.opt_binding_of_name p_names
131 [Env (List.map Env.opt_declaration p_names),
133 [[Gramext.Sopt (Gramext.srules [p_atoms, p_action])],
134 Gramext.action action]]
135 | Ast.List0 (p, _) | Ast.List1 (p, _) ->
136 let (p_bindings, p_atoms, p_names, p_action) = inner_pattern p in
137 let action (env_list : CicNotationEnv.t list) (loc : Ast.location) =
138 CicNotationEnv.coalesce_env p_names env_list
142 Ast.List0 (_, None) -> Gramext.Slist0 s
143 | Ast.List1 (_, None) -> Gramext.Slist1 s
144 | Ast.List0 (_, Some l) -> Gramext.Slist0sep (s, gram_of_literal l)
145 | Ast.List1 (_, Some l) -> Gramext.Slist1sep (s, gram_of_literal l)
148 [Env (List.map Env.list_declaration p_names),
150 [[gram_of_list (Gramext.srules [p_atoms, p_action])],
151 Gramext.action action]]
155 Ast.NumVar s -> [Binding (s, Env.NumType), gram_number ""]
156 | Ast.TermVar s -> [Binding (s, Env.TermType), gram_term]
157 | Ast.IdentVar s -> [Binding (s, Env.StringType), gram_ident ""]
158 | Ast.Ascription (p, s) -> assert false
159 | Ast.FreshVar _ -> assert false
160 and inner_pattern p =
161 let (p_bindings, p_atoms) = List.split (aux p) in
162 let p_names = flatten_opt p_bindings in
164 make_action (fun (env : CicNotationEnv.t) (loc : Ast.location) -> env)
167 p_bindings, p_atoms, p_names, action
171 let level_of precedence associativity =
172 if precedence < min_precedence || precedence > max_precedence then
173 raise (Level_not_found precedence);
175 match associativity with
177 | Gramext.LeftA -> "L"
178 | Gramext.RightA -> "R"
180 string_of_int precedence ^ assoc_string
182 type rule_id = Token.t Gramext.g_symbol list
184 (* mapping: rule_id -> owned keywords. (rule_id, string list) Hashtbl.t *)
185 let owned_keywords = Hashtbl.create 23
187 let extend level1_pattern ~precedence ~associativity action =
188 let (p_bindings, p_atoms) =
189 List.split (extract_term_production level1_pattern)
191 let level = level_of precedence associativity in
192 let p_names = flatten_opt p_bindings in
195 [Grammar.Entry.obj (term : 'a Grammar.Entry.e),
196 Some (Gramext.Level level),
197 [None, Some associativity,
200 (fun (env : CicNotationEnv.t) (loc : Ast.location) ->
204 let keywords = CicNotationUtil.keywords_of_term level1_pattern in
205 let rule_id = p_atoms in
206 List.iter CicNotationLexer.add_level2_ast_keyword keywords;
207 Hashtbl.add owned_keywords rule_id keywords;
211 let atoms = rule_id in
213 let keywords = Hashtbl.find owned_keywords rule_id in
214 List.iter CicNotationLexer.remove_level2_ast_keyword keywords
216 Not_found -> assert false
218 Grammar.delete_rule term atoms
222 let parse_level1_pattern_ref = ref (fun _ -> assert false)
223 let parse_level2_ast_ref = ref (fun _ -> assert false)
224 let parse_level2_meta_ref = ref (fun _ -> assert false)
226 let fold_cluster binder terms ty body =
227 List.fold_right (fun term body -> Ast.Binder (binder, (term, ty), body))
228 terms body (* terms are names: either Ident or FreshVar *)
230 let fold_exists terms ty body =
233 let lambda = Ast.Binder (`Lambda, (term, ty), body) in
234 Ast.Appl [Ast.Symbol ("exists", 0); lambda])
237 let fold_binder binder pt_names body =
238 List.fold_right (fun (names, ty) body -> fold_cluster binder names ty body)
241 let return_term loc term = Ast.AttributedTerm (`Loc loc, term)
243 (* create empty precedence level for "term" *)
247 (fun _ -> failwith "internal error, lexer generated a dummy token")
249 let dummy_prod = [[Gramext.Stoken ("DUMMY", "")], dummy_action] in
250 let mk_level_list first last =
253 i when i < first -> acc
256 ((Some (string_of_int i ^ "N"), Some Gramext.NonA, dummy_prod) ::
257 (Some (string_of_int i ^ "L"), Some Gramext.LeftA,
259 (Some (string_of_int i ^ "R"), Some Gramext.RightA,
267 [Grammar.Entry.obj (term : 'a Grammar.Entry.e), None,
268 mk_level_list min_precedence max_precedence]
270 (* {{{ Grammar for concrete syntax patterns, notation level 1 *)
273 (let _ = (level1_pattern : 'level1_pattern Grammar.Entry.e) in
274 let grammar_entry_create s =
275 Grammar.Entry.create (Grammar.of_entry level1_pattern) s
277 let l1_pattern : 'l1_pattern Grammar.Entry.e =
278 grammar_entry_create "l1_pattern"
279 and literal : 'literal Grammar.Entry.e = grammar_entry_create "literal"
280 and sep : 'sep Grammar.Entry.e = grammar_entry_create "sep"
281 and l1_magic_pattern : 'l1_magic_pattern Grammar.Entry.e =
282 grammar_entry_create "l1_magic_pattern"
283 and l1_pattern_variable : 'l1_pattern_variable Grammar.Entry.e =
284 grammar_entry_create "l1_pattern_variable"
285 and l1_simple_pattern : 'l1_simple_pattern Grammar.Entry.e =
286 grammar_entry_create "l1_simple_pattern"
288 [Grammar.Entry.obj (level1_pattern : 'level1_pattern Grammar.Entry.e),
292 (Grammar.Entry.obj (l1_pattern : 'l1_pattern Grammar.Entry.e));
293 Gramext.Stoken ("EOI", "")],
295 (fun _ (p : 'l1_pattern)
296 (loc : Lexing.position * Lexing.position) ->
297 (CicNotationUtil.boxify p : 'level1_pattern))]];
298 Grammar.Entry.obj (l1_pattern : 'l1_pattern Grammar.Entry.e), None,
303 (l1_simple_pattern : 'l1_simple_pattern Grammar.Entry.e)))],
305 (fun (p : 'l1_simple_pattern list)
306 (loc : Lexing.position * Lexing.position) ->
307 (p : 'l1_pattern))]];
308 Grammar.Entry.obj (literal : 'literal Grammar.Entry.e), None,
310 [[Gramext.Stoken ("NUMBER", "")],
312 (fun (n : string) (loc : Lexing.position * Lexing.position) ->
313 (`Number n : 'literal));
314 [Gramext.Stoken ("QKEYWORD", "")],
316 (fun (k : string) (loc : Lexing.position * Lexing.position) ->
317 (`Keyword k : 'literal));
318 [Gramext.Stoken ("SYMBOL", "")],
320 (fun (s : string) (loc : Lexing.position * Lexing.position) ->
321 (`Symbol s : 'literal))]];
322 Grammar.Entry.obj (sep : 'sep Grammar.Entry.e), None,
324 [[Gramext.Stoken ("", "sep");
326 (Grammar.Entry.obj (literal : 'literal Grammar.Entry.e))],
328 (fun (sep : 'literal) _ (loc : Lexing.position * Lexing.position) ->
331 (l1_magic_pattern : 'l1_magic_pattern Grammar.Entry.e),
334 [[Gramext.Stoken ("", "opt");
337 (l1_simple_pattern : 'l1_simple_pattern Grammar.Entry.e))],
339 (fun (p : 'l1_simple_pattern) _
340 (loc : Lexing.position * Lexing.position) ->
341 (Ast.Opt p : 'l1_magic_pattern));
342 [Gramext.Stoken ("", "list1");
345 (l1_simple_pattern : 'l1_simple_pattern Grammar.Entry.e));
347 (Gramext.Snterm (Grammar.Entry.obj (sep : 'sep Grammar.Entry.e)))],
349 (fun (sep : 'sep option) (p : 'l1_simple_pattern) _
350 (loc : Lexing.position * Lexing.position) ->
351 (Ast.List1 (p, sep) : 'l1_magic_pattern));
352 [Gramext.Stoken ("", "list0");
355 (l1_simple_pattern : 'l1_simple_pattern Grammar.Entry.e));
357 (Gramext.Snterm (Grammar.Entry.obj (sep : 'sep Grammar.Entry.e)))],
359 (fun (sep : 'sep option) (p : 'l1_simple_pattern) _
360 (loc : Lexing.position * Lexing.position) ->
361 (Ast.List0 (p, sep) : 'l1_magic_pattern))]];
363 (l1_pattern_variable : 'l1_pattern_variable Grammar.Entry.e),
366 [[Gramext.Stoken ("", "ident"); Gramext.Stoken ("IDENT", "")],
368 (fun (id : string) _ (loc : Lexing.position * Lexing.position) ->
369 (Ast.IdentVar id : 'l1_pattern_variable));
370 [Gramext.Stoken ("", "number"); Gramext.Stoken ("IDENT", "")],
372 (fun (id : string) _ (loc : Lexing.position * Lexing.position) ->
373 (Ast.NumVar id : 'l1_pattern_variable));
374 [Gramext.Stoken ("", "term"); Gramext.Stoken ("IDENT", "")],
376 (fun (id : string) _ (loc : Lexing.position * Lexing.position) ->
377 (Ast.TermVar id : 'l1_pattern_variable))]];
379 (l1_simple_pattern : 'l1_simple_pattern Grammar.Entry.e),
381 [Some "layout", Some Gramext.LeftA,
382 [[Gramext.Stoken ("LPAREN", "");
384 (Grammar.Entry.obj (l1_pattern : 'l1_pattern Grammar.Entry.e));
385 Gramext.Stoken ("RPAREN", "")],
387 (fun _ (p : 'l1_pattern) _
388 (loc : Lexing.position * Lexing.position) ->
389 (return_term loc (CicNotationUtil.group p) :
390 'l1_simple_pattern));
391 [Gramext.Stoken ("", "break")],
393 (fun _ (loc : Lexing.position * Lexing.position) ->
394 (return_term loc (Ast.Layout Ast.Break) : 'l1_simple_pattern));
395 [Gramext.Stoken ("", "hovbox"); Gramext.Stoken ("LPAREN", "");
397 (Grammar.Entry.obj (l1_pattern : 'l1_pattern Grammar.Entry.e));
398 Gramext.Stoken ("RPAREN", "")],
400 (fun _ (p : 'l1_pattern) _ _
401 (loc : Lexing.position * Lexing.position) ->
403 (Ast.Layout (Ast.Box ((Ast.HOV, false, false), p))) :
404 'l1_simple_pattern));
405 [Gramext.Stoken ("", "hvbox"); Gramext.Stoken ("LPAREN", "");
407 (Grammar.Entry.obj (l1_pattern : 'l1_pattern Grammar.Entry.e));
408 Gramext.Stoken ("RPAREN", "")],
410 (fun _ (p : 'l1_pattern) _ _
411 (loc : Lexing.position * Lexing.position) ->
413 (Ast.Layout (Ast.Box ((Ast.HV, false, false), p))) :
414 'l1_simple_pattern));
415 [Gramext.Stoken ("", "vbox"); Gramext.Stoken ("LPAREN", "");
417 (Grammar.Entry.obj (l1_pattern : 'l1_pattern Grammar.Entry.e));
418 Gramext.Stoken ("RPAREN", "")],
420 (fun _ (p : 'l1_pattern) _ _
421 (loc : Lexing.position * Lexing.position) ->
423 (Ast.Layout (Ast.Box ((Ast.V, false, false), p))) :
424 'l1_simple_pattern));
425 [Gramext.Stoken ("", "hbox"); Gramext.Stoken ("LPAREN", "");
427 (Grammar.Entry.obj (l1_pattern : 'l1_pattern Grammar.Entry.e));
428 Gramext.Stoken ("RPAREN", "")],
430 (fun _ (p : 'l1_pattern) _ _
431 (loc : Lexing.position * Lexing.position) ->
433 (Ast.Layout (Ast.Box ((Ast.H, false, false), p))) :
434 'l1_simple_pattern));
435 [Gramext.Stoken ("SYMBOL", "\\root"); Gramext.Sself;
436 Gramext.Stoken ("SYMBOL", "\\of"); Gramext.Sself],
438 (fun (arg : 'l1_simple_pattern) _ (index : 'l1_simple_pattern) _
439 (loc : Lexing.position * Lexing.position) ->
440 (return_term loc (Ast.Layout (Ast.Root (arg, index))) :
441 'l1_simple_pattern));
442 [Gramext.Stoken ("SYMBOL", "\\sqrt"); Gramext.Sself],
444 (fun (p : 'l1_simple_pattern) _
445 (loc : Lexing.position * Lexing.position) ->
446 (return_term loc (Ast.Layout (Ast.Sqrt p)) :
447 'l1_simple_pattern));
448 [Gramext.Stoken ("SYMBOL", "\\frac"); Gramext.Sself; Gramext.Sself],
450 (fun (p2 : 'l1_simple_pattern) (p1 : 'l1_simple_pattern) _
451 (loc : Lexing.position * Lexing.position) ->
452 (return_term loc (Ast.Layout (Ast.Frac (p1, p2))) :
453 'l1_simple_pattern));
454 [Gramext.Sself; Gramext.Stoken ("SYMBOL", "\\atop"); Gramext.Sself],
456 (fun (p2 : 'l1_simple_pattern) _ (p1 : 'l1_simple_pattern)
457 (loc : Lexing.position * Lexing.position) ->
458 (return_term loc (Ast.Layout (Ast.Atop (p1, p2))) :
459 'l1_simple_pattern));
460 [Gramext.Sself; Gramext.Stoken ("SYMBOL", "\\over"); Gramext.Sself],
462 (fun (p2 : 'l1_simple_pattern) _ (p1 : 'l1_simple_pattern)
463 (loc : Lexing.position * Lexing.position) ->
464 (return_term loc (Ast.Layout (Ast.Over (p1, p2))) :
465 'l1_simple_pattern));
466 [Gramext.Sself; Gramext.Stoken ("SYMBOL", "\\above"); Gramext.Sself],
468 (fun (p2 : 'l1_simple_pattern) _ (p1 : 'l1_simple_pattern)
469 (loc : Lexing.position * Lexing.position) ->
470 (return_term loc (Ast.Layout (Ast.Above (p1, p2))) :
471 'l1_simple_pattern));
472 [Gramext.Sself; Gramext.Stoken ("SYMBOL", "\\below"); Gramext.Sself],
474 (fun (p2 : 'l1_simple_pattern) _ (p1 : 'l1_simple_pattern)
475 (loc : Lexing.position * Lexing.position) ->
476 (return_term loc (Ast.Layout (Ast.Below (p1, p2))) :
477 'l1_simple_pattern));
478 [Gramext.Sself; Gramext.Stoken ("SYMBOL", "\\sup"); Gramext.Sself],
480 (fun (p2 : 'l1_simple_pattern) _ (p1 : 'l1_simple_pattern)
481 (loc : Lexing.position * Lexing.position) ->
482 (return_term loc (Ast.Layout (Ast.Sup (p1, p2))) :
483 'l1_simple_pattern));
484 [Gramext.Sself; Gramext.Stoken ("SYMBOL", "\\sub"); Gramext.Sself],
486 (fun (p2 : 'l1_simple_pattern) _ (p1 : 'l1_simple_pattern)
487 (loc : Lexing.position * Lexing.position) ->
488 (return_term loc (Ast.Layout (Ast.Sub (p1, p2))) :
489 'l1_simple_pattern))];
490 Some "simple", Some Gramext.NonA,
492 (Grammar.Entry.obj (literal : 'literal Grammar.Entry.e))],
494 (fun (l : 'literal) (loc : Lexing.position * Lexing.position) ->
495 (return_term loc (Ast.Literal l) : 'l1_simple_pattern));
498 (l1_pattern_variable : 'l1_pattern_variable Grammar.Entry.e))],
500 (fun (v : 'l1_pattern_variable)
501 (loc : Lexing.position * Lexing.position) ->
502 (return_term loc (Ast.Variable v) : 'l1_simple_pattern));
505 (l1_magic_pattern : 'l1_magic_pattern Grammar.Entry.e))],
507 (fun (m : 'l1_magic_pattern)
508 (loc : Lexing.position * Lexing.position) ->
509 (return_term loc (Ast.Magic m) : 'l1_simple_pattern));
510 [Gramext.Stoken ("IDENT", "")],
512 (fun (i : string) (loc : Lexing.position * Lexing.position) ->
513 (return_term loc (Ast.Variable (Ast.TermVar i)) :
514 'l1_simple_pattern))]]])
517 (* {{{ Grammar for ast magics, notation level 2 *)
520 (let _ = (level2_meta : 'level2_meta Grammar.Entry.e) in
521 let grammar_entry_create s =
522 Grammar.Entry.create (Grammar.of_entry level2_meta) s
524 let l2_variable : 'l2_variable Grammar.Entry.e =
525 grammar_entry_create "l2_variable"
526 and l2_magic : 'l2_magic Grammar.Entry.e =
527 grammar_entry_create "l2_magic"
529 [Grammar.Entry.obj (l2_variable : 'l2_variable Grammar.Entry.e), None,
531 [[Gramext.Stoken ("IDENT", "")],
533 (fun (id : string) (loc : Lexing.position * Lexing.position) ->
534 (Ast.TermVar id : 'l2_variable));
535 [Gramext.Stoken ("", "anonymous")],
537 (fun _ (loc : Lexing.position * Lexing.position) ->
538 (Ast.TermVar "_" : 'l2_variable));
539 [Gramext.Stoken ("", "fresh"); Gramext.Stoken ("IDENT", "")],
541 (fun (id : string) _ (loc : Lexing.position * Lexing.position) ->
542 (Ast.FreshVar id : 'l2_variable));
543 [Gramext.Stoken ("", "ident"); Gramext.Stoken ("IDENT", "")],
545 (fun (id : string) _ (loc : Lexing.position * Lexing.position) ->
546 (Ast.IdentVar id : 'l2_variable));
547 [Gramext.Stoken ("", "number"); Gramext.Stoken ("IDENT", "")],
549 (fun (id : string) _ (loc : Lexing.position * Lexing.position) ->
550 (Ast.NumVar id : 'l2_variable));
551 [Gramext.Stoken ("", "term"); Gramext.Stoken ("IDENT", "")],
553 (fun (id : string) _ (loc : Lexing.position * Lexing.position) ->
554 (Ast.TermVar id : 'l2_variable))]];
555 Grammar.Entry.obj (l2_magic : 'l2_magic Grammar.Entry.e), None,
557 [[Gramext.Stoken ("", "fail")],
559 (fun _ (loc : Lexing.position * Lexing.position) ->
560 (Ast.Fail : 'l2_magic));
561 [Gramext.Stoken ("", "if");
563 (Grammar.Entry.obj (level2_meta : 'level2_meta Grammar.Entry.e));
564 Gramext.Stoken ("", "then");
566 (Grammar.Entry.obj (level2_meta : 'level2_meta Grammar.Entry.e));
567 Gramext.Stoken ("", "else");
569 (Grammar.Entry.obj (level2_meta : 'level2_meta Grammar.Entry.e))],
571 (fun (p_false : 'level2_meta) _ (p_true : 'level2_meta) _
572 (p_test : 'level2_meta) _
573 (loc : Lexing.position * Lexing.position) ->
574 (Ast.If (p_test, p_true, p_false) : 'l2_magic));
575 [Gramext.Stoken ("", "default");
577 (Grammar.Entry.obj (level2_meta : 'level2_meta Grammar.Entry.e));
579 (Grammar.Entry.obj (level2_meta : 'level2_meta Grammar.Entry.e))],
581 (fun (none : 'level2_meta) (some : 'level2_meta) _
582 (loc : Lexing.position * Lexing.position) ->
583 (Ast.Default (some, none) : 'l2_magic));
584 [Gramext.Stoken ("", "fold");
586 [[Gramext.Stoken ("", "right")],
588 (fun _ (loc : Lexing.position * Lexing.position) ->
590 [Gramext.Stoken ("", "left")],
592 (fun _ (loc : Lexing.position * Lexing.position) ->
595 (Grammar.Entry.obj (level2_meta : 'level2_meta Grammar.Entry.e));
596 Gramext.Stoken ("", "rec"); Gramext.Stoken ("IDENT", "");
598 (Grammar.Entry.obj (level2_meta : 'level2_meta Grammar.Entry.e))],
600 (fun (recursive : 'level2_meta) (id : string) _
601 (base : 'level2_meta) (kind : 'e__1) _
602 (loc : Lexing.position * Lexing.position) ->
603 (Ast.Fold (kind, base, [id], recursive) : 'l2_magic))]];
604 Grammar.Entry.obj (level2_meta : 'level2_meta Grammar.Entry.e), None,
606 [[Gramext.Stoken ("UNPARSED_AST", "")],
608 (fun (blob : string) (loc : Lexing.position * Lexing.position) ->
609 (!parse_level2_ast_ref (Ulexing.from_utf8_string blob) :
612 (Grammar.Entry.obj (l2_variable : 'l2_variable Grammar.Entry.e))],
614 (fun (var : 'l2_variable)
615 (loc : Lexing.position * Lexing.position) ->
616 (Ast.Variable var : 'level2_meta));
618 (Grammar.Entry.obj (l2_magic : 'l2_magic Grammar.Entry.e))],
620 (fun (magic : 'l2_magic)
621 (loc : Lexing.position * Lexing.position) ->
622 (Ast.Magic magic : 'level2_meta))]]])
625 (* {{{ Grammar for ast patterns, notation level 2 *)
628 (let _ = (level2_ast : 'level2_ast Grammar.Entry.e)
629 and _ = (term : 'term Grammar.Entry.e)
630 and _ = (let_defs : 'let_defs Grammar.Entry.e) in
631 let grammar_entry_create s =
632 Grammar.Entry.create (Grammar.of_entry level2_ast) s
634 let sort : 'sort Grammar.Entry.e = grammar_entry_create "sort"
635 and explicit_subst : 'explicit_subst Grammar.Entry.e =
636 grammar_entry_create "explicit_subst"
637 and meta_subst : 'meta_subst Grammar.Entry.e =
638 grammar_entry_create "meta_subst"
639 and meta_substs : 'meta_substs Grammar.Entry.e =
640 grammar_entry_create "meta_substs"
641 and possibly_typed_name : 'possibly_typed_name Grammar.Entry.e =
642 grammar_entry_create "possibly_typed_name"
643 and match_pattern : 'match_pattern Grammar.Entry.e =
644 grammar_entry_create "match_pattern"
645 and binder : 'binder Grammar.Entry.e = grammar_entry_create "binder"
646 and arg : 'arg Grammar.Entry.e = grammar_entry_create "arg"
647 and single_arg : 'single_arg Grammar.Entry.e =
648 grammar_entry_create "single_arg"
649 and induction_kind : 'induction_kind Grammar.Entry.e =
650 grammar_entry_create "induction_kind"
651 and binder_vars : 'binder_vars Grammar.Entry.e =
652 grammar_entry_create "binder_vars"
654 [Grammar.Entry.obj (level2_ast : 'level2_ast Grammar.Entry.e), None,
656 [[Gramext.Snterm (Grammar.Entry.obj (term : 'term Grammar.Entry.e))],
658 (fun (p : 'term) (loc : Lexing.position * Lexing.position) ->
659 (p : 'level2_ast))]];
660 Grammar.Entry.obj (sort : 'sort Grammar.Entry.e), None,
662 [[Gramext.Stoken ("", "CProp")],
664 (fun _ (loc : Lexing.position * Lexing.position) ->
666 [Gramext.Stoken ("", "Type")],
668 (fun _ (loc : Lexing.position * Lexing.position) ->
670 [Gramext.Stoken ("", "Set")],
672 (fun _ (loc : Lexing.position * Lexing.position) -> (`Set : 'sort));
673 [Gramext.Stoken ("", "Prop")],
675 (fun _ (loc : Lexing.position * Lexing.position) ->
677 Grammar.Entry.obj (explicit_subst : 'explicit_subst Grammar.Entry.e),
680 [[Gramext.Stoken ("SYMBOL", "\\subst"); Gramext.Stoken ("SYMBOL", "[");
683 [[Gramext.Stoken ("IDENT", "");
684 Gramext.Stoken ("SYMBOL", "≔");
686 (Grammar.Entry.obj (term : 'term Grammar.Entry.e))],
688 (fun (t : 'term) _ (i : string)
689 (loc : Lexing.position * Lexing.position) ->
691 Gramext.Stoken ("SYMBOL", ";"));
692 Gramext.Stoken ("SYMBOL", "]")],
694 (fun _ (substs : 'e__2 list) _ _
695 (loc : Lexing.position * Lexing.position) ->
696 (substs : 'explicit_subst))]];
697 Grammar.Entry.obj (meta_subst : 'meta_subst Grammar.Entry.e), None,
699 [[Gramext.Snterm (Grammar.Entry.obj (term : 'term Grammar.Entry.e))],
701 (fun (p : 'term) (loc : Lexing.position * Lexing.position) ->
702 (Some p : 'meta_subst));
703 [Gramext.Stoken ("SYMBOL", "_")],
705 (fun (s : string) (loc : Lexing.position * Lexing.position) ->
706 (None : 'meta_subst))]];
707 Grammar.Entry.obj (meta_substs : 'meta_substs Grammar.Entry.e), None,
709 [[Gramext.Stoken ("SYMBOL", "[");
712 (Grammar.Entry.obj (meta_subst : 'meta_subst Grammar.Entry.e)));
713 Gramext.Stoken ("SYMBOL", "]")],
715 (fun _ (substs : 'meta_subst list) _
716 (loc : Lexing.position * Lexing.position) ->
717 (substs : 'meta_substs))]];
719 (possibly_typed_name : 'possibly_typed_name Grammar.Entry.e),
723 (Grammar.Entry.obj (single_arg : 'single_arg Grammar.Entry.e))],
725 (fun (arg : 'single_arg)
726 (loc : Lexing.position * Lexing.position) ->
727 (arg, None : 'possibly_typed_name));
728 [Gramext.Stoken ("LPAREN", "");
730 (Grammar.Entry.obj (single_arg : 'single_arg Grammar.Entry.e));
731 Gramext.Stoken ("SYMBOL", ":");
732 Gramext.Snterm (Grammar.Entry.obj (term : 'term Grammar.Entry.e));
733 Gramext.Stoken ("RPAREN", "")],
735 (fun _ (typ : 'term) _ (id : 'single_arg) _
736 (loc : Lexing.position * Lexing.position) ->
737 (id, Some typ : 'possibly_typed_name))]];
738 Grammar.Entry.obj (match_pattern : 'match_pattern Grammar.Entry.e),
741 [[Gramext.Stoken ("LPAREN", ""); Gramext.Stoken ("IDENT", "");
745 (possibly_typed_name :
746 'possibly_typed_name Grammar.Entry.e)));
747 Gramext.Stoken ("RPAREN", "")],
749 (fun _ (vars : 'possibly_typed_name list) (id : string) _
750 (loc : Lexing.position * Lexing.position) ->
751 (id, None, vars : 'match_pattern));
752 [Gramext.Stoken ("IDENT", "")],
754 (fun (id : string) (loc : Lexing.position * Lexing.position) ->
755 (id, None, [] : 'match_pattern))]];
756 Grammar.Entry.obj (binder : 'binder Grammar.Entry.e), None,
758 [[Gramext.Stoken ("SYMBOL", "λ")],
760 (fun _ (loc : Lexing.position * Lexing.position) ->
761 (`Lambda : 'binder));
762 [Gramext.Stoken ("SYMBOL", "∀")],
764 (fun _ (loc : Lexing.position * Lexing.position) ->
765 (`Forall : 'binder));
766 [Gramext.Stoken ("SYMBOL", "Π")],
768 (fun _ (loc : Lexing.position * Lexing.position) ->
770 Grammar.Entry.obj (arg : 'arg Grammar.Entry.e), None,
772 [[Gramext.Stoken ("UNPARSED_META", "")],
774 (fun (blob : string) (loc : Lexing.position * Lexing.position) ->
776 !parse_level2_meta_ref (Ulexing.from_utf8_string blob)
779 Ast.Variable (Ast.FreshVar _) -> [meta], None
780 | Ast.Variable (Ast.TermVar "_") ->
781 [Ast.Ident ("_", None)], None
782 | _ -> failwith "Invalid bound name." :
784 [Gramext.Stoken ("IDENT", "")],
786 (fun (name : string) (loc : Lexing.position * Lexing.position) ->
787 ([Ast.Ident (name, None)], None : 'arg));
788 [Gramext.Stoken ("LPAREN", "");
790 (Gramext.Stoken ("IDENT", ""), Gramext.Stoken ("SYMBOL", ","));
791 Gramext.Stoken ("SYMBOL", ":");
792 Gramext.Snterm (Grammar.Entry.obj (term : 'term Grammar.Entry.e));
793 Gramext.Stoken ("RPAREN", "")],
795 (fun _ (ty : 'term) _ (names : string list) _
796 (loc : Lexing.position * Lexing.position) ->
797 (List.map (fun n -> Ast.Ident (n, None)) names, Some ty :
799 Grammar.Entry.obj (single_arg : 'single_arg Grammar.Entry.e), None,
801 [[Gramext.Stoken ("UNPARSED_META", "")],
803 (fun (blob : string) (loc : Lexing.position * Lexing.position) ->
805 !parse_level2_meta_ref (Ulexing.from_utf8_string blob)
808 Ast.Variable (Ast.FreshVar _) |
809 Ast.Variable (Ast.IdentVar _) ->
811 | Ast.Variable (Ast.TermVar "_") -> Ast.Ident ("_", None)
812 | _ -> failwith "Invalid index name." :
814 [Gramext.Stoken ("IDENT", "")],
816 (fun (name : string) (loc : Lexing.position * Lexing.position) ->
817 (Ast.Ident (name, None) : 'single_arg))]];
818 Grammar.Entry.obj (induction_kind : 'induction_kind Grammar.Entry.e),
821 [[Gramext.Stoken ("", "corec")],
823 (fun _ (loc : Lexing.position * Lexing.position) ->
824 (`CoInductive : 'induction_kind));
825 [Gramext.Stoken ("", "rec")],
827 (fun _ (loc : Lexing.position * Lexing.position) ->
828 (`Inductive : 'induction_kind))]];
829 Grammar.Entry.obj (let_defs : 'let_defs Grammar.Entry.e), None,
835 (single_arg : 'single_arg Grammar.Entry.e));
838 (Grammar.Entry.obj (arg : 'arg Grammar.Entry.e)));
841 [[Gramext.Stoken ("", "on");
844 (single_arg : 'single_arg Grammar.Entry.e))],
846 (fun (id : 'single_arg) _
847 (loc : Lexing.position * Lexing.position) ->
851 [[Gramext.Stoken ("SYMBOL", ":");
853 (Grammar.Entry.obj (term : 'term Grammar.Entry.e))],
856 (loc : Lexing.position * Lexing.position) ->
858 Gramext.Stoken ("SYMBOL", "≝");
860 (Grammar.Entry.obj (term : 'term Grammar.Entry.e))],
862 (fun (body : 'term) _ (ty : 'e__4 option)
863 (index_name : 'e__3 option) (args : 'arg list)
865 (loc : Lexing.position * Lexing.position) ->
866 (let body = fold_binder `Lambda args body in
870 | Some ty -> Some (fold_binder `Pi args ty)
872 let rec position_of name p =
875 | n :: _ when n = name -> Some p, p
876 | _ :: tl -> position_of name (p + 1) tl
878 let rec find_arg name n =
882 (sprintf "Argument %s not found"
883 (CicNotationPp.pp_term name))
885 match position_of name 0 l with
886 None, len -> find_arg name (n + len) tl
887 | Some where, len -> n + where
890 match index_name with
892 | Some index_name -> find_arg index_name 0 args
894 (name, ty), body, index :
896 Gramext.Stoken ("", "and"))],
898 (fun (defs : 'e__5 list)
899 (loc : Lexing.position * Lexing.position) ->
900 (defs : 'let_defs))]];
901 Grammar.Entry.obj (binder_vars : 'binder_vars Grammar.Entry.e), None,
903 [[Gramext.Stoken ("LPAREN", "");
905 [[Gramext.Stoken ("SYMBOL", "_")],
907 (fun _ (loc : Lexing.position * Lexing.position) ->
908 ([Ast.Ident ("_", None)] : 'e__8));
912 (single_arg : 'single_arg Grammar.Entry.e)),
913 Gramext.Stoken ("SYMBOL", ","))],
915 (fun (l : 'single_arg list)
916 (loc : Lexing.position * Lexing.position) ->
920 [[Gramext.Stoken ("SYMBOL", ":");
922 (Grammar.Entry.obj (term : 'term Grammar.Entry.e))],
925 (loc : Lexing.position * Lexing.position) ->
927 Gramext.Stoken ("RPAREN", "")],
929 (fun _ (typ : 'e__9 option) (vars : 'e__8) _
930 (loc : Lexing.position * Lexing.position) ->
931 (vars, typ : 'binder_vars));
933 [[Gramext.Stoken ("SYMBOL", "_")],
935 (fun _ (loc : Lexing.position * Lexing.position) ->
936 ([Ast.Ident ("_", None)] : 'e__6));
940 (single_arg : 'single_arg Grammar.Entry.e)),
941 Gramext.Stoken ("SYMBOL", ","))],
943 (fun (l : 'single_arg list)
944 (loc : Lexing.position * Lexing.position) ->
948 [[Gramext.Stoken ("SYMBOL", ":");
950 (Grammar.Entry.obj (term : 'term Grammar.Entry.e))],
953 (loc : Lexing.position * Lexing.position) ->
956 (fun (typ : 'e__7 option) (vars : 'e__6)
957 (loc : Lexing.position * Lexing.position) ->
958 (vars, typ : 'binder_vars))]];
959 Grammar.Entry.obj (term : 'term Grammar.Entry.e),
960 Some (Gramext.Level "10N"),
962 [[Gramext.Stoken ("", "let");
965 (induction_kind : 'induction_kind Grammar.Entry.e));
967 (Grammar.Entry.obj (let_defs : 'let_defs Grammar.Entry.e));
968 Gramext.Stoken ("", "in"); Gramext.Sself],
970 (fun (body : 'term) _ (defs : 'let_defs) (k : 'induction_kind) _
971 (loc : Lexing.position * Lexing.position) ->
972 (return_term loc (Ast.LetRec (k, defs, body)) : 'term));
973 [Gramext.Stoken ("", "let");
976 (possibly_typed_name : 'possibly_typed_name Grammar.Entry.e));
977 Gramext.Stoken ("SYMBOL", "≝"); Gramext.Sself;
978 Gramext.Stoken ("", "in"); Gramext.Sself],
980 (fun (p2 : 'term) _ (p1 : 'term) _ (var : 'possibly_typed_name) _
981 (loc : Lexing.position * Lexing.position) ->
982 (return_term loc (Ast.LetIn (var, p1, p2)) : 'term))]];
983 Grammar.Entry.obj (term : 'term Grammar.Entry.e),
984 Some (Gramext.Level "20R"),
986 [[Gramext.Stoken ("SYMBOL", "∃");
988 (Grammar.Entry.obj (binder_vars : 'binder_vars Grammar.Entry.e));
989 Gramext.Stoken ("SYMBOL", "."); Gramext.Sself],
991 (fun (body : 'term) _ (vars, typ : 'binder_vars) _
992 (loc : Lexing.position * Lexing.position) ->
993 (return_term loc (fold_exists vars typ body) : 'term));
995 (Grammar.Entry.obj (binder : 'binder Grammar.Entry.e));
997 (Grammar.Entry.obj (binder_vars : 'binder_vars Grammar.Entry.e));
998 Gramext.Stoken ("SYMBOL", "."); Gramext.Sself],
1000 (fun (body : 'term) _ (vars, typ : 'binder_vars) (b : 'binder)
1001 (loc : Lexing.position * Lexing.position) ->
1002 (return_term loc (fold_cluster b vars typ body) : 'term))]];
1003 Grammar.Entry.obj (term : 'term Grammar.Entry.e),
1004 Some (Gramext.Level "70L"),
1006 [[Gramext.Sself; Gramext.Sself],
1008 (fun (p2 : 'term) (p1 : 'term)
1009 (loc : Lexing.position * Lexing.position) ->
1012 Ast.Appl (hd :: tl) |
1013 Ast.AttributedTerm (_, Ast.Appl (hd :: tl)) ->
1017 return_term loc (Ast.Appl (aux p1 @ [p2])) :
1019 Grammar.Entry.obj (term : 'term Grammar.Entry.e),
1020 Some (Gramext.Level "90N"),
1022 [[Gramext.Stoken ("UNPARSED_META", "")],
1024 (fun (blob : string) (loc : Lexing.position * Lexing.position) ->
1025 (!parse_level2_meta_ref (Ulexing.from_utf8_string blob) :
1027 [Gramext.Stoken ("LPAREN", ""); Gramext.Sself;
1028 Gramext.Stoken ("RPAREN", "")],
1030 (fun _ (p : 'term) _ (loc : Lexing.position * Lexing.position) ->
1032 [Gramext.Stoken ("LPAREN", ""); Gramext.Sself;
1033 Gramext.Stoken ("SYMBOL", ":"); Gramext.Sself;
1034 Gramext.Stoken ("RPAREN", "")],
1036 (fun _ (p2 : 'term) _ (p1 : 'term) _
1037 (loc : Lexing.position * Lexing.position) ->
1038 (return_term loc (Ast.Cast (p1, p2)) : 'term));
1041 [[Gramext.Stoken ("SYMBOL", "[");
1043 (Grammar.Entry.obj (term : 'term Grammar.Entry.e));
1044 Gramext.Stoken ("SYMBOL", "]")],
1046 (fun _ (ty : 'term) _
1047 (loc : Lexing.position * Lexing.position) ->
1049 Gramext.Stoken ("", "match"); Gramext.Sself;
1052 [[Gramext.Stoken ("", "in"); Gramext.Stoken ("IDENT", "")],
1054 (fun (id : string) _
1055 (loc : Lexing.position * Lexing.position) ->
1056 (id, None : 'e__11))]);
1057 Gramext.Stoken ("", "with"); Gramext.Stoken ("SYMBOL", "[");
1062 (match_pattern : 'match_pattern Grammar.Entry.e));
1063 Gramext.Stoken ("SYMBOL", "⇒");
1065 (Grammar.Entry.obj (term : 'term Grammar.Entry.e))],
1067 (fun (rhs : 'term) _ (lhs : 'match_pattern)
1068 (loc : Lexing.position * Lexing.position) ->
1069 (lhs, rhs : 'e__12))],
1070 Gramext.Stoken ("SYMBOL", "|"));
1071 Gramext.Stoken ("SYMBOL", "]")],
1073 (fun _ (patterns : 'e__12 list) _ _ (indty_ident : 'e__11 option)
1074 (t : 'term) _ (outtyp : 'e__10 option)
1075 (loc : Lexing.position * Lexing.position) ->
1076 (return_term loc (Ast.Case (t, indty_ident, outtyp, patterns)) :
1078 [Gramext.Snterm (Grammar.Entry.obj (sort : 'sort Grammar.Entry.e))],
1080 (fun (s : 'sort) (loc : Lexing.position * Lexing.position) ->
1081 (return_term loc (Ast.Sort s) : 'term));
1082 [Gramext.Stoken ("META", "");
1084 (Grammar.Entry.obj (meta_substs : 'meta_substs Grammar.Entry.e))],
1086 (fun (s : 'meta_substs) (m : string)
1087 (loc : Lexing.position * Lexing.position) ->
1088 (return_term loc (Ast.Meta (int_of_string m, s)) : 'term));
1089 [Gramext.Stoken ("META", "")],
1091 (fun (m : string) (loc : Lexing.position * Lexing.position) ->
1092 (return_term loc (Ast.Meta (int_of_string m, [])) : 'term));
1093 [Gramext.Stoken ("PLACEHOLDER", "")],
1095 (fun _ (loc : Lexing.position * Lexing.position) ->
1096 (return_term loc Ast.UserInput : 'term));
1097 [Gramext.Stoken ("IMPLICIT", "")],
1099 (fun _ (loc : Lexing.position * Lexing.position) ->
1100 (return_term loc Ast.Implicit : 'term));
1101 [Gramext.Stoken ("NUMBER", "")],
1103 (fun (n : string) (loc : Lexing.position * Lexing.position) ->
1104 (return_term loc (Ast.Num (n, 0)) : 'term));
1105 [Gramext.Stoken ("URI", "")],
1107 (fun (u : string) (loc : Lexing.position * Lexing.position) ->
1108 (return_term loc (Ast.Uri (u, None)) : 'term));
1109 [Gramext.Stoken ("CSYMBOL", "")],
1111 (fun (s : string) (loc : Lexing.position * Lexing.position) ->
1112 (return_term loc (Ast.Symbol (s, 0)) : 'term));
1113 [Gramext.Stoken ("IDENT", "");
1116 (explicit_subst : 'explicit_subst Grammar.Entry.e))],
1118 (fun (s : 'explicit_subst) (id : string)
1119 (loc : Lexing.position * Lexing.position) ->
1120 (return_term loc (Ast.Ident (id, Some s)) : 'term));
1121 [Gramext.Stoken ("IDENT", "")],
1123 (fun (id : string) (loc : Lexing.position * Lexing.position) ->
1124 (return_term loc (Ast.Ident (id, None)) : 'term))]]])
1127 (** {2 API implementation} *)
1129 let exc_located_wrapper f =
1131 Stdpp.Exc_located (floc, Stream.Error msg) ->
1132 raise (Parse_error (floc, msg))
1133 | Stdpp.Exc_located (floc, exn) ->
1134 raise (Parse_error (floc, Printexc.to_string exn))
1136 let parse_level1_pattern lexbuf =
1137 CicNotationLexer.set_lexbuf lexbuf;
1139 (fun () -> Grammar.Entry.parse level1_pattern Stream.sempty)
1141 let parse_level2_ast lexbuf =
1142 CicNotationLexer.set_lexbuf lexbuf;
1143 exc_located_wrapper (fun () -> Grammar.Entry.parse level2_ast Stream.sempty)
1145 let parse_level2_meta lexbuf =
1146 CicNotationLexer.set_lexbuf lexbuf;
1148 (fun () -> Grammar.Entry.parse level2_meta Stream.sempty)
1151 parse_level1_pattern_ref := parse_level1_pattern;
1152 parse_level2_ast_ref := parse_level2_ast;
1153 parse_level2_meta_ref := parse_level2_meta
1155 (** {2 Debugging} *)
1157 let print_l2_pattern () =
1158 Grammar.print_entry Format.std_formatter (Grammar.Entry.obj term);
1159 Format.pp_print_flush Format.std_formatter ();
1162 (* vim:set encoding=utf8 foldmethod=marker: *)