package reparse
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Module Parser.InfixSource
Provides functions to support infix and let syntax operators.
Open the module to use it:
open Reparse.Parser.Infix p >>= f returns a new parser b where,
ais the parsed value ofpbisf a
Also known as bind operation.
Examples
module P = Reparse.Parser
open P.Infix
;;
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 = 104p >|= f returns a new parser encapsulating value b where,
ais the parsed value ofp.bisf a.
Also known as map operation.
Examples
module P = Reparse.Parser
open P.Infix
;;
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 = 104pf <*> q returns a new parser encapsulating value b where
pfandqare evaluated sequentially in order as given.fis the parsed value ofpfais the parsed value ofqbisf a
Also known as Applicative operation.
Examples
module P = Reparse.Parser
open P.Infix
;;
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 = 4v <$ p replaces the parse value of p with v.
Examples
module P = Reparse.Parser
open P.Infix
;;
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,
ais the parsed value ofpbisf a
This is the infix version of map.
Examples
module P = Reparse.Parser
open P.Infix
;;
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,qare evaluated sequentially in order as given.ais parsed value ofq.- The parsed value of
pis discarded.
Also known as discard left.
Examples
module P = Reparse.Parser
open P.Infix
;;
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,qare evaluated sequentially in order as given.ais parsed value ofp.- The parsed value of
qis discarded.
Also know as discard_right.
Examples
module P = Reparse.Parser
open P.Infix
;;
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,qare evaluated sequentially in order as given.ais the parsed value ofpifpis successfulais the parsed value ofqifpis a failure andqis a success.- If both -
pandq- fails, then the parser fails.
Examples
p fails and q succeeds, therefore we return q's parsed value 'w'
module P = Reparse.Parser
open P.Infix
;;
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 = truep <?> 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.Parser
open P.Infix
;;
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 = truelet* is a let syntax binding for (>>=)
Examples
module P = Reparse.Parser
open P.Infix
;;
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