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
pub use fe_common::diagnostics::Label;
use fe_common::diagnostics::{Diagnostic, Severity};
use fe_common::files::SourceFileId;

use crate::lexer::{Lexer, Token, TokenKind};
use crate::node::Span;
use std::{error, fmt};

#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)]
pub struct ParseFailed;
impl fmt::Display for ParseFailed {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
        write!(fmt, "ParseFailed")
    }
}
impl error::Error for ParseFailed {}

pub type ParseResult<T> = Result<T, ParseFailed>;

/// `Parser` maintains the parsing state, such as the token stream,
/// "enclosure" (paren, brace, ..) stack, diagnostics, etc.
/// Syntax parsing logic is in the [`crate::grammar`] module.
///
/// See [`BTParser`] if you need backtrackable parser.
pub struct Parser<'a> {
    pub file_id: SourceFileId,
    lexer: Lexer<'a>,

    /// Tokens that have been "peeked", or split from a larger token.
    /// Eg. `>>` may be split into two `>` tokens when parsing the end of a
    /// generic type parameter list (eg. `Map<u256, Map<u256, address>>`).
    buffered: Vec<Token<'a>>,

    enclosure_stack: Vec<Enclosure>,

    /// The diagnostics (errors and warnings) emitted during parsing.
    pub diagnostics: Vec<Diagnostic>,
}

impl<'a> Parser<'a> {
    /// Create a new parser for a source code string and associated file id.
    pub fn new(file_id: SourceFileId, content: &'a str) -> Self {
        Parser {
            file_id,
            lexer: Lexer::new(file_id, content),
            buffered: vec![],
            enclosure_stack: vec![],
            diagnostics: vec![],
        }
    }

    /// Returns back tracking parser.
    pub fn as_bt_parser<'b>(&'b mut self) -> BTParser<'a, 'b> {
        BTParser::new(self)
    }

    /// Return the next token, or an error if we've reached the end of the file.
    #[allow(clippy::should_implement_trait)] // next() is a nice short name for a common task
    pub fn next(&mut self) -> ParseResult<Token<'a>> {
        self.eat_newlines_if_in_nonblock_enclosure();
        if let Some(tok) = self.next_raw() {
            if is_enclosure_open(tok.kind) {
                self.enclosure_stack
                    .push(Enclosure::non_block(tok.kind, tok.span));
            } else if is_enclosure_close(tok.kind) {
                if let Some(open) = self.enclosure_stack.pop() {
                    if !enclosure_tokens_match(open.token_kind, tok.kind) {
                        // TODO: we should search the enclosure_stack
                        //  for the last matching enclosure open token.
                        //  If any enclosures are unclosed, we should emit an
                        //  error, and somehow close their respective ast nodes.
                        //  We could synthesize the correct close token, or emit
                        //  a special TokenKind::UnclosedEnclosure or whatever.
                        todo!()
                    }
                } else {
                    self.error(tok.span, format!("Unmatched `{}`", tok.text));
                }
            }
            Ok(tok)
        } else {
            self.error(
                Span::new(
                    self.file_id,
                    self.lexer.source().len(),
                    self.lexer.source().len(),
                ),
                "unexpected end of file",
            );
            Err(ParseFailed)
        }
    }

    fn next_raw(&mut self) -> Option<Token<'a>> {
        self.buffered.pop().or_else(|| self.lexer.next())
    }

    /// Take a peek at the next token kind without consuming it, or return an
    /// error if we've reached the end of the file.
    pub fn peek_or_err(&mut self) -> ParseResult<TokenKind> {
        self.eat_newlines_if_in_nonblock_enclosure();
        if let Some(tk) = self.peek_raw() {
            Ok(tk)
        } else {
            let index = self.lexer.source().len();
            self.error(
                Span::new(self.file_id, index, index),
                "unexpected end of file",
            );
            Err(ParseFailed)
        }
    }

    /// Take a peek at the next token kind. Returns `None` if we've reached the
    /// end of the file.
    pub fn peek(&mut self) -> Option<TokenKind> {
        self.eat_newlines_if_in_nonblock_enclosure();
        self.peek_raw()
    }

    fn peek_raw(&mut self) -> Option<TokenKind> {
        if self.buffered.is_empty() {
            if let Some(tok) = self.lexer.next() {
                self.buffered.push(tok);
            } else {
                return None;
            }
        }
        Some(self.buffered.last().unwrap().kind)
    }

    fn eat_newlines_if_in_nonblock_enclosure(&mut self) {
        // TODO: allow newlines inside angle brackets?
        //   eg `fn f(x: map\n <\n u8\n, ...`
        if let Some(enc) = self.enclosure_stack.last() {
            if !enc.is_block {
                self.eat_newlines();
            }
        }
    }

    /// Split the next token into two tokens, returning the first. Only supports
    /// splitting the `>>` token into two `>` tokens, specifically for
    /// parsing the closing angle bracket of a generic type argument list
    /// (`Map<x, Map<y, z>>`).
    ///
    /// # Panics
    /// Panics if the next token isn't `>>`
    pub fn split_next(&mut self) -> ParseResult<Token<'a>> {
        let gtgt = self.next()?;
        assert_eq!(gtgt.kind, TokenKind::GtGt);

        let (gt1, gt2) = gtgt.text.split_at(1);
        self.buffered.push(Token {
            kind: TokenKind::Gt,
            text: gt2,
            span: Span::new(self.file_id, gtgt.span.start + 1, gtgt.span.end),
        });

        Ok(Token {
            kind: TokenKind::Gt,
            text: gt1,
            span: Span::new(self.file_id, gtgt.span.start, gtgt.span.end - 1),
        })
    }

    /// Returns `true` if the parser has reached the end of the file.
    pub fn done(&mut self) -> bool {
        self.peek_raw().is_none()
    }

    pub fn eat_newlines(&mut self) {
        while self.peek_raw() == Some(TokenKind::Newline) {
            self.next_raw();
        }
    }

    /// Assert that the next token kind it matches the expected token
    /// kind, and return it. This should be used in cases where the next token
    /// kind is expected to have been checked already.
    ///
    /// # Panics
    /// Panics if the next token kind isn't `tk`.
    pub fn assert(&mut self, tk: TokenKind) -> Token<'a> {
        let tok = self.next().unwrap();
        assert_eq!(tok.kind, tk, "internal parser error");
        tok
    }

    /// If the next token matches the expected kind, return it. Otherwise emit
    /// an error diagnostic with the given message and return an error.
    pub fn expect<S: Into<String>>(
        &mut self,
        expected: TokenKind,
        message: S,
    ) -> ParseResult<Token<'a>> {
        self.expect_with_notes(expected, message, |_| Vec::new())
    }

    /// Like [`Parser::expect`], but with additional notes to be appended to the
    /// bottom of the diagnostic message. The notes are provided by a
    /// function that returns a `Vec<String>`, to avoid allocations in the
    /// case where the token is as expected.
    pub fn expect_with_notes<Str, NotesFn>(
        &mut self,
        expected: TokenKind,
        message: Str,
        notes_fn: NotesFn,
    ) -> ParseResult<Token<'a>>
    where
        Str: Into<String>,
        NotesFn: FnOnce(&Token) -> Vec<String>,
    {
        let tok = self.next()?;
        if tok.kind == expected {
            Ok(tok)
        } else {
            self.fancy_error(
                message.into(),
                vec![Label::primary(
                    tok.span,
                    format!(
                        "expected {}, found {}",
                        expected.describe(),
                        tok.kind.describe()
                    ),
                )],
                notes_fn(&tok),
            );
            Err(ParseFailed)
        }
    }

    /// If the next token matches the expected kind, return it. Otherwise return
    /// None.
    pub fn optional(&mut self, kind: TokenKind) -> Option<Token<'a>> {
        if self.peek() == Some(kind) {
            Some(self.next().unwrap())
        } else {
            None
        }
    }

    /// Emit an "unexpected token" error diagnostic with the given message.
    pub fn unexpected_token_error<S: Into<String>>(
        &mut self,
        tok: &Token,
        message: S,
        notes: Vec<String>,
    ) {
        self.fancy_error(
            message,
            vec![Label::primary(tok.span, "unexpected token")],
            notes,
        );
    }

    /// Enter a "block", which is a brace-enclosed list of statements,
    /// separated by newlines and/or semicolons.
    /// This checks for and consumes the `{` that precedes the block.
    pub fn enter_block(&mut self, context_span: Span, context_name: &str) -> ParseResult<()> {
        if self.peek_raw() == Some(TokenKind::BraceOpen) {
            let tok = self.next_raw().unwrap();
            self.enclosure_stack.push(Enclosure::block(tok.span));
            self.eat_newlines();
            Ok(())
        } else {
            self.fancy_error(
                format!("{context_name} must start with `{{`"),
                vec![Label::primary(
                    Span::new(self.file_id, context_span.end, context_span.end),
                    "expected `{` here",
                )],
                vec![],
            );
            Err(ParseFailed)
        }
    }

    /// Consumes newlines and semicolons. Returns Ok if one or more newlines or
    /// semicolons are consumed, or if the next token is a `}`.
    pub fn expect_stmt_end(&mut self, context_name: &str) -> ParseResult<()> {
        let mut newline = false;
        while matches!(self.peek_raw(), Some(TokenKind::Newline | TokenKind::Semi)) {
            newline = true;
            self.next_raw().unwrap();
        }
        if newline {
            return Ok(());
        }
        match self.peek_raw() {
            Some(TokenKind::BraceClose) => Ok(()),
            Some(_) => {
                let tok = self.next()?;
                self.unexpected_token_error(
                    &tok,
                    format!("unexpected token while parsing {context_name}"),
                    vec![format!(
                        "expected a newline; found {} instead",
                        tok.kind.describe()
                    )],
                );
                Err(ParseFailed)
            }
            None => Ok(()), // unexpect eof error will be generated be parent block
        }
    }

    /// Emit an error diagnostic, but don't stop parsing
    pub fn error<S: Into<String>>(&mut self, span: Span, message: S) {
        self.diagnostics.push(Diagnostic {
            severity: Severity::Error,
            message: message.into(),
            labels: vec![Label::primary(span, "")],
            notes: vec![],
        })
    }

    /// Emit a "fancy" error diagnostic with any number of labels and notes,
    /// but don't stop parsing.
    pub fn fancy_error<S: Into<String>>(
        &mut self,
        message: S,
        labels: Vec<Label>,
        notes: Vec<String>,
    ) {
        self.diagnostics.push(Diagnostic {
            severity: Severity::Error,
            message: message.into(),
            labels,
            notes,
        })
    }
}

/// A thin wrapper that makes [`Parser`] backtrackable.
pub struct BTParser<'a, 'b> {
    snapshot: &'b mut Parser<'a>,
    parser: Parser<'a>,
}

impl<'a, 'b> BTParser<'a, 'b> {
    pub fn new(snapshot: &'b mut Parser<'a>) -> Self {
        let parser = Parser {
            file_id: snapshot.file_id,
            lexer: snapshot.lexer.clone(),
            buffered: snapshot.buffered.clone(),
            enclosure_stack: snapshot.enclosure_stack.clone(),
            diagnostics: Vec::new(),
        };
        Self { snapshot, parser }
    }

    pub fn accept(self) {
        self.snapshot.lexer = self.parser.lexer;
        self.snapshot.buffered = self.parser.buffered;
        self.snapshot.enclosure_stack = self.parser.enclosure_stack;
        self.snapshot.diagnostics.extend(self.parser.diagnostics);
    }
}

impl<'a, 'b> std::ops::Deref for BTParser<'a, 'b> {
    type Target = Parser<'a>;

    fn deref(&self) -> &Self::Target {
        &self.parser
    }
}

impl<'a, 'b> std::ops::DerefMut for BTParser<'a, 'b> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.parser
    }
}

/// The start position of a chunk of code enclosed by (), [], or {}.
/// (This has nothing to do with "closures".)
///
/// Note that <> isn't currently considered an enclosure, as `<` might be
/// a less-than operator, while the rest are unambiguous.
///
/// A `{}` enclosure may or may not be a block. A block contains a list of
/// statements, separated by newlines or semicolons (or both).
/// Semicolons and newlines are consumed by `par.expect_stmt_end()`.
///
/// Non-block enclosures contains zero or more expressions, maybe separated by
/// commas. When the top-most enclosure is a non-block, newlines are ignored
/// (by `par.next()`), and semicolons are just normal tokens that'll be
/// rejected by the parsing fns in grammar/.
#[derive(Clone, Debug)]
struct Enclosure {
    token_kind: TokenKind,
    _token_span: Span, // TODO: mark mismatched tokens
    is_block: bool,
}
impl Enclosure {
    pub fn block(token_span: Span) -> Self {
        Self {
            token_kind: TokenKind::BraceOpen,
            _token_span: token_span,
            is_block: true,
        }
    }
    pub fn non_block(token_kind: TokenKind, token_span: Span) -> Self {
        Self {
            token_kind,
            _token_span: token_span,
            is_block: false,
        }
    }
}

fn is_enclosure_open(tk: TokenKind) -> bool {
    use TokenKind::*;
    matches!(tk, ParenOpen | BraceOpen | BracketOpen)
}

fn is_enclosure_close(tk: TokenKind) -> bool {
    use TokenKind::*;
    matches!(tk, ParenClose | BraceClose | BracketClose)
}

fn enclosure_tokens_match(open: TokenKind, close: TokenKind) -> bool {
    use TokenKind::*;
    matches!(
        (open, close),
        (ParenOpen, ParenClose) | (BraceOpen, BraceClose) | (BracketOpen, BracketClose)
    )
}