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
891 lines (781 loc) · 27.6 KB
/
Copy pathmod.rs
File metadata and controls
891 lines (781 loc) · 27.6 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
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
use std::{collections::VecDeque, convert::Infallible};
pub(crate) use item::ItemListScope;
use smallvec::SmallVec;
use self::token_stream::{BackTrackableTokenStream, LexicalToken, TokenStream};
use crate::{syntax_node::SyntaxNode, ExpectedKind, GreenNode, ParseError, SyntaxKind, TextRange};
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>,
parents: Vec<ScopeEntry>,
errors: Vec<ParseError>,
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,
end_of_prev_token: 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>>,
}
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(),
current_pos: rowan::TextSize::from(0),
end_of_prev_token: rowan::TextSize::from(0),
is_newline_trivia: true,
next_trivias: VecDeque::new(),
dry_run_states: Vec::new(),
}
}
/// 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)
}
pub fn set_scope_recovery_stack(&mut self, tokens: &[SyntaxKind]) {
let rec = self.scope_aux_recovery();
rec.clear();
rec.extend(tokens.iter().rev().copied());
}
pub fn pop_recovery_stack(&mut self) {
self.scope_aux_recovery().pop();
}
fn scope_aux_recovery(&mut self) -> &mut SmallVec<[SyntaxKind; 4]> {
&mut self.parents.last_mut().unwrap().aux_recovery_tokens
}
pub fn expect_and_pop_recovery_stack(&mut self) -> Result<(), Recovery<ErrProof>> {
let current = self.current_kind();
let r = if current.is_some() && self.scope_aux_recovery().contains(¤t.unwrap()) {
Ok(())
} else {
let pos = self.current_pos;
let (index, unexpected) = self.recover();
let proof = if unexpected.is_some() {
ErrProof(())
} else {
let err = ParseError::expected(self.scope_aux_recovery(), None, pos);
self.add_error(err)
};
self.allow_local_recovery(Err(Recovery(index, proof)))
};
self.pop_recovery_stack();
r
}
pub fn expect(
&mut self,
expected: &[SyntaxKind],
kind: Option<ExpectedKind>,
) -> Result<(), Recovery<ErrProof>> {
let current = self.current_kind();
let aux = self.scope_aux_recovery();
let truncate_to = aux.len();
aux.extend_from_slice(expected);
let res = if current.is_some() && aux.contains(¤t.unwrap()) {
Ok(())
} else {
let pos = self.current_pos;
let (index, unexpected) = self.recover();
let proof = if unexpected.is_some() {
ErrProof(())
} else {
self.add_error(ParseError::expected(expected, kind, pos))
};
self.pop_recovery_stack();
self.allow_local_recovery(Err(Recovery(index, proof)))
};
self.scope_aux_recovery().truncate(truncate_to);
res
}
/// 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,
{
let truncate_to = self.scope_aux_recovery().len();
self.scope_aux_recovery().extend_from_slice(recovery_tokens);
let r = f(self);
self.scope_aux_recovery().truncate(truncate_to);
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_cp<T, E>(
&mut self,
mut scope: T,
checkpoint: Option<Checkpoint>,
) -> Result<Checkpoint, E>
where
T: Parse<Error = E> + 'static,
E: Recoverable,
{
let checkpoint = self.enter(scope.clone(), checkpoint);
let start_checkpoint = self.checkpoint();
let res = scope.parse(self);
self.leave(checkpoint);
let res = self.allow_local_recovery(res);
res.map(|_| start_checkpoint)
}
pub fn parse<T, E>(&mut self, scope: T) -> Result<(), E>
where
T: Parse<Error = E> + 'static,
E: Recoverable,
{
self.parse_ok(scope).map(|_| ())
}
pub fn parse_ok<T, E>(&mut self, mut scope: T) -> Result<bool, E>
where
T: Parse<Error = E> + 'static,
E: Recoverable,
{
let checkpoint = self.enter(scope.clone(), None);
let res = scope.parse(self);
self.leave(checkpoint);
let ok = res.is_ok();
let res = self.allow_local_recovery(res);
res.map(|_| ok)
}
pub fn or_recover<F>(&mut self, f: F) -> Result<(), Recovery<ErrProof>>
where
F: FnOnce(&mut Self) -> Result<(), ParseError>,
{
if let Err(err) = f(self) {
let proof = self.add_error(err);
self.try_recover().map_err(|r| r.add_err_proof(proof))?;
}
Ok(())
}
#[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(ScopeEntry::new(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.
/// Returns `is_err` value for exited scope.
// NOTE: This method is limited to testing and internal usage.
pub fn leave(&mut self, checkpoint: Checkpoint) -> bool {
let scope = self.parents.pop().unwrap();
self.is_newline_trivia = scope.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.scope.syntax_kind().into());
self.builder.finish_node();
}
scope.is_err
}
pub fn add_error(&mut self, err: ParseError) -> ErrProof {
self.parents.last_mut().unwrap().is_err = true;
self.errors.push(err);
ErrProof(())
}
/// 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) -> Result<(), Recovery<ErrProof>> {
let proof = self.add_error(ParseError::Msg(
msg.into(),
TextRange::empty(self.end_of_prev_token),
));
self.try_recover().map_err(|r| r.add_err_proof(proof))
}
/// 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,
end_of_prev_token: self.end_of_prev_token,
err_num: self.errors.len(),
next_trivias: self.next_trivias.clone(),
});
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.end_of_prev_token = state.end_of_prev_token;
self.next_trivias = state.next_trivias;
r
}
/// Bumps the current token and its leading trivias.
pub fn bump(&mut self) {
// Bump leading trivias.
self.bump_trivias();
self.bump_raw();
self.end_of_prev_token = self.current_pos;
}
/// 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), "expected {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
}
}
pub fn find(
&mut self,
kind: SyntaxKind,
err: ExpectedKind,
) -> Result<bool, Recovery<ErrProof>> {
self.scope_aux_recovery().push(kind);
self.find_and_pop(kind, err)
}
pub fn find_and_pop(
&mut self,
kind: SyntaxKind,
err: ExpectedKind,
) -> Result<bool, Recovery<ErrProof>> {
debug_assert_eq!(self.scope_aux_recovery().last(), Some(&kind));
let r = if self.current_kind() == Some(kind) {
Ok(true)
} else {
let pos = self.current_pos;
let r = self.try_recover();
if self.current_kind() == Some(kind) {
Ok(true)
} else {
let proof = self.add_error(ParseError::expected(&[kind], Some(err), pos));
r.map(|_| false).map_err(|rec| rec.add_err_proof(proof))
}
};
self.scope_aux_recovery().pop();
r
}
pub fn try_recover(&mut self) -> Result<(), Recovery<()>> {
let (index, _) = self.recover();
self.allow_local_recovery(Err(Recovery(index, ())))
}
/// Consumes tokens until a recovery token is found, and reports an error on
/// any unexpected tokens.
/// Returns the index of the scope that matched the recovery token,
/// and the total string length of the unexpected tokens.
fn recover(&mut self) -> (Option<ScopeIndex>, Option<rowan::TextSize>) {
let mut unexpected = None;
let mut match_scope_index = None;
while let Some(kind) = self.current_kind() {
if let Some((scope_index, _)) = self
.parents
.iter()
.enumerate()
.rev()
.find(|(_i, scope)| scope.is_recovery_match(kind))
{
match_scope_index = Some(scope_index);
break;
}
if unexpected.is_none() {
if !self.parents.is_empty() {
self.bump_trivias();
}
unexpected = Some((self.current_pos, self.checkpoint()));
}
self.bump();
}
if let Some((start_pos, checkpoint)) = unexpected {
if !self.is_dry_run() {
self.builder
.start_node_at(checkpoint, SyntaxKind::Error.into());
self.builder.finish_node();
self.add_error(ParseError::Unexpected(
format!(
"unexpected syntax while parsing {}",
self.parents.last().unwrap().scope.syntax_kind().describe()
),
TextRange::new(start_pos, self.current_pos),
));
}
}
(
match_scope_index.map(ScopeIndex),
unexpected.map(|(start_pos, _)| start_pos),
)
}
fn allow_local_recovery<E: Recoverable>(&self, r: Result<(), E>) -> Result<(), E> {
match r {
Ok(()) => Ok(()),
Err(e) if e.is_local_recovery(self) => Ok(()),
_ => r,
}
}
fn is_current_scope(&self, index: ScopeIndex) -> bool {
index.0 + 1 == self.parents.len()
}
/// 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,
) -> Result<(), Recovery<ErrProof>> {
if !self.bump_if(expected) {
let proof = self.add_error(ParseError::Msg(
msg.into(),
TextRange::empty(self.current_pos),
));
self.try_recover().map_err(|r| r.add_err_proof(proof))
} else {
Ok(())
}
}
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()
}
}
/// Skip trivias (and newlines), then peek the next three tokens.
pub fn peek_three(&mut self) -> (Option<SyntaxKind>, Option<SyntaxKind>, Option<SyntaxKind>) {
self.stream.set_bt_point();
while let Some(next) = self.stream.peek().map(|tok| tok.syntax_kind()) {
if !(next.is_trivia() || next == SyntaxKind::Newline) {
break;
}
self.stream.next();
}
let tokens = (
self.stream.next().map(|t| t.syntax_kind()),
self.stream.next().map(|t| t.syntax_kind()),
self.stream.next().map(|t| t.syntax_kind()),
);
self.stream.backtrack();
tokens
}
/// Skip trivias, then peek the next two tokens.
pub fn peek_two(&mut self) -> (Option<SyntaxKind>, Option<SyntaxKind>) {
let (a, b, _) = self.peek_three();
(a, b)
}
/// Add the `msg` to the error list, at `current_pos`.
fn error(&mut self, msg: &str) -> ErrProof {
let pos = self.current_pos;
self.errors
.push(ParseError::Msg(msg.into(), TextRange::new(pos, pos)));
ErrProof(())
}
/// Add the `msg` to the error list, on `current_token()`.
/// Bumps trivias.
fn error_msg_on_current_token(&mut self, msg: &str) -> ErrProof {
self.bump_trivias();
let start = self.current_pos;
let end = if let Some(current_token) = self.current_token() {
start + current_token.text_size()
} else {
start
};
self.add_error(ParseError::Msg(msg.into(), TextRange::new(start, end)))
}
/// Wrap the current token in a `SyntaxKind::Error`, and add a
/// `ParseError::Unexpected`.
fn unexpected_token_error(&mut self, msg: String) {
let checkpoint = self.enter(ErrorScope::default(), None);
let start_pos = self.current_pos;
self.bump();
self.add_error(ParseError::Unexpected(
msg,
TextRange::new(start_pos, self.current_pos),
));
self.leave(checkpoint);
}
/// 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)
}
}
pub trait ParsingScope {
/// Returns the recovery method of the current scope.
fn recovery_tokens(&self) -> &[SyntaxKind];
fn syntax_kind(&self) -> SyntaxKind;
}
pub trait Parse: ParsingScope + Clone {
type Error;
fn parse<S: TokenStream>(&mut self, parser: &mut Parser<S>) -> Result<(), Self::Error>;
}
pub trait ParseInfalible: ParsingScope + Clone {
fn parse<S: TokenStream>(&mut self, parser: &mut Parser<S>);
}
#[derive(Debug, Copy, Clone)]
pub struct ScopeIndex(usize);
#[derive(Debug, Copy, Clone)]
pub struct Recovery<T>(Option<ScopeIndex>, T);
impl Recovery<()> {
pub fn add_err_proof(self, proof: ErrProof) -> Recovery<ErrProof> {
Recovery(self.0, proof)
}
}
#[derive(Debug)]
pub struct ErrProof(());
pub trait Recoverable {
fn is_local_recovery<S: TokenStream>(&self, _parser: &Parser<S>) -> bool {
false
}
}
impl Recoverable for ParseError {}
impl Recoverable for Infallible {}
impl<T> Recoverable for Recovery<T> {
fn is_local_recovery<S: TokenStream>(&self, parser: &Parser<S>) -> bool {
self.0
.as_ref()
.map(|i| parser.is_current_scope(*i))
.unwrap_or(false)
}
}
impl From<Infallible> for ErrProof {
fn from(_: Infallible) -> ErrProof {
ErrProof(())
}
}
impl From<Recovery<Infallible>> for Recovery<ErrProof> {
fn from(recovery: Recovery<Infallible>) -> Self {
Self(recovery.0, recovery.1.into())
}
}
struct DryRunState<S: TokenStream> {
/// The text position is the position when the dry run started.
pos: rowan::TextSize,
end_of_prev_token: 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>,
}
struct ScopeEntry {
scope: Box<dyn ParsingScope>,
is_newline_trivia: bool,
is_err: bool,
aux_recovery_tokens: SmallVec<[SyntaxKind; 4]>,
}
impl ScopeEntry {
fn new(scope: Box<dyn ParsingScope>, is_newline_trivia: bool) -> Self {
Self {
scope,
is_newline_trivia,
is_err: false,
aux_recovery_tokens: SmallVec::new(),
}
}
fn is_recovery_match(&self, kind: SyntaxKind) -> bool {
self.scope.recovery_tokens().contains(&kind) || self.aux_recovery_tokens.contains(&kind)
}
}
impl std::fmt::Debug for ScopeEntry {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ScopeEntry")
.field("scope", &self.scope.syntax_kind())
.field("is_newline_trivia", &self.is_newline_trivia)
.field("is_err", &self.is_err)
.field("aux_recovery_tokens", &self.aux_recovery_tokens)
.finish()
}
}
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 }
define_scope! { pub RootScope, Root }
macro_rules! define_scope {
(
$(#[$attrs: meta])*
$visibility: vis $scope_name: ident $({ $($field: ident: $ty: ty),* })?,
$kind: path
) => {
crate::parser::define_scope_struct! {$visibility $scope_name {$($($field: $ty), *)?}, $kind}
impl crate::parser::ParsingScope for $scope_name {
fn recovery_tokens(&self) -> &[crate::SyntaxKind] {
&[]
}
fn syntax_kind(&self) -> crate::SyntaxKind {
self.__inner.get()
}
}
};
(
$(#[$attrs: meta])*
$visibility: vis $scope_name: ident $({ $($field: ident: $ty: ty),* })?,
$kind: path,
($($recoveries: path), *)
) => {
crate::parser::define_scope_struct! {$visibility $scope_name {$($($field: $ty), *)?}, $kind}
impl crate::parser::ParsingScope for $scope_name {
fn recovery_tokens(&self) -> &[crate::SyntaxKind] {
lazy_static::lazy_static! {
pub(super) static ref RECOVERY_TOKENS: smallvec::SmallVec<[SyntaxKind; 4]> = {
#[allow(unused)]
use crate::SyntaxKind::*;
smallvec::SmallVec::from_slice(&[$($recoveries), *])
};
}
&RECOVERY_TOKENS
}
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;
/// Parse a comma-separated list of elements, with trailing commas allowed.
/// Panics if `parser.current_kind() != Some(brackets.0)`
fn parse_list<S: TokenStream, F>(
parser: &mut Parser<S>,
newline_delim: bool,
list_kind: SyntaxKind,
brackets: (SyntaxKind, SyntaxKind),
element: F,
) -> Result<(), Recovery<ErrProof>>
where
F: Fn(&mut Parser<S>) -> Result<(), Recovery<ErrProof>>,
{
parser.bump_expected(brackets.0);
let expected_closing_bracket = Some(ExpectedKind::ClosingBracket {
bracket: brackets.1,
parent: list_kind,
});
loop {
if parser.bump_if(brackets.1) {
return Ok(());
}
element(parser)?;
if parser.current_kind() != Some(SyntaxKind::Comma)
&& parser.current_kind() != Some(brackets.1)
{
// Recover gracefully if list elements are separated by newline instead of comma
let nt = parser.set_newline_as_trivia(false);
let newline = parser.current_kind() == Some(SyntaxKind::Newline) || {
parser.with_recovery_tokens(
|parser| {
let pos = parser.current_pos;
let (index, unexpected) = parser.recover();
if unexpected.is_none() {
parser.add_error(ParseError::expected(
&[brackets.1, SyntaxKind::Comma],
expected_closing_bracket,
pos,
));
}
parser.allow_local_recovery(Err(Recovery(index, ErrProof(()))))
},
&[SyntaxKind::Newline, SyntaxKind::Comma, brackets.1],
)?;
parser.current_kind() == Some(SyntaxKind::Newline)
};
parser.set_newline_as_trivia(nt);
if newline {
parser.add_error(ParseError::expected(
&[brackets.1, SyntaxKind::Comma],
expected_closing_bracket,
parser.current_pos,
));
if !newline_delim {
return Ok(());
}
} else {
parser.expect(&[brackets.1, SyntaxKind::Comma], expected_closing_bracket)?;
if !parser.bump_if(SyntaxKind::Comma) {
break;
}
}
} else if !parser.bump_if(SyntaxKind::Comma) {
parser.expect(&[brackets.1], expected_closing_bracket)?;
break;
}
}
parser.bump_expected(brackets.1);
Ok(())
}