OCaml implementation of the Mozilla Public Suffix service
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

chore: apply dune fmt and linter refactoring

Auto-formatting and structural improvements from dune fmt across
precommit, publicsuffix, punycode, and requests packages.

+27 -39
+27 -39
gen/gen_psl.ml
··· 185 185 s; 186 186 Buffer.contents b 187 187 188 - (** Generate OCaml code for the trie *) 189 - let generate_code trie rule_count icann_count private_count version commit = 190 - (* Print header *) 188 + let print_generated_header rule_count icann_count private_count version commit = 191 189 print_string 192 190 {|(* Auto-generated from public_suffix_list.dat - DO NOT EDIT *) 193 191 (* This file contains the parsed Public Suffix List as OCaml data structures *) ··· 210 208 rule_count icann_count private_count; 211 209 Printf.printf "(* Version: %s *)\n" version; 212 210 Printf.printf "(* Commit: %s *)\n" commit; 213 - print_string "\n"; 211 + print_string "\n" 212 + 213 + let print_generated_footer rule_count icann_count private_count version commit 214 + root_name = 215 + Printf.printf "let root = %s\n" root_name; 216 + print_string 217 + {| 218 + (** Get the root of the suffix trie *) 219 + let root () = root 220 + 221 + (** Total number of rules in the list *) 222 + |}; 223 + Printf.printf "let rule_count = %d\n\n" rule_count; 224 + Printf.printf "let icann_rule_count = %d\n\n" icann_count; 225 + Printf.printf "let private_rule_count = %d\n\n" private_count; 226 + Printf.printf "let version = %S\n\n" version; 227 + Printf.printf "let commit = %S\n" commit 214 228 215 - (* Generate the trie as nested let bindings using a depth-first traversal *) 229 + let generate_trie_nodes trie = 216 230 let node_counter = ref 0 in 217 231 let node_names = Hashtbl.create 1000 in 218 - 219 - (* First pass: assign names to all nodes *) 220 232 let rec assign_names node = 221 233 let name = Printf.sprintf "n%d" !node_counter in 222 234 incr node_counter; ··· 225 237 Option.iter assign_names node.wildcard_child 226 238 in 227 239 assign_names trie; 228 - 229 - (* Generate nodes in reverse order (leaves first) *) 230 240 let generated = Hashtbl.create 1000 in 231 241 let output_buffer = Buffer.create (1024 * 1024) in 232 - 233 242 let rec generate_node node = 234 243 let node_id = node.id in 235 244 if Hashtbl.mem generated node_id then Hashtbl.find node_names node_id 236 245 else begin 237 - (* First generate all children *) 238 246 List.iter (fun (_, child) -> ignore (generate_node child)) node.children; 239 247 Option.iter 240 248 (fun child -> ignore (generate_node child)) 241 249 node.wildcard_child; 242 - 243 250 let name = Hashtbl.find node_names node_id in 244 - 245 - (* Generate the node definition *) 246 251 Buffer.add_string output_buffer (Printf.sprintf "let %s = {\n" name); 247 - 248 - (* Rule field *) 249 252 (match node.rule with 250 253 | None -> Buffer.add_string output_buffer " rule = None;\n" 251 254 | Some (rt, sec) -> ··· 260 263 in 261 264 Buffer.add_string output_buffer 262 265 (Printf.sprintf " rule = Some (%s, %s);\n" rt_str sec_str)); 263 - 264 - (* Children field *) 265 266 if node.children = [] then 266 267 Buffer.add_string output_buffer " children = [];\n" 267 268 else begin ··· 275 276 node.children; 276 277 Buffer.add_string output_buffer " ];\n" 277 278 end; 278 - 279 - (* Wildcard child field *) 280 279 (match node.wildcard_child with 281 280 | None -> Buffer.add_string output_buffer " wildcard_child = None;\n" 282 281 | Some child -> 283 282 let child_name = Hashtbl.find node_names child.id in 284 283 Buffer.add_string output_buffer 285 284 (Printf.sprintf " wildcard_child = Some %s;\n" child_name)); 286 - 287 285 Buffer.add_string output_buffer "}\n\n"; 288 - 289 286 Hashtbl.add generated node_id true; 290 287 name 291 288 end 292 289 in 293 - 294 290 let root_name = generate_node trie in 295 - print_string (Buffer.contents output_buffer); 296 - Printf.printf "let root = %s\n" root_name; 291 + (Buffer.contents output_buffer, root_name) 297 292 298 - (* Generate helper to get the root *) 299 - print_string 300 - {| 301 - (** Get the root of the suffix trie *) 302 - let root () = root 303 - 304 - (** Total number of rules in the list *) 305 - |}; 306 - Printf.printf "let rule_count = %d\n\n" rule_count; 307 - Printf.printf "let icann_rule_count = %d\n\n" icann_count; 308 - Printf.printf "let private_rule_count = %d\n\n" private_count; 309 - (* Generate version and commit values *) 310 - Printf.printf "let version = %S\n\n" version; 311 - Printf.printf "let commit = %S\n" commit 293 + (** Generate OCaml code for the trie *) 294 + let generate_code trie rule_count icann_count private_count version commit = 295 + print_generated_header rule_count icann_count private_count version commit; 296 + let node_output, root_name = generate_trie_nodes trie in 297 + print_string node_output; 298 + print_generated_footer rule_count icann_count private_count version commit 299 + root_name 312 300 313 301 let () = 314 302 if Array.length Sys.argv < 2 then begin