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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/// A convenient macro to create a choice parser from two or more parsers.
///
/// This macro recursively combines multiple parsers into nested `por` combinators,
/// allowing you to write `choice!(p1, p2, p3, ...)` instead of nested calls.
///
/// # Examples
///
/// ```
/// use cypress::prelude::*;
/// let input = "B".into_input();
/// let parser1 = just('A').or(just('B'));
/// let parser2 = choice!(just('A'), just('B'));
///
/// // Note that these two parsers are the same
/// match parser1.parse(input.clone()) {
/// Ok(PSuccess { val, rest: _ }) => assert_eq!(val, b'B'),
/// Err(_) => assert!(false),
/// };
///
/// match parser2.parse(input) {
/// Ok(PSuccess { val, rest: _ }) => assert_eq!(val, b'B'),
/// Err(_) => assert!(false),
/// };
/// ```
///
/// This is equivalent to `por(p1, por(p2, p3))`.
/// Macro for conveniently creating a match and map like statement.
///
/// This macro recursively combines multiple literal or parsers along with
/// an into_(..) clause to convert the first successfully parsed token into
/// the associated value.
///
/// Note that using a declerative macro one cannot differentiate between char, u8,
/// and str. Thus, one must use `(pident(..))` to match a str. Further note that
/// you must wrap such parsers in parens to properly parse with macros.
///
/// # Examples
///
/// ```
/// use cypress::prelude::*;
/// let input = "Luke".into_input();
///
/// #[derive(Clone, PartialEq, Debug)]
/// enum StarWarsCharacters {
/// Luke,
/// ObiWan,
/// DarthVader,
/// Yoda
/// };
///
/// let select_parser = select! {
/// (pident("ObiWan")) => StarWarsCharacters::ObiWan,
/// (pident("Yoda")) => StarWarsCharacters::Yoda,
/// (pident("DarthVader")) => StarWarsCharacters::DarthVader,
/// (pident("Luke")) => StarWarsCharacters::Luke,
/// };
///
/// // equivalent parser using choice!(..)
/// let choice_parser = choice!(
/// (pident("ObiWan")).into_(StarWarsCharacters::ObiWan),
/// (pident("Yoda")).into_(StarWarsCharacters::Yoda),
/// (pident("DarthVader")).into_(StarWarsCharacters::DarthVader),
/// (pident("Luke")).into_(StarWarsCharacters::Luke),
/// );
///
/// let Some(PSuccess {val: select_val, rest: _ }) = select_parser.parse(input.clone()).ok() else { panic!() };
/// let Some(PSuccess {val: choice_val, rest: _ }) = choice_parser.parse(input).ok() else { panic!() };
/// assert_eq!(select_val, choice_val);
///
/// // or use just chars to make parsing more ergonomic
/// #[derive(Clone, PartialEq, Debug)]
/// enum Letter {
/// A, B, C, D
/// };
///
/// let letter_parser = select! {
/// 'A' => Letter::A, // expands to just('A') => Letter::A like above example
/// 'B' => Letter::B,
/// 'C' => Letter::C,
/// 'D' => Letter::D,
/// }.many();
///
/// let input = "ADBC".into_input();
///
/// match letter_parser.parse(input) {
/// Ok(PSuccess { val, rest: _ }) => assert_eq!(val, vec![Letter::A, Letter::D, Letter::B, Letter::C]),
/// Err(_) => assert!(false)
/// }
///
/// ```
=> ;
=> ;
=> ;
=> ;
}
/// Macro for ergonomic parsing in sequence.
///
/// This macro recursively combines multiple parser or literals
/// into a sequene parser using pseq. Optionally one can use `=>`
/// along with a closure to map the parser result into a useful result.
///
/// See [`select!`] for details about passing parser versus literals.
///
/// # Examples
///
/// ```
/// use cypress::prelude::*;
///
/// let input = "1+2".into_input();
///
/// #[derive(PartialEq, Debug)]
/// enum Expr {
/// Num(u8),
/// Add(Box<Expr>, Box<Expr>)
/// };
///
/// let parser = sequence!(
/// (pnum()) > '+' > (pnum()) => |(a, (_, b))| Expr::Add(Box::new(Expr::Num(a)), Box::new(Expr::Num(b)))
/// );
///
/// match parser.parse(input) {
/// Ok(PSuccess {val, rest: _ }) => assert_eq!(val, Expr::Add(Box::new(Expr::Num(b'1')), Box::new(Expr::Num(b'2')))),
/// Err(_) => assert!(false),
/// };
/// ```
/// Macro for wraping expressions or literals, typically for inside other macros.
///
/// This macro will be called upon automatically from macros like [`sequence!`] and [`choice!`]
/// but can also be used on its own.
///
/// # Examples
///
/// ```
/// use cypress::prelude::*;
///
/// // the following to parsers are equivalent
/// let p1 = choice!(wrap!('A'), wrap!('B'), wrap!('C'));
/// let p2 = choice!(just('A'), just('B'), just('C'));
///
/// match p1.parse("B".into_input()) {
/// Ok(PSuccess { val, rest: _ }) => assert_eq!(val, b'B'),
/// Err(_) => assert!(false),
/// };
///
/// match p2.parse("B".into_input()) {
/// Ok(PSuccess { val, rest: _ }) => assert_eq!(val, b'B'),
/// Err(_) => assert!(false),
/// };
/// ```
;
=> ;
=> ;
}
/// For ergonomic use of foldl for parsing with precedence.
///
/// The lower the block the lower the precedence, start with the basecase
/// in the below example is would be a number, then successive blocks are
/// at one lower precedence level. Wrap the level in `{..}`.
///
/// ```rust
/// use cypress::prelude::*;
///
/// #[derive(Clone, PartialEq, Debug)]
/// enum BinOp {
/// Add,
/// Sub,
/// Mult,
/// Div,
/// };
///
/// #[derive(Clone, PartialEq, Debug)]
/// enum Expr {
/// Num(i32),
/// Op(Box<Expr>, BinOp, Box<Expr>),
/// };
///
/// let base = pnum()
/// .many1()
/// .map(|xs| Expr::Num(String::from_utf8(xs).unwrap().parse::<i32>().unwrap()))
/// .padded_by(pws());
///
/// // build precedence scale
/// let parser = precedence! {
/// // highest precedence
/// base,
/// {
/// choice!(
/// just('*').into_(BinOp::Mult),
/// just('/').into_(BinOp::Div),
/// )
/// =>
/// |a, (bop, b)| Expr::Op(Box::new(a), bop, Box::new(b))
/// },
/// // lowest precedence
/// {
/// choice!(
/// just('+').into_(BinOp::Add),
/// just('-').into_(BinOp::Sub),
/// )
/// =>
/// |a, (bop, b)| Expr::Op(Box::new(a), bop, Box::new(b))
/// },
/// };
///
/// let input = "3 + 2 * 2 - 1";
///
/// // should bind `2` and `2` together with `*`
/// let expected_result = Expr::Op(
/// Box::new(Expr::Op(
/// Box::new(Expr::Num(3)),
/// BinOp::Add,
/// Box::new(Expr::Op(
/// Box::new(Expr::Num(2)),
/// BinOp::Mult,
/// Box::new(Expr::Num(2)),
/// )),
/// )),
/// BinOp::Sub,
/// Box::new(Expr::Num(1)),
/// );
/// match parser.parse(input.into_input()) {
/// Ok(PSuccess { val, rest: _ }) => assert_eq!(val, expected_result),
/// Err(_) => assert!(false),
/// }
/// ```
};
// Recursive: apply first and then the rest
=> ;
}