1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
//! Defines the parse tokens, parser interface, and output syntax tree.
use std::cell::RefCell;
use std::fmt;
use std::rc::Rc;

use rowan::{GreenNode, GreenNodeBuilder};

use crate::Error;
use crate::{
    language::{SyntaxElement, SyntaxNode},
    SyntaxKind,
};

/// A parsed token.
///
/// Consists of a [`crate::SyntaxKind`] and a reference to the corresponding
/// span of the source code.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Token<'a> {
    kind: SyntaxKind,
    text: &'a str,
}

impl<'a> Token<'a> {
    pub fn new(kind: SyntaxKind, text: &'a str) -> Self {
        Self { kind, text }
    }

    pub fn kind(&self) -> SyntaxKind {
        self.kind
    }

    pub fn text(&self) -> &str {
        self.text
    }

    fn from_lex_token(
        lex: pomelo_lex::LexToken,
        src: &'a str,
        offset: usize,
    ) -> (Self, Option<crate::Error>) {
        let text = &src[offset..offset + lex.len()];
        let (kind, opt_err_msg) = SyntaxKind::from_lexed(lex.kind(), text);
        let opt_err = opt_err_msg.map(|msg| Error::new(msg, text, offset));
        (Self::new(kind, text), opt_err)
    }
}

/// Output of the parsing stage.
///
/// Holds a [`rowan::GreenNode`] representing the root of the tree,
/// and any errors encountered during parsing.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SyntaxTree {
    node: GreenNode,
    errors: Vec<Error>,
}

impl SyntaxTree {
    pub fn new(node: GreenNode, errors: Vec<Error>) -> Self {
        Self { node, errors }
    }

    pub fn syntax(&self) -> SyntaxNode {
        SyntaxNode::new_root(self.node.clone())
    }

    pub fn errors(&self) -> impl Iterator<Item = &Error> {
        self.errors.iter()
    }

    pub fn has_errors(&self) -> bool {
        !self.errors.is_empty()
    }

    pub fn node(&self) -> GreenNode {
        self.node.clone()
    }

    pub fn into_parts(self) -> (GreenNode, Vec<Error>) {
        (self.node, self.errors)
    }
}

impl fmt::Display for SyntaxTree {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fn pretty_print(
            f: &mut fmt::Formatter<'_>,
            indent: usize,
            element: SyntaxElement,
        ) -> fmt::Result {
            write!(f, "{:indent$}", "", indent = indent)?;
            match element {
                rowan::NodeOrToken::Node(node) => {
                    writeln!(f, "{node:?}")?;
                    for c in node.children_with_tokens() {
                        pretty_print(f, indent + 2, c)?;
                    }
                    Ok(())
                }
                rowan::NodeOrToken::Token(token) => match token.kind() {
                    // Don't show whitespace text when formatting the tree
                    SyntaxKind::WHITESPACE => {
                        writeln!(f, "{:?}@{:?}", token.kind(), token.text_range())
                    }
                    _ => writeln!(f, "{token:?}"),
                },
            }
        }
        // Ignore errors for now!
        pretty_print(f, 0, SyntaxElement::Node(self.syntax()))
    }
}

/// The main interface for manipulating the lexed source.
///
/// This handles iterating over the lex tokens as well as building the syntax tree
/// (see [`rowan::GreenNodeBuilder`](https://docs.rs/rowan/latest/rowan/struct.GreenNodeBuilder.html)
/// for details).
#[derive(Debug, Clone)]
pub struct Parser<'a> {
    current_pos: usize,
    /// Tokens are stored in reverse order
    tokens: Vec<Token<'a>>,
    errors: Vec<Error>,
    builder: NodeBuilder,
}

impl<'a> Parser<'a> {
    pub fn new(src: &'a str) -> Self {
        let mut tokens = Vec::new();
        let mut errors = Vec::new();

        let lex_tokens = pomelo_lex::lex(src);

        let mut offset = 0;
        for lex_tok in lex_tokens {
            let lex_tok_len = lex_tok.len();

            let (token, opt_err) = Token::from_lex_token(lex_tok, src, offset);
            tokens.push(token);

            if let Some(err) = opt_err {
                errors.push(err);
            }

            offset += lex_tok_len;
        }
        tokens.reverse(); // Do this so we can pop tokens off the back of the vector efficiently

        let builder = NodeBuilder::new();

        Self {
            current_pos: 0,
            tokens,
            errors,
            builder,
        }
    }

    /// Parse an entire source file.
    pub fn parse(self) -> SyntaxTree {
        self.parse_inner(crate::grammar::source_file)
    }

    /// Parse a single expression.
    pub fn parse_expr(self) -> SyntaxTree {
        self.parse_inner(crate::grammar::expression)
    }

    /// Parse a single pattern.
    pub fn parse_pat(self) -> SyntaxTree {
        self.parse_inner(crate::grammar::pattern)
    }

    /// Parse a single type.
    pub fn parse_type(self) -> SyntaxTree {
        self.parse_inner(crate::grammar::ty)
    }

    /// Parse a single declaration.
    pub fn parse_dec(self) -> SyntaxTree {
        self.parse_inner(crate::grammar::declaration)
    }
}

// Parsing utilities
impl<'a> Parser<'a> {
    /// Parse according to a specified parsing function `f`.
    ///
    /// This is here for testing purposes.
    pub(crate) fn parse_inner<F>(mut self, mut f: F) -> SyntaxTree
    where
        F: FnMut(&mut Parser),
    {
        f(&mut self);

        SyntaxTree {
            node: self.builder.finish(),
            errors: self.errors,
        }
    }

    /// Peek at the kind of the next token.
    pub(crate) fn peek(&self) -> SyntaxKind {
        self.peek_nth(0)
    }

    /// Peek ahead `n` tokens.
    pub(crate) fn peek_nth(&self, n: usize) -> SyntaxKind {
        self.tokens
            .iter()
            .rev()
            .nth(n)
            .map(Token::kind)
            .unwrap_or(SyntaxKind::EOF)
    }

    /// Skips past `skip` nontrivia tokens, then peeks at the kind of the next one.
    pub(crate) fn peek_next_nontrivia(&self, skip: usize) -> SyntaxKind {
        self.peek_token_next_nontrivia(skip)
            .map(Token::kind)
            .unwrap_or(SyntaxKind::EOF)
    }

    /// Peek at the next token.
    pub(crate) fn peek_token(&self) -> Option<&Token> {
        self.tokens.last()
    }

    /// Peek at the text of the next token.
    pub(crate) fn peek_text(&self) -> &str {
        self.peek_token().map(Token::text).unwrap_or("\0")
    }

    /// Peeks past `skip` nontrivia tokens, then peeks at the next token.
    pub(crate) fn peek_token_next_nontrivia(&self, skip: usize) -> Option<&Token> {
        self.tokens
            .iter()
            .rev()
            .filter(|t| !t.kind().is_trivia())
            .nth(skip)
    }

    /// If `kind` matches the next token kind, consumes the token and returns true.
    /// Else, returns false.
    pub(crate) fn eat(&mut self, kind: SyntaxKind) -> bool {
        if kind == self.peek() {
            let token = self.pop();
            self.push_token(token);
            true
        } else {
            false
        }
    }

    /// Consume the next token, regardless of its kind.
    pub(crate) fn eat_any(&mut self) -> SyntaxKind {
        let token = self.pop();
        let kind = token.kind();
        self.push_token(token);
        kind
    }

    /// While the next token is trivia, consume it.
    pub(crate) fn eat_trivia(&mut self) -> bool {
        let mut eaten = false;

        while !self.is_eof() {
            if self.peek().is_trivia() {
                self.eat_any();
                eaten = true;
            } else {
                break;
            }
        }
        eaten
    }

    /// If the next nontrivia token matches `kind`, consume it (and any leading trivia) and
    /// return true. Else returns false.
    pub(crate) fn eat_through_trivia(&mut self, kind: SyntaxKind) -> bool {
        if self.peek_next_nontrivia(0) == kind {
            self.eat_trivia();
            assert!(self.eat(kind));
            true
        } else {
            false
        }
    }

    /// Consume the next token if it matches `kind`, else generate an error.
    pub(crate) fn expect(&mut self, kind: SyntaxKind) {
        if !self.eat(kind) {
            self.error(format!("expected {kind:?}"))
        }
    }

    /// Append a new error to the stored list of errors.
    pub(crate) fn error(&mut self, msg: impl Into<String> + Clone) {
        let pos = self.current_pos;
        let text = self.peek_token().map(|t| t.text()).unwrap_or("").to_owned();

        self.errors.push(Error::new(msg, text, pos));

        // Push into syntax tree as well for now
        self.push_token(Token::new(SyntaxKind::ERROR, ""))
    }

    /// Consume the next token but remap its `SyntaxKind` to be `kind`.
    pub(crate) fn eat_mapped(&mut self, kind: SyntaxKind) -> SyntaxKind {
        let token = self.pop();
        let mapped = Token::new(kind, token.text());
        self.push_token(mapped);
        token.kind()
    }

    pub(crate) fn is_eof(&self) -> bool {
        self.peek() == SyntaxKind::EOF
    }

    /// Check if the current token is a valid VId.
    ///
    /// With the current lexing strategy, correct
    /// symbolic identifiers are caught at lexing stage.
    /// Thus, the only special one we need to check for is EQ.
    ///
    /// However, not sure if this is a good strategy for being
    /// error-resilient. The lexer obviously has less context than
    /// the parser for determining what to do if there is an error.
    /// This may be generally an issue with gluing together tokens at
    /// lex-time (like "=>" as THICK_ARROW, "..." as ELLIPSIS, etc.)
    pub(crate) fn is_vid(&self) -> bool {
        matches!(self.peek(), SyntaxKind::IDENT | SyntaxKind::EQ)
    }

    /// Check if the current token is a valid VId.
    ///
    /// With the current lexing strategy, correct
    /// symbolic identifiers are caught at lexing stage.
    /// Thus, the only special one we need to check for is EQ.
    ///
    /// However, not sure if this is a good strategy for being
    /// error-resilient. The lexer obviously has less context than
    /// the parser for determining what to do if there is an error.
    /// This may be generally an issue with gluing together tokens at
    /// lex-time (like "=>" as THICK_ARROW, "..." as ELLIPSIS, etc.)
    pub(crate) fn next_nontrivia_is_vid(&self) -> bool {
        matches!(
            self.peek_next_nontrivia(0),
            SyntaxKind::IDENT | SyntaxKind::EQ
        )
    }

    /// Check if the current token is a valid StrId.
    pub(crate) fn is_strid(&self) -> bool {
        let t = self.peek_token();

        if let Some(t) = t {
            match t.kind() {
                SyntaxKind::IDENT => t.text().chars().all(char::is_alphanumeric),
                _ => false,
            }
        } else {
            false
        }
    }

    fn pop(&mut self) -> Token<'a> {
        match self.tokens.pop() {
            Some(t) => {
                self.current_pos += t.text().len();
                t
            }
            None => Token::new(SyntaxKind::EOF, ""),
        }
    }
}

// Syntax tree builder
impl<'a> Parser<'a> {
    /// Start a new node in the syntax tree.
    ///
    /// Note that the returned [`NodeGuard`] will be dropped immediately if not
    /// bound to a variable.
    #[must_use]
    pub(crate) fn start_node(&mut self, kind: SyntaxKind) -> NodeGuard {
        self.builder.start_node(kind)
    }

    /// Set a checkpoint that can be used later to create a node earlier in the tree.
    ///
    /// This is essentially just lookahead. Make an example?
    #[must_use]
    pub(crate) fn checkpoint(&self) -> Checkpoint {
        self.builder.checkpoint()
    }

    /// Use a previously set `checkpoint` to create a new node at its position (higher up in the tree).
    ///
    /// Note that the returned [`NodeGuard`] will be dropped immediately if not
    /// bound to a variable.
    #[must_use]
    pub(crate) fn start_node_at(&mut self, checkpoint: Checkpoint, kind: SyntaxKind) -> NodeGuard {
        self.builder.start_node_at(checkpoint, kind)
    }

    /// Add a token to the current node.
    pub fn push_token(&mut self, token: Token) {
        self.builder.push_token(token)
    }
}

/// A wrapper for [`rowan::GreenNodeBuilder`].
#[derive(Debug, Clone)]
pub(crate) struct NodeBuilder(Rc<RefCell<GreenNodeBuilder<'static>>>);

impl NodeBuilder {
    pub(crate) fn new() -> Self {
        Self(Rc::new(RefCell::new(GreenNodeBuilder::default())))
    }

    #[must_use]
    pub(crate) fn start_node(&mut self, kind: SyntaxKind) -> NodeGuard {
        self.0.borrow_mut().start_node(kind.into());
        NodeGuard {
            builder: self.0.clone(),
        }
    }

    #[must_use]
    pub(crate) fn checkpoint(&self) -> Checkpoint {
        Checkpoint(self.0.borrow().checkpoint())
    }

    #[must_use]
    pub(crate) fn start_node_at(&mut self, checkpoint: Checkpoint, kind: SyntaxKind) -> NodeGuard {
        self.0.borrow_mut().start_node_at(checkpoint.0, kind.into());
        NodeGuard {
            builder: self.0.clone(),
        }
    }

    pub(crate) fn push_token(&mut self, token: Token) {
        self.0.borrow_mut().token(token.kind().into(), token.text())
    }

    pub(crate) fn finish(self) -> GreenNode {
        self.0.take().finish()
    }
}

/// A wrapper for [`rowan::Checkpoint`].
#[derive(Debug, Clone)]
pub(crate) struct Checkpoint(rowan::Checkpoint);

/// A guard to avoid forgetting to call `builder.finish_node()`.
///
/// This nice RAII trick is copied from
/// [`apollo-parser`](https://docs.rs/apollo-parser/latest/apollo_parser/).
#[derive(Debug, Clone)]
pub(crate) struct NodeGuard {
    builder: Rc<RefCell<GreenNodeBuilder<'static>>>,
}

impl Drop for NodeGuard {
    fn drop(&mut self) {
        self.builder.borrow_mut().finish_node();
    }
}