beancount-parser-lima 0.16.2

A zero-copy parser for Beancount
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
use super::types::*;
use logos::Logos;
use rust_decimal::Decimal;
use std::{
    borrow::Cow,
    error::Error,
    fmt::{self, Debug, Display, Formatter},
    ops::Range,
    str::FromStr,
};
use time::{Date, Month, Time};
use unescaper::unescape;

// when adjusting any of these regexes, be sure to check whether `RecoveryToken` needs the same
#[derive(Logos, Clone, Debug, PartialEq, Eq)]
#[logos(error = LexerError, skip r"[ \t]+")]
// ANOMALY: to be ignored, we have to require a flag occurs at the beginning of a line, otherwise
// indented tag or flagged postings are consumed by this rule.
//
// It ought to be possible to ignore an indented line where colon is the first non-whitespace character,
// but if we try to do that here we hit a limitation in Logos which means we lose indent not followed by colon.
#[logos(subpattern ignored_whole_line= r"([*!&#?%:].*\r?\n)")] // rolled into end-of-line handling below
#[logos(subpattern comment_to_eol= r"(;.*)")] // rolled into end-of-line handling below
#[logos(subpattern currency = r"[A-Z][A-Z0-9'\._-]*|/[0-9'\._-]*[A-Z][A-Z0-9'\._-]*")]
#[logos(subpattern date = r"\d{4}[\-/]\d{2}[\-/]\d{2}")]
#[logos(subpattern time = r"\d{1,2}:\d{2}(:\d{2})?")]
#[logos(subpattern account_type = r"[\p{Lu}\p{Lo}][\p{L}\p{N}\-]*")]
#[logos(subpattern account_name = r"[\p{Lu}\p{Lo}\p{N}][\p{L}\p{N}\-]*")]
#[logos(subpattern string_literal = r#""([^\\"]|\\.)*""#)]
#[logos(subpattern number = r"\d+(,\d{3})*(\.\d+)?")]
#[logos(subpattern tag_or_link_identifier = r"[A-Za-z0-9\-_/.]+")]
#[logos(subpattern key = r"[a-z][a-zA-Z0-9\-_]+")]
pub enum Token<'a> {
    #[token("TRUE")]
    True,
    #[token("FALSE")]
    False,
    #[token("NULL")]
    Null,

    #[regex(r#"(?&currency)"#, |lex| lex.slice() )]
    Currency(&'a str),

    #[token("|")]
    Pipe,
    #[token("@@")]
    AtAt,
    #[token("@")]
    At,
    #[token("{{")]
    LcurlCurl,
    #[token("}}")]
    RcurlCurl,
    #[token("{")]
    Lcurl,
    #[token("}")]
    Rcurl,
    #[token(",")]
    Comma,
    #[token("~")]
    Tilde,
    #[token("+")]
    Plus,
    #[token("-")]
    Minus,
    #[token("/")]
    Slash,
    #[token("(")]
    Lparen,
    #[token(")")]
    Rparen,
    #[token("#")]
    Hash,
    #[token("*")]
    Asterisk,
    #[token(":")]
    Colon,

    #[token("!", |_lex| Flag::Exclamation)]
    #[token("&", |_lex| Flag::Ampersand)]
    #[token("?", |_lex| Flag::Question)]
    #[token("%", |_lex| Flag::Percent)]
    #[regex("'[A-Z]", |lex| FlagLetter::try_from(lex.slice().chars().nth(1).unwrap()).map(Flag::Letter))]
    DedicatedFlag(Flag),

    #[token("txn")]
    Txn,
    #[token("balance")]
    Balance,
    #[token("open")]
    Open,
    #[token("close")]
    Close,
    #[token("commodity")]
    Commodity,
    #[token("pad")]
    Pad,
    #[token("event")]
    Event,
    #[token("query")]
    Query,
    #[token("custom")]
    Custom,
    #[token("price")]
    Price,
    #[token("note")]
    Note,
    #[token("document")]
    Document,
    #[token("pushtag")]
    Pushtag,
    #[token("poptag")]
    Poptag,
    #[token("pushmeta")]
    Pushmeta,
    #[token("popmeta")]
    Popmeta,
    #[token("option")]
    Option,
    #[token("options")]
    Options,
    #[token("plugin")]
    Plugin,
    #[token("include")]
    Include,

    #[regex(r"(?&date)", |lex| parse_date(lex.slice()))]
    Date(Date),

    #[regex(r"(?&time)", |lex| parse_time(lex.slice()))]
    Time(Time),

    #[regex(r"(?&account_type)(:(?&account_name))+", |lex| lex.slice())]
    Account(&'a str),

    #[regex(r"(?&string_literal)", |lex| {
        let len = lex.slice().len();
        unescape_string_literal(&lex.slice()[1..len-1])
    })]
    StringLiteral(Cow<'a, str>),

    #[regex(r"(?&number)", |lex| parse_number(lex.slice()))]
    Number(Decimal),

    #[regex(r"#(?&tag_or_link_identifier)", |lex| &lex.slice()[1..])]
    Tag(&'a str),

    #[regex(r"\^(?&tag_or_link_identifier)", |lex| &lex.slice()[1..])]
    Link(&'a str),

    // A key is only supposed to be matched in a trailing colon context, but Logos doesn't support that,
    // so we work around it using parsers::keyword(), which is able to hijack a keyword out of another token.
    #[regex(r"(?&key)", |lex| lex.slice())]
    Key(&'a str),

    // end-of-line and indent, which is the only significant whitespace
    // ignored_whole_line must be anchored immediately after a newline
    #[regex(r"(?&comment_to_eol)?\r?\n(?&ignored_whole_line)*[ \t]+")]
    EolThenIndent,

    #[regex(r"(?&comment_to_eol)?\r?\n(?&ignored_whole_line)*")]
    Eol,

    // A trailing comment without a following newline (occurs at EOF).
    // Logos longest-match ensures this only fires when Eol can't match.
    #[regex(r";[^\r\n]*", allow_greedy = true)]
    EolComment,

    // indent handling is post-processed by lexer, when `EolThenIndent` is broken into separate `Eol` and `Indent`
    // which fails to capture an indented first line, but that isn't a thing in Beancount anyway
    Indent,

    // errors are returned as an error token
    Error(LexerError),
}

impl Token<'_> {
    fn is_eol(&self) -> bool {
        use Token::*;

        *self == EolThenIndent || *self == Eol || *self == EolComment
    }
}

impl Display for Token<'_> {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        use Token::*;
        const INDENT: &str = "    ";

        match self {
            True => f.write_str("TRUE"),
            False => f.write_str("FALSE"),
            Null => f.write_str("NULL"),

            Currency(cur) => write!(f, "{}", cur),

            Pipe => f.write_str("|"),
            AtAt => f.write_str("@@"),
            At => f.write_str("@"),
            LcurlCurl => f.write_str("{{"),
            RcurlCurl => f.write_str("}}"),
            Lcurl => f.write_str("{"),
            Rcurl => f.write_str("}"),
            Comma => f.write_str(","),
            Tilde => f.write_str("~"),
            Plus => f.write_str("+"),
            Minus => f.write_str("-"),
            Slash => f.write_str("/"),
            Lparen => f.write_str("("),
            Rparen => f.write_str(")"),
            Hash => f.write_str("#"),
            Asterisk => f.write_str("*"),
            Colon => f.write_str(":"),

            DedicatedFlag(x) => write!(f, "{}", x),

            Txn => f.write_str("txn"),
            Balance => f.write_str("balance"),
            Open => f.write_str("open"),
            Close => f.write_str("close"),
            Commodity => f.write_str("commodity"),
            Pad => f.write_str("pad"),
            Event => f.write_str("event"),
            Query => f.write_str("query"),
            Custom => f.write_str("custom"),
            Price => f.write_str("price"),
            Note => f.write_str("note"),
            Document => f.write_str("document"),
            Pushtag => f.write_str("pushtag"),
            Poptag => f.write_str("poptag"),
            Pushmeta => f.write_str("pushmeta"),
            Popmeta => f.write_str("popmeta"),
            Option => f.write_str("option"),
            Options => f.write_str("options"),
            Plugin => f.write_str("plugin"),
            Include => f.write_str("include"),

            Date(x) => write!(f, "{}", x),
            Time(x) => write!(f, "{}", x),
            Account(x) => write!(f, "{}", x),

            // TODO escapes
            StringLiteral(x) => write!(f, "\"{}\"", x),
            Number(x) => write!(f, "{}", x),
            Tag(x) => write!(f, "{}", x),
            Link(x) => write!(f, "{}", x),
            Key(x) => write!(f, "{}", x),

            EolThenIndent => write!(f, "\\n{}", INDENT),
            Eol => write!(f, "\\n"),
            EolComment => write!(f, "; ..."),
            Indent => write!(f, "{}", INDENT),

            Error(e) => write!(f, "ERROR {}", e),
        }
    }
}

type RangedToken<'a> = (Token<'a>, Range<usize>);
type RangedTokenOrError<'a> = (Result<Token<'a>, LexerError>, Range<usize>);
type SpannedToken<'a> = (Token<'a>, Span_);

// Work-around for Logos issue #315.  See `RecoveryAttempter`.
//
// when adjusting any of these regexes, be sure to make the same change in `Token`
#[derive(Logos, Clone, Debug, PartialEq, Eq)]
#[logos(error = LexerError)]
#[logos(subpattern currency = r"[A-Z][A-Z0-9'\._-]*|/[0-9'\._-]*[A-Z][A-Z0-9'\._-]*")]
#[logos(subpattern date = r"\d{4}[\-/]\d{2}[\-/]\d{2}")]
#[logos(subpattern number = r"\d+(,\d{3})*(\.\d+)?")]
enum RecoveryToken {
    #[token("-")]
    Minus,
    #[token("/")]
    Slash,
    #[token(":")]
    Colon,

    #[regex(r"(?&date)", |lex| parse_date(lex.slice()))]
    Date(Date),

    #[regex(r"(?&number)", |lex| parse_number(lex.slice()))]
    Number(Decimal),
}

impl From<RecoveryToken> for Token<'_> {
    fn from(value: RecoveryToken) -> Self {
        use RecoveryToken::*;

        match value {
            Minus => Token::Minus,
            Slash => Token::Slash,
            Colon => Token::Colon,
            Date(date) => Token::Date(date),
            Number(decimal) => Token::Number(decimal),
        }
    }
}

/// Lex the input discarding empty lines,
/// and forcing a final `Eol` in case missing.
///
/// Lexing errors are returned as `Error` tokens.
pub fn lex<'a>(s: &'a str) -> impl Iterator<Item = RangedToken<'a>> {
    let end_of_input = s.len()..s.len();
    lex_with_final_eol(s, Some(end_of_input))
}

/// Lex the input discarding empty lines.
#[cfg(test)]
pub fn bare_lex<'a>(s: &'a str) -> impl Iterator<Item = RangedToken<'a>> {
    lex_with_final_eol(s, None)
}

fn lex_with_final_eol<'a>(
    s: &'a str,
    final_eol: Option<Range<usize>>,
) -> impl Iterator<Item = RangedToken<'a>> {
    Token::lexer(s)
        .spanned()
        .attempt_recovery(s)
        .keyword_then_colon_to_key()
        .handle_eol_indent(final_eol)
}

// This is a work-around for Logos issue #315.
// Logos starts matching text like '/1.24' as a currency, and when it fails, it doesn't retry as slash followed by number.
//
// What we do is to attempt to lex the failed span using a subset of the original tokens, namely `RecoveryToken`.
// The set of `RecoveryToken` has been chosen to match where we currently get lexing failures, namely:
// 1. currency failures, which preclude shorter regexes which should match
// 2. colons
//
// It may be necessary to extend `RecoveryToken` as and when further failures in lexing arise.
// The long-term solution is the Logos rewrite, mentioned in that issue.
struct RecoveryAttempter<'a, I> {
    primary_iter: I,
    recovery: Option<(logos::SpannedIter<'a, RecoveryToken>, usize)>,
    source: &'a str,
}

impl<'a, I> RecoveryAttempter<'a, I>
where
    I: Iterator<Item = RangedTokenOrError<'a>>,
{
    fn new(iter: I, source: &'a str) -> Self {
        RecoveryAttempter {
            primary_iter: iter,
            recovery: None,
            source,
        }
    }

    fn create_recovery_lexer(&mut self, failed_span: &Range<usize>) {
        let failed_source = &self.source[failed_span.start..failed_span.end];

        self.recovery = Some((
            RecoveryToken::lexer(failed_source).spanned(),
            failed_span.start,
        ));
    }

    fn next_primary_item(&mut self) -> Option<RangedToken<'a>> {
        match self.primary_iter.next() {
            Some((Ok(t), span)) => Some((t, span)),
            Some((Err(_), span)) => {
                self.create_recovery_lexer(&span);
                self.next_recovery_item()
            }
            None => None,
        }
    }

    fn next_recovery_item(&mut self) -> Option<RangedToken<'a>> {
        fn offset(r: &Range<usize>, o: usize) -> Range<usize> {
            r.start + o..r.end + o
        }

        if let Some((recovery_iter, recovery_offset)) = self.recovery.as_mut() {
            if let Some((recovery_item, span)) = recovery_iter.next() {
                let span = offset(&span, *recovery_offset);
                match recovery_item {
                    Ok(tok) => Some((tok.into(), span)),
                    Err(e) => Some((Token::Error(e), span)),
                }
            } else {
                self.recovery = None;

                self.next_primary_item()
            }
        } else {
            self.next_primary_item()
        }
    }
}

impl<'a, I> Iterator for RecoveryAttempter<'a, I>
where
    I: Iterator<Item = RangedTokenOrError<'a>>,
{
    type Item = RangedToken<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        self.next_recovery_item()
    }
}

trait RecoveryAttempterIteratorAdaptor<'a>: Iterator<Item = RangedTokenOrError<'a>> + Sized {
    fn attempt_recovery(self, source: &'a str) -> RecoveryAttempter<'a, Self> {
        RecoveryAttempter::new(self, source)
    }
}

impl<'a, I: Iterator<Item = RangedTokenOrError<'a>>> RecoveryAttempterIteratorAdaptor<'a> for I {}

struct KeywordThenColonToKey<'a, I> {
    iter: I,
    pending_tok: Option<(RangedToken<'a>, Option<&'static str>)>,
}

impl<I> KeywordThenColonToKey<'_, I> {
    fn new(iter: I) -> Self {
        KeywordThenColonToKey {
            iter,
            pending_tok: None,
        }
    }

    // for a token which is potentially a key return that key string
    fn potential_key(tok: &Token) -> Option<&'static str> {
        match tok {
            Token::Txn => Some("txn"),
            Token::Balance => Some("balance"),
            Token::Open => Some("open"),
            Token::Close => Some("close"),
            Token::Commodity => Some("commodity"),
            Token::Pad => Some("pad"),
            Token::Event => Some("event"),
            Token::Query => Some("query"),
            Token::Custom => Some("custom"),
            Token::Price => Some("price"),
            Token::Note => Some("note"),
            Token::Document => Some("document"),
            Token::Pushtag => Some("pushtag"),
            Token::Poptag => Some("poptag"),
            Token::Pushmeta => Some("pushmeta"),
            Token::Popmeta => Some("popmeta"),
            Token::Option => Some("option"),
            Token::Options => Some("options"),
            Token::Plugin => Some("plugin"),
            Token::Include => Some("include"),
            _ => None,
        }
    }
}

impl<'a, I> Iterator for KeywordThenColonToKey<'a, I>
where
    I: Iterator<Item = RangedToken<'a>>,
{
    type Item = RangedToken<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.pending_tok.is_none() {
            if let Some(tok) = self.iter.next() {
                let potential_key = Self::potential_key(&tok.0);
                self.pending_tok = Some((tok, potential_key));
            }
        }

        match self.pending_tok.take() {
            Some((pending_tok, Some(potential_key))) => match self.iter.next() {
                None => Some(pending_tok),
                Some(colon @ (Token::Colon, _)) => {
                    self.pending_tok = Some((colon, None));
                    Some((Token::Key(potential_key), pending_tok.1))
                }
                Some(other_tok) => {
                    let potential_key = Self::potential_key(&other_tok.0);
                    self.pending_tok = Some((other_tok, potential_key));
                    Some(pending_tok)
                }
            },
            Some((pending_tok, None)) => Some(pending_tok),
            None => None,
        }
    }
}

trait KeywordThenColonToKeyIteratorAdaptor<'a>: Iterator<Item = RangedToken<'a>> + Sized {
    /// Iterator adapter for mapping keyword-then-colon to key.
    fn keyword_then_colon_to_key(self) -> KeywordThenColonToKey<'a, Self> {
        KeywordThenColonToKey::new(self)
    }
}

impl<'a, I: Iterator<Item = RangedToken<'a>>> KeywordThenColonToKeyIteratorAdaptor<'a> for I {}

/// An iterator adapter which does all of the following:
/// 1. Expand EolThenIndent into separate Eol and Indent tokens
/// 2. Fold consecutive Eol tokens into a single one
/// 3. Drop any initial Eol
/// 4. Optionally, force a final Eol (unless input was empty)
/// 5. Drop tokens for indent followed by colon (Logos deficiency precludes using Logos regexes for this)
struct EolIndentHandler<'a, I> {
    iter: I,
    first_tok: Option<RangedToken<'a>>,
    pending: [Option<RangedToken<'a>>; 2],
    final_eol: Option<Range<usize>>,
    previous_was_eol: bool,
    any: bool,
}

impl<'a, I> EolIndentHandler<'a, I>
where
    I: Iterator<Item = RangedToken<'a>>,
{
    fn new(mut iter: I, final_eol: Option<Range<usize>>) -> Self {
        let mut first_tok = Some((Token::Eol, Range::default()));
        let mut ready = false;

        while !ready {
            while first_tok.as_ref().is_some_and(|tok| tok.0.is_eol()) {
                first_tok = iter.next()
            }
            // handle the colon-at-start-of-file case here
            if first_tok.as_ref().is_some_and(|tok| tok.0 == Token::Colon) {
                first_tok = iter.next();

                while first_tok.as_ref().is_some_and(|tok| !tok.0.is_eol()) {
                    first_tok = iter.next();
                }
            } else {
                ready = true;
            }
        }
        EolIndentHandler {
            iter,
            first_tok,
            pending: [None, None],
            final_eol,
            previous_was_eol: false,
            any: false,
        }
    }

    fn skip_to_end_of_line(&mut self) -> Option<RangedToken<'a>> {
        let mut tok = self.iter.next();
        while tok.as_ref().is_some_and(|tok| !tok.0.is_eol()) {
            tok = self.iter.next();
        }

        tok
    }

    // only get a second token if the first is some kind of EOL, because only then is there anything to merge
    fn get_second_tok_if_required(
        &mut self,
        first_tok: &Option<RangedToken<'a>>,
    ) -> Option<RangedToken<'a>> {
        if first_tok.as_ref().is_some_and(|tok| tok.0.is_eol()) {
            self.iter.next()
        } else {
            None
        }
    }

    fn merge(&mut self) -> Option<RangedToken<'a>> {
        let mut first_tok = self.iter.next();
        let mut second_tok = self.get_second_tok_if_required(&first_tok);

        let mut merging = true;
        while merging {
            match (&mut first_tok, &mut second_tok) {
                (Some(first), Some(second)) if first.0.is_eol() && second.0.is_eol() => {
                    let (tok, span) = second_tok.take().unwrap();
                    let end = if tok == Token::EolComment { span.start } else { span.end };
                    first_tok = Some((tok, first.1.start..end));
                    second_tok = self.iter.next();
                }
                (Some(first), Some(second))
                    if first.0 == Token::EolThenIndent && second.0 == Token::Colon =>
                {
                    first_tok = self.skip_to_end_of_line();
                    second_tok = self.get_second_tok_if_required(&first_tok);
                }
                _ => {
                    merging = false;
                }
            }
        }

        match first_tok {
            Some((Token::EolThenIndent, span)) => {
                self.pending = [Some((Token::Indent, span.end - 1..span.end)), second_tok];
                Some((Token::Eol, span))
            }
            Some(tok) => {
                self.pending = [second_tok, None];
                Some(tok)
            }
            None => None,
        }
    }

    fn next_merged(&mut self) -> Option<RangedToken<'a>> {
        match [self.pending[0].take(), self.pending[1].take()] {
            [Some(first), second] => {
                self.pending = [second, None];
                Some(first)
            }
            _ => self.merge(),
        }
    }
}

impl<'a, I> Iterator for EolIndentHandler<'a, I>
where
    I: Iterator<Item = RangedToken<'a>>,
{
    type Item = RangedToken<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        let next = if self.first_tok.is_some() {
            self.first_tok.take()
        } else {
            self.next_merged()
        };

        match (next, &self.final_eol) {
            (None, Some(span)) => {
                if !self.previous_was_eol {
                    self.previous_was_eol = true;

                    // only force a final Eol if we've emitted anything at all
                    if self.any {
                        Some((Token::Eol, span.clone()))
                    } else {
                        None
                    }
                } else {
                    None
                }
            }
            (None, None) => None,
            (Some(item), _) => {
                let item = if item.0 == Token::EolComment {
                    (Token::Eol, item.1.start..item.1.start)
                } else {
                    item
                };
                if item.0 == Token::Eol {
                    self.previous_was_eol = true;
                }

                self.any = true;
                Some(item)
            }
        }
    }
}

trait EolIndentHandlerIteratorAdaptor<'a>: Iterator<Item = RangedToken<'a>> + Sized {
    /// See comment for `EolIndentHandler`
    fn handle_eol_indent(self, final_eol: Option<Range<usize>>) -> EolIndentHandler<'a, Self> {
        EolIndentHandler::new(self, final_eol)
    }
}

impl<'a, I: Iterator<Item = RangedToken<'a>>> EolIndentHandlerIteratorAdaptor<'a> for I {}

fn parse_date(s: &str) -> Result<Date, LexerError> {
    let mut date = s.split(&['-', '/']);
    let year = date.by_ref().next().unwrap().parse::<i32>().unwrap();
    let month = date.by_ref().next().unwrap().parse::<u8>().unwrap();
    let month = Month::try_from(month).or(Err(LexerError::new("month out of range")))?;
    let day = date.by_ref().next().unwrap().parse::<u8>().unwrap();
    Date::from_calendar_date(year, month, day).or(Err(LexerError::new("date out of range")))
}

fn parse_time(s: &str) -> Result<Time, LexerError> {
    let mut time = s.split(':');
    let hour = time.by_ref().next().unwrap().parse::<u8>().unwrap();
    let min = time.by_ref().next().unwrap().parse::<u8>().unwrap();
    let sec = time
        .by_ref()
        .next()
        .map(|s| s.parse::<u8>().unwrap())
        .unwrap_or(0);

    Time::from_hms(hour, min, sec).or(Err(LexerError::new("time out of range")))
}

// Unescape string literal using the inverse of std::ascii::escape_default
// https://doc.rust-lang.org/std/ascii/fn.escape_default.html
fn unescape_string_literal<'a>(s: &'a str) -> Result<Cow<'a, str>, LexerError> {
    if s.contains('\\') {
        unescape(s)
            .map(Cow::Owned)
            .map_err(|e| LexerError::new(e.to_string()))
    } else {
        Ok(Cow::Borrowed(s))
    }
}

fn parse_number(s: &str) -> Result<Decimal, LexerError> {
    let result = if s.contains(',') {
        let mut without_commas = s.to_string();
        without_commas.retain(|c| c != ',');
        FromStr::from_str(&without_commas)
    } else {
        FromStr::from_str(s)
    };

    result.map_err(|e: <rust_decimal::Decimal as std::str::FromStr>::Err| {
        LexerError::new(e.to_string())
    })
}

#[derive(PartialEq, Eq, Clone, Debug)]
pub struct LexerError {
    message: String,
}

impl LexerError {
    fn new<T: Into<String>>(s: T) -> Self {
        Self { message: s.into() }
    }
}

impl Default for LexerError {
    fn default() -> Self {
        Self::new("unrecognized token")
    }
}

impl Display for LexerError {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.message)
    }
}

impl Error for LexerError {}

impl From<CurrencyError> for LexerError {
    fn from(e: CurrencyError) -> Self {
        Self::new(e.to_string())
    }
}

impl From<FlagLetterError> for LexerError {
    fn from(e: FlagLetterError) -> Self {
        Self::new(e.to_string())
    }
}

impl From<AccountNameError> for LexerError {
    fn from(e: AccountNameError) -> Self {
        Self::new(e.to_string())
    }
}

impl From<AccountTypeNameError> for LexerError {
    fn from(e: AccountTypeNameError) -> Self {
        Self::new(e.to_string())
    }
}

impl From<TagOrLinkIdentifierError> for LexerError {
    fn from(e: TagOrLinkIdentifierError) -> Self {
        Self::new(e.to_string())
    }
}

impl From<KeyError> for LexerError {
    fn from(e: KeyError) -> Self {
        Self::new(e.to_string())
    }
}

impl From<strum::ParseError> for LexerError {
    fn from(e: strum::ParseError) -> Self {
        Self::new(e.to_string())
    }
}

mod tests;