Skip to content

Instantly share code, notes, and snippets.

@gsg
Last active August 29, 2015 14:04
Show Gist options
  • Save gsg/28ca0dcb42bfdf7a8b13 to your computer and use it in GitHub Desktop.
Save gsg/28ca0dcb42bfdf7a8b13 to your computer and use it in GitHub Desktop.
let map_collect_by_name tbl list =
let rec loop = function
| [] -> ()
| D _::elts -> loop elts
| E (((_, name), attrs) as tag, children)::elts ->
try (Hashtbl.find tbl name) attrs children
with Not_found ->
failwith (Printf.sprintf "bad tag: %s" (fmt_tag tag)) in
loop list
let define_op tbl (name:string) f =
let cell = ref [] in
Hashtbl.add tbl name
(fun attrs children ->
cell := f attrs children::!cell);
cell
let parse_domain_children list =
let ops = Hashtbl.create 2 in
let rules = define_op ops "rule" parse_rule in
let assertions = define_op ops "assert" parse_assertion in
map_collect_by_name ops list;
!rules, !assertions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment