<commit_before>(* Distributed under the MIT License.
   (See accompanying file LICENSE.txt)
   (C) Copyright <NAME>
*)

open Rewriting_ast
open Rewriting_system_error
open Utils


type 'a sym_tbl = (string, info * 'a) Hashtbl.t

let kind_table : kind sym_tbl = Hashtbl.create 5
let constant_table : constant sym_tbl = Hashtbl.create 5
let operator_table : operator sym_tbl = Hashtbl.create 5
let rule_table : rule sym_tbl = Hashtbl.create 5


let raise_unknown_symbol pos kind id =
  raise (RewritingSystemError (
    RewritingUnboundSymbol,
    kind ^ " " ^ id ^ (pos_to_string pos)))

let warn_on_redeclaration symbol_category id pos =
  Format.printf "Warning : %s %s at %s is already defined.\n"
    symbol_category id (pos_to_string pos)

let raise_on_redeclaration symbol_category id pos =
  raise (RewritingSystemError (RedeclaredSymbol, 
    Printf.sprintf "[%s] %s %s" (pos_to_string pos) symbol_category id))

let add_symbol_impl (name, info, desc) redeclaration_policy  =
  let aux tbl symbol_category value =
    if Hashtbl.mem tbl name then
      redeclaration_policy symbol_category name info
    else
      Hashtbl.add tbl name value
  in
  match desc with
  | DKind k -> aux kind_table "Kind" (info, k)
  | DConstant c -> aux constant_table "Constant" (info, c)
  | DOperator op -> aux operator_table "Operator" (info, op)
  | DRule r -> aux rule_table "Rule" (info, r)

let set_up_environment_impl add_decl = function
  | RewritingAST decls -> List.iter add_decl decls

let set_up_environment ast =
  set_up_environment_impl add_symbol ast

let set_up_environment_strict ast =
  set_up_environment_impl add_symbol_strict ast

let clear_symbols () =
  Hashtbl.reset kind_table;
  Hashtbl.reset constant_table;
  Hashtbl.reset operator_table;
  Hashtbl.reset rule_table

let add_symbol symbol = add_symbol_impl symbol warn_on_redeclaration
let add_symbol_strict symbol = add_symbol_impl symbol raise_on_redeclaration

let lookup tbl sym_kind ?(pos=Lexing.dummy_pos) id =
  try
    Hashtbl.find tbl id
  with<commit_msg>Fix compilation error, move add_symbol up.<commit_after>(* Distributed under the MIT License.
   (See accompanying file LICENSE.txt)
   (C) Copyright <NAME>
*)

open Rewriting_ast
open Rewriting_system_error
open Utils


type 'a sym_tbl = (string, info * 'a) Hashtbl.t

let kind_table : kind sym_tbl = Hashtbl.create 5
let constant_table : constant sym_tbl = Hashtbl.create 5
let operator_table : operator sym_tbl = Hashtbl.create 5
let rule_table : rule sym_tbl = Hashtbl.create 5


let raise_unknown_symbol pos kind id =
  raise (RewritingSystemError (
    RewritingUnboundSymbol,
    kind ^ " " ^ id ^ (pos_to_string pos)))

let warn_on_redeclaration symbol_category id pos =
  Format.printf "Warning : %s %s at %s is already defined.\n"
    symbol_category id (pos_to_string pos)

let raise_on_redeclaration symbol_category id pos =
  raise (RewritingSystemError (RedeclaredSymbol, 
    Printf.sprintf "[%s] %s %s" (pos_to_string pos) symbol_category id))

let add_symbol_impl (name, info, desc) redeclaration_policy  =
  let aux tbl symbol_category value =
    if Hashtbl.mem tbl name then
      redeclaration_policy symbol_category name info
    else
      Hashtbl.add tbl name value
  in
  match desc with
  | DKind k -> aux kind_table "Kind" (info, k)
  | DConstant c -> aux constant_table "Constant" (info, c)
  | DOperator op -> aux operator_table "Operator" (info, op)
  | DRule r -> aux rule_table "Rule" (info, r)

let add_symbol symbol = add_symbol_impl symbol warn_on_redeclaration
let add_symbol_strict symbol = add_symbol_impl symbol raise_on_redeclaration

let set_up_environment_impl add_decl = function
  | RewritingAST decls -> List.iter add_decl decls

let set_up_environment ast =
  set_up_environment_impl add_symbol ast

let set_up_environment_strict ast =
  set_up_environment_impl add_symbol_strict ast

let clear_symbols () =
  Hashtbl.reset kind_table;
  Hashtbl.reset constant_table;
  Hashtbl.reset operator_table;
  Hashtbl.reset rule_table

let lookup tbl sym_kind ?(pos=Lexing.dummy_pos) id =
  try
    Hashtbl.find tbl id
  with
{'fraction_non_alphanumeric': 0.06980717598242617, 'fraction_numerical': 0.001952648279228704, 'mean_word_length': 4.442231075697211, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7534794', 'n_tokens_mistral': 1395, 'n_tokens_neox': 1356, 'n_words': 418}