1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
use std::cell::RefCell;

use winnow::bytes::any;
use winnow::bytes::one_of;
use winnow::combinator::cut_err;
use winnow::combinator::eof;
use winnow::combinator::opt;
use winnow::combinator::peek;
use winnow::error::FromExternalError;
use winnow::multi::many0;

use crate::document::Document;
use crate::key::Key;
use crate::parser::inline_table::KEYVAL_SEP;
use crate::parser::key::key;
use crate::parser::prelude::*;
use crate::parser::state::ParseState;
use crate::parser::table::table;
use crate::parser::trivia::{comment, line_ending, line_trailing, newline, ws};
use crate::parser::value::value;
use crate::table::TableKeyValue;
use crate::Item;
use crate::RawString;

// ;; TOML

// toml = expression *( newline expression )

// expression = ( ( ws comment ) /
//                ( ws keyval ws [ comment ] ) /
//                ( ws table ws [ comment ] ) /
//                  ws )
pub(crate) fn document(input: Input<'_>) -> IResult<Input<'_>, Document, ParserError<'_>> {
    let state = RefCell::new(ParseState::default());
    let state_ref = &state;

    let (i, _o) = (
        // Remove BOM if present
        opt(b"\xEF\xBB\xBF"),
        parse_ws(state_ref),
        many0((
            dispatch! {peek(any);
                crate::parser::trivia::COMMENT_START_SYMBOL => cut_err(parse_comment(state_ref)),
                crate::parser::table::STD_TABLE_OPEN => cut_err(table(state_ref)),
                crate::parser::trivia::LF |
                crate::parser::trivia::CR => parse_newline(state_ref),
                _ => cut_err(keyval(state_ref)),
            },
            parse_ws(state_ref),
        ))
        .map(|()| ()),
        eof,
    )
        .parse_next(input)?;
    state
        .into_inner()
        .into_document()
        .map(|document| (i, document))
        .map_err(|err| {
            winnow::error::ErrMode::Backtrack(ParserError::from_external_error(
                i,
                winnow::error::ErrorKind::Verify,
                err,
            ))
        })
}

pub(crate) fn parse_comment<'s, 'i>(
    state: &'s RefCell<ParseState>,
) -> impl FnMut(Input<'i>) -> IResult<Input<'i>, (), ParserError<'_>> + 's {
    move |i| {
        (comment, line_ending)
            .span()
            .map(|span| {
                state.borrow_mut().on_comment(span);
            })
            .parse_next(i)
    }
}

pub(crate) fn parse_ws<'s, 'i>(
    state: &'s RefCell<ParseState>,
) -> impl FnMut(Input<'i>) -> IResult<Input<'i>, (), ParserError<'i>> + 's {
    move |i| {
        ws.span()
            .map(|span| state.borrow_mut().on_ws(span))
            .parse_next(i)
    }
}

pub(crate) fn parse_newline<'s, 'i>(
    state: &'s RefCell<ParseState>,
) -> impl FnMut(Input<'i>) -> IResult<Input<'i>, (), ParserError<'i>> + 's {
    move |i| {
        newline
            .span()
            .map(|span| state.borrow_mut().on_ws(span))
            .parse_next(i)
    }
}

pub(crate) fn keyval<'s, 'i>(
    state: &'s RefCell<ParseState>,
) -> impl FnMut(Input<'i>) -> IResult<Input<'i>, (), ParserError<'i>> + 's {
    move |i| {
        parse_keyval
            .map_res(|(p, kv)| state.borrow_mut().on_keyval(p, kv))
            .parse_next(i)
    }
}

// keyval = key keyval-sep val
pub(crate) fn parse_keyval(
    input: Input<'_>,
) -> IResult<Input<'_>, (Vec<Key>, TableKeyValue), ParserError<'_>> {
    (
        key,
        cut_err((
            one_of(KEYVAL_SEP)
                .context(Context::Expected(ParserValue::CharLiteral('.')))
                .context(Context::Expected(ParserValue::CharLiteral('='))),
            (
                ws.span(),
                value(RecursionCheck::default()),
                line_trailing
                    .context(Context::Expected(ParserValue::CharLiteral('\n')))
                    .context(Context::Expected(ParserValue::CharLiteral('#'))),
            ),
        )),
    )
        .map_res::<_, _, std::str::Utf8Error>(|(key, (_, v))| {
            let mut path = key;
            let key = path.pop().expect("grammar ensures at least 1");

            let (pre, v, suf) = v;
            let pre = RawString::with_span(pre);
            let suf = RawString::with_span(suf);
            let v = v.decorated(pre, suf);
            Ok((
                path,
                TableKeyValue {
                    key,
                    value: Item::Value(v),
                },
            ))
        })
        .parse_next(input)
}