To focus the search input from anywhere on the page, press the 'S' key.
in-package search v0.1.0
Library
Module
Module type
Parameter
Class
Class type
p >>= f
returns a new parser b where,
a
is the parsed value ofp
b
isf a
Also known asbind
operation.
Examples
module P = Reparse.String
open P;;
let f a = P.pure (Char.code a) in
let p = P.char 'h' in
let p = p >>= f in
let v = P.parse_string p "hello" in
v = 104
p >>| f
returns a new parser encapsulating value b
where,
a
is the parsed value ofp
.b
isf a
. Also known asmap
operation.
Examples
module P = Reparse.String
open P;;
let f a = Char.code a in
let p = P.char 'h' in
let p = p >>| f in
let v = P.parse_string p "hello" in
v = 104
pf <*> q
returns a new parser encapsulating value b
where
pf
andq
are evaluated sequentially in order as given.f
is the parsed value ofpf
a
is the parsed value ofq
b
isf a
Also known asApplicative
operation.
Examples
module P = Reparse
open P;;
let f a = a + 2 in
let pf = P.pure f in
let q = P.pure 2 in
let p = pf <*> q in
let v = P.parse_string p "hello" in
v = 4
v <$ p
replaces the parse value of p
with v
.
Examples
module P = Reparse.String
open P;;
let v = "hello" in
let p = P.char 'h' in
let p = v <$ p in
let v2 = P.parse_string p "hello" in
v2 = "hello"
p *> q
returns a parser encapsulating value a
where,
p
,q
are evaluated sequentially in order as given.a
is parsed value ofq
.- The parsed value of
p
is discarded. Also known asdiscard left
.
Examples
module P = Reparse.String
open P;;
let p = P.string "world" in
let q = P.pure "hello" in
let p = p *> q in
let v = P.parse_string p "world" in
v = "hello"
p <* q
returns a parser encapsulating value a
where,
p
,q
are evaluated sequentially in order as given.a
is parsed value ofp
.- The parsed value of
q
is discarded. Also know as discard_right.
Examples
module P = Reparse.String
open P;;
let p = P.string "world" in
let q = P.pure "hello" in
let p = p <* q in
let v = P.parse_string p "world" in
v = "world"
p <|> q
returns a parser encapsulating value a
where,
p
,q
are evaluated sequentially in order as given.a
is the parsed value ofp
ifp
is successfula
is the parsed value ofq
ifp
is a failure andq
is a success.- If both -
p
andq
- fails, then the parser fails.
Examples
p
fails and q
succeeds, therefore we return q
's parsed value 'w'
module P = Reparse.String
open P;;
let p = P.char 'h' in
let q = P.char 'w' in
let p = p <|> q in
let v = P.parse_string p "world" in
v = 'w'
p
succeeds therefore we return its parsed value 'h'
let p = P.char 'h' in
let q = P.char 'w' in
let p = p <|> q in
let v = P.parse_string p "hello" in
v = 'h'
The parser fails if both p
and q
fails.
let p = P.char 'h' in
let q = P.char 'w' in
let p = p <|> q in
let v =
try
let _ = P.parse_string p "" in
false
with _ -> true
in
v = true
let*
is a let syntax binding for Reparse
.Infix.((>>=))
Examples
module P = Reparse.String
open P;;
let p =
let* a = P.pure 5 in
let total = a + 5 in
P.pure total
in
let v = P.parse_string p "" in
v = 10
let*
is a let syntax binding for Reparse
.((>|=))
Examples
module P = Reparse.String
open P;;
let p =
let+ a = P.pure 5 in
let total = a + 5 in
total
in
let v = P.parse_string p "" in
v = 10
p <?> err_msg
parses p
to value a
and returns a new parser encapsulating a
. If p
is a failure, then it fails with error message err_msg
. Often used as a last choice in <|>
, e.g. a <|> b <|> c <?> "expected a b c"
.
Examples
module P = Reparse.String
open P;;
let p = P.char 'h' <|> P.char 'w' in
let err_msg = "[error]" in
let p = p <?> err_msg in
let v =
try
let _ = P.parse_string p "" in
false
with
| P.Parser
{offset= 0; line_number= 0; column_number= 0; msg= "[error]"} ->
true
| _ -> false
in
v = true