+(** {2 Grammar extension} *)
+
+llet symbol s = Gramext.Stoken ("SYMBOL", s)llet ident s = Gramext.Stoken ("IDENT", s)llet number s = Gramext.Stoken ("NUMBER", s)llet term = Gramext.Sself
+llet g_symbol_of_literal =
+ function
+ `Symbol s -> symbol s
+ | `Keyword s -> ident s
+ | `Number s -> number s
+ttype binding =
+ NoBinding
+ | Binding of string * value_type
+ | Env of (string * value_type) list
+llet make_action action bindings =
+ let rec aux (vl : env_type) =
+ function
+ [] -> Gramext.action (fun (loc : location) -> action vl loc)
+ | NoBinding :: tl -> Gramext.action (fun _ -> aux vl tl)
+ | Binding (name, TermType) :: tl ->
+ Gramext.action
+ (fun (v : term) -> aux ((name, (TermType, TermValue v)) :: vl) tl)
+ | Binding (name, StringType) :: tl ->
+ Gramext.action
+ (fun (v : string) ->
+ aux ((name, (StringType, StringValue v)) :: vl) tl)
+ | Binding (name, NumType) :: tl ->
+ Gramext.action
+ (fun (v : string) -> aux ((name, (NumType, NumValue v)) :: vl) tl)
+ | Binding (name, OptType t) :: tl ->
+ Gramext.action
+ (fun (v : 'a option) ->
+ aux ((name, (OptType t, OptValue v)) :: vl) tl)
+ | Binding (name, ListType t) :: tl ->
+ Gramext.action
+ (fun (v : 'a list) ->
+ aux ((name, (ListType t, ListValue v)) :: vl) tl)
+ | Env _ :: tl -> Gramext.action (fun (v : env_type) -> aux (v @ vl) tl)
+ in
+ aux [] (List.rev bindings)
+llet flatten_opt =
+ let rec aux acc =
+ function
+ [] -> List.rev acc
+ | NoBinding :: tl -> aux acc tl
+ | Env names :: tl -> aux (List.rev names @ acc) tl
+ | Binding (name, ty) :: tl -> aux ((name, ty) :: acc) tl
+ in
+ aux []
+
+ (* given a level 1 pattern computes the new RHS of "term" grammar entry *)
+let extract_term_production pattern =
+ let rec aux =
+ function
+ AttributedTerm (_, t) -> aux t
+ | Literal l -> aux_literal l
+ | Layout l -> aux_layout l
+ | Magic m -> aux_magic m
+ | Variable v -> aux_variable v
+ | t ->
+ prerr_endline (CicNotationPp.pp_term t);
+ assert false
+ and aux_literal =
+ function
+ `Symbol s -> [NoBinding, symbol s]
+ | `Keyword s -> [NoBinding, ident s]
+ | `Number s -> [NoBinding, number s]
+ and aux_layout =
+ function
+ Sub (p1, p2) -> aux p1 @ [NoBinding, symbol "\\SUB"] @ aux p2
+ | Sup (p1, p2) -> aux p1 @ [NoBinding, symbol "\\SUP"] @ aux p2
+ | Below (p1, p2) -> aux p1 @ [NoBinding, symbol "\\BELOW"] @ aux p2
+ | Above (p1, p2) -> aux p1 @ [NoBinding, symbol "\\ABOVE"] @ aux p2
+ | Frac (p1, p2) -> aux p1 @ [NoBinding, symbol "\\FRAC"] @ aux p2
+ | Atop (p1, p2) -> aux p1 @ [NoBinding, symbol "\\ATOP"] @ aux p2
+ | Over (p1, p2) -> aux p1 @ [NoBinding, symbol "\\OVER"] @ aux p2
+ | Root (p1, p2) ->
+ [NoBinding, symbol "\\ROOT"] @ aux p2 @ [NoBinding, symbol "\\OF"] @
+ aux p1
+ | Sqrt p -> [NoBinding, symbol "\\SQRT"] @ aux p
+(* | Break -> [] *)
+ | Box (_, pl) -> List.flatten (List.map aux pl)
+ and aux_magic magic =
+ match magic with
+ Opt p ->
+ let (p_bindings, p_atoms, p_names, p_action) = inner_pattern p in
+ let action (env_opt : env_type option) (loc : location) =
+ match env_opt with
+ Some env -> List.map opt_binding_some env
+ | None -> List.map opt_binding_of_name p_names
+ in
+ [Env (List.map opt_declaration p_names),
+ Gramext.srules
+ [[Gramext.Sopt (Gramext.srules [p_atoms, p_action])],
+ Gramext.action action]]
+ | List0 (p, _) | List1 (p, _) ->
+ let (p_bindings, p_atoms, p_names, p_action) = inner_pattern p in
+ let env0 = List.map list_binding_of_name p_names in
+ let grow_env_entry env n v =
+ List.map
+ (function
+ n', (ty, ListValue vl) as entry ->
+ if n' = n then n', (ty, ListValue (v :: vl)) else entry
+ | _ -> assert false)
+ env
+ in
+ let grow_env env_i env =
+ List.fold_left (fun env (n, (_, v)) -> grow_env_entry env n v) env
+ env_i
+ in
+ let action (env_list : env_type list) (loc : location) =
+ List.fold_right grow_env env_list env0
+ in
+ let g_symbol s =
+ match magic with
+ List0 (_, None) -> Gramext.Slist0 s
+ | List1 (_, None) -> Gramext.Slist1 s
+ | List0 (_, Some l) -> Gramext.Slist0sep (s, g_symbol_of_literal l)
+ | List1 (_, Some l) -> Gramext.Slist1sep (s, g_symbol_of_literal l)
+ | _ -> assert false
+ in
+ [Env (List.map list_declaration p_names),
+ Gramext.srules
+ [[g_symbol (Gramext.srules [p_atoms, p_action])],
+ Gramext.action action]]
+ | _ -> assert false
+ and aux_variable =
+ function
+ NumVar s -> [Binding (s, NumType), number ""]
+ | TermVar s -> [Binding (s, TermType), term]
+ | IdentVar s -> [Binding (s, StringType), ident ""]
+ | Ascription (p, s) -> assert false
+ | FreshVar _ -> assert false
+ and inner_pattern p =
+ let (p_bindings, p_atoms) = List.split (aux p) in
+ let p_names = flatten_opt p_bindings in
+ let action =
+ make_action
+ (fun (env : env_type) (loc : location) -> env)
+ p_bindings
+ in
+ p_bindings, p_atoms, p_names, action
+ in
+ aux pattern
+
+let level_of_int precedence =
+ if precedence < min_precedence || precedence > max_precedence then
+ raise (Level_not_found precedence);
+ string_of_int precedence
+
+type rule_id = Token.t Gramext.g_symbol list
+
+let extend level1_pattern ?(precedence = default_precedence) =
+ fun ?associativity action ->
+ let (p_bindings, p_atoms) =
+ List.split (extract_term_production level1_pattern)
+ in
+ let level = level_of_int precedence in
+ let p_names = flatten_opt p_bindings in
+ let _ =
+ Grammar.extend
+ [Grammar.Entry.obj (l2_pattern : 'a Grammar.Entry.e),
+ Some (Gramext.Level level),
+ [None, associativity,
+ [p_atoms,
+ make_action
+ (fun (env : env_type) (loc : location) -> action env loc)
+ p_bindings]]]
+ in
+ p_atoms
+
+let delete atoms = Grammar.delete_rule l2_pattern atoms
+
+(** {2 Grammar} *)
+
+let boxify =