1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/// Preprocessor directive parser declaration
use std::collections::VecDeque;

use rowan::{TextRange, TextSize};

use crate::{
    lexer::{self, Lexer, TextToken},
    util::Unescaped,
};

mod ast;
pub use ast::*;

mod error;
pub use error::*;

mod lang;
pub use lang::*;

mod syntax_kind;
pub use syntax_kind::*;

mod syntax;

type SyntaxBitset = cbitset::BitSet256;
// SyntaxBitset::capacity() isn't a const fn, keep this in sync
static_assertions::const_assert!((SyntaxKind::_LAST as usize) < 256);

pub(crate) type SyntaxNode = rowan::SyntaxNode<PreprocessorLang>;
pub(crate) type SyntaxToken = rowan::SyntaxToken<PreprocessorLang>;

pub struct Parser<'i> {
    source: &'i str,
}

pub struct ParserRun<'i, 'cache> {
    builder: rowan::GreenNodeBuilder<'cache>,
    errors: Vec<Error>,
    peeked: Option<Option<lexer::TextToken>>,
    trivia_buffer: VecDeque<lexer::TextToken>,
    source: &'i str,
    input: Lexer<'i>,
}

// Public parser API
impl<'i> Parser<'i> {
    pub fn new(input: &'i str) -> Self {
        Self { source: input }
    }

    pub fn parse(self) -> Ast {
        ParserRun::new(self.source).parse_ast()
    }

    pub fn parse_define_body(self) -> Option<SyntaxNode> {
        ParserRun::new(self.source).parse_one(syntax::define_body)
    }
}

// Builder wrapper methods
impl<'i, 'cache> ParserRun<'i, 'cache> {
    fn new(source: &'i str) -> Self {
        Self {
            builder: rowan::GreenNodeBuilder::new(),
            errors: Vec::new(),
            peeked: None,
            trivia_buffer: VecDeque::with_capacity(4),
            source,
            input: Lexer::new(source),
        }
    }

    fn parse_ast(mut self) -> Ast {
        self.start_node(SyntaxKind::ROOT);
        syntax::file(&mut self);
        self.finish_node();

        Ast::new(
            self.builder.finish(),
            self.errors,
            self.input.into_line_map(),
        )
    }

    fn parse_one(mut self, f: impl FnOnce(&mut Self)) -> Option<SyntaxNode> {
        self.start_node(SyntaxKind::ROOT);
        f(&mut self);
        self.finish_node();

        if self.errors.is_empty() {
            SyntaxNode::new_root(self.builder.finish()).first_child()
        } else {
            None
        }
    }

    fn checkpoint(&mut self) -> rowan::Checkpoint {
        self.builder.checkpoint()
    }

    fn start_node(&mut self, kind: SyntaxKind) {
        self.builder.start_node(kind.into());
    }

    fn start_node_at(&mut self, checkpoint: rowan::Checkpoint, kind: SyntaxKind) {
        self.builder.start_node_at(checkpoint, kind.into());
    }

    fn finish_node(&mut self) {
        self.builder.finish_node();
    }
}

// Private parser API
impl<'i, 'cache> ParserRun<'i, 'cache> {
    fn skip(&mut self, what: impl Fn(&lexer::Token) -> bool) {
        while self.peek().map(|tk| what(&tk.token)).unwrap_or(false) {
            self.bump();
        }
    }

    fn skip_trivia(&mut self) {
        self.skip(lexer::Token::is_trivia)
    }

    fn peek(&mut self) -> Option<lexer::TextToken> {
        if self.peeked.is_none() {
            // No token was peeked, read one from the input
            self.peeked = Some(self.input.next());
        }

        // TODO: Move this to unwrap_unchecked when it is stable
        // unwrap: self.peeked is necessarily Some
        self.peeked.unwrap()
    }

    fn raw(&self, token: lexer::TextToken) -> &'i str {
        token.raw(self.source)
    }

    fn text(&self, token: lexer::TextToken) -> Unescaped {
        Unescaped::new(self.raw(token))
    }

    fn eat_trivia(&mut self) {
        while let Some(token) = self.trivia_buffer.pop_front() {
            self.builder
                .token(SyntaxKind::from(token.token).into(), token.raw(self.source));
        }
    }

    fn buffer_trivia(&mut self) {
        while let Some(current) = self.peek() {
            if current.token.is_trivia() {
                self.trivia_buffer.push_back(current);
            } else {
                break;
            }

            // Advance
            self.peeked.take();
        }
    }

    fn push_error(&mut self, error_kind: ErrorKind, range: TextRange) {
        self.errors.push(
            Error::builder()
                .pos(range)
                .resolve(self.input.line_map())
                .finish(error_kind),
        );
    }

    fn expect_one(&mut self, expected: lexer::Token) -> ExpectAny {
        if let Some(token) = self.peek() {
            self.bump();

            if expected == *token {
                return ExpectAny::Found(token);
            } else {
                self.push_error(
                    ErrorKind::Unexpected {
                        actual: *token,
                        expected: Box::new([expected]),
                    },
                    token.range,
                );

                return ExpectAny::Unexpected(token);
            }
        }

        self.push_error(
            ErrorKind::EndOfInput {
                expected: Box::new([expected]),
            },
            TextRange::new(TextSize::of(self.source), TextSize::of(self.source)),
        );

        ExpectAny::EndOfInput
    }

    #[must_use = "None is returned if the expected token was not found"]
    fn expect_any(&mut self, expected: &[lexer::Token], dont_bump: &[lexer::Token]) -> ExpectAny {
        let bitset: SyntaxBitset = expected.iter().map(|&k| k as u16).collect();
        let dont_bump_bitset: SyntaxBitset = dont_bump.iter().map(|&k| k as u16).collect();

        if let Some(token) = self.peek() {
            // Always bump, expect consumes
            if !dont_bump_bitset.contains(*token as _) {
                self.bump();
            }

            if bitset.contains(*token as _) {
                return ExpectAny::Found(token);
            } else {
                self.push_error(
                    ErrorKind::Unexpected {
                        actual: *token,
                        expected: expected.into(),
                    },
                    token.range,
                );

                return ExpectAny::Unexpected(token);
            }
        }

        self.push_error(
            ErrorKind::EndOfInput {
                expected: expected.into(),
            },
            TextRange::new(TextSize::of(self.source), TextSize::of(self.source)),
        );

        ExpectAny::EndOfInput
    }

    fn bump(&mut self) {
        // Get input token
        let token = if let Some(token) = self.peeked.take() {
            token
        } else {
            self.input.next()
        };

        if let Some(token) = token {
            self.builder
                .token(SyntaxKind::from(token.token).into(), token.raw(self.source));
        } else {
            panic!("tried to bump at end of input");
        }
    }
}

#[derive(Debug, Clone, Copy)]
enum ExpectAny {
    Found(TextToken),
    Unexpected(TextToken),
    EndOfInput,
}

#[cfg(test)]
mod tests;