+(** {2 Grammar extension} *)
+
+type associativity_kind = [ `Left | `Right | `None ]
+
+let symbol s = Gramext.Stoken ("SYMBOL", s)
+let ident s = Gramext.Stoken ("IDENT", s)
+let number s = Gramext.Stoken ("NUMBER", s)
+let term = Gramext.Sself
+
+type env_type = (string * (value_type * value)) list
+
+let make_action action =
+ let rec aux (vl : env_type) =
+ function
+ [] -> Gramext.action (fun (loc: location) -> action vl loc)
+ | None :: tl -> Gramext.action (fun _ -> aux vl tl)
+ | Some (name, typ) :: tl ->
+ (* i tipi servono? Magari servono solo quando si verifica la
+ * correttezza della notazione?
+ *)
+ Gramext.action (fun (v: value) -> aux ((name, (typ, v))::vl) tl)
+ in
+ aux []
+
+let flatten_opt =
+ let rec aux acc =
+ function
+ [] -> List.rev acc
+ | None::tl -> aux acc tl
+ | Some hd::tl -> aux (hd::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
+ | Literal l -> aux_literal l
+ | Layout l -> aux_layout l
+ | Magic m -> aux_magic m
+ | Variable v -> aux_variable v
+ | _ -> assert false
+ and aux_literal = function
+ | `Symbol s -> [None, symbol s]
+ | `Keyword s -> [None, ident s]
+ | `Number s -> [None, number s]
+ and aux_layout = function
+ | Sub (p1, p2) -> aux p1 @ [None, symbol "\\SUB"] @ aux p2
+ | Sup (p1, p2) -> aux p1 @ [None, symbol "\\SUP"] @ aux p2
+ | Below (p1, p2) -> aux p1 @ [None, symbol "\\BELOW"] @ aux p2
+ | Above (p1, p2) -> aux p1 @ [None, symbol "\\ABOVE"] @ aux p2
+ | Frac (p1, p2) -> aux p1 @ [None, symbol "\\FRAC"] @ aux p2
+ | Atop (p1, p2) -> aux p1 @ [None, symbol "\\ATOP"] @ aux p2
+ | Over (p1, p2) -> aux p1 @ [None, symbol "\\OVER"] @ aux p2
+ | Root (p1, p2) ->
+ [None, symbol "\\ROOT"] @ aux p2 @ [None, symbol "\\OF"] @ aux p1
+ | Sqrt p -> [None, symbol "\\SQRT"] @ aux p
+ | Break -> []
+ | Box (_, pl) -> List.flatten (List.map aux pl)
+ and aux_magic = function
+ | Opt p ->
+ let p_bindings, p_atoms = List.split (aux p) in
+ let p_names = flatten_opt p_bindings in
+ [ None,
+ Gramext.srules
+ [ [ Gramext.Sopt
+ (Gramext.srules
+ [ p_atoms,
+ (make_action
+ (fun (env : env_type) (loc : location) -> env)
+ p_bindings)])],
+ Gramext.action
+ (fun (env_opt : env_type option) (loc : location) ->
+ match env_opt with
+ Some env ->
+ List.map
+ (fun (name, (typ, v)) ->
+ (name, (OptType typ, OptValue (Some v))))
+ env
+ | None ->
+ List.map
+ (fun (name, typ) ->
+ (name, (OptType typ, OptValue None)))
+ p_names) ]]
+ | _ -> assert false
+ and aux_variable = function
+ | NumVar s -> [Some (s, NumType), number ""]
+ | TermVar s -> [Some (s, TermType), term]
+ | IdentVar s -> [Some (s, StringType), ident ""]
+ | Ascription (p, s) -> assert false (* TODO *)
+ | FreshVar _ -> assert false
+ in
+ aux pattern
+
+let level_of_int precedence =
+ (* TODO "mod" test to be removed as soon as we add all 100 levels *)
+ if precedence mod 10 <> 0 || precedence < 0 || precedence > 100 then
+ raise (Level_not_found precedence);
+ string_of_int precedence
+
+type rule_id = term Grammar.Entry.e * Token.t Gramext.g_symbol list
+
+let extend level1_pattern ?(precedence = 0) ?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 entry = Grammar.Entry.obj (level2_pattern: 'a Grammar.Entry.e) in
+ let _ =
+ Grammar.extend
+ [ entry, Some (Gramext.Level level),
+ [ Some level, (* TODO should we put None here? *)
+ associativity,
+ [ p_atoms,
+ (make_action
+ (fun (env: env_type)(loc: location) -> TermValue (action env loc))
+ p_bindings) ]]]
+ in
+ (level2_pattern, p_atoms)
+
+let delete (entry, atoms) = Grammar.delete_rule entry atoms
+
+let print_level2_pattern () =
+ Grammar.print_entry Format.std_formatter (Grammar.Entry.obj level2_pattern);
+ Format.pp_print_flush Format.std_formatter ()
+