lex-just-parse 0.3.0

lex-just-parse is a simple and easy-to-use lexing and parsing crate for Rust. It provides a fast, stream-based lexical analyzer (Lexer) and combinator-style parser utilities (Parser) to assist in developing custom programming languages, DSLs, or handling structured text parsing needs.
Documentation
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//! Combinator parsing utilities.
//!
//! Provides the core `Parser` type and related type definitions
//! for building lexer-driven parsers.

use crate::lexer::Lexer;

/// A mutable reference to a `Lexer`, commonly used by parser functions to consume tokens.
pub type RefLexer<'lex> = &'lex mut Lexer<'lex>;

/// Represents a typical parser function signature that takes a lexer reference and returns a `Parser` result.
pub type ParserFn<'lex, T, E> = fn(lex: RefLexer) -> Parser<T, E>;

#[macro_export]
/// The `?` operator for [`Parser`]
macro_rules! try_parse {
    ($f:expr) => {
        match $f {
            Parser::Success(lexer, expr) => (lexer, expr),
            Parser::Fail(lexer, e) => return Parser::Fail(lexer, e),
        }
    };
    ($lex:ident, $f:expr) => {
        match $f {
            Parser::Success(lexer, expr) => {
                $lex = lexer;
                expr
            }
            Parser::Fail(lexer, e) => {
                $lex = lexer;
                return Parser::Fail($lex, e);
            }
        }
    };
}

/// Represents the result of a parsing operation.
///
/// A parser either succeeds with an advanced lexer and a parsed value `T`, or fails
/// and returns the unchanged lexer state along with an error `E`.
pub enum Parser<'lex, T, E> {
    Success(RefLexer<'lex>, T),
    Fail(RefLexer<'lex>, E),
}

impl<'lex, T, E> Parser<'lex, T, E> {
    /// Chains another parsing attempt if the current parser failed.
    pub fn or_else<F>(self, f: F) -> Self
    where
        F: FnOnce(RefLexer) -> Parser<T, E>,
    {
        match self {
            Parser::Success(..) => self,
            Parser::Fail(lexer, ..) => f(lexer),
        }
    }

    /// Chains a subsequent parser if the current parser succeeds.
    pub fn and_then<U, F>(self, f: F) -> Parser<'lex, U, E>
    where
        F: FnOnce(RefLexer<'lex>, T) -> Parser<'lex, U, E>,
    {
        match self {
            Parser::Success(lexer, e) => f(lexer, e),
            Parser::Fail(lexer, e) => Parser::Fail(lexer, e),
        }
    }

    /// Converts this parser result into a standard `Result`,
    /// returning the parsed item or the lexer and error pair upon failure.
    pub fn success(self) -> Result<T, (RefLexer<'lex>, E)> {
        match self {
            Parser::Success(_, e) => Ok(e),
            Parser::Fail(lex, e) => Err((lex, e)),
        }
    }
}

/// Parses zero or more occurrences of `parser` until it fails.
/// Returns the collected items and the updated lexer.
pub fn many<'lex, T, E, F>(mut lex: RefLexer<'lex>, parser: F) -> Parser<'lex, Vec<T>, E>
where
    F: Fn(RefLexer<'lex>) -> Parser<'lex, T, E>,
{
    let mut results = Vec::new();
    loop {
        match parser(lex) {
            Parser::Success(next_lex, val) => {
                results.push(val);
                lex = next_lex;
            }
            Parser::Fail(next_lex, _) => {
                return Parser::Success(next_lex, results);
            }
        }
    }
}

/// Parses one or more occurrences of `parser`.
/// Returns Fail if the first attempt fails.
pub fn many1<'lex, T, E, F>(lex: RefLexer<'lex>, parser: F) -> Parser<'lex, Vec<T>, E>
where
    F: Fn(RefLexer<'lex>) -> Parser<'lex, T, E>,
{
    match parser(lex) {
        Parser::Success(lex, first_val) => {
            let mut results = vec![first_val];
            let mut current_lex = lex;
            loop {
                match parser(current_lex) {
                    Parser::Success(next_lex, val) => {
                        results.push(val);
                        current_lex = next_lex;
                    }
                    Parser::Fail(next_lex, _) => {
                        return Parser::Success(next_lex, results);
                    }
                }
            }
        }
        Parser::Fail(lex, err) => Parser::Fail(lex, err),
    }
}

/// Parses zero or more occurrences of `parser` separated by `separator`.
pub fn sep_by<'lex, T, S, E, F, G>(
    lex: RefLexer<'lex>,
    parser: F,
    separator: G,
) -> Parser<'lex, Vec<T>, E>
where
    F: Fn(RefLexer<'lex>) -> Parser<'lex, T, E>,
    G: Fn(RefLexer<'lex>) -> Parser<'lex, S, E>,
{
    match parser(lex) {
        Parser::Success(lex, first_val) => {
            let mut results = vec![first_val];
            let mut current_lex = lex;
            loop {
                match separator(current_lex) {
                    Parser::Success(sep_lex, _) => match parser(sep_lex) {
                        Parser::Success(next_lex, val) => {
                            results.push(val);
                            current_lex = next_lex;
                        }
                        Parser::Fail(fail_lex, _) => {
                            return Parser::Success(fail_lex, results);
                        }
                    },
                    Parser::Fail(next_lex, _) => {
                        return Parser::Success(next_lex, results);
                    }
                }
            }
        }
        Parser::Fail(lex, _) => Parser::Success(lex, Vec::new()),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::lexer::{Lexer, TokenKind};

    fn parse_ident<'lex>(lex: RefLexer<'lex>, expected: &str) -> Parser<'lex, String, String> {
        let tok = lex.peek().clone();
        if tok.kind == TokenKind::Identifier && tok.source() == expected {
            lex.next(); // consume
            Parser::Success(lex, tok.source.to_string())
        } else {
            Parser::Fail(
                lex,
                format!("Expected identifier '{}', got {:?}", expected, tok.kind),
            )
        }
    }

    #[test]
    fn test_parser_success_and_fail_variants() {
        let mut lexer = Lexer::new("abc");
        let result_success = parse_ident(&mut lexer, "abc");
        match result_success {
            Parser::Success(_, val) => assert_eq!(val, "abc"),
            _ => panic!("Expected Success"),
        }

        let mut lexer = Lexer::new("xyz");
        let result_fail = parse_ident(&mut lexer, "abc");
        match result_fail {
            Parser::Fail(_, err) => assert!(err.contains("Expected identifier 'abc'")),
            _ => panic!("Expected Fail"),
        }
    }

    #[test]
    fn test_parser_success_method() {
        let mut lexer = Lexer::new("abc");
        let result = parse_ident(&mut lexer, "abc").success();
        assert_eq!(result.ok().unwrap(), "abc");

        let mut lexer2 = Lexer::new("xyz");
        let result2 = parse_ident(&mut lexer2, "abc").success();
        assert!(result2.is_err());
        let (remaining_lexer, err) = result2.err().unwrap();
        assert_eq!(remaining_lexer.next().source(), "xyz");
        assert!(err.contains("Expected identifier 'abc'"));
    }

    #[test]
    fn test_parser_or_else() {
        // Test standard or_else functionality where first parser fails and second succeeds
        let mut lexer = Lexer::new("xyz");
        let result = parse_ident(&mut lexer, "abc").or_else(|lex| parse_ident(lex, "xyz"));

        match result {
            Parser::Success(_, val) => assert_eq!(val, "xyz"),
            _ => panic!("Expected success after or_else fallback"),
        }

        // Test or_else where first parser succeeds (second should not run)
        let mut lexer = Lexer::new("abc");
        let result = parse_ident(&mut lexer, "abc")
            .or_else(|_lex| panic!("Should not execute or_else fallback when first succeeded"));
        match result {
            Parser::Success(_, val) => assert_eq!(val, "abc"),
            _ => panic!("Expected success"),
        }
    }

    #[test]
    fn test_parser_and_then() {
        // Test chaining with and_then
        // We want to parse "abc" then "xyz"
        let mut lexer = Lexer::new("abc xyz");
        let result = parse_ident(&mut lexer, "abc").and_then(|lex, first_val| {
            parse_ident(lex, "xyz")
                .and_then(|lex, second_val| Parser::Success(lex, (first_val, second_val)))
        });

        match result {
            Parser::Success(_, (v1, v2)) => {
                assert_eq!(v1, "abc");
                assert_eq!(v2, "xyz");
            }
            _ => panic!("Expected success for chained and_then"),
        }

        // Test and_then failure propagation
        let mut lexer2 = Lexer::new("abc error");
        let result2 =
            parse_ident(&mut lexer2, "abc").and_then(|lex, _first_val| parse_ident(lex, "xyz"));
        match result2 {
            Parser::Fail(_, err) => assert!(err.contains("Expected identifier 'xyz'")),
            _ => panic!("Expected Fail"),
        }

        // Test and_then when first parser fails (second should not run)
        let mut lexer3 = Lexer::new("error xyz");
        let result3 = parse_ident(&mut lexer3, "abc").and_then(
            |_lex, _first_val| -> Parser<String, String> {
                panic!("Should not execute and_then function when first failed")
            },
        );
        match result3 {
            Parser::Fail(_, err) => assert!(err.contains("Expected identifier 'abc'")),
            _ => panic!("Expected Fail"),
        }
    }

    #[test]
    fn test_try_parse_macro() {
        // Define a parser function that parses "abc xyz" using try_parse!
        fn parse_pair<'lex>(lex: RefLexer<'lex>) -> Parser<'lex, (String, String), String> {
            let (lex, first) = try_parse!(parse_ident(lex, "abc"));
            let (lex, second) = try_parse!(parse_ident(lex, "xyz"));
            Parser::Success(lex, (first, second))
        }

        let mut lexer = Lexer::new("abc xyz");
        let res = parse_pair(&mut lexer);
        match res {
            Parser::Success(_, (v1, v2)) => {
                assert_eq!(v1, "abc");
                assert_eq!(v2, "xyz");
            }
            _ => panic!("Expected success using try_parse!"),
        }

        let mut lexer2 = Lexer::new("abc err");
        let res2 = parse_pair(&mut lexer2);
        match res2 {
            Parser::Fail(_, err) => assert!(err.contains("Expected identifier 'xyz'")),
            _ => panic!("Expected Fail using try_parse!"),
        }
    }

    #[test]
    fn test_try_parse_macro_in_place() {
        fn parse_pair_in_place<'lex>(
            mut lex: RefLexer<'lex>,
        ) -> Parser<'lex, (String, String), String> {
            let first = try_parse!(lex, parse_ident(lex, "abc"));
            let second = try_parse!(lex, parse_ident(lex, "xyz"));
            Parser::Success(lex, (first, second))
        }

        let mut lexer = Lexer::new("abc xyz");
        let res = parse_pair_in_place(&mut lexer);
        match res {
            Parser::Success(_, (v1, v2)) => {
                assert_eq!(v1, "abc");
                assert_eq!(v2, "xyz");
            }
            _ => panic!("Expected success using try_parse! in-place"),
        }

        let mut lexer2 = Lexer::new("abc err");
        let res2 = parse_pair_in_place(&mut lexer2);
        match res2 {
            Parser::Fail(_, err) => assert!(err.contains("Expected identifier 'xyz'")),
            _ => panic!("Expected Fail using try_parse! in-place"),
        }
    }

    #[test]
    fn test_combinator_many() {
        let mut lexer = Lexer::new("abc abc abc xyz");
        let result = many(&mut lexer, |l| parse_ident(l, "abc"));
        match result {
            Parser::Success(remaining_lexer, items) => {
                assert_eq!(items, vec!["abc", "abc", "abc"]);
                assert_eq!(remaining_lexer.next().source(), "xyz");
            }
            _ => panic!("Expected Success for many"),
        }

        let mut lexer2 = Lexer::new("xyz");
        let result2 = many(&mut lexer2, |l| parse_ident(l, "abc"));
        match result2 {
            Parser::Success(remaining_lexer, items) => {
                assert!(items.is_empty());
                assert_eq!(remaining_lexer.next().source(), "xyz");
            }
            _ => panic!("Expected Success for many with empty results"),
        }
    }

    #[test]
    fn test_combinator_many1() {
        let mut lexer = Lexer::new("abc abc xyz");
        let result = many1(&mut lexer, |l| parse_ident(l, "abc"));
        match result {
            Parser::Success(remaining_lexer, items) => {
                assert_eq!(items, vec!["abc", "abc"]);
                assert_eq!(remaining_lexer.next().source(), "xyz");
            }
            _ => panic!("Expected Success for many1"),
        }

        let mut lexer2 = Lexer::new("xyz");
        let result2 = many1(&mut lexer2, |l| parse_ident(l, "abc"));
        match result2 {
            Parser::Fail(remaining_lexer, err) => {
                assert!(err.contains("Expected identifier 'abc'"));
                assert_eq!(remaining_lexer.next().source(), "xyz");
            }
            _ => panic!("Expected Fail for many1 on immediately failing parser"),
        }
    }

    #[test]
    fn test_combinator_sep_by() {
        fn parse_comma<'lex>(lex: RefLexer<'lex>) -> Parser<'lex, (), String> {
            let tok = lex.peek().clone();
            if tok.kind == TokenKind::Comma {
                lex.next();
                Parser::Success(lex, ())
            } else {
                Parser::Fail(lex, "Expected comma".to_string())
            }
        }

        let mut lexer = Lexer::new("abc , abc , abc ;");
        let result = sep_by(&mut lexer, |l| parse_ident(l, "abc"), parse_comma);
        match result {
            Parser::Success(remaining_lexer, items) => {
                assert_eq!(items, vec!["abc", "abc", "abc"]);
                assert_eq!(remaining_lexer.next().kind, TokenKind::SemiColon);
            }
            _ => panic!("Expected Success for sep_by"),
        }

        let mut lexer2 = Lexer::new("xyz");
        let result2 = sep_by(&mut lexer2, |l| parse_ident(l, "abc"), parse_comma);
        match result2 {
            Parser::Success(remaining_lexer, items) => {
                assert!(items.is_empty());
                assert_eq!(remaining_lexer.next().source(), "xyz");
            }
            _ => panic!("Expected Success for sep_by on empty sequence"),
        }
    }
}