-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmod.rs
348 lines (294 loc) · 10.3 KB
/
mod.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
use crate::{
error::{Error, ErrorHandler, Locatable, Location, ParseResult, SyntaxError},
interner::Interner,
symbol_table::{Graph, MaybeSym, NodeId, Scope},
token::{Token, TokenStream, TokenType},
};
#[cfg(feature = "logging")]
use log::{info, trace};
use stadium::Stadium;
use alloc::{format, vec::Vec};
use core::{convert::TryFrom, fmt, mem, num::NonZeroUsize};
mod ast;
mod expr;
mod stmt;
mod string_escapes;
#[cfg(test)]
mod tests;
mod types;
mod utils;
pub use ast::{
Alias, Ast, Attribute, Decorator, Enum, EnumVariant, ExtendBlock, FuncArg, Function, Import,
ImportDest, ImportExposure, Trait, TypeDecl, TypeMember, Visibility,
};
pub use expr::{
AssignmentType, BinaryOperand, ComparisonOperand, Expr, Expression, Float, Integer, Literal,
Rune, Sign, Text, UnaryOperand,
};
pub use stmt::{Statement, Stmt};
pub use types::{BuiltinType, Signedness, Type};
pub use utils::{CurrentFile, ItemPath, SyntaxTree};
use utils::{BinaryPrecedence, StackGuard};
// TODO: Make the parser a little more lax, it's kinda strict about whitespace
pub struct Parser<'src, 'expr, 'stmt> {
token_stream: TokenStream<'src>,
next: Option<Token<'src>>,
peek: Option<Token<'src>>,
error_handler: ErrorHandler,
stack_frames: StackGuard,
current_file: CurrentFile,
expr_arena: Stadium<'expr, Expression<'expr>>,
stmt_arena: Stadium<'stmt, Statement<'expr, 'stmt>>,
string_interner: Interner,
symbol_table: Graph<Scope, MaybeSym>,
module_scope: NodeId,
}
/// Initialization and high-level usage
impl<'src, 'expr, 'stmt> Parser<'src, 'expr, 'stmt> {
pub fn new(source: &'src str, current_file: CurrentFile, string_interner: Interner) -> Self {
let (token_stream, next, peek) = Self::lex(source);
let mut symbol_table = Graph::new();
let module_scope =
symbol_table.push_with_capacity(Scope::LocalScope(Vec::with_capacity(10)), 10);
Self {
token_stream,
next,
peek,
error_handler: ErrorHandler::new(),
stack_frames: StackGuard::new(),
current_file,
expr_arena: Stadium::with_capacity(NonZeroUsize::new(512).unwrap()),
stmt_arena: Stadium::with_capacity(NonZeroUsize::new(512).unwrap()),
string_interner,
symbol_table,
module_scope,
}
}
pub fn from_tokens(
token_stream: TokenStream<'src>,
next: Option<Token<'src>>,
peek: Option<Token<'src>>,
current_file: CurrentFile,
string_interner: Interner,
) -> Self {
let mut symbol_table = Graph::new();
let module_scope =
symbol_table.push_with_capacity(Scope::LocalScope(Vec::with_capacity(10)), 10);
Self {
token_stream,
next,
peek,
error_handler: ErrorHandler::new(),
stack_frames: StackGuard::new(),
current_file,
expr_arena: Stadium::with_capacity(NonZeroUsize::new(512).unwrap()),
stmt_arena: Stadium::with_capacity(NonZeroUsize::new(512).unwrap()),
string_interner,
symbol_table,
module_scope,
}
}
pub fn parse(
mut self,
) -> Result<
(
SyntaxTree<'expr, 'stmt>,
Interner,
ErrorHandler,
Graph<Scope, MaybeSym>,
NodeId,
),
ErrorHandler,
> {
#[cfg(feature = "logging")]
info!("Started parsing");
let mut ast = Vec::with_capacity(20);
while self.peek().is_ok() {
#[cfg(feature = "logging")]
trace!("Parsing top-level token");
match self.ast() {
Ok(node) => {
if let Some(node) = node {
ast.push(node);
}
}
Err(err) => {
self.error_handler.push_err(err);
if let Err(err) = self.stress_eat() {
self.error_handler.push_err(err);
#[cfg(feature = "logging")]
info!("Finished parsing unsuccessfully");
return Err(self.error_handler);
}
}
}
}
let ast = SyntaxTree {
ast,
__exprs: self.expr_arena,
__stmts: self.stmt_arena,
};
#[cfg(feature = "logging")]
info!("Finished parsing successfully");
Ok((
ast,
self.string_interner,
self.error_handler,
self.symbol_table,
self.module_scope,
))
}
pub fn lex(source: &'src str) -> (TokenStream<'src>, Option<Token<'src>>, Option<Token<'src>>) {
#[cfg(feature = "logging")]
info!("Started lexing");
let mut token_stream = TokenStream::new(source, true, true);
let next = None;
let peek = token_stream.next_token();
#[cfg(feature = "logging")]
info!("Finished lexing");
(token_stream, next, peek)
}
#[inline(always)]
pub fn error_handler_mut(&mut self) -> &mut ErrorHandler {
&mut self.error_handler
}
}
/// Utility functions
impl<'src, 'expr, 'stmt> Parser<'src, 'expr, 'stmt> {
#[inline(always)]
fn next(&mut self) -> ParseResult<Token<'src>> {
let mut next = self.token_stream.next_token();
mem::swap(&mut next, &mut self.peek);
self.next = next;
if let Some(next) = self.next {
self.current_file.advance(next.span().width());
}
next.ok_or_else(|| Locatable::new(Error::EndOfFile, self.current_file.eof()))
}
#[inline(always)]
fn peek(&self) -> ParseResult<Token<'src>> {
self.peek
.ok_or_else(|| Locatable::new(Error::EndOfFile, self.current_file.eof()))
}
/// Eats one of the `expected` token, ignoring (and consuming) any tokens included in `ignoring`
#[inline(always)]
fn eat<T>(&mut self, expected: TokenType, ignoring: T) -> ParseResult<Token<'src>>
where
T: AsRef<[TokenType]>,
{
let ignoring = ignoring.as_ref();
let mut token = self.next()?;
// Assert that the expected token is not ignored, as that's likely a dev error
debug_assert!(!ignoring.contains(&expected));
loop {
match token.ty() {
ty if ty == expected => return Ok(token),
ignored if ignoring.contains(&ignored) => {}
_ => {
return Err(Locatable::new(
Error::Syntax(SyntaxError::Generic(format!(
"Expected {:?}, got {:?}",
expected.to_str(),
token.source()
))),
Location::concrete(&token, self.current_file.file()),
));
}
}
token = self.next()?;
}
}
/// Eats one of the `expected` tokens, ignoring (and consuming) any tokens included in `ignoring`
#[inline(always)]
fn eat_of<T, E>(&mut self, expected: T, ignoring: E) -> ParseResult<Token<'src>>
where
T: AsRef<[TokenType]>,
E: AsRef<[TokenType]>,
{
let expected = expected.as_ref();
let ignoring = ignoring.as_ref();
let mut token = self.next()?;
// Assert that the two slices don't share any elements, as that's likely a dev error
debug_assert!(ignoring.iter().any(|i| !expected.contains(i)));
loop {
match token.ty() {
ty if expected.contains(&ty) => return Ok(token),
ignored if ignoring.contains(&ignored) => {}
_ => {
let expected = expected
.iter()
.map(|t| format!("{:?}", t.to_str()))
.collect::<Vec<_>>()
.join(", ");
return Err(Locatable::new(
Error::Syntax(SyntaxError::Generic(format!(
"Expected one of {:?}, got {:?}",
expected,
token.source()
))),
Location::concrete(&token, self.current_file.file()),
));
}
}
token = self.next()?;
}
}
#[inline(always)]
fn stress_eat(&mut self) -> ParseResult<()> {
const TOP_TOKENS: &[TokenType] = &[
TokenType::Function,
TokenType::Enum,
TokenType::AtSign,
TokenType::Exposed,
TokenType::Package,
TokenType::Trait,
TokenType::Type,
TokenType::Import,
];
while !TOP_TOKENS.contains(&self.peek()?.ty()) {
self.next()?;
}
Ok(())
}
#[inline(always)]
fn current_precedence(&self) -> usize {
self.peek
.map(|p| {
BinaryPrecedence::try_from(p.ty())
.map(|p| p.precedence())
.unwrap_or(0)
})
.unwrap_or(0)
}
#[inline(always)]
fn add_stack_frame(&self) -> ParseResult<StackGuard> {
// TODO: Find out what this number should be
#[cfg(debug_assertions)]
const MAX_DEPTH: usize = 50;
#[cfg(not(debug_assertions))]
const MAX_DEPTH: usize = 150;
let guard = self.stack_frames.clone();
let depth = guard.frames();
if depth > MAX_DEPTH {
Err(Locatable::new(
Error::Syntax(SyntaxError::RecursionLimit(depth, MAX_DEPTH)),
self.current_file.recursion(),
))
} else {
Ok(guard)
}
}
}
impl fmt::Debug for Parser<'_, '_, '_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Parser")
.field("token_stream", &self.token_stream)
.field("next", &self.next)
.field("peek", &self.peek)
.field("error_handler", &self.error_handler)
.field("stack_frames", &self.stack_frames)
.field("current_file", &self.current_file)
.field("string_interner", &self.string_interner)
.finish()
}
}