forked from argotorg/fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuse_tree.rs
More file actions
136 lines (118 loc) · 4.42 KB
/
use_tree.rs
File metadata and controls
136 lines (118 loc) · 4.42 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
use std::{cell::Cell, rc::Rc};
use crate::{ParseError, SyntaxKind, TextRange, parser::path::is_path_segment};
use super::{ErrProof, Parser, Recovery, define_scope, parse_list, token_stream::TokenStream};
define_scope! { pub(crate) UseTreeScope, UseTree }
impl super::Parse for UseTreeScope {
type Error = Recovery<ErrProof>;
fn parse<S: TokenStream>(&mut self, parser: &mut Parser<S>) -> Result<(), Self::Error> {
parser.set_newline_as_trivia(false);
if let Some(SyntaxKind::LBrace) = parser.current_kind() {
return parser.parse(UseTreeListScope::default());
}
let use_path_scope = UsePathScope::default();
parser.parse_or_recover(use_path_scope.clone())?;
let is_glob = use_path_scope.is_glob.get();
if parser.current_kind() == Some(SyntaxKind::AsKw) {
if is_glob {
parser.error_msg_on_current_token("can't use `as` with `*`");
}
if parser.current_kind() == Some(SyntaxKind::AsKw) {
parser.or_recover(|p| p.parse(UseTreeAliasScope::default()))?;
}
return Ok(());
}
if !parser.bump_if(SyntaxKind::Colon2) {
return Ok(());
}
if parser.current_kind() == Some(SyntaxKind::LBrace) {
if is_glob {
parser.error_msg_on_current_token("can't use `*` with `{}`");
}
parser.parse(UseTreeListScope::default())?;
}
Ok(())
}
}
define_scope! { UseTreeListScope, UseTreeList, (Comma, RBrace) }
impl super::Parse for UseTreeListScope {
type Error = Recovery<ErrProof>;
fn parse<S: TokenStream>(&mut self, parser: &mut Parser<S>) -> Result<(), Self::Error> {
parse_list(
parser,
true,
SyntaxKind::UseTreeList,
(SyntaxKind::LBrace, SyntaxKind::RBrace),
|parser| parser.parse(UseTreeScope::default()),
)
}
}
define_scope! {
pub(super) UsePathScope{ is_glob: Rc<Cell<bool>>},
UsePath,
(Colon2)
}
impl super::Parse for UsePathScope {
type Error = ParseError;
fn parse<S: TokenStream>(&mut self, parser: &mut Parser<S>) -> Result<(), Self::Error> {
parser.set_newline_as_trivia(false);
parser.parse(UsePathSegmentScope::default())?;
loop {
let is_path_segment = matches!(
parser.peek_n_non_trivia(2).as_slice(),
[SyntaxKind::Colon2, kind] if is_use_path_segment(*kind)
);
if is_path_segment {
if self.is_glob.get() {
parser.error_msg_on_current_token("can't specify path after `*`");
}
parser.bump_expected(SyntaxKind::Colon2);
self.is_glob
.set(parser.current_kind() == Some(SyntaxKind::Star));
parser.parse(UsePathSegmentScope::default())?;
} else {
break;
}
}
Ok(())
}
}
define_scope! { UsePathSegmentScope, UsePathSegment }
impl super::Parse for UsePathSegmentScope {
type Error = ParseError;
fn parse<S: TokenStream>(&mut self, parser: &mut Parser<S>) -> Result<(), Self::Error> {
match parser.current_kind() {
Some(kind) if is_use_path_segment(kind) => {
parser.bump();
}
_ => {
return Err(ParseError::Msg(
"expected identifier or `self`".into(),
TextRange::empty(parser.end_of_prev_token),
));
}
}
Ok(())
}
}
define_scope! { UseTreeAliasScope, UseTreeRename }
impl super::Parse for UseTreeAliasScope {
type Error = ParseError;
fn parse<S: TokenStream>(&mut self, parser: &mut Parser<S>) -> Result<(), Self::Error> {
parser.set_newline_as_trivia(false);
parser.bump_expected(SyntaxKind::AsKw);
match parser.current_kind() {
Some(SyntaxKind::Ident) => parser.bump_expected(SyntaxKind::Ident),
Some(SyntaxKind::Underscore) => parser.bump_expected(SyntaxKind::Underscore),
_ => {
return Err(ParseError::Msg(
"expected identifier or `_`".into(),
TextRange::empty(parser.current_pos),
));
}
};
Ok(())
}
}
fn is_use_path_segment(kind: SyntaxKind) -> bool {
is_path_segment(kind) || matches!(kind, SyntaxKind::Star)
}