beancount_parser_lima/
parsers.rs

1use crate::{
2    lexer::Token,
3    options::{BeancountOption, BeancountOptionError, ParserOptions},
4    types::*,
5};
6use chumsky::{input::BorrowInput, prelude::*};
7use either::Either;
8use rust_decimal::Decimal;
9use std::{
10    collections::{hash_map, HashMap, HashSet},
11    iter::once,
12    ops::Deref,
13    path::Path,
14};
15use time::Date;
16
17/// Matches all the includes in the file, ignoring everything else.
18pub(crate) fn includes<'s, I>() -> impl Parser<'s, I, Vec<String>, Extra<'s>>
19where
20    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
21{
22    (just(Token::Include).ignore_then(string()).map(Some))
23        .or(any_ref().map(|_| None))
24        .repeated()
25        .collect::<Vec<_>>()
26        .map(|includes| {
27            includes
28                .into_iter()
29                .filter_map(|s| s.as_ref().map(|s| s.to_string()))
30                .collect::<Vec<_>>()
31        })
32}
33
34/// Matches the whole file.
35pub(crate) fn file<'s, I>(
36    source_path: Option<&'s Path>,
37) -> impl Parser<'s, I, Vec<Spanned<Declaration<'s>>>, Extra<'s>>
38where
39    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
40{
41    declaration(source_path).repeated().collect::<Vec<_>>()
42}
43
44/// Matches a [Declaration], and returns with Span.
45pub(crate) fn declaration<'s, I>(
46    source_path: Option<&'s Path>,
47) -> impl Parser<'s, I, Spanned<Declaration<'s>>, Extra<'s>>
48where
49    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
50{
51    use Declaration::*;
52
53    choice((directive().map(Directive), pragma(source_path).map(Pragma)))
54        .map_with(spanned_extra)
55        .recover_with(skip_then_retry_until(any_ref().ignored(), end()))
56}
57
58/// Matches a [Directive].
59pub(crate) fn directive<'s, I>() -> impl Parser<'s, I, Directive<'s>, Extra<'s>>
60where
61    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
62{
63    choice((
64        transaction().labelled("transaction").as_context(),
65        choice((
66            price(),
67            balance(),
68            open(),
69            close(),
70            commodity(),
71            pad(),
72            document(),
73            note(),
74            event(),
75            query(),
76            // TODO custom
77        ))
78        .labelled("directive")
79        .as_context(),
80    ))
81}
82
83/// Matches a [Pragma].
84pub(crate) fn pragma<'s, I>(
85    source_path: Option<&'s Path>,
86) -> impl Parser<'s, I, Pragma<'s>, Extra<'s>>
87where
88    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
89{
90    choice((
91        just(Token::Pushtag)
92            .ignore_then(tag())
93            .map_with(|tag, e| Pragma::Pushtag(spanned(tag, e.span()))),
94        just(Token::Poptag)
95            .ignore_then(tag())
96            .map_with(|tag, e| Pragma::Poptag(spanned(tag, e.span()))),
97        just(Token::Pushmeta)
98            .ignore_then(meta_key_value())
99            .map(Pragma::Pushmeta),
100        just(Token::Popmeta)
101            .ignore_then(key())
102            .then_ignore(just(Token::Colon))
103            .map_with(|key, e| Pragma::Popmeta(spanned(key, e.span()))),
104        just(Token::Include)
105            .ignore_then(string().map_with(|path, e| Pragma::Include(spanned(path, e.span())))),
106        option(source_path).map(Pragma::Option),
107        just(Token::Plugin)
108            .ignore_then(string().map_with(spanned_extra))
109            .then(string().map_with(spanned_extra).or_not())
110            .map(|(module_name, config)| {
111                Pragma::Plugin(Plugin {
112                    module_name,
113                    config,
114                })
115            }),
116    ))
117    .then_ignore(just(Token::Eol))
118    .labelled("directive") // yeah, pragma is not a user-facing concept
119    .as_context()
120}
121
122/// Matches a [BeancountOption], failing if the option cannot be processed.
123pub(crate) fn option<'s, I>(
124    source_path: Option<&'s Path>,
125) -> impl Parser<'s, I, BeancountOption<'s>, Extra<'s>>
126where
127    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
128{
129    just(Token::Option)
130        .ignore_then(string().map_with(|name, e| spanned(name, e.span())))
131        .then(string().map_with(|value, e| spanned(value, e.span())))
132        .try_map_with(move |(name, value), e| {
133            use BeancountOptionError::*;
134
135            let opt = BeancountOption::parse(name, value, source_path).map_err(|e| match e {
136                UnknownOption => Rich::custom(name.span, e.to_string()),
137                BadValue(_) => Rich::custom(value.span, e.to_string()),
138            });
139
140            if let Ok(opt) = opt {
141                let parser_state: &mut extra::SimpleState<ParserState> = e.state();
142                parser_state
143                    .options
144                    .assimilate(opt)
145                    .map_err(|e| Rich::custom(value.span, e.to_string()))
146            } else {
147                opt
148            }
149
150            // TODO
151            // match parser_options.assimilate(&opt.name, &opt.value) {
152            //     Ok(()) => Ok(opt),
153            //     // TODO report location of duplicate option
154            //     Err(ref e @ DuplicateOption(ref _span)) => {
155            //         Err(Rich::custom(name.span, e.to_string()))
156            //     }
157            //     Err(ref e @ UnknownOption) => Err(Rich::custom(name.span, e.to_string())),
158            //     Err(ref e @ BadValue(_)) => Err(Rich::custom(value.span, e.to_string())),
159            //     // TODO report location of duplicate value
160            //     Err(ref e @ DuplicateValue(ref _span)) => {
161            //         Err(Rich::custom(value.span, e.to_string()))
162            //     }
163            // }
164        })
165}
166
167/// Matches a transaction, including metadata and postings, over several lines.
168pub(crate) fn transaction<'s, I>() -> impl Parser<'s, I, Directive<'s>, Extra<'s>>
169where
170    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
171{
172    group((
173        transaction_header_line(),
174        metadata().map_with(spanned_extra),
175        posting().repeated().collect::<Vec<_>>(),
176    ))
177    .validate(
178        |((date, flag, (payee, narration), (tags, links)), mut metadata, postings),
179         _span,
180         emitter| {
181            metadata.merge_tags(&tags, emitter);
182            metadata.merge_links(&links, emitter);
183
184            Directive {
185                date,
186                metadata,
187                variant: DirectiveVariant::Transaction(Transaction {
188                    flag,
189                    payee,
190                    narration,
191                    postings,
192                }),
193            }
194        },
195    )
196}
197
198type TransactionHeaderLine<'s> = (
199    Spanned<Date>,
200    Spanned<Flag>,
201    (Option<Spanned<&'s str>>, Option<Spanned<&'s str>>),
202    (HashSet<Spanned<Tag<'s>>>, HashSet<Spanned<Link<'s>>>),
203);
204
205/// Matches the first line of a transaction.
206fn transaction_header_line<'s, I>() -> impl Parser<'s, I, TransactionHeaderLine<'s>, Extra<'s>>
207where
208    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
209{
210    group((
211        date().map_with(spanned_extra),
212        txn().map_with(spanned_extra),
213        // payee and narration get special handling in case one is omitted
214        group((
215            string().map_with(spanned_extra).or_not(),
216            string().map_with(spanned_extra).or_not(),
217        ))
218        .map(|(s1, s2)| match (s1, s2) {
219            // a single string is narration
220            (Some(s1), None) => (None, Some(s1)),
221            (s1, s2) => (s1, s2),
222        })
223        .map(|(payee, narration)| {
224            (
225                replace_some_empty_with_none(payee),
226                replace_some_empty_with_none(narration),
227            )
228        }),
229        tags_links(),
230    ))
231    .then_ignore(just(Token::Eol))
232}
233
234fn replace_some_empty_with_none(s: Option<Spanned<&str>>) -> Option<Spanned<&str>> {
235    match s {
236        Some(maybe_empty) => {
237            if maybe_empty.is_empty() {
238                None
239            } else {
240                s
241            }
242        }
243        None => None,
244    }
245}
246
247/// Matches a price directive, including metadata, over several lines.
248pub(crate) fn price<'s, I>() -> impl Parser<'s, I, Directive<'s>, Extra<'s>>
249where
250    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
251{
252    group((
253        date().map_with(spanned_extra),
254        just(Token::Price),
255        currency().map_with(spanned_extra),
256        amount().map_with(spanned_extra),
257        tags_links(),
258    ))
259    .then_ignore(just(Token::Eol))
260    .then(metadata().map_with(spanned_extra))
261    .validate(
262        |((date, _, currency, amount, (tags, links)), mut metadata), _span, emitter| {
263            metadata.merge_tags(&tags, emitter);
264            metadata.merge_links(&links, emitter);
265            Directive {
266                date,
267                metadata,
268                variant: DirectiveVariant::Price(Price { currency, amount }),
269            }
270        },
271    )
272    .labelled("price")
273    .as_context()
274}
275
276/// Matches a balance directive, including metadata, over several lines.
277pub(crate) fn balance<'s, I>() -> impl Parser<'s, I, Directive<'s>, Extra<'s>>
278where
279    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
280{
281    group((
282        date().map_with(spanned_extra),
283        just(Token::Balance),
284        account().map_with(spanned_extra),
285        amount_with_tolerance().map_with(spanned_extra),
286        tags_links(),
287    ))
288    .then_ignore(just(Token::Eol))
289    .then(metadata().map_with(spanned_extra))
290    .validate(
291        |((date, _, account, atol, (tags, links)), mut metadata), _span, emitter| {
292            metadata.merge_tags(&tags, emitter);
293            metadata.merge_links(&links, emitter);
294            Directive {
295                date,
296                metadata,
297                variant: DirectiveVariant::Balance(Balance { account, atol }),
298            }
299        },
300    )
301    .labelled("balance")
302    .as_context()
303}
304
305/// Matches a open, including metadata, over several lines.
306pub(crate) fn open<'s, I>() -> impl Parser<'s, I, Directive<'s>, Extra<'s>>
307where
308    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
309{
310    group((open_header_line(), metadata().map_with(spanned_extra))).validate(
311        |((date, account, currencies, booking, (tags, links)), mut metadata), _span, emitter| {
312            metadata.merge_tags(&tags, emitter);
313            metadata.merge_links(&links, emitter);
314
315            Directive {
316                date,
317                metadata,
318                variant: DirectiveVariant::Open(Open {
319                    account,
320                    currencies,
321                    booking,
322                }),
323            }
324        },
325    )
326}
327
328type OpenHeaderLine<'s> = (
329    Spanned<Date>,
330    Spanned<Account<'s>>,
331    HashSet<Spanned<Currency<'s>>>,
332    Option<Spanned<Booking>>,
333    (HashSet<Spanned<Tag<'s>>>, HashSet<Spanned<Link<'s>>>),
334);
335
336/// Matches the first line of a open.
337fn open_header_line<'s, I>() -> impl Parser<'s, I, OpenHeaderLine<'s>, Extra<'s>>
338where
339    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
340{
341    group((
342        date().map_with(spanned_extra),
343        just(Token::Open),
344        account().map_with(spanned_extra),
345        currency_list(),
346        booking().map_with(spanned_extra).or_not(),
347        tags_links(),
348    ))
349    .then_ignore(just(Token::Eol))
350    .map(|(date, _, account, currency, booking, tags_links)| {
351        (date, account, currency, booking, tags_links)
352    })
353}
354
355/// Matches zero or more currencies, comma-separated.
356fn currency_list<'s, I>() -> impl Parser<'s, I, HashSet<Spanned<Currency<'s>>>, Extra<'s>>
357where
358    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
359{
360    group((
361        currency().map_with(spanned_extra),
362        (just(Token::Comma).ignore_then(currency().map_with(spanned_extra)))
363            .repeated()
364            .collect::<Vec<_>>(),
365    ))
366    .validate(|(first_currency, mut currencies), _span, emitter| {
367        currencies.push(first_currency);
368        currencies
369            .into_iter()
370            .fold(HashSet::new(), |mut currencies, currency| {
371                if currencies.contains(&currency) {
372                    emitter.emit(Rich::custom(
373                        currency.span,
374                        format!("duplicate currency {}", currency),
375                    ))
376                } else {
377                    currencies.insert(currency);
378                }
379
380                currencies
381            })
382    })
383    .or_not()
384    .map(|currencies| currencies.unwrap_or_default())
385}
386
387/// Matches a [Account].
388fn account<'s, I>() -> impl Parser<'s, I, Account<'s>, Extra<'s>>
389where
390    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
391{
392    let s = select_ref!(Token::Account(s) => *s);
393
394    s.try_map_with(|s, e| {
395        let span = e.span();
396        //
397        // look up the account type name to see which account type it is currently mapped to
398        let parser_state: &mut extra::SimpleState<ParserState> = e.state();
399        let account_type_names = &parser_state.options.account_type_names;
400
401        Account::new(s, account_type_names).map_err(|e| Rich::custom(span, e.to_string()))
402    })
403}
404
405/// Matches a [Booking].
406fn booking<'s, I>() -> impl Parser<'s, I, Booking, Extra<'s>>
407where
408    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
409{
410    string().try_map(|s, span| Booking::try_from(s).map_err(|e| Rich::custom(span, e.to_string())))
411}
412
413/// Matches a close, including metadata, over several lines.
414pub(crate) fn close<'s, I>() -> impl Parser<'s, I, Directive<'s>, Extra<'s>>
415where
416    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
417{
418    group((
419        date().map_with(spanned_extra),
420        just(Token::Close),
421        account().map_with(spanned_extra),
422        tags_links(),
423    ))
424    .then_ignore(just(Token::Eol))
425    .then(metadata().map_with(spanned_extra))
426    .validate(
427        |((date, _, account, (tags, links)), mut metadata), _span, emitter| {
428            metadata.merge_tags(&tags, emitter);
429            metadata.merge_links(&links, emitter);
430
431            Directive {
432                date,
433                metadata,
434                variant: DirectiveVariant::Close(Close { account }),
435            }
436        },
437    )
438}
439
440/// Matches a commodity, including metadata, over several lines.
441pub(crate) fn commodity<'s, I>() -> impl Parser<'s, I, Directive<'s>, Extra<'s>>
442where
443    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
444{
445    group((
446        date().map_with(spanned_extra),
447        just(Token::Commodity),
448        currency().map_with(spanned_extra),
449        tags_links(),
450    ))
451    .then_ignore(just(Token::Eol))
452    .then(metadata().map_with(spanned_extra))
453    .validate(
454        |((date, _, currency, (tags, links)), mut metadata), _span, emitter| {
455            metadata.merge_tags(&tags, emitter);
456            metadata.merge_links(&links, emitter);
457
458            Directive {
459                date,
460                metadata,
461                variant: DirectiveVariant::Commodity(Commodity { currency }),
462            }
463        },
464    )
465}
466
467/// Matches a pad, including metadata, over several lines.
468pub(crate) fn pad<'s, I>() -> impl Parser<'s, I, Directive<'s>, Extra<'s>>
469where
470    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
471{
472    group((
473        date().map_with(spanned_extra),
474        just(Token::Pad),
475        account().map_with(spanned_extra),
476        account().map_with(spanned_extra),
477        tags_links(),
478    ))
479    .then_ignore(just(Token::Eol))
480    .then(metadata().map_with(spanned_extra))
481    .validate(
482        |((date, _, account, source, (tags, links)), mut metadata), _span, emitter| {
483            metadata.merge_tags(&tags, emitter);
484            metadata.merge_links(&links, emitter);
485
486            Directive {
487                date,
488                metadata,
489                variant: DirectiveVariant::Pad(Pad { account, source }),
490            }
491        },
492    )
493}
494
495/// Matches a document, including metadata, over several lines.
496pub(crate) fn document<'s, I>() -> impl Parser<'s, I, Directive<'s>, Extra<'s>>
497where
498    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
499{
500    group((
501        date().map_with(spanned_extra),
502        just(Token::Document),
503        account().map_with(spanned_extra),
504        string().map_with(spanned_extra),
505        tags_links(),
506    ))
507    .then_ignore(just(Token::Eol))
508    .then(metadata().map_with(spanned_extra))
509    .validate(
510        |((date, _, account, path, (tags, links)), mut metadata), _span, emitter| {
511            metadata.merge_tags(&tags, emitter);
512            metadata.merge_links(&links, emitter);
513
514            Directive {
515                date,
516                metadata,
517                variant: DirectiveVariant::Document(Document { account, path }),
518            }
519        },
520    )
521}
522
523/// Matches a note, including metadata, over several lines.
524pub(crate) fn note<'s, I>() -> impl Parser<'s, I, Directive<'s>, Extra<'s>>
525where
526    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
527{
528    group((
529        date().map_with(spanned_extra),
530        just(Token::Note),
531        account().map_with(spanned_extra),
532        string().map_with(spanned_extra),
533        tags_links(),
534    ))
535    .then_ignore(just(Token::Eol))
536    .then(metadata().map_with(spanned_extra))
537    .validate(
538        |((date, _, account, comment, (tags, links)), mut metadata), _span, emitter| {
539            metadata.merge_tags(&tags, emitter);
540            metadata.merge_links(&links, emitter);
541
542            Directive {
543                date,
544                metadata,
545                variant: DirectiveVariant::Note(Note { account, comment }),
546            }
547        },
548    )
549}
550
551/// Matches an event, including metadata, over several lines.
552pub(crate) fn event<'s, I>() -> impl Parser<'s, I, Directive<'s>, Extra<'s>>
553where
554    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
555{
556    group((
557        date().map_with(spanned_extra),
558        just(Token::Event),
559        string().map_with(spanned_extra),
560        string().map_with(spanned_extra),
561        tags_links(),
562    ))
563    .then_ignore(just(Token::Eol))
564    .then(metadata().map_with(spanned_extra))
565    .validate(
566        |((date, _, event_type, description, (tags, links)), mut metadata), _span, emitter| {
567            metadata.merge_tags(&tags, emitter);
568            metadata.merge_links(&links, emitter);
569
570            Directive {
571                date,
572                metadata,
573                variant: DirectiveVariant::Event(Event {
574                    event_type,
575                    description,
576                }),
577            }
578        },
579    )
580}
581
582/// Matches a query, including metadata, over several lines.
583pub(crate) fn query<'s, I>() -> impl Parser<'s, I, Directive<'s>, Extra<'s>>
584where
585    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
586{
587    group((
588        date().map_with(spanned_extra),
589        just(Token::Query),
590        string().map_with(spanned_extra),
591        string().map_with(spanned_extra),
592        tags_links(),
593    ))
594    .then_ignore(just(Token::Eol))
595    .then(metadata().map_with(spanned_extra))
596    .validate(
597        |((date, _, name, content, (tags, links)), mut metadata), _span, emitter| {
598            metadata.merge_tags(&tags, emitter);
599            metadata.merge_links(&links, emitter);
600
601            Directive {
602                date,
603                metadata,
604                variant: DirectiveVariant::Query(Query { name, content }),
605            }
606        },
607    )
608}
609
610/// Matches the `txn` keyword or a flag.
611pub(crate) fn txn<'s, I>() -> impl Parser<'s, I, Flag, Extra<'s>>
612where
613    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
614{
615    choice((just(Token::Txn).to(Flag::default()), flag()))
616}
617
618/// Matches any flag, dedicated or overloaded
619pub(crate) fn flag<'s, I>() -> impl Parser<'s, I, Flag, Extra<'s>>
620where
621    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
622{
623    let dedicated_flag = select_ref!(Token::DedicatedFlag(flag) => *flag);
624
625    choice((
626        dedicated_flag,
627        just(Token::Asterisk).to(Flag::Asterisk),
628        just(Token::Hash).to(Flag::Hash),
629    ))
630}
631
632/// Matches a [Posting] complete with [Metadata] over several lines.
633fn posting<'s, I>() -> impl Parser<'s, I, Spanned<Posting<'s>>, Extra<'s>>
634where
635    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
636{
637    just(Token::Indent)
638        .ignore_then(
639            group((
640                flag().map_with(spanned_extra).or_not(),
641                account().map_with(spanned_extra),
642                expr_value().map_with(spanned_extra).or_not(),
643                currency().map_with(spanned_extra).or_not(),
644                cost_spec().or_not().map_with(|cost_spec, e| {
645                    cost_spec
646                        .flatten()
647                        .map(|cost_spec| spanned(cost_spec, e.span()))
648                }),
649                price_annotation().or_not().map_with(|price_spec, e| {
650                    price_spec
651                        .flatten()
652                        .map(|price_spec| spanned(price_spec, e.span()))
653                }),
654            ))
655            .map_with(spanned_extra)
656            .then_ignore(just(Token::Eol))
657            .then(metadata().map_with(spanned_extra))
658            .map(
659                |(
660                    Spanned {
661                        item: (flag, account, amount, currency, cost_spec, price_annotation),
662                        span: posting_span_without_metadata,
663                    },
664                    metadata,
665                )| {
666                    spanned(
667                        Posting {
668                            flag,
669                            account,
670                            amount,
671                            currency,
672                            cost_spec,
673                            price_annotation,
674                            metadata,
675                        },
676                        posting_span_without_metadata,
677                    )
678                },
679            ),
680        )
681        .labelled("posting")
682        .as_context()
683}
684
685/// Matches [Metadata], over several lines.
686fn metadata<'s, I>() -> impl Parser<'s, I, Metadata<'s>, Extra<'s>>
687where
688    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
689{
690    use Metadatum::*;
691
692    metadatum_line()
693        .repeated()
694        .collect::<Vec<_>>()
695        .validate(|metadata, _span, emitter| {
696            // collate by type of metadatum
697            metadata
698                .into_iter()
699                .fold(Metadata::default(), |mut m, item| match item {
700                    KeyValue(kv) => {
701                        use hash_map::Entry::*;
702
703                        let MetaKeyValue { key, value } = kv.item;
704
705                        let key_span = key.span;
706                        match m.key_values.entry(key) {
707                            Occupied(entry) => emitter.emit(Rich::custom(
708                                key_span,
709                                format!("duplicate key {}", entry.key()),
710                            )),
711                            Vacant(entry) => {
712                                entry.insert(value);
713                            }
714                        }
715
716                        m
717                    }
718                    Tag(tag) => {
719                        if m.tags.contains(&tag) {
720                            emitter.emit(Rich::custom(tag.span, format!("duplicate tag {}", tag)))
721                        } else {
722                            m.tags.insert(tag);
723                        }
724
725                        m
726                    }
727                    Link(link) => {
728                        if m.links.contains(&link) {
729                            emitter
730                                .emit(Rich::custom(link.span, format!("duplicate link {}", link)))
731                        } else {
732                            m.links.insert(link);
733                        }
734
735                        m
736                    }
737                })
738        })
739}
740
741/// A single instance of [Metadata]
742enum Metadatum<'a> {
743    KeyValue(Spanned<MetaKeyValue<'a>>),
744    Tag(Spanned<Tag<'a>>),
745    Link(Spanned<Link<'a>>),
746}
747
748/// Matches a single Metadatum on a single line.
749fn meta_key_value<'s, I>() -> impl Parser<'s, I, MetaKeyValue<'s>, Extra<'s>>
750where
751    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
752{
753    key()
754        .map_with(spanned_extra)
755        .then(just(Token::Colon).ignore_then(meta_value().map_with(spanned_extra)))
756        .map(|(key, value)| MetaKeyValue { key, value })
757}
758
759/// Matches a single Metadatum on a single line.
760fn metadatum_line<'s, I>() -> impl Parser<'s, I, Metadatum<'s>, Extra<'s>>
761where
762    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
763{
764    use Metadatum::*;
765
766    just(Token::Indent)
767        .ignore_then(
768            choice((
769                meta_key_value().map_with(spanned_extra).map(KeyValue),
770                tag().map_with(spanned_extra).map(Tag),
771                link().map_with(spanned_extra).map(Link),
772            ))
773            .then_ignore(just(Token::Eol)),
774        )
775        .labelled("metadata")
776        .as_context()
777}
778
779/// Matches a [MetaValue].
780pub(crate) fn meta_value<'s, I>() -> impl Parser<'s, I, MetaValue<'s>, Extra<'s>>
781where
782    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
783{
784    use MetaValue::*;
785
786    // try for amount first
787    choice((amount().map(Amount), simple_value().map(Simple)))
788}
789
790/// Matches a [SimpleValue].
791pub(crate) fn simple_value<'s, I>() -> impl Parser<'s, I, SimpleValue<'s>, Extra<'s>>
792where
793    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
794{
795    use SimpleValue::*;
796
797    choice((
798        string().map(String),
799        currency().map(Currency),
800        account().map(Account),
801        tag().map(Tag),
802        link().map(Link),
803        date().map(Date),
804        bool().map(Bool),
805        just(Token::Null).to(None),
806        expr_value().map(Expr),
807        empty().to(None),
808    ))
809}
810
811pub(crate) fn amount<'s, I>() -> impl Parser<'s, I, Amount<'s>, Extra<'s>>
812where
813    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
814{
815    group((
816        expr_value().map_with(spanned_extra),
817        currency().map_with(spanned_extra),
818    ))
819    .map(Amount::new)
820}
821
822pub(crate) fn amount_with_tolerance<'s, I>(
823) -> impl Parser<'s, I, AmountWithTolerance<'s>, Extra<'s>>
824where
825    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
826{
827    choice((
828        amount().map_with(|amount, e| AmountWithTolerance::new((spanned_extra(amount, e), None))),
829        group((
830            expr_value().map_with(spanned_extra),
831            just(Token::Tilde),
832            decimal().map_with(spanned_extra),
833            currency().map_with(spanned_extra),
834        ))
835        .map_with(|(number, _, tolerance, currency), e| {
836            AmountWithTolerance::new((
837                spanned_extra(Amount::new((number, currency)), e),
838                Some(tolerance),
839            ))
840        }),
841    ))
842}
843
844pub(crate) fn loose_amount<'s, I>() -> impl Parser<'s, I, LooseAmount<'s>, Extra<'s>>
845where
846    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
847{
848    group((
849        expr_value().map_with(spanned_extra).or_not(),
850        currency().map_with(spanned_extra).or_not(),
851    ))
852    .map(LooseAmount::new)
853}
854
855pub(crate) fn compound_amount<'s, I>() -> impl Parser<'s, I, CompoundAmount<'s>, Extra<'s>>
856where
857    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
858{
859    use CompoundAmount::*;
860
861    choice((
862        (compound_expr().then(currency())).map(|(amount, cur)| CurrencyAmount(amount, cur)),
863        compound_expr().map(BareAmount),
864        just(Token::Hash) // bare currency may or may not be preceeded by hash
865            .or_not()
866            .ignore_then(currency().map(BareCurrency)),
867    ))
868}
869
870pub(crate) fn compound_expr<'s, I>() -> impl Parser<'s, I, CompoundExprValue, Extra<'s>>
871where
872    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
873{
874    use CompoundExprValue::*;
875
876    choice((
877        // try for both per-unit and total first
878        expr_value()
879            .then_ignore(just(Token::Hash))
880            .then(expr_value())
881            .map(|(per_unit, total)| PerUnitAndTotal(per_unit, total)),
882        expr_value().then_ignore(just(Token::Hash)).map(PerUnit),
883        expr_value().map(PerUnit),
884        just(Token::Hash).ignore_then(expr_value()).map(Total),
885    ))
886}
887
888pub(crate) fn scoped_expr<'s, I>() -> impl Parser<'s, I, ScopedExprValue, Extra<'s>>
889where
890    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
891{
892    use ScopedExprValue::*;
893
894    choice((
895        expr_value().then_ignore(just(Token::Hash)).map(PerUnit),
896        expr_value().map(PerUnit),
897        just(Token::Hash).ignore_then(expr_value()).map(Total),
898    ))
899}
900
901pub(crate) fn price_annotation<'s, I>() -> impl Parser<'s, I, Option<PriceSpec<'s>>, Extra<'s>>
902where
903    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
904{
905    use PriceSpec::*;
906
907    fn scope(amount: ExprValue, is_total: bool) -> ScopedExprValue {
908        use ScopedExprValue::*;
909
910        if is_total {
911            Total(amount)
912        } else {
913            PerUnit(amount)
914        }
915    }
916
917    group((
918        choice((just(Token::At).to(false), just(Token::AtAt).to(true))),
919        expr_value().or_not(),
920        currency().or_not(),
921    ))
922    .try_map(|(is_total, amount, cur), _span| match (amount, cur) {
923        (Some(amount), Some(cur)) => Ok(Some(CurrencyAmount(scope(amount, is_total), cur))),
924        (Some(amount), None) => Ok(Some(BareAmount(scope(amount, is_total)))),
925        (None, Some(cur)) => Ok(Some(BareCurrency(cur))),
926        (None, None) => Ok(None),
927    })
928}
929
930/// Matches a [CostSpec].
931/// For now we only match the new syntax of single braces.
932fn cost_spec<'s, I>() -> impl Parser<'s, I, Option<CostSpec<'s>>, Extra<'s>>
933where
934    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
935{
936    use self::CompoundAmount::*;
937    use CostComp::*;
938
939    just(Token::Lcurl)
940        .ignore_then(
941            group((
942                cost_comp().map_with(spanned_extra),
943                (just(Token::Comma).ignore_then(cost_comp().map_with(spanned_extra)))
944                    .repeated()
945                    .collect::<Vec<_>>(),
946            ))
947            .or_not(), // allow for empty cost spec
948        )
949        .then_ignore(just(Token::Rcurl))
950        .try_map(move |cost_spec, span| match cost_spec {
951            Some((head, tail)) => {
952                once(head)
953                    .chain(tail)
954                    .fold(
955                        // accumulate the `CostComp`s in a `CostSpecBuilder`
956                        CostSpecBuilder::default(),
957                        |builder, cost_comp| match cost_comp.item {
958                            CompoundAmount(compound_amount) => match compound_amount {
959                                BareCurrency(cur) => builder.currency(cur, cost_comp.span),
960                                BareAmount(amount) => builder.compound_expr(amount, cost_comp.span),
961                                CurrencyAmount(amount, cur) => builder
962                                    .compound_expr(amount, cost_comp.span)
963                                    .currency(cur, cost_comp.span),
964                            },
965                            Date(date) => builder.date(date, cost_comp.span),
966                            Label(s) => builder.label(s, cost_comp.span),
967                            Merge => builder.merge(cost_comp.span),
968                        },
969                    )
970                    .build()
971                    .map(Some)
972                    .map_err(|e| Rich::custom(span, e.to_string()))
973            }
974            None => Ok(None),
975        })
976}
977
978#[derive(PartialEq, Eq, Clone, Debug)]
979/// One component of a cost specification.
980/// Setting a field type multiple times is rejected by methods in [CostSpec].
981enum CostComp<'a> {
982    CompoundAmount(CompoundAmount<'a>),
983    Date(Date),
984    Label(&'a str),
985    Merge,
986}
987
988/// Matches one component of a [CostSpec].
989fn cost_comp<'s, I>() -> impl Parser<'s, I, CostComp<'s>, Extra<'s>>
990where
991    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
992{
993    use CostComp::*;
994
995    choice((
996        compound_amount().map(CompoundAmount),
997        date().map(Date),
998        string().map(Label),
999        just(Token::Asterisk).to(Merge),
1000    ))
1001}
1002
1003/// Matches zero or more tags or links.
1004/// Duplicates are errors.
1005pub(crate) fn tags_links<'s, I>(
1006) -> impl Parser<'s, I, (HashSet<Spanned<Tag<'s>>>, HashSet<Spanned<Link<'s>>>), Extra<'s>>
1007where
1008    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
1009{
1010    choice((
1011        tag().map_with(spanned_extra).map(Either::Left),
1012        link().map_with(spanned_extra).map(Either::Right),
1013    ))
1014    .repeated()
1015    .collect::<Vec<_>>()
1016    .validate(|tags_or_links, _span, emitter| {
1017        tags_or_links.into_iter().fold(
1018            (HashSet::new(), HashSet::new()),
1019            |(mut tags, mut links), item| match item {
1020                Either::Left(tag) => {
1021                    if tags.contains(&tag) {
1022                        emitter.emit(Rich::custom(tag.span, format!("duplicate tag {}", tag)))
1023                    } else {
1024                        tags.insert(tag);
1025                    }
1026
1027                    (tags, links)
1028                }
1029                Either::Right(link) => {
1030                    if links.contains(&link) {
1031                        emitter.emit(Rich::custom(link.span, format!("duplicate link {}", link)))
1032                    } else {
1033                        links.insert(link);
1034                    }
1035
1036                    (tags, links)
1037                }
1038            },
1039        )
1040    })
1041}
1042
1043/// Matches a bool
1044pub(crate) fn bool<'s, I>() -> impl Parser<'s, I, bool, Extra<'s>>
1045where
1046    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
1047{
1048    choice((just(Token::True).to(true), just(Token::False).to(false)))
1049}
1050
1051/// Match and evaluate an expression
1052pub(crate) fn expr_value<'s, I>() -> impl Parser<'s, I, ExprValue, Extra<'s>>
1053where
1054    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
1055{
1056    expr().map(ExprValue::from)
1057}
1058
1059/// Match an expression
1060pub(crate) fn expr<'s, I>() -> impl Parser<'s, I, Expr, Extra<'s>>
1061where
1062    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
1063{
1064    use Token::*;
1065
1066    recursive(|expr| {
1067        // Match a parenthesized expression
1068        let parens = expr
1069            .clone()
1070            .delimited_by(just(Lparen), just(Rparen))
1071            .map(|x| Expr::Paren(Box::new(x)));
1072
1073        // Match a bare number
1074        let number = select_ref! { Number(x) => Expr::Value(*x) };
1075
1076        // Match a factor of an expression
1077        let factor = choice((just(Minus), just(Plus)))
1078            .or_not()
1079            .then(number.or(parens.clone()))
1080            .map(|(negated, x)| {
1081                if negated.is_some_and(|tok| tok == Minus) {
1082                    Expr::Neg(Box::new(x))
1083                } else {
1084                    x
1085                }
1086            });
1087
1088        // Match a product of factors
1089        let product = factor.clone().foldl(
1090            choice((
1091                just(Asterisk).to(Expr::Mul as fn(_, _) -> _),
1092                just(Slash).to(Expr::Div as fn(_, _) -> _),
1093            ))
1094            .then(factor.clone())
1095            .repeated(),
1096            |lhs, (op, rhs)| op(Box::new(lhs), Box::new(rhs)),
1097        );
1098
1099        // Match an expression
1100        product.clone().foldl(
1101            choice((
1102                just(Plus).to(Expr::Add as fn(_, _) -> _),
1103                just(Minus).to(Expr::Sub as fn(_, _) -> _),
1104            ))
1105            .then(product.clone())
1106            .repeated(),
1107            |lhs, (op, rhs)| op(Box::new(lhs), Box::new(rhs)),
1108        )
1109    })
1110}
1111
1112/// Matches a Tag
1113fn tag<'s, I>() -> impl Parser<'s, I, Tag<'s>, Extra<'s>>
1114where
1115    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
1116{
1117    let tag = select_ref!(Token::Tag(s) => *s);
1118    tag.try_map(|s, span| {
1119        TagOrLinkIdentifier::try_from(s)
1120            .map(Tag)
1121            .map_err(|e| Rich::custom(span, e.to_string()))
1122    })
1123}
1124
1125/// Matches a Link
1126fn link<'s, I>() -> impl Parser<'s, I, Link<'s>, Extra<'s>>
1127where
1128    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
1129{
1130    let link = select_ref!(Token::Link(s) => *s);
1131    link.try_map(|s, span| {
1132        TagOrLinkIdentifier::try_from(s)
1133            .map(Link)
1134            .map_err(|e| Rich::custom(span, e.to_string()))
1135    })
1136}
1137
1138/// Matches a Key.
1139/// Note that we may have to hijack another token and use it as a key,
1140/// since keywords do get used as metadata keys.
1141fn key<'s, I>() -> impl Parser<'s, I, Key<'s>, Extra<'s>>
1142where
1143    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
1144{
1145    let key = select_ref!(Token::Key(s) => *s);
1146
1147    key.try_map(|s, span| Key::try_from(s).map_err(|e| Rich::custom(span, e.to_string())))
1148}
1149
1150/// Matches a Currency
1151fn currency<'s, I>() -> impl Parser<'s, I, Currency<'s>, Extra<'s>>
1152where
1153    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
1154{
1155    let currency = select_ref!(Token::Currency(s) => *s);
1156    currency.try_map(|s, span| Currency::try_from(s).map_err(|e| Rich::custom(span, e.to_string())))
1157}
1158
1159/// Matches a Date
1160fn date<'s, I>() -> impl Parser<'s, I, Date, Extra<'s>>
1161where
1162    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
1163{
1164    select_ref!(Token::Date(date) => *date)
1165}
1166
1167/// Matches a Decimal
1168fn decimal<'s, I>() -> impl Parser<'s, I, Decimal, Extra<'s>>
1169where
1170    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
1171{
1172    select_ref!(Token::Number(x) => *x)
1173}
1174
1175/// Matches a string
1176fn string<'s, I>() -> impl Parser<'s, I, &'s str, Extra<'s>>
1177where
1178    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
1179{
1180    let string = select_ref!(Token::StringLiteral(s) => s.deref());
1181
1182    string.map_with(|s, e| {
1183        let span = e.span();
1184        let simple_state: &mut extra::SimpleState<ParserState> = e.state();
1185        let parser_state: &mut ParserState = simple_state;
1186        let ParserState { warnings, options } = parser_state;
1187        let line_count = s.chars().filter(|c| *c == '\n').count() + 1;
1188        if line_count > options.long_string_maxlines.item {
1189            let option_span = options.long_string_maxlines.source.map(|s| s.value);
1190            let is_default = option_span.is_none();
1191            let warning = Warning::new(
1192                "string too long",
1193                format!(
1194                    "exceeds long_string_maxlines({}{}) - hint: would require option \"long_string_maxlines\" \"{}\"",
1195                    if is_default { "default " } else { "" },
1196                    options.long_string_maxlines.item,
1197                    line_count
1198                ),
1199                span,
1200            );
1201
1202            if let Some(option_span) = option_span {
1203                warnings.push(warning.related_to_named_span("max allowed", option_span));
1204            } else {
1205                warnings.push(warning)
1206            }
1207        }
1208        s
1209    })
1210}
1211
1212impl<'a> Metadata<'a> {
1213    pub(crate) fn merge_tags<E>(&mut self, tags: &HashSet<Spanned<Tag<'a>>>, emitter: &mut E)
1214    where
1215        E: Emit<ParserError<'a>>,
1216    {
1217        for tag in tags {
1218            match self.tags.get(tag) {
1219                None => {
1220                    self.tags.insert(*tag);
1221                }
1222                Some(existing_tag) => {
1223                    let error = Rich::custom(existing_tag.span, format!("duplicate tag {}", tag));
1224                    // TODO: label the error in context, type annotations need fixing for chumsky 1.0.0-alpha7 to alpha8 transition
1225                    // LabelError::<
1226                    //     chumsky::input::WithContext<
1227                    //         Span,
1228                    //         chumsky::input::SpannedInput<Token<'_>, Span, &[(Token<'_>, Span)]>,
1229                    //     >,
1230                    //     &str,
1231                    // >::in_context(&mut error, "tag", tag.span);
1232                    emitter.emit(error);
1233                }
1234            }
1235        }
1236    }
1237
1238    // Augment only for tags which are not already present, others silently ignored.
1239    // This is so that tags attached to directives take precedence over the push stack.
1240    pub(crate) fn augment_tags(&mut self, tags: &HashMap<Spanned<Tag<'a>>, Vec<Spanned<Tag<'a>>>>) {
1241        for (tag, spans) in tags.iter() {
1242            if !self.tags.contains(tag) {
1243                let most_recently_pushed_tag = spans.last().unwrap_or(tag);
1244                self.tags.insert(*most_recently_pushed_tag);
1245            }
1246        }
1247    }
1248
1249    pub(crate) fn merge_links<E>(&mut self, links: &HashSet<Spanned<Link<'a>>>, emitter: &mut E)
1250    where
1251        E: Emit<ParserError<'a>>,
1252    {
1253        for link in links {
1254            match self.links.get(link) {
1255                None => {
1256                    self.links.insert(*link);
1257                }
1258                Some(existing_link) => {
1259                    let error =
1260                        Rich::custom(existing_link.span, format!("duplicate link {}", link));
1261                    // TODO: label the error in context, type annotations need fixing for chumsky 1.0.0-alpha7 to alpha8 transition
1262                    // LabelError::<
1263                    //     chumsky::input::WithContext<
1264                    //         Span,
1265                    //         chumsky::input::SpannedInput<Token<'_>, Span, &[(Token<'_>, Span)]>,
1266                    //     >,
1267                    //     &str,
1268                    // >::in_context(&mut error, "link", link.span);
1269                    emitter.emit(error);
1270                }
1271            }
1272        }
1273    }
1274
1275    // Augment only for keys which are not already present, others silently ignored.
1276    // This is so that key/values attached to directives take precedence over the push stack.
1277    pub(crate) fn augment_key_values(
1278        &mut self,
1279        key_values: &HashMap<Spanned<Key<'a>>, Vec<(Span, Spanned<MetaValue<'a>>)>>,
1280    ) {
1281        for (key, values) in key_values {
1282            if !self.key_values.contains_key(key) {
1283                let (key_span, value) = values.last().unwrap();
1284                self.key_values.insert(
1285                    spanned(*key.item(), *key_span),
1286                    // Sadly we do have to clone the value here, so we can
1287                    // merge in metadata key/values from the push/pop stack
1288                    // without consuming it.
1289                    value.clone(),
1290                );
1291            }
1292        }
1293    }
1294}
1295
1296type ParserError<'a> = Rich<'a, Token<'a>, Span>;
1297
1298impl From<ParserError<'_>> for Error {
1299    fn from(error: ParserError) -> Self {
1300        let error = error.map_token(|tok| tok.to_string());
1301
1302        Error::with_contexts(
1303            error.to_string(),
1304            error.reason().to_string(),
1305            *error.span(),
1306            error
1307                .contexts()
1308                .map(|(label, span)| (label.to_string(), *span))
1309                .collect(),
1310        )
1311    }
1312}
1313
1314// the state we thread through the parsers
1315#[derive(Default, Debug)]
1316pub(crate) struct ParserState<'a> {
1317    pub(crate) options: ParserOptions<'a>,
1318    pub(crate) warnings: Vec<Warning>,
1319}
1320
1321// our ParserExtra with our error and state types
1322pub(crate) type Extra<'a> = extra::Full<ParserError<'a>, extra::SimpleState<ParserState<'a>>, ()>;
1323
1324/// Enable use of own functions which emit errors
1325pub(crate) trait Emit<E> {
1326    fn emit(&mut self, err: E);
1327}
1328
1329impl<E> Emit<E> for chumsky::input::Emitter<E> {
1330    fn emit(&mut self, err: E) {
1331        self.emit(err)
1332    }
1333}
1334
1335// simple collection of errors in a Vec
1336impl<E> Emit<E> for Vec<Error>
1337where
1338    E: Into<Error>,
1339{
1340    fn emit(&mut self, err: E) {
1341        self.push(err.into())
1342    }
1343}
1344// a degenerate error sink
1345struct NullEmitter;
1346
1347impl<E> Emit<E> for NullEmitter {
1348    fn emit(&mut self, _err: E) {}
1349}
1350
1351mod tests;