forked from argotorg/fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
662 lines (576 loc) · 20.4 KB
/
Copy pathmod.rs
File metadata and controls
662 lines (576 loc) · 20.4 KB
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
use std::collections::VecDeque;
pub(crate) use item::ItemListScope;
use fxhash::{FxHashMap, FxHashSet};
use crate::{syntax_node::SyntaxNode, GreenNode, ParseError, SyntaxKind, TextRange};
use self::token_stream::{BackTrackableTokenStream, LexicalToken, TokenStream};
pub mod token_stream;
pub use pat::parse_pat;
pub mod attr;
pub mod expr;
pub mod func;
pub mod item;
pub mod lit;
pub mod param;
pub mod pat;
pub mod path;
pub mod stmt;
pub mod struct_;
pub mod type_;
pub mod use_tree;
mod expr_atom;
type Checkpoint = rowan::Checkpoint;
/// Parser to build a rowan syntax tree.
pub struct Parser<S: TokenStream> {
/// Token stream to parse.
stream: BackTrackableTokenStream<S>,
builder: rowan::GreenNodeBuilder<'static>,
/// The second element holds `is_newline_trivia` of the parent.
parents: Vec<(Box<dyn ParsingScope>, bool)>,
errors: Vec<ParseError>,
is_err: bool,
next_trivias: VecDeque<S::Token>,
/// if `is_newline_trivia` is `true`, `Newline` is also regarded as a trivia
/// token.
is_newline_trivia: bool,
current_pos: rowan::TextSize,
/// The dry run states which holds the each state of the parser when it
/// enters dry run mode.
dry_run_states: Vec<DryRunState<S>>,
auxiliary_recovery_set: FxHashMap<SyntaxKind, usize>,
}
impl<S: TokenStream> Parser<S> {
/// Create a parser with the given token stream.
pub fn new(stream: S) -> Self {
Self {
stream: BackTrackableTokenStream::new(stream),
builder: rowan::GreenNodeBuilder::new(),
parents: Vec::new(),
errors: Vec::new(),
is_err: false,
current_pos: rowan::TextSize::from(0),
is_newline_trivia: true,
next_trivias: VecDeque::new(),
dry_run_states: Vec::new(),
auxiliary_recovery_set: FxHashMap::default(),
}
}
/// Returns the current token of the parser.
pub fn current_token(&mut self) -> Option<S::Token> {
self.peek_non_trivia()
}
/// Returns the current non-trivia token kind of the parser.
pub fn current_kind(&mut self) -> Option<SyntaxKind> {
self.current_token().map(|tok| tok.syntax_kind())
}
/// Sets the newline kind as trivia if `is_trivia` is `true`. Otherwise, the
/// newline kind is not regarded as a trivia.
///
/// Returns previous value.
pub fn set_newline_as_trivia(&mut self, is_trivia: bool) -> bool {
std::mem::replace(&mut self.is_newline_trivia, is_trivia)
}
/// Finish the parsing and return the GreeNode.
pub fn finish(self) -> (GreenNode, Vec<ParseError>) {
debug_assert!(self.parents.is_empty());
debug_assert!(!self.is_dry_run());
(self.builder.finish(), self.errors)
}
/// Finish the parsing and return the SyntaxNode.
/// **NOTE:** This method is mainly used for testing.
pub fn finish_to_node(self) -> (SyntaxNode, Vec<ParseError>) {
let (green_node, errors) = self.finish();
(SyntaxNode::new_root(green_node), errors)
}
/// Adds the `recovery_tokens` as a temporary recovery token set.
/// These tokens are used as a recovery token set in addition to scope's
/// recovery token set.
///
/// This is useful when you want to specify auxiliary recovery tokens which
/// are valid only in a limited part of the scope.
pub fn with_recovery_tokens<F, R>(&mut self, f: F, recovery_tokens: &[SyntaxKind]) -> R
where
F: FnOnce(&mut Self) -> R,
{
for token in recovery_tokens {
self.add_recovery_token(*token);
}
let r = f(self);
for token in recovery_tokens {
self.remove_recovery_token(*token);
}
r
}
/// Adds `expected_tokens` as a temporary recovery token set, the invokes
/// the `f` closure. If the `f` closure fails to parse,
/// `expected_tokens` are also used as a recovery token set in addition to
/// scope's recovery token set.
///
/// If `current_token()` is not in `expected_tokens` after `f` returns, an
/// error is reported and try to recover with `expected_tokens` and scope's
/// recovery token set.
pub fn with_next_expected_tokens<F, R>(&mut self, f: F, expected_tokens: &[SyntaxKind]) -> R
where
F: FnOnce(&mut Self) -> R,
{
for token in expected_tokens {
self.add_recovery_token(*token);
}
let r = f(self);
if self.current_kind().is_some()
&& expected_tokens
.iter()
.all(|token| *token != self.current_kind().unwrap())
{
self.error_and_recover("unexpected token", None);
}
for token in expected_tokens {
self.remove_recovery_token(*token);
}
r
}
/// Invoke the scope to parse. The scope is wrapped up by the node specified
/// by the scope.
///
/// # Arguments
/// * If the `checkpoint` is `Some`, the marked branch is wrapped up by the
/// node.
/// * If the `checkpoint` is `None`, the current branch is wrapped up by the
/// node.
///
/// # Returns
/// * If the parsing succeeds, the first element of the return value is
/// `true`. otherwise, the first element is `false`.
/// * The second element of the return value is the checkpoint of the start
/// of the node.
pub fn parse<T>(&mut self, mut scope: T, checkpoint: Option<Checkpoint>) -> (bool, Checkpoint)
where
T: Parse + 'static,
{
let mut is_err = std::mem::take(&mut self.is_err);
let checkpoint = self.enter(scope.clone(), checkpoint);
let start_checkpoint = self.checkpoint();
scope.parse(self);
self.leave(checkpoint);
std::mem::swap(&mut self.is_err, &mut is_err);
(!is_err, start_checkpoint)
}
#[doc(hidden)]
/// Enter the scope and return the checkpoint. The checkpoint branch will be
/// wrapped up by the scope's node when [`leave`] is called.
// NOTE: This method is limited to testing and internal usage.
pub fn enter<T>(&mut self, scope: T, checkpoint: Option<Checkpoint>) -> Checkpoint
where
T: ParsingScope + 'static,
{
// Ensure the leading trivias are added to the parent node.
if !self.parents.is_empty() {
self.bump_trivias();
}
self.parents.push((Box::new(scope), self.is_newline_trivia));
// `is_newline_trivia` is always `true` when entering a scope.
self.is_newline_trivia = true;
checkpoint.unwrap_or_else(|| self.checkpoint())
}
#[doc(hidden)]
/// Leave the scope and wrap up the checkpoint by the scope's node.
// NOTE: This method is limited to testing and internal usage.
pub fn leave(&mut self, checkpoint: Checkpoint) {
let (scope, is_newline_trivia) = self.parents.pop().unwrap();
self.is_newline_trivia = is_newline_trivia;
// Ensure the trailing trivias are added to the current node if the current
// scope is the root.
if self.parents.is_empty() {
self.bump_trivias()
}
if !self.is_dry_run() {
self.builder
.start_node_at(checkpoint, scope.syntax_kind().into());
self.builder.finish_node();
}
}
/// Add `msg` as an error to the error list, then bumps consecutive tokens
/// until a token in the recovery set is found.
///
/// * If checkpoint is `Some`, the marked branch is wrapped up by an error
/// node.
/// * If checkpoint is `None`, the current branch is wrapped up by an error
/// node.
pub fn error_and_recover(&mut self, msg: &str, checkpoint: Option<Checkpoint>) {
let err_scope = self.error(msg);
let checkpoint = self.enter(err_scope, checkpoint);
self.recover();
self.leave(checkpoint);
}
/// Add `msg` as an error to the error list, then bumps consecutive tokens
/// until a `tok` is found or the end of the file is reached.
///
/// * If checkpoint is `Some`, the marked branch is wrapped up by an error
/// node.
/// * If checkpoint is `None`, the current branch is wrapped up by an error
/// node.
pub fn error_and_bump_until(
&mut self,
msg: &str,
checkpoint: Option<Checkpoint>,
kind: SyntaxKind,
) {
let err_scope = self.error(msg);
let checkpoint = self.enter(err_scope, checkpoint);
loop {
if self.current_kind() == Some(kind) || self.current_kind().is_none() {
break;
}
self.bump()
}
self.leave(checkpoint);
}
/// Runs the parser in the dry run mode.
///
/// Any changes to the parser state will be reverted.
pub fn dry_run<F, R>(&mut self, f: F) -> R
where
F: FnOnce(&mut Self) -> R,
{
// Enters the dry run mode.
self.stream.set_bt_point();
self.dry_run_states.push(DryRunState {
pos: self.current_pos,
err_num: self.errors.len(),
next_trivias: self.next_trivias.clone(),
auxiliary_recovery_set: self.auxiliary_recovery_set.clone(),
is_err: self.is_err,
});
let r = f(self);
// Leaves the dry run mode.
self.stream.backtrack();
let state = self.dry_run_states.pop().unwrap();
self.errors.truncate(state.err_num);
self.current_pos = state.pos;
self.next_trivias = state.next_trivias;
self.auxiliary_recovery_set = state.auxiliary_recovery_set;
self.is_err = state.is_err;
r
}
/// Bumps the current token and its leading trivias.
pub fn bump(&mut self) {
// Bump leading trivias.
self.bump_trivias();
self.bump_raw();
}
/// Bumps the current token if the current token is the `expected` kind.
///
/// # Panics
/// Panics If the current token is not the `expected` kind.
pub fn bump_expected(&mut self, expected: SyntaxKind) {
assert_eq!(self.current_kind(), Some(expected));
self.bump();
}
/// Bumps the current token if the current token is the `expected` kind.
/// Return `true` if the current token is the `expected` kind.
pub fn bump_if(&mut self, expected: SyntaxKind) -> bool {
if self.current_kind() == Some(expected) {
self.bump();
true
} else {
false
}
}
/// Proceeds the parser to the recovery token of the current scope.
pub fn recover(&mut self) {
let mut recovery_set: FxHashSet<SyntaxKind> = fxhash::FxHashSet::default();
let mut scope_index = self.parents.len() - 1;
loop {
match self
.parents
.get(scope_index)
.map(|scope| scope.0.recovery_method())
{
Some(RecoveryMethod::Inheritance(set)) => {
recovery_set.extend(set.iter());
if scope_index == 0 {
break;
}
scope_index -= 1;
}
Some(RecoveryMethod::Override(set)) => {
recovery_set.extend(set.iter());
break;
}
None => break,
}
}
let is_newline_trivia = self.set_newline_as_trivia(false);
self.auxiliary_recovery_set.insert(SyntaxKind::Newline, 1);
let mut open_brackets_in_error = FxHashMap::default();
while let Some(kind) = self.current_kind() {
if kind.is_open_bracket_kind() {
*open_brackets_in_error.entry(kind).or_insert(0) += 1;
}
if recovery_set.contains(&kind) || self.auxiliary_recovery_set.contains_key(&kind) {
if let Some(open_bracket) = kind.corresponding_open_bracket_kind() {
if open_brackets_in_error.get(&open_bracket).unwrap_or(&0) != &0 {
*open_brackets_in_error.get_mut(&open_bracket).unwrap() -= 1;
} else {
break;
}
} else {
break;
}
}
self.bump();
}
self.set_newline_as_trivia(is_newline_trivia);
}
/// Bumps the current token if the current token is the `expected` kind.
/// Otherwise, reports an error and proceeds the parser to the recovery
/// tokens.
pub fn bump_or_recover(
&mut self,
expected: SyntaxKind,
msg: &str,
checkpoint: Option<Checkpoint>,
) {
if !self.bump_if(expected) {
self.error_and_recover(msg, checkpoint);
}
}
fn checkpoint(&mut self) -> Checkpoint {
self.builder.checkpoint()
}
/// Bumps the current token and
/// current branch.
fn bump_raw(&mut self) {
let tok = match self.next_trivias.pop_front() {
Some(tok) => tok,
None => self.stream.next().unwrap(),
};
self.current_pos += rowan::TextSize::of(tok.text());
if !self.is_dry_run() {
self.builder.token(tok.syntax_kind().into(), tok.text());
}
}
fn bump_trivias(&mut self) {
// Bump trivias.
loop {
match self.peek_raw() {
Some(tok) if self.is_trivia(tok.syntax_kind()) => self.bump_raw(),
_ => break,
}
}
}
/// Peek the next non-trivia token.
fn peek_non_trivia(&mut self) -> Option<S::Token> {
if !self.is_newline_trivia {
for tok in &self.next_trivias {
if tok.syntax_kind() == SyntaxKind::Newline {
return Some(tok.clone());
}
}
}
while let Some(next) = self.stream.peek().map(|tok| tok.syntax_kind()) {
if self.is_trivia(next) {
let next = self.stream.next().unwrap();
self.next_trivias.push_back(next);
continue;
} else {
return self.stream.peek().cloned();
}
}
None
}
fn peek_raw(&mut self) -> Option<S::Token> {
if let Some(tok) = self.next_trivias.front() {
Some(tok.clone())
} else {
self.stream.peek().cloned()
}
}
/// Add the `msg` to the error list.
fn error(&mut self, msg: &str) -> ErrorScope {
self.bump_trivias();
self.is_err = true;
let start = self.current_pos;
let end = if let Some(current_token) = self.current_token() {
start + current_token.text_size()
} else {
start
};
let range = TextRange::new(start, end);
self.errors.push(ParseError {
range,
msg: msg.to_string(),
});
ErrorScope::default()
}
fn error_at_current_pos(&mut self, msg: &str) -> ErrorScope {
let pos = self.current_pos;
let range = TextRange::new(pos, pos);
self.errors.push(ParseError {
range,
msg: msg.to_string(),
});
ErrorScope::default()
}
/// Returns `true` if the parser is in the dry run mode.
fn is_dry_run(&self) -> bool {
!self.dry_run_states.is_empty()
}
fn is_trivia(&self, kind: SyntaxKind) -> bool {
kind.is_trivia() || (self.is_newline_trivia && kind == SyntaxKind::Newline)
}
fn add_recovery_token(&mut self, token: SyntaxKind) {
*self.auxiliary_recovery_set.entry(token).or_insert(0) += 1;
}
fn remove_recovery_token(&mut self, token: SyntaxKind) {
if let Some(num) = self.auxiliary_recovery_set.get_mut(&token) {
*num -= 1;
}
if self.auxiliary_recovery_set.get(&token) == Some(&0) {
self.auxiliary_recovery_set.remove(&token);
}
}
}
pub trait ParsingScope {
/// Returns the recovery method of the current scope.
fn recovery_method(&self) -> &RecoveryMethod;
fn syntax_kind(&self) -> SyntaxKind;
}
pub trait Parse: ParsingScope + Clone {
fn parse<S: TokenStream>(&mut self, parser: &mut Parser<S>);
}
struct DryRunState<S: TokenStream> {
/// The text position is the position when the dry run started.
pos: rowan::TextSize,
/// The number of errors when the dry run started.
err_num: usize,
/// The stored trivias when the dry run started.
next_trivias: VecDeque<S::Token>,
auxiliary_recovery_set: FxHashMap<SyntaxKind, usize>,
is_err: bool,
}
/// Represents the recovery method of the current scope.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RecoveryMethod {
/// Uses the recovery method of the parent scope and its own recovery set.
Inheritance(FxHashSet<SyntaxKind>),
/// The scope has its own recovery set and don't use parent scope's recovery
/// set.
Override(FxHashSet<SyntaxKind>),
}
impl RecoveryMethod {
fn inheritance(tokens: &[SyntaxKind]) -> Self {
Self::Inheritance(tokens.iter().copied().collect())
}
fn override_(tokens: &[SyntaxKind]) -> Self {
Self::Override(tokens.iter().copied().collect())
}
}
trait TextSize {
fn text_size(&self) -> rowan::TextSize;
}
impl<T> TextSize for T
where
T: LexicalToken,
{
fn text_size(&self) -> rowan::TextSize {
rowan::TextSize::of(self.text())
}
}
define_scope! {
ErrorScope,
Error,
Inheritance
}
define_scope! {
pub RootScope,
Root,
Override()
}
macro_rules! define_scope {
(
$(#[$attrs: meta])*
$visibility: vis $scope_name: ident $({ $($field: ident: $ty: ty),* })?,
$kind: path,
Inheritance $(($($recoveries: path), *))?
) => {
crate::parser::define_scope_struct! {$visibility $scope_name {$($($field: $ty), *)?}, $kind}
impl crate::parser::ParsingScope for $scope_name {
fn recovery_method(&self) -> &crate::parser::RecoveryMethod {
lazy_static::lazy_static! {
pub(super) static ref RECOVERY_METHOD: crate::parser::RecoveryMethod = {
#[allow(unused)]
use crate::SyntaxKind::*;
crate::parser::RecoveryMethod::inheritance(&[$($($recoveries), *)?])
};
}
&RECOVERY_METHOD
}
fn syntax_kind(&self) -> crate::SyntaxKind {
self.__inner.get()
}
}
};
(
$(#[$attrs: meta])*
$visibility: vis $scope_name: ident $({ $($field: ident: $ty: ty),* })?,
$kind: path,
Override($($recoveries: path), *)
) => {
crate::parser::define_scope_struct! {$visibility $scope_name {$($($field: $ty), *)?}, $kind}
impl crate::parser::ParsingScope for $scope_name {
fn recovery_method(&self) -> &crate::parser::RecoveryMethod {
lazy_static::lazy_static! {
pub(super) static ref RECOVERY_METHOD: crate::parser::RecoveryMethod = {
#[allow(unused)]
use crate::SyntaxKind::*;
crate::parser::RecoveryMethod::override_(&[$($recoveries), *])
};
}
&RECOVERY_METHOD
}
fn syntax_kind(&self) -> crate::SyntaxKind {
self.__inner.get()
}
}
};
}
macro_rules! define_scope_struct {
(
$(#[$attrs: meta])*
$visibility: vis $scope_name: ident { $($field: ident: $ty: ty),* },
$kind: path
) => {
$(#[$attrs])*
#[derive(Debug, Clone)]
$visibility struct $scope_name {
__inner: std::rc::Rc<std::cell::Cell<crate::SyntaxKind>>,
$($field: $ty),*
}
impl $scope_name {
#[allow(unused)]
$visibility fn new($($field: $ty),*) -> Self {
use crate::SyntaxKind::*;
Self {
$($field,)*
__inner: std::cell::Cell::new($kind).into(),
}
}
#[allow(unused)]
fn set_kind(&mut self, kind: crate::SyntaxKind) {
self.__inner.set(kind);
}
}
impl Default for $scope_name {
fn default() -> Self {
use crate::SyntaxKind::*;
Self {
__inner: std::cell::Cell::new($kind).into(),
$($field: Default::default()),*
}
}
}
};
}
use define_scope;
#[doc(hidden)]
use define_scope_struct;