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.map(|cost_spec| spanned(cost_spec, e.span()))
646                }),
647                price_annotation().or_not().map_with(|price_spec, e| {
648                    price_spec.map(|price_spec| spanned(price_spec, e.span()))
649                }),
650            ))
651            .map_with(spanned_extra)
652            .then_ignore(just(Token::Eol))
653            .then(metadata().map_with(spanned_extra))
654            .map(
655                |(
656                    Spanned {
657                        item: (flag, account, amount, currency, cost_spec, price_annotation),
658                        span: posting_span_without_metadata,
659                    },
660                    metadata,
661                )| {
662                    spanned(
663                        Posting {
664                            flag,
665                            account,
666                            amount,
667                            currency,
668                            cost_spec,
669                            price_annotation,
670                            metadata,
671                        },
672                        posting_span_without_metadata,
673                    )
674                },
675            ),
676        )
677        .labelled("posting")
678        .as_context()
679}
680
681/// Matches [Metadata], over several lines.
682fn metadata<'s, I>() -> impl Parser<'s, I, Metadata<'s>, Extra<'s>>
683where
684    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
685{
686    use Metadatum::*;
687
688    metadatum_line()
689        .repeated()
690        .collect::<Vec<_>>()
691        .validate(|metadata, _span, emitter| {
692            // collate by type of metadatum
693            metadata
694                .into_iter()
695                .fold(Metadata::default(), |mut m, item| match item {
696                    KeyValue(kv) => {
697                        use hash_map::Entry::*;
698
699                        let MetaKeyValue { key, value } = kv.item;
700
701                        let key_span = key.span;
702                        match m.key_values.entry(key) {
703                            Occupied(entry) => emitter.emit(Rich::custom(
704                                key_span,
705                                format!("duplicate key {}", entry.key()),
706                            )),
707                            Vacant(entry) => {
708                                entry.insert(value);
709                            }
710                        }
711
712                        m
713                    }
714                    Tag(tag) => {
715                        if m.tags.contains(&tag) {
716                            emitter.emit(Rich::custom(tag.span, format!("duplicate tag {}", tag)))
717                        } else {
718                            m.tags.insert(tag);
719                        }
720
721                        m
722                    }
723                    Link(link) => {
724                        if m.links.contains(&link) {
725                            emitter
726                                .emit(Rich::custom(link.span, format!("duplicate link {}", link)))
727                        } else {
728                            m.links.insert(link);
729                        }
730
731                        m
732                    }
733                })
734        })
735}
736
737/// A single instance of [Metadata]
738enum Metadatum<'a> {
739    KeyValue(Spanned<MetaKeyValue<'a>>),
740    Tag(Spanned<Tag<'a>>),
741    Link(Spanned<Link<'a>>),
742}
743
744/// Matches a single Metadatum on a single line.
745fn meta_key_value<'s, I>() -> impl Parser<'s, I, MetaKeyValue<'s>, Extra<'s>>
746where
747    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
748{
749    key()
750        .map_with(spanned_extra)
751        .then(just(Token::Colon).ignore_then(meta_value().map_with(spanned_extra)))
752        .map(|(key, value)| MetaKeyValue { key, value })
753}
754
755/// Matches a single Metadatum on a single line.
756fn metadatum_line<'s, I>() -> impl Parser<'s, I, Metadatum<'s>, Extra<'s>>
757where
758    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
759{
760    use Metadatum::*;
761
762    just(Token::Indent)
763        .ignore_then(
764            choice((
765                meta_key_value().map_with(spanned_extra).map(KeyValue),
766                tag().map_with(spanned_extra).map(Tag),
767                link().map_with(spanned_extra).map(Link),
768            ))
769            .then_ignore(just(Token::Eol)),
770        )
771        .labelled("metadata")
772        .as_context()
773}
774
775/// Matches a [MetaValue].
776pub(crate) fn meta_value<'s, I>() -> impl Parser<'s, I, MetaValue<'s>, Extra<'s>>
777where
778    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
779{
780    use MetaValue::*;
781
782    // try for amount first
783    choice((amount().map(Amount), simple_value().map(Simple)))
784}
785
786/// Matches a [SimpleValue].
787pub(crate) fn simple_value<'s, I>() -> impl Parser<'s, I, SimpleValue<'s>, Extra<'s>>
788where
789    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
790{
791    use SimpleValue::*;
792
793    choice((
794        string().map(String),
795        currency().map(Currency),
796        account().map(Account),
797        tag().map(Tag),
798        link().map(Link),
799        date().map(Date),
800        bool().map(Bool),
801        just(Token::Null).to(None),
802        expr_value().map(Expr),
803        empty().to(None),
804    ))
805}
806
807pub(crate) fn amount<'s, I>() -> impl Parser<'s, I, Amount<'s>, Extra<'s>>
808where
809    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
810{
811    group((
812        expr_value().map_with(spanned_extra),
813        currency().map_with(spanned_extra),
814    ))
815    .map(Amount::new)
816}
817
818pub(crate) fn amount_with_tolerance<'s, I>(
819) -> impl Parser<'s, I, AmountWithTolerance<'s>, Extra<'s>>
820where
821    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
822{
823    choice((
824        amount().map_with(|amount, e| AmountWithTolerance::new((spanned_extra(amount, e), None))),
825        group((
826            expr_value().map_with(spanned_extra),
827            just(Token::Tilde),
828            decimal().map_with(spanned_extra),
829            currency().map_with(spanned_extra),
830        ))
831        .map_with(|(number, _, tolerance, currency), e| {
832            AmountWithTolerance::new((
833                spanned_extra(Amount::new((number, currency)), e),
834                Some(tolerance),
835            ))
836        }),
837    ))
838}
839
840pub(crate) fn loose_amount<'s, I>() -> impl Parser<'s, I, LooseAmount<'s>, Extra<'s>>
841where
842    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
843{
844    group((
845        expr_value().map_with(spanned_extra).or_not(),
846        currency().map_with(spanned_extra).or_not(),
847    ))
848    .map(LooseAmount::new)
849}
850
851pub(crate) fn compound_amount<'s, I>() -> impl Parser<'s, I, CompoundAmount<'s>, Extra<'s>>
852where
853    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
854{
855    use CompoundAmount::*;
856
857    choice((
858        (compound_expr().then(currency())).map(|(amount, cur)| CurrencyAmount(amount, cur)),
859        compound_expr().map(BareAmount),
860        just(Token::Hash) // bare currency may or may not be preceeded by hash
861            .or_not()
862            .ignore_then(currency().map(BareCurrency)),
863    ))
864}
865
866pub(crate) fn compound_expr<'s, I>() -> impl Parser<'s, I, CompoundExprValue, Extra<'s>>
867where
868    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
869{
870    use CompoundExprValue::*;
871
872    choice((
873        // try for both per-unit and total first
874        expr_value()
875            .then_ignore(just(Token::Hash))
876            .then(expr_value())
877            .map(|(per_unit, total)| PerUnitAndTotal(per_unit, total)),
878        expr_value().then_ignore(just(Token::Hash)).map(PerUnit),
879        expr_value().map(PerUnit),
880        just(Token::Hash).ignore_then(expr_value()).map(Total),
881    ))
882}
883
884pub(crate) fn scoped_expr<'s, I>() -> impl Parser<'s, I, ScopedExprValue, Extra<'s>>
885where
886    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
887{
888    use ScopedExprValue::*;
889
890    choice((
891        expr_value().then_ignore(just(Token::Hash)).map(PerUnit),
892        expr_value().map(PerUnit),
893        just(Token::Hash).ignore_then(expr_value()).map(Total),
894    ))
895}
896
897pub(crate) fn price_annotation<'s, I>() -> impl Parser<'s, I, PriceSpec<'s>, Extra<'s>>
898where
899    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
900{
901    use PriceSpec::*;
902
903    fn scope(amount: ExprValue, is_total: bool) -> ScopedExprValue {
904        use ScopedExprValue::*;
905
906        if is_total {
907            Total(amount)
908        } else {
909            PerUnit(amount)
910        }
911    }
912
913    group((
914        choice((just(Token::At).to(false), just(Token::AtAt).to(true))),
915        expr_value().or_not(),
916        currency().or_not(),
917    ))
918    .try_map(|(is_total, amount, cur), _span| match (amount, cur) {
919        (Some(amount), Some(cur)) => Ok(CurrencyAmount(scope(amount, is_total), cur)),
920        (Some(amount), None) => Ok(BareAmount(scope(amount, is_total))),
921        (None, Some(cur)) => Ok(BareCurrency(cur)),
922        (None, None) => Ok(Unspecified),
923    })
924}
925
926/// Matches a [CostSpec].
927/// For now we only match the new syntax of single braces.
928fn cost_spec<'s, I>() -> impl Parser<'s, I, CostSpec<'s>, Extra<'s>>
929where
930    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
931{
932    use self::CompoundAmount::*;
933    use CostComp::*;
934
935    just(Token::Lcurl)
936        .ignore_then(
937            group((
938                cost_comp().map_with(spanned_extra),
939                (just(Token::Comma).ignore_then(cost_comp().map_with(spanned_extra)))
940                    .repeated()
941                    .collect::<Vec<_>>(),
942            ))
943            .or_not(), // allow for empty cost spec
944        )
945        .then_ignore(just(Token::Rcurl))
946        .try_map(move |cost_spec, span| {
947            let mut builder = match cost_spec {
948                Some((head, tail)) => {
949                    once(head).chain(tail).fold(
950                        // accumulate the `CostComp`s in a `CostSpecBuilder`
951                        CostSpecBuilder::default(),
952                        |builder, cost_comp| match cost_comp.item {
953                            CompoundAmount(compound_amount) => match compound_amount {
954                                BareCurrency(cur) => builder.currency(cur, cost_comp.span),
955                                BareAmount(amount) => builder.compound_expr(amount, cost_comp.span),
956                                CurrencyAmount(amount, cur) => builder
957                                    .compound_expr(amount, cost_comp.span)
958                                    .currency(cur, cost_comp.span),
959                            },
960                            Date(date) => builder.date(date, cost_comp.span),
961                            Label(s) => builder.label(s, cost_comp.span),
962                            Merge => builder.merge(cost_comp.span),
963                        },
964                    )
965                }
966                None => CostSpecBuilder::default(),
967            };
968            builder
969                .build()
970                .map_err(|e| Rich::custom(span, e.to_string()))
971        })
972}
973
974#[derive(PartialEq, Eq, Clone, Debug)]
975/// One component of a cost specification.
976/// Setting a field type multiple times is rejected by methods in [CostSpec].
977enum CostComp<'a> {
978    CompoundAmount(CompoundAmount<'a>),
979    Date(Date),
980    Label(&'a str),
981    Merge,
982}
983
984/// Matches one component of a [CostSpec].
985fn cost_comp<'s, I>() -> impl Parser<'s, I, CostComp<'s>, Extra<'s>>
986where
987    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
988{
989    use CostComp::*;
990
991    choice((
992        compound_amount().map(CompoundAmount),
993        date().map(Date),
994        string().map(Label),
995        just(Token::Asterisk).to(Merge),
996    ))
997}
998
999/// Matches zero or more tags or links.
1000/// Duplicates are errors.
1001pub(crate) fn tags_links<'s, I>(
1002) -> impl Parser<'s, I, (HashSet<Spanned<Tag<'s>>>, HashSet<Spanned<Link<'s>>>), Extra<'s>>
1003where
1004    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
1005{
1006    choice((
1007        tag().map_with(spanned_extra).map(Either::Left),
1008        link().map_with(spanned_extra).map(Either::Right),
1009    ))
1010    .repeated()
1011    .collect::<Vec<_>>()
1012    .validate(|tags_or_links, _span, emitter| {
1013        tags_or_links.into_iter().fold(
1014            (HashSet::new(), HashSet::new()),
1015            |(mut tags, mut links), item| match item {
1016                Either::Left(tag) => {
1017                    if tags.contains(&tag) {
1018                        emitter.emit(Rich::custom(tag.span, format!("duplicate tag {}", tag)))
1019                    } else {
1020                        tags.insert(tag);
1021                    }
1022
1023                    (tags, links)
1024                }
1025                Either::Right(link) => {
1026                    if links.contains(&link) {
1027                        emitter.emit(Rich::custom(link.span, format!("duplicate link {}", link)))
1028                    } else {
1029                        links.insert(link);
1030                    }
1031
1032                    (tags, links)
1033                }
1034            },
1035        )
1036    })
1037}
1038
1039/// Matches a bool
1040pub(crate) fn bool<'s, I>() -> impl Parser<'s, I, bool, Extra<'s>>
1041where
1042    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
1043{
1044    choice((just(Token::True).to(true), just(Token::False).to(false)))
1045}
1046
1047/// Match and evaluate an expression
1048pub(crate) fn expr_value<'s, I>() -> impl Parser<'s, I, ExprValue, Extra<'s>>
1049where
1050    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
1051{
1052    expr().map(ExprValue::from)
1053}
1054
1055/// Match an expression
1056pub(crate) fn expr<'s, I>() -> impl Parser<'s, I, Expr, Extra<'s>>
1057where
1058    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
1059{
1060    use Token::*;
1061
1062    recursive(|expr| {
1063        // Match a parenthesized expression
1064        let parens = expr
1065            .clone()
1066            .delimited_by(just(Lparen), just(Rparen))
1067            .map(|x| Expr::Paren(Box::new(x)));
1068
1069        // Match a bare number
1070        let number = select_ref! { Number(x) => Expr::Value(*x) };
1071
1072        // Match a factor of an expression
1073        let factor = choice((just(Minus), just(Plus)))
1074            .or_not()
1075            .then(number.or(parens.clone()))
1076            .map(|(negated, x)| {
1077                if negated.is_some_and(|tok| tok == Minus) {
1078                    Expr::Neg(Box::new(x))
1079                } else {
1080                    x
1081                }
1082            });
1083
1084        // Match a product of factors
1085        let product = factor.clone().foldl(
1086            choice((
1087                just(Asterisk).to(Expr::Mul as fn(_, _) -> _),
1088                just(Slash).to(Expr::Div as fn(_, _) -> _),
1089            ))
1090            .then(factor.clone())
1091            .repeated(),
1092            |lhs, (op, rhs)| op(Box::new(lhs), Box::new(rhs)),
1093        );
1094
1095        // Match an expression
1096        product.clone().foldl(
1097            choice((
1098                just(Plus).to(Expr::Add as fn(_, _) -> _),
1099                just(Minus).to(Expr::Sub as fn(_, _) -> _),
1100            ))
1101            .then(product.clone())
1102            .repeated(),
1103            |lhs, (op, rhs)| op(Box::new(lhs), Box::new(rhs)),
1104        )
1105    })
1106}
1107
1108/// Matches a Tag
1109fn tag<'s, I>() -> impl Parser<'s, I, Tag<'s>, Extra<'s>>
1110where
1111    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
1112{
1113    let tag = select_ref!(Token::Tag(s) => *s);
1114    tag.try_map(|s, span| {
1115        TagOrLinkIdentifier::try_from(s)
1116            .map(Tag)
1117            .map_err(|e| Rich::custom(span, e.to_string()))
1118    })
1119}
1120
1121/// Matches a Link
1122fn link<'s, I>() -> impl Parser<'s, I, Link<'s>, Extra<'s>>
1123where
1124    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
1125{
1126    let link = select_ref!(Token::Link(s) => *s);
1127    link.try_map(|s, span| {
1128        TagOrLinkIdentifier::try_from(s)
1129            .map(Link)
1130            .map_err(|e| Rich::custom(span, e.to_string()))
1131    })
1132}
1133
1134/// Matches a Key.
1135/// Note that we may have to hijack another token and use it as a key,
1136/// since keywords do get used as metadata keys.
1137fn key<'s, I>() -> impl Parser<'s, I, Key<'s>, Extra<'s>>
1138where
1139    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
1140{
1141    let key = select_ref!(Token::Key(s) => *s);
1142
1143    key.try_map(|s, span| Key::try_from(s).map_err(|e| Rich::custom(span, e.to_string())))
1144}
1145
1146/// Matches a Currency
1147fn currency<'s, I>() -> impl Parser<'s, I, Currency<'s>, Extra<'s>>
1148where
1149    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
1150{
1151    let currency = select_ref!(Token::Currency(s) => *s);
1152    currency.try_map(|s, span| Currency::try_from(s).map_err(|e| Rich::custom(span, e.to_string())))
1153}
1154
1155/// Matches a Date
1156fn date<'s, I>() -> impl Parser<'s, I, Date, Extra<'s>>
1157where
1158    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
1159{
1160    select_ref!(Token::Date(date) => *date)
1161}
1162
1163/// Matches a Decimal
1164fn decimal<'s, I>() -> impl Parser<'s, I, Decimal, Extra<'s>>
1165where
1166    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
1167{
1168    select_ref!(Token::Number(x) => *x)
1169}
1170
1171/// Matches a string
1172fn string<'s, I>() -> impl Parser<'s, I, &'s str, Extra<'s>>
1173where
1174    I: BorrowInput<'s, Token = Token<'s>, Span = Span>,
1175{
1176    let string = select_ref!(Token::StringLiteral(s) => s.deref());
1177
1178    string.map_with(|s, e| {
1179        let span = e.span();
1180        let simple_state: &mut extra::SimpleState<ParserState> = e.state();
1181        let parser_state: &mut ParserState = simple_state;
1182        let ParserState { warnings, options } = parser_state;
1183        let line_count = s.chars().filter(|c| *c == '\n').count() + 1;
1184        if line_count > options.long_string_maxlines.item {
1185            let option_span = options.long_string_maxlines.source.map(|s| s.value);
1186            let is_default = option_span.is_none();
1187            let warning = Warning::new(
1188                "string too long",
1189                format!(
1190                    "exceeds long_string_maxlines({}{}) - hint: would require option \"long_string_maxlines\" \"{}\"",
1191                    if is_default { "default " } else { "" },
1192                    options.long_string_maxlines.item,
1193                    line_count
1194                ),
1195                span,
1196            );
1197
1198            if let Some(option_span) = option_span {
1199                warnings.push(warning.related_to_named_span("max allowed", option_span));
1200            } else {
1201                warnings.push(warning)
1202            }
1203        }
1204        s
1205    })
1206}
1207
1208impl<'a> Metadata<'a> {
1209    pub(crate) fn merge_tags<E>(&mut self, tags: &HashSet<Spanned<Tag<'a>>>, emitter: &mut E)
1210    where
1211        E: Emit<ParserError<'a>>,
1212    {
1213        for tag in tags {
1214            match self.tags.get(tag) {
1215                None => {
1216                    self.tags.insert(*tag);
1217                }
1218                Some(existing_tag) => {
1219                    let error = Rich::custom(existing_tag.span, format!("duplicate tag {}", tag));
1220                    // TODO: label the error in context, type annotations need fixing for chumsky 1.0.0-alpha7 to alpha8 transition
1221                    // LabelError::<
1222                    //     chumsky::input::WithContext<
1223                    //         Span,
1224                    //         chumsky::input::SpannedInput<Token<'_>, Span, &[(Token<'_>, Span)]>,
1225                    //     >,
1226                    //     &str,
1227                    // >::in_context(&mut error, "tag", tag.span);
1228                    emitter.emit(error);
1229                }
1230            }
1231        }
1232    }
1233
1234    // Augment only for tags which are not already present, others silently ignored.
1235    // This is so that tags attached to directives take precedence over the push stack.
1236    pub(crate) fn augment_tags(&mut self, tags: &HashMap<Spanned<Tag<'a>>, Vec<Spanned<Tag<'a>>>>) {
1237        for (tag, spans) in tags.iter() {
1238            if !self.tags.contains(tag) {
1239                let most_recently_pushed_tag = spans.last().unwrap_or(tag);
1240                self.tags.insert(*most_recently_pushed_tag);
1241            }
1242        }
1243    }
1244
1245    pub(crate) fn merge_links<E>(&mut self, links: &HashSet<Spanned<Link<'a>>>, emitter: &mut E)
1246    where
1247        E: Emit<ParserError<'a>>,
1248    {
1249        for link in links {
1250            match self.links.get(link) {
1251                None => {
1252                    self.links.insert(*link);
1253                }
1254                Some(existing_link) => {
1255                    let error =
1256                        Rich::custom(existing_link.span, format!("duplicate link {}", link));
1257                    // TODO: label the error in context, type annotations need fixing for chumsky 1.0.0-alpha7 to alpha8 transition
1258                    // LabelError::<
1259                    //     chumsky::input::WithContext<
1260                    //         Span,
1261                    //         chumsky::input::SpannedInput<Token<'_>, Span, &[(Token<'_>, Span)]>,
1262                    //     >,
1263                    //     &str,
1264                    // >::in_context(&mut error, "link", link.span);
1265                    emitter.emit(error);
1266                }
1267            }
1268        }
1269    }
1270
1271    // Augment only for keys which are not already present, others silently ignored.
1272    // This is so that key/values attached to directives take precedence over the push stack.
1273    pub(crate) fn augment_key_values(
1274        &mut self,
1275        key_values: &HashMap<Spanned<Key<'a>>, Vec<(Span, Spanned<MetaValue<'a>>)>>,
1276    ) {
1277        for (key, values) in key_values {
1278            if !self.key_values.contains_key(key) {
1279                let (key_span, value) = values.last().unwrap();
1280                self.key_values.insert(
1281                    spanned(*key.item(), *key_span),
1282                    // Sadly we do have to clone the value here, so we can
1283                    // merge in metadata key/values from the push/pop stack
1284                    // without consuming it.
1285                    value.clone(),
1286                );
1287            }
1288        }
1289    }
1290}
1291
1292type ParserError<'a> = Rich<'a, Token<'a>, Span>;
1293
1294impl From<ParserError<'_>> for Error {
1295    fn from(error: ParserError) -> Self {
1296        let error = error.map_token(|tok| tok.to_string());
1297
1298        Error::with_contexts(
1299            error.to_string(),
1300            error.reason().to_string(),
1301            *error.span(),
1302            error
1303                .contexts()
1304                .map(|(label, span)| (label.to_string(), *span))
1305                .collect(),
1306        )
1307    }
1308}
1309
1310// the state we thread through the parsers
1311#[derive(Default, Debug)]
1312pub(crate) struct ParserState<'a> {
1313    pub(crate) options: ParserOptions<'a>,
1314    pub(crate) warnings: Vec<Warning>,
1315}
1316
1317// our ParserExtra with our error and state types
1318pub(crate) type Extra<'a> = extra::Full<ParserError<'a>, extra::SimpleState<ParserState<'a>>, ()>;
1319
1320/// Enable use of own functions which emit errors
1321pub(crate) trait Emit<E> {
1322    fn emit(&mut self, err: E);
1323}
1324
1325impl<E> Emit<E> for chumsky::input::Emitter<E> {
1326    fn emit(&mut self, err: E) {
1327        self.emit(err)
1328    }
1329}
1330
1331// simple collection of errors in a Vec
1332impl<E> Emit<E> for Vec<Error>
1333where
1334    E: Into<Error>,
1335{
1336    fn emit(&mut self, err: E) {
1337        self.push(err.into())
1338    }
1339}
1340// a degenerate error sink
1341struct NullEmitter;
1342
1343impl<E> Emit<E> for NullEmitter {
1344    fn emit(&mut self, _err: E) {}
1345}
1346
1347mod tests;