OCaml package documentation
13 |-
14 |
- reparse 3.0.0 15 |
- reparse-lwt 3.0.0 16 |
├── docs ├── reparse │ ├── Reparse │ │ ├── .dune-keep │ │ ├── Make │ │ │ ├── Let_syntax │ │ │ │ ├── index.html │ │ │ │ └── Let_syntax │ │ │ │ │ └── index.html │ │ │ ├── argument-1-Input │ │ │ │ └── index.html │ │ │ └── Infix │ │ │ │ └── index.html │ │ ├── String │ │ │ ├── Let_syntax │ │ │ │ ├── index.html │ │ │ │ └── Let_syntax │ │ │ │ │ └── index.html │ │ │ └── Infix │ │ │ │ └── index.html │ │ ├── module-type-PARSER │ │ │ ├── Let_syntax │ │ │ │ ├── index.html │ │ │ │ └── Let_syntax │ │ │ │ │ └── index.html │ │ │ └── Infix │ │ │ │ └── index.html │ │ ├── index.html │ │ └── module-type-INPUT │ │ │ └── index.html │ └── index.html ├── reparse-lwt │ ├── Reparse_lwt │ │ ├── .dune-keep │ │ ├── index.html │ │ └── Stream │ │ │ ├── Let_syntax │ │ │ ├── index.html │ │ │ └── Let_syntax │ │ │ │ └── index.html │ │ │ └── Infix │ │ │ └── index.html │ └── index.html ├── index.html ├── highlight.pack.js └── odoc.css ├── dune ├── examples ├── dune ├── calc.ml └── json.ml ├── lib └── dune ├── Makefile ├── dune-project ├── lwt ├── dune ├── reparse_lwt.mli └── reparse_lwt.ml ├── lwt-unix ├── dune ├── reparse_lwt_unix.mli └── reparse_lwt_unix.ml ├── tests ├── dune ├── run.ml ├── test_input_parsers.ml ├── test_boolean_parsers.ml ├── test_parser.ml ├── test_alternative_parsers.ml └── test_string_parsers.ml ├── .ocamlformat ├── .gitignore ├── reparse-lwt-unix.opam ├── reparse-lwt.opam ├── reparse.opam ├── CHANGES.md ├── README.md └── LICENSE /docs/reparse/Reparse/.dune-keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/reparse-lwt/Reparse_lwt/.dune-keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /dune: -------------------------------------------------------------------------------- 1 | (mdx 2 | (files README.md) 3 | (packages reparse)) 4 | -------------------------------------------------------------------------------- /examples/dune: -------------------------------------------------------------------------------- 1 | (executables 2 | (names calc json) 3 | (libraries reparse)) 4 | -------------------------------------------------------------------------------- /lib/dune: -------------------------------------------------------------------------------- 1 | (library 2 | (name reparse) 3 | (public_name reparse) 4 | (libraries cstruct)) 5 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | dune b @doc 3 | cp -R _build/default/_doc/_html/* docs/ 4 | 5 | .PHONY: all 6 | 7 | -------------------------------------------------------------------------------- /dune-project: -------------------------------------------------------------------------------- 1 | (lang dune 2.9) 2 | 3 | (name reparse) 4 | 5 | (version 3.1.0) 6 | 7 | (using mdx 0.1) 8 | -------------------------------------------------------------------------------- /lwt/dune: -------------------------------------------------------------------------------- 1 | (library 2 | (name reparse_lwt) 3 | (public_name reparse-lwt) 4 | (libraries reparse lwt cstruct)) 5 | -------------------------------------------------------------------------------- /lwt-unix/dune: -------------------------------------------------------------------------------- 1 | (library 2 | (name reparse_lwt_unix) 3 | (public_name reparse-lwt-unix) 4 | (libraries reparse cstruct lwt lwt.unix)) 5 | -------------------------------------------------------------------------------- /tests/dune: -------------------------------------------------------------------------------- 1 | (test 2 | (name run) 3 | (libraries cstruct lwt lwt.unix popper reparse reparse-lwt reparse-lwt-unix) 4 | (preprocess 5 | (pps ppx_deriving.show ppx_deriving.ord ppx_deriving_popper))) 6 | -------------------------------------------------------------------------------- /.ocamlformat: -------------------------------------------------------------------------------- 1 | profile = ocamlformat 2 | module-item-spacing=compact 3 | sequence-blank-line=preserve-one 4 | single-case=compact 5 | break-cases = fit 6 | parse-docstrings = true 7 | wrap-comments = true 8 | 9 | -------------------------------------------------------------------------------- /tests/run.ml: -------------------------------------------------------------------------------- 1 | let () = Printexc.record_backtrace true 2 | 3 | let () = 4 | Popper.suite 5 | [ ("String", Test_string_parsers.suite) 6 | ; ("Alternative", Test_alternative_parsers.suite) 7 | ; ("Boolean", Test_boolean_parsers.suite) 8 | ; ("Input", Test_input_parsers.suite) ] 9 | |> Popper.run 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.log 2 | *.annot 3 | *.cmo 4 | *.cma 5 | *.cmi 6 | *.a 7 | *.o 8 | *.cmx 9 | *.cmxs 10 | *.cmxa 11 | 12 | # ocamlbuild working directory 13 | _build/ 14 | 15 | # ocamlbuild targets 16 | *.byte 17 | *.native 18 | 19 | # oasis generated files 20 | setup.data 21 | setup.log 22 | 23 | # Merlin configuring file for Vim and Emacs 24 | .merlin 25 | 26 | *.install 27 | node_modules/ 28 | _build 29 | _esy 30 | _release 31 | *.byte 32 | *.native 33 | *.installlmerlin 34 | node_modules/ 35 | _build 36 | _esy 37 | _release 38 | *.byte 39 | *.native 40 | *.install 41 | .ionide 42 | *.swp 43 | .vscode 44 | .nvimlog 45 | -------------------------------------------------------------------------------- /lwt/reparse_lwt.mli: -------------------------------------------------------------------------------- 1 | (*------------------------------------------------------------------------- 2 | * Copyright (c) 2020, 2021 Bikal Gurung. All rights reserved. 3 | * 4 | * This Source Code Form is subject to the terms of the Mozilla Public 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this 6 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. 7 | * 8 | * %%NAME%% %%VERSION%% 9 | *-------------------------------------------------------------------------*) 10 | 11 | module Stream : sig 12 | include Reparse.PARSER with type 'a promise = 'a Lwt.t 13 | 14 | val create_input : char Lwt_stream.t -> input 15 | end 16 | -------------------------------------------------------------------------------- /docs/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 |The entry point of this library is the module: Reparse.
The entry point of this library is the module: Reparse_lwt.
Reparse_lwtmodule Stream : sig ... endMake.Let_syntaxppx_let syntax support module.
val return : 'a -> 'a tmodule Let_syntax : sig ... endString.Let_syntaxppx_let syntax support module.
val return : 'a -> 'a tmodule Let_syntax : sig ... endPARSER.Let_syntaxppx_let syntax support module.
val return : 'a -> 'a tmodule Let_syntax : sig ... endStream.Let_syntaxppx_let syntax support module.
val return : 'a -> 'a tmodule Let_syntax : sig ... endReparseParser provides functions and types to construct robust, performant and reusable parsers. At the core is a type Reparse.PARSER.t which represents a constructed parser definition. A parser Reparse.PARSER.t is defined by composing together one or more parsers or Reparse.ts via usage of parser operators.
An instance of Reparse.PARSER.t represents an un-evaluated parser. Use Reparse.PARSER.parse function to evaluate it.
Reparse.INPUT represents a generalization of data input to Reparse.parse. Implement the interface to create new input types.
Parser functions are broadly organized into following categories:
module type PARSER = sig ... endmodule type INPUT = sig ... endmodule Make (Input : INPUT) : PARSER with type 'a promise = 'a Input.promise with type input = Input.tA functor to create parsers based on the given Input module.
module String : sig ... endA parser when the input is a string.
Reparse.INPUTval return : 'a -> 'a promisecommit t ~pos ensures that the parser will not backtrack on input t beyond pos.
get t ~pos ~len returns `String s where String.length s <= len or `Eof if EOI is reached.
get_unbuffered t ~pos ~len similar to get t ~pos ~len except it doesn't buffer the taken len bytes.
Make.1-Inputval return : 'a -> 'a promisecommit t ~pos ensures that the parser will not backtrack on input t beyond pos.
get t ~pos ~len returns `String s where String.length s <= len or `Eof if EOI is reached.
get_unbuffered t ~pos ~len similar to get t ~pos ~len except it doesn't buffer the taken len bytes.
Let_syntax.Let_syntaxval return : 'a -> 'a tLet_syntax.Let_syntaxval return : 'a -> 'a tLet_syntax.Let_syntaxval return : 'a -> 'a tLet_syntax.Let_syntaxval return : 'a -> 'a tMake.Infixp >>= f returns a new parser b where,
a is the parsed value of pb is f a Also known as bind operation.Examples
module P = Reparse.String
3 | open P
4 |
5 | ;;
6 | let f a = P.pure (Char.code a) in
7 | let p = P.char 'h' in
8 | let p = p >>= f in
9 | let v = P.parse_string p "hello" in
10 | v = 104p >>| 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
11 | open P
12 |
13 | ;;
14 | let f a = Char.code a in
15 | let p = P.char 'h' in
16 | let p = p >>| f in
17 | let v = P.parse_string p "hello" in
18 | v = 104pf <*> 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 pfa is the parsed value of qb is f a Also known as Applicative operation.Examples
module P = Reparse
19 | open P
20 |
21 | ;;
22 | let f a = a + 2 in
23 | let pf = P.pure f in
24 | let q = P.pure 2 in
25 | let p = pf <*> q in
26 | let v = P.parse_string p "hello" in
27 | v = 4v <$ p replaces the parse value of p with v.
Examples
module P = Reparse.String
28 | open P
29 |
30 | ;;
31 | let v = "hello" in
32 | let p = P.char 'h' in
33 | let p = v <$ p in
34 | let v2 = P.parse_string p "hello" in
35 | v2 = "hello"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.String
36 | open P
37 |
38 | ;;
39 | let p = P.string "world" in
40 | let q = P.pure "hello" in
41 | let p = p *> q in
42 | let v = P.parse_string p "world" in
43 | 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.String
44 | open P
45 |
46 | ;;
47 | let p = P.string "world" in
48 | let q = P.pure "hello" in
49 | let p = p <* q in
50 | let v = P.parse_string p "world" in
51 | 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.String
52 | open P
53 |
54 | ;;
55 | let p = P.char 'h' in
56 | let q = P.char 'w' in
57 | let p = p <|> q in
58 | let v = P.parse_string p "world" in
59 | v = 'w'p succeeds therefore we return its parsed value 'h'
let p = P.char 'h' in
60 | let q = P.char 'w' in
61 | let p = p <|> q in
62 | let v = P.parse_string p "hello" in
63 | v = 'h'The parser fails if both p and q fails.
let p = P.char 'h' in
64 | let q = P.char 'w' in
65 | let p = p <|> q in
66 | let v =
67 | try
68 | let _ = P.parse_string p "" in
69 | false
70 | with
71 | | _ -> true
72 | in
73 | v = truelet* is a let syntax binding for Reparse.Infix.((>>=))
Examples
module P = Reparse.String
74 | open P
75 |
76 | ;;
77 | let p =
78 | let* a = P.pure 5 in
79 | let total = a + 5 in
80 | P.pure total
81 | in
82 | let v = P.parse_string p "" in
83 | v = 10let* is a let syntax binding for Reparse.((>|=))
Examples
module P = Reparse.String
84 | open P
85 |
86 | ;;
87 | let p =
88 | let+ a = P.pure 5 in
89 | let total = a + 5 in
90 | total
91 | in
92 | let v = P.parse_string p "" in
93 | v = 10p <?> 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
94 | open P
95 |
96 | ;;
97 | let p = P.char 'h' <|> P.char 'w' in
98 | let err_msg = "[error]" in
99 | let p = p <?> err_msg in
100 | let v =
101 | try
102 | let _ = P.parse_string p "" in
103 | false
104 | with
105 | | P.Parser
106 | { offset = 0
107 | ; line_number = 0
108 | ; column_number = 0
109 | ; msg = "[error]"
110 | } ->
111 | true
112 | | _ -> false
113 | in
114 | v = trueString.Infixp >>= f returns a new parser b where,
a is the parsed value of pb is f a Also known as bind operation.Examples
module P = Reparse.String
3 | open P
4 |
5 | ;;
6 | let f a = P.pure (Char.code a) in
7 | let p = P.char 'h' in
8 | let p = p >>= f in
9 | let v = P.parse_string p "hello" in
10 | v = 104p >>| 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
11 | open P
12 |
13 | ;;
14 | let f a = Char.code a in
15 | let p = P.char 'h' in
16 | let p = p >>| f in
17 | let v = P.parse_string p "hello" in
18 | v = 104pf <*> 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 pfa is the parsed value of qb is f a Also known as Applicative operation.Examples
module P = Reparse
19 | open P
20 |
21 | ;;
22 | let f a = a + 2 in
23 | let pf = P.pure f in
24 | let q = P.pure 2 in
25 | let p = pf <*> q in
26 | let v = P.parse_string p "hello" in
27 | v = 4v <$ p replaces the parse value of p with v.
Examples
module P = Reparse.String
28 | open P
29 |
30 | ;;
31 | let v = "hello" in
32 | let p = P.char 'h' in
33 | let p = v <$ p in
34 | let v2 = P.parse_string p "hello" in
35 | v2 = "hello"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.String
36 | open P
37 |
38 | ;;
39 | let p = P.string "world" in
40 | let q = P.pure "hello" in
41 | let p = p *> q in
42 | let v = P.parse_string p "world" in
43 | 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.String
44 | open P
45 |
46 | ;;
47 | let p = P.string "world" in
48 | let q = P.pure "hello" in
49 | let p = p <* q in
50 | let v = P.parse_string p "world" in
51 | 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.String
52 | open P
53 |
54 | ;;
55 | let p = P.char 'h' in
56 | let q = P.char 'w' in
57 | let p = p <|> q in
58 | let v = P.parse_string p "world" in
59 | v = 'w'p succeeds therefore we return its parsed value 'h'
let p = P.char 'h' in
60 | let q = P.char 'w' in
61 | let p = p <|> q in
62 | let v = P.parse_string p "hello" in
63 | v = 'h'The parser fails if both p and q fails.
let p = P.char 'h' in
64 | let q = P.char 'w' in
65 | let p = p <|> q in
66 | let v =
67 | try
68 | let _ = P.parse_string p "" in
69 | false
70 | with
71 | | _ -> true
72 | in
73 | v = truelet* is a let syntax binding for Reparse.Infix.((>>=))
Examples
module P = Reparse.String
74 | open P
75 |
76 | ;;
77 | let p =
78 | let* a = P.pure 5 in
79 | let total = a + 5 in
80 | P.pure total
81 | in
82 | let v = P.parse_string p "" in
83 | v = 10let* is a let syntax binding for Reparse.((>|=))
Examples
module P = Reparse.String
84 | open P
85 |
86 | ;;
87 | let p =
88 | let+ a = P.pure 5 in
89 | let total = a + 5 in
90 | total
91 | in
92 | let v = P.parse_string p "" in
93 | v = 10p <?> 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
94 | open P
95 |
96 | ;;
97 | let p = P.char 'h' <|> P.char 'w' in
98 | let err_msg = "[error]" in
99 | let p = p <?> err_msg in
100 | let v =
101 | try
102 | let _ = P.parse_string p "" in
103 | false
104 | with
105 | | P.Parser
106 | { offset = 0
107 | ; line_number = 0
108 | ; column_number = 0
109 | ; msg = "[error]"
110 | } ->
111 | true
112 | | _ -> false
113 | in
114 | v = truePARSER.Infixp >>= f returns a new parser b where,
a is the parsed value of pb is f a Also known as bind operation.Examples
module P = Reparse.String
3 | open P
4 |
5 | ;;
6 | let f a = P.pure (Char.code a) in
7 | let p = P.char 'h' in
8 | let p = p >>= f in
9 | let v = P.parse_string p "hello" in
10 | v = 104p >>| 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
11 | open P
12 |
13 | ;;
14 | let f a = Char.code a in
15 | let p = P.char 'h' in
16 | let p = p >>| f in
17 | let v = P.parse_string p "hello" in
18 | v = 104pf <*> 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 pfa is the parsed value of qb is f a Also known as Applicative operation.Examples
module P = Reparse
19 | open P
20 |
21 | ;;
22 | let f a = a + 2 in
23 | let pf = P.pure f in
24 | let q = P.pure 2 in
25 | let p = pf <*> q in
26 | let v = P.parse_string p "hello" in
27 | v = 4v <$ p replaces the parse value of p with v.
Examples
module P = Reparse.String
28 | open P
29 |
30 | ;;
31 | let v = "hello" in
32 | let p = P.char 'h' in
33 | let p = v <$ p in
34 | let v2 = P.parse_string p "hello" in
35 | v2 = "hello"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.String
36 | open P
37 |
38 | ;;
39 | let p = P.string "world" in
40 | let q = P.pure "hello" in
41 | let p = p *> q in
42 | let v = P.parse_string p "world" in
43 | 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.String
44 | open P
45 |
46 | ;;
47 | let p = P.string "world" in
48 | let q = P.pure "hello" in
49 | let p = p <* q in
50 | let v = P.parse_string p "world" in
51 | 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.String
52 | open P
53 |
54 | ;;
55 | let p = P.char 'h' in
56 | let q = P.char 'w' in
57 | let p = p <|> q in
58 | let v = P.parse_string p "world" in
59 | v = 'w'p succeeds therefore we return its parsed value 'h'
let p = P.char 'h' in
60 | let q = P.char 'w' in
61 | let p = p <|> q in
62 | let v = P.parse_string p "hello" in
63 | v = 'h'The parser fails if both p and q fails.
let p = P.char 'h' in
64 | let q = P.char 'w' in
65 | let p = p <|> q in
66 | let v =
67 | try
68 | let _ = P.parse_string p "" in
69 | false
70 | with
71 | | _ -> true
72 | in
73 | v = truelet* is a let syntax binding for Reparse.Infix.((>>=))
Examples
module P = Reparse.String
74 | open P
75 |
76 | ;;
77 | let p =
78 | let* a = P.pure 5 in
79 | let total = a + 5 in
80 | P.pure total
81 | in
82 | let v = P.parse_string p "" in
83 | v = 10let* is a let syntax binding for Reparse.((>|=))
Examples
module P = Reparse.String
84 | open P
85 |
86 | ;;
87 | let p =
88 | let+ a = P.pure 5 in
89 | let total = a + 5 in
90 | total
91 | in
92 | let v = P.parse_string p "" in
93 | v = 10p <?> 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
94 | open P
95 |
96 | ;;
97 | let p = P.char 'h' <|> P.char 'w' in
98 | let err_msg = "[error]" in
99 | let p = p <?> err_msg in
100 | let v =
101 | try
102 | let _ = P.parse_string p "" in
103 | false
104 | with
105 | | P.Parser
106 | { offset = 0
107 | ; line_number = 0
108 | ; column_number = 0
109 | ; msg = "[error]"
110 | } ->
111 | true
112 | | _ -> false
113 | in
114 | v = trueStream.Infixp >>= f returns a new parser b where,
a is the parsed value of pb is f a Also known as bind operation.Examples
module P = Reparse.String
3 | open P
4 |
5 | ;;
6 | let f a = P.pure (Char.code a) in
7 | let p = P.char 'h' in
8 | let p = p >>= f in
9 | let v = P.parse_string p "hello" in
10 | v = 104p >>| 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
11 | open P
12 |
13 | ;;
14 | let f a = Char.code a in
15 | let p = P.char 'h' in
16 | let p = p >>| f in
17 | let v = P.parse_string p "hello" in
18 | v = 104pf <*> 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 pfa is the parsed value of qb is f a Also known as Applicative operation.Examples
module P = Reparse
19 | open P
20 |
21 | ;;
22 | let f a = a + 2 in
23 | let pf = P.pure f in
24 | let q = P.pure 2 in
25 | let p = pf <*> q in
26 | let v = P.parse_string p "hello" in
27 | v = 4v <$ p replaces the parse value of p with v.
Examples
module P = Reparse.String
28 | open P
29 |
30 | ;;
31 | let v = "hello" in
32 | let p = P.char 'h' in
33 | let p = v <$ p in
34 | let v2 = P.parse_string p "hello" in
35 | v2 = "hello"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.String
36 | open P
37 |
38 | ;;
39 | let p = P.string "world" in
40 | let q = P.pure "hello" in
41 | let p = p *> q in
42 | let v = P.parse_string p "world" in
43 | 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.String
44 | open P
45 |
46 | ;;
47 | let p = P.string "world" in
48 | let q = P.pure "hello" in
49 | let p = p <* q in
50 | let v = P.parse_string p "world" in
51 | 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.String
52 | open P
53 |
54 | ;;
55 | let p = P.char 'h' in
56 | let q = P.char 'w' in
57 | let p = p <|> q in
58 | let v = P.parse_string p "world" in
59 | v = 'w'p succeeds therefore we return its parsed value 'h'
let p = P.char 'h' in
60 | let q = P.char 'w' in
61 | let p = p <|> q in
62 | let v = P.parse_string p "hello" in
63 | v = 'h'The parser fails if both p and q fails.
let p = P.char 'h' in
64 | let q = P.char 'w' in
65 | let p = p <|> q in
66 | let v =
67 | try
68 | let _ = P.parse_string p "" in
69 | false
70 | with
71 | | _ -> true
72 | in
73 | v = truelet* is a let syntax binding for Reparse.Infix.((>>=))
Examples
module P = Reparse.String
74 | open P
75 |
76 | ;;
77 | let p =
78 | let* a = P.pure 5 in
79 | let total = a + 5 in
80 | P.pure total
81 | in
82 | let v = P.parse_string p "" in
83 | v = 10let* is a let syntax binding for Reparse.((>|=))
Examples
module P = Reparse.String
84 | open P
85 |
86 | ;;
87 | let p =
88 | let+ a = P.pure 5 in
89 | let total = a + 5 in
90 | total
91 | in
92 | let v = P.parse_string p "" in
93 | v = 10p <?> 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
94 | open P
95 |
96 | ;;
97 | let p = P.char 'h' <|> P.char 'w' in
98 | let err_msg = "[error]" in
99 | let p = p <?> err_msg in
100 | let v =
101 | try
102 | let _ = P.parse_string p "" in
103 | false
104 | with
105 | | P.Parser
106 | { offset = 0
107 | ; line_number = 0
108 | ; column_number = 0
109 | ; msg = "[error]"
110 | } ->
111 | true
112 | | _ -> false
113 | in
114 | v = true