Legend:
Library
Module
Module type
Parameter
Class
Class type
Library
Module
Module type
Parameter
Class
Class type
Provides functions to support infix and let syntax operators.
Open the module to use it:
open Reparse.Infix
p >>= f
returns a new parser b where,
a
is the parsed value of p
b
is f a
Also known as bind
operation.
Examples
module P = Reparse
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 of p
.b
is f a
.Also known as map
operation.
Examples
module P = Reparse
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
p >|= f
returns a new parser encapsulating value b
where,
a
is the parsed value of p
.b
is f a
.Also known as map
operation.
Examples
module P = Reparse
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
and q
are evaluated sequentially in order as given.f
is the parsed value of pf
a
is the parsed value of q
b
is f a
Also known as Applicative
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
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"
f <$> p
returns a parser encapsulating value b
where,
a
is the parsed value of p
b
is f a
This is the infix version of Reparse.Infix.map
.
Examples
module P = Reparse
open P
;;
let f a = a ^ " world" in
let p = P.string "hello" in
let p = f <$> p in
let v = P.parse_string p "hello" in
v = "hello world"
p *> q
returns a parser encapsulating value a
where,
p
, q
are evaluated sequentially in order as given.a
is parsed value of q
.p
is discarded.Also known as discard left
.
Examples
module P = Reparse
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 of p
.q
is discarded.Also know as discard_right.
Examples
module P = Reparse
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 of p
if p
is successfula
is the parsed value of q
if p
is a failure and q
is a success.p
and q
- fails, then the parser fails.Examples
p
fails and q
succeeds, therefore we return q
's parsed value 'w'
module P = Reparse
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
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
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
let*
is a let syntax binding for Reparse.Infix.((>>=))
Examples
module P = Reparse
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
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