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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
//! Provides a way to parse a token stream into an abstract syntax tree.
use derive_more::{Deref, DerefMut};
use enum_as_inner::EnumAsInner;
use crate::{
base::{self, Handler},
lexical::{
token::{Identifier, Keyword, KeywordKind, Numeric, Punctuation, StringLiteral, Token},
token_stream::{Delimited, Delimiter, TokenStream, TokenTree},
},
};
use super::error::{Error, ParseResult, SyntaxKind, UnexpectedSyntax};
/// Represents a parser that reads a token stream and constructs an abstract syntax tree.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Deref, DerefMut)]
pub struct Parser<'a> {
#[deref]
#[deref_mut]
current_frame: Frame<'a>,
stack: Vec<Frame<'a>>,
}
impl<'a> Parser<'a> {
/// Creates a new parser from the given token stream.
#[must_use]
pub fn new(token_stream: &'a TokenStream) -> Self {
Self {
current_frame: Frame {
token_provider: TokenProvider::TokenStream(token_stream),
current_index: 0,
},
stack: Vec::new(),
}
}
/// Steps into the [`Delimited`] token stream and parses the content within the delimiters.
///
/// The parser's position must be at the delimited token stream.
///
/// # Errors
/// - If the parser's position is not at the delimited token stream.
pub fn step_into<T>(
&mut self,
delimiter: Delimiter,
f: impl FnOnce(&mut Self) -> ParseResult<T>,
handler: &impl Handler<base::Error>,
) -> ParseResult<DelimitedTree<T>> {
self.current_frame.stop_at_significant();
let raw_token_tree = self
.current_frame
.token_provider
.token_stream()
.get(self.current_frame.current_index);
// move after the whole delimited list
self.current_frame.forward();
let expected = delimiter.opening_char();
let delimited_stream = if let Some(token_tree) = raw_token_tree {
match token_tree {
TokenTree::Delimited(delimited_tree) if delimited_tree.delimiter == delimiter => {
delimited_tree
}
found => {
let err = Error::UnexpectedSyntax(UnexpectedSyntax {
expected: SyntaxKind::Punctuation(expected),
found: Some(match found {
TokenTree::Token(token) => token.clone(),
TokenTree::Delimited(delimited_tree) => {
Token::Punctuation(delimited_tree.open.clone())
}
}),
});
handler.receive(err.clone());
return Err(err);
}
}
} else {
let err = Error::UnexpectedSyntax(UnexpectedSyntax {
expected: SyntaxKind::Punctuation(expected),
found: self.get_reading(None).into_token(),
});
handler.receive(err.clone());
return Err(err);
};
// creates a new frame
let new_frame = Frame {
token_provider: TokenProvider::Delimited(delimited_stream),
current_index: 0,
};
// pushes the current frame onto the stack and replaces the current frame with the new one
self.stack
.push(std::mem::replace(&mut self.current_frame, new_frame));
let open = delimited_stream.open.clone();
let tree = f(self);
// pops the current frame off the stack
let new_frame = self
.stack
.pop()
.expect("frame has been pushed on the stack before");
// the current frame must be at the end
if !self.current_frame.is_exhausted() {
let expected = self
.current_frame
.token_provider
.as_delimited()
.unwrap()
.delimiter
.closing_char();
let err = Error::UnexpectedSyntax(UnexpectedSyntax {
expected: SyntaxKind::Punctuation(expected),
found: self.peek().into_token(),
});
handler.receive(err.clone());
return Err(err);
}
let close_punctuation = self
.current_frame
.token_provider
.as_delimited()
.unwrap()
.close
.clone();
// replaces the current frame with the popped one
self.current_frame = new_frame;
Ok(DelimitedTree {
open,
tree,
close: close_punctuation,
})
}
/// Tries to parse the given function, and if it fails, resets the current index to the
/// `current_index` before the function call.
///
/// # Errors
/// - If the given function returns an error.
pub fn try_parse<T>(&mut self, f: impl FnOnce(&mut Self) -> ParseResult<T>) -> ParseResult<T> {
let current_index = self.current_frame.current_index;
let result = f(self);
if result.is_err() {
self.current_frame.current_index = current_index;
}
result
}
}
/// Represents a result of [`Parser::step_into()`] function.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct DelimitedTree<T> {
/// The opening delimiter.
pub open: Punctuation,
/// The tree inside the delimiter.
pub tree: ParseResult<T>,
/// The closing delimiter.
pub close: Punctuation,
}
/// Provides a way to iterate over a token stream.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, EnumAsInner)]
pub enum TokenProvider<'a> {
/// Iterating at the top level of the token stream.
TokenStream(&'a TokenStream),
/// Iterating inside a delimited token stream.
Delimited(&'a Delimited),
}
impl<'a> TokenProvider<'a> {
/// Gets the token stream of the current token provider.
#[must_use]
pub fn token_stream(&self) -> &'a TokenStream {
match self {
TokenProvider::TokenStream(token_stream) => token_stream,
TokenProvider::Delimited(delimited) => &delimited.token_stream,
}
}
}
/// Represents a single frame of the parser's stack, responsible for reading a token stream in
/// that given token stream level.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Frame<'a> {
token_provider: TokenProvider<'a>,
current_index: usize,
}
impl<'a> Frame<'a> {
/// Checks if the current [`Frame`] doesn't have any more significant [`TokenTree`]s to
/// parse.
#[must_use]
pub fn is_exhausted(&self) -> bool {
let token_stream = self.token_provider.token_stream();
for i in self.current_index..self.token_provider.token_stream().len() {
if !matches!(
token_stream.get(i),
Some(TokenTree::Token(
Token::WhiteSpaces(..) | Token::Comment(..)
))
) {
return false;
}
}
true
}
/// Checks if the current [`Frame`] has reached the end of the [`TokenStream`].
#[must_use]
pub fn is_end(&self) -> bool {
self.current_index >= self.token_provider.token_stream().len()
}
fn get_reading(&self, token: Option<&TokenTree>) -> Reading {
token.map_or_else(
|| match self.token_provider {
// end of file
TokenProvider::TokenStream(..) => Reading::Eof,
TokenProvider::Delimited(delimited) => {
Reading::DelimitedEnd(delimited.close.clone())
}
},
|token| match token {
TokenTree::Token(token) => Reading::Atomic(token.clone()),
TokenTree::Delimited(delimited) => Reading::IntoDelimited(delimited.open.clone()),
},
)
}
/// Returns a [`Token`] pointing by the `current_index` of the [`Frame`].
#[must_use]
pub fn peek(&self) -> Reading {
self.get_reading(self.token_provider.token_stream().get(self.current_index))
}
/// Returns a raw [`Token`] pointing by the `current_index` of the [`Frame`].
#[must_use]
pub fn peek_raw(&self) -> Option<&TokenTree> {
self.token_provider.token_stream().get(self.current_index)
}
/// Returns the next significant [`Token`] after the `current_index` of the [`Frame`].
#[must_use]
pub fn peek_significant(&self) -> Reading {
let mut index = self.current_index;
let token_stream = self.token_provider.token_stream();
while index < self.token_provider.token_stream().len() {
let token = self.get_reading(token_stream.get(index));
if !matches!(
token,
Reading::Atomic(Token::WhiteSpaces(..) | Token::Comment(..))
) {
return token;
}
index += 1;
}
match self.token_provider {
TokenProvider::TokenStream(..) => Reading::Eof,
TokenProvider::Delimited(delimited) => Reading::DelimitedEnd(delimited.close.clone()),
}
}
/// Returns a [`Token`] pointing by the `current_index` with the given index offset of the
/// [`Frame`].
///
/// # Returns
///
/// `None` if `offset + current_index` is less than zero or greter than
/// `self.token_provider.token_stream().len() + 1`
#[must_use]
pub fn peek_offset(&self, offset: isize) -> Option<Reading> {
let index = self.current_index.checked_add(offset.try_into().ok()?)?;
if index > self.token_provider.token_stream().len() + 1 {
return None;
}
Some(self.get_reading(self.token_provider.token_stream().get(index)))
}
/// Returns a [`Token`] pointing by the `current_index` of the [`Frame`] and increments the
/// `current_index` by 1.
pub fn next_token(&mut self) -> Reading {
let token = self.peek();
// increment the index
self.forward();
token
}
/// Forwards the `current_index` by 1 if the [`Frame`] is not exhausted.
pub fn forward(&mut self) {
// increment the index
if !self.is_end() {
self.current_index += 1;
}
}
/// Skips any insignificant [`Token`]s, returns the next significant [`Token`] found, and
/// increments the `current_index` afterward.
pub fn next_significant_token(&mut self) -> Reading {
let token = self.stop_at_significant();
// increment the index
self.forward();
token
}
/// Makes the current [`Frame`] point to the significant [`Token`] if currently not.
///
/// # Returns
/// The significant [`Token`] if found, otherwise `None`.
pub fn stop_at_significant(&mut self) -> Reading {
while !self.is_end() {
let token = self.peek();
if !matches!(
token,
Reading::Atomic(Token::WhiteSpaces(..) | Token::Comment(..))
) {
return token;
}
self.forward();
}
match self.token_provider {
TokenProvider::TokenStream(..) => Reading::Eof,
TokenProvider::Delimited(delimited) => Reading::DelimitedEnd(delimited.close.clone()),
}
}
/// Makes the current position stops at the first token that satisfies the predicate.
pub fn stop_at(&mut self, predicate: impl Fn(&Reading) -> bool) -> Reading {
while !self.is_end() {
let token = self.peek();
if predicate(&token) {
return token;
}
self.current_index += 1;
}
match self.token_provider {
TokenProvider::TokenStream(..) => Reading::Eof,
TokenProvider::Delimited(delimited) => Reading::DelimitedEnd(delimited.close.clone()),
}
}
/// Expects the next [`Token`] to be an [`Identifier`], and returns it.
///
/// # Errors
/// If the next [`Token`] is not an [`Identifier`].
pub fn parse_identifier(
&mut self,
handler: &impl Handler<base::Error>,
) -> ParseResult<Identifier> {
match self.next_significant_token() {
Reading::Atomic(Token::Identifier(ident)) => Ok(ident),
found => {
let err = Error::UnexpectedSyntax(UnexpectedSyntax {
expected: SyntaxKind::Identifier,
found: found.into_token(),
});
handler.receive(err.clone());
Err(err)
}
}
}
/// Expects the next [`Token`] to be an [`Numeric`], and returns it.
///
/// # Errors
/// If the next [`Token`] is not an [`Identifier`].
pub fn parse_numeric(&mut self, handler: &impl Handler<Error>) -> ParseResult<Numeric> {
match self.next_significant_token() {
Reading::Atomic(Token::Numeric(ident)) => Ok(ident),
found => {
let err = Error::UnexpectedSyntax(UnexpectedSyntax {
expected: SyntaxKind::Numeric,
found: found.into_token(),
});
handler.receive(err.clone());
Err(err)
}
}
}
/// Expects the next [`Token`] to be an [`StringLiteral`], and returns it.
///
/// # Errors
/// If the next [`Token`] is not an [`StringLiteral`].
pub fn parse_string_literal(
&mut self,
handler: &impl Handler<base::Error>,
) -> ParseResult<StringLiteral> {
match self.next_significant_token() {
Reading::Atomic(Token::StringLiteral(literal)) => Ok(literal),
found => {
let err = Error::UnexpectedSyntax(UnexpectedSyntax {
expected: SyntaxKind::StringLiteral,
found: found.into_token(),
});
handler.receive(err.clone());
Err(err)
}
}
}
/// Expects the next [`Token`] to be a [`Keyword`] of specific kind, and returns it.
///
/// # Errors
/// If the next [`Token`] is not a [`Keyword`] of specific kind.
pub fn parse_keyword(
&mut self,
expected: KeywordKind,
handler: &impl Handler<base::Error>,
) -> ParseResult<Keyword> {
match self.next_significant_token() {
Reading::Atomic(Token::Keyword(keyword_token)) if keyword_token.keyword == expected => {
Ok(keyword_token)
}
found => {
let err = Error::UnexpectedSyntax(UnexpectedSyntax {
expected: SyntaxKind::Keyword(expected),
found: found.into_token(),
});
handler.receive(err.clone());
Err(err)
}
}
}
/// Expects the next [`Token`] to be a [`Punctuation`] of specific kind, and returns it.
///
/// # Errors
/// If the next [`Token`] is not a [`Punctuation`] of specific kind.
pub fn parse_punctuation(
&mut self,
expected: char,
skip_insignificant: bool,
handler: &impl Handler<base::Error>,
) -> ParseResult<Punctuation> {
match if skip_insignificant {
self.next_significant_token()
} else {
self.next_token()
} {
Reading::Atomic(Token::Punctuation(punctuation_token))
if punctuation_token.punctuation == expected =>
{
Ok(punctuation_token)
}
found => {
let err = Error::UnexpectedSyntax(UnexpectedSyntax {
expected: SyntaxKind::Punctuation(expected),
found: found.into_token(),
});
handler.receive(err.clone());
Err(err)
}
}
}
}
/// Represents the read value of the [`Frame`].
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Reading {
/// A singular token.
Atomic(Token),
/// Found an openning delimiter token, which means that the parser can step into a new
/// delimited frame.
IntoDelimited(Punctuation),
/// Found a closing delimiter token, which means that the parser should step out of the current
/// delimited frame.
DelimitedEnd(Punctuation),
/// End of file.
Eof,
}
impl Reading {
/// Gets the read token inside the [`Reading`] as `Option<Token>`
///
/// # Returns
///
/// Returns `None` if the [`Reading`] is [`Reading::Eof`].
#[must_use]
pub fn into_token(self) -> Option<Token> {
match self {
Self::Atomic(token) => Some(token),
Self::IntoDelimited(punc) | Self::DelimitedEnd(punc) => Some(Token::Punctuation(punc)),
Self::Eof => None,
}
}
}