Module Sexplib.Path
Types
type el
=
|
Pos of int
Pos n
denotesn
th element in a tuple|
Match of string * int
Match (tag, n)
denotesn
th argument of sum matchingtag
|
Rec of string
Rec name
denotes the record field havingname
Type of substitution elements
type t
= el list
Type of substitution paths
High-level functions
val parse : string -> t
parse str
- returns
a substitution path represented by string
str
.Syntax:
"." -> separates path elements; must be present at start of string.
"[4]" -> specifies the 4th element in a tuple.
"some_tag[4]" -> tries to match
some_tag
, then denotes its 4th argument."name" -> denotes record field having
name
Example from test code:
".t.x.B
1
" -> choose record field with namet
, then subfieldx
. Match this value againstB
, and denote its first argument.
- raises Failure
if the path is syntactically invalid.
val get : ?path:t -> ?str:string -> Sexp.t -> Sexp.t
get ?path ?str sexp
ifpath
is provided, use it as path. Otherwise, ifstr
is provided, parse it as a path. If neither is provided, assume an empty path.- returns
the sub-expression from S-expression
sexp
denoted by the path.
- raises Failure
if path is syntactically invalid or if the path structure clashes with the structure of the data.
val replace : ?path:t -> ?str:string -> Sexp.t -> subst:Sexp.t -> Sexp.t
replace ?path ?str sexp ~subst
likeget
, but does not extract a sub-expression but substitutes it withsubst
.- returns
resulting S-expression.
- raises Failure
if path is syntactically invalid or if the path structure clashes with the structure of the data.
val replace_no_path : str:string -> Sexp.t -> subst:Sexp.t -> Sexp.t
replace_no_path ~str sexp ~subst
likereplace
, but does not take optional arguments.str
must be specified.- raises Failure
if path is syntactically invalid or if the path structure clashes with the structure of the data.
val subst_path : Sexp.t -> t -> (Sexp.t -> Sexp.t) * Sexp.t
subst_path sexp path
- returns
the tuple
(subst, sub)
, wheresubst
is a function that returns an S-expression in which the subexpression denoted bypath
insexp
has been substituted by its argument.sub
is the denoted subexpression. Note thatsubst sub = sexp
.
- raises Failure
if path is syntactically invalid or if the path structure clashes with the structure of the data.
Low-level functions
val extract_pos : int -> Sexp.t -> (Sexp.t option -> Sexp.t) * Sexp.t
extract_pos n sexp
- returns
the tuple
(subst, sub)
, wheresubst
is a function that returns an S-expression in which the subexpression denoted at positionn
insexp
, which must be a list, has been substituted byvalue
if the optional argument isSome value
, or removes the denoted subexpression if the optional argument isNone
.sub
is the denoted subexpression. Note thatsubst (Some sub) = sexp
.
- raises Failure
if the position cannot be resolved within the given S-expression.
val extract_match : string -> int -> Sexp.t -> (Sexp.t option -> Sexp.t) * Sexp.t
extract_match tag n sexp
- returns
the tuple
(subst, sub)
, wheresubst
is a function that returns an S-expression in which the subexpression denoted by matchingtag
and taking itsn
th argument insexp
has been substituted byvalue
if the argument isSome value
, or removes the denoted subexpression if the optional argument isNone
.sub
is the denoted subexpression. Note thatsubst (Some sub) = sexp
.
- raises Failure
if the S-expression does not represent a sum tag.
val extract_rec : string -> Sexp.t -> (Sexp.t -> Sexp.t) * Sexp.t
extract_rec name sexp
- returns
the tuple
(subst, sub)
, wheresubst
is a function that returns an S-expression in which the subexpression denoted by matching field namename
insexp
has been substituted by its argument.sub
is the denoted subexpression. Note thatsubst (Some sub) = sexp
.
- raises Failure
if the S-expression does not represent a record.