rune/parse/
parser.rs

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
use core::fmt;
use core::ops;

use crate::alloc::VecDeque;
use crate::ast::Spanned;
use crate::ast::{Kind, OptionSpanned, Span, Token};
use crate::compile::WithSpan;
use crate::compile::{self, ErrorKind};
use crate::macros::{TokenStream, TokenStreamIter};
use crate::parse::{Advance, Lexer, Parse, Peek};
use crate::shared::FixedVec;
use crate::SourceId;

/// Parser for the rune language.
///
/// # Examples
///
/// ```
/// use rune::ast;
/// use rune::SourceId;
/// use rune::parse::Parser;
///
/// let mut parser = Parser::new("fn foo() {}", SourceId::empty(), false);
/// let ast = parser.parse::<ast::ItemFn>()?;
/// # Ok::<_, rune::support::Error>(())
/// ```
#[derive(Debug)]
pub struct Parser<'a> {
    peeker: Peeker<'a>,
}

impl<'a> Parser<'a> {
    /// Construct a new parser around the given source.
    ///
    /// `shebang` indicates if the parser should try and parse a shebang or not.
    pub fn new(source: &'a str, source_id: SourceId, shebang: bool) -> Self {
        Self::with_source(
            Source {
                inner: SourceInner::Lexer(Lexer::new(source, source_id, shebang)),
            },
            Span::new(0u32, source.len()),
        )
    }

    /// Construct a parser from a token stream. The second argument `span` is
    /// the span to use if the stream is empty.
    pub fn from_token_stream(token_stream: &'a TokenStream, span: Span) -> Self {
        Self::with_source(
            Source {
                inner: SourceInner::TokenStream(token_stream.iter()),
            },
            span,
        )
    }

    /// Parse a specific item from the parser.
    pub fn parse<T>(&mut self) -> compile::Result<T>
    where
        T: Parse,
    {
        T::parse(self)
    }

    /// Parse a specific item from the parser and then expect end of input.
    pub fn parse_all<T>(&mut self) -> compile::Result<T>
    where
        T: Parse,
    {
        let item = self.parse::<T>()?;
        self.eof()?;
        Ok(item)
    }

    /// Peek for the given token.
    pub fn peek<T>(&mut self) -> compile::Result<bool>
    where
        T: Peek,
    {
        if let Some(error) = self.peeker.error.take() {
            return Err(error);
        }

        let result = T::peek(&mut self.peeker);

        if let Some(error) = self.peeker.error.take() {
            return Err(error);
        }

        Ok(result)
    }

    /// Assert that the parser has reached its end-of-file.
    pub fn eof(&mut self) -> compile::Result<()> {
        if let Some(token) = self.peeker.at(0)? {
            return Err(compile::Error::new(
                token,
                ErrorKind::ExpectedEof { actual: token.kind },
            ));
        }

        Ok(())
    }

    /// Test if the parser is at end-of-file, after which there is no more input
    /// to parse.
    pub fn is_eof(&mut self) -> compile::Result<bool> {
        Ok(self.peeker.at(0)?.is_none())
    }

    /// Construct a new parser with a source.
    fn with_source(source: Source<'a>, span: Span) -> Self {
        let default_span = source.span().unwrap_or(span);

        Self {
            peeker: Peeker {
                source,
                buf: VecDeque::new(),
                error: None,
                last: None,
                default_span,
            },
        }
    }

    /// Try to consume a single thing matching `T`, returns `true` if any tokens
    /// were consumed.
    pub fn try_consume<T>(&mut self) -> compile::Result<bool>
    where
        T: Parse + Peek,
    {
        Ok(if self.peek::<T>()? {
            self.parse::<T>()?;
            true
        } else {
            false
        })
    }

    /// Try to consume all things matching `T`, returns `true` if any tokens
    /// were consumed.
    pub fn try_consume_all<T>(&mut self) -> compile::Result<bool>
    where
        T: Parse + Peek,
    {
        let mut consumed = false;

        while self.peek::<T>()? {
            self.parse::<T>()?;
            consumed = true;
        }

        Ok(consumed)
    }

    /// Get the span for the given range offset of tokens.
    pub(crate) fn span(&mut self, range: ops::Range<usize>) -> Span {
        self.span_at(range.start).join(self.span_at(range.end))
    }

    /// Access the interior peeker of the parser.
    pub(crate) fn peeker(&mut self) -> &mut Peeker<'a> {
        &mut self.peeker
    }

    /// Consume the next token from the parser.
    #[allow(clippy::should_implement_trait)]
    pub(crate) fn next(&mut self) -> compile::Result<Token> {
        if let Some(error) = self.peeker.error.take() {
            return Err(error);
        }

        if let Some(t) = self.peeker.buf.pop_front() {
            return Ok(t);
        }

        match self.peeker.next()? {
            Some(t) => Ok(t),
            None => Err(compile::Error::new(
                self.last_span().tail(),
                ErrorKind::UnexpectedEof,
            )),
        }
    }

    /// Peek the token kind at the given position.
    pub(crate) fn nth(&mut self, n: usize) -> compile::Result<Kind> {
        if let Some(t) = self.peeker.at(n)? {
            Ok(t.kind)
        } else {
            Ok(Kind::Eof)
        }
    }

    /// Get the span for the given offset.
    pub(crate) fn span_at(&mut self, n: usize) -> Span {
        if let Ok(Some(t)) = self.peeker.at(n) {
            t.span
        } else {
            self.last_span().tail()
        }
    }

    /// Get the token at the given offset.
    pub(crate) fn tok_at(&mut self, n: usize) -> compile::Result<Token> {
        Ok(if let Some(t) = self.peeker.at(n)? {
            t
        } else {
            Token {
                kind: Kind::Eof,
                span: self.last_span().tail(),
            }
        })
    }

    /// The last known span in this parser.
    pub(crate) fn last_span(&self) -> Span {
        self.peeker.last_span()
    }
}

/// Construct used to peek a parser.
#[derive(Debug)]
pub struct Peeker<'a> {
    /// The source being processed.
    source: Source<'a>,
    /// The buffer of tokens seen.
    buf: VecDeque<Token>,
    // NB: parse errors encountered during peeking.
    error: Option<compile::Error>,
    /// The last span we encountered. Used to provide better EOF diagnostics.
    last: Option<Span>,
    /// The default span to use in case no better one is available.
    default_span: Span,
}

impl Peeker<'_> {
    /// Peek the token kind at the given position.
    pub(crate) fn nth(&mut self, n: usize) -> Kind {
        // Error tripped already, this peeker returns nothing but errors from
        // here on out.
        if self.error.is_some() {
            return Kind::Error;
        }

        match self.at(n) {
            Ok(t) => match t {
                Some(t) => t.kind,
                None => Kind::Eof,
            },
            Err(error) => {
                self.error = Some(error);
                Kind::Error
            }
        }
    }

    /// Peek an array.
    pub(crate) fn array<const N: usize>(&mut self) -> FixedVec<Token, N> {
        let mut vec = FixedVec::new();

        if N == 0 {
            return vec;
        }

        if let Err(error) = self.fill(N) {
            self.error = Some(error);
        }

        let mut it = 0..N;

        for (&tok, _) in self.buf.iter().zip(it.by_ref()) {
            _ = vec.try_push(tok);
        }

        if let Some(error) = &self.error {
            for _ in it {
                _ = vec.try_push(Token {
                    kind: Kind::Error,
                    span: error.span(),
                });
            }
        } else {
            for _ in it {
                _ = vec.try_push(Token {
                    kind: Kind::Eof,
                    span: self.last_span(),
                });
            }
        }

        vec
    }

    /// Test if we are at end of file.
    pub(crate) fn is_eof(&mut self) -> bool {
        match self.at(0) {
            Ok(t) => t.is_none(),
            Err(error) => {
                self.error = Some(error);
                false
            }
        }
    }

    /// Advance the internals of the peeker and return the next token (without
    /// buffering).
    fn next(&mut self) -> compile::Result<Option<Token>> {
        loop {
            let Some(token) = self.source.next()? else {
                return Ok(None);
            };

            match token.kind {
                Kind::Comment | Kind::Whitespace => {
                    continue;
                }
                Kind::MultilineComment(term) => {
                    if !term {
                        return Err(compile::Error::new(
                            token.span,
                            ErrorKind::ExpectedMultilineCommentTerm,
                        ));
                    }

                    continue;
                }
                _ => (),
            }

            return Ok(Some(token));
        }
    }

    /// Make sure there are at least `n` items in the buffer, and return the
    /// item at that point.
    fn at(&mut self, n: usize) -> compile::Result<Option<Token>> {
        self.fill(n)?;
        Ok(self.buf.get(n).copied())
    }

    fn fill(&mut self, n: usize) -> compile::Result<()> {
        if let Some(error) = self.error.take() {
            return Err(error);
        }

        while self.buf.len() <= n {
            let Some(tok) = self.next()? else {
                break;
            };

            self.last = Some(tok.span);
            self.buf.try_push_back(tok).with_span(tok.span)?;
        }

        Ok(())
    }

    /// The last known span in this parser.
    fn last_span(&self) -> Span {
        self.last.unwrap_or(self.default_span)
    }
}

/// A source adapter.
pub(crate) struct Source<'a> {
    inner: SourceInner<'a>,
}

impl Source<'_> {
    /// Get the span of the source.
    fn span(&self) -> Option<Span> {
        match &self.inner {
            SourceInner::Lexer(lexer) => Some(lexer.span()),
            SourceInner::TokenStream(token_stream) => token_stream.option_span(),
        }
    }

    /// Get the next token in the stream.
    fn next(&mut self) -> compile::Result<Option<Token>> {
        match &mut self.inner {
            SourceInner::Lexer(lexer) => lexer.next(),
            SourceInner::TokenStream(token_stream) => Ok(token_stream.next()),
        }
    }
}

impl fmt::Debug for Source<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(&self.inner, f)
    }
}

#[derive(Debug)]
enum SourceInner<'a> {
    Lexer(Lexer<'a>),
    TokenStream(TokenStreamIter<'a>),
}

impl Advance for Parser<'_> {
    type Error = compile::Error;

    #[inline]
    fn advance(&mut self, n: usize) -> Result<(), Self::Error> {
        for _ in 0..n {
            self.next()?;
        }

        Ok(())
    }
}