moore_vhdl_syntax/parser/
token_stream.rs

1// Copyright (c) 2016-2021 Fabian Schuiki
2
3use moore_common::errors::*;
4use moore_common::source::*;
5
6/// A generalized stream of tokens that accepts emission of diagnostics and
7/// tracking of the severity of issues encountered.
8pub trait TokenStream<T> {
9    /// Look ahead at a token in the stream.
10    fn peek(&mut self, offset: usize) -> Spanned<T>;
11
12    /// Consume the current token.
13    fn bump(&mut self);
14
15    /// Skip the current token. Usually the same as `bump`, but may be used to
16    /// keep skipped tokens out of the consumed tokens count by some parsers.
17    fn skip(&mut self) {
18        self.bump()
19    }
20
21    /// Get the number of tokens consumed. Excludes tokens skipped with `skip`.
22    fn consumed(&self) -> usize;
23
24    /// Get the span of the last token consumed token (bumped or skipped).
25    fn last_span(&self) -> Span;
26
27    /// Get the tail location of the last consumed token (bumped or skipped).
28    fn last_loc(&self) -> Location {
29        self.last_span().end()
30    }
31
32    /// Emit a diagnostic.
33    fn emit(&mut self, diag: DiagBuilder2);
34
35    /// Get the severity of the worst diagnostic emitted so far.
36    fn severity(&self) -> Severity;
37
38    /// Check whether a fatal diagnostic has been emitted.
39    fn is_fatal(&self) -> bool {
40        self.severity() >= Severity::Fatal
41    }
42
43    /// Check whether an error diagnostic has been emitted.
44    fn is_error(&self) -> bool {
45        self.severity() >= Severity::Error
46    }
47}