forked from argotorg/fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.rs
More file actions
253 lines (224 loc) · 6.88 KB
/
Copy pathtypes.rs
File metadata and controls
253 lines (224 loc) · 6.88 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
use rowan::ast::{AstNode, support};
use super::{AstChildren, ast_node};
use crate::{SyntaxKind as SK, SyntaxToken};
ast_node! {
/// A type node.
/// If you want to match a specific kind of type, use `[Type::kind]`.
pub struct Type,
SK::PtrType
| SK::ModeType
| SK::PathType
| SK::SelfType
| SK::TupleType
| SK::ArrayType
| SK::NeverType
}
impl Type {
pub fn kind(&self) -> TypeKind {
match self.syntax().kind() {
SK::PtrType => TypeKind::Ptr(AstNode::cast(self.syntax().clone()).unwrap()),
SK::ModeType => TypeKind::Mode(AstNode::cast(self.syntax().clone()).unwrap()),
SK::PathType => TypeKind::Path(AstNode::cast(self.syntax().clone()).unwrap()),
SK::TupleType => TypeKind::Tuple(AstNode::cast(self.syntax().clone()).unwrap()),
SK::ArrayType => TypeKind::Array(AstNode::cast(self.syntax().clone()).unwrap()),
SK::NeverType => TypeKind::Never(AstNode::cast(self.syntax().clone()).unwrap()),
_ => unreachable!(),
}
}
}
ast_node! {
/// A mode type.
/// `ref T`, `mut T`, `own T`
pub struct ModeType,
SK::ModeType,
}
impl ModeType {
pub fn mode_token(&self) -> Option<SyntaxToken> {
support::token(self.syntax(), SK::MutKw)
.or_else(|| support::token(self.syntax(), SK::RefKw))
.or_else(|| support::token(self.syntax(), SK::OwnKw))
}
pub fn mode(&self) -> Option<TypeMode> {
let token = self.mode_token()?;
match token.kind() {
SK::MutKw => Some(TypeMode::Mut(token)),
SK::RefKw => Some(TypeMode::Ref(token)),
SK::OwnKw => Some(TypeMode::Own(token)),
_ => None,
}
}
pub fn inner(&self) -> Option<Type> {
support::child(self.syntax())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum TypeMode {
Mut(SyntaxToken),
Ref(SyntaxToken),
Own(SyntaxToken),
}
ast_node! {
/// A pointer type.
/// `*i32`
pub struct PtrType,
SK::PtrType,
}
impl PtrType {
/// Returns the `*` token.
pub fn star(&self) -> Option<SyntaxToken> {
support::token(self.syntax(), SK::Star)
}
/// Returns the type pointed to.
pub fn inner(&self) -> Option<Type> {
support::child(self.syntax())
}
}
ast_node! {
/// A path type.
/// `foo::Type<T, U + 2>`
pub struct PathType,
SK::PathType
}
impl PathType {
/// Returns the path of the type.
pub fn path(&self) -> Option<super::Path> {
support::child(self.syntax())
}
}
impl super::GenericArgsOwner for PathType {}
ast_node! {
/// A tuple type.
/// `(i32, foo::Bar)`
pub struct TupleType,
SK::TupleType,
IntoIterator<Item=Type>,
}
impl TupleType {
pub fn l_paren(&self) -> Option<SyntaxToken> {
support::token(self.syntax(), SK::LParen)
}
pub fn r_paren(&self) -> Option<SyntaxToken> {
support::token(self.syntax(), SK::RParen)
}
/// Returns the types in the tuple.
pub fn elem_tys(&self) -> AstChildren<Type> {
support::children(self.syntax())
}
}
ast_node! {
/// An array type.
/// `[i32; 4]`
pub struct ArrayType,
SK::ArrayType,
}
impl ArrayType {
pub fn l_bracket(&self) -> Option<SyntaxToken> {
support::token(self.syntax(), SK::LBracket)
}
pub fn r_bracket(&self) -> Option<SyntaxToken> {
support::token(self.syntax(), SK::LBracket)
}
/// Returns the type of the array elements.
pub fn elem_ty(&self) -> Option<Type> {
support::child(self.syntax())
}
/// Returns the length of the array.
pub fn len(&self) -> Option<super::Expr> {
support::child(self.syntax())
}
}
ast_node! {
pub struct NeverType,
SK::NeverType,
}
/// A specific kind of type.
#[derive(Debug, Clone, PartialEq, Eq, Hash, derive_more::From, derive_more::TryInto)]
pub enum TypeKind {
Ptr(PtrType),
Mode(ModeType),
Path(PathType),
Tuple(TupleType),
Array(ArrayType),
Never(NeverType),
}
#[cfg(test)]
mod tests {
use derive_more::TryIntoError;
use wasm_bindgen_test::wasm_bindgen_test;
use super::*;
use crate::{ast::prelude::*, lexer::Lexer, parser};
fn parse_type<T>(source: &str) -> T
where
T: TryFrom<TypeKind, Error = TryIntoError<TypeKind>> + std::fmt::Debug,
{
let lexer = Lexer::new(source);
let mut parser = parser::Parser::new(lexer, parser::RecoveryMode::Recover);
let _ = parser::type_::parse_type(&mut parser, None);
Type::cast(parser.finish_to_node().0)
.unwrap()
.kind()
.try_into()
.unwrap()
}
#[test]
#[wasm_bindgen_test]
fn ptr_type() {
let ptr_ty: PtrType = parse_type("*i32");
assert_eq!(ptr_ty.star().unwrap().text(), "*");
assert!(matches!(ptr_ty.inner().unwrap().kind(), TypeKind::Path(_)));
}
#[test]
#[wasm_bindgen_test]
fn path_type() {
let path_ty: PathType = parse_type("Foo::Bar<T, {U + 2}>");
for (i, segment) in path_ty.path().unwrap().segments().enumerate() {
match i {
0 => assert_eq!(segment.ident().unwrap().text(), "Foo"),
1 => {
assert_eq!(segment.ident().unwrap().text(), "Bar");
let generic_args = segment.generic_args().unwrap();
for (i, arg) in generic_args.iter().enumerate() {
match i {
0 => assert!(matches!(arg.kind(), crate::ast::GenericArgKind::Type(_))),
1 => {
assert!(matches!(arg.kind(), crate::ast::GenericArgKind::Const(_)))
}
_ => panic!(),
}
}
}
_ => panic!(),
}
}
}
#[test]
#[wasm_bindgen_test]
fn tuple_type() {
let tuple_ty: TupleType = parse_type("((i32, u32), foo::Bar, *usize");
for (i, ty) in tuple_ty.elem_tys().enumerate() {
match i {
0 => assert!(matches!(ty.kind(), TypeKind::Tuple(_))),
1 => assert!(matches!(ty.kind(), TypeKind::Path(_))),
2 => assert!(matches!(ty.kind(), TypeKind::Ptr(_))),
_ => panic!(),
}
}
}
#[test]
#[wasm_bindgen_test]
fn array_type() {
let array_ty: ArrayType = parse_type("[(i32, u32); 1]");
assert!(matches!(
array_ty.elem_ty().unwrap().kind(),
TypeKind::Tuple(_)
));
assert!(array_ty.len().is_some());
}
#[test]
#[wasm_bindgen_test]
fn mode_type() {
let mode_ty: ModeType = parse_type("ref Foo");
assert!(matches!(mode_ty.mode().unwrap(), TypeMode::Ref(_)));
assert!(matches!(mode_ty.inner().unwrap().kind(), TypeKind::Path(_)));
}
}