Source

hasexp / combinator.mli

open Sexplib

type filter = Sexp.t -> Sexp.t Zlist.t
(** Type of filter, which takes an Sexp tree, 
    and returns the stream of subtrees which match with the filter criteria 
*)
    
(** No recursive basic operators, which check only the top of Sexp *)

val none : filter
(** returns none *)

val keep : filter
(** returns the entire tree *)

val atom : filter
(** returns the tree if it is an Atom *)

val list : filter
(** return the tree if it is a List *)

(** More complicated combinators, which look List childrens *) 

val children : filter
(** returns the children, if the tree is a List. If an Atom, returns none *)

(** Combinators *)

val (^.) : filter -> filter -> filter
(** Composition. 
    [a ^. b] filters an sexp by [b], then the result is filtered by [a].
    The result is concatenated.
*)

val (|||) : filter -> filter -> filter
(** [a ||| b] runs [a] and [b] on the same Sexp and returns the appended results *) 

val with_ : filter -> filter -> filter
(** [with_ f g] returns the result of [f] on an Sexp, filtered by [g].
    If some of the result are filtered out by [g], i.e. [g res] is null, 
    they are removed from the result of [with_ f g] on the Sexp.
 *) 

val without : filter -> filter -> filter
(** Opposite of [with_] *)

val ( /> ) : filter -> filter -> filter
(** [f /> g] is equal to [g ^. children ^. f]. 
    Run [f] then run [g] against the children of the result *)
 
val ( </ ) : filter -> filter -> filter
(** [f </ g] is equal to [with_ f (g ^. children)]. 
    Run [f] and returns result whose children are not filtered out by [g] *)

val if_ : filter -> then_:filter -> else_:filter -> filter
(** Conditional [if_ cond ~then_ ~else]: if [cond] returns non-null, 
    return the result of the run of [then_].
    Otherwise return the result of the run of [else_]
*)

val ( |>| ) : filter -> filter -> filter
(** Conditional like [a ? b : c] in C. [f |>| g] runs [f] and if the result is non-null returns them.
    Otherwise it runs [g] and returns [g]'s result. *)

val deep : filter -> filter
(** Depth search. At [deep f], if some Sexp matches with [f], its children are not searched. *)

val deepest : filter -> filter
(** Depth search. If some Sexp matches with [f], its parents are not searched. *)

(** XML specific combinators *)
module Xml : sig
  val pcdata : filter
  val tag : filter
  val tag_named : string -> filter
  val tag_p : (string -> bool) -> filter 

  val contents : filter
  val attrs : filter
  val filter_attrs : (string -> string -> bool) -> filter
  val filter_map_attrs : (string -> string -> Sexp.t option) -> filter
  val assoc_attrs : string -> filter

  val assoc_attrs_conv : string -> (string -> 'a) -> Sexp.t -> 'a

  module Make : sig
    val pcdata : string -> Sexp.t
    val tag : string -> attrs:Sexp.t list -> Sexp.t list -> Sexp.t
  end
end

(** Combinators for OCaml value Sexp representaions (?) *)
 module Caml : sig
  val string : string -> Sexp.t
  val record_elt : string -> Sexp.t -> Sexp.t
end