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
use std::fmt::Debug;
use bumpalo::Bump;
use bumpalo::collections::Vec as BVec;
use mago_database::file::FileId;
use mago_database::file::HasFileId;
use mago_span::Position;
use mago_span::Span;
use mago_syntax_core::parser::LookaheadBuf;
use crate::error::ParseError;
use crate::error::SyntaxError;
use crate::lexer::TypeLexer;
use crate::token::TypeToken;
use crate::token::TypeTokenKind;
/// A buffered token stream that wraps a `TypeLexer`, providing lookahead
/// capabilities and automatically skipping trivia tokens (whitespace, comments).
#[derive(Debug)]
#[allow(clippy::field_scoped_visibility_modifiers)]
pub struct TypeTokenStream<'arena> {
pub(crate) arena: &'arena Bump,
pub(crate) lexer: TypeLexer<'arena>,
file_id: FileId,
buffer: LookaheadBuf<TypeToken<'arena>, 64>,
position: Position,
}
impl<'arena> TypeTokenStream<'arena> {
/// Creates a new `TypeTokenStream` wrapping the given `TypeLexer`.
#[inline]
pub fn new(arena: &'arena Bump, lexer: TypeLexer<'arena>) -> TypeTokenStream<'arena> {
let position = lexer.current_position();
let file_id = lexer.file_id();
TypeTokenStream { arena, lexer, file_id, buffer: LookaheadBuf::new(), position }
}
/// Consume the next token and return its [`Span`]. Equivalent to
/// `stream.consume_span()?` but avoids the extra
/// method dispatch through `HasFileId`.
#[inline]
pub fn consume_span(&mut self) -> Result<Span, ParseError> {
let token = self.consume()?;
Ok(Span::new(self.file_id, token.start, token.end()))
}
/// Eat a token of `kind` and return its [`Span`].
#[inline]
pub fn eat_span(&mut self, kind: TypeTokenKind) -> Result<Span, ParseError> {
let token = self.eat(kind)?;
Ok(Span::new(self.file_id, token.start, token.end()))
}
/// Consume the next token and wrap it as a [`Keyword`](crate::ast::Keyword).
#[inline]
pub fn consume_keyword(&mut self) -> Result<crate::ast::Keyword<'arena>, ParseError> {
let token = self.consume()?;
let span = Span::new(self.file_id, token.start, token.end());
Ok(crate::ast::Keyword { span, value: token.value })
}
/// Eat a token of `kind` and wrap it as a [`Keyword`](crate::ast::Keyword).
#[inline]
pub fn eat_keyword(&mut self, kind: TypeTokenKind) -> Result<crate::ast::Keyword<'arena>, ParseError> {
let token = self.eat(kind)?;
let span = Span::new(self.file_id, token.start, token.end());
Ok(crate::ast::Keyword { span, value: token.value })
}
/// Arena-allocate a value and return an `&'arena T` reference.
#[inline]
#[must_use]
pub fn alloc<T>(&self, value: T) -> &'arena T {
self.arena.alloc(value)
}
/// A fresh arena-backed [`BVec`].
#[inline]
#[must_use]
pub fn new_bvec<T>(&self) -> BVec<'arena, T> {
BVec::new_in(self.arena)
}
/// Returns the current position of the stream within the source file.
///
/// This position represents the end location of the most recently
/// consumed significant token via `advance()` or `consume()`.
#[inline]
pub const fn current_position(&self) -> Position {
self.position
}
/// Consumes and returns the next significant token.
///
/// Advances the stream's position.
///
/// # Returns
///
/// - `Ok(TypeToken)`: The next significant token.
/// - `Err(ParseError::UnexpectedEndOfFile)`: If EOF is reached.
/// - `Err(ParseError::SyntaxError)`: If the underlying lexer returned an error.
#[inline]
pub fn consume(&mut self) -> Result<TypeToken<'arena>, ParseError> {
match self.advance() {
Some(Ok(token)) => Ok(token),
Some(Err(error)) => Err(error.into()),
None => Err(self.unexpected(None, &[])),
}
}
/// Consumes the next token *only if* it matches the expected `kind`.
///
/// Advances the stream's position if the token matches.
///
/// # Returns
///
/// - `Ok(TypeToken)`: If the next token matches `kind`.
/// - `Err(ParseError::UnexpectedToken)`: If the next token does *not* match `kind`.
/// - `Err(ParseError::UnexpectedEndOfFile)`: If EOF is reached.
/// - `Err(ParseError::SyntaxError)`: If the underlying lexer returned an error.
#[inline]
pub fn eat(&mut self, kind: TypeTokenKind) -> Result<TypeToken<'arena>, ParseError> {
if let Some(token) = self.buffer.get(0) {
if kind == token.kind {
let _ = self.buffer.pop_front();
self.position = token.end();
return Ok(token);
}
return Err(self.unexpected(Some(token), &[kind]));
}
let token_result = self.consume();
match token_result {
Ok(token) => {
if kind == token.kind {
Ok(token)
} else {
Err(self.unexpected(Some(token), &[kind]))
}
}
Err(e) => Err(e),
}
}
/// Advances the underlying lexer and returns the raw result (including trivia).
/// Internal use or when trivia needs to be observed. `consume()` is preferred for parsers.
/// Returns `None` on EOF, `Some(Err)` on lexer error, `Some(Ok)` on success.
#[inline]
fn advance(&mut self) -> Option<Result<TypeToken<'arena>, SyntaxError>> {
match self.fill_buffer(1) {
Ok(true) => {
if let Some(token) = self.buffer.pop_front() {
self.position = token.end();
Some(Ok(token))
} else {
None
}
}
Ok(false) => None,
Err(error) => Some(Err(error)),
}
}
/// Returns the kind of the next significant token without consuming it.
/// More efficient than `peek()` when only the kind is needed.
///
/// # Returns
///
/// - `Ok(Some(TypeTokenKind))`: The kind of the next token.
/// - `Ok(None)`: If EOF is reached.
/// - `Err(ParseError)`: If the underlying lexer produced an error.
#[inline]
pub fn peek_kind(&mut self) -> Result<Option<TypeTokenKind>, ParseError> {
if let Some(t) = self.buffer.get(0) {
return Ok(Some(t.kind));
}
match self.fill_buffer(1) {
Ok(true) => Ok(self.buffer.get(0).map(|t| t.kind)),
Ok(false) => Ok(None),
Err(e) => Err(e.into()),
}
}
#[inline]
pub fn is_at(&mut self, kind: TypeTokenKind) -> Result<bool, ParseError> {
if let Some(t) = self.buffer.get(0) {
return Ok(t.kind == kind);
}
Ok(match self.peek_kind()? {
Some(k) => k == kind,
None => false,
})
}
/// Peeks at the next significant token without consuming it.
///
/// Requires `&mut self` as it might need to fill the buffer.
///
/// # Returns
///
/// - `Ok(TypeToken)`: A copy of the next significant token.
/// - `Err(SyntaxError::UnexpectedEndOfFile)`: If EOF is reached.
/// - `Err(ParseError)`: If the underlying lexer produced an error while peeking.
#[inline]
pub fn peek(&mut self) -> Result<TypeToken<'arena>, ParseError> {
match self.lookahead(0)? {
Some(token) => Ok(token),
None => Err(ParseError::UnexpectedEndOfFile(self.file_id(), vec![], self.current_position())),
}
}
/// Peeks at the nth (0-indexed) significant token ahead without consuming it.
///
/// `lookahead(0)` is equivalent to `peek()`, but returns `Ok(None)` on EOF
/// instead of an error. Requires `&mut self` as it might need to fill the buffer.
///
/// # Returns
///
/// - `Ok(Some(TypeToken))`: If the nth token exists.
/// - `Ok(None)`: If EOF is reached before the nth token.
/// - `Err(ParseError)`: If the underlying lexer produced an error.
#[inline]
pub fn lookahead(&mut self, n: usize) -> Result<Option<TypeToken<'arena>>, ParseError> {
if n < self.buffer.len() {
return Ok(self.buffer.get(n));
}
match self.fill_buffer(n + 1) {
Ok(true) => Ok(self.buffer.get(n)),
Ok(false) => Ok(None),
Err(error) => Err(error.into()),
}
}
/// Creates a `ParseError` for an unexpected token or EOF.
/// Internal helper for `consume` and `eat`.
#[inline]
fn unexpected(&self, found: Option<TypeToken<'arena>>, expected_one_of: &[TypeTokenKind]) -> ParseError {
if let Some(token) = found {
// Found a token, but it was the wrong kind
ParseError::UnexpectedToken(expected_one_of.to_vec(), token.kind, token.span_for(self.file_id()))
} else {
// Reached EOF when expecting specific kinds
ParseError::UnexpectedEndOfFile(self.file_id(), expected_one_of.to_vec(), self.current_position())
}
}
/// Internal helper to ensure the lookahead buffer contains at least `n` items.
/// Skips trivia tokens automatically. Returns `Ok(true)` on success,
/// `Ok(false)` on EOF, `Err` on lexer error.
#[inline]
fn fill_buffer(&mut self, n: usize) -> Result<bool, SyntaxError> {
if self.buffer.len() >= n {
return Ok(true);
}
self.fill_buffer_slow(n)
}
#[inline(never)]
fn fill_buffer_slow(&mut self, n: usize) -> Result<bool, SyntaxError> {
while self.buffer.len() < n {
match self.lexer.advance() {
Some(Ok(token)) => {
if token.kind.is_trivia() {
continue; // Skip trivia
}
self.buffer.push_back(token);
}
Some(Err(error)) => return Err(error),
None => return Ok(false),
}
}
Ok(true) // Buffer filled successfully
}
}
impl HasFileId for TypeTokenStream<'_> {
#[inline]
fn file_id(&self) -> FileId {
self.file_id
}
}