package reparse-lwt

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type
val (>>=) : 'a t -> ('a -> 'b t) -> 'b t

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.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
val (>>|) : 'a t -> ('a -> 'b) -> 'b t

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.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
val (<*>) : 'a t -> ('a -> 'b) t -> 'b t

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
val (<$>) : 'a t -> ('a -> 'b) -> 'b t

f <$> p is return f <*> p.

val (<$$>) : ('a t * 'b t) -> ('a -> 'b -> 'c) -> 'c t
val (<$$$>) : ('a t * 'b t * 'c t) -> ('a -> 'b -> 'c -> 'd) -> 'd t
val (<$$$$>) : ('a t * 'b t * 'c t * 'd t) -> ('a -> 'b -> 'c -> 'd -> 'e) -> 'e t
val (<$) : 'a -> 'b t -> 'a t

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"
val ($>) : 'a t -> 'b -> 'b t

p $> v is inverse of v <$ p.

val (*>) : _ t -> 'b t -> 'b t

p *> q returns a parser encapsulating value a where,

  • p, q are evaluated sequentially in order as given.
  • a is parsed value of q.
  • The parsed value of p is discarded. Also known as discard 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"
val (<*) : 'a t -> _ t -> 'a t

p <* q returns a parser encapsulating value a where,

  • p, q are evaluated sequentially in order as given.
  • a is parsed value of p.
  • 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"
val (<|>) : 'a t -> 'a t -> 'a t

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 successful
  • a is the parsed value of q if p is a failure and q is a success.
  • If both - 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.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
val let* : 'a t -> ('a -> 'b t) -> 'b t

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
val and* : 'a t -> 'b t -> ('a * 'b) t
val let+ : 'a t -> ('a -> 'b) -> 'b t

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
val and+ : 'a t -> 'b t -> ('a * 'b) t
val (<?>) : 'a t -> string -> 'a t

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
OCaml

Innovation. Community. Security.