1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//
// ::formally - the open-source formal methods toolchain
//
// Copyright (c) 2025 Nicola Gigante
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
use crate;
use *;
/// A parser that alwasys succeeds.
/// A parser that alwasys fails.
/// Same as [reject()] but returning `()` instead of an arbitrary type.
/// Converts a function returning a parser into a parser.
///
/// [lazy()] takes a function of the form `Fn() -> Parser<Out>` and returns a `Parser<Out>` just
/// by invoking the given function, but *during parsing*, i.e., inside the parsing function.
///
/// This means the given function is actually executed each time, and only if, the parser is
/// executed.
///
/// This is useful to implement recursive parsers, see [recursive()].
/// Utility to create recursive parsers.
///
/// [recursive()] does not actually implement any recursion, but is just an alias for
/// `lazy(f).map(Box::new)`. This is however exactly what we need when building recursive parsers.
///
/// Consider a parser for the following type:
/// ```rust
/// pub enum Expr {
/// Var(String),
/// Plus(Box<Expr>, Box<Expr>),
/// Mult(Box<Expr>, Box<Expr>)
/// }
/// ```
/// The `Expr` fields in the enum are boxed because the type is recursive, as is common in many
/// recursive data types in Rust. Now, to parse such a type, one may write something like this:
///
/// ```rust,no_run
/// # use formally_io::parse::{*, combinators::*, parsers::*};
/// # use formally_support::*;
/// # mod formally {
/// # pub mod io { pub use formally_io::*; }
/// # pub mod support { pub use formally_support::*; }
/// # }
/// # pub enum Expr {
/// # Var(String),
/// # Plus(Box<Expr>, Box<Expr>),
/// # Mult(Box<Expr>, Box<Expr>)
/// # }
/// pub fn variable() -> Parser<'static, String> {
/// alphabetic().many1().to_string()
/// }
///
/// pub fn expr() -> Parser<'static, Expr> {
/// variable().map(Expr::Var)
/// .or(expr().and(char('+')).and(expr())
/// .map(|((e1,_), e2)| Expr::Plus(Box::new(e1), Box::new(e2))))
/// .or(expr().and(char('*')).and(expr())
/// .map(|((e1,_), e2)| Expr::Mult(Box::new(e1), Box::new(e2))))
/// }
/// ```
///
/// However, the code above does not work, because `expr()` calls itself in an infinite recursion.
/// What we need is to delay the execution of `expr()` to when the parser will be actually executed.
/// This is exactly the job of [lazy()]. On top of this, [recursive()] boxes the results to ease the
/// common pattern above. The correct code therefore is the following:
///
/// ```rust,no_run
/// # use formally_io::parse::{*, combinators::*, parsers::*};
/// # use formally_support::*;
/// # mod formally {
/// # pub mod io { pub use formally_io::*; }
/// # pub mod support { pub use formally_support::*; }
/// # }
/// # pub enum Expr {
/// # Var(String),
/// # Plus(Box<Expr>, Box<Expr>),
/// # Mult(Box<Expr>, Box<Expr>)
/// # }
/// # pub fn variable() -> Parser<'static, String> {
/// # alphabetic().many1().to_string()
/// # }
/// pub fn expr() -> Parser<'static, Expr> {
/// variable().map(Expr::Var)
/// .or(recursive(expr).and(char('+')).and(recursive(expr))
/// .map(|((e1,_), e2)| Expr::Plus(e1, e2)))
/// .or(recursive(expr).and(char('*')).and(recursive(expr))
/// .map(|((e1,_), e2)| Expr::Mult(e1, e2)))
/// }
/// ```
///
/// If a different smart pointer is needed, one can always use [lazy()] directly.
/// A parser which always succeeds returning a `None`.
/// A parser which always succeeds returning the current parsing location.
/// A parser that succeeds if and only if the argument fails.