diff options
author | Dmitry <[email protected]> | 2020-08-14 19:32:05 +0100 |
---|---|---|
committer | Dmitry <[email protected]> | 2020-08-14 19:32:05 +0100 |
commit | 178c3e135a2a249692f7784712492e7884ae0c00 (patch) | |
tree | ac6b769dbf7162150caa0c1624786a4dd79ff3be /crates/syntax/src/ast | |
parent | 06ff8e6c760ff05f10e868b5d1f9d79e42fbb49c (diff) | |
parent | c2594daf2974dbd4ce3d9b7ec72481764abaceb5 (diff) |
Merge remote-tracking branch 'origin/master'
Diffstat (limited to 'crates/syntax/src/ast')
-rw-r--r-- | crates/syntax/src/ast/edit.rs | 650 | ||||
-rw-r--r-- | crates/syntax/src/ast/expr_ext.rs | 418 | ||||
-rw-r--r-- | crates/syntax/src/ast/generated.rs | 41 | ||||
-rw-r--r-- | crates/syntax/src/ast/generated/nodes.rs | 4067 | ||||
-rw-r--r-- | crates/syntax/src/ast/generated/tokens.rs | 91 | ||||
-rw-r--r-- | crates/syntax/src/ast/make.rs | 402 | ||||
-rw-r--r-- | crates/syntax/src/ast/node_ext.rs | 485 | ||||
-rw-r--r-- | crates/syntax/src/ast/token_ext.rs | 538 | ||||
-rw-r--r-- | crates/syntax/src/ast/traits.rs | 141 |
9 files changed, 6833 insertions, 0 deletions
diff --git a/crates/syntax/src/ast/edit.rs b/crates/syntax/src/ast/edit.rs new file mode 100644 index 000000000..190746e09 --- /dev/null +++ b/crates/syntax/src/ast/edit.rs | |||
@@ -0,0 +1,650 @@ | |||
1 | //! This module contains functions for editing syntax trees. As the trees are | ||
2 | //! immutable, all function here return a fresh copy of the tree, instead of | ||
3 | //! doing an in-place modification. | ||
4 | use std::{ | ||
5 | fmt, iter, | ||
6 | ops::{self, RangeInclusive}, | ||
7 | }; | ||
8 | |||
9 | use arrayvec::ArrayVec; | ||
10 | |||
11 | use crate::{ | ||
12 | algo::{self, neighbor, SyntaxRewriter}, | ||
13 | ast::{ | ||
14 | self, | ||
15 | make::{self, tokens}, | ||
16 | AstNode, TypeBoundsOwner, | ||
17 | }, | ||
18 | AstToken, Direction, InsertPosition, SmolStr, SyntaxElement, SyntaxKind, | ||
19 | SyntaxKind::{ATTR, COMMENT, WHITESPACE}, | ||
20 | SyntaxNode, SyntaxToken, T, | ||
21 | }; | ||
22 | |||
23 | impl ast::BinExpr { | ||
24 | #[must_use] | ||
25 | pub fn replace_op(&self, op: SyntaxKind) -> Option<ast::BinExpr> { | ||
26 | let op_node: SyntaxElement = self.op_details()?.0.into(); | ||
27 | let to_insert: Option<SyntaxElement> = Some(make::token(op).into()); | ||
28 | Some(self.replace_children(single_node(op_node), to_insert)) | ||
29 | } | ||
30 | } | ||
31 | |||
32 | impl ast::Fn { | ||
33 | #[must_use] | ||
34 | pub fn with_body(&self, body: ast::BlockExpr) -> ast::Fn { | ||
35 | let mut to_insert: ArrayVec<[SyntaxElement; 2]> = ArrayVec::new(); | ||
36 | let old_body_or_semi: SyntaxElement = if let Some(old_body) = self.body() { | ||
37 | old_body.syntax().clone().into() | ||
38 | } else if let Some(semi) = self.semicolon_token() { | ||
39 | to_insert.push(make::tokens::single_space().into()); | ||
40 | semi.into() | ||
41 | } else { | ||
42 | to_insert.push(make::tokens::single_space().into()); | ||
43 | to_insert.push(body.syntax().clone().into()); | ||
44 | return self.insert_children(InsertPosition::Last, to_insert); | ||
45 | }; | ||
46 | to_insert.push(body.syntax().clone().into()); | ||
47 | self.replace_children(single_node(old_body_or_semi), to_insert) | ||
48 | } | ||
49 | } | ||
50 | |||
51 | fn make_multiline<N>(node: N) -> N | ||
52 | where | ||
53 | N: AstNode + Clone, | ||
54 | { | ||
55 | let l_curly = match node.syntax().children_with_tokens().find(|it| it.kind() == T!['{']) { | ||
56 | Some(it) => it, | ||
57 | None => return node, | ||
58 | }; | ||
59 | let sibling = match l_curly.next_sibling_or_token() { | ||
60 | Some(it) => it, | ||
61 | None => return node, | ||
62 | }; | ||
63 | let existing_ws = match sibling.as_token() { | ||
64 | None => None, | ||
65 | Some(tok) if tok.kind() != WHITESPACE => None, | ||
66 | Some(ws) => { | ||
67 | if ws.text().contains('\n') { | ||
68 | return node; | ||
69 | } | ||
70 | Some(ws.clone()) | ||
71 | } | ||
72 | }; | ||
73 | |||
74 | let indent = leading_indent(node.syntax()).unwrap_or_default(); | ||
75 | let ws = tokens::WsBuilder::new(&format!("\n{}", indent)); | ||
76 | let to_insert = iter::once(ws.ws().into()); | ||
77 | match existing_ws { | ||
78 | None => node.insert_children(InsertPosition::After(l_curly), to_insert), | ||
79 | Some(ws) => node.replace_children(single_node(ws), to_insert), | ||
80 | } | ||
81 | } | ||
82 | |||
83 | impl ast::AssocItemList { | ||
84 | #[must_use] | ||
85 | pub fn append_items( | ||
86 | &self, | ||
87 | items: impl IntoIterator<Item = ast::AssocItem>, | ||
88 | ) -> ast::AssocItemList { | ||
89 | let mut res = self.clone(); | ||
90 | if !self.syntax().text().contains_char('\n') { | ||
91 | res = make_multiline(res); | ||
92 | } | ||
93 | items.into_iter().for_each(|it| res = res.append_item(it)); | ||
94 | res | ||
95 | } | ||
96 | |||
97 | #[must_use] | ||
98 | pub fn append_item(&self, item: ast::AssocItem) -> ast::AssocItemList { | ||
99 | let (indent, position) = match self.assoc_items().last() { | ||
100 | Some(it) => ( | ||
101 | leading_indent(it.syntax()).unwrap_or_default().to_string(), | ||
102 | InsertPosition::After(it.syntax().clone().into()), | ||
103 | ), | ||
104 | None => match self.l_curly_token() { | ||
105 | Some(it) => ( | ||
106 | " ".to_string() + &leading_indent(self.syntax()).unwrap_or_default(), | ||
107 | InsertPosition::After(it.into()), | ||
108 | ), | ||
109 | None => return self.clone(), | ||
110 | }, | ||
111 | }; | ||
112 | let ws = tokens::WsBuilder::new(&format!("\n{}", indent)); | ||
113 | let to_insert: ArrayVec<[SyntaxElement; 2]> = | ||
114 | [ws.ws().into(), item.syntax().clone().into()].into(); | ||
115 | self.insert_children(position, to_insert) | ||
116 | } | ||
117 | } | ||
118 | |||
119 | impl ast::RecordExprFieldList { | ||
120 | #[must_use] | ||
121 | pub fn append_field(&self, field: &ast::RecordExprField) -> ast::RecordExprFieldList { | ||
122 | self.insert_field(InsertPosition::Last, field) | ||
123 | } | ||
124 | |||
125 | #[must_use] | ||
126 | pub fn insert_field( | ||
127 | &self, | ||
128 | position: InsertPosition<&'_ ast::RecordExprField>, | ||
129 | field: &ast::RecordExprField, | ||
130 | ) -> ast::RecordExprFieldList { | ||
131 | let is_multiline = self.syntax().text().contains_char('\n'); | ||
132 | let ws; | ||
133 | let space = if is_multiline { | ||
134 | ws = tokens::WsBuilder::new(&format!( | ||
135 | "\n{} ", | ||
136 | leading_indent(self.syntax()).unwrap_or_default() | ||
137 | )); | ||
138 | ws.ws() | ||
139 | } else { | ||
140 | tokens::single_space() | ||
141 | }; | ||
142 | |||
143 | let mut to_insert: ArrayVec<[SyntaxElement; 4]> = ArrayVec::new(); | ||
144 | to_insert.push(space.into()); | ||
145 | to_insert.push(field.syntax().clone().into()); | ||
146 | to_insert.push(make::token(T![,]).into()); | ||
147 | |||
148 | macro_rules! after_l_curly { | ||
149 | () => {{ | ||
150 | let anchor = match self.l_curly_token() { | ||
151 | Some(it) => it.into(), | ||
152 | None => return self.clone(), | ||
153 | }; | ||
154 | InsertPosition::After(anchor) | ||
155 | }}; | ||
156 | } | ||
157 | |||
158 | macro_rules! after_field { | ||
159 | ($anchor:expr) => { | ||
160 | if let Some(comma) = $anchor | ||
161 | .syntax() | ||
162 | .siblings_with_tokens(Direction::Next) | ||
163 | .find(|it| it.kind() == T![,]) | ||
164 | { | ||
165 | InsertPosition::After(comma) | ||
166 | } else { | ||
167 | to_insert.insert(0, make::token(T![,]).into()); | ||
168 | InsertPosition::After($anchor.syntax().clone().into()) | ||
169 | } | ||
170 | }; | ||
171 | }; | ||
172 | |||
173 | let position = match position { | ||
174 | InsertPosition::First => after_l_curly!(), | ||
175 | InsertPosition::Last => { | ||
176 | if !is_multiline { | ||
177 | // don't insert comma before curly | ||
178 | to_insert.pop(); | ||
179 | } | ||
180 | match self.fields().last() { | ||
181 | Some(it) => after_field!(it), | ||
182 | None => after_l_curly!(), | ||
183 | } | ||
184 | } | ||
185 | InsertPosition::Before(anchor) => { | ||
186 | InsertPosition::Before(anchor.syntax().clone().into()) | ||
187 | } | ||
188 | InsertPosition::After(anchor) => after_field!(anchor), | ||
189 | }; | ||
190 | |||
191 | self.insert_children(position, to_insert) | ||
192 | } | ||
193 | } | ||
194 | |||
195 | impl ast::TypeAlias { | ||
196 | #[must_use] | ||
197 | pub fn remove_bounds(&self) -> ast::TypeAlias { | ||
198 | let colon = match self.colon_token() { | ||
199 | Some(it) => it, | ||
200 | None => return self.clone(), | ||
201 | }; | ||
202 | let end = match self.type_bound_list() { | ||
203 | Some(it) => it.syntax().clone().into(), | ||
204 | None => colon.clone().into(), | ||
205 | }; | ||
206 | self.replace_children(colon.into()..=end, iter::empty()) | ||
207 | } | ||
208 | } | ||
209 | |||
210 | impl ast::TypeParam { | ||
211 | #[must_use] | ||
212 | pub fn remove_bounds(&self) -> ast::TypeParam { | ||
213 | let colon = match self.colon_token() { | ||
214 | Some(it) => it, | ||
215 | None => return self.clone(), | ||
216 | }; | ||
217 | let end = match self.type_bound_list() { | ||
218 | Some(it) => it.syntax().clone().into(), | ||
219 | None => colon.clone().into(), | ||
220 | }; | ||
221 | self.replace_children(colon.into()..=end, iter::empty()) | ||
222 | } | ||
223 | } | ||
224 | |||
225 | impl ast::Path { | ||
226 | #[must_use] | ||
227 | pub fn with_segment(&self, segment: ast::PathSegment) -> ast::Path { | ||
228 | if let Some(old) = self.segment() { | ||
229 | return self.replace_children( | ||
230 | single_node(old.syntax().clone()), | ||
231 | iter::once(segment.syntax().clone().into()), | ||
232 | ); | ||
233 | } | ||
234 | self.clone() | ||
235 | } | ||
236 | } | ||
237 | |||
238 | impl ast::PathSegment { | ||
239 | #[must_use] | ||
240 | pub fn with_type_args(&self, type_args: ast::GenericArgList) -> ast::PathSegment { | ||
241 | self._with_type_args(type_args, false) | ||
242 | } | ||
243 | |||
244 | #[must_use] | ||
245 | pub fn with_turbo_fish(&self, type_args: ast::GenericArgList) -> ast::PathSegment { | ||
246 | self._with_type_args(type_args, true) | ||
247 | } | ||
248 | |||
249 | fn _with_type_args(&self, type_args: ast::GenericArgList, turbo: bool) -> ast::PathSegment { | ||
250 | if let Some(old) = self.generic_arg_list() { | ||
251 | return self.replace_children( | ||
252 | single_node(old.syntax().clone()), | ||
253 | iter::once(type_args.syntax().clone().into()), | ||
254 | ); | ||
255 | } | ||
256 | let mut to_insert: ArrayVec<[SyntaxElement; 2]> = ArrayVec::new(); | ||
257 | if turbo { | ||
258 | to_insert.push(make::token(T![::]).into()); | ||
259 | } | ||
260 | to_insert.push(type_args.syntax().clone().into()); | ||
261 | self.insert_children(InsertPosition::Last, to_insert) | ||
262 | } | ||
263 | } | ||
264 | |||
265 | impl ast::Use { | ||
266 | #[must_use] | ||
267 | pub fn with_use_tree(&self, use_tree: ast::UseTree) -> ast::Use { | ||
268 | if let Some(old) = self.use_tree() { | ||
269 | return self.replace_descendant(old, use_tree); | ||
270 | } | ||
271 | self.clone() | ||
272 | } | ||
273 | |||
274 | pub fn remove(&self) -> SyntaxRewriter<'static> { | ||
275 | let mut res = SyntaxRewriter::default(); | ||
276 | res.delete(self.syntax()); | ||
277 | let next_ws = self | ||
278 | .syntax() | ||
279 | .next_sibling_or_token() | ||
280 | .and_then(|it| it.into_token()) | ||
281 | .and_then(ast::Whitespace::cast); | ||
282 | if let Some(next_ws) = next_ws { | ||
283 | let ws_text = next_ws.syntax().text(); | ||
284 | if ws_text.starts_with('\n') { | ||
285 | let rest = &ws_text[1..]; | ||
286 | if rest.is_empty() { | ||
287 | res.delete(next_ws.syntax()) | ||
288 | } else { | ||
289 | res.replace(next_ws.syntax(), &make::tokens::whitespace(rest)); | ||
290 | } | ||
291 | } | ||
292 | } | ||
293 | res | ||
294 | } | ||
295 | } | ||
296 | |||
297 | impl ast::UseTree { | ||
298 | #[must_use] | ||
299 | pub fn with_path(&self, path: ast::Path) -> ast::UseTree { | ||
300 | if let Some(old) = self.path() { | ||
301 | return self.replace_descendant(old, path); | ||
302 | } | ||
303 | self.clone() | ||
304 | } | ||
305 | |||
306 | #[must_use] | ||
307 | pub fn with_use_tree_list(&self, use_tree_list: ast::UseTreeList) -> ast::UseTree { | ||
308 | if let Some(old) = self.use_tree_list() { | ||
309 | return self.replace_descendant(old, use_tree_list); | ||
310 | } | ||
311 | self.clone() | ||
312 | } | ||
313 | |||
314 | #[must_use] | ||
315 | pub fn split_prefix(&self, prefix: &ast::Path) -> ast::UseTree { | ||
316 | let suffix = if self.path().as_ref() == Some(prefix) && self.use_tree_list().is_none() { | ||
317 | make::path_unqualified(make::path_segment_self()) | ||
318 | } else { | ||
319 | match split_path_prefix(&prefix) { | ||
320 | Some(it) => it, | ||
321 | None => return self.clone(), | ||
322 | } | ||
323 | }; | ||
324 | |||
325 | let use_tree = make::use_tree( | ||
326 | suffix, | ||
327 | self.use_tree_list(), | ||
328 | self.rename(), | ||
329 | self.star_token().is_some(), | ||
330 | ); | ||
331 | let nested = make::use_tree_list(iter::once(use_tree)); | ||
332 | return make::use_tree(prefix.clone(), Some(nested), None, false); | ||
333 | |||
334 | fn split_path_prefix(prefix: &ast::Path) -> Option<ast::Path> { | ||
335 | let parent = prefix.parent_path()?; | ||
336 | let segment = parent.segment()?; | ||
337 | if algo::has_errors(segment.syntax()) { | ||
338 | return None; | ||
339 | } | ||
340 | let mut res = make::path_unqualified(segment); | ||
341 | for p in iter::successors(parent.parent_path(), |it| it.parent_path()) { | ||
342 | res = make::path_qualified(res, p.segment()?); | ||
343 | } | ||
344 | Some(res) | ||
345 | } | ||
346 | } | ||
347 | |||
348 | pub fn remove(&self) -> SyntaxRewriter<'static> { | ||
349 | let mut res = SyntaxRewriter::default(); | ||
350 | res.delete(self.syntax()); | ||
351 | for &dir in [Direction::Next, Direction::Prev].iter() { | ||
352 | if let Some(nb) = neighbor(self, dir) { | ||
353 | self.syntax() | ||
354 | .siblings_with_tokens(dir) | ||
355 | .skip(1) | ||
356 | .take_while(|it| it.as_node() != Some(nb.syntax())) | ||
357 | .for_each(|el| res.delete(&el)); | ||
358 | return res; | ||
359 | } | ||
360 | } | ||
361 | res | ||
362 | } | ||
363 | } | ||
364 | |||
365 | impl ast::MatchArmList { | ||
366 | #[must_use] | ||
367 | pub fn append_arms(&self, items: impl IntoIterator<Item = ast::MatchArm>) -> ast::MatchArmList { | ||
368 | let mut res = self.clone(); | ||
369 | res = res.strip_if_only_whitespace(); | ||
370 | if !res.syntax().text().contains_char('\n') { | ||
371 | res = make_multiline(res); | ||
372 | } | ||
373 | items.into_iter().for_each(|it| res = res.append_arm(it)); | ||
374 | res | ||
375 | } | ||
376 | |||
377 | fn strip_if_only_whitespace(&self) -> ast::MatchArmList { | ||
378 | let mut iter = self.syntax().children_with_tokens().skip_while(|it| it.kind() != T!['{']); | ||
379 | iter.next(); // Eat the curly | ||
380 | let mut inner = iter.take_while(|it| it.kind() != T!['}']); | ||
381 | if !inner.clone().all(|it| it.kind() == WHITESPACE) { | ||
382 | return self.clone(); | ||
383 | } | ||
384 | let start = match inner.next() { | ||
385 | Some(s) => s, | ||
386 | None => return self.clone(), | ||
387 | }; | ||
388 | let end = match inner.last() { | ||
389 | Some(s) => s, | ||
390 | None => start.clone(), | ||
391 | }; | ||
392 | self.replace_children(start..=end, &mut iter::empty()) | ||
393 | } | ||
394 | |||
395 | #[must_use] | ||
396 | pub fn remove_placeholder(&self) -> ast::MatchArmList { | ||
397 | let placeholder = | ||
398 | self.arms().find(|arm| matches!(arm.pat(), Some(ast::Pat::WildcardPat(_)))); | ||
399 | if let Some(placeholder) = placeholder { | ||
400 | self.remove_arm(&placeholder) | ||
401 | } else { | ||
402 | self.clone() | ||
403 | } | ||
404 | } | ||
405 | |||
406 | #[must_use] | ||
407 | fn remove_arm(&self, arm: &ast::MatchArm) -> ast::MatchArmList { | ||
408 | let start = arm.syntax().clone(); | ||
409 | let end = if let Some(comma) = start | ||
410 | .siblings_with_tokens(Direction::Next) | ||
411 | .skip(1) | ||
412 | .skip_while(|it| it.kind().is_trivia()) | ||
413 | .next() | ||
414 | .filter(|it| it.kind() == T![,]) | ||
415 | { | ||
416 | comma | ||
417 | } else { | ||
418 | start.clone().into() | ||
419 | }; | ||
420 | self.replace_children(start.into()..=end, None) | ||
421 | } | ||
422 | |||
423 | #[must_use] | ||
424 | pub fn append_arm(&self, item: ast::MatchArm) -> ast::MatchArmList { | ||
425 | let r_curly = match self.syntax().children_with_tokens().find(|it| it.kind() == T!['}']) { | ||
426 | Some(t) => t, | ||
427 | None => return self.clone(), | ||
428 | }; | ||
429 | let position = InsertPosition::Before(r_curly.into()); | ||
430 | let arm_ws = tokens::WsBuilder::new(" "); | ||
431 | let match_indent = &leading_indent(self.syntax()).unwrap_or_default(); | ||
432 | let match_ws = tokens::WsBuilder::new(&format!("\n{}", match_indent)); | ||
433 | let to_insert: ArrayVec<[SyntaxElement; 3]> = | ||
434 | [arm_ws.ws().into(), item.syntax().clone().into(), match_ws.ws().into()].into(); | ||
435 | self.insert_children(position, to_insert) | ||
436 | } | ||
437 | } | ||
438 | |||
439 | #[must_use] | ||
440 | pub fn remove_attrs_and_docs<N: ast::AttrsOwner>(node: &N) -> N { | ||
441 | N::cast(remove_attrs_and_docs_inner(node.syntax().clone())).unwrap() | ||
442 | } | ||
443 | |||
444 | fn remove_attrs_and_docs_inner(mut node: SyntaxNode) -> SyntaxNode { | ||
445 | while let Some(start) = | ||
446 | node.children_with_tokens().find(|it| it.kind() == ATTR || it.kind() == COMMENT) | ||
447 | { | ||
448 | let end = match &start.next_sibling_or_token() { | ||
449 | Some(el) if el.kind() == WHITESPACE => el.clone(), | ||
450 | Some(_) | None => start.clone(), | ||
451 | }; | ||
452 | node = algo::replace_children(&node, start..=end, &mut iter::empty()); | ||
453 | } | ||
454 | node | ||
455 | } | ||
456 | |||
457 | #[derive(Debug, Clone, Copy)] | ||
458 | pub struct IndentLevel(pub u8); | ||
459 | |||
460 | impl From<u8> for IndentLevel { | ||
461 | fn from(level: u8) -> IndentLevel { | ||
462 | IndentLevel(level) | ||
463 | } | ||
464 | } | ||
465 | |||
466 | impl fmt::Display for IndentLevel { | ||
467 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
468 | let spaces = " "; | ||
469 | let buf; | ||
470 | let len = self.0 as usize * 4; | ||
471 | let indent = if len <= spaces.len() { | ||
472 | &spaces[..len] | ||
473 | } else { | ||
474 | buf = iter::repeat(' ').take(len).collect::<String>(); | ||
475 | &buf | ||
476 | }; | ||
477 | fmt::Display::fmt(indent, f) | ||
478 | } | ||
479 | } | ||
480 | |||
481 | impl ops::Add<u8> for IndentLevel { | ||
482 | type Output = IndentLevel; | ||
483 | fn add(self, rhs: u8) -> IndentLevel { | ||
484 | IndentLevel(self.0 + rhs) | ||
485 | } | ||
486 | } | ||
487 | |||
488 | impl IndentLevel { | ||
489 | pub fn from_node(node: &SyntaxNode) -> IndentLevel { | ||
490 | let first_token = match node.first_token() { | ||
491 | Some(it) => it, | ||
492 | None => return IndentLevel(0), | ||
493 | }; | ||
494 | for ws in prev_tokens(first_token).filter_map(ast::Whitespace::cast) { | ||
495 | let text = ws.syntax().text(); | ||
496 | if let Some(pos) = text.rfind('\n') { | ||
497 | let level = text[pos + 1..].chars().count() / 4; | ||
498 | return IndentLevel(level as u8); | ||
499 | } | ||
500 | } | ||
501 | IndentLevel(0) | ||
502 | } | ||
503 | |||
504 | /// XXX: this intentionally doesn't change the indent of the very first token. | ||
505 | /// Ie, in something like | ||
506 | /// ``` | ||
507 | /// fn foo() { | ||
508 | /// 92 | ||
509 | /// } | ||
510 | /// ``` | ||
511 | /// if you indent the block, the `{` token would stay put. | ||
512 | fn increase_indent(self, node: SyntaxNode) -> SyntaxNode { | ||
513 | let mut rewriter = SyntaxRewriter::default(); | ||
514 | node.descendants_with_tokens() | ||
515 | .filter_map(|el| el.into_token()) | ||
516 | .filter_map(ast::Whitespace::cast) | ||
517 | .filter(|ws| { | ||
518 | let text = ws.syntax().text(); | ||
519 | text.contains('\n') | ||
520 | }) | ||
521 | .for_each(|ws| { | ||
522 | let new_ws = make::tokens::whitespace(&format!("{}{}", ws.syntax(), self,)); | ||
523 | rewriter.replace(ws.syntax(), &new_ws) | ||
524 | }); | ||
525 | rewriter.rewrite(&node) | ||
526 | } | ||
527 | |||
528 | fn decrease_indent(self, node: SyntaxNode) -> SyntaxNode { | ||
529 | let mut rewriter = SyntaxRewriter::default(); | ||
530 | node.descendants_with_tokens() | ||
531 | .filter_map(|el| el.into_token()) | ||
532 | .filter_map(ast::Whitespace::cast) | ||
533 | .filter(|ws| { | ||
534 | let text = ws.syntax().text(); | ||
535 | text.contains('\n') | ||
536 | }) | ||
537 | .for_each(|ws| { | ||
538 | let new_ws = make::tokens::whitespace( | ||
539 | &ws.syntax().text().replace(&format!("\n{}", self), "\n"), | ||
540 | ); | ||
541 | rewriter.replace(ws.syntax(), &new_ws) | ||
542 | }); | ||
543 | rewriter.rewrite(&node) | ||
544 | } | ||
545 | } | ||
546 | |||
547 | // FIXME: replace usages with IndentLevel above | ||
548 | fn leading_indent(node: &SyntaxNode) -> Option<SmolStr> { | ||
549 | for token in prev_tokens(node.first_token()?) { | ||
550 | if let Some(ws) = ast::Whitespace::cast(token.clone()) { | ||
551 | let ws_text = ws.text(); | ||
552 | if let Some(pos) = ws_text.rfind('\n') { | ||
553 | return Some(ws_text[pos + 1..].into()); | ||
554 | } | ||
555 | } | ||
556 | if token.text().contains('\n') { | ||
557 | break; | ||
558 | } | ||
559 | } | ||
560 | None | ||
561 | } | ||
562 | |||
563 | fn prev_tokens(token: SyntaxToken) -> impl Iterator<Item = SyntaxToken> { | ||
564 | iter::successors(Some(token), |token| token.prev_token()) | ||
565 | } | ||
566 | |||
567 | pub trait AstNodeEdit: AstNode + Clone + Sized { | ||
568 | #[must_use] | ||
569 | fn insert_children( | ||
570 | &self, | ||
571 | position: InsertPosition<SyntaxElement>, | ||
572 | to_insert: impl IntoIterator<Item = SyntaxElement>, | ||
573 | ) -> Self { | ||
574 | let new_syntax = algo::insert_children(self.syntax(), position, to_insert); | ||
575 | Self::cast(new_syntax).unwrap() | ||
576 | } | ||
577 | |||
578 | #[must_use] | ||
579 | fn replace_children( | ||
580 | &self, | ||
581 | to_replace: RangeInclusive<SyntaxElement>, | ||
582 | to_insert: impl IntoIterator<Item = SyntaxElement>, | ||
583 | ) -> Self { | ||
584 | let new_syntax = algo::replace_children(self.syntax(), to_replace, to_insert); | ||
585 | Self::cast(new_syntax).unwrap() | ||
586 | } | ||
587 | |||
588 | #[must_use] | ||
589 | fn replace_descendant<D: AstNode>(&self, old: D, new: D) -> Self { | ||
590 | self.replace_descendants(iter::once((old, new))) | ||
591 | } | ||
592 | |||
593 | #[must_use] | ||
594 | fn replace_descendants<D: AstNode>( | ||
595 | &self, | ||
596 | replacement_map: impl IntoIterator<Item = (D, D)>, | ||
597 | ) -> Self { | ||
598 | let mut rewriter = SyntaxRewriter::default(); | ||
599 | for (from, to) in replacement_map { | ||
600 | rewriter.replace(from.syntax(), to.syntax()) | ||
601 | } | ||
602 | rewriter.rewrite_ast(self) | ||
603 | } | ||
604 | fn indent_level(&self) -> IndentLevel { | ||
605 | IndentLevel::from_node(self.syntax()) | ||
606 | } | ||
607 | #[must_use] | ||
608 | fn indent(&self, level: IndentLevel) -> Self { | ||
609 | Self::cast(level.increase_indent(self.syntax().clone())).unwrap() | ||
610 | } | ||
611 | #[must_use] | ||
612 | fn dedent(&self, level: IndentLevel) -> Self { | ||
613 | Self::cast(level.decrease_indent(self.syntax().clone())).unwrap() | ||
614 | } | ||
615 | #[must_use] | ||
616 | fn reset_indent(&self) -> Self { | ||
617 | let level = IndentLevel::from_node(self.syntax()); | ||
618 | self.dedent(level) | ||
619 | } | ||
620 | } | ||
621 | |||
622 | impl<N: AstNode + Clone> AstNodeEdit for N {} | ||
623 | |||
624 | fn single_node(element: impl Into<SyntaxElement>) -> RangeInclusive<SyntaxElement> { | ||
625 | let element = element.into(); | ||
626 | element.clone()..=element | ||
627 | } | ||
628 | |||
629 | #[test] | ||
630 | fn test_increase_indent() { | ||
631 | let arm_list = { | ||
632 | let arm = make::match_arm(iter::once(make::wildcard_pat().into()), make::expr_unit()); | ||
633 | make::match_arm_list(vec![arm.clone(), arm]) | ||
634 | }; | ||
635 | assert_eq!( | ||
636 | arm_list.syntax().to_string(), | ||
637 | "{ | ||
638 | _ => (), | ||
639 | _ => (), | ||
640 | }" | ||
641 | ); | ||
642 | let indented = arm_list.indent(IndentLevel(2)); | ||
643 | assert_eq!( | ||
644 | indented.syntax().to_string(), | ||
645 | "{ | ||
646 | _ => (), | ||
647 | _ => (), | ||
648 | }" | ||
649 | ); | ||
650 | } | ||
diff --git a/crates/syntax/src/ast/expr_ext.rs b/crates/syntax/src/ast/expr_ext.rs new file mode 100644 index 000000000..f5ba87223 --- /dev/null +++ b/crates/syntax/src/ast/expr_ext.rs | |||
@@ -0,0 +1,418 @@ | |||
1 | //! Various extension methods to ast Expr Nodes, which are hard to code-generate. | ||
2 | |||
3 | use crate::{ | ||
4 | ast::{self, support, AstChildren, AstNode}, | ||
5 | SmolStr, | ||
6 | SyntaxKind::*, | ||
7 | SyntaxToken, T, | ||
8 | }; | ||
9 | |||
10 | impl ast::AttrsOwner for ast::Expr {} | ||
11 | |||
12 | impl ast::Expr { | ||
13 | pub fn is_block_like(&self) -> bool { | ||
14 | match self { | ||
15 | ast::Expr::IfExpr(_) | ||
16 | | ast::Expr::LoopExpr(_) | ||
17 | | ast::Expr::ForExpr(_) | ||
18 | | ast::Expr::WhileExpr(_) | ||
19 | | ast::Expr::BlockExpr(_) | ||
20 | | ast::Expr::MatchExpr(_) | ||
21 | | ast::Expr::EffectExpr(_) => true, | ||
22 | _ => false, | ||
23 | } | ||
24 | } | ||
25 | } | ||
26 | |||
27 | #[derive(Debug, Clone, PartialEq, Eq)] | ||
28 | pub enum ElseBranch { | ||
29 | Block(ast::BlockExpr), | ||
30 | IfExpr(ast::IfExpr), | ||
31 | } | ||
32 | |||
33 | impl ast::IfExpr { | ||
34 | pub fn then_branch(&self) -> Option<ast::BlockExpr> { | ||
35 | self.blocks().next() | ||
36 | } | ||
37 | pub fn else_branch(&self) -> Option<ElseBranch> { | ||
38 | let res = match self.blocks().nth(1) { | ||
39 | Some(block) => ElseBranch::Block(block), | ||
40 | None => { | ||
41 | let elif: ast::IfExpr = support::child(self.syntax())?; | ||
42 | ElseBranch::IfExpr(elif) | ||
43 | } | ||
44 | }; | ||
45 | Some(res) | ||
46 | } | ||
47 | |||
48 | pub fn blocks(&self) -> AstChildren<ast::BlockExpr> { | ||
49 | support::children(self.syntax()) | ||
50 | } | ||
51 | } | ||
52 | |||
53 | #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] | ||
54 | pub enum PrefixOp { | ||
55 | /// The `*` operator for dereferencing | ||
56 | Deref, | ||
57 | /// The `!` operator for logical inversion | ||
58 | Not, | ||
59 | /// The `-` operator for negation | ||
60 | Neg, | ||
61 | } | ||
62 | |||
63 | impl ast::PrefixExpr { | ||
64 | pub fn op_kind(&self) -> Option<PrefixOp> { | ||
65 | match self.op_token()?.kind() { | ||
66 | T![*] => Some(PrefixOp::Deref), | ||
67 | T![!] => Some(PrefixOp::Not), | ||
68 | T![-] => Some(PrefixOp::Neg), | ||
69 | _ => None, | ||
70 | } | ||
71 | } | ||
72 | |||
73 | pub fn op_token(&self) -> Option<SyntaxToken> { | ||
74 | self.syntax().first_child_or_token()?.into_token() | ||
75 | } | ||
76 | } | ||
77 | |||
78 | #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] | ||
79 | pub enum BinOp { | ||
80 | /// The `||` operator for boolean OR | ||
81 | BooleanOr, | ||
82 | /// The `&&` operator for boolean AND | ||
83 | BooleanAnd, | ||
84 | /// The `==` operator for equality testing | ||
85 | EqualityTest, | ||
86 | /// The `!=` operator for equality testing | ||
87 | NegatedEqualityTest, | ||
88 | /// The `<=` operator for lesser-equal testing | ||
89 | LesserEqualTest, | ||
90 | /// The `>=` operator for greater-equal testing | ||
91 | GreaterEqualTest, | ||
92 | /// The `<` operator for comparison | ||
93 | LesserTest, | ||
94 | /// The `>` operator for comparison | ||
95 | GreaterTest, | ||
96 | /// The `+` operator for addition | ||
97 | Addition, | ||
98 | /// The `*` operator for multiplication | ||
99 | Multiplication, | ||
100 | /// The `-` operator for subtraction | ||
101 | Subtraction, | ||
102 | /// The `/` operator for division | ||
103 | Division, | ||
104 | /// The `%` operator for remainder after division | ||
105 | Remainder, | ||
106 | /// The `<<` operator for left shift | ||
107 | LeftShift, | ||
108 | /// The `>>` operator for right shift | ||
109 | RightShift, | ||
110 | /// The `^` operator for bitwise XOR | ||
111 | BitwiseXor, | ||
112 | /// The `|` operator for bitwise OR | ||
113 | BitwiseOr, | ||
114 | /// The `&` operator for bitwise AND | ||
115 | BitwiseAnd, | ||
116 | /// The `=` operator for assignment | ||
117 | Assignment, | ||
118 | /// The `+=` operator for assignment after addition | ||
119 | AddAssign, | ||
120 | /// The `/=` operator for assignment after division | ||
121 | DivAssign, | ||
122 | /// The `*=` operator for assignment after multiplication | ||
123 | MulAssign, | ||
124 | /// The `%=` operator for assignment after remainders | ||
125 | RemAssign, | ||
126 | /// The `>>=` operator for assignment after shifting right | ||
127 | ShrAssign, | ||
128 | /// The `<<=` operator for assignment after shifting left | ||
129 | ShlAssign, | ||
130 | /// The `-=` operator for assignment after subtraction | ||
131 | SubAssign, | ||
132 | /// The `|=` operator for assignment after bitwise OR | ||
133 | BitOrAssign, | ||
134 | /// The `&=` operator for assignment after bitwise AND | ||
135 | BitAndAssign, | ||
136 | /// The `^=` operator for assignment after bitwise XOR | ||
137 | BitXorAssign, | ||
138 | } | ||
139 | |||
140 | impl BinOp { | ||
141 | pub fn is_assignment(self) -> bool { | ||
142 | match self { | ||
143 | BinOp::Assignment | ||
144 | | BinOp::AddAssign | ||
145 | | BinOp::DivAssign | ||
146 | | BinOp::MulAssign | ||
147 | | BinOp::RemAssign | ||
148 | | BinOp::ShrAssign | ||
149 | | BinOp::ShlAssign | ||
150 | | BinOp::SubAssign | ||
151 | | BinOp::BitOrAssign | ||
152 | | BinOp::BitAndAssign | ||
153 | | BinOp::BitXorAssign => true, | ||
154 | _ => false, | ||
155 | } | ||
156 | } | ||
157 | } | ||
158 | |||
159 | impl ast::BinExpr { | ||
160 | pub fn op_details(&self) -> Option<(SyntaxToken, BinOp)> { | ||
161 | self.syntax().children_with_tokens().filter_map(|it| it.into_token()).find_map(|c| { | ||
162 | let bin_op = match c.kind() { | ||
163 | T![||] => BinOp::BooleanOr, | ||
164 | T![&&] => BinOp::BooleanAnd, | ||
165 | T![==] => BinOp::EqualityTest, | ||
166 | T![!=] => BinOp::NegatedEqualityTest, | ||
167 | T![<=] => BinOp::LesserEqualTest, | ||
168 | T![>=] => BinOp::GreaterEqualTest, | ||
169 | T![<] => BinOp::LesserTest, | ||
170 | T![>] => BinOp::GreaterTest, | ||
171 | T![+] => BinOp::Addition, | ||
172 | T![*] => BinOp::Multiplication, | ||
173 | T![-] => BinOp::Subtraction, | ||
174 | T![/] => BinOp::Division, | ||
175 | T![%] => BinOp::Remainder, | ||
176 | T![<<] => BinOp::LeftShift, | ||
177 | T![>>] => BinOp::RightShift, | ||
178 | T![^] => BinOp::BitwiseXor, | ||
179 | T![|] => BinOp::BitwiseOr, | ||
180 | T![&] => BinOp::BitwiseAnd, | ||
181 | T![=] => BinOp::Assignment, | ||
182 | T![+=] => BinOp::AddAssign, | ||
183 | T![/=] => BinOp::DivAssign, | ||
184 | T![*=] => BinOp::MulAssign, | ||
185 | T![%=] => BinOp::RemAssign, | ||
186 | T![>>=] => BinOp::ShrAssign, | ||
187 | T![<<=] => BinOp::ShlAssign, | ||
188 | T![-=] => BinOp::SubAssign, | ||
189 | T![|=] => BinOp::BitOrAssign, | ||
190 | T![&=] => BinOp::BitAndAssign, | ||
191 | T![^=] => BinOp::BitXorAssign, | ||
192 | _ => return None, | ||
193 | }; | ||
194 | Some((c, bin_op)) | ||
195 | }) | ||
196 | } | ||
197 | |||
198 | pub fn op_kind(&self) -> Option<BinOp> { | ||
199 | self.op_details().map(|t| t.1) | ||
200 | } | ||
201 | |||
202 | pub fn op_token(&self) -> Option<SyntaxToken> { | ||
203 | self.op_details().map(|t| t.0) | ||
204 | } | ||
205 | |||
206 | pub fn lhs(&self) -> Option<ast::Expr> { | ||
207 | support::children(self.syntax()).next() | ||
208 | } | ||
209 | |||
210 | pub fn rhs(&self) -> Option<ast::Expr> { | ||
211 | support::children(self.syntax()).nth(1) | ||
212 | } | ||
213 | |||
214 | pub fn sub_exprs(&self) -> (Option<ast::Expr>, Option<ast::Expr>) { | ||
215 | let mut children = support::children(self.syntax()); | ||
216 | let first = children.next(); | ||
217 | let second = children.next(); | ||
218 | (first, second) | ||
219 | } | ||
220 | } | ||
221 | |||
222 | #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] | ||
223 | pub enum RangeOp { | ||
224 | /// `..` | ||
225 | Exclusive, | ||
226 | /// `..=` | ||
227 | Inclusive, | ||
228 | } | ||
229 | |||
230 | impl ast::RangeExpr { | ||
231 | fn op_details(&self) -> Option<(usize, SyntaxToken, RangeOp)> { | ||
232 | self.syntax().children_with_tokens().enumerate().find_map(|(ix, child)| { | ||
233 | let token = child.into_token()?; | ||
234 | let bin_op = match token.kind() { | ||
235 | T![..] => RangeOp::Exclusive, | ||
236 | T![..=] => RangeOp::Inclusive, | ||
237 | _ => return None, | ||
238 | }; | ||
239 | Some((ix, token, bin_op)) | ||
240 | }) | ||
241 | } | ||
242 | |||
243 | pub fn op_kind(&self) -> Option<RangeOp> { | ||
244 | self.op_details().map(|t| t.2) | ||
245 | } | ||
246 | |||
247 | pub fn op_token(&self) -> Option<SyntaxToken> { | ||
248 | self.op_details().map(|t| t.1) | ||
249 | } | ||
250 | |||
251 | pub fn start(&self) -> Option<ast::Expr> { | ||
252 | let op_ix = self.op_details()?.0; | ||
253 | self.syntax() | ||
254 | .children_with_tokens() | ||
255 | .take(op_ix) | ||
256 | .find_map(|it| ast::Expr::cast(it.into_node()?)) | ||
257 | } | ||
258 | |||
259 | pub fn end(&self) -> Option<ast::Expr> { | ||
260 | let op_ix = self.op_details()?.0; | ||
261 | self.syntax() | ||
262 | .children_with_tokens() | ||
263 | .skip(op_ix + 1) | ||
264 | .find_map(|it| ast::Expr::cast(it.into_node()?)) | ||
265 | } | ||
266 | } | ||
267 | |||
268 | impl ast::IndexExpr { | ||
269 | pub fn base(&self) -> Option<ast::Expr> { | ||
270 | support::children(self.syntax()).next() | ||
271 | } | ||
272 | pub fn index(&self) -> Option<ast::Expr> { | ||
273 | support::children(self.syntax()).nth(1) | ||
274 | } | ||
275 | } | ||
276 | |||
277 | pub enum ArrayExprKind { | ||
278 | Repeat { initializer: Option<ast::Expr>, repeat: Option<ast::Expr> }, | ||
279 | ElementList(AstChildren<ast::Expr>), | ||
280 | } | ||
281 | |||
282 | impl ast::ArrayExpr { | ||
283 | pub fn kind(&self) -> ArrayExprKind { | ||
284 | if self.is_repeat() { | ||
285 | ArrayExprKind::Repeat { | ||
286 | initializer: support::children(self.syntax()).next(), | ||
287 | repeat: support::children(self.syntax()).nth(1), | ||
288 | } | ||
289 | } else { | ||
290 | ArrayExprKind::ElementList(support::children(self.syntax())) | ||
291 | } | ||
292 | } | ||
293 | |||
294 | fn is_repeat(&self) -> bool { | ||
295 | self.syntax().children_with_tokens().any(|it| it.kind() == T![;]) | ||
296 | } | ||
297 | } | ||
298 | |||
299 | #[derive(Clone, Debug, PartialEq, Eq, Hash)] | ||
300 | pub enum LiteralKind { | ||
301 | String, | ||
302 | ByteString, | ||
303 | Char, | ||
304 | Byte, | ||
305 | IntNumber { suffix: Option<SmolStr> }, | ||
306 | FloatNumber { suffix: Option<SmolStr> }, | ||
307 | Bool(bool), | ||
308 | } | ||
309 | |||
310 | impl ast::Literal { | ||
311 | pub fn token(&self) -> SyntaxToken { | ||
312 | self.syntax() | ||
313 | .children_with_tokens() | ||
314 | .find(|e| e.kind() != ATTR && !e.kind().is_trivia()) | ||
315 | .and_then(|e| e.into_token()) | ||
316 | .unwrap() | ||
317 | } | ||
318 | |||
319 | fn find_suffix(text: &str, possible_suffixes: &[&str]) -> Option<SmolStr> { | ||
320 | possible_suffixes | ||
321 | .iter() | ||
322 | .find(|&suffix| text.ends_with(suffix)) | ||
323 | .map(|&suffix| SmolStr::new(suffix)) | ||
324 | } | ||
325 | |||
326 | pub fn kind(&self) -> LiteralKind { | ||
327 | const INT_SUFFIXES: [&str; 12] = [ | ||
328 | "u64", "u32", "u16", "u8", "usize", "isize", "i64", "i32", "i16", "i8", "u128", "i128", | ||
329 | ]; | ||
330 | const FLOAT_SUFFIXES: [&str; 2] = ["f32", "f64"]; | ||
331 | |||
332 | let token = self.token(); | ||
333 | |||
334 | match token.kind() { | ||
335 | INT_NUMBER => { | ||
336 | // FYI: there was a bug here previously, thus the if statement below is necessary. | ||
337 | // The lexer treats e.g. `1f64` as an integer literal. See | ||
338 | // https://github.com/rust-analyzer/rust-analyzer/issues/1592 | ||
339 | // and the comments on the linked PR. | ||
340 | |||
341 | let text = token.text(); | ||
342 | if let suffix @ Some(_) = Self::find_suffix(&text, &FLOAT_SUFFIXES) { | ||
343 | LiteralKind::FloatNumber { suffix } | ||
344 | } else { | ||
345 | LiteralKind::IntNumber { suffix: Self::find_suffix(&text, &INT_SUFFIXES) } | ||
346 | } | ||
347 | } | ||
348 | FLOAT_NUMBER => { | ||
349 | let text = token.text(); | ||
350 | LiteralKind::FloatNumber { suffix: Self::find_suffix(&text, &FLOAT_SUFFIXES) } | ||
351 | } | ||
352 | STRING | RAW_STRING => LiteralKind::String, | ||
353 | T![true] => LiteralKind::Bool(true), | ||
354 | T![false] => LiteralKind::Bool(false), | ||
355 | BYTE_STRING | RAW_BYTE_STRING => LiteralKind::ByteString, | ||
356 | CHAR => LiteralKind::Char, | ||
357 | BYTE => LiteralKind::Byte, | ||
358 | _ => unreachable!(), | ||
359 | } | ||
360 | } | ||
361 | } | ||
362 | |||
363 | #[derive(Debug, Clone, PartialEq, Eq)] | ||
364 | pub enum Effect { | ||
365 | Async(SyntaxToken), | ||
366 | Unsafe(SyntaxToken), | ||
367 | Try(SyntaxToken), | ||
368 | // Very much not an effect, but we stuff it into this node anyway | ||
369 | Label(ast::Label), | ||
370 | } | ||
371 | |||
372 | impl ast::EffectExpr { | ||
373 | pub fn effect(&self) -> Effect { | ||
374 | if let Some(token) = self.async_token() { | ||
375 | return Effect::Async(token); | ||
376 | } | ||
377 | if let Some(token) = self.unsafe_token() { | ||
378 | return Effect::Unsafe(token); | ||
379 | } | ||
380 | if let Some(token) = self.try_token() { | ||
381 | return Effect::Try(token); | ||
382 | } | ||
383 | if let Some(label) = self.label() { | ||
384 | return Effect::Label(label); | ||
385 | } | ||
386 | unreachable!("ast::EffectExpr without Effect") | ||
387 | } | ||
388 | } | ||
389 | |||
390 | impl ast::BlockExpr { | ||
391 | /// false if the block is an intrinsic part of the syntax and can't be | ||
392 | /// replaced with arbitrary expression. | ||
393 | /// | ||
394 | /// ```not_rust | ||
395 | /// fn foo() { not_stand_alone } | ||
396 | /// const FOO: () = { stand_alone }; | ||
397 | /// ``` | ||
398 | pub fn is_standalone(&self) -> bool { | ||
399 | let parent = match self.syntax().parent() { | ||
400 | Some(it) => it, | ||
401 | None => return true, | ||
402 | }; | ||
403 | !matches!(parent.kind(), FN | IF_EXPR | WHILE_EXPR | LOOP_EXPR | EFFECT_EXPR) | ||
404 | } | ||
405 | } | ||
406 | |||
407 | #[test] | ||
408 | fn test_literal_with_attr() { | ||
409 | let parse = ast::SourceFile::parse(r#"const _: &str = { #[attr] "Hello" };"#); | ||
410 | let lit = parse.tree().syntax().descendants().find_map(ast::Literal::cast).unwrap(); | ||
411 | assert_eq!(lit.token().text(), r#""Hello""#); | ||
412 | } | ||
413 | |||
414 | impl ast::RecordExprField { | ||
415 | pub fn parent_record_lit(&self) -> ast::RecordExpr { | ||
416 | self.syntax().ancestors().find_map(ast::RecordExpr::cast).unwrap() | ||
417 | } | ||
418 | } | ||
diff --git a/crates/syntax/src/ast/generated.rs b/crates/syntax/src/ast/generated.rs new file mode 100644 index 000000000..4a6f41ee7 --- /dev/null +++ b/crates/syntax/src/ast/generated.rs | |||
@@ -0,0 +1,41 @@ | |||
1 | //! This file is actually hand-written, but the submodules are indeed generated. | ||
2 | #[rustfmt::skip] | ||
3 | mod nodes; | ||
4 | #[rustfmt::skip] | ||
5 | mod tokens; | ||
6 | |||
7 | use crate::{ | ||
8 | AstNode, | ||
9 | SyntaxKind::{self, *}, | ||
10 | SyntaxNode, | ||
11 | }; | ||
12 | |||
13 | pub use {nodes::*, tokens::*}; | ||
14 | |||
15 | // Stmt is the only nested enum, so it's easier to just hand-write it | ||
16 | impl AstNode for Stmt { | ||
17 | fn can_cast(kind: SyntaxKind) -> bool { | ||
18 | match kind { | ||
19 | LET_STMT | EXPR_STMT => true, | ||
20 | _ => Item::can_cast(kind), | ||
21 | } | ||
22 | } | ||
23 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
24 | let res = match syntax.kind() { | ||
25 | LET_STMT => Stmt::LetStmt(LetStmt { syntax }), | ||
26 | EXPR_STMT => Stmt::ExprStmt(ExprStmt { syntax }), | ||
27 | _ => { | ||
28 | let item = Item::cast(syntax)?; | ||
29 | Stmt::Item(item) | ||
30 | } | ||
31 | }; | ||
32 | Some(res) | ||
33 | } | ||
34 | fn syntax(&self) -> &SyntaxNode { | ||
35 | match self { | ||
36 | Stmt::LetStmt(it) => &it.syntax, | ||
37 | Stmt::ExprStmt(it) => &it.syntax, | ||
38 | Stmt::Item(it) => it.syntax(), | ||
39 | } | ||
40 | } | ||
41 | } | ||
diff --git a/crates/syntax/src/ast/generated/nodes.rs b/crates/syntax/src/ast/generated/nodes.rs new file mode 100644 index 000000000..3d49309d1 --- /dev/null +++ b/crates/syntax/src/ast/generated/nodes.rs | |||
@@ -0,0 +1,4067 @@ | |||
1 | //! Generated file, do not edit by hand, see `xtask/src/codegen` | ||
2 | |||
3 | use crate::{ | ||
4 | ast::{self, support, AstChildren, AstNode}, | ||
5 | SyntaxKind::{self, *}, | ||
6 | SyntaxNode, SyntaxToken, T, | ||
7 | }; | ||
8 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
9 | pub struct Name { | ||
10 | pub(crate) syntax: SyntaxNode, | ||
11 | } | ||
12 | impl Name { | ||
13 | pub fn ident_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![ident]) } | ||
14 | } | ||
15 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
16 | pub struct NameRef { | ||
17 | pub(crate) syntax: SyntaxNode, | ||
18 | } | ||
19 | impl NameRef { | ||
20 | pub fn ident_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![ident]) } | ||
21 | } | ||
22 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
23 | pub struct Path { | ||
24 | pub(crate) syntax: SyntaxNode, | ||
25 | } | ||
26 | impl Path { | ||
27 | pub fn qualifier(&self) -> Option<Path> { support::child(&self.syntax) } | ||
28 | pub fn coloncolon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![::]) } | ||
29 | pub fn segment(&self) -> Option<PathSegment> { support::child(&self.syntax) } | ||
30 | } | ||
31 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
32 | pub struct PathSegment { | ||
33 | pub(crate) syntax: SyntaxNode, | ||
34 | } | ||
35 | impl PathSegment { | ||
36 | pub fn crate_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![crate]) } | ||
37 | pub fn self_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![self]) } | ||
38 | pub fn super_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![super]) } | ||
39 | pub fn coloncolon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![::]) } | ||
40 | pub fn name_ref(&self) -> Option<NameRef> { support::child(&self.syntax) } | ||
41 | pub fn generic_arg_list(&self) -> Option<GenericArgList> { support::child(&self.syntax) } | ||
42 | pub fn param_list(&self) -> Option<ParamList> { support::child(&self.syntax) } | ||
43 | pub fn ret_type(&self) -> Option<RetType> { support::child(&self.syntax) } | ||
44 | pub fn l_angle_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![<]) } | ||
45 | pub fn path_type(&self) -> Option<PathType> { support::child(&self.syntax) } | ||
46 | pub fn as_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![as]) } | ||
47 | pub fn r_angle_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![>]) } | ||
48 | } | ||
49 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
50 | pub struct GenericArgList { | ||
51 | pub(crate) syntax: SyntaxNode, | ||
52 | } | ||
53 | impl GenericArgList { | ||
54 | pub fn coloncolon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![::]) } | ||
55 | pub fn l_angle_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![<]) } | ||
56 | pub fn generic_args(&self) -> AstChildren<GenericArg> { support::children(&self.syntax) } | ||
57 | pub fn r_angle_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![>]) } | ||
58 | } | ||
59 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
60 | pub struct ParamList { | ||
61 | pub(crate) syntax: SyntaxNode, | ||
62 | } | ||
63 | impl ParamList { | ||
64 | pub fn l_paren_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['(']) } | ||
65 | pub fn self_param(&self) -> Option<SelfParam> { support::child(&self.syntax) } | ||
66 | pub fn comma_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![,]) } | ||
67 | pub fn params(&self) -> AstChildren<Param> { support::children(&self.syntax) } | ||
68 | pub fn r_paren_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![')']) } | ||
69 | } | ||
70 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
71 | pub struct RetType { | ||
72 | pub(crate) syntax: SyntaxNode, | ||
73 | } | ||
74 | impl RetType { | ||
75 | pub fn thin_arrow_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![->]) } | ||
76 | pub fn ty(&self) -> Option<Type> { support::child(&self.syntax) } | ||
77 | } | ||
78 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
79 | pub struct PathType { | ||
80 | pub(crate) syntax: SyntaxNode, | ||
81 | } | ||
82 | impl PathType { | ||
83 | pub fn path(&self) -> Option<Path> { support::child(&self.syntax) } | ||
84 | } | ||
85 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
86 | pub struct TypeArg { | ||
87 | pub(crate) syntax: SyntaxNode, | ||
88 | } | ||
89 | impl TypeArg { | ||
90 | pub fn ty(&self) -> Option<Type> { support::child(&self.syntax) } | ||
91 | } | ||
92 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
93 | pub struct AssocTypeArg { | ||
94 | pub(crate) syntax: SyntaxNode, | ||
95 | } | ||
96 | impl ast::TypeBoundsOwner for AssocTypeArg {} | ||
97 | impl AssocTypeArg { | ||
98 | pub fn name_ref(&self) -> Option<NameRef> { support::child(&self.syntax) } | ||
99 | pub fn eq_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![=]) } | ||
100 | pub fn ty(&self) -> Option<Type> { support::child(&self.syntax) } | ||
101 | } | ||
102 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
103 | pub struct LifetimeArg { | ||
104 | pub(crate) syntax: SyntaxNode, | ||
105 | } | ||
106 | impl LifetimeArg { | ||
107 | pub fn lifetime_token(&self) -> Option<SyntaxToken> { | ||
108 | support::token(&self.syntax, T![lifetime]) | ||
109 | } | ||
110 | } | ||
111 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
112 | pub struct ConstArg { | ||
113 | pub(crate) syntax: SyntaxNode, | ||
114 | } | ||
115 | impl ConstArg { | ||
116 | pub fn expr(&self) -> Option<Expr> { support::child(&self.syntax) } | ||
117 | } | ||
118 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
119 | pub struct TypeBoundList { | ||
120 | pub(crate) syntax: SyntaxNode, | ||
121 | } | ||
122 | impl TypeBoundList { | ||
123 | pub fn bounds(&self) -> AstChildren<TypeBound> { support::children(&self.syntax) } | ||
124 | } | ||
125 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
126 | pub struct MacroCall { | ||
127 | pub(crate) syntax: SyntaxNode, | ||
128 | } | ||
129 | impl ast::AttrsOwner for MacroCall {} | ||
130 | impl ast::NameOwner for MacroCall {} | ||
131 | impl MacroCall { | ||
132 | pub fn path(&self) -> Option<Path> { support::child(&self.syntax) } | ||
133 | pub fn excl_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![!]) } | ||
134 | pub fn token_tree(&self) -> Option<TokenTree> { support::child(&self.syntax) } | ||
135 | pub fn semicolon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![;]) } | ||
136 | } | ||
137 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
138 | pub struct Attr { | ||
139 | pub(crate) syntax: SyntaxNode, | ||
140 | } | ||
141 | impl Attr { | ||
142 | pub fn pound_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![#]) } | ||
143 | pub fn excl_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![!]) } | ||
144 | pub fn l_brack_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['[']) } | ||
145 | pub fn path(&self) -> Option<Path> { support::child(&self.syntax) } | ||
146 | pub fn eq_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![=]) } | ||
147 | pub fn literal(&self) -> Option<Literal> { support::child(&self.syntax) } | ||
148 | pub fn token_tree(&self) -> Option<TokenTree> { support::child(&self.syntax) } | ||
149 | pub fn r_brack_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![']']) } | ||
150 | } | ||
151 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
152 | pub struct TokenTree { | ||
153 | pub(crate) syntax: SyntaxNode, | ||
154 | } | ||
155 | impl TokenTree { | ||
156 | pub fn l_paren_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['(']) } | ||
157 | pub fn r_paren_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![')']) } | ||
158 | pub fn l_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['{']) } | ||
159 | pub fn r_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['}']) } | ||
160 | pub fn l_brack_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['[']) } | ||
161 | pub fn r_brack_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![']']) } | ||
162 | } | ||
163 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
164 | pub struct MacroItems { | ||
165 | pub(crate) syntax: SyntaxNode, | ||
166 | } | ||
167 | impl ast::ModuleItemOwner for MacroItems {} | ||
168 | impl MacroItems {} | ||
169 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
170 | pub struct MacroStmts { | ||
171 | pub(crate) syntax: SyntaxNode, | ||
172 | } | ||
173 | impl MacroStmts { | ||
174 | pub fn statements(&self) -> AstChildren<Stmt> { support::children(&self.syntax) } | ||
175 | pub fn expr(&self) -> Option<Expr> { support::child(&self.syntax) } | ||
176 | } | ||
177 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
178 | pub struct SourceFile { | ||
179 | pub(crate) syntax: SyntaxNode, | ||
180 | } | ||
181 | impl ast::AttrsOwner for SourceFile {} | ||
182 | impl ast::ModuleItemOwner for SourceFile {} | ||
183 | impl SourceFile { | ||
184 | pub fn shebang_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![shebang]) } | ||
185 | } | ||
186 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
187 | pub struct Const { | ||
188 | pub(crate) syntax: SyntaxNode, | ||
189 | } | ||
190 | impl ast::AttrsOwner for Const {} | ||
191 | impl ast::NameOwner for Const {} | ||
192 | impl ast::VisibilityOwner for Const {} | ||
193 | impl Const { | ||
194 | pub fn default_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![default]) } | ||
195 | pub fn const_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![const]) } | ||
196 | pub fn underscore_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![_]) } | ||
197 | pub fn colon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![:]) } | ||
198 | pub fn ty(&self) -> Option<Type> { support::child(&self.syntax) } | ||
199 | pub fn eq_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![=]) } | ||
200 | pub fn body(&self) -> Option<Expr> { support::child(&self.syntax) } | ||
201 | pub fn semicolon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![;]) } | ||
202 | } | ||
203 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
204 | pub struct Enum { | ||
205 | pub(crate) syntax: SyntaxNode, | ||
206 | } | ||
207 | impl ast::AttrsOwner for Enum {} | ||
208 | impl ast::NameOwner for Enum {} | ||
209 | impl ast::VisibilityOwner for Enum {} | ||
210 | impl ast::GenericParamsOwner for Enum {} | ||
211 | impl Enum { | ||
212 | pub fn enum_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![enum]) } | ||
213 | pub fn variant_list(&self) -> Option<VariantList> { support::child(&self.syntax) } | ||
214 | } | ||
215 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
216 | pub struct ExternBlock { | ||
217 | pub(crate) syntax: SyntaxNode, | ||
218 | } | ||
219 | impl ast::AttrsOwner for ExternBlock {} | ||
220 | impl ExternBlock { | ||
221 | pub fn abi(&self) -> Option<Abi> { support::child(&self.syntax) } | ||
222 | pub fn extern_item_list(&self) -> Option<ExternItemList> { support::child(&self.syntax) } | ||
223 | } | ||
224 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
225 | pub struct ExternCrate { | ||
226 | pub(crate) syntax: SyntaxNode, | ||
227 | } | ||
228 | impl ast::AttrsOwner for ExternCrate {} | ||
229 | impl ast::VisibilityOwner for ExternCrate {} | ||
230 | impl ExternCrate { | ||
231 | pub fn extern_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![extern]) } | ||
232 | pub fn crate_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![crate]) } | ||
233 | pub fn name_ref(&self) -> Option<NameRef> { support::child(&self.syntax) } | ||
234 | pub fn self_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![self]) } | ||
235 | pub fn rename(&self) -> Option<Rename> { support::child(&self.syntax) } | ||
236 | pub fn semicolon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![;]) } | ||
237 | } | ||
238 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
239 | pub struct Fn { | ||
240 | pub(crate) syntax: SyntaxNode, | ||
241 | } | ||
242 | impl ast::AttrsOwner for Fn {} | ||
243 | impl ast::NameOwner for Fn {} | ||
244 | impl ast::VisibilityOwner for Fn {} | ||
245 | impl ast::GenericParamsOwner for Fn {} | ||
246 | impl Fn { | ||
247 | pub fn default_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![default]) } | ||
248 | pub fn async_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![async]) } | ||
249 | pub fn const_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![const]) } | ||
250 | pub fn unsafe_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![unsafe]) } | ||
251 | pub fn abi(&self) -> Option<Abi> { support::child(&self.syntax) } | ||
252 | pub fn fn_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![fn]) } | ||
253 | pub fn param_list(&self) -> Option<ParamList> { support::child(&self.syntax) } | ||
254 | pub fn ret_type(&self) -> Option<RetType> { support::child(&self.syntax) } | ||
255 | pub fn body(&self) -> Option<BlockExpr> { support::child(&self.syntax) } | ||
256 | pub fn semicolon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![;]) } | ||
257 | } | ||
258 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
259 | pub struct Impl { | ||
260 | pub(crate) syntax: SyntaxNode, | ||
261 | } | ||
262 | impl ast::AttrsOwner for Impl {} | ||
263 | impl ast::VisibilityOwner for Impl {} | ||
264 | impl ast::GenericParamsOwner for Impl {} | ||
265 | impl Impl { | ||
266 | pub fn default_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![default]) } | ||
267 | pub fn unsafe_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![unsafe]) } | ||
268 | pub fn impl_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![impl]) } | ||
269 | pub fn const_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![const]) } | ||
270 | pub fn excl_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![!]) } | ||
271 | pub fn for_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![for]) } | ||
272 | pub fn assoc_item_list(&self) -> Option<AssocItemList> { support::child(&self.syntax) } | ||
273 | } | ||
274 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
275 | pub struct Module { | ||
276 | pub(crate) syntax: SyntaxNode, | ||
277 | } | ||
278 | impl ast::AttrsOwner for Module {} | ||
279 | impl ast::NameOwner for Module {} | ||
280 | impl ast::VisibilityOwner for Module {} | ||
281 | impl Module { | ||
282 | pub fn mod_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![mod]) } | ||
283 | pub fn item_list(&self) -> Option<ItemList> { support::child(&self.syntax) } | ||
284 | pub fn semicolon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![;]) } | ||
285 | } | ||
286 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
287 | pub struct Static { | ||
288 | pub(crate) syntax: SyntaxNode, | ||
289 | } | ||
290 | impl ast::AttrsOwner for Static {} | ||
291 | impl ast::NameOwner for Static {} | ||
292 | impl ast::VisibilityOwner for Static {} | ||
293 | impl Static { | ||
294 | pub fn static_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![static]) } | ||
295 | pub fn mut_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![mut]) } | ||
296 | pub fn colon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![:]) } | ||
297 | pub fn ty(&self) -> Option<Type> { support::child(&self.syntax) } | ||
298 | pub fn eq_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![=]) } | ||
299 | pub fn body(&self) -> Option<Expr> { support::child(&self.syntax) } | ||
300 | pub fn semicolon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![;]) } | ||
301 | } | ||
302 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
303 | pub struct Struct { | ||
304 | pub(crate) syntax: SyntaxNode, | ||
305 | } | ||
306 | impl ast::AttrsOwner for Struct {} | ||
307 | impl ast::NameOwner for Struct {} | ||
308 | impl ast::VisibilityOwner for Struct {} | ||
309 | impl ast::GenericParamsOwner for Struct {} | ||
310 | impl Struct { | ||
311 | pub fn struct_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![struct]) } | ||
312 | pub fn semicolon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![;]) } | ||
313 | pub fn field_list(&self) -> Option<FieldList> { support::child(&self.syntax) } | ||
314 | } | ||
315 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
316 | pub struct Trait { | ||
317 | pub(crate) syntax: SyntaxNode, | ||
318 | } | ||
319 | impl ast::AttrsOwner for Trait {} | ||
320 | impl ast::NameOwner for Trait {} | ||
321 | impl ast::VisibilityOwner for Trait {} | ||
322 | impl ast::GenericParamsOwner for Trait {} | ||
323 | impl ast::TypeBoundsOwner for Trait {} | ||
324 | impl Trait { | ||
325 | pub fn unsafe_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![unsafe]) } | ||
326 | pub fn auto_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![auto]) } | ||
327 | pub fn trait_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![trait]) } | ||
328 | pub fn assoc_item_list(&self) -> Option<AssocItemList> { support::child(&self.syntax) } | ||
329 | } | ||
330 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
331 | pub struct TypeAlias { | ||
332 | pub(crate) syntax: SyntaxNode, | ||
333 | } | ||
334 | impl ast::AttrsOwner for TypeAlias {} | ||
335 | impl ast::NameOwner for TypeAlias {} | ||
336 | impl ast::VisibilityOwner for TypeAlias {} | ||
337 | impl ast::GenericParamsOwner for TypeAlias {} | ||
338 | impl ast::TypeBoundsOwner for TypeAlias {} | ||
339 | impl TypeAlias { | ||
340 | pub fn default_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![default]) } | ||
341 | pub fn type_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![type]) } | ||
342 | pub fn eq_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![=]) } | ||
343 | pub fn ty(&self) -> Option<Type> { support::child(&self.syntax) } | ||
344 | pub fn semicolon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![;]) } | ||
345 | } | ||
346 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
347 | pub struct Union { | ||
348 | pub(crate) syntax: SyntaxNode, | ||
349 | } | ||
350 | impl ast::AttrsOwner for Union {} | ||
351 | impl ast::NameOwner for Union {} | ||
352 | impl ast::VisibilityOwner for Union {} | ||
353 | impl ast::GenericParamsOwner for Union {} | ||
354 | impl Union { | ||
355 | pub fn union_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![union]) } | ||
356 | pub fn record_field_list(&self) -> Option<RecordFieldList> { support::child(&self.syntax) } | ||
357 | } | ||
358 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
359 | pub struct Use { | ||
360 | pub(crate) syntax: SyntaxNode, | ||
361 | } | ||
362 | impl ast::AttrsOwner for Use {} | ||
363 | impl ast::VisibilityOwner for Use {} | ||
364 | impl Use { | ||
365 | pub fn use_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![use]) } | ||
366 | pub fn use_tree(&self) -> Option<UseTree> { support::child(&self.syntax) } | ||
367 | pub fn semicolon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![;]) } | ||
368 | } | ||
369 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
370 | pub struct Visibility { | ||
371 | pub(crate) syntax: SyntaxNode, | ||
372 | } | ||
373 | impl Visibility { | ||
374 | pub fn pub_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![pub]) } | ||
375 | pub fn l_paren_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['(']) } | ||
376 | pub fn super_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![super]) } | ||
377 | pub fn self_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![self]) } | ||
378 | pub fn crate_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![crate]) } | ||
379 | pub fn in_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![in]) } | ||
380 | pub fn path(&self) -> Option<Path> { support::child(&self.syntax) } | ||
381 | pub fn r_paren_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![')']) } | ||
382 | } | ||
383 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
384 | pub struct ItemList { | ||
385 | pub(crate) syntax: SyntaxNode, | ||
386 | } | ||
387 | impl ast::AttrsOwner for ItemList {} | ||
388 | impl ast::ModuleItemOwner for ItemList {} | ||
389 | impl ItemList { | ||
390 | pub fn l_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['{']) } | ||
391 | pub fn r_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['}']) } | ||
392 | } | ||
393 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
394 | pub struct Rename { | ||
395 | pub(crate) syntax: SyntaxNode, | ||
396 | } | ||
397 | impl ast::NameOwner for Rename {} | ||
398 | impl Rename { | ||
399 | pub fn as_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![as]) } | ||
400 | pub fn underscore_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![_]) } | ||
401 | } | ||
402 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
403 | pub struct UseTree { | ||
404 | pub(crate) syntax: SyntaxNode, | ||
405 | } | ||
406 | impl UseTree { | ||
407 | pub fn path(&self) -> Option<Path> { support::child(&self.syntax) } | ||
408 | pub fn coloncolon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![::]) } | ||
409 | pub fn star_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![*]) } | ||
410 | pub fn use_tree_list(&self) -> Option<UseTreeList> { support::child(&self.syntax) } | ||
411 | pub fn rename(&self) -> Option<Rename> { support::child(&self.syntax) } | ||
412 | } | ||
413 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
414 | pub struct UseTreeList { | ||
415 | pub(crate) syntax: SyntaxNode, | ||
416 | } | ||
417 | impl UseTreeList { | ||
418 | pub fn l_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['{']) } | ||
419 | pub fn use_trees(&self) -> AstChildren<UseTree> { support::children(&self.syntax) } | ||
420 | pub fn r_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['}']) } | ||
421 | } | ||
422 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
423 | pub struct Abi { | ||
424 | pub(crate) syntax: SyntaxNode, | ||
425 | } | ||
426 | impl Abi { | ||
427 | pub fn extern_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![extern]) } | ||
428 | } | ||
429 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
430 | pub struct GenericParamList { | ||
431 | pub(crate) syntax: SyntaxNode, | ||
432 | } | ||
433 | impl GenericParamList { | ||
434 | pub fn l_angle_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![<]) } | ||
435 | pub fn generic_params(&self) -> AstChildren<GenericParam> { support::children(&self.syntax) } | ||
436 | pub fn r_angle_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![>]) } | ||
437 | } | ||
438 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
439 | pub struct WhereClause { | ||
440 | pub(crate) syntax: SyntaxNode, | ||
441 | } | ||
442 | impl WhereClause { | ||
443 | pub fn where_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![where]) } | ||
444 | pub fn predicates(&self) -> AstChildren<WherePred> { support::children(&self.syntax) } | ||
445 | } | ||
446 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
447 | pub struct BlockExpr { | ||
448 | pub(crate) syntax: SyntaxNode, | ||
449 | } | ||
450 | impl ast::AttrsOwner for BlockExpr {} | ||
451 | impl BlockExpr { | ||
452 | pub fn l_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['{']) } | ||
453 | pub fn statements(&self) -> AstChildren<Stmt> { support::children(&self.syntax) } | ||
454 | pub fn expr(&self) -> Option<Expr> { support::child(&self.syntax) } | ||
455 | pub fn r_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['}']) } | ||
456 | } | ||
457 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
458 | pub struct SelfParam { | ||
459 | pub(crate) syntax: SyntaxNode, | ||
460 | } | ||
461 | impl ast::AttrsOwner for SelfParam {} | ||
462 | impl SelfParam { | ||
463 | pub fn amp_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![&]) } | ||
464 | pub fn lifetime_token(&self) -> Option<SyntaxToken> { | ||
465 | support::token(&self.syntax, T![lifetime]) | ||
466 | } | ||
467 | pub fn mut_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![mut]) } | ||
468 | pub fn self_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![self]) } | ||
469 | pub fn colon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![:]) } | ||
470 | pub fn ty(&self) -> Option<Type> { support::child(&self.syntax) } | ||
471 | } | ||
472 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
473 | pub struct Param { | ||
474 | pub(crate) syntax: SyntaxNode, | ||
475 | } | ||
476 | impl ast::AttrsOwner for Param {} | ||
477 | impl Param { | ||
478 | pub fn pat(&self) -> Option<Pat> { support::child(&self.syntax) } | ||
479 | pub fn colon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![:]) } | ||
480 | pub fn ty(&self) -> Option<Type> { support::child(&self.syntax) } | ||
481 | pub fn dotdotdot_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![...]) } | ||
482 | } | ||
483 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
484 | pub struct RecordFieldList { | ||
485 | pub(crate) syntax: SyntaxNode, | ||
486 | } | ||
487 | impl RecordFieldList { | ||
488 | pub fn l_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['{']) } | ||
489 | pub fn fields(&self) -> AstChildren<RecordField> { support::children(&self.syntax) } | ||
490 | pub fn r_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['}']) } | ||
491 | } | ||
492 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
493 | pub struct TupleFieldList { | ||
494 | pub(crate) syntax: SyntaxNode, | ||
495 | } | ||
496 | impl TupleFieldList { | ||
497 | pub fn l_paren_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['(']) } | ||
498 | pub fn fields(&self) -> AstChildren<TupleField> { support::children(&self.syntax) } | ||
499 | pub fn r_paren_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![')']) } | ||
500 | } | ||
501 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
502 | pub struct RecordField { | ||
503 | pub(crate) syntax: SyntaxNode, | ||
504 | } | ||
505 | impl ast::AttrsOwner for RecordField {} | ||
506 | impl ast::NameOwner for RecordField {} | ||
507 | impl ast::VisibilityOwner for RecordField {} | ||
508 | impl RecordField { | ||
509 | pub fn colon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![:]) } | ||
510 | pub fn ty(&self) -> Option<Type> { support::child(&self.syntax) } | ||
511 | } | ||
512 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
513 | pub struct TupleField { | ||
514 | pub(crate) syntax: SyntaxNode, | ||
515 | } | ||
516 | impl ast::AttrsOwner for TupleField {} | ||
517 | impl ast::VisibilityOwner for TupleField {} | ||
518 | impl TupleField { | ||
519 | pub fn ty(&self) -> Option<Type> { support::child(&self.syntax) } | ||
520 | } | ||
521 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
522 | pub struct VariantList { | ||
523 | pub(crate) syntax: SyntaxNode, | ||
524 | } | ||
525 | impl VariantList { | ||
526 | pub fn l_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['{']) } | ||
527 | pub fn variants(&self) -> AstChildren<Variant> { support::children(&self.syntax) } | ||
528 | pub fn r_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['}']) } | ||
529 | } | ||
530 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
531 | pub struct Variant { | ||
532 | pub(crate) syntax: SyntaxNode, | ||
533 | } | ||
534 | impl ast::AttrsOwner for Variant {} | ||
535 | impl ast::NameOwner for Variant {} | ||
536 | impl ast::VisibilityOwner for Variant {} | ||
537 | impl Variant { | ||
538 | pub fn field_list(&self) -> Option<FieldList> { support::child(&self.syntax) } | ||
539 | pub fn eq_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![=]) } | ||
540 | pub fn expr(&self) -> Option<Expr> { support::child(&self.syntax) } | ||
541 | } | ||
542 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
543 | pub struct AssocItemList { | ||
544 | pub(crate) syntax: SyntaxNode, | ||
545 | } | ||
546 | impl ast::AttrsOwner for AssocItemList {} | ||
547 | impl AssocItemList { | ||
548 | pub fn l_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['{']) } | ||
549 | pub fn assoc_items(&self) -> AstChildren<AssocItem> { support::children(&self.syntax) } | ||
550 | pub fn r_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['}']) } | ||
551 | } | ||
552 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
553 | pub struct ExternItemList { | ||
554 | pub(crate) syntax: SyntaxNode, | ||
555 | } | ||
556 | impl ast::AttrsOwner for ExternItemList {} | ||
557 | impl ExternItemList { | ||
558 | pub fn l_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['{']) } | ||
559 | pub fn extern_items(&self) -> AstChildren<ExternItem> { support::children(&self.syntax) } | ||
560 | pub fn r_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['}']) } | ||
561 | } | ||
562 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
563 | pub struct ConstParam { | ||
564 | pub(crate) syntax: SyntaxNode, | ||
565 | } | ||
566 | impl ast::AttrsOwner for ConstParam {} | ||
567 | impl ast::NameOwner for ConstParam {} | ||
568 | impl ConstParam { | ||
569 | pub fn const_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![const]) } | ||
570 | pub fn colon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![:]) } | ||
571 | pub fn ty(&self) -> Option<Type> { support::child(&self.syntax) } | ||
572 | pub fn eq_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![=]) } | ||
573 | pub fn default_val(&self) -> Option<Expr> { support::child(&self.syntax) } | ||
574 | } | ||
575 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
576 | pub struct LifetimeParam { | ||
577 | pub(crate) syntax: SyntaxNode, | ||
578 | } | ||
579 | impl ast::AttrsOwner for LifetimeParam {} | ||
580 | impl ast::TypeBoundsOwner for LifetimeParam {} | ||
581 | impl LifetimeParam { | ||
582 | pub fn lifetime_token(&self) -> Option<SyntaxToken> { | ||
583 | support::token(&self.syntax, T![lifetime]) | ||
584 | } | ||
585 | } | ||
586 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
587 | pub struct TypeParam { | ||
588 | pub(crate) syntax: SyntaxNode, | ||
589 | } | ||
590 | impl ast::AttrsOwner for TypeParam {} | ||
591 | impl ast::NameOwner for TypeParam {} | ||
592 | impl ast::TypeBoundsOwner for TypeParam {} | ||
593 | impl TypeParam { | ||
594 | pub fn eq_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![=]) } | ||
595 | pub fn default_type(&self) -> Option<Type> { support::child(&self.syntax) } | ||
596 | } | ||
597 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
598 | pub struct WherePred { | ||
599 | pub(crate) syntax: SyntaxNode, | ||
600 | } | ||
601 | impl ast::TypeBoundsOwner for WherePred {} | ||
602 | impl WherePred { | ||
603 | pub fn for_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![for]) } | ||
604 | pub fn generic_param_list(&self) -> Option<GenericParamList> { support::child(&self.syntax) } | ||
605 | pub fn lifetime_token(&self) -> Option<SyntaxToken> { | ||
606 | support::token(&self.syntax, T![lifetime]) | ||
607 | } | ||
608 | pub fn ty(&self) -> Option<Type> { support::child(&self.syntax) } | ||
609 | } | ||
610 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
611 | pub struct Literal { | ||
612 | pub(crate) syntax: SyntaxNode, | ||
613 | } | ||
614 | impl ast::AttrsOwner for Literal {} | ||
615 | impl Literal {} | ||
616 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
617 | pub struct ExprStmt { | ||
618 | pub(crate) syntax: SyntaxNode, | ||
619 | } | ||
620 | impl ast::AttrsOwner for ExprStmt {} | ||
621 | impl ExprStmt { | ||
622 | pub fn expr(&self) -> Option<Expr> { support::child(&self.syntax) } | ||
623 | pub fn semicolon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![;]) } | ||
624 | } | ||
625 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
626 | pub struct LetStmt { | ||
627 | pub(crate) syntax: SyntaxNode, | ||
628 | } | ||
629 | impl ast::AttrsOwner for LetStmt {} | ||
630 | impl LetStmt { | ||
631 | pub fn let_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![let]) } | ||
632 | pub fn pat(&self) -> Option<Pat> { support::child(&self.syntax) } | ||
633 | pub fn colon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![:]) } | ||
634 | pub fn ty(&self) -> Option<Type> { support::child(&self.syntax) } | ||
635 | pub fn eq_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![=]) } | ||
636 | pub fn initializer(&self) -> Option<Expr> { support::child(&self.syntax) } | ||
637 | pub fn semicolon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![;]) } | ||
638 | } | ||
639 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
640 | pub struct ArrayExpr { | ||
641 | pub(crate) syntax: SyntaxNode, | ||
642 | } | ||
643 | impl ast::AttrsOwner for ArrayExpr {} | ||
644 | impl ArrayExpr { | ||
645 | pub fn l_brack_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['[']) } | ||
646 | pub fn exprs(&self) -> AstChildren<Expr> { support::children(&self.syntax) } | ||
647 | pub fn expr(&self) -> Option<Expr> { support::child(&self.syntax) } | ||
648 | pub fn semicolon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![;]) } | ||
649 | pub fn r_brack_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![']']) } | ||
650 | } | ||
651 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
652 | pub struct AwaitExpr { | ||
653 | pub(crate) syntax: SyntaxNode, | ||
654 | } | ||
655 | impl ast::AttrsOwner for AwaitExpr {} | ||
656 | impl AwaitExpr { | ||
657 | pub fn expr(&self) -> Option<Expr> { support::child(&self.syntax) } | ||
658 | pub fn dot_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![.]) } | ||
659 | pub fn await_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![await]) } | ||
660 | } | ||
661 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
662 | pub struct BinExpr { | ||
663 | pub(crate) syntax: SyntaxNode, | ||
664 | } | ||
665 | impl ast::AttrsOwner for BinExpr {} | ||
666 | impl BinExpr {} | ||
667 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
668 | pub struct BoxExpr { | ||
669 | pub(crate) syntax: SyntaxNode, | ||
670 | } | ||
671 | impl ast::AttrsOwner for BoxExpr {} | ||
672 | impl BoxExpr { | ||
673 | pub fn box_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![box]) } | ||
674 | pub fn expr(&self) -> Option<Expr> { support::child(&self.syntax) } | ||
675 | } | ||
676 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
677 | pub struct BreakExpr { | ||
678 | pub(crate) syntax: SyntaxNode, | ||
679 | } | ||
680 | impl ast::AttrsOwner for BreakExpr {} | ||
681 | impl BreakExpr { | ||
682 | pub fn break_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![break]) } | ||
683 | pub fn lifetime_token(&self) -> Option<SyntaxToken> { | ||
684 | support::token(&self.syntax, T![lifetime]) | ||
685 | } | ||
686 | pub fn expr(&self) -> Option<Expr> { support::child(&self.syntax) } | ||
687 | } | ||
688 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
689 | pub struct CallExpr { | ||
690 | pub(crate) syntax: SyntaxNode, | ||
691 | } | ||
692 | impl ast::AttrsOwner for CallExpr {} | ||
693 | impl ast::ArgListOwner for CallExpr {} | ||
694 | impl CallExpr { | ||
695 | pub fn expr(&self) -> Option<Expr> { support::child(&self.syntax) } | ||
696 | } | ||
697 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
698 | pub struct CastExpr { | ||
699 | pub(crate) syntax: SyntaxNode, | ||
700 | } | ||
701 | impl ast::AttrsOwner for CastExpr {} | ||
702 | impl CastExpr { | ||
703 | pub fn expr(&self) -> Option<Expr> { support::child(&self.syntax) } | ||
704 | pub fn as_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![as]) } | ||
705 | pub fn ty(&self) -> Option<Type> { support::child(&self.syntax) } | ||
706 | } | ||
707 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
708 | pub struct ClosureExpr { | ||
709 | pub(crate) syntax: SyntaxNode, | ||
710 | } | ||
711 | impl ast::AttrsOwner for ClosureExpr {} | ||
712 | impl ClosureExpr { | ||
713 | pub fn static_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![static]) } | ||
714 | pub fn async_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![async]) } | ||
715 | pub fn move_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![move]) } | ||
716 | pub fn param_list(&self) -> Option<ParamList> { support::child(&self.syntax) } | ||
717 | pub fn ret_type(&self) -> Option<RetType> { support::child(&self.syntax) } | ||
718 | pub fn body(&self) -> Option<Expr> { support::child(&self.syntax) } | ||
719 | } | ||
720 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
721 | pub struct ContinueExpr { | ||
722 | pub(crate) syntax: SyntaxNode, | ||
723 | } | ||
724 | impl ast::AttrsOwner for ContinueExpr {} | ||
725 | impl ContinueExpr { | ||
726 | pub fn continue_token(&self) -> Option<SyntaxToken> { | ||
727 | support::token(&self.syntax, T![continue]) | ||
728 | } | ||
729 | pub fn lifetime_token(&self) -> Option<SyntaxToken> { | ||
730 | support::token(&self.syntax, T![lifetime]) | ||
731 | } | ||
732 | } | ||
733 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
734 | pub struct EffectExpr { | ||
735 | pub(crate) syntax: SyntaxNode, | ||
736 | } | ||
737 | impl ast::AttrsOwner for EffectExpr {} | ||
738 | impl EffectExpr { | ||
739 | pub fn label(&self) -> Option<Label> { support::child(&self.syntax) } | ||
740 | pub fn try_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![try]) } | ||
741 | pub fn unsafe_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![unsafe]) } | ||
742 | pub fn async_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![async]) } | ||
743 | pub fn block_expr(&self) -> Option<BlockExpr> { support::child(&self.syntax) } | ||
744 | } | ||
745 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
746 | pub struct FieldExpr { | ||
747 | pub(crate) syntax: SyntaxNode, | ||
748 | } | ||
749 | impl ast::AttrsOwner for FieldExpr {} | ||
750 | impl FieldExpr { | ||
751 | pub fn expr(&self) -> Option<Expr> { support::child(&self.syntax) } | ||
752 | pub fn dot_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![.]) } | ||
753 | pub fn name_ref(&self) -> Option<NameRef> { support::child(&self.syntax) } | ||
754 | } | ||
755 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
756 | pub struct ForExpr { | ||
757 | pub(crate) syntax: SyntaxNode, | ||
758 | } | ||
759 | impl ast::AttrsOwner for ForExpr {} | ||
760 | impl ast::LoopBodyOwner for ForExpr {} | ||
761 | impl ForExpr { | ||
762 | pub fn for_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![for]) } | ||
763 | pub fn pat(&self) -> Option<Pat> { support::child(&self.syntax) } | ||
764 | pub fn in_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![in]) } | ||
765 | pub fn iterable(&self) -> Option<Expr> { support::child(&self.syntax) } | ||
766 | } | ||
767 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
768 | pub struct IfExpr { | ||
769 | pub(crate) syntax: SyntaxNode, | ||
770 | } | ||
771 | impl ast::AttrsOwner for IfExpr {} | ||
772 | impl IfExpr { | ||
773 | pub fn if_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![if]) } | ||
774 | pub fn condition(&self) -> Option<Condition> { support::child(&self.syntax) } | ||
775 | pub fn else_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![else]) } | ||
776 | } | ||
777 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
778 | pub struct IndexExpr { | ||
779 | pub(crate) syntax: SyntaxNode, | ||
780 | } | ||
781 | impl ast::AttrsOwner for IndexExpr {} | ||
782 | impl IndexExpr { | ||
783 | pub fn l_brack_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['[']) } | ||
784 | pub fn r_brack_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![']']) } | ||
785 | } | ||
786 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
787 | pub struct LoopExpr { | ||
788 | pub(crate) syntax: SyntaxNode, | ||
789 | } | ||
790 | impl ast::AttrsOwner for LoopExpr {} | ||
791 | impl ast::LoopBodyOwner for LoopExpr {} | ||
792 | impl LoopExpr { | ||
793 | pub fn loop_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![loop]) } | ||
794 | } | ||
795 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
796 | pub struct MatchExpr { | ||
797 | pub(crate) syntax: SyntaxNode, | ||
798 | } | ||
799 | impl ast::AttrsOwner for MatchExpr {} | ||
800 | impl MatchExpr { | ||
801 | pub fn match_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![match]) } | ||
802 | pub fn expr(&self) -> Option<Expr> { support::child(&self.syntax) } | ||
803 | pub fn match_arm_list(&self) -> Option<MatchArmList> { support::child(&self.syntax) } | ||
804 | } | ||
805 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
806 | pub struct MethodCallExpr { | ||
807 | pub(crate) syntax: SyntaxNode, | ||
808 | } | ||
809 | impl ast::AttrsOwner for MethodCallExpr {} | ||
810 | impl ast::ArgListOwner for MethodCallExpr {} | ||
811 | impl MethodCallExpr { | ||
812 | pub fn expr(&self) -> Option<Expr> { support::child(&self.syntax) } | ||
813 | pub fn dot_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![.]) } | ||
814 | pub fn name_ref(&self) -> Option<NameRef> { support::child(&self.syntax) } | ||
815 | pub fn generic_arg_list(&self) -> Option<GenericArgList> { support::child(&self.syntax) } | ||
816 | } | ||
817 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
818 | pub struct ParenExpr { | ||
819 | pub(crate) syntax: SyntaxNode, | ||
820 | } | ||
821 | impl ast::AttrsOwner for ParenExpr {} | ||
822 | impl ParenExpr { | ||
823 | pub fn l_paren_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['(']) } | ||
824 | pub fn expr(&self) -> Option<Expr> { support::child(&self.syntax) } | ||
825 | pub fn r_paren_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![')']) } | ||
826 | } | ||
827 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
828 | pub struct PathExpr { | ||
829 | pub(crate) syntax: SyntaxNode, | ||
830 | } | ||
831 | impl ast::AttrsOwner for PathExpr {} | ||
832 | impl PathExpr { | ||
833 | pub fn path(&self) -> Option<Path> { support::child(&self.syntax) } | ||
834 | } | ||
835 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
836 | pub struct PrefixExpr { | ||
837 | pub(crate) syntax: SyntaxNode, | ||
838 | } | ||
839 | impl ast::AttrsOwner for PrefixExpr {} | ||
840 | impl PrefixExpr { | ||
841 | pub fn expr(&self) -> Option<Expr> { support::child(&self.syntax) } | ||
842 | } | ||
843 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
844 | pub struct RangeExpr { | ||
845 | pub(crate) syntax: SyntaxNode, | ||
846 | } | ||
847 | impl ast::AttrsOwner for RangeExpr {} | ||
848 | impl RangeExpr {} | ||
849 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
850 | pub struct RecordExpr { | ||
851 | pub(crate) syntax: SyntaxNode, | ||
852 | } | ||
853 | impl RecordExpr { | ||
854 | pub fn path(&self) -> Option<Path> { support::child(&self.syntax) } | ||
855 | pub fn record_expr_field_list(&self) -> Option<RecordExprFieldList> { | ||
856 | support::child(&self.syntax) | ||
857 | } | ||
858 | } | ||
859 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
860 | pub struct RefExpr { | ||
861 | pub(crate) syntax: SyntaxNode, | ||
862 | } | ||
863 | impl ast::AttrsOwner for RefExpr {} | ||
864 | impl RefExpr { | ||
865 | pub fn amp_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![&]) } | ||
866 | pub fn raw_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![raw]) } | ||
867 | pub fn mut_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![mut]) } | ||
868 | pub fn const_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![const]) } | ||
869 | pub fn expr(&self) -> Option<Expr> { support::child(&self.syntax) } | ||
870 | } | ||
871 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
872 | pub struct ReturnExpr { | ||
873 | pub(crate) syntax: SyntaxNode, | ||
874 | } | ||
875 | impl ast::AttrsOwner for ReturnExpr {} | ||
876 | impl ReturnExpr { | ||
877 | pub fn return_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![return]) } | ||
878 | pub fn expr(&self) -> Option<Expr> { support::child(&self.syntax) } | ||
879 | } | ||
880 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
881 | pub struct TryExpr { | ||
882 | pub(crate) syntax: SyntaxNode, | ||
883 | } | ||
884 | impl ast::AttrsOwner for TryExpr {} | ||
885 | impl TryExpr { | ||
886 | pub fn expr(&self) -> Option<Expr> { support::child(&self.syntax) } | ||
887 | pub fn question_mark_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![?]) } | ||
888 | } | ||
889 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
890 | pub struct TupleExpr { | ||
891 | pub(crate) syntax: SyntaxNode, | ||
892 | } | ||
893 | impl ast::AttrsOwner for TupleExpr {} | ||
894 | impl TupleExpr { | ||
895 | pub fn l_paren_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['(']) } | ||
896 | pub fn fields(&self) -> AstChildren<Expr> { support::children(&self.syntax) } | ||
897 | pub fn r_paren_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![')']) } | ||
898 | } | ||
899 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
900 | pub struct WhileExpr { | ||
901 | pub(crate) syntax: SyntaxNode, | ||
902 | } | ||
903 | impl ast::AttrsOwner for WhileExpr {} | ||
904 | impl ast::LoopBodyOwner for WhileExpr {} | ||
905 | impl WhileExpr { | ||
906 | pub fn while_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![while]) } | ||
907 | pub fn condition(&self) -> Option<Condition> { support::child(&self.syntax) } | ||
908 | } | ||
909 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
910 | pub struct Label { | ||
911 | pub(crate) syntax: SyntaxNode, | ||
912 | } | ||
913 | impl Label { | ||
914 | pub fn lifetime_token(&self) -> Option<SyntaxToken> { | ||
915 | support::token(&self.syntax, T![lifetime]) | ||
916 | } | ||
917 | } | ||
918 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
919 | pub struct RecordExprFieldList { | ||
920 | pub(crate) syntax: SyntaxNode, | ||
921 | } | ||
922 | impl ast::AttrsOwner for RecordExprFieldList {} | ||
923 | impl RecordExprFieldList { | ||
924 | pub fn l_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['{']) } | ||
925 | pub fn fields(&self) -> AstChildren<RecordExprField> { support::children(&self.syntax) } | ||
926 | pub fn dotdot_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![..]) } | ||
927 | pub fn spread(&self) -> Option<Expr> { support::child(&self.syntax) } | ||
928 | pub fn r_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['}']) } | ||
929 | } | ||
930 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
931 | pub struct RecordExprField { | ||
932 | pub(crate) syntax: SyntaxNode, | ||
933 | } | ||
934 | impl ast::AttrsOwner for RecordExprField {} | ||
935 | impl RecordExprField { | ||
936 | pub fn name_ref(&self) -> Option<NameRef> { support::child(&self.syntax) } | ||
937 | pub fn colon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![:]) } | ||
938 | pub fn expr(&self) -> Option<Expr> { support::child(&self.syntax) } | ||
939 | } | ||
940 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
941 | pub struct ArgList { | ||
942 | pub(crate) syntax: SyntaxNode, | ||
943 | } | ||
944 | impl ArgList { | ||
945 | pub fn l_paren_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['(']) } | ||
946 | pub fn args(&self) -> AstChildren<Expr> { support::children(&self.syntax) } | ||
947 | pub fn r_paren_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![')']) } | ||
948 | } | ||
949 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
950 | pub struct Condition { | ||
951 | pub(crate) syntax: SyntaxNode, | ||
952 | } | ||
953 | impl Condition { | ||
954 | pub fn let_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![let]) } | ||
955 | pub fn pat(&self) -> Option<Pat> { support::child(&self.syntax) } | ||
956 | pub fn eq_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![=]) } | ||
957 | pub fn expr(&self) -> Option<Expr> { support::child(&self.syntax) } | ||
958 | } | ||
959 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
960 | pub struct MatchArmList { | ||
961 | pub(crate) syntax: SyntaxNode, | ||
962 | } | ||
963 | impl ast::AttrsOwner for MatchArmList {} | ||
964 | impl MatchArmList { | ||
965 | pub fn l_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['{']) } | ||
966 | pub fn arms(&self) -> AstChildren<MatchArm> { support::children(&self.syntax) } | ||
967 | pub fn r_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['}']) } | ||
968 | } | ||
969 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
970 | pub struct MatchArm { | ||
971 | pub(crate) syntax: SyntaxNode, | ||
972 | } | ||
973 | impl ast::AttrsOwner for MatchArm {} | ||
974 | impl MatchArm { | ||
975 | pub fn pat(&self) -> Option<Pat> { support::child(&self.syntax) } | ||
976 | pub fn guard(&self) -> Option<MatchGuard> { support::child(&self.syntax) } | ||
977 | pub fn fat_arrow_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![=>]) } | ||
978 | pub fn expr(&self) -> Option<Expr> { support::child(&self.syntax) } | ||
979 | pub fn comma_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![,]) } | ||
980 | } | ||
981 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
982 | pub struct MatchGuard { | ||
983 | pub(crate) syntax: SyntaxNode, | ||
984 | } | ||
985 | impl MatchGuard { | ||
986 | pub fn if_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![if]) } | ||
987 | pub fn expr(&self) -> Option<Expr> { support::child(&self.syntax) } | ||
988 | } | ||
989 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
990 | pub struct ArrayType { | ||
991 | pub(crate) syntax: SyntaxNode, | ||
992 | } | ||
993 | impl ArrayType { | ||
994 | pub fn l_brack_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['[']) } | ||
995 | pub fn ty(&self) -> Option<Type> { support::child(&self.syntax) } | ||
996 | pub fn semicolon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![;]) } | ||
997 | pub fn expr(&self) -> Option<Expr> { support::child(&self.syntax) } | ||
998 | pub fn r_brack_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![']']) } | ||
999 | } | ||
1000 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
1001 | pub struct DynTraitType { | ||
1002 | pub(crate) syntax: SyntaxNode, | ||
1003 | } | ||
1004 | impl DynTraitType { | ||
1005 | pub fn dyn_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![dyn]) } | ||
1006 | pub fn type_bound_list(&self) -> Option<TypeBoundList> { support::child(&self.syntax) } | ||
1007 | } | ||
1008 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
1009 | pub struct FnPtrType { | ||
1010 | pub(crate) syntax: SyntaxNode, | ||
1011 | } | ||
1012 | impl FnPtrType { | ||
1013 | pub fn const_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![const]) } | ||
1014 | pub fn async_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![async]) } | ||
1015 | pub fn unsafe_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![unsafe]) } | ||
1016 | pub fn abi(&self) -> Option<Abi> { support::child(&self.syntax) } | ||
1017 | pub fn fn_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![fn]) } | ||
1018 | pub fn param_list(&self) -> Option<ParamList> { support::child(&self.syntax) } | ||
1019 | pub fn ret_type(&self) -> Option<RetType> { support::child(&self.syntax) } | ||
1020 | } | ||
1021 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
1022 | pub struct ForType { | ||
1023 | pub(crate) syntax: SyntaxNode, | ||
1024 | } | ||
1025 | impl ForType { | ||
1026 | pub fn for_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![for]) } | ||
1027 | pub fn generic_param_list(&self) -> Option<GenericParamList> { support::child(&self.syntax) } | ||
1028 | pub fn ty(&self) -> Option<Type> { support::child(&self.syntax) } | ||
1029 | } | ||
1030 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
1031 | pub struct ImplTraitType { | ||
1032 | pub(crate) syntax: SyntaxNode, | ||
1033 | } | ||
1034 | impl ImplTraitType { | ||
1035 | pub fn impl_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![impl]) } | ||
1036 | pub fn type_bound_list(&self) -> Option<TypeBoundList> { support::child(&self.syntax) } | ||
1037 | } | ||
1038 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
1039 | pub struct InferType { | ||
1040 | pub(crate) syntax: SyntaxNode, | ||
1041 | } | ||
1042 | impl InferType { | ||
1043 | pub fn underscore_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![_]) } | ||
1044 | } | ||
1045 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
1046 | pub struct NeverType { | ||
1047 | pub(crate) syntax: SyntaxNode, | ||
1048 | } | ||
1049 | impl NeverType { | ||
1050 | pub fn excl_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![!]) } | ||
1051 | } | ||
1052 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
1053 | pub struct ParenType { | ||
1054 | pub(crate) syntax: SyntaxNode, | ||
1055 | } | ||
1056 | impl ParenType { | ||
1057 | pub fn l_paren_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['(']) } | ||
1058 | pub fn ty(&self) -> Option<Type> { support::child(&self.syntax) } | ||
1059 | pub fn r_paren_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![')']) } | ||
1060 | } | ||
1061 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
1062 | pub struct PtrType { | ||
1063 | pub(crate) syntax: SyntaxNode, | ||
1064 | } | ||
1065 | impl PtrType { | ||
1066 | pub fn star_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![*]) } | ||
1067 | pub fn const_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![const]) } | ||
1068 | pub fn mut_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![mut]) } | ||
1069 | pub fn ty(&self) -> Option<Type> { support::child(&self.syntax) } | ||
1070 | } | ||
1071 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
1072 | pub struct RefType { | ||
1073 | pub(crate) syntax: SyntaxNode, | ||
1074 | } | ||
1075 | impl RefType { | ||
1076 | pub fn amp_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![&]) } | ||
1077 | pub fn lifetime_token(&self) -> Option<SyntaxToken> { | ||
1078 | support::token(&self.syntax, T![lifetime]) | ||
1079 | } | ||
1080 | pub fn mut_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![mut]) } | ||
1081 | pub fn ty(&self) -> Option<Type> { support::child(&self.syntax) } | ||
1082 | } | ||
1083 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
1084 | pub struct SliceType { | ||
1085 | pub(crate) syntax: SyntaxNode, | ||
1086 | } | ||
1087 | impl SliceType { | ||
1088 | pub fn l_brack_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['[']) } | ||
1089 | pub fn ty(&self) -> Option<Type> { support::child(&self.syntax) } | ||
1090 | pub fn r_brack_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![']']) } | ||
1091 | } | ||
1092 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
1093 | pub struct TupleType { | ||
1094 | pub(crate) syntax: SyntaxNode, | ||
1095 | } | ||
1096 | impl TupleType { | ||
1097 | pub fn l_paren_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['(']) } | ||
1098 | pub fn fields(&self) -> AstChildren<Type> { support::children(&self.syntax) } | ||
1099 | pub fn r_paren_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![')']) } | ||
1100 | } | ||
1101 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
1102 | pub struct TypeBound { | ||
1103 | pub(crate) syntax: SyntaxNode, | ||
1104 | } | ||
1105 | impl TypeBound { | ||
1106 | pub fn lifetime_token(&self) -> Option<SyntaxToken> { | ||
1107 | support::token(&self.syntax, T![lifetime]) | ||
1108 | } | ||
1109 | pub fn question_mark_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![?]) } | ||
1110 | pub fn ty(&self) -> Option<Type> { support::child(&self.syntax) } | ||
1111 | } | ||
1112 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
1113 | pub struct IdentPat { | ||
1114 | pub(crate) syntax: SyntaxNode, | ||
1115 | } | ||
1116 | impl ast::AttrsOwner for IdentPat {} | ||
1117 | impl ast::NameOwner for IdentPat {} | ||
1118 | impl IdentPat { | ||
1119 | pub fn ref_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![ref]) } | ||
1120 | pub fn mut_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![mut]) } | ||
1121 | pub fn at_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![@]) } | ||
1122 | pub fn pat(&self) -> Option<Pat> { support::child(&self.syntax) } | ||
1123 | } | ||
1124 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
1125 | pub struct BoxPat { | ||
1126 | pub(crate) syntax: SyntaxNode, | ||
1127 | } | ||
1128 | impl BoxPat { | ||
1129 | pub fn box_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![box]) } | ||
1130 | pub fn pat(&self) -> Option<Pat> { support::child(&self.syntax) } | ||
1131 | } | ||
1132 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
1133 | pub struct RestPat { | ||
1134 | pub(crate) syntax: SyntaxNode, | ||
1135 | } | ||
1136 | impl RestPat { | ||
1137 | pub fn dotdot_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![..]) } | ||
1138 | } | ||
1139 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
1140 | pub struct LiteralPat { | ||
1141 | pub(crate) syntax: SyntaxNode, | ||
1142 | } | ||
1143 | impl LiteralPat { | ||
1144 | pub fn literal(&self) -> Option<Literal> { support::child(&self.syntax) } | ||
1145 | } | ||
1146 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
1147 | pub struct MacroPat { | ||
1148 | pub(crate) syntax: SyntaxNode, | ||
1149 | } | ||
1150 | impl MacroPat { | ||
1151 | pub fn macro_call(&self) -> Option<MacroCall> { support::child(&self.syntax) } | ||
1152 | } | ||
1153 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
1154 | pub struct OrPat { | ||
1155 | pub(crate) syntax: SyntaxNode, | ||
1156 | } | ||
1157 | impl OrPat { | ||
1158 | pub fn pats(&self) -> AstChildren<Pat> { support::children(&self.syntax) } | ||
1159 | } | ||
1160 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
1161 | pub struct ParenPat { | ||
1162 | pub(crate) syntax: SyntaxNode, | ||
1163 | } | ||
1164 | impl ParenPat { | ||
1165 | pub fn l_paren_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['(']) } | ||
1166 | pub fn pat(&self) -> Option<Pat> { support::child(&self.syntax) } | ||
1167 | pub fn r_paren_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![')']) } | ||
1168 | } | ||
1169 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
1170 | pub struct PathPat { | ||
1171 | pub(crate) syntax: SyntaxNode, | ||
1172 | } | ||
1173 | impl PathPat { | ||
1174 | pub fn path(&self) -> Option<Path> { support::child(&self.syntax) } | ||
1175 | } | ||
1176 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
1177 | pub struct WildcardPat { | ||
1178 | pub(crate) syntax: SyntaxNode, | ||
1179 | } | ||
1180 | impl WildcardPat { | ||
1181 | pub fn underscore_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![_]) } | ||
1182 | } | ||
1183 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
1184 | pub struct RangePat { | ||
1185 | pub(crate) syntax: SyntaxNode, | ||
1186 | } | ||
1187 | impl RangePat {} | ||
1188 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
1189 | pub struct RecordPat { | ||
1190 | pub(crate) syntax: SyntaxNode, | ||
1191 | } | ||
1192 | impl RecordPat { | ||
1193 | pub fn path(&self) -> Option<Path> { support::child(&self.syntax) } | ||
1194 | pub fn record_pat_field_list(&self) -> Option<RecordPatFieldList> { | ||
1195 | support::child(&self.syntax) | ||
1196 | } | ||
1197 | } | ||
1198 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
1199 | pub struct RefPat { | ||
1200 | pub(crate) syntax: SyntaxNode, | ||
1201 | } | ||
1202 | impl RefPat { | ||
1203 | pub fn amp_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![&]) } | ||
1204 | pub fn mut_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![mut]) } | ||
1205 | pub fn pat(&self) -> Option<Pat> { support::child(&self.syntax) } | ||
1206 | } | ||
1207 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
1208 | pub struct SlicePat { | ||
1209 | pub(crate) syntax: SyntaxNode, | ||
1210 | } | ||
1211 | impl SlicePat { | ||
1212 | pub fn l_brack_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['[']) } | ||
1213 | pub fn pats(&self) -> AstChildren<Pat> { support::children(&self.syntax) } | ||
1214 | pub fn r_brack_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![']']) } | ||
1215 | } | ||
1216 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
1217 | pub struct TuplePat { | ||
1218 | pub(crate) syntax: SyntaxNode, | ||
1219 | } | ||
1220 | impl TuplePat { | ||
1221 | pub fn l_paren_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['(']) } | ||
1222 | pub fn fields(&self) -> AstChildren<Pat> { support::children(&self.syntax) } | ||
1223 | pub fn r_paren_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![')']) } | ||
1224 | } | ||
1225 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
1226 | pub struct TupleStructPat { | ||
1227 | pub(crate) syntax: SyntaxNode, | ||
1228 | } | ||
1229 | impl TupleStructPat { | ||
1230 | pub fn path(&self) -> Option<Path> { support::child(&self.syntax) } | ||
1231 | pub fn l_paren_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['(']) } | ||
1232 | pub fn fields(&self) -> AstChildren<Pat> { support::children(&self.syntax) } | ||
1233 | pub fn r_paren_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![')']) } | ||
1234 | } | ||
1235 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
1236 | pub struct RecordPatFieldList { | ||
1237 | pub(crate) syntax: SyntaxNode, | ||
1238 | } | ||
1239 | impl RecordPatFieldList { | ||
1240 | pub fn l_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['{']) } | ||
1241 | pub fn fields(&self) -> AstChildren<RecordPatField> { support::children(&self.syntax) } | ||
1242 | pub fn dotdot_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![..]) } | ||
1243 | pub fn r_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['}']) } | ||
1244 | } | ||
1245 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
1246 | pub struct RecordPatField { | ||
1247 | pub(crate) syntax: SyntaxNode, | ||
1248 | } | ||
1249 | impl ast::AttrsOwner for RecordPatField {} | ||
1250 | impl RecordPatField { | ||
1251 | pub fn name_ref(&self) -> Option<NameRef> { support::child(&self.syntax) } | ||
1252 | pub fn colon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![:]) } | ||
1253 | pub fn pat(&self) -> Option<Pat> { support::child(&self.syntax) } | ||
1254 | } | ||
1255 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
1256 | pub enum GenericArg { | ||
1257 | TypeArg(TypeArg), | ||
1258 | AssocTypeArg(AssocTypeArg), | ||
1259 | LifetimeArg(LifetimeArg), | ||
1260 | ConstArg(ConstArg), | ||
1261 | } | ||
1262 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
1263 | pub enum Type { | ||
1264 | ArrayType(ArrayType), | ||
1265 | DynTraitType(DynTraitType), | ||
1266 | FnPtrType(FnPtrType), | ||
1267 | ForType(ForType), | ||
1268 | ImplTraitType(ImplTraitType), | ||
1269 | InferType(InferType), | ||
1270 | NeverType(NeverType), | ||
1271 | ParenType(ParenType), | ||
1272 | PathType(PathType), | ||
1273 | PtrType(PtrType), | ||
1274 | RefType(RefType), | ||
1275 | SliceType(SliceType), | ||
1276 | TupleType(TupleType), | ||
1277 | } | ||
1278 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
1279 | pub enum Expr { | ||
1280 | ArrayExpr(ArrayExpr), | ||
1281 | AwaitExpr(AwaitExpr), | ||
1282 | BinExpr(BinExpr), | ||
1283 | BlockExpr(BlockExpr), | ||
1284 | BoxExpr(BoxExpr), | ||
1285 | BreakExpr(BreakExpr), | ||
1286 | CallExpr(CallExpr), | ||
1287 | CastExpr(CastExpr), | ||
1288 | ClosureExpr(ClosureExpr), | ||
1289 | ContinueExpr(ContinueExpr), | ||
1290 | EffectExpr(EffectExpr), | ||
1291 | FieldExpr(FieldExpr), | ||
1292 | ForExpr(ForExpr), | ||
1293 | IfExpr(IfExpr), | ||
1294 | IndexExpr(IndexExpr), | ||
1295 | Literal(Literal), | ||
1296 | LoopExpr(LoopExpr), | ||
1297 | MacroCall(MacroCall), | ||
1298 | MatchExpr(MatchExpr), | ||
1299 | MethodCallExpr(MethodCallExpr), | ||
1300 | ParenExpr(ParenExpr), | ||
1301 | PathExpr(PathExpr), | ||
1302 | PrefixExpr(PrefixExpr), | ||
1303 | RangeExpr(RangeExpr), | ||
1304 | RecordExpr(RecordExpr), | ||
1305 | RefExpr(RefExpr), | ||
1306 | ReturnExpr(ReturnExpr), | ||
1307 | TryExpr(TryExpr), | ||
1308 | TupleExpr(TupleExpr), | ||
1309 | WhileExpr(WhileExpr), | ||
1310 | } | ||
1311 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
1312 | pub enum Item { | ||
1313 | Const(Const), | ||
1314 | Enum(Enum), | ||
1315 | ExternBlock(ExternBlock), | ||
1316 | ExternCrate(ExternCrate), | ||
1317 | Fn(Fn), | ||
1318 | Impl(Impl), | ||
1319 | MacroCall(MacroCall), | ||
1320 | Module(Module), | ||
1321 | Static(Static), | ||
1322 | Struct(Struct), | ||
1323 | Trait(Trait), | ||
1324 | TypeAlias(TypeAlias), | ||
1325 | Union(Union), | ||
1326 | Use(Use), | ||
1327 | } | ||
1328 | impl ast::AttrsOwner for Item {} | ||
1329 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
1330 | pub enum Stmt { | ||
1331 | ExprStmt(ExprStmt), | ||
1332 | Item(Item), | ||
1333 | LetStmt(LetStmt), | ||
1334 | } | ||
1335 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
1336 | pub enum Pat { | ||
1337 | IdentPat(IdentPat), | ||
1338 | BoxPat(BoxPat), | ||
1339 | RestPat(RestPat), | ||
1340 | LiteralPat(LiteralPat), | ||
1341 | MacroPat(MacroPat), | ||
1342 | OrPat(OrPat), | ||
1343 | ParenPat(ParenPat), | ||
1344 | PathPat(PathPat), | ||
1345 | WildcardPat(WildcardPat), | ||
1346 | RangePat(RangePat), | ||
1347 | RecordPat(RecordPat), | ||
1348 | RefPat(RefPat), | ||
1349 | SlicePat(SlicePat), | ||
1350 | TuplePat(TuplePat), | ||
1351 | TupleStructPat(TupleStructPat), | ||
1352 | } | ||
1353 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
1354 | pub enum FieldList { | ||
1355 | RecordFieldList(RecordFieldList), | ||
1356 | TupleFieldList(TupleFieldList), | ||
1357 | } | ||
1358 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
1359 | pub enum AdtDef { | ||
1360 | Enum(Enum), | ||
1361 | Struct(Struct), | ||
1362 | Union(Union), | ||
1363 | } | ||
1364 | impl ast::AttrsOwner for AdtDef {} | ||
1365 | impl ast::GenericParamsOwner for AdtDef {} | ||
1366 | impl ast::NameOwner for AdtDef {} | ||
1367 | impl ast::VisibilityOwner for AdtDef {} | ||
1368 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
1369 | pub enum AssocItem { | ||
1370 | Const(Const), | ||
1371 | Fn(Fn), | ||
1372 | MacroCall(MacroCall), | ||
1373 | TypeAlias(TypeAlias), | ||
1374 | } | ||
1375 | impl ast::AttrsOwner for AssocItem {} | ||
1376 | impl ast::NameOwner for AssocItem {} | ||
1377 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
1378 | pub enum ExternItem { | ||
1379 | Fn(Fn), | ||
1380 | MacroCall(MacroCall), | ||
1381 | Static(Static), | ||
1382 | } | ||
1383 | impl ast::AttrsOwner for ExternItem {} | ||
1384 | impl ast::NameOwner for ExternItem {} | ||
1385 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
1386 | pub enum GenericParam { | ||
1387 | ConstParam(ConstParam), | ||
1388 | LifetimeParam(LifetimeParam), | ||
1389 | TypeParam(TypeParam), | ||
1390 | } | ||
1391 | impl ast::AttrsOwner for GenericParam {} | ||
1392 | impl AstNode for Name { | ||
1393 | fn can_cast(kind: SyntaxKind) -> bool { kind == NAME } | ||
1394 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1395 | if Self::can_cast(syntax.kind()) { | ||
1396 | Some(Self { syntax }) | ||
1397 | } else { | ||
1398 | None | ||
1399 | } | ||
1400 | } | ||
1401 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1402 | } | ||
1403 | impl AstNode for NameRef { | ||
1404 | fn can_cast(kind: SyntaxKind) -> bool { kind == NAME_REF } | ||
1405 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1406 | if Self::can_cast(syntax.kind()) { | ||
1407 | Some(Self { syntax }) | ||
1408 | } else { | ||
1409 | None | ||
1410 | } | ||
1411 | } | ||
1412 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1413 | } | ||
1414 | impl AstNode for Path { | ||
1415 | fn can_cast(kind: SyntaxKind) -> bool { kind == PATH } | ||
1416 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1417 | if Self::can_cast(syntax.kind()) { | ||
1418 | Some(Self { syntax }) | ||
1419 | } else { | ||
1420 | None | ||
1421 | } | ||
1422 | } | ||
1423 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1424 | } | ||
1425 | impl AstNode for PathSegment { | ||
1426 | fn can_cast(kind: SyntaxKind) -> bool { kind == PATH_SEGMENT } | ||
1427 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1428 | if Self::can_cast(syntax.kind()) { | ||
1429 | Some(Self { syntax }) | ||
1430 | } else { | ||
1431 | None | ||
1432 | } | ||
1433 | } | ||
1434 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1435 | } | ||
1436 | impl AstNode for GenericArgList { | ||
1437 | fn can_cast(kind: SyntaxKind) -> bool { kind == GENERIC_ARG_LIST } | ||
1438 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1439 | if Self::can_cast(syntax.kind()) { | ||
1440 | Some(Self { syntax }) | ||
1441 | } else { | ||
1442 | None | ||
1443 | } | ||
1444 | } | ||
1445 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1446 | } | ||
1447 | impl AstNode for ParamList { | ||
1448 | fn can_cast(kind: SyntaxKind) -> bool { kind == PARAM_LIST } | ||
1449 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1450 | if Self::can_cast(syntax.kind()) { | ||
1451 | Some(Self { syntax }) | ||
1452 | } else { | ||
1453 | None | ||
1454 | } | ||
1455 | } | ||
1456 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1457 | } | ||
1458 | impl AstNode for RetType { | ||
1459 | fn can_cast(kind: SyntaxKind) -> bool { kind == RET_TYPE } | ||
1460 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1461 | if Self::can_cast(syntax.kind()) { | ||
1462 | Some(Self { syntax }) | ||
1463 | } else { | ||
1464 | None | ||
1465 | } | ||
1466 | } | ||
1467 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1468 | } | ||
1469 | impl AstNode for PathType { | ||
1470 | fn can_cast(kind: SyntaxKind) -> bool { kind == PATH_TYPE } | ||
1471 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1472 | if Self::can_cast(syntax.kind()) { | ||
1473 | Some(Self { syntax }) | ||
1474 | } else { | ||
1475 | None | ||
1476 | } | ||
1477 | } | ||
1478 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1479 | } | ||
1480 | impl AstNode for TypeArg { | ||
1481 | fn can_cast(kind: SyntaxKind) -> bool { kind == TYPE_ARG } | ||
1482 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1483 | if Self::can_cast(syntax.kind()) { | ||
1484 | Some(Self { syntax }) | ||
1485 | } else { | ||
1486 | None | ||
1487 | } | ||
1488 | } | ||
1489 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1490 | } | ||
1491 | impl AstNode for AssocTypeArg { | ||
1492 | fn can_cast(kind: SyntaxKind) -> bool { kind == ASSOC_TYPE_ARG } | ||
1493 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1494 | if Self::can_cast(syntax.kind()) { | ||
1495 | Some(Self { syntax }) | ||
1496 | } else { | ||
1497 | None | ||
1498 | } | ||
1499 | } | ||
1500 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1501 | } | ||
1502 | impl AstNode for LifetimeArg { | ||
1503 | fn can_cast(kind: SyntaxKind) -> bool { kind == LIFETIME_ARG } | ||
1504 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1505 | if Self::can_cast(syntax.kind()) { | ||
1506 | Some(Self { syntax }) | ||
1507 | } else { | ||
1508 | None | ||
1509 | } | ||
1510 | } | ||
1511 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1512 | } | ||
1513 | impl AstNode for ConstArg { | ||
1514 | fn can_cast(kind: SyntaxKind) -> bool { kind == CONST_ARG } | ||
1515 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1516 | if Self::can_cast(syntax.kind()) { | ||
1517 | Some(Self { syntax }) | ||
1518 | } else { | ||
1519 | None | ||
1520 | } | ||
1521 | } | ||
1522 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1523 | } | ||
1524 | impl AstNode for TypeBoundList { | ||
1525 | fn can_cast(kind: SyntaxKind) -> bool { kind == TYPE_BOUND_LIST } | ||
1526 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1527 | if Self::can_cast(syntax.kind()) { | ||
1528 | Some(Self { syntax }) | ||
1529 | } else { | ||
1530 | None | ||
1531 | } | ||
1532 | } | ||
1533 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1534 | } | ||
1535 | impl AstNode for MacroCall { | ||
1536 | fn can_cast(kind: SyntaxKind) -> bool { kind == MACRO_CALL } | ||
1537 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1538 | if Self::can_cast(syntax.kind()) { | ||
1539 | Some(Self { syntax }) | ||
1540 | } else { | ||
1541 | None | ||
1542 | } | ||
1543 | } | ||
1544 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1545 | } | ||
1546 | impl AstNode for Attr { | ||
1547 | fn can_cast(kind: SyntaxKind) -> bool { kind == ATTR } | ||
1548 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1549 | if Self::can_cast(syntax.kind()) { | ||
1550 | Some(Self { syntax }) | ||
1551 | } else { | ||
1552 | None | ||
1553 | } | ||
1554 | } | ||
1555 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1556 | } | ||
1557 | impl AstNode for TokenTree { | ||
1558 | fn can_cast(kind: SyntaxKind) -> bool { kind == TOKEN_TREE } | ||
1559 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1560 | if Self::can_cast(syntax.kind()) { | ||
1561 | Some(Self { syntax }) | ||
1562 | } else { | ||
1563 | None | ||
1564 | } | ||
1565 | } | ||
1566 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1567 | } | ||
1568 | impl AstNode for MacroItems { | ||
1569 | fn can_cast(kind: SyntaxKind) -> bool { kind == MACRO_ITEMS } | ||
1570 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1571 | if Self::can_cast(syntax.kind()) { | ||
1572 | Some(Self { syntax }) | ||
1573 | } else { | ||
1574 | None | ||
1575 | } | ||
1576 | } | ||
1577 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1578 | } | ||
1579 | impl AstNode for MacroStmts { | ||
1580 | fn can_cast(kind: SyntaxKind) -> bool { kind == MACRO_STMTS } | ||
1581 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1582 | if Self::can_cast(syntax.kind()) { | ||
1583 | Some(Self { syntax }) | ||
1584 | } else { | ||
1585 | None | ||
1586 | } | ||
1587 | } | ||
1588 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1589 | } | ||
1590 | impl AstNode for SourceFile { | ||
1591 | fn can_cast(kind: SyntaxKind) -> bool { kind == SOURCE_FILE } | ||
1592 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1593 | if Self::can_cast(syntax.kind()) { | ||
1594 | Some(Self { syntax }) | ||
1595 | } else { | ||
1596 | None | ||
1597 | } | ||
1598 | } | ||
1599 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1600 | } | ||
1601 | impl AstNode for Const { | ||
1602 | fn can_cast(kind: SyntaxKind) -> bool { kind == CONST } | ||
1603 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1604 | if Self::can_cast(syntax.kind()) { | ||
1605 | Some(Self { syntax }) | ||
1606 | } else { | ||
1607 | None | ||
1608 | } | ||
1609 | } | ||
1610 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1611 | } | ||
1612 | impl AstNode for Enum { | ||
1613 | fn can_cast(kind: SyntaxKind) -> bool { kind == ENUM } | ||
1614 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1615 | if Self::can_cast(syntax.kind()) { | ||
1616 | Some(Self { syntax }) | ||
1617 | } else { | ||
1618 | None | ||
1619 | } | ||
1620 | } | ||
1621 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1622 | } | ||
1623 | impl AstNode for ExternBlock { | ||
1624 | fn can_cast(kind: SyntaxKind) -> bool { kind == EXTERN_BLOCK } | ||
1625 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1626 | if Self::can_cast(syntax.kind()) { | ||
1627 | Some(Self { syntax }) | ||
1628 | } else { | ||
1629 | None | ||
1630 | } | ||
1631 | } | ||
1632 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1633 | } | ||
1634 | impl AstNode for ExternCrate { | ||
1635 | fn can_cast(kind: SyntaxKind) -> bool { kind == EXTERN_CRATE } | ||
1636 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1637 | if Self::can_cast(syntax.kind()) { | ||
1638 | Some(Self { syntax }) | ||
1639 | } else { | ||
1640 | None | ||
1641 | } | ||
1642 | } | ||
1643 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1644 | } | ||
1645 | impl AstNode for Fn { | ||
1646 | fn can_cast(kind: SyntaxKind) -> bool { kind == FN } | ||
1647 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1648 | if Self::can_cast(syntax.kind()) { | ||
1649 | Some(Self { syntax }) | ||
1650 | } else { | ||
1651 | None | ||
1652 | } | ||
1653 | } | ||
1654 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1655 | } | ||
1656 | impl AstNode for Impl { | ||
1657 | fn can_cast(kind: SyntaxKind) -> bool { kind == IMPL } | ||
1658 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1659 | if Self::can_cast(syntax.kind()) { | ||
1660 | Some(Self { syntax }) | ||
1661 | } else { | ||
1662 | None | ||
1663 | } | ||
1664 | } | ||
1665 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1666 | } | ||
1667 | impl AstNode for Module { | ||
1668 | fn can_cast(kind: SyntaxKind) -> bool { kind == MODULE } | ||
1669 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1670 | if Self::can_cast(syntax.kind()) { | ||
1671 | Some(Self { syntax }) | ||
1672 | } else { | ||
1673 | None | ||
1674 | } | ||
1675 | } | ||
1676 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1677 | } | ||
1678 | impl AstNode for Static { | ||
1679 | fn can_cast(kind: SyntaxKind) -> bool { kind == STATIC } | ||
1680 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1681 | if Self::can_cast(syntax.kind()) { | ||
1682 | Some(Self { syntax }) | ||
1683 | } else { | ||
1684 | None | ||
1685 | } | ||
1686 | } | ||
1687 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1688 | } | ||
1689 | impl AstNode for Struct { | ||
1690 | fn can_cast(kind: SyntaxKind) -> bool { kind == STRUCT } | ||
1691 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1692 | if Self::can_cast(syntax.kind()) { | ||
1693 | Some(Self { syntax }) | ||
1694 | } else { | ||
1695 | None | ||
1696 | } | ||
1697 | } | ||
1698 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1699 | } | ||
1700 | impl AstNode for Trait { | ||
1701 | fn can_cast(kind: SyntaxKind) -> bool { kind == TRAIT } | ||
1702 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1703 | if Self::can_cast(syntax.kind()) { | ||
1704 | Some(Self { syntax }) | ||
1705 | } else { | ||
1706 | None | ||
1707 | } | ||
1708 | } | ||
1709 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1710 | } | ||
1711 | impl AstNode for TypeAlias { | ||
1712 | fn can_cast(kind: SyntaxKind) -> bool { kind == TYPE_ALIAS } | ||
1713 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1714 | if Self::can_cast(syntax.kind()) { | ||
1715 | Some(Self { syntax }) | ||
1716 | } else { | ||
1717 | None | ||
1718 | } | ||
1719 | } | ||
1720 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1721 | } | ||
1722 | impl AstNode for Union { | ||
1723 | fn can_cast(kind: SyntaxKind) -> bool { kind == UNION } | ||
1724 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1725 | if Self::can_cast(syntax.kind()) { | ||
1726 | Some(Self { syntax }) | ||
1727 | } else { | ||
1728 | None | ||
1729 | } | ||
1730 | } | ||
1731 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1732 | } | ||
1733 | impl AstNode for Use { | ||
1734 | fn can_cast(kind: SyntaxKind) -> bool { kind == USE } | ||
1735 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1736 | if Self::can_cast(syntax.kind()) { | ||
1737 | Some(Self { syntax }) | ||
1738 | } else { | ||
1739 | None | ||
1740 | } | ||
1741 | } | ||
1742 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1743 | } | ||
1744 | impl AstNode for Visibility { | ||
1745 | fn can_cast(kind: SyntaxKind) -> bool { kind == VISIBILITY } | ||
1746 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1747 | if Self::can_cast(syntax.kind()) { | ||
1748 | Some(Self { syntax }) | ||
1749 | } else { | ||
1750 | None | ||
1751 | } | ||
1752 | } | ||
1753 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1754 | } | ||
1755 | impl AstNode for ItemList { | ||
1756 | fn can_cast(kind: SyntaxKind) -> bool { kind == ITEM_LIST } | ||
1757 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1758 | if Self::can_cast(syntax.kind()) { | ||
1759 | Some(Self { syntax }) | ||
1760 | } else { | ||
1761 | None | ||
1762 | } | ||
1763 | } | ||
1764 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1765 | } | ||
1766 | impl AstNode for Rename { | ||
1767 | fn can_cast(kind: SyntaxKind) -> bool { kind == RENAME } | ||
1768 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1769 | if Self::can_cast(syntax.kind()) { | ||
1770 | Some(Self { syntax }) | ||
1771 | } else { | ||
1772 | None | ||
1773 | } | ||
1774 | } | ||
1775 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1776 | } | ||
1777 | impl AstNode for UseTree { | ||
1778 | fn can_cast(kind: SyntaxKind) -> bool { kind == USE_TREE } | ||
1779 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1780 | if Self::can_cast(syntax.kind()) { | ||
1781 | Some(Self { syntax }) | ||
1782 | } else { | ||
1783 | None | ||
1784 | } | ||
1785 | } | ||
1786 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1787 | } | ||
1788 | impl AstNode for UseTreeList { | ||
1789 | fn can_cast(kind: SyntaxKind) -> bool { kind == USE_TREE_LIST } | ||
1790 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1791 | if Self::can_cast(syntax.kind()) { | ||
1792 | Some(Self { syntax }) | ||
1793 | } else { | ||
1794 | None | ||
1795 | } | ||
1796 | } | ||
1797 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1798 | } | ||
1799 | impl AstNode for Abi { | ||
1800 | fn can_cast(kind: SyntaxKind) -> bool { kind == ABI } | ||
1801 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1802 | if Self::can_cast(syntax.kind()) { | ||
1803 | Some(Self { syntax }) | ||
1804 | } else { | ||
1805 | None | ||
1806 | } | ||
1807 | } | ||
1808 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1809 | } | ||
1810 | impl AstNode for GenericParamList { | ||
1811 | fn can_cast(kind: SyntaxKind) -> bool { kind == GENERIC_PARAM_LIST } | ||
1812 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1813 | if Self::can_cast(syntax.kind()) { | ||
1814 | Some(Self { syntax }) | ||
1815 | } else { | ||
1816 | None | ||
1817 | } | ||
1818 | } | ||
1819 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1820 | } | ||
1821 | impl AstNode for WhereClause { | ||
1822 | fn can_cast(kind: SyntaxKind) -> bool { kind == WHERE_CLAUSE } | ||
1823 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1824 | if Self::can_cast(syntax.kind()) { | ||
1825 | Some(Self { syntax }) | ||
1826 | } else { | ||
1827 | None | ||
1828 | } | ||
1829 | } | ||
1830 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1831 | } | ||
1832 | impl AstNode for BlockExpr { | ||
1833 | fn can_cast(kind: SyntaxKind) -> bool { kind == BLOCK_EXPR } | ||
1834 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1835 | if Self::can_cast(syntax.kind()) { | ||
1836 | Some(Self { syntax }) | ||
1837 | } else { | ||
1838 | None | ||
1839 | } | ||
1840 | } | ||
1841 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1842 | } | ||
1843 | impl AstNode for SelfParam { | ||
1844 | fn can_cast(kind: SyntaxKind) -> bool { kind == SELF_PARAM } | ||
1845 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1846 | if Self::can_cast(syntax.kind()) { | ||
1847 | Some(Self { syntax }) | ||
1848 | } else { | ||
1849 | None | ||
1850 | } | ||
1851 | } | ||
1852 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1853 | } | ||
1854 | impl AstNode for Param { | ||
1855 | fn can_cast(kind: SyntaxKind) -> bool { kind == PARAM } | ||
1856 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1857 | if Self::can_cast(syntax.kind()) { | ||
1858 | Some(Self { syntax }) | ||
1859 | } else { | ||
1860 | None | ||
1861 | } | ||
1862 | } | ||
1863 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1864 | } | ||
1865 | impl AstNode for RecordFieldList { | ||
1866 | fn can_cast(kind: SyntaxKind) -> bool { kind == RECORD_FIELD_LIST } | ||
1867 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1868 | if Self::can_cast(syntax.kind()) { | ||
1869 | Some(Self { syntax }) | ||
1870 | } else { | ||
1871 | None | ||
1872 | } | ||
1873 | } | ||
1874 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1875 | } | ||
1876 | impl AstNode for TupleFieldList { | ||
1877 | fn can_cast(kind: SyntaxKind) -> bool { kind == TUPLE_FIELD_LIST } | ||
1878 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1879 | if Self::can_cast(syntax.kind()) { | ||
1880 | Some(Self { syntax }) | ||
1881 | } else { | ||
1882 | None | ||
1883 | } | ||
1884 | } | ||
1885 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1886 | } | ||
1887 | impl AstNode for RecordField { | ||
1888 | fn can_cast(kind: SyntaxKind) -> bool { kind == RECORD_FIELD } | ||
1889 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1890 | if Self::can_cast(syntax.kind()) { | ||
1891 | Some(Self { syntax }) | ||
1892 | } else { | ||
1893 | None | ||
1894 | } | ||
1895 | } | ||
1896 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1897 | } | ||
1898 | impl AstNode for TupleField { | ||
1899 | fn can_cast(kind: SyntaxKind) -> bool { kind == TUPLE_FIELD } | ||
1900 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1901 | if Self::can_cast(syntax.kind()) { | ||
1902 | Some(Self { syntax }) | ||
1903 | } else { | ||
1904 | None | ||
1905 | } | ||
1906 | } | ||
1907 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1908 | } | ||
1909 | impl AstNode for VariantList { | ||
1910 | fn can_cast(kind: SyntaxKind) -> bool { kind == VARIANT_LIST } | ||
1911 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1912 | if Self::can_cast(syntax.kind()) { | ||
1913 | Some(Self { syntax }) | ||
1914 | } else { | ||
1915 | None | ||
1916 | } | ||
1917 | } | ||
1918 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1919 | } | ||
1920 | impl AstNode for Variant { | ||
1921 | fn can_cast(kind: SyntaxKind) -> bool { kind == VARIANT } | ||
1922 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1923 | if Self::can_cast(syntax.kind()) { | ||
1924 | Some(Self { syntax }) | ||
1925 | } else { | ||
1926 | None | ||
1927 | } | ||
1928 | } | ||
1929 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1930 | } | ||
1931 | impl AstNode for AssocItemList { | ||
1932 | fn can_cast(kind: SyntaxKind) -> bool { kind == ASSOC_ITEM_LIST } | ||
1933 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1934 | if Self::can_cast(syntax.kind()) { | ||
1935 | Some(Self { syntax }) | ||
1936 | } else { | ||
1937 | None | ||
1938 | } | ||
1939 | } | ||
1940 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1941 | } | ||
1942 | impl AstNode for ExternItemList { | ||
1943 | fn can_cast(kind: SyntaxKind) -> bool { kind == EXTERN_ITEM_LIST } | ||
1944 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1945 | if Self::can_cast(syntax.kind()) { | ||
1946 | Some(Self { syntax }) | ||
1947 | } else { | ||
1948 | None | ||
1949 | } | ||
1950 | } | ||
1951 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1952 | } | ||
1953 | impl AstNode for ConstParam { | ||
1954 | fn can_cast(kind: SyntaxKind) -> bool { kind == CONST_PARAM } | ||
1955 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1956 | if Self::can_cast(syntax.kind()) { | ||
1957 | Some(Self { syntax }) | ||
1958 | } else { | ||
1959 | None | ||
1960 | } | ||
1961 | } | ||
1962 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1963 | } | ||
1964 | impl AstNode for LifetimeParam { | ||
1965 | fn can_cast(kind: SyntaxKind) -> bool { kind == LIFETIME_PARAM } | ||
1966 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1967 | if Self::can_cast(syntax.kind()) { | ||
1968 | Some(Self { syntax }) | ||
1969 | } else { | ||
1970 | None | ||
1971 | } | ||
1972 | } | ||
1973 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1974 | } | ||
1975 | impl AstNode for TypeParam { | ||
1976 | fn can_cast(kind: SyntaxKind) -> bool { kind == TYPE_PARAM } | ||
1977 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1978 | if Self::can_cast(syntax.kind()) { | ||
1979 | Some(Self { syntax }) | ||
1980 | } else { | ||
1981 | None | ||
1982 | } | ||
1983 | } | ||
1984 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1985 | } | ||
1986 | impl AstNode for WherePred { | ||
1987 | fn can_cast(kind: SyntaxKind) -> bool { kind == WHERE_PRED } | ||
1988 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1989 | if Self::can_cast(syntax.kind()) { | ||
1990 | Some(Self { syntax }) | ||
1991 | } else { | ||
1992 | None | ||
1993 | } | ||
1994 | } | ||
1995 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1996 | } | ||
1997 | impl AstNode for Literal { | ||
1998 | fn can_cast(kind: SyntaxKind) -> bool { kind == LITERAL } | ||
1999 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2000 | if Self::can_cast(syntax.kind()) { | ||
2001 | Some(Self { syntax }) | ||
2002 | } else { | ||
2003 | None | ||
2004 | } | ||
2005 | } | ||
2006 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2007 | } | ||
2008 | impl AstNode for ExprStmt { | ||
2009 | fn can_cast(kind: SyntaxKind) -> bool { kind == EXPR_STMT } | ||
2010 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2011 | if Self::can_cast(syntax.kind()) { | ||
2012 | Some(Self { syntax }) | ||
2013 | } else { | ||
2014 | None | ||
2015 | } | ||
2016 | } | ||
2017 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2018 | } | ||
2019 | impl AstNode for LetStmt { | ||
2020 | fn can_cast(kind: SyntaxKind) -> bool { kind == LET_STMT } | ||
2021 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2022 | if Self::can_cast(syntax.kind()) { | ||
2023 | Some(Self { syntax }) | ||
2024 | } else { | ||
2025 | None | ||
2026 | } | ||
2027 | } | ||
2028 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2029 | } | ||
2030 | impl AstNode for ArrayExpr { | ||
2031 | fn can_cast(kind: SyntaxKind) -> bool { kind == ARRAY_EXPR } | ||
2032 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2033 | if Self::can_cast(syntax.kind()) { | ||
2034 | Some(Self { syntax }) | ||
2035 | } else { | ||
2036 | None | ||
2037 | } | ||
2038 | } | ||
2039 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2040 | } | ||
2041 | impl AstNode for AwaitExpr { | ||
2042 | fn can_cast(kind: SyntaxKind) -> bool { kind == AWAIT_EXPR } | ||
2043 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2044 | if Self::can_cast(syntax.kind()) { | ||
2045 | Some(Self { syntax }) | ||
2046 | } else { | ||
2047 | None | ||
2048 | } | ||
2049 | } | ||
2050 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2051 | } | ||
2052 | impl AstNode for BinExpr { | ||
2053 | fn can_cast(kind: SyntaxKind) -> bool { kind == BIN_EXPR } | ||
2054 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2055 | if Self::can_cast(syntax.kind()) { | ||
2056 | Some(Self { syntax }) | ||
2057 | } else { | ||
2058 | None | ||
2059 | } | ||
2060 | } | ||
2061 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2062 | } | ||
2063 | impl AstNode for BoxExpr { | ||
2064 | fn can_cast(kind: SyntaxKind) -> bool { kind == BOX_EXPR } | ||
2065 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2066 | if Self::can_cast(syntax.kind()) { | ||
2067 | Some(Self { syntax }) | ||
2068 | } else { | ||
2069 | None | ||
2070 | } | ||
2071 | } | ||
2072 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2073 | } | ||
2074 | impl AstNode for BreakExpr { | ||
2075 | fn can_cast(kind: SyntaxKind) -> bool { kind == BREAK_EXPR } | ||
2076 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2077 | if Self::can_cast(syntax.kind()) { | ||
2078 | Some(Self { syntax }) | ||
2079 | } else { | ||
2080 | None | ||
2081 | } | ||
2082 | } | ||
2083 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2084 | } | ||
2085 | impl AstNode for CallExpr { | ||
2086 | fn can_cast(kind: SyntaxKind) -> bool { kind == CALL_EXPR } | ||
2087 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2088 | if Self::can_cast(syntax.kind()) { | ||
2089 | Some(Self { syntax }) | ||
2090 | } else { | ||
2091 | None | ||
2092 | } | ||
2093 | } | ||
2094 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2095 | } | ||
2096 | impl AstNode for CastExpr { | ||
2097 | fn can_cast(kind: SyntaxKind) -> bool { kind == CAST_EXPR } | ||
2098 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2099 | if Self::can_cast(syntax.kind()) { | ||
2100 | Some(Self { syntax }) | ||
2101 | } else { | ||
2102 | None | ||
2103 | } | ||
2104 | } | ||
2105 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2106 | } | ||
2107 | impl AstNode for ClosureExpr { | ||
2108 | fn can_cast(kind: SyntaxKind) -> bool { kind == CLOSURE_EXPR } | ||
2109 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2110 | if Self::can_cast(syntax.kind()) { | ||
2111 | Some(Self { syntax }) | ||
2112 | } else { | ||
2113 | None | ||
2114 | } | ||
2115 | } | ||
2116 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2117 | } | ||
2118 | impl AstNode for ContinueExpr { | ||
2119 | fn can_cast(kind: SyntaxKind) -> bool { kind == CONTINUE_EXPR } | ||
2120 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2121 | if Self::can_cast(syntax.kind()) { | ||
2122 | Some(Self { syntax }) | ||
2123 | } else { | ||
2124 | None | ||
2125 | } | ||
2126 | } | ||
2127 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2128 | } | ||
2129 | impl AstNode for EffectExpr { | ||
2130 | fn can_cast(kind: SyntaxKind) -> bool { kind == EFFECT_EXPR } | ||
2131 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2132 | if Self::can_cast(syntax.kind()) { | ||
2133 | Some(Self { syntax }) | ||
2134 | } else { | ||
2135 | None | ||
2136 | } | ||
2137 | } | ||
2138 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2139 | } | ||
2140 | impl AstNode for FieldExpr { | ||
2141 | fn can_cast(kind: SyntaxKind) -> bool { kind == FIELD_EXPR } | ||
2142 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2143 | if Self::can_cast(syntax.kind()) { | ||
2144 | Some(Self { syntax }) | ||
2145 | } else { | ||
2146 | None | ||
2147 | } | ||
2148 | } | ||
2149 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2150 | } | ||
2151 | impl AstNode for ForExpr { | ||
2152 | fn can_cast(kind: SyntaxKind) -> bool { kind == FOR_EXPR } | ||
2153 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2154 | if Self::can_cast(syntax.kind()) { | ||
2155 | Some(Self { syntax }) | ||
2156 | } else { | ||
2157 | None | ||
2158 | } | ||
2159 | } | ||
2160 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2161 | } | ||
2162 | impl AstNode for IfExpr { | ||
2163 | fn can_cast(kind: SyntaxKind) -> bool { kind == IF_EXPR } | ||
2164 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2165 | if Self::can_cast(syntax.kind()) { | ||
2166 | Some(Self { syntax }) | ||
2167 | } else { | ||
2168 | None | ||
2169 | } | ||
2170 | } | ||
2171 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2172 | } | ||
2173 | impl AstNode for IndexExpr { | ||
2174 | fn can_cast(kind: SyntaxKind) -> bool { kind == INDEX_EXPR } | ||
2175 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2176 | if Self::can_cast(syntax.kind()) { | ||
2177 | Some(Self { syntax }) | ||
2178 | } else { | ||
2179 | None | ||
2180 | } | ||
2181 | } | ||
2182 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2183 | } | ||
2184 | impl AstNode for LoopExpr { | ||
2185 | fn can_cast(kind: SyntaxKind) -> bool { kind == LOOP_EXPR } | ||
2186 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2187 | if Self::can_cast(syntax.kind()) { | ||
2188 | Some(Self { syntax }) | ||
2189 | } else { | ||
2190 | None | ||
2191 | } | ||
2192 | } | ||
2193 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2194 | } | ||
2195 | impl AstNode for MatchExpr { | ||
2196 | fn can_cast(kind: SyntaxKind) -> bool { kind == MATCH_EXPR } | ||
2197 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2198 | if Self::can_cast(syntax.kind()) { | ||
2199 | Some(Self { syntax }) | ||
2200 | } else { | ||
2201 | None | ||
2202 | } | ||
2203 | } | ||
2204 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2205 | } | ||
2206 | impl AstNode for MethodCallExpr { | ||
2207 | fn can_cast(kind: SyntaxKind) -> bool { kind == METHOD_CALL_EXPR } | ||
2208 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2209 | if Self::can_cast(syntax.kind()) { | ||
2210 | Some(Self { syntax }) | ||
2211 | } else { | ||
2212 | None | ||
2213 | } | ||
2214 | } | ||
2215 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2216 | } | ||
2217 | impl AstNode for ParenExpr { | ||
2218 | fn can_cast(kind: SyntaxKind) -> bool { kind == PAREN_EXPR } | ||
2219 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2220 | if Self::can_cast(syntax.kind()) { | ||
2221 | Some(Self { syntax }) | ||
2222 | } else { | ||
2223 | None | ||
2224 | } | ||
2225 | } | ||
2226 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2227 | } | ||
2228 | impl AstNode for PathExpr { | ||
2229 | fn can_cast(kind: SyntaxKind) -> bool { kind == PATH_EXPR } | ||
2230 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2231 | if Self::can_cast(syntax.kind()) { | ||
2232 | Some(Self { syntax }) | ||
2233 | } else { | ||
2234 | None | ||
2235 | } | ||
2236 | } | ||
2237 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2238 | } | ||
2239 | impl AstNode for PrefixExpr { | ||
2240 | fn can_cast(kind: SyntaxKind) -> bool { kind == PREFIX_EXPR } | ||
2241 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2242 | if Self::can_cast(syntax.kind()) { | ||
2243 | Some(Self { syntax }) | ||
2244 | } else { | ||
2245 | None | ||
2246 | } | ||
2247 | } | ||
2248 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2249 | } | ||
2250 | impl AstNode for RangeExpr { | ||
2251 | fn can_cast(kind: SyntaxKind) -> bool { kind == RANGE_EXPR } | ||
2252 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2253 | if Self::can_cast(syntax.kind()) { | ||
2254 | Some(Self { syntax }) | ||
2255 | } else { | ||
2256 | None | ||
2257 | } | ||
2258 | } | ||
2259 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2260 | } | ||
2261 | impl AstNode for RecordExpr { | ||
2262 | fn can_cast(kind: SyntaxKind) -> bool { kind == RECORD_EXPR } | ||
2263 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2264 | if Self::can_cast(syntax.kind()) { | ||
2265 | Some(Self { syntax }) | ||
2266 | } else { | ||
2267 | None | ||
2268 | } | ||
2269 | } | ||
2270 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2271 | } | ||
2272 | impl AstNode for RefExpr { | ||
2273 | fn can_cast(kind: SyntaxKind) -> bool { kind == REF_EXPR } | ||
2274 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2275 | if Self::can_cast(syntax.kind()) { | ||
2276 | Some(Self { syntax }) | ||
2277 | } else { | ||
2278 | None | ||
2279 | } | ||
2280 | } | ||
2281 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2282 | } | ||
2283 | impl AstNode for ReturnExpr { | ||
2284 | fn can_cast(kind: SyntaxKind) -> bool { kind == RETURN_EXPR } | ||
2285 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2286 | if Self::can_cast(syntax.kind()) { | ||
2287 | Some(Self { syntax }) | ||
2288 | } else { | ||
2289 | None | ||
2290 | } | ||
2291 | } | ||
2292 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2293 | } | ||
2294 | impl AstNode for TryExpr { | ||
2295 | fn can_cast(kind: SyntaxKind) -> bool { kind == TRY_EXPR } | ||
2296 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2297 | if Self::can_cast(syntax.kind()) { | ||
2298 | Some(Self { syntax }) | ||
2299 | } else { | ||
2300 | None | ||
2301 | } | ||
2302 | } | ||
2303 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2304 | } | ||
2305 | impl AstNode for TupleExpr { | ||
2306 | fn can_cast(kind: SyntaxKind) -> bool { kind == TUPLE_EXPR } | ||
2307 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2308 | if Self::can_cast(syntax.kind()) { | ||
2309 | Some(Self { syntax }) | ||
2310 | } else { | ||
2311 | None | ||
2312 | } | ||
2313 | } | ||
2314 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2315 | } | ||
2316 | impl AstNode for WhileExpr { | ||
2317 | fn can_cast(kind: SyntaxKind) -> bool { kind == WHILE_EXPR } | ||
2318 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2319 | if Self::can_cast(syntax.kind()) { | ||
2320 | Some(Self { syntax }) | ||
2321 | } else { | ||
2322 | None | ||
2323 | } | ||
2324 | } | ||
2325 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2326 | } | ||
2327 | impl AstNode for Label { | ||
2328 | fn can_cast(kind: SyntaxKind) -> bool { kind == LABEL } | ||
2329 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2330 | if Self::can_cast(syntax.kind()) { | ||
2331 | Some(Self { syntax }) | ||
2332 | } else { | ||
2333 | None | ||
2334 | } | ||
2335 | } | ||
2336 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2337 | } | ||
2338 | impl AstNode for RecordExprFieldList { | ||
2339 | fn can_cast(kind: SyntaxKind) -> bool { kind == RECORD_EXPR_FIELD_LIST } | ||
2340 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2341 | if Self::can_cast(syntax.kind()) { | ||
2342 | Some(Self { syntax }) | ||
2343 | } else { | ||
2344 | None | ||
2345 | } | ||
2346 | } | ||
2347 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2348 | } | ||
2349 | impl AstNode for RecordExprField { | ||
2350 | fn can_cast(kind: SyntaxKind) -> bool { kind == RECORD_EXPR_FIELD } | ||
2351 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2352 | if Self::can_cast(syntax.kind()) { | ||
2353 | Some(Self { syntax }) | ||
2354 | } else { | ||
2355 | None | ||
2356 | } | ||
2357 | } | ||
2358 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2359 | } | ||
2360 | impl AstNode for ArgList { | ||
2361 | fn can_cast(kind: SyntaxKind) -> bool { kind == ARG_LIST } | ||
2362 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2363 | if Self::can_cast(syntax.kind()) { | ||
2364 | Some(Self { syntax }) | ||
2365 | } else { | ||
2366 | None | ||
2367 | } | ||
2368 | } | ||
2369 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2370 | } | ||
2371 | impl AstNode for Condition { | ||
2372 | fn can_cast(kind: SyntaxKind) -> bool { kind == CONDITION } | ||
2373 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2374 | if Self::can_cast(syntax.kind()) { | ||
2375 | Some(Self { syntax }) | ||
2376 | } else { | ||
2377 | None | ||
2378 | } | ||
2379 | } | ||
2380 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2381 | } | ||
2382 | impl AstNode for MatchArmList { | ||
2383 | fn can_cast(kind: SyntaxKind) -> bool { kind == MATCH_ARM_LIST } | ||
2384 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2385 | if Self::can_cast(syntax.kind()) { | ||
2386 | Some(Self { syntax }) | ||
2387 | } else { | ||
2388 | None | ||
2389 | } | ||
2390 | } | ||
2391 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2392 | } | ||
2393 | impl AstNode for MatchArm { | ||
2394 | fn can_cast(kind: SyntaxKind) -> bool { kind == MATCH_ARM } | ||
2395 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2396 | if Self::can_cast(syntax.kind()) { | ||
2397 | Some(Self { syntax }) | ||
2398 | } else { | ||
2399 | None | ||
2400 | } | ||
2401 | } | ||
2402 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2403 | } | ||
2404 | impl AstNode for MatchGuard { | ||
2405 | fn can_cast(kind: SyntaxKind) -> bool { kind == MATCH_GUARD } | ||
2406 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2407 | if Self::can_cast(syntax.kind()) { | ||
2408 | Some(Self { syntax }) | ||
2409 | } else { | ||
2410 | None | ||
2411 | } | ||
2412 | } | ||
2413 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2414 | } | ||
2415 | impl AstNode for ArrayType { | ||
2416 | fn can_cast(kind: SyntaxKind) -> bool { kind == ARRAY_TYPE } | ||
2417 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2418 | if Self::can_cast(syntax.kind()) { | ||
2419 | Some(Self { syntax }) | ||
2420 | } else { | ||
2421 | None | ||
2422 | } | ||
2423 | } | ||
2424 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2425 | } | ||
2426 | impl AstNode for DynTraitType { | ||
2427 | fn can_cast(kind: SyntaxKind) -> bool { kind == DYN_TRAIT_TYPE } | ||
2428 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2429 | if Self::can_cast(syntax.kind()) { | ||
2430 | Some(Self { syntax }) | ||
2431 | } else { | ||
2432 | None | ||
2433 | } | ||
2434 | } | ||
2435 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2436 | } | ||
2437 | impl AstNode for FnPtrType { | ||
2438 | fn can_cast(kind: SyntaxKind) -> bool { kind == FN_PTR_TYPE } | ||
2439 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2440 | if Self::can_cast(syntax.kind()) { | ||
2441 | Some(Self { syntax }) | ||
2442 | } else { | ||
2443 | None | ||
2444 | } | ||
2445 | } | ||
2446 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2447 | } | ||
2448 | impl AstNode for ForType { | ||
2449 | fn can_cast(kind: SyntaxKind) -> bool { kind == FOR_TYPE } | ||
2450 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2451 | if Self::can_cast(syntax.kind()) { | ||
2452 | Some(Self { syntax }) | ||
2453 | } else { | ||
2454 | None | ||
2455 | } | ||
2456 | } | ||
2457 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2458 | } | ||
2459 | impl AstNode for ImplTraitType { | ||
2460 | fn can_cast(kind: SyntaxKind) -> bool { kind == IMPL_TRAIT_TYPE } | ||
2461 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2462 | if Self::can_cast(syntax.kind()) { | ||
2463 | Some(Self { syntax }) | ||
2464 | } else { | ||
2465 | None | ||
2466 | } | ||
2467 | } | ||
2468 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2469 | } | ||
2470 | impl AstNode for InferType { | ||
2471 | fn can_cast(kind: SyntaxKind) -> bool { kind == INFER_TYPE } | ||
2472 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2473 | if Self::can_cast(syntax.kind()) { | ||
2474 | Some(Self { syntax }) | ||
2475 | } else { | ||
2476 | None | ||
2477 | } | ||
2478 | } | ||
2479 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2480 | } | ||
2481 | impl AstNode for NeverType { | ||
2482 | fn can_cast(kind: SyntaxKind) -> bool { kind == NEVER_TYPE } | ||
2483 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2484 | if Self::can_cast(syntax.kind()) { | ||
2485 | Some(Self { syntax }) | ||
2486 | } else { | ||
2487 | None | ||
2488 | } | ||
2489 | } | ||
2490 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2491 | } | ||
2492 | impl AstNode for ParenType { | ||
2493 | fn can_cast(kind: SyntaxKind) -> bool { kind == PAREN_TYPE } | ||
2494 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2495 | if Self::can_cast(syntax.kind()) { | ||
2496 | Some(Self { syntax }) | ||
2497 | } else { | ||
2498 | None | ||
2499 | } | ||
2500 | } | ||
2501 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2502 | } | ||
2503 | impl AstNode for PtrType { | ||
2504 | fn can_cast(kind: SyntaxKind) -> bool { kind == PTR_TYPE } | ||
2505 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2506 | if Self::can_cast(syntax.kind()) { | ||
2507 | Some(Self { syntax }) | ||
2508 | } else { | ||
2509 | None | ||
2510 | } | ||
2511 | } | ||
2512 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2513 | } | ||
2514 | impl AstNode for RefType { | ||
2515 | fn can_cast(kind: SyntaxKind) -> bool { kind == REF_TYPE } | ||
2516 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2517 | if Self::can_cast(syntax.kind()) { | ||
2518 | Some(Self { syntax }) | ||
2519 | } else { | ||
2520 | None | ||
2521 | } | ||
2522 | } | ||
2523 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2524 | } | ||
2525 | impl AstNode for SliceType { | ||
2526 | fn can_cast(kind: SyntaxKind) -> bool { kind == SLICE_TYPE } | ||
2527 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2528 | if Self::can_cast(syntax.kind()) { | ||
2529 | Some(Self { syntax }) | ||
2530 | } else { | ||
2531 | None | ||
2532 | } | ||
2533 | } | ||
2534 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2535 | } | ||
2536 | impl AstNode for TupleType { | ||
2537 | fn can_cast(kind: SyntaxKind) -> bool { kind == TUPLE_TYPE } | ||
2538 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2539 | if Self::can_cast(syntax.kind()) { | ||
2540 | Some(Self { syntax }) | ||
2541 | } else { | ||
2542 | None | ||
2543 | } | ||
2544 | } | ||
2545 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2546 | } | ||
2547 | impl AstNode for TypeBound { | ||
2548 | fn can_cast(kind: SyntaxKind) -> bool { kind == TYPE_BOUND } | ||
2549 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2550 | if Self::can_cast(syntax.kind()) { | ||
2551 | Some(Self { syntax }) | ||
2552 | } else { | ||
2553 | None | ||
2554 | } | ||
2555 | } | ||
2556 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2557 | } | ||
2558 | impl AstNode for IdentPat { | ||
2559 | fn can_cast(kind: SyntaxKind) -> bool { kind == IDENT_PAT } | ||
2560 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2561 | if Self::can_cast(syntax.kind()) { | ||
2562 | Some(Self { syntax }) | ||
2563 | } else { | ||
2564 | None | ||
2565 | } | ||
2566 | } | ||
2567 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2568 | } | ||
2569 | impl AstNode for BoxPat { | ||
2570 | fn can_cast(kind: SyntaxKind) -> bool { kind == BOX_PAT } | ||
2571 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2572 | if Self::can_cast(syntax.kind()) { | ||
2573 | Some(Self { syntax }) | ||
2574 | } else { | ||
2575 | None | ||
2576 | } | ||
2577 | } | ||
2578 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2579 | } | ||
2580 | impl AstNode for RestPat { | ||
2581 | fn can_cast(kind: SyntaxKind) -> bool { kind == REST_PAT } | ||
2582 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2583 | if Self::can_cast(syntax.kind()) { | ||
2584 | Some(Self { syntax }) | ||
2585 | } else { | ||
2586 | None | ||
2587 | } | ||
2588 | } | ||
2589 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2590 | } | ||
2591 | impl AstNode for LiteralPat { | ||
2592 | fn can_cast(kind: SyntaxKind) -> bool { kind == LITERAL_PAT } | ||
2593 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2594 | if Self::can_cast(syntax.kind()) { | ||
2595 | Some(Self { syntax }) | ||
2596 | } else { | ||
2597 | None | ||
2598 | } | ||
2599 | } | ||
2600 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2601 | } | ||
2602 | impl AstNode for MacroPat { | ||
2603 | fn can_cast(kind: SyntaxKind) -> bool { kind == MACRO_PAT } | ||
2604 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2605 | if Self::can_cast(syntax.kind()) { | ||
2606 | Some(Self { syntax }) | ||
2607 | } else { | ||
2608 | None | ||
2609 | } | ||
2610 | } | ||
2611 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2612 | } | ||
2613 | impl AstNode for OrPat { | ||
2614 | fn can_cast(kind: SyntaxKind) -> bool { kind == OR_PAT } | ||
2615 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2616 | if Self::can_cast(syntax.kind()) { | ||
2617 | Some(Self { syntax }) | ||
2618 | } else { | ||
2619 | None | ||
2620 | } | ||
2621 | } | ||
2622 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2623 | } | ||
2624 | impl AstNode for ParenPat { | ||
2625 | fn can_cast(kind: SyntaxKind) -> bool { kind == PAREN_PAT } | ||
2626 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2627 | if Self::can_cast(syntax.kind()) { | ||
2628 | Some(Self { syntax }) | ||
2629 | } else { | ||
2630 | None | ||
2631 | } | ||
2632 | } | ||
2633 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2634 | } | ||
2635 | impl AstNode for PathPat { | ||
2636 | fn can_cast(kind: SyntaxKind) -> bool { kind == PATH_PAT } | ||
2637 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2638 | if Self::can_cast(syntax.kind()) { | ||
2639 | Some(Self { syntax }) | ||
2640 | } else { | ||
2641 | None | ||
2642 | } | ||
2643 | } | ||
2644 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2645 | } | ||
2646 | impl AstNode for WildcardPat { | ||
2647 | fn can_cast(kind: SyntaxKind) -> bool { kind == WILDCARD_PAT } | ||
2648 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2649 | if Self::can_cast(syntax.kind()) { | ||
2650 | Some(Self { syntax }) | ||
2651 | } else { | ||
2652 | None | ||
2653 | } | ||
2654 | } | ||
2655 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2656 | } | ||
2657 | impl AstNode for RangePat { | ||
2658 | fn can_cast(kind: SyntaxKind) -> bool { kind == RANGE_PAT } | ||
2659 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2660 | if Self::can_cast(syntax.kind()) { | ||
2661 | Some(Self { syntax }) | ||
2662 | } else { | ||
2663 | None | ||
2664 | } | ||
2665 | } | ||
2666 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2667 | } | ||
2668 | impl AstNode for RecordPat { | ||
2669 | fn can_cast(kind: SyntaxKind) -> bool { kind == RECORD_PAT } | ||
2670 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2671 | if Self::can_cast(syntax.kind()) { | ||
2672 | Some(Self { syntax }) | ||
2673 | } else { | ||
2674 | None | ||
2675 | } | ||
2676 | } | ||
2677 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2678 | } | ||
2679 | impl AstNode for RefPat { | ||
2680 | fn can_cast(kind: SyntaxKind) -> bool { kind == REF_PAT } | ||
2681 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2682 | if Self::can_cast(syntax.kind()) { | ||
2683 | Some(Self { syntax }) | ||
2684 | } else { | ||
2685 | None | ||
2686 | } | ||
2687 | } | ||
2688 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2689 | } | ||
2690 | impl AstNode for SlicePat { | ||
2691 | fn can_cast(kind: SyntaxKind) -> bool { kind == SLICE_PAT } | ||
2692 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2693 | if Self::can_cast(syntax.kind()) { | ||
2694 | Some(Self { syntax }) | ||
2695 | } else { | ||
2696 | None | ||
2697 | } | ||
2698 | } | ||
2699 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2700 | } | ||
2701 | impl AstNode for TuplePat { | ||
2702 | fn can_cast(kind: SyntaxKind) -> bool { kind == TUPLE_PAT } | ||
2703 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2704 | if Self::can_cast(syntax.kind()) { | ||
2705 | Some(Self { syntax }) | ||
2706 | } else { | ||
2707 | None | ||
2708 | } | ||
2709 | } | ||
2710 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2711 | } | ||
2712 | impl AstNode for TupleStructPat { | ||
2713 | fn can_cast(kind: SyntaxKind) -> bool { kind == TUPLE_STRUCT_PAT } | ||
2714 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2715 | if Self::can_cast(syntax.kind()) { | ||
2716 | Some(Self { syntax }) | ||
2717 | } else { | ||
2718 | None | ||
2719 | } | ||
2720 | } | ||
2721 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2722 | } | ||
2723 | impl AstNode for RecordPatFieldList { | ||
2724 | fn can_cast(kind: SyntaxKind) -> bool { kind == RECORD_PAT_FIELD_LIST } | ||
2725 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2726 | if Self::can_cast(syntax.kind()) { | ||
2727 | Some(Self { syntax }) | ||
2728 | } else { | ||
2729 | None | ||
2730 | } | ||
2731 | } | ||
2732 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2733 | } | ||
2734 | impl AstNode for RecordPatField { | ||
2735 | fn can_cast(kind: SyntaxKind) -> bool { kind == RECORD_PAT_FIELD } | ||
2736 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2737 | if Self::can_cast(syntax.kind()) { | ||
2738 | Some(Self { syntax }) | ||
2739 | } else { | ||
2740 | None | ||
2741 | } | ||
2742 | } | ||
2743 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2744 | } | ||
2745 | impl From<TypeArg> for GenericArg { | ||
2746 | fn from(node: TypeArg) -> GenericArg { GenericArg::TypeArg(node) } | ||
2747 | } | ||
2748 | impl From<AssocTypeArg> for GenericArg { | ||
2749 | fn from(node: AssocTypeArg) -> GenericArg { GenericArg::AssocTypeArg(node) } | ||
2750 | } | ||
2751 | impl From<LifetimeArg> for GenericArg { | ||
2752 | fn from(node: LifetimeArg) -> GenericArg { GenericArg::LifetimeArg(node) } | ||
2753 | } | ||
2754 | impl From<ConstArg> for GenericArg { | ||
2755 | fn from(node: ConstArg) -> GenericArg { GenericArg::ConstArg(node) } | ||
2756 | } | ||
2757 | impl AstNode for GenericArg { | ||
2758 | fn can_cast(kind: SyntaxKind) -> bool { | ||
2759 | match kind { | ||
2760 | TYPE_ARG | ASSOC_TYPE_ARG | LIFETIME_ARG | CONST_ARG => true, | ||
2761 | _ => false, | ||
2762 | } | ||
2763 | } | ||
2764 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2765 | let res = match syntax.kind() { | ||
2766 | TYPE_ARG => GenericArg::TypeArg(TypeArg { syntax }), | ||
2767 | ASSOC_TYPE_ARG => GenericArg::AssocTypeArg(AssocTypeArg { syntax }), | ||
2768 | LIFETIME_ARG => GenericArg::LifetimeArg(LifetimeArg { syntax }), | ||
2769 | CONST_ARG => GenericArg::ConstArg(ConstArg { syntax }), | ||
2770 | _ => return None, | ||
2771 | }; | ||
2772 | Some(res) | ||
2773 | } | ||
2774 | fn syntax(&self) -> &SyntaxNode { | ||
2775 | match self { | ||
2776 | GenericArg::TypeArg(it) => &it.syntax, | ||
2777 | GenericArg::AssocTypeArg(it) => &it.syntax, | ||
2778 | GenericArg::LifetimeArg(it) => &it.syntax, | ||
2779 | GenericArg::ConstArg(it) => &it.syntax, | ||
2780 | } | ||
2781 | } | ||
2782 | } | ||
2783 | impl From<ArrayType> for Type { | ||
2784 | fn from(node: ArrayType) -> Type { Type::ArrayType(node) } | ||
2785 | } | ||
2786 | impl From<DynTraitType> for Type { | ||
2787 | fn from(node: DynTraitType) -> Type { Type::DynTraitType(node) } | ||
2788 | } | ||
2789 | impl From<FnPtrType> for Type { | ||
2790 | fn from(node: FnPtrType) -> Type { Type::FnPtrType(node) } | ||
2791 | } | ||
2792 | impl From<ForType> for Type { | ||
2793 | fn from(node: ForType) -> Type { Type::ForType(node) } | ||
2794 | } | ||
2795 | impl From<ImplTraitType> for Type { | ||
2796 | fn from(node: ImplTraitType) -> Type { Type::ImplTraitType(node) } | ||
2797 | } | ||
2798 | impl From<InferType> for Type { | ||
2799 | fn from(node: InferType) -> Type { Type::InferType(node) } | ||
2800 | } | ||
2801 | impl From<NeverType> for Type { | ||
2802 | fn from(node: NeverType) -> Type { Type::NeverType(node) } | ||
2803 | } | ||
2804 | impl From<ParenType> for Type { | ||
2805 | fn from(node: ParenType) -> Type { Type::ParenType(node) } | ||
2806 | } | ||
2807 | impl From<PathType> for Type { | ||
2808 | fn from(node: PathType) -> Type { Type::PathType(node) } | ||
2809 | } | ||
2810 | impl From<PtrType> for Type { | ||
2811 | fn from(node: PtrType) -> Type { Type::PtrType(node) } | ||
2812 | } | ||
2813 | impl From<RefType> for Type { | ||
2814 | fn from(node: RefType) -> Type { Type::RefType(node) } | ||
2815 | } | ||
2816 | impl From<SliceType> for Type { | ||
2817 | fn from(node: SliceType) -> Type { Type::SliceType(node) } | ||
2818 | } | ||
2819 | impl From<TupleType> for Type { | ||
2820 | fn from(node: TupleType) -> Type { Type::TupleType(node) } | ||
2821 | } | ||
2822 | impl AstNode for Type { | ||
2823 | fn can_cast(kind: SyntaxKind) -> bool { | ||
2824 | match kind { | ||
2825 | ARRAY_TYPE | DYN_TRAIT_TYPE | FN_PTR_TYPE | FOR_TYPE | IMPL_TRAIT_TYPE | INFER_TYPE | ||
2826 | | NEVER_TYPE | PAREN_TYPE | PATH_TYPE | PTR_TYPE | REF_TYPE | SLICE_TYPE | ||
2827 | | TUPLE_TYPE => true, | ||
2828 | _ => false, | ||
2829 | } | ||
2830 | } | ||
2831 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2832 | let res = match syntax.kind() { | ||
2833 | ARRAY_TYPE => Type::ArrayType(ArrayType { syntax }), | ||
2834 | DYN_TRAIT_TYPE => Type::DynTraitType(DynTraitType { syntax }), | ||
2835 | FN_PTR_TYPE => Type::FnPtrType(FnPtrType { syntax }), | ||
2836 | FOR_TYPE => Type::ForType(ForType { syntax }), | ||
2837 | IMPL_TRAIT_TYPE => Type::ImplTraitType(ImplTraitType { syntax }), | ||
2838 | INFER_TYPE => Type::InferType(InferType { syntax }), | ||
2839 | NEVER_TYPE => Type::NeverType(NeverType { syntax }), | ||
2840 | PAREN_TYPE => Type::ParenType(ParenType { syntax }), | ||
2841 | PATH_TYPE => Type::PathType(PathType { syntax }), | ||
2842 | PTR_TYPE => Type::PtrType(PtrType { syntax }), | ||
2843 | REF_TYPE => Type::RefType(RefType { syntax }), | ||
2844 | SLICE_TYPE => Type::SliceType(SliceType { syntax }), | ||
2845 | TUPLE_TYPE => Type::TupleType(TupleType { syntax }), | ||
2846 | _ => return None, | ||
2847 | }; | ||
2848 | Some(res) | ||
2849 | } | ||
2850 | fn syntax(&self) -> &SyntaxNode { | ||
2851 | match self { | ||
2852 | Type::ArrayType(it) => &it.syntax, | ||
2853 | Type::DynTraitType(it) => &it.syntax, | ||
2854 | Type::FnPtrType(it) => &it.syntax, | ||
2855 | Type::ForType(it) => &it.syntax, | ||
2856 | Type::ImplTraitType(it) => &it.syntax, | ||
2857 | Type::InferType(it) => &it.syntax, | ||
2858 | Type::NeverType(it) => &it.syntax, | ||
2859 | Type::ParenType(it) => &it.syntax, | ||
2860 | Type::PathType(it) => &it.syntax, | ||
2861 | Type::PtrType(it) => &it.syntax, | ||
2862 | Type::RefType(it) => &it.syntax, | ||
2863 | Type::SliceType(it) => &it.syntax, | ||
2864 | Type::TupleType(it) => &it.syntax, | ||
2865 | } | ||
2866 | } | ||
2867 | } | ||
2868 | impl From<ArrayExpr> for Expr { | ||
2869 | fn from(node: ArrayExpr) -> Expr { Expr::ArrayExpr(node) } | ||
2870 | } | ||
2871 | impl From<AwaitExpr> for Expr { | ||
2872 | fn from(node: AwaitExpr) -> Expr { Expr::AwaitExpr(node) } | ||
2873 | } | ||
2874 | impl From<BinExpr> for Expr { | ||
2875 | fn from(node: BinExpr) -> Expr { Expr::BinExpr(node) } | ||
2876 | } | ||
2877 | impl From<BlockExpr> for Expr { | ||
2878 | fn from(node: BlockExpr) -> Expr { Expr::BlockExpr(node) } | ||
2879 | } | ||
2880 | impl From<BoxExpr> for Expr { | ||
2881 | fn from(node: BoxExpr) -> Expr { Expr::BoxExpr(node) } | ||
2882 | } | ||
2883 | impl From<BreakExpr> for Expr { | ||
2884 | fn from(node: BreakExpr) -> Expr { Expr::BreakExpr(node) } | ||
2885 | } | ||
2886 | impl From<CallExpr> for Expr { | ||
2887 | fn from(node: CallExpr) -> Expr { Expr::CallExpr(node) } | ||
2888 | } | ||
2889 | impl From<CastExpr> for Expr { | ||
2890 | fn from(node: CastExpr) -> Expr { Expr::CastExpr(node) } | ||
2891 | } | ||
2892 | impl From<ClosureExpr> for Expr { | ||
2893 | fn from(node: ClosureExpr) -> Expr { Expr::ClosureExpr(node) } | ||
2894 | } | ||
2895 | impl From<ContinueExpr> for Expr { | ||
2896 | fn from(node: ContinueExpr) -> Expr { Expr::ContinueExpr(node) } | ||
2897 | } | ||
2898 | impl From<EffectExpr> for Expr { | ||
2899 | fn from(node: EffectExpr) -> Expr { Expr::EffectExpr(node) } | ||
2900 | } | ||
2901 | impl From<FieldExpr> for Expr { | ||
2902 | fn from(node: FieldExpr) -> Expr { Expr::FieldExpr(node) } | ||
2903 | } | ||
2904 | impl From<ForExpr> for Expr { | ||
2905 | fn from(node: ForExpr) -> Expr { Expr::ForExpr(node) } | ||
2906 | } | ||
2907 | impl From<IfExpr> for Expr { | ||
2908 | fn from(node: IfExpr) -> Expr { Expr::IfExpr(node) } | ||
2909 | } | ||
2910 | impl From<IndexExpr> for Expr { | ||
2911 | fn from(node: IndexExpr) -> Expr { Expr::IndexExpr(node) } | ||
2912 | } | ||
2913 | impl From<Literal> for Expr { | ||
2914 | fn from(node: Literal) -> Expr { Expr::Literal(node) } | ||
2915 | } | ||
2916 | impl From<LoopExpr> for Expr { | ||
2917 | fn from(node: LoopExpr) -> Expr { Expr::LoopExpr(node) } | ||
2918 | } | ||
2919 | impl From<MacroCall> for Expr { | ||
2920 | fn from(node: MacroCall) -> Expr { Expr::MacroCall(node) } | ||
2921 | } | ||
2922 | impl From<MatchExpr> for Expr { | ||
2923 | fn from(node: MatchExpr) -> Expr { Expr::MatchExpr(node) } | ||
2924 | } | ||
2925 | impl From<MethodCallExpr> for Expr { | ||
2926 | fn from(node: MethodCallExpr) -> Expr { Expr::MethodCallExpr(node) } | ||
2927 | } | ||
2928 | impl From<ParenExpr> for Expr { | ||
2929 | fn from(node: ParenExpr) -> Expr { Expr::ParenExpr(node) } | ||
2930 | } | ||
2931 | impl From<PathExpr> for Expr { | ||
2932 | fn from(node: PathExpr) -> Expr { Expr::PathExpr(node) } | ||
2933 | } | ||
2934 | impl From<PrefixExpr> for Expr { | ||
2935 | fn from(node: PrefixExpr) -> Expr { Expr::PrefixExpr(node) } | ||
2936 | } | ||
2937 | impl From<RangeExpr> for Expr { | ||
2938 | fn from(node: RangeExpr) -> Expr { Expr::RangeExpr(node) } | ||
2939 | } | ||
2940 | impl From<RecordExpr> for Expr { | ||
2941 | fn from(node: RecordExpr) -> Expr { Expr::RecordExpr(node) } | ||
2942 | } | ||
2943 | impl From<RefExpr> for Expr { | ||
2944 | fn from(node: RefExpr) -> Expr { Expr::RefExpr(node) } | ||
2945 | } | ||
2946 | impl From<ReturnExpr> for Expr { | ||
2947 | fn from(node: ReturnExpr) -> Expr { Expr::ReturnExpr(node) } | ||
2948 | } | ||
2949 | impl From<TryExpr> for Expr { | ||
2950 | fn from(node: TryExpr) -> Expr { Expr::TryExpr(node) } | ||
2951 | } | ||
2952 | impl From<TupleExpr> for Expr { | ||
2953 | fn from(node: TupleExpr) -> Expr { Expr::TupleExpr(node) } | ||
2954 | } | ||
2955 | impl From<WhileExpr> for Expr { | ||
2956 | fn from(node: WhileExpr) -> Expr { Expr::WhileExpr(node) } | ||
2957 | } | ||
2958 | impl AstNode for Expr { | ||
2959 | fn can_cast(kind: SyntaxKind) -> bool { | ||
2960 | match kind { | ||
2961 | ARRAY_EXPR | AWAIT_EXPR | BIN_EXPR | BLOCK_EXPR | BOX_EXPR | BREAK_EXPR | CALL_EXPR | ||
2962 | | CAST_EXPR | CLOSURE_EXPR | CONTINUE_EXPR | EFFECT_EXPR | FIELD_EXPR | FOR_EXPR | ||
2963 | | IF_EXPR | INDEX_EXPR | LITERAL | LOOP_EXPR | MACRO_CALL | MATCH_EXPR | ||
2964 | | METHOD_CALL_EXPR | PAREN_EXPR | PATH_EXPR | PREFIX_EXPR | RANGE_EXPR | ||
2965 | | RECORD_EXPR | REF_EXPR | RETURN_EXPR | TRY_EXPR | TUPLE_EXPR | WHILE_EXPR => true, | ||
2966 | _ => false, | ||
2967 | } | ||
2968 | } | ||
2969 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2970 | let res = match syntax.kind() { | ||
2971 | ARRAY_EXPR => Expr::ArrayExpr(ArrayExpr { syntax }), | ||
2972 | AWAIT_EXPR => Expr::AwaitExpr(AwaitExpr { syntax }), | ||
2973 | BIN_EXPR => Expr::BinExpr(BinExpr { syntax }), | ||
2974 | BLOCK_EXPR => Expr::BlockExpr(BlockExpr { syntax }), | ||
2975 | BOX_EXPR => Expr::BoxExpr(BoxExpr { syntax }), | ||
2976 | BREAK_EXPR => Expr::BreakExpr(BreakExpr { syntax }), | ||
2977 | CALL_EXPR => Expr::CallExpr(CallExpr { syntax }), | ||
2978 | CAST_EXPR => Expr::CastExpr(CastExpr { syntax }), | ||
2979 | CLOSURE_EXPR => Expr::ClosureExpr(ClosureExpr { syntax }), | ||
2980 | CONTINUE_EXPR => Expr::ContinueExpr(ContinueExpr { syntax }), | ||
2981 | EFFECT_EXPR => Expr::EffectExpr(EffectExpr { syntax }), | ||
2982 | FIELD_EXPR => Expr::FieldExpr(FieldExpr { syntax }), | ||
2983 | FOR_EXPR => Expr::ForExpr(ForExpr { syntax }), | ||
2984 | IF_EXPR => Expr::IfExpr(IfExpr { syntax }), | ||
2985 | INDEX_EXPR => Expr::IndexExpr(IndexExpr { syntax }), | ||
2986 | LITERAL => Expr::Literal(Literal { syntax }), | ||
2987 | LOOP_EXPR => Expr::LoopExpr(LoopExpr { syntax }), | ||
2988 | MACRO_CALL => Expr::MacroCall(MacroCall { syntax }), | ||
2989 | MATCH_EXPR => Expr::MatchExpr(MatchExpr { syntax }), | ||
2990 | METHOD_CALL_EXPR => Expr::MethodCallExpr(MethodCallExpr { syntax }), | ||
2991 | PAREN_EXPR => Expr::ParenExpr(ParenExpr { syntax }), | ||
2992 | PATH_EXPR => Expr::PathExpr(PathExpr { syntax }), | ||
2993 | PREFIX_EXPR => Expr::PrefixExpr(PrefixExpr { syntax }), | ||
2994 | RANGE_EXPR => Expr::RangeExpr(RangeExpr { syntax }), | ||
2995 | RECORD_EXPR => Expr::RecordExpr(RecordExpr { syntax }), | ||
2996 | REF_EXPR => Expr::RefExpr(RefExpr { syntax }), | ||
2997 | RETURN_EXPR => Expr::ReturnExpr(ReturnExpr { syntax }), | ||
2998 | TRY_EXPR => Expr::TryExpr(TryExpr { syntax }), | ||
2999 | TUPLE_EXPR => Expr::TupleExpr(TupleExpr { syntax }), | ||
3000 | WHILE_EXPR => Expr::WhileExpr(WhileExpr { syntax }), | ||
3001 | _ => return None, | ||
3002 | }; | ||
3003 | Some(res) | ||
3004 | } | ||
3005 | fn syntax(&self) -> &SyntaxNode { | ||
3006 | match self { | ||
3007 | Expr::ArrayExpr(it) => &it.syntax, | ||
3008 | Expr::AwaitExpr(it) => &it.syntax, | ||
3009 | Expr::BinExpr(it) => &it.syntax, | ||
3010 | Expr::BlockExpr(it) => &it.syntax, | ||
3011 | Expr::BoxExpr(it) => &it.syntax, | ||
3012 | Expr::BreakExpr(it) => &it.syntax, | ||
3013 | Expr::CallExpr(it) => &it.syntax, | ||
3014 | Expr::CastExpr(it) => &it.syntax, | ||
3015 | Expr::ClosureExpr(it) => &it.syntax, | ||
3016 | Expr::ContinueExpr(it) => &it.syntax, | ||
3017 | Expr::EffectExpr(it) => &it.syntax, | ||
3018 | Expr::FieldExpr(it) => &it.syntax, | ||
3019 | Expr::ForExpr(it) => &it.syntax, | ||
3020 | Expr::IfExpr(it) => &it.syntax, | ||
3021 | Expr::IndexExpr(it) => &it.syntax, | ||
3022 | Expr::Literal(it) => &it.syntax, | ||
3023 | Expr::LoopExpr(it) => &it.syntax, | ||
3024 | Expr::MacroCall(it) => &it.syntax, | ||
3025 | Expr::MatchExpr(it) => &it.syntax, | ||
3026 | Expr::MethodCallExpr(it) => &it.syntax, | ||
3027 | Expr::ParenExpr(it) => &it.syntax, | ||
3028 | Expr::PathExpr(it) => &it.syntax, | ||
3029 | Expr::PrefixExpr(it) => &it.syntax, | ||
3030 | Expr::RangeExpr(it) => &it.syntax, | ||
3031 | Expr::RecordExpr(it) => &it.syntax, | ||
3032 | Expr::RefExpr(it) => &it.syntax, | ||
3033 | Expr::ReturnExpr(it) => &it.syntax, | ||
3034 | Expr::TryExpr(it) => &it.syntax, | ||
3035 | Expr::TupleExpr(it) => &it.syntax, | ||
3036 | Expr::WhileExpr(it) => &it.syntax, | ||
3037 | } | ||
3038 | } | ||
3039 | } | ||
3040 | impl From<Const> for Item { | ||
3041 | fn from(node: Const) -> Item { Item::Const(node) } | ||
3042 | } | ||
3043 | impl From<Enum> for Item { | ||
3044 | fn from(node: Enum) -> Item { Item::Enum(node) } | ||
3045 | } | ||
3046 | impl From<ExternBlock> for Item { | ||
3047 | fn from(node: ExternBlock) -> Item { Item::ExternBlock(node) } | ||
3048 | } | ||
3049 | impl From<ExternCrate> for Item { | ||
3050 | fn from(node: ExternCrate) -> Item { Item::ExternCrate(node) } | ||
3051 | } | ||
3052 | impl From<Fn> for Item { | ||
3053 | fn from(node: Fn) -> Item { Item::Fn(node) } | ||
3054 | } | ||
3055 | impl From<Impl> for Item { | ||
3056 | fn from(node: Impl) -> Item { Item::Impl(node) } | ||
3057 | } | ||
3058 | impl From<MacroCall> for Item { | ||
3059 | fn from(node: MacroCall) -> Item { Item::MacroCall(node) } | ||
3060 | } | ||
3061 | impl From<Module> for Item { | ||
3062 | fn from(node: Module) -> Item { Item::Module(node) } | ||
3063 | } | ||
3064 | impl From<Static> for Item { | ||
3065 | fn from(node: Static) -> Item { Item::Static(node) } | ||
3066 | } | ||
3067 | impl From<Struct> for Item { | ||
3068 | fn from(node: Struct) -> Item { Item::Struct(node) } | ||
3069 | } | ||
3070 | impl From<Trait> for Item { | ||
3071 | fn from(node: Trait) -> Item { Item::Trait(node) } | ||
3072 | } | ||
3073 | impl From<TypeAlias> for Item { | ||
3074 | fn from(node: TypeAlias) -> Item { Item::TypeAlias(node) } | ||
3075 | } | ||
3076 | impl From<Union> for Item { | ||
3077 | fn from(node: Union) -> Item { Item::Union(node) } | ||
3078 | } | ||
3079 | impl From<Use> for Item { | ||
3080 | fn from(node: Use) -> Item { Item::Use(node) } | ||
3081 | } | ||
3082 | impl AstNode for Item { | ||
3083 | fn can_cast(kind: SyntaxKind) -> bool { | ||
3084 | match kind { | ||
3085 | CONST | ENUM | EXTERN_BLOCK | EXTERN_CRATE | FN | IMPL | MACRO_CALL | MODULE | ||
3086 | | STATIC | STRUCT | TRAIT | TYPE_ALIAS | UNION | USE => true, | ||
3087 | _ => false, | ||
3088 | } | ||
3089 | } | ||
3090 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
3091 | let res = match syntax.kind() { | ||
3092 | CONST => Item::Const(Const { syntax }), | ||
3093 | ENUM => Item::Enum(Enum { syntax }), | ||
3094 | EXTERN_BLOCK => Item::ExternBlock(ExternBlock { syntax }), | ||
3095 | EXTERN_CRATE => Item::ExternCrate(ExternCrate { syntax }), | ||
3096 | FN => Item::Fn(Fn { syntax }), | ||
3097 | IMPL => Item::Impl(Impl { syntax }), | ||
3098 | MACRO_CALL => Item::MacroCall(MacroCall { syntax }), | ||
3099 | MODULE => Item::Module(Module { syntax }), | ||
3100 | STATIC => Item::Static(Static { syntax }), | ||
3101 | STRUCT => Item::Struct(Struct { syntax }), | ||
3102 | TRAIT => Item::Trait(Trait { syntax }), | ||
3103 | TYPE_ALIAS => Item::TypeAlias(TypeAlias { syntax }), | ||
3104 | UNION => Item::Union(Union { syntax }), | ||
3105 | USE => Item::Use(Use { syntax }), | ||
3106 | _ => return None, | ||
3107 | }; | ||
3108 | Some(res) | ||
3109 | } | ||
3110 | fn syntax(&self) -> &SyntaxNode { | ||
3111 | match self { | ||
3112 | Item::Const(it) => &it.syntax, | ||
3113 | Item::Enum(it) => &it.syntax, | ||
3114 | Item::ExternBlock(it) => &it.syntax, | ||
3115 | Item::ExternCrate(it) => &it.syntax, | ||
3116 | Item::Fn(it) => &it.syntax, | ||
3117 | Item::Impl(it) => &it.syntax, | ||
3118 | Item::MacroCall(it) => &it.syntax, | ||
3119 | Item::Module(it) => &it.syntax, | ||
3120 | Item::Static(it) => &it.syntax, | ||
3121 | Item::Struct(it) => &it.syntax, | ||
3122 | Item::Trait(it) => &it.syntax, | ||
3123 | Item::TypeAlias(it) => &it.syntax, | ||
3124 | Item::Union(it) => &it.syntax, | ||
3125 | Item::Use(it) => &it.syntax, | ||
3126 | } | ||
3127 | } | ||
3128 | } | ||
3129 | impl From<ExprStmt> for Stmt { | ||
3130 | fn from(node: ExprStmt) -> Stmt { Stmt::ExprStmt(node) } | ||
3131 | } | ||
3132 | impl From<Item> for Stmt { | ||
3133 | fn from(node: Item) -> Stmt { Stmt::Item(node) } | ||
3134 | } | ||
3135 | impl From<LetStmt> for Stmt { | ||
3136 | fn from(node: LetStmt) -> Stmt { Stmt::LetStmt(node) } | ||
3137 | } | ||
3138 | impl From<IdentPat> for Pat { | ||
3139 | fn from(node: IdentPat) -> Pat { Pat::IdentPat(node) } | ||
3140 | } | ||
3141 | impl From<BoxPat> for Pat { | ||
3142 | fn from(node: BoxPat) -> Pat { Pat::BoxPat(node) } | ||
3143 | } | ||
3144 | impl From<RestPat> for Pat { | ||
3145 | fn from(node: RestPat) -> Pat { Pat::RestPat(node) } | ||
3146 | } | ||
3147 | impl From<LiteralPat> for Pat { | ||
3148 | fn from(node: LiteralPat) -> Pat { Pat::LiteralPat(node) } | ||
3149 | } | ||
3150 | impl From<MacroPat> for Pat { | ||
3151 | fn from(node: MacroPat) -> Pat { Pat::MacroPat(node) } | ||
3152 | } | ||
3153 | impl From<OrPat> for Pat { | ||
3154 | fn from(node: OrPat) -> Pat { Pat::OrPat(node) } | ||
3155 | } | ||
3156 | impl From<ParenPat> for Pat { | ||
3157 | fn from(node: ParenPat) -> Pat { Pat::ParenPat(node) } | ||
3158 | } | ||
3159 | impl From<PathPat> for Pat { | ||
3160 | fn from(node: PathPat) -> Pat { Pat::PathPat(node) } | ||
3161 | } | ||
3162 | impl From<WildcardPat> for Pat { | ||
3163 | fn from(node: WildcardPat) -> Pat { Pat::WildcardPat(node) } | ||
3164 | } | ||
3165 | impl From<RangePat> for Pat { | ||
3166 | fn from(node: RangePat) -> Pat { Pat::RangePat(node) } | ||
3167 | } | ||
3168 | impl From<RecordPat> for Pat { | ||
3169 | fn from(node: RecordPat) -> Pat { Pat::RecordPat(node) } | ||
3170 | } | ||
3171 | impl From<RefPat> for Pat { | ||
3172 | fn from(node: RefPat) -> Pat { Pat::RefPat(node) } | ||
3173 | } | ||
3174 | impl From<SlicePat> for Pat { | ||
3175 | fn from(node: SlicePat) -> Pat { Pat::SlicePat(node) } | ||
3176 | } | ||
3177 | impl From<TuplePat> for Pat { | ||
3178 | fn from(node: TuplePat) -> Pat { Pat::TuplePat(node) } | ||
3179 | } | ||
3180 | impl From<TupleStructPat> for Pat { | ||
3181 | fn from(node: TupleStructPat) -> Pat { Pat::TupleStructPat(node) } | ||
3182 | } | ||
3183 | impl AstNode for Pat { | ||
3184 | fn can_cast(kind: SyntaxKind) -> bool { | ||
3185 | match kind { | ||
3186 | IDENT_PAT | BOX_PAT | REST_PAT | LITERAL_PAT | MACRO_PAT | OR_PAT | PAREN_PAT | ||
3187 | | PATH_PAT | WILDCARD_PAT | RANGE_PAT | RECORD_PAT | REF_PAT | SLICE_PAT | ||
3188 | | TUPLE_PAT | TUPLE_STRUCT_PAT => true, | ||
3189 | _ => false, | ||
3190 | } | ||
3191 | } | ||
3192 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
3193 | let res = match syntax.kind() { | ||
3194 | IDENT_PAT => Pat::IdentPat(IdentPat { syntax }), | ||
3195 | BOX_PAT => Pat::BoxPat(BoxPat { syntax }), | ||
3196 | REST_PAT => Pat::RestPat(RestPat { syntax }), | ||
3197 | LITERAL_PAT => Pat::LiteralPat(LiteralPat { syntax }), | ||
3198 | MACRO_PAT => Pat::MacroPat(MacroPat { syntax }), | ||
3199 | OR_PAT => Pat::OrPat(OrPat { syntax }), | ||
3200 | PAREN_PAT => Pat::ParenPat(ParenPat { syntax }), | ||
3201 | PATH_PAT => Pat::PathPat(PathPat { syntax }), | ||
3202 | WILDCARD_PAT => Pat::WildcardPat(WildcardPat { syntax }), | ||
3203 | RANGE_PAT => Pat::RangePat(RangePat { syntax }), | ||
3204 | RECORD_PAT => Pat::RecordPat(RecordPat { syntax }), | ||
3205 | REF_PAT => Pat::RefPat(RefPat { syntax }), | ||
3206 | SLICE_PAT => Pat::SlicePat(SlicePat { syntax }), | ||
3207 | TUPLE_PAT => Pat::TuplePat(TuplePat { syntax }), | ||
3208 | TUPLE_STRUCT_PAT => Pat::TupleStructPat(TupleStructPat { syntax }), | ||
3209 | _ => return None, | ||
3210 | }; | ||
3211 | Some(res) | ||
3212 | } | ||
3213 | fn syntax(&self) -> &SyntaxNode { | ||
3214 | match self { | ||
3215 | Pat::IdentPat(it) => &it.syntax, | ||
3216 | Pat::BoxPat(it) => &it.syntax, | ||
3217 | Pat::RestPat(it) => &it.syntax, | ||
3218 | Pat::LiteralPat(it) => &it.syntax, | ||
3219 | Pat::MacroPat(it) => &it.syntax, | ||
3220 | Pat::OrPat(it) => &it.syntax, | ||
3221 | Pat::ParenPat(it) => &it.syntax, | ||
3222 | Pat::PathPat(it) => &it.syntax, | ||
3223 | Pat::WildcardPat(it) => &it.syntax, | ||
3224 | Pat::RangePat(it) => &it.syntax, | ||
3225 | Pat::RecordPat(it) => &it.syntax, | ||
3226 | Pat::RefPat(it) => &it.syntax, | ||
3227 | Pat::SlicePat(it) => &it.syntax, | ||
3228 | Pat::TuplePat(it) => &it.syntax, | ||
3229 | Pat::TupleStructPat(it) => &it.syntax, | ||
3230 | } | ||
3231 | } | ||
3232 | } | ||
3233 | impl From<RecordFieldList> for FieldList { | ||
3234 | fn from(node: RecordFieldList) -> FieldList { FieldList::RecordFieldList(node) } | ||
3235 | } | ||
3236 | impl From<TupleFieldList> for FieldList { | ||
3237 | fn from(node: TupleFieldList) -> FieldList { FieldList::TupleFieldList(node) } | ||
3238 | } | ||
3239 | impl AstNode for FieldList { | ||
3240 | fn can_cast(kind: SyntaxKind) -> bool { | ||
3241 | match kind { | ||
3242 | RECORD_FIELD_LIST | TUPLE_FIELD_LIST => true, | ||
3243 | _ => false, | ||
3244 | } | ||
3245 | } | ||
3246 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
3247 | let res = match syntax.kind() { | ||
3248 | RECORD_FIELD_LIST => FieldList::RecordFieldList(RecordFieldList { syntax }), | ||
3249 | TUPLE_FIELD_LIST => FieldList::TupleFieldList(TupleFieldList { syntax }), | ||
3250 | _ => return None, | ||
3251 | }; | ||
3252 | Some(res) | ||
3253 | } | ||
3254 | fn syntax(&self) -> &SyntaxNode { | ||
3255 | match self { | ||
3256 | FieldList::RecordFieldList(it) => &it.syntax, | ||
3257 | FieldList::TupleFieldList(it) => &it.syntax, | ||
3258 | } | ||
3259 | } | ||
3260 | } | ||
3261 | impl From<Enum> for AdtDef { | ||
3262 | fn from(node: Enum) -> AdtDef { AdtDef::Enum(node) } | ||
3263 | } | ||
3264 | impl From<Struct> for AdtDef { | ||
3265 | fn from(node: Struct) -> AdtDef { AdtDef::Struct(node) } | ||
3266 | } | ||
3267 | impl From<Union> for AdtDef { | ||
3268 | fn from(node: Union) -> AdtDef { AdtDef::Union(node) } | ||
3269 | } | ||
3270 | impl AstNode for AdtDef { | ||
3271 | fn can_cast(kind: SyntaxKind) -> bool { | ||
3272 | match kind { | ||
3273 | ENUM | STRUCT | UNION => true, | ||
3274 | _ => false, | ||
3275 | } | ||
3276 | } | ||
3277 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
3278 | let res = match syntax.kind() { | ||
3279 | ENUM => AdtDef::Enum(Enum { syntax }), | ||
3280 | STRUCT => AdtDef::Struct(Struct { syntax }), | ||
3281 | UNION => AdtDef::Union(Union { syntax }), | ||
3282 | _ => return None, | ||
3283 | }; | ||
3284 | Some(res) | ||
3285 | } | ||
3286 | fn syntax(&self) -> &SyntaxNode { | ||
3287 | match self { | ||
3288 | AdtDef::Enum(it) => &it.syntax, | ||
3289 | AdtDef::Struct(it) => &it.syntax, | ||
3290 | AdtDef::Union(it) => &it.syntax, | ||
3291 | } | ||
3292 | } | ||
3293 | } | ||
3294 | impl From<Const> for AssocItem { | ||
3295 | fn from(node: Const) -> AssocItem { AssocItem::Const(node) } | ||
3296 | } | ||
3297 | impl From<Fn> for AssocItem { | ||
3298 | fn from(node: Fn) -> AssocItem { AssocItem::Fn(node) } | ||
3299 | } | ||
3300 | impl From<MacroCall> for AssocItem { | ||
3301 | fn from(node: MacroCall) -> AssocItem { AssocItem::MacroCall(node) } | ||
3302 | } | ||
3303 | impl From<TypeAlias> for AssocItem { | ||
3304 | fn from(node: TypeAlias) -> AssocItem { AssocItem::TypeAlias(node) } | ||
3305 | } | ||
3306 | impl AstNode for AssocItem { | ||
3307 | fn can_cast(kind: SyntaxKind) -> bool { | ||
3308 | match kind { | ||
3309 | CONST | FN | MACRO_CALL | TYPE_ALIAS => true, | ||
3310 | _ => false, | ||
3311 | } | ||
3312 | } | ||
3313 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
3314 | let res = match syntax.kind() { | ||
3315 | CONST => AssocItem::Const(Const { syntax }), | ||
3316 | FN => AssocItem::Fn(Fn { syntax }), | ||
3317 | MACRO_CALL => AssocItem::MacroCall(MacroCall { syntax }), | ||
3318 | TYPE_ALIAS => AssocItem::TypeAlias(TypeAlias { syntax }), | ||
3319 | _ => return None, | ||
3320 | }; | ||
3321 | Some(res) | ||
3322 | } | ||
3323 | fn syntax(&self) -> &SyntaxNode { | ||
3324 | match self { | ||
3325 | AssocItem::Const(it) => &it.syntax, | ||
3326 | AssocItem::Fn(it) => &it.syntax, | ||
3327 | AssocItem::MacroCall(it) => &it.syntax, | ||
3328 | AssocItem::TypeAlias(it) => &it.syntax, | ||
3329 | } | ||
3330 | } | ||
3331 | } | ||
3332 | impl From<Fn> for ExternItem { | ||
3333 | fn from(node: Fn) -> ExternItem { ExternItem::Fn(node) } | ||
3334 | } | ||
3335 | impl From<MacroCall> for ExternItem { | ||
3336 | fn from(node: MacroCall) -> ExternItem { ExternItem::MacroCall(node) } | ||
3337 | } | ||
3338 | impl From<Static> for ExternItem { | ||
3339 | fn from(node: Static) -> ExternItem { ExternItem::Static(node) } | ||
3340 | } | ||
3341 | impl AstNode for ExternItem { | ||
3342 | fn can_cast(kind: SyntaxKind) -> bool { | ||
3343 | match kind { | ||
3344 | FN | MACRO_CALL | STATIC => true, | ||
3345 | _ => false, | ||
3346 | } | ||
3347 | } | ||
3348 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
3349 | let res = match syntax.kind() { | ||
3350 | FN => ExternItem::Fn(Fn { syntax }), | ||
3351 | MACRO_CALL => ExternItem::MacroCall(MacroCall { syntax }), | ||
3352 | STATIC => ExternItem::Static(Static { syntax }), | ||
3353 | _ => return None, | ||
3354 | }; | ||
3355 | Some(res) | ||
3356 | } | ||
3357 | fn syntax(&self) -> &SyntaxNode { | ||
3358 | match self { | ||
3359 | ExternItem::Fn(it) => &it.syntax, | ||
3360 | ExternItem::MacroCall(it) => &it.syntax, | ||
3361 | ExternItem::Static(it) => &it.syntax, | ||
3362 | } | ||
3363 | } | ||
3364 | } | ||
3365 | impl From<ConstParam> for GenericParam { | ||
3366 | fn from(node: ConstParam) -> GenericParam { GenericParam::ConstParam(node) } | ||
3367 | } | ||
3368 | impl From<LifetimeParam> for GenericParam { | ||
3369 | fn from(node: LifetimeParam) -> GenericParam { GenericParam::LifetimeParam(node) } | ||
3370 | } | ||
3371 | impl From<TypeParam> for GenericParam { | ||
3372 | fn from(node: TypeParam) -> GenericParam { GenericParam::TypeParam(node) } | ||
3373 | } | ||
3374 | impl AstNode for GenericParam { | ||
3375 | fn can_cast(kind: SyntaxKind) -> bool { | ||
3376 | match kind { | ||
3377 | CONST_PARAM | LIFETIME_PARAM | TYPE_PARAM => true, | ||
3378 | _ => false, | ||
3379 | } | ||
3380 | } | ||
3381 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
3382 | let res = match syntax.kind() { | ||
3383 | CONST_PARAM => GenericParam::ConstParam(ConstParam { syntax }), | ||
3384 | LIFETIME_PARAM => GenericParam::LifetimeParam(LifetimeParam { syntax }), | ||
3385 | TYPE_PARAM => GenericParam::TypeParam(TypeParam { syntax }), | ||
3386 | _ => return None, | ||
3387 | }; | ||
3388 | Some(res) | ||
3389 | } | ||
3390 | fn syntax(&self) -> &SyntaxNode { | ||
3391 | match self { | ||
3392 | GenericParam::ConstParam(it) => &it.syntax, | ||
3393 | GenericParam::LifetimeParam(it) => &it.syntax, | ||
3394 | GenericParam::TypeParam(it) => &it.syntax, | ||
3395 | } | ||
3396 | } | ||
3397 | } | ||
3398 | impl std::fmt::Display for GenericArg { | ||
3399 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3400 | std::fmt::Display::fmt(self.syntax(), f) | ||
3401 | } | ||
3402 | } | ||
3403 | impl std::fmt::Display for Type { | ||
3404 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3405 | std::fmt::Display::fmt(self.syntax(), f) | ||
3406 | } | ||
3407 | } | ||
3408 | impl std::fmt::Display for Expr { | ||
3409 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3410 | std::fmt::Display::fmt(self.syntax(), f) | ||
3411 | } | ||
3412 | } | ||
3413 | impl std::fmt::Display for Item { | ||
3414 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3415 | std::fmt::Display::fmt(self.syntax(), f) | ||
3416 | } | ||
3417 | } | ||
3418 | impl std::fmt::Display for Stmt { | ||
3419 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3420 | std::fmt::Display::fmt(self.syntax(), f) | ||
3421 | } | ||
3422 | } | ||
3423 | impl std::fmt::Display for Pat { | ||
3424 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3425 | std::fmt::Display::fmt(self.syntax(), f) | ||
3426 | } | ||
3427 | } | ||
3428 | impl std::fmt::Display for FieldList { | ||
3429 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3430 | std::fmt::Display::fmt(self.syntax(), f) | ||
3431 | } | ||
3432 | } | ||
3433 | impl std::fmt::Display for AdtDef { | ||
3434 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3435 | std::fmt::Display::fmt(self.syntax(), f) | ||
3436 | } | ||
3437 | } | ||
3438 | impl std::fmt::Display for AssocItem { | ||
3439 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3440 | std::fmt::Display::fmt(self.syntax(), f) | ||
3441 | } | ||
3442 | } | ||
3443 | impl std::fmt::Display for ExternItem { | ||
3444 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3445 | std::fmt::Display::fmt(self.syntax(), f) | ||
3446 | } | ||
3447 | } | ||
3448 | impl std::fmt::Display for GenericParam { | ||
3449 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3450 | std::fmt::Display::fmt(self.syntax(), f) | ||
3451 | } | ||
3452 | } | ||
3453 | impl std::fmt::Display for Name { | ||
3454 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3455 | std::fmt::Display::fmt(self.syntax(), f) | ||
3456 | } | ||
3457 | } | ||
3458 | impl std::fmt::Display for NameRef { | ||
3459 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3460 | std::fmt::Display::fmt(self.syntax(), f) | ||
3461 | } | ||
3462 | } | ||
3463 | impl std::fmt::Display for Path { | ||
3464 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3465 | std::fmt::Display::fmt(self.syntax(), f) | ||
3466 | } | ||
3467 | } | ||
3468 | impl std::fmt::Display for PathSegment { | ||
3469 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3470 | std::fmt::Display::fmt(self.syntax(), f) | ||
3471 | } | ||
3472 | } | ||
3473 | impl std::fmt::Display for GenericArgList { | ||
3474 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3475 | std::fmt::Display::fmt(self.syntax(), f) | ||
3476 | } | ||
3477 | } | ||
3478 | impl std::fmt::Display for ParamList { | ||
3479 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3480 | std::fmt::Display::fmt(self.syntax(), f) | ||
3481 | } | ||
3482 | } | ||
3483 | impl std::fmt::Display for RetType { | ||
3484 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3485 | std::fmt::Display::fmt(self.syntax(), f) | ||
3486 | } | ||
3487 | } | ||
3488 | impl std::fmt::Display for PathType { | ||
3489 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3490 | std::fmt::Display::fmt(self.syntax(), f) | ||
3491 | } | ||
3492 | } | ||
3493 | impl std::fmt::Display for TypeArg { | ||
3494 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3495 | std::fmt::Display::fmt(self.syntax(), f) | ||
3496 | } | ||
3497 | } | ||
3498 | impl std::fmt::Display for AssocTypeArg { | ||
3499 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3500 | std::fmt::Display::fmt(self.syntax(), f) | ||
3501 | } | ||
3502 | } | ||
3503 | impl std::fmt::Display for LifetimeArg { | ||
3504 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3505 | std::fmt::Display::fmt(self.syntax(), f) | ||
3506 | } | ||
3507 | } | ||
3508 | impl std::fmt::Display for ConstArg { | ||
3509 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3510 | std::fmt::Display::fmt(self.syntax(), f) | ||
3511 | } | ||
3512 | } | ||
3513 | impl std::fmt::Display for TypeBoundList { | ||
3514 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3515 | std::fmt::Display::fmt(self.syntax(), f) | ||
3516 | } | ||
3517 | } | ||
3518 | impl std::fmt::Display for MacroCall { | ||
3519 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3520 | std::fmt::Display::fmt(self.syntax(), f) | ||
3521 | } | ||
3522 | } | ||
3523 | impl std::fmt::Display for Attr { | ||
3524 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3525 | std::fmt::Display::fmt(self.syntax(), f) | ||
3526 | } | ||
3527 | } | ||
3528 | impl std::fmt::Display for TokenTree { | ||
3529 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3530 | std::fmt::Display::fmt(self.syntax(), f) | ||
3531 | } | ||
3532 | } | ||
3533 | impl std::fmt::Display for MacroItems { | ||
3534 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3535 | std::fmt::Display::fmt(self.syntax(), f) | ||
3536 | } | ||
3537 | } | ||
3538 | impl std::fmt::Display for MacroStmts { | ||
3539 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3540 | std::fmt::Display::fmt(self.syntax(), f) | ||
3541 | } | ||
3542 | } | ||
3543 | impl std::fmt::Display for SourceFile { | ||
3544 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3545 | std::fmt::Display::fmt(self.syntax(), f) | ||
3546 | } | ||
3547 | } | ||
3548 | impl std::fmt::Display for Const { | ||
3549 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3550 | std::fmt::Display::fmt(self.syntax(), f) | ||
3551 | } | ||
3552 | } | ||
3553 | impl std::fmt::Display for Enum { | ||
3554 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3555 | std::fmt::Display::fmt(self.syntax(), f) | ||
3556 | } | ||
3557 | } | ||
3558 | impl std::fmt::Display for ExternBlock { | ||
3559 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3560 | std::fmt::Display::fmt(self.syntax(), f) | ||
3561 | } | ||
3562 | } | ||
3563 | impl std::fmt::Display for ExternCrate { | ||
3564 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3565 | std::fmt::Display::fmt(self.syntax(), f) | ||
3566 | } | ||
3567 | } | ||
3568 | impl std::fmt::Display for Fn { | ||
3569 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3570 | std::fmt::Display::fmt(self.syntax(), f) | ||
3571 | } | ||
3572 | } | ||
3573 | impl std::fmt::Display for Impl { | ||
3574 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3575 | std::fmt::Display::fmt(self.syntax(), f) | ||
3576 | } | ||
3577 | } | ||
3578 | impl std::fmt::Display for Module { | ||
3579 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3580 | std::fmt::Display::fmt(self.syntax(), f) | ||
3581 | } | ||
3582 | } | ||
3583 | impl std::fmt::Display for Static { | ||
3584 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3585 | std::fmt::Display::fmt(self.syntax(), f) | ||
3586 | } | ||
3587 | } | ||
3588 | impl std::fmt::Display for Struct { | ||
3589 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3590 | std::fmt::Display::fmt(self.syntax(), f) | ||
3591 | } | ||
3592 | } | ||
3593 | impl std::fmt::Display for Trait { | ||
3594 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3595 | std::fmt::Display::fmt(self.syntax(), f) | ||
3596 | } | ||
3597 | } | ||
3598 | impl std::fmt::Display for TypeAlias { | ||
3599 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3600 | std::fmt::Display::fmt(self.syntax(), f) | ||
3601 | } | ||
3602 | } | ||
3603 | impl std::fmt::Display for Union { | ||
3604 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3605 | std::fmt::Display::fmt(self.syntax(), f) | ||
3606 | } | ||
3607 | } | ||
3608 | impl std::fmt::Display for Use { | ||
3609 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3610 | std::fmt::Display::fmt(self.syntax(), f) | ||
3611 | } | ||
3612 | } | ||
3613 | impl std::fmt::Display for Visibility { | ||
3614 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3615 | std::fmt::Display::fmt(self.syntax(), f) | ||
3616 | } | ||
3617 | } | ||
3618 | impl std::fmt::Display for ItemList { | ||
3619 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3620 | std::fmt::Display::fmt(self.syntax(), f) | ||
3621 | } | ||
3622 | } | ||
3623 | impl std::fmt::Display for Rename { | ||
3624 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3625 | std::fmt::Display::fmt(self.syntax(), f) | ||
3626 | } | ||
3627 | } | ||
3628 | impl std::fmt::Display for UseTree { | ||
3629 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3630 | std::fmt::Display::fmt(self.syntax(), f) | ||
3631 | } | ||
3632 | } | ||
3633 | impl std::fmt::Display for UseTreeList { | ||
3634 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3635 | std::fmt::Display::fmt(self.syntax(), f) | ||
3636 | } | ||
3637 | } | ||
3638 | impl std::fmt::Display for Abi { | ||
3639 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3640 | std::fmt::Display::fmt(self.syntax(), f) | ||
3641 | } | ||
3642 | } | ||
3643 | impl std::fmt::Display for GenericParamList { | ||
3644 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3645 | std::fmt::Display::fmt(self.syntax(), f) | ||
3646 | } | ||
3647 | } | ||
3648 | impl std::fmt::Display for WhereClause { | ||
3649 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3650 | std::fmt::Display::fmt(self.syntax(), f) | ||
3651 | } | ||
3652 | } | ||
3653 | impl std::fmt::Display for BlockExpr { | ||
3654 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3655 | std::fmt::Display::fmt(self.syntax(), f) | ||
3656 | } | ||
3657 | } | ||
3658 | impl std::fmt::Display for SelfParam { | ||
3659 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3660 | std::fmt::Display::fmt(self.syntax(), f) | ||
3661 | } | ||
3662 | } | ||
3663 | impl std::fmt::Display for Param { | ||
3664 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3665 | std::fmt::Display::fmt(self.syntax(), f) | ||
3666 | } | ||
3667 | } | ||
3668 | impl std::fmt::Display for RecordFieldList { | ||
3669 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3670 | std::fmt::Display::fmt(self.syntax(), f) | ||
3671 | } | ||
3672 | } | ||
3673 | impl std::fmt::Display for TupleFieldList { | ||
3674 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3675 | std::fmt::Display::fmt(self.syntax(), f) | ||
3676 | } | ||
3677 | } | ||
3678 | impl std::fmt::Display for RecordField { | ||
3679 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3680 | std::fmt::Display::fmt(self.syntax(), f) | ||
3681 | } | ||
3682 | } | ||
3683 | impl std::fmt::Display for TupleField { | ||
3684 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3685 | std::fmt::Display::fmt(self.syntax(), f) | ||
3686 | } | ||
3687 | } | ||
3688 | impl std::fmt::Display for VariantList { | ||
3689 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3690 | std::fmt::Display::fmt(self.syntax(), f) | ||
3691 | } | ||
3692 | } | ||
3693 | impl std::fmt::Display for Variant { | ||
3694 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3695 | std::fmt::Display::fmt(self.syntax(), f) | ||
3696 | } | ||
3697 | } | ||
3698 | impl std::fmt::Display for AssocItemList { | ||
3699 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3700 | std::fmt::Display::fmt(self.syntax(), f) | ||
3701 | } | ||
3702 | } | ||
3703 | impl std::fmt::Display for ExternItemList { | ||
3704 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3705 | std::fmt::Display::fmt(self.syntax(), f) | ||
3706 | } | ||
3707 | } | ||
3708 | impl std::fmt::Display for ConstParam { | ||
3709 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3710 | std::fmt::Display::fmt(self.syntax(), f) | ||
3711 | } | ||
3712 | } | ||
3713 | impl std::fmt::Display for LifetimeParam { | ||
3714 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3715 | std::fmt::Display::fmt(self.syntax(), f) | ||
3716 | } | ||
3717 | } | ||
3718 | impl std::fmt::Display for TypeParam { | ||
3719 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3720 | std::fmt::Display::fmt(self.syntax(), f) | ||
3721 | } | ||
3722 | } | ||
3723 | impl std::fmt::Display for WherePred { | ||
3724 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3725 | std::fmt::Display::fmt(self.syntax(), f) | ||
3726 | } | ||
3727 | } | ||
3728 | impl std::fmt::Display for Literal { | ||
3729 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3730 | std::fmt::Display::fmt(self.syntax(), f) | ||
3731 | } | ||
3732 | } | ||
3733 | impl std::fmt::Display for ExprStmt { | ||
3734 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3735 | std::fmt::Display::fmt(self.syntax(), f) | ||
3736 | } | ||
3737 | } | ||
3738 | impl std::fmt::Display for LetStmt { | ||
3739 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3740 | std::fmt::Display::fmt(self.syntax(), f) | ||
3741 | } | ||
3742 | } | ||
3743 | impl std::fmt::Display for ArrayExpr { | ||
3744 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3745 | std::fmt::Display::fmt(self.syntax(), f) | ||
3746 | } | ||
3747 | } | ||
3748 | impl std::fmt::Display for AwaitExpr { | ||
3749 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3750 | std::fmt::Display::fmt(self.syntax(), f) | ||
3751 | } | ||
3752 | } | ||
3753 | impl std::fmt::Display for BinExpr { | ||
3754 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3755 | std::fmt::Display::fmt(self.syntax(), f) | ||
3756 | } | ||
3757 | } | ||
3758 | impl std::fmt::Display for BoxExpr { | ||
3759 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3760 | std::fmt::Display::fmt(self.syntax(), f) | ||
3761 | } | ||
3762 | } | ||
3763 | impl std::fmt::Display for BreakExpr { | ||
3764 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3765 | std::fmt::Display::fmt(self.syntax(), f) | ||
3766 | } | ||
3767 | } | ||
3768 | impl std::fmt::Display for CallExpr { | ||
3769 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3770 | std::fmt::Display::fmt(self.syntax(), f) | ||
3771 | } | ||
3772 | } | ||
3773 | impl std::fmt::Display for CastExpr { | ||
3774 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3775 | std::fmt::Display::fmt(self.syntax(), f) | ||
3776 | } | ||
3777 | } | ||
3778 | impl std::fmt::Display for ClosureExpr { | ||
3779 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3780 | std::fmt::Display::fmt(self.syntax(), f) | ||
3781 | } | ||
3782 | } | ||
3783 | impl std::fmt::Display for ContinueExpr { | ||
3784 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3785 | std::fmt::Display::fmt(self.syntax(), f) | ||
3786 | } | ||
3787 | } | ||
3788 | impl std::fmt::Display for EffectExpr { | ||
3789 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3790 | std::fmt::Display::fmt(self.syntax(), f) | ||
3791 | } | ||
3792 | } | ||
3793 | impl std::fmt::Display for FieldExpr { | ||
3794 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3795 | std::fmt::Display::fmt(self.syntax(), f) | ||
3796 | } | ||
3797 | } | ||
3798 | impl std::fmt::Display for ForExpr { | ||
3799 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3800 | std::fmt::Display::fmt(self.syntax(), f) | ||
3801 | } | ||
3802 | } | ||
3803 | impl std::fmt::Display for IfExpr { | ||
3804 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3805 | std::fmt::Display::fmt(self.syntax(), f) | ||
3806 | } | ||
3807 | } | ||
3808 | impl std::fmt::Display for IndexExpr { | ||
3809 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3810 | std::fmt::Display::fmt(self.syntax(), f) | ||
3811 | } | ||
3812 | } | ||
3813 | impl std::fmt::Display for LoopExpr { | ||
3814 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3815 | std::fmt::Display::fmt(self.syntax(), f) | ||
3816 | } | ||
3817 | } | ||
3818 | impl std::fmt::Display for MatchExpr { | ||
3819 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3820 | std::fmt::Display::fmt(self.syntax(), f) | ||
3821 | } | ||
3822 | } | ||
3823 | impl std::fmt::Display for MethodCallExpr { | ||
3824 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3825 | std::fmt::Display::fmt(self.syntax(), f) | ||
3826 | } | ||
3827 | } | ||
3828 | impl std::fmt::Display for ParenExpr { | ||
3829 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3830 | std::fmt::Display::fmt(self.syntax(), f) | ||
3831 | } | ||
3832 | } | ||
3833 | impl std::fmt::Display for PathExpr { | ||
3834 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3835 | std::fmt::Display::fmt(self.syntax(), f) | ||
3836 | } | ||
3837 | } | ||
3838 | impl std::fmt::Display for PrefixExpr { | ||
3839 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3840 | std::fmt::Display::fmt(self.syntax(), f) | ||
3841 | } | ||
3842 | } | ||
3843 | impl std::fmt::Display for RangeExpr { | ||
3844 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3845 | std::fmt::Display::fmt(self.syntax(), f) | ||
3846 | } | ||
3847 | } | ||
3848 | impl std::fmt::Display for RecordExpr { | ||
3849 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3850 | std::fmt::Display::fmt(self.syntax(), f) | ||
3851 | } | ||
3852 | } | ||
3853 | impl std::fmt::Display for RefExpr { | ||
3854 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3855 | std::fmt::Display::fmt(self.syntax(), f) | ||
3856 | } | ||
3857 | } | ||
3858 | impl std::fmt::Display for ReturnExpr { | ||
3859 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3860 | std::fmt::Display::fmt(self.syntax(), f) | ||
3861 | } | ||
3862 | } | ||
3863 | impl std::fmt::Display for TryExpr { | ||
3864 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3865 | std::fmt::Display::fmt(self.syntax(), f) | ||
3866 | } | ||
3867 | } | ||
3868 | impl std::fmt::Display for TupleExpr { | ||
3869 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3870 | std::fmt::Display::fmt(self.syntax(), f) | ||
3871 | } | ||
3872 | } | ||
3873 | impl std::fmt::Display for WhileExpr { | ||
3874 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3875 | std::fmt::Display::fmt(self.syntax(), f) | ||
3876 | } | ||
3877 | } | ||
3878 | impl std::fmt::Display for Label { | ||
3879 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3880 | std::fmt::Display::fmt(self.syntax(), f) | ||
3881 | } | ||
3882 | } | ||
3883 | impl std::fmt::Display for RecordExprFieldList { | ||
3884 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3885 | std::fmt::Display::fmt(self.syntax(), f) | ||
3886 | } | ||
3887 | } | ||
3888 | impl std::fmt::Display for RecordExprField { | ||
3889 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3890 | std::fmt::Display::fmt(self.syntax(), f) | ||
3891 | } | ||
3892 | } | ||
3893 | impl std::fmt::Display for ArgList { | ||
3894 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3895 | std::fmt::Display::fmt(self.syntax(), f) | ||
3896 | } | ||
3897 | } | ||
3898 | impl std::fmt::Display for Condition { | ||
3899 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3900 | std::fmt::Display::fmt(self.syntax(), f) | ||
3901 | } | ||
3902 | } | ||
3903 | impl std::fmt::Display for MatchArmList { | ||
3904 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3905 | std::fmt::Display::fmt(self.syntax(), f) | ||
3906 | } | ||
3907 | } | ||
3908 | impl std::fmt::Display for MatchArm { | ||
3909 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3910 | std::fmt::Display::fmt(self.syntax(), f) | ||
3911 | } | ||
3912 | } | ||
3913 | impl std::fmt::Display for MatchGuard { | ||
3914 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3915 | std::fmt::Display::fmt(self.syntax(), f) | ||
3916 | } | ||
3917 | } | ||
3918 | impl std::fmt::Display for ArrayType { | ||
3919 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3920 | std::fmt::Display::fmt(self.syntax(), f) | ||
3921 | } | ||
3922 | } | ||
3923 | impl std::fmt::Display for DynTraitType { | ||
3924 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3925 | std::fmt::Display::fmt(self.syntax(), f) | ||
3926 | } | ||
3927 | } | ||
3928 | impl std::fmt::Display for FnPtrType { | ||
3929 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3930 | std::fmt::Display::fmt(self.syntax(), f) | ||
3931 | } | ||
3932 | } | ||
3933 | impl std::fmt::Display for ForType { | ||
3934 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3935 | std::fmt::Display::fmt(self.syntax(), f) | ||
3936 | } | ||
3937 | } | ||
3938 | impl std::fmt::Display for ImplTraitType { | ||
3939 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3940 | std::fmt::Display::fmt(self.syntax(), f) | ||
3941 | } | ||
3942 | } | ||
3943 | impl std::fmt::Display for InferType { | ||
3944 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3945 | std::fmt::Display::fmt(self.syntax(), f) | ||
3946 | } | ||
3947 | } | ||
3948 | impl std::fmt::Display for NeverType { | ||
3949 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3950 | std::fmt::Display::fmt(self.syntax(), f) | ||
3951 | } | ||
3952 | } | ||
3953 | impl std::fmt::Display for ParenType { | ||
3954 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3955 | std::fmt::Display::fmt(self.syntax(), f) | ||
3956 | } | ||
3957 | } | ||
3958 | impl std::fmt::Display for PtrType { | ||
3959 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3960 | std::fmt::Display::fmt(self.syntax(), f) | ||
3961 | } | ||
3962 | } | ||
3963 | impl std::fmt::Display for RefType { | ||
3964 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3965 | std::fmt::Display::fmt(self.syntax(), f) | ||
3966 | } | ||
3967 | } | ||
3968 | impl std::fmt::Display for SliceType { | ||
3969 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3970 | std::fmt::Display::fmt(self.syntax(), f) | ||
3971 | } | ||
3972 | } | ||
3973 | impl std::fmt::Display for TupleType { | ||
3974 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3975 | std::fmt::Display::fmt(self.syntax(), f) | ||
3976 | } | ||
3977 | } | ||
3978 | impl std::fmt::Display for TypeBound { | ||
3979 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3980 | std::fmt::Display::fmt(self.syntax(), f) | ||
3981 | } | ||
3982 | } | ||
3983 | impl std::fmt::Display for IdentPat { | ||
3984 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3985 | std::fmt::Display::fmt(self.syntax(), f) | ||
3986 | } | ||
3987 | } | ||
3988 | impl std::fmt::Display for BoxPat { | ||
3989 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3990 | std::fmt::Display::fmt(self.syntax(), f) | ||
3991 | } | ||
3992 | } | ||
3993 | impl std::fmt::Display for RestPat { | ||
3994 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3995 | std::fmt::Display::fmt(self.syntax(), f) | ||
3996 | } | ||
3997 | } | ||
3998 | impl std::fmt::Display for LiteralPat { | ||
3999 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
4000 | std::fmt::Display::fmt(self.syntax(), f) | ||
4001 | } | ||
4002 | } | ||
4003 | impl std::fmt::Display for MacroPat { | ||
4004 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
4005 | std::fmt::Display::fmt(self.syntax(), f) | ||
4006 | } | ||
4007 | } | ||
4008 | impl std::fmt::Display for OrPat { | ||
4009 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
4010 | std::fmt::Display::fmt(self.syntax(), f) | ||
4011 | } | ||
4012 | } | ||
4013 | impl std::fmt::Display for ParenPat { | ||
4014 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
4015 | std::fmt::Display::fmt(self.syntax(), f) | ||
4016 | } | ||
4017 | } | ||
4018 | impl std::fmt::Display for PathPat { | ||
4019 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
4020 | std::fmt::Display::fmt(self.syntax(), f) | ||
4021 | } | ||
4022 | } | ||
4023 | impl std::fmt::Display for WildcardPat { | ||
4024 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
4025 | std::fmt::Display::fmt(self.syntax(), f) | ||
4026 | } | ||
4027 | } | ||
4028 | impl std::fmt::Display for RangePat { | ||
4029 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
4030 | std::fmt::Display::fmt(self.syntax(), f) | ||
4031 | } | ||
4032 | } | ||
4033 | impl std::fmt::Display for RecordPat { | ||
4034 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
4035 | std::fmt::Display::fmt(self.syntax(), f) | ||
4036 | } | ||
4037 | } | ||
4038 | impl std::fmt::Display for RefPat { | ||
4039 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
4040 | std::fmt::Display::fmt(self.syntax(), f) | ||
4041 | } | ||
4042 | } | ||
4043 | impl std::fmt::Display for SlicePat { | ||
4044 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
4045 | std::fmt::Display::fmt(self.syntax(), f) | ||
4046 | } | ||
4047 | } | ||
4048 | impl std::fmt::Display for TuplePat { | ||
4049 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
4050 | std::fmt::Display::fmt(self.syntax(), f) | ||
4051 | } | ||
4052 | } | ||
4053 | impl std::fmt::Display for TupleStructPat { | ||
4054 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
4055 | std::fmt::Display::fmt(self.syntax(), f) | ||
4056 | } | ||
4057 | } | ||
4058 | impl std::fmt::Display for RecordPatFieldList { | ||
4059 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
4060 | std::fmt::Display::fmt(self.syntax(), f) | ||
4061 | } | ||
4062 | } | ||
4063 | impl std::fmt::Display for RecordPatField { | ||
4064 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
4065 | std::fmt::Display::fmt(self.syntax(), f) | ||
4066 | } | ||
4067 | } | ||
diff --git a/crates/syntax/src/ast/generated/tokens.rs b/crates/syntax/src/ast/generated/tokens.rs new file mode 100644 index 000000000..abadd0b61 --- /dev/null +++ b/crates/syntax/src/ast/generated/tokens.rs | |||
@@ -0,0 +1,91 @@ | |||
1 | //! Generated file, do not edit by hand, see `xtask/src/codegen` | ||
2 | |||
3 | use crate::{ | ||
4 | ast::AstToken, | ||
5 | SyntaxKind::{self, *}, | ||
6 | SyntaxToken, | ||
7 | }; | ||
8 | |||
9 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
10 | pub struct Whitespace { | ||
11 | pub(crate) syntax: SyntaxToken, | ||
12 | } | ||
13 | impl std::fmt::Display for Whitespace { | ||
14 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
15 | std::fmt::Display::fmt(&self.syntax, f) | ||
16 | } | ||
17 | } | ||
18 | impl AstToken for Whitespace { | ||
19 | fn can_cast(kind: SyntaxKind) -> bool { kind == WHITESPACE } | ||
20 | fn cast(syntax: SyntaxToken) -> Option<Self> { | ||
21 | if Self::can_cast(syntax.kind()) { | ||
22 | Some(Self { syntax }) | ||
23 | } else { | ||
24 | None | ||
25 | } | ||
26 | } | ||
27 | fn syntax(&self) -> &SyntaxToken { &self.syntax } | ||
28 | } | ||
29 | |||
30 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
31 | pub struct Comment { | ||
32 | pub(crate) syntax: SyntaxToken, | ||
33 | } | ||
34 | impl std::fmt::Display for Comment { | ||
35 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
36 | std::fmt::Display::fmt(&self.syntax, f) | ||
37 | } | ||
38 | } | ||
39 | impl AstToken for Comment { | ||
40 | fn can_cast(kind: SyntaxKind) -> bool { kind == COMMENT } | ||
41 | fn cast(syntax: SyntaxToken) -> Option<Self> { | ||
42 | if Self::can_cast(syntax.kind()) { | ||
43 | Some(Self { syntax }) | ||
44 | } else { | ||
45 | None | ||
46 | } | ||
47 | } | ||
48 | fn syntax(&self) -> &SyntaxToken { &self.syntax } | ||
49 | } | ||
50 | |||
51 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
52 | pub struct String { | ||
53 | pub(crate) syntax: SyntaxToken, | ||
54 | } | ||
55 | impl std::fmt::Display for String { | ||
56 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
57 | std::fmt::Display::fmt(&self.syntax, f) | ||
58 | } | ||
59 | } | ||
60 | impl AstToken for String { | ||
61 | fn can_cast(kind: SyntaxKind) -> bool { kind == STRING } | ||
62 | fn cast(syntax: SyntaxToken) -> Option<Self> { | ||
63 | if Self::can_cast(syntax.kind()) { | ||
64 | Some(Self { syntax }) | ||
65 | } else { | ||
66 | None | ||
67 | } | ||
68 | } | ||
69 | fn syntax(&self) -> &SyntaxToken { &self.syntax } | ||
70 | } | ||
71 | |||
72 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
73 | pub struct RawString { | ||
74 | pub(crate) syntax: SyntaxToken, | ||
75 | } | ||
76 | impl std::fmt::Display for RawString { | ||
77 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
78 | std::fmt::Display::fmt(&self.syntax, f) | ||
79 | } | ||
80 | } | ||
81 | impl AstToken for RawString { | ||
82 | fn can_cast(kind: SyntaxKind) -> bool { kind == RAW_STRING } | ||
83 | fn cast(syntax: SyntaxToken) -> Option<Self> { | ||
84 | if Self::can_cast(syntax.kind()) { | ||
85 | Some(Self { syntax }) | ||
86 | } else { | ||
87 | None | ||
88 | } | ||
89 | } | ||
90 | fn syntax(&self) -> &SyntaxToken { &self.syntax } | ||
91 | } | ||
diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs new file mode 100644 index 000000000..d20c085aa --- /dev/null +++ b/crates/syntax/src/ast/make.rs | |||
@@ -0,0 +1,402 @@ | |||
1 | //! This module contains free-standing functions for creating AST fragments out | ||
2 | //! of smaller pieces. | ||
3 | //! | ||
4 | //! Note that all functions here intended to be stupid constructors, which just | ||
5 | //! assemble a finish node from immediate children. If you want to do something | ||
6 | //! smarter than that, it probably doesn't belong in this module. | ||
7 | use itertools::Itertools; | ||
8 | use stdx::format_to; | ||
9 | |||
10 | use crate::{ast, AstNode, SourceFile, SyntaxKind, SyntaxNode, SyntaxToken}; | ||
11 | |||
12 | pub fn name(text: &str) -> ast::Name { | ||
13 | ast_from_text(&format!("mod {};", text)) | ||
14 | } | ||
15 | |||
16 | pub fn name_ref(text: &str) -> ast::NameRef { | ||
17 | ast_from_text(&format!("fn f() {{ {}; }}", text)) | ||
18 | } | ||
19 | |||
20 | pub fn ty(text: &str) -> ast::Type { | ||
21 | ast_from_text(&format!("impl {} for D {{}};", text)) | ||
22 | } | ||
23 | |||
24 | pub fn path_segment(name_ref: ast::NameRef) -> ast::PathSegment { | ||
25 | ast_from_text(&format!("use {};", name_ref)) | ||
26 | } | ||
27 | pub fn path_segment_self() -> ast::PathSegment { | ||
28 | ast_from_text("use self;") | ||
29 | } | ||
30 | pub fn path_unqualified(segment: ast::PathSegment) -> ast::Path { | ||
31 | path_from_text(&format!("use {}", segment)) | ||
32 | } | ||
33 | pub fn path_qualified(qual: ast::Path, segment: ast::PathSegment) -> ast::Path { | ||
34 | path_from_text(&format!("{}::{}", qual, segment)) | ||
35 | } | ||
36 | pub fn path_from_text(text: &str) -> ast::Path { | ||
37 | ast_from_text(text) | ||
38 | } | ||
39 | |||
40 | pub fn use_tree( | ||
41 | path: ast::Path, | ||
42 | use_tree_list: Option<ast::UseTreeList>, | ||
43 | alias: Option<ast::Rename>, | ||
44 | add_star: bool, | ||
45 | ) -> ast::UseTree { | ||
46 | let mut buf = "use ".to_string(); | ||
47 | buf += &path.syntax().to_string(); | ||
48 | if let Some(use_tree_list) = use_tree_list { | ||
49 | format_to!(buf, "::{}", use_tree_list); | ||
50 | } | ||
51 | if add_star { | ||
52 | buf += "::*"; | ||
53 | } | ||
54 | |||
55 | if let Some(alias) = alias { | ||
56 | format_to!(buf, " {}", alias); | ||
57 | } | ||
58 | ast_from_text(&buf) | ||
59 | } | ||
60 | |||
61 | pub fn use_tree_list(use_trees: impl IntoIterator<Item = ast::UseTree>) -> ast::UseTreeList { | ||
62 | let use_trees = use_trees.into_iter().map(|it| it.syntax().clone()).join(", "); | ||
63 | ast_from_text(&format!("use {{{}}};", use_trees)) | ||
64 | } | ||
65 | |||
66 | pub fn use_(use_tree: ast::UseTree) -> ast::Use { | ||
67 | ast_from_text(&format!("use {};", use_tree)) | ||
68 | } | ||
69 | |||
70 | pub fn record_expr_field(name: ast::NameRef, expr: Option<ast::Expr>) -> ast::RecordExprField { | ||
71 | return match expr { | ||
72 | Some(expr) => from_text(&format!("{}: {}", name, expr)), | ||
73 | None => from_text(&name.to_string()), | ||
74 | }; | ||
75 | |||
76 | fn from_text(text: &str) -> ast::RecordExprField { | ||
77 | ast_from_text(&format!("fn f() {{ S {{ {}, }} }}", text)) | ||
78 | } | ||
79 | } | ||
80 | |||
81 | pub fn record_field(name: ast::NameRef, ty: ast::Type) -> ast::RecordField { | ||
82 | ast_from_text(&format!("struct S {{ {}: {}, }}", name, ty)) | ||
83 | } | ||
84 | |||
85 | pub fn block_expr( | ||
86 | stmts: impl IntoIterator<Item = ast::Stmt>, | ||
87 | tail_expr: Option<ast::Expr>, | ||
88 | ) -> ast::BlockExpr { | ||
89 | let mut buf = "{\n".to_string(); | ||
90 | for stmt in stmts.into_iter() { | ||
91 | format_to!(buf, " {}\n", stmt); | ||
92 | } | ||
93 | if let Some(tail_expr) = tail_expr { | ||
94 | format_to!(buf, " {}\n", tail_expr) | ||
95 | } | ||
96 | buf += "}"; | ||
97 | ast_from_text(&format!("fn f() {}", buf)) | ||
98 | } | ||
99 | |||
100 | pub fn expr_unit() -> ast::Expr { | ||
101 | expr_from_text("()") | ||
102 | } | ||
103 | pub fn expr_empty_block() -> ast::Expr { | ||
104 | expr_from_text("{}") | ||
105 | } | ||
106 | pub fn expr_unimplemented() -> ast::Expr { | ||
107 | expr_from_text("unimplemented!()") | ||
108 | } | ||
109 | pub fn expr_unreachable() -> ast::Expr { | ||
110 | expr_from_text("unreachable!()") | ||
111 | } | ||
112 | pub fn expr_todo() -> ast::Expr { | ||
113 | expr_from_text("todo!()") | ||
114 | } | ||
115 | pub fn expr_path(path: ast::Path) -> ast::Expr { | ||
116 | expr_from_text(&path.to_string()) | ||
117 | } | ||
118 | pub fn expr_continue() -> ast::Expr { | ||
119 | expr_from_text("continue") | ||
120 | } | ||
121 | pub fn expr_break() -> ast::Expr { | ||
122 | expr_from_text("break") | ||
123 | } | ||
124 | pub fn expr_return() -> ast::Expr { | ||
125 | expr_from_text("return") | ||
126 | } | ||
127 | pub fn expr_match(expr: ast::Expr, match_arm_list: ast::MatchArmList) -> ast::Expr { | ||
128 | expr_from_text(&format!("match {} {}", expr, match_arm_list)) | ||
129 | } | ||
130 | pub fn expr_if(condition: ast::Condition, then_branch: ast::BlockExpr) -> ast::Expr { | ||
131 | expr_from_text(&format!("if {} {}", condition, then_branch)) | ||
132 | } | ||
133 | pub fn expr_prefix(op: SyntaxKind, expr: ast::Expr) -> ast::Expr { | ||
134 | let token = token(op); | ||
135 | expr_from_text(&format!("{}{}", token, expr)) | ||
136 | } | ||
137 | pub fn expr_call(f: ast::Expr, arg_list: ast::ArgList) -> ast::Expr { | ||
138 | expr_from_text(&format!("{}{}", f, arg_list)) | ||
139 | } | ||
140 | fn expr_from_text(text: &str) -> ast::Expr { | ||
141 | ast_from_text(&format!("const C: () = {};", text)) | ||
142 | } | ||
143 | |||
144 | pub fn try_expr_from_text(text: &str) -> Option<ast::Expr> { | ||
145 | try_ast_from_text(&format!("const C: () = {};", text)) | ||
146 | } | ||
147 | |||
148 | pub fn condition(expr: ast::Expr, pattern: Option<ast::Pat>) -> ast::Condition { | ||
149 | match pattern { | ||
150 | None => ast_from_text(&format!("const _: () = while {} {{}};", expr)), | ||
151 | Some(pattern) => { | ||
152 | ast_from_text(&format!("const _: () = while let {} = {} {{}};", pattern, expr)) | ||
153 | } | ||
154 | } | ||
155 | } | ||
156 | |||
157 | pub fn arg_list(args: impl IntoIterator<Item = ast::Expr>) -> ast::ArgList { | ||
158 | ast_from_text(&format!("fn main() {{ ()({}) }}", args.into_iter().format(", "))) | ||
159 | } | ||
160 | |||
161 | pub fn ident_pat(name: ast::Name) -> ast::IdentPat { | ||
162 | return from_text(name.text()); | ||
163 | |||
164 | fn from_text(text: &str) -> ast::IdentPat { | ||
165 | ast_from_text(&format!("fn f({}: ())", text)) | ||
166 | } | ||
167 | } | ||
168 | |||
169 | pub fn wildcard_pat() -> ast::WildcardPat { | ||
170 | return from_text("_"); | ||
171 | |||
172 | fn from_text(text: &str) -> ast::WildcardPat { | ||
173 | ast_from_text(&format!("fn f({}: ())", text)) | ||
174 | } | ||
175 | } | ||
176 | |||
177 | /// Creates a tuple of patterns from an interator of patterns. | ||
178 | /// | ||
179 | /// Invariant: `pats` must be length > 1 | ||
180 | /// | ||
181 | /// FIXME handle `pats` length == 1 | ||
182 | pub fn tuple_pat(pats: impl IntoIterator<Item = ast::Pat>) -> ast::TuplePat { | ||
183 | let pats_str = pats.into_iter().map(|p| p.to_string()).join(", "); | ||
184 | return from_text(&format!("({})", pats_str)); | ||
185 | |||
186 | fn from_text(text: &str) -> ast::TuplePat { | ||
187 | ast_from_text(&format!("fn f({}: ())", text)) | ||
188 | } | ||
189 | } | ||
190 | |||
191 | pub fn tuple_struct_pat( | ||
192 | path: ast::Path, | ||
193 | pats: impl IntoIterator<Item = ast::Pat>, | ||
194 | ) -> ast::TupleStructPat { | ||
195 | let pats_str = pats.into_iter().join(", "); | ||
196 | return from_text(&format!("{}({})", path, pats_str)); | ||
197 | |||
198 | fn from_text(text: &str) -> ast::TupleStructPat { | ||
199 | ast_from_text(&format!("fn f({}: ())", text)) | ||
200 | } | ||
201 | } | ||
202 | |||
203 | pub fn record_pat(path: ast::Path, pats: impl IntoIterator<Item = ast::Pat>) -> ast::RecordPat { | ||
204 | let pats_str = pats.into_iter().join(", "); | ||
205 | return from_text(&format!("{} {{ {} }}", path, pats_str)); | ||
206 | |||
207 | fn from_text(text: &str) -> ast::RecordPat { | ||
208 | ast_from_text(&format!("fn f({}: ())", text)) | ||
209 | } | ||
210 | } | ||
211 | |||
212 | /// Returns a `BindPat` if the path has just one segment, a `PathPat` otherwise. | ||
213 | pub fn path_pat(path: ast::Path) -> ast::Pat { | ||
214 | return from_text(&path.to_string()); | ||
215 | fn from_text(text: &str) -> ast::Pat { | ||
216 | ast_from_text(&format!("fn f({}: ())", text)) | ||
217 | } | ||
218 | } | ||
219 | |||
220 | pub fn match_arm(pats: impl IntoIterator<Item = ast::Pat>, expr: ast::Expr) -> ast::MatchArm { | ||
221 | let pats_str = pats.into_iter().join(" | "); | ||
222 | return from_text(&format!("{} => {}", pats_str, expr)); | ||
223 | |||
224 | fn from_text(text: &str) -> ast::MatchArm { | ||
225 | ast_from_text(&format!("fn f() {{ match () {{{}}} }}", text)) | ||
226 | } | ||
227 | } | ||
228 | |||
229 | pub fn match_arm_list(arms: impl IntoIterator<Item = ast::MatchArm>) -> ast::MatchArmList { | ||
230 | let arms_str = arms | ||
231 | .into_iter() | ||
232 | .map(|arm| { | ||
233 | let needs_comma = arm.expr().map_or(true, |it| !it.is_block_like()); | ||
234 | let comma = if needs_comma { "," } else { "" }; | ||
235 | format!(" {}{}\n", arm.syntax(), comma) | ||
236 | }) | ||
237 | .collect::<String>(); | ||
238 | return from_text(&arms_str); | ||
239 | |||
240 | fn from_text(text: &str) -> ast::MatchArmList { | ||
241 | ast_from_text(&format!("fn f() {{ match () {{\n{}}} }}", text)) | ||
242 | } | ||
243 | } | ||
244 | |||
245 | pub fn where_pred( | ||
246 | path: ast::Path, | ||
247 | bounds: impl IntoIterator<Item = ast::TypeBound>, | ||
248 | ) -> ast::WherePred { | ||
249 | let bounds = bounds.into_iter().join(" + "); | ||
250 | return from_text(&format!("{}: {}", path, bounds)); | ||
251 | |||
252 | fn from_text(text: &str) -> ast::WherePred { | ||
253 | ast_from_text(&format!("fn f() where {} {{ }}", text)) | ||
254 | } | ||
255 | } | ||
256 | |||
257 | pub fn where_clause(preds: impl IntoIterator<Item = ast::WherePred>) -> ast::WhereClause { | ||
258 | let preds = preds.into_iter().join(", "); | ||
259 | return from_text(preds.as_str()); | ||
260 | |||
261 | fn from_text(text: &str) -> ast::WhereClause { | ||
262 | ast_from_text(&format!("fn f() where {} {{ }}", text)) | ||
263 | } | ||
264 | } | ||
265 | |||
266 | pub fn let_stmt(pattern: ast::Pat, initializer: Option<ast::Expr>) -> ast::LetStmt { | ||
267 | let text = match initializer { | ||
268 | Some(it) => format!("let {} = {};", pattern, it), | ||
269 | None => format!("let {};", pattern), | ||
270 | }; | ||
271 | ast_from_text(&format!("fn f() {{ {} }}", text)) | ||
272 | } | ||
273 | pub fn expr_stmt(expr: ast::Expr) -> ast::ExprStmt { | ||
274 | let semi = if expr.is_block_like() { "" } else { ";" }; | ||
275 | ast_from_text(&format!("fn f() {{ {}{} (); }}", expr, semi)) | ||
276 | } | ||
277 | |||
278 | pub fn token(kind: SyntaxKind) -> SyntaxToken { | ||
279 | tokens::SOURCE_FILE | ||
280 | .tree() | ||
281 | .syntax() | ||
282 | .descendants_with_tokens() | ||
283 | .filter_map(|it| it.into_token()) | ||
284 | .find(|it| it.kind() == kind) | ||
285 | .unwrap_or_else(|| panic!("unhandled token: {:?}", kind)) | ||
286 | } | ||
287 | |||
288 | pub fn param(name: String, ty: String) -> ast::Param { | ||
289 | ast_from_text(&format!("fn f({}: {}) {{ }}", name, ty)) | ||
290 | } | ||
291 | |||
292 | pub fn param_list(pats: impl IntoIterator<Item = ast::Param>) -> ast::ParamList { | ||
293 | let args = pats.into_iter().join(", "); | ||
294 | ast_from_text(&format!("fn f({}) {{ }}", args)) | ||
295 | } | ||
296 | |||
297 | pub fn visibility_pub_crate() -> ast::Visibility { | ||
298 | ast_from_text("pub(crate) struct S") | ||
299 | } | ||
300 | |||
301 | pub fn fn_( | ||
302 | visibility: Option<ast::Visibility>, | ||
303 | fn_name: ast::Name, | ||
304 | type_params: Option<ast::GenericParamList>, | ||
305 | params: ast::ParamList, | ||
306 | body: ast::BlockExpr, | ||
307 | ) -> ast::Fn { | ||
308 | let type_params = | ||
309 | if let Some(type_params) = type_params { format!("<{}>", type_params) } else { "".into() }; | ||
310 | let visibility = match visibility { | ||
311 | None => String::new(), | ||
312 | Some(it) => format!("{} ", it), | ||
313 | }; | ||
314 | ast_from_text(&format!("{}fn {}{}{} {}", visibility, fn_name, type_params, params, body)) | ||
315 | } | ||
316 | |||
317 | fn ast_from_text<N: AstNode>(text: &str) -> N { | ||
318 | let parse = SourceFile::parse(text); | ||
319 | let node = match parse.tree().syntax().descendants().find_map(N::cast) { | ||
320 | Some(it) => it, | ||
321 | None => { | ||
322 | panic!("Failed to make ast node `{}` from text {}", std::any::type_name::<N>(), text) | ||
323 | } | ||
324 | }; | ||
325 | let node = node.syntax().clone(); | ||
326 | let node = unroot(node); | ||
327 | let node = N::cast(node).unwrap(); | ||
328 | assert_eq!(node.syntax().text_range().start(), 0.into()); | ||
329 | node | ||
330 | } | ||
331 | |||
332 | fn try_ast_from_text<N: AstNode>(text: &str) -> Option<N> { | ||
333 | let parse = SourceFile::parse(text); | ||
334 | let node = parse.tree().syntax().descendants().find_map(N::cast)?; | ||
335 | let node = node.syntax().clone(); | ||
336 | let node = unroot(node); | ||
337 | let node = N::cast(node).unwrap(); | ||
338 | assert_eq!(node.syntax().text_range().start(), 0.into()); | ||
339 | Some(node) | ||
340 | } | ||
341 | |||
342 | fn unroot(n: SyntaxNode) -> SyntaxNode { | ||
343 | SyntaxNode::new_root(n.green().clone()) | ||
344 | } | ||
345 | |||
346 | pub mod tokens { | ||
347 | use once_cell::sync::Lazy; | ||
348 | |||
349 | use crate::{ast, AstNode, Parse, SourceFile, SyntaxKind::*, SyntaxToken}; | ||
350 | |||
351 | pub(super) static SOURCE_FILE: Lazy<Parse<SourceFile>> = | ||
352 | Lazy::new(|| SourceFile::parse("const C: <()>::Item = (1 != 1, 2 == 2, !true)\n;")); | ||
353 | |||
354 | pub fn single_space() -> SyntaxToken { | ||
355 | SOURCE_FILE | ||
356 | .tree() | ||
357 | .syntax() | ||
358 | .descendants_with_tokens() | ||
359 | .filter_map(|it| it.into_token()) | ||
360 | .find(|it| it.kind() == WHITESPACE && it.text().as_str() == " ") | ||
361 | .unwrap() | ||
362 | } | ||
363 | |||
364 | pub fn whitespace(text: &str) -> SyntaxToken { | ||
365 | assert!(text.trim().is_empty()); | ||
366 | let sf = SourceFile::parse(text).ok().unwrap(); | ||
367 | sf.syntax().first_child_or_token().unwrap().into_token().unwrap() | ||
368 | } | ||
369 | |||
370 | pub fn doc_comment(text: &str) -> SyntaxToken { | ||
371 | assert!(!text.trim().is_empty()); | ||
372 | let sf = SourceFile::parse(text).ok().unwrap(); | ||
373 | sf.syntax().first_child_or_token().unwrap().into_token().unwrap() | ||
374 | } | ||
375 | |||
376 | pub fn literal(text: &str) -> SyntaxToken { | ||
377 | assert_eq!(text.trim(), text); | ||
378 | let lit: ast::Literal = super::ast_from_text(&format!("fn f() {{ let _ = {}; }}", text)); | ||
379 | lit.syntax().first_child_or_token().unwrap().into_token().unwrap() | ||
380 | } | ||
381 | |||
382 | pub fn single_newline() -> SyntaxToken { | ||
383 | SOURCE_FILE | ||
384 | .tree() | ||
385 | .syntax() | ||
386 | .descendants_with_tokens() | ||
387 | .filter_map(|it| it.into_token()) | ||
388 | .find(|it| it.kind() == WHITESPACE && it.text().as_str() == "\n") | ||
389 | .unwrap() | ||
390 | } | ||
391 | |||
392 | pub struct WsBuilder(SourceFile); | ||
393 | |||
394 | impl WsBuilder { | ||
395 | pub fn new(text: &str) -> WsBuilder { | ||
396 | WsBuilder(SourceFile::parse(text).ok().unwrap()) | ||
397 | } | ||
398 | pub fn ws(&self) -> SyntaxToken { | ||
399 | self.0.syntax().first_child_or_token().unwrap().into_token().unwrap() | ||
400 | } | ||
401 | } | ||
402 | } | ||
diff --git a/crates/syntax/src/ast/node_ext.rs b/crates/syntax/src/ast/node_ext.rs new file mode 100644 index 000000000..50c1c157d --- /dev/null +++ b/crates/syntax/src/ast/node_ext.rs | |||
@@ -0,0 +1,485 @@ | |||
1 | //! Various extension methods to ast Nodes, which are hard to code-generate. | ||
2 | //! Extensions for various expressions live in a sibling `expr_extensions` module. | ||
3 | |||
4 | use std::fmt; | ||
5 | |||
6 | use itertools::Itertools; | ||
7 | use parser::SyntaxKind; | ||
8 | |||
9 | use crate::{ | ||
10 | ast::{self, support, AstNode, NameOwner, SyntaxNode}, | ||
11 | SmolStr, SyntaxElement, SyntaxToken, T, | ||
12 | }; | ||
13 | |||
14 | impl ast::Name { | ||
15 | pub fn text(&self) -> &SmolStr { | ||
16 | text_of_first_token(self.syntax()) | ||
17 | } | ||
18 | } | ||
19 | |||
20 | impl ast::NameRef { | ||
21 | pub fn text(&self) -> &SmolStr { | ||
22 | text_of_first_token(self.syntax()) | ||
23 | } | ||
24 | |||
25 | pub fn as_tuple_field(&self) -> Option<usize> { | ||
26 | self.text().parse().ok() | ||
27 | } | ||
28 | } | ||
29 | |||
30 | fn text_of_first_token(node: &SyntaxNode) -> &SmolStr { | ||
31 | node.green().children().next().and_then(|it| it.into_token()).unwrap().text() | ||
32 | } | ||
33 | |||
34 | #[derive(Debug, Clone, PartialEq, Eq)] | ||
35 | pub enum AttrKind { | ||
36 | Inner, | ||
37 | Outer, | ||
38 | } | ||
39 | |||
40 | impl ast::Attr { | ||
41 | pub fn as_simple_atom(&self) -> Option<SmolStr> { | ||
42 | if self.eq_token().is_some() || self.token_tree().is_some() { | ||
43 | return None; | ||
44 | } | ||
45 | self.simple_name() | ||
46 | } | ||
47 | |||
48 | pub fn as_simple_call(&self) -> Option<(SmolStr, ast::TokenTree)> { | ||
49 | let tt = self.token_tree()?; | ||
50 | Some((self.simple_name()?, tt)) | ||
51 | } | ||
52 | |||
53 | pub fn as_simple_key_value(&self) -> Option<(SmolStr, SmolStr)> { | ||
54 | let lit = self.literal()?; | ||
55 | let key = self.simple_name()?; | ||
56 | // FIXME: escape? raw string? | ||
57 | let value = lit.syntax().first_token()?.text().trim_matches('"').into(); | ||
58 | Some((key, value)) | ||
59 | } | ||
60 | |||
61 | pub fn simple_name(&self) -> Option<SmolStr> { | ||
62 | let path = self.path()?; | ||
63 | match (path.segment(), path.qualifier()) { | ||
64 | (Some(segment), None) => Some(segment.syntax().first_token()?.text().clone()), | ||
65 | _ => None, | ||
66 | } | ||
67 | } | ||
68 | |||
69 | pub fn kind(&self) -> AttrKind { | ||
70 | let first_token = self.syntax().first_token(); | ||
71 | let first_token_kind = first_token.as_ref().map(SyntaxToken::kind); | ||
72 | let second_token_kind = | ||
73 | first_token.and_then(|token| token.next_token()).as_ref().map(SyntaxToken::kind); | ||
74 | |||
75 | match (first_token_kind, second_token_kind) { | ||
76 | (Some(SyntaxKind::POUND), Some(T![!])) => AttrKind::Inner, | ||
77 | _ => AttrKind::Outer, | ||
78 | } | ||
79 | } | ||
80 | } | ||
81 | |||
82 | #[derive(Debug, Clone, PartialEq, Eq)] | ||
83 | pub enum PathSegmentKind { | ||
84 | Name(ast::NameRef), | ||
85 | Type { type_ref: Option<ast::Type>, trait_ref: Option<ast::PathType> }, | ||
86 | SelfKw, | ||
87 | SuperKw, | ||
88 | CrateKw, | ||
89 | } | ||
90 | |||
91 | impl ast::PathSegment { | ||
92 | pub fn parent_path(&self) -> ast::Path { | ||
93 | self.syntax() | ||
94 | .parent() | ||
95 | .and_then(ast::Path::cast) | ||
96 | .expect("segments are always nested in paths") | ||
97 | } | ||
98 | |||
99 | pub fn kind(&self) -> Option<PathSegmentKind> { | ||
100 | let res = if let Some(name_ref) = self.name_ref() { | ||
101 | PathSegmentKind::Name(name_ref) | ||
102 | } else { | ||
103 | match self.syntax().first_child_or_token()?.kind() { | ||
104 | T![self] => PathSegmentKind::SelfKw, | ||
105 | T![super] => PathSegmentKind::SuperKw, | ||
106 | T![crate] => PathSegmentKind::CrateKw, | ||
107 | T![<] => { | ||
108 | // <T> or <T as Trait> | ||
109 | // T is any TypeRef, Trait has to be a PathType | ||
110 | let mut type_refs = | ||
111 | self.syntax().children().filter(|node| ast::Type::can_cast(node.kind())); | ||
112 | let type_ref = type_refs.next().and_then(ast::Type::cast); | ||
113 | let trait_ref = type_refs.next().and_then(ast::PathType::cast); | ||
114 | PathSegmentKind::Type { type_ref, trait_ref } | ||
115 | } | ||
116 | _ => return None, | ||
117 | } | ||
118 | }; | ||
119 | Some(res) | ||
120 | } | ||
121 | } | ||
122 | |||
123 | impl ast::Path { | ||
124 | pub fn parent_path(&self) -> Option<ast::Path> { | ||
125 | self.syntax().parent().and_then(ast::Path::cast) | ||
126 | } | ||
127 | } | ||
128 | |||
129 | impl ast::UseTreeList { | ||
130 | pub fn parent_use_tree(&self) -> ast::UseTree { | ||
131 | self.syntax() | ||
132 | .parent() | ||
133 | .and_then(ast::UseTree::cast) | ||
134 | .expect("UseTreeLists are always nested in UseTrees") | ||
135 | } | ||
136 | } | ||
137 | |||
138 | impl ast::Impl { | ||
139 | pub fn self_ty(&self) -> Option<ast::Type> { | ||
140 | match self.target() { | ||
141 | (Some(t), None) | (_, Some(t)) => Some(t), | ||
142 | _ => None, | ||
143 | } | ||
144 | } | ||
145 | |||
146 | pub fn trait_(&self) -> Option<ast::Type> { | ||
147 | match self.target() { | ||
148 | (Some(t), Some(_)) => Some(t), | ||
149 | _ => None, | ||
150 | } | ||
151 | } | ||
152 | |||
153 | fn target(&self) -> (Option<ast::Type>, Option<ast::Type>) { | ||
154 | let mut types = support::children(self.syntax()); | ||
155 | let first = types.next(); | ||
156 | let second = types.next(); | ||
157 | (first, second) | ||
158 | } | ||
159 | } | ||
160 | |||
161 | #[derive(Debug, Clone, PartialEq, Eq)] | ||
162 | pub enum StructKind { | ||
163 | Record(ast::RecordFieldList), | ||
164 | Tuple(ast::TupleFieldList), | ||
165 | Unit, | ||
166 | } | ||
167 | |||
168 | impl StructKind { | ||
169 | fn from_node<N: AstNode>(node: &N) -> StructKind { | ||
170 | if let Some(nfdl) = support::child::<ast::RecordFieldList>(node.syntax()) { | ||
171 | StructKind::Record(nfdl) | ||
172 | } else if let Some(pfl) = support::child::<ast::TupleFieldList>(node.syntax()) { | ||
173 | StructKind::Tuple(pfl) | ||
174 | } else { | ||
175 | StructKind::Unit | ||
176 | } | ||
177 | } | ||
178 | } | ||
179 | |||
180 | impl ast::Struct { | ||
181 | pub fn kind(&self) -> StructKind { | ||
182 | StructKind::from_node(self) | ||
183 | } | ||
184 | } | ||
185 | |||
186 | impl ast::RecordExprField { | ||
187 | pub fn for_field_name(field_name: &ast::NameRef) -> Option<ast::RecordExprField> { | ||
188 | let candidate = | ||
189 | field_name.syntax().parent().and_then(ast::RecordExprField::cast).or_else(|| { | ||
190 | field_name.syntax().ancestors().nth(4).and_then(ast::RecordExprField::cast) | ||
191 | })?; | ||
192 | if candidate.field_name().as_ref() == Some(field_name) { | ||
193 | Some(candidate) | ||
194 | } else { | ||
195 | None | ||
196 | } | ||
197 | } | ||
198 | |||
199 | /// Deals with field init shorthand | ||
200 | pub fn field_name(&self) -> Option<ast::NameRef> { | ||
201 | if let Some(name_ref) = self.name_ref() { | ||
202 | return Some(name_ref); | ||
203 | } | ||
204 | if let Some(ast::Expr::PathExpr(expr)) = self.expr() { | ||
205 | let path = expr.path()?; | ||
206 | let segment = path.segment()?; | ||
207 | let name_ref = segment.name_ref()?; | ||
208 | if path.qualifier().is_none() { | ||
209 | return Some(name_ref); | ||
210 | } | ||
211 | } | ||
212 | None | ||
213 | } | ||
214 | } | ||
215 | |||
216 | pub enum NameOrNameRef { | ||
217 | Name(ast::Name), | ||
218 | NameRef(ast::NameRef), | ||
219 | } | ||
220 | |||
221 | impl fmt::Display for NameOrNameRef { | ||
222 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
223 | match self { | ||
224 | NameOrNameRef::Name(it) => fmt::Display::fmt(it, f), | ||
225 | NameOrNameRef::NameRef(it) => fmt::Display::fmt(it, f), | ||
226 | } | ||
227 | } | ||
228 | } | ||
229 | |||
230 | impl ast::RecordPatField { | ||
231 | /// Deals with field init shorthand | ||
232 | pub fn field_name(&self) -> Option<NameOrNameRef> { | ||
233 | if let Some(name_ref) = self.name_ref() { | ||
234 | return Some(NameOrNameRef::NameRef(name_ref)); | ||
235 | } | ||
236 | if let Some(ast::Pat::IdentPat(pat)) = self.pat() { | ||
237 | let name = pat.name()?; | ||
238 | return Some(NameOrNameRef::Name(name)); | ||
239 | } | ||
240 | None | ||
241 | } | ||
242 | } | ||
243 | |||
244 | impl ast::Variant { | ||
245 | pub fn parent_enum(&self) -> ast::Enum { | ||
246 | self.syntax() | ||
247 | .parent() | ||
248 | .and_then(|it| it.parent()) | ||
249 | .and_then(ast::Enum::cast) | ||
250 | .expect("EnumVariants are always nested in Enums") | ||
251 | } | ||
252 | pub fn kind(&self) -> StructKind { | ||
253 | StructKind::from_node(self) | ||
254 | } | ||
255 | } | ||
256 | |||
257 | #[derive(Debug, Clone, PartialEq, Eq)] | ||
258 | pub enum FieldKind { | ||
259 | Name(ast::NameRef), | ||
260 | Index(SyntaxToken), | ||
261 | } | ||
262 | |||
263 | impl ast::FieldExpr { | ||
264 | pub fn index_token(&self) -> Option<SyntaxToken> { | ||
265 | self.syntax | ||
266 | .children_with_tokens() | ||
267 | // FIXME: Accepting floats here to reject them in validation later | ||
268 | .find(|c| c.kind() == SyntaxKind::INT_NUMBER || c.kind() == SyntaxKind::FLOAT_NUMBER) | ||
269 | .as_ref() | ||
270 | .and_then(SyntaxElement::as_token) | ||
271 | .cloned() | ||
272 | } | ||
273 | |||
274 | pub fn field_access(&self) -> Option<FieldKind> { | ||
275 | if let Some(nr) = self.name_ref() { | ||
276 | Some(FieldKind::Name(nr)) | ||
277 | } else if let Some(tok) = self.index_token() { | ||
278 | Some(FieldKind::Index(tok)) | ||
279 | } else { | ||
280 | None | ||
281 | } | ||
282 | } | ||
283 | } | ||
284 | |||
285 | pub struct SlicePatComponents { | ||
286 | pub prefix: Vec<ast::Pat>, | ||
287 | pub slice: Option<ast::Pat>, | ||
288 | pub suffix: Vec<ast::Pat>, | ||
289 | } | ||
290 | |||
291 | impl ast::SlicePat { | ||
292 | pub fn components(&self) -> SlicePatComponents { | ||
293 | let mut args = self.pats().peekable(); | ||
294 | let prefix = args | ||
295 | .peeking_take_while(|p| match p { | ||
296 | ast::Pat::RestPat(_) => false, | ||
297 | ast::Pat::IdentPat(bp) => match bp.pat() { | ||
298 | Some(ast::Pat::RestPat(_)) => false, | ||
299 | _ => true, | ||
300 | }, | ||
301 | ast::Pat::RefPat(rp) => match rp.pat() { | ||
302 | Some(ast::Pat::RestPat(_)) => false, | ||
303 | Some(ast::Pat::IdentPat(bp)) => match bp.pat() { | ||
304 | Some(ast::Pat::RestPat(_)) => false, | ||
305 | _ => true, | ||
306 | }, | ||
307 | _ => true, | ||
308 | }, | ||
309 | _ => true, | ||
310 | }) | ||
311 | .collect(); | ||
312 | let slice = args.next(); | ||
313 | let suffix = args.collect(); | ||
314 | |||
315 | SlicePatComponents { prefix, slice, suffix } | ||
316 | } | ||
317 | } | ||
318 | |||
319 | #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] | ||
320 | pub enum SelfParamKind { | ||
321 | /// self | ||
322 | Owned, | ||
323 | /// &self | ||
324 | Ref, | ||
325 | /// &mut self | ||
326 | MutRef, | ||
327 | } | ||
328 | |||
329 | impl ast::SelfParam { | ||
330 | pub fn kind(&self) -> SelfParamKind { | ||
331 | if self.amp_token().is_some() { | ||
332 | if self.mut_token().is_some() { | ||
333 | SelfParamKind::MutRef | ||
334 | } else { | ||
335 | SelfParamKind::Ref | ||
336 | } | ||
337 | } else { | ||
338 | SelfParamKind::Owned | ||
339 | } | ||
340 | } | ||
341 | } | ||
342 | |||
343 | #[derive(Clone, Debug, PartialEq, Eq, Hash)] | ||
344 | pub enum TypeBoundKind { | ||
345 | /// Trait | ||
346 | PathType(ast::PathType), | ||
347 | /// for<'a> ... | ||
348 | ForType(ast::ForType), | ||
349 | /// 'a | ||
350 | Lifetime(SyntaxToken), | ||
351 | } | ||
352 | |||
353 | impl ast::TypeBound { | ||
354 | pub fn kind(&self) -> TypeBoundKind { | ||
355 | if let Some(path_type) = support::children(self.syntax()).next() { | ||
356 | TypeBoundKind::PathType(path_type) | ||
357 | } else if let Some(for_type) = support::children(self.syntax()).next() { | ||
358 | TypeBoundKind::ForType(for_type) | ||
359 | } else if let Some(lifetime) = self.lifetime_token() { | ||
360 | TypeBoundKind::Lifetime(lifetime) | ||
361 | } else { | ||
362 | unreachable!() | ||
363 | } | ||
364 | } | ||
365 | } | ||
366 | |||
367 | pub enum VisibilityKind { | ||
368 | In(ast::Path), | ||
369 | PubCrate, | ||
370 | PubSuper, | ||
371 | PubSelf, | ||
372 | Pub, | ||
373 | } | ||
374 | |||
375 | impl ast::Visibility { | ||
376 | pub fn kind(&self) -> VisibilityKind { | ||
377 | if let Some(path) = support::children(self.syntax()).next() { | ||
378 | VisibilityKind::In(path) | ||
379 | } else if self.crate_token().is_some() { | ||
380 | VisibilityKind::PubCrate | ||
381 | } else if self.super_token().is_some() { | ||
382 | VisibilityKind::PubSuper | ||
383 | } else if self.self_token().is_some() { | ||
384 | VisibilityKind::PubSelf | ||
385 | } else { | ||
386 | VisibilityKind::Pub | ||
387 | } | ||
388 | } | ||
389 | } | ||
390 | |||
391 | impl ast::MacroCall { | ||
392 | pub fn is_macro_rules(&self) -> Option<ast::Name> { | ||
393 | let name_ref = self.path()?.segment()?.name_ref()?; | ||
394 | if name_ref.text() == "macro_rules" { | ||
395 | self.name() | ||
396 | } else { | ||
397 | None | ||
398 | } | ||
399 | } | ||
400 | |||
401 | pub fn is_bang(&self) -> bool { | ||
402 | self.is_macro_rules().is_none() | ||
403 | } | ||
404 | } | ||
405 | |||
406 | impl ast::LifetimeParam { | ||
407 | pub fn lifetime_bounds(&self) -> impl Iterator<Item = SyntaxToken> { | ||
408 | self.syntax() | ||
409 | .children_with_tokens() | ||
410 | .filter_map(|it| it.into_token()) | ||
411 | .skip_while(|x| x.kind() != T![:]) | ||
412 | .filter(|it| it.kind() == T![lifetime]) | ||
413 | } | ||
414 | } | ||
415 | |||
416 | impl ast::RangePat { | ||
417 | pub fn start(&self) -> Option<ast::Pat> { | ||
418 | self.syntax() | ||
419 | .children_with_tokens() | ||
420 | .take_while(|it| !(it.kind() == T![..] || it.kind() == T![..=])) | ||
421 | .filter_map(|it| it.into_node()) | ||
422 | .find_map(ast::Pat::cast) | ||
423 | } | ||
424 | |||
425 | pub fn end(&self) -> Option<ast::Pat> { | ||
426 | self.syntax() | ||
427 | .children_with_tokens() | ||
428 | .skip_while(|it| !(it.kind() == T![..] || it.kind() == T![..=])) | ||
429 | .filter_map(|it| it.into_node()) | ||
430 | .find_map(ast::Pat::cast) | ||
431 | } | ||
432 | } | ||
433 | |||
434 | impl ast::TokenTree { | ||
435 | pub fn left_delimiter_token(&self) -> Option<SyntaxToken> { | ||
436 | self.syntax() | ||
437 | .first_child_or_token()? | ||
438 | .into_token() | ||
439 | .filter(|it| matches!(it.kind(), T!['{'] | T!['('] | T!['['])) | ||
440 | } | ||
441 | |||
442 | pub fn right_delimiter_token(&self) -> Option<SyntaxToken> { | ||
443 | self.syntax() | ||
444 | .last_child_or_token()? | ||
445 | .into_token() | ||
446 | .filter(|it| matches!(it.kind(), T!['}'] | T![')'] | T![']'])) | ||
447 | } | ||
448 | } | ||
449 | |||
450 | impl ast::GenericParamList { | ||
451 | pub fn lifetime_params(&self) -> impl Iterator<Item = ast::LifetimeParam> { | ||
452 | self.generic_params().filter_map(|param| match param { | ||
453 | ast::GenericParam::LifetimeParam(it) => Some(it), | ||
454 | ast::GenericParam::TypeParam(_) | ast::GenericParam::ConstParam(_) => None, | ||
455 | }) | ||
456 | } | ||
457 | pub fn type_params(&self) -> impl Iterator<Item = ast::TypeParam> { | ||
458 | self.generic_params().filter_map(|param| match param { | ||
459 | ast::GenericParam::TypeParam(it) => Some(it), | ||
460 | ast::GenericParam::LifetimeParam(_) | ast::GenericParam::ConstParam(_) => None, | ||
461 | }) | ||
462 | } | ||
463 | pub fn const_params(&self) -> impl Iterator<Item = ast::ConstParam> { | ||
464 | self.generic_params().filter_map(|param| match param { | ||
465 | ast::GenericParam::ConstParam(it) => Some(it), | ||
466 | ast::GenericParam::TypeParam(_) | ast::GenericParam::LifetimeParam(_) => None, | ||
467 | }) | ||
468 | } | ||
469 | } | ||
470 | |||
471 | impl ast::DocCommentsOwner for ast::SourceFile {} | ||
472 | impl ast::DocCommentsOwner for ast::Fn {} | ||
473 | impl ast::DocCommentsOwner for ast::Struct {} | ||
474 | impl ast::DocCommentsOwner for ast::Union {} | ||
475 | impl ast::DocCommentsOwner for ast::RecordField {} | ||
476 | impl ast::DocCommentsOwner for ast::TupleField {} | ||
477 | impl ast::DocCommentsOwner for ast::Enum {} | ||
478 | impl ast::DocCommentsOwner for ast::Variant {} | ||
479 | impl ast::DocCommentsOwner for ast::Trait {} | ||
480 | impl ast::DocCommentsOwner for ast::Module {} | ||
481 | impl ast::DocCommentsOwner for ast::Static {} | ||
482 | impl ast::DocCommentsOwner for ast::Const {} | ||
483 | impl ast::DocCommentsOwner for ast::TypeAlias {} | ||
484 | impl ast::DocCommentsOwner for ast::Impl {} | ||
485 | impl ast::DocCommentsOwner for ast::MacroCall {} | ||
diff --git a/crates/syntax/src/ast/token_ext.rs b/crates/syntax/src/ast/token_ext.rs new file mode 100644 index 000000000..c5ef92733 --- /dev/null +++ b/crates/syntax/src/ast/token_ext.rs | |||
@@ -0,0 +1,538 @@ | |||
1 | //! There are many AstNodes, but only a few tokens, so we hand-write them here. | ||
2 | |||
3 | use std::{ | ||
4 | borrow::Cow, | ||
5 | convert::{TryFrom, TryInto}, | ||
6 | }; | ||
7 | |||
8 | use rustc_lexer::unescape::{unescape_literal, Mode}; | ||
9 | |||
10 | use crate::{ | ||
11 | ast::{AstToken, Comment, RawString, String, Whitespace}, | ||
12 | TextRange, TextSize, | ||
13 | }; | ||
14 | |||
15 | impl Comment { | ||
16 | pub fn kind(&self) -> CommentKind { | ||
17 | kind_by_prefix(self.text()) | ||
18 | } | ||
19 | |||
20 | pub fn prefix(&self) -> &'static str { | ||
21 | for (prefix, k) in COMMENT_PREFIX_TO_KIND.iter() { | ||
22 | if *k == self.kind() && self.text().starts_with(prefix) { | ||
23 | return prefix; | ||
24 | } | ||
25 | } | ||
26 | unreachable!() | ||
27 | } | ||
28 | } | ||
29 | |||
30 | #[derive(Debug, PartialEq, Eq, Clone, Copy)] | ||
31 | pub struct CommentKind { | ||
32 | pub shape: CommentShape, | ||
33 | pub doc: Option<CommentPlacement>, | ||
34 | } | ||
35 | |||
36 | #[derive(Debug, PartialEq, Eq, Clone, Copy)] | ||
37 | pub enum CommentShape { | ||
38 | Line, | ||
39 | Block, | ||
40 | } | ||
41 | |||
42 | impl CommentShape { | ||
43 | pub fn is_line(self) -> bool { | ||
44 | self == CommentShape::Line | ||
45 | } | ||
46 | |||
47 | pub fn is_block(self) -> bool { | ||
48 | self == CommentShape::Block | ||
49 | } | ||
50 | } | ||
51 | |||
52 | #[derive(Debug, PartialEq, Eq, Clone, Copy)] | ||
53 | pub enum CommentPlacement { | ||
54 | Inner, | ||
55 | Outer, | ||
56 | } | ||
57 | |||
58 | const COMMENT_PREFIX_TO_KIND: &[(&str, CommentKind)] = { | ||
59 | use {CommentPlacement::*, CommentShape::*}; | ||
60 | &[ | ||
61 | ("////", CommentKind { shape: Line, doc: None }), | ||
62 | ("///", CommentKind { shape: Line, doc: Some(Outer) }), | ||
63 | ("//!", CommentKind { shape: Line, doc: Some(Inner) }), | ||
64 | ("/**", CommentKind { shape: Block, doc: Some(Outer) }), | ||
65 | ("/*!", CommentKind { shape: Block, doc: Some(Inner) }), | ||
66 | ("//", CommentKind { shape: Line, doc: None }), | ||
67 | ("/*", CommentKind { shape: Block, doc: None }), | ||
68 | ] | ||
69 | }; | ||
70 | |||
71 | fn kind_by_prefix(text: &str) -> CommentKind { | ||
72 | if text == "/**/" { | ||
73 | return CommentKind { shape: CommentShape::Block, doc: None }; | ||
74 | } | ||
75 | for (prefix, kind) in COMMENT_PREFIX_TO_KIND.iter() { | ||
76 | if text.starts_with(prefix) { | ||
77 | return *kind; | ||
78 | } | ||
79 | } | ||
80 | panic!("bad comment text: {:?}", text) | ||
81 | } | ||
82 | |||
83 | impl Whitespace { | ||
84 | pub fn spans_multiple_lines(&self) -> bool { | ||
85 | let text = self.text(); | ||
86 | text.find('\n').map_or(false, |idx| text[idx + 1..].contains('\n')) | ||
87 | } | ||
88 | } | ||
89 | |||
90 | pub struct QuoteOffsets { | ||
91 | pub quotes: (TextRange, TextRange), | ||
92 | pub contents: TextRange, | ||
93 | } | ||
94 | |||
95 | impl QuoteOffsets { | ||
96 | fn new(literal: &str) -> Option<QuoteOffsets> { | ||
97 | let left_quote = literal.find('"')?; | ||
98 | let right_quote = literal.rfind('"')?; | ||
99 | if left_quote == right_quote { | ||
100 | // `literal` only contains one quote | ||
101 | return None; | ||
102 | } | ||
103 | |||
104 | let start = TextSize::from(0); | ||
105 | let left_quote = TextSize::try_from(left_quote).unwrap() + TextSize::of('"'); | ||
106 | let right_quote = TextSize::try_from(right_quote).unwrap(); | ||
107 | let end = TextSize::of(literal); | ||
108 | |||
109 | let res = QuoteOffsets { | ||
110 | quotes: (TextRange::new(start, left_quote), TextRange::new(right_quote, end)), | ||
111 | contents: TextRange::new(left_quote, right_quote), | ||
112 | }; | ||
113 | Some(res) | ||
114 | } | ||
115 | } | ||
116 | |||
117 | pub trait HasQuotes: AstToken { | ||
118 | fn quote_offsets(&self) -> Option<QuoteOffsets> { | ||
119 | let text = self.text().as_str(); | ||
120 | let offsets = QuoteOffsets::new(text)?; | ||
121 | let o = self.syntax().text_range().start(); | ||
122 | let offsets = QuoteOffsets { | ||
123 | quotes: (offsets.quotes.0 + o, offsets.quotes.1 + o), | ||
124 | contents: offsets.contents + o, | ||
125 | }; | ||
126 | Some(offsets) | ||
127 | } | ||
128 | fn open_quote_text_range(&self) -> Option<TextRange> { | ||
129 | self.quote_offsets().map(|it| it.quotes.0) | ||
130 | } | ||
131 | |||
132 | fn close_quote_text_range(&self) -> Option<TextRange> { | ||
133 | self.quote_offsets().map(|it| it.quotes.1) | ||
134 | } | ||
135 | |||
136 | fn text_range_between_quotes(&self) -> Option<TextRange> { | ||
137 | self.quote_offsets().map(|it| it.contents) | ||
138 | } | ||
139 | } | ||
140 | |||
141 | impl HasQuotes for String {} | ||
142 | impl HasQuotes for RawString {} | ||
143 | |||
144 | pub trait HasStringValue: HasQuotes { | ||
145 | fn value(&self) -> Option<Cow<'_, str>>; | ||
146 | } | ||
147 | |||
148 | impl HasStringValue for String { | ||
149 | fn value(&self) -> Option<Cow<'_, str>> { | ||
150 | let text = self.text().as_str(); | ||
151 | let text = &text[self.text_range_between_quotes()? - self.syntax().text_range().start()]; | ||
152 | |||
153 | let mut buf = std::string::String::with_capacity(text.len()); | ||
154 | let mut has_error = false; | ||
155 | unescape_literal(text, Mode::Str, &mut |_, unescaped_char| match unescaped_char { | ||
156 | Ok(c) => buf.push(c), | ||
157 | Err(_) => has_error = true, | ||
158 | }); | ||
159 | |||
160 | if has_error { | ||
161 | return None; | ||
162 | } | ||
163 | // FIXME: don't actually allocate for borrowed case | ||
164 | let res = if buf == text { Cow::Borrowed(text) } else { Cow::Owned(buf) }; | ||
165 | Some(res) | ||
166 | } | ||
167 | } | ||
168 | |||
169 | impl HasStringValue for RawString { | ||
170 | fn value(&self) -> Option<Cow<'_, str>> { | ||
171 | let text = self.text().as_str(); | ||
172 | let text = &text[self.text_range_between_quotes()? - self.syntax().text_range().start()]; | ||
173 | Some(Cow::Borrowed(text)) | ||
174 | } | ||
175 | } | ||
176 | |||
177 | impl RawString { | ||
178 | pub fn map_range_up(&self, range: TextRange) -> Option<TextRange> { | ||
179 | let contents_range = self.text_range_between_quotes()?; | ||
180 | assert!(TextRange::up_to(contents_range.len()).contains_range(range)); | ||
181 | Some(range + contents_range.start()) | ||
182 | } | ||
183 | } | ||
184 | |||
185 | #[derive(Debug)] | ||
186 | pub enum FormatSpecifier { | ||
187 | Open, | ||
188 | Close, | ||
189 | Integer, | ||
190 | Identifier, | ||
191 | Colon, | ||
192 | Fill, | ||
193 | Align, | ||
194 | Sign, | ||
195 | NumberSign, | ||
196 | Zero, | ||
197 | DollarSign, | ||
198 | Dot, | ||
199 | Asterisk, | ||
200 | QuestionMark, | ||
201 | } | ||
202 | |||
203 | pub trait HasFormatSpecifier: AstToken { | ||
204 | fn char_ranges( | ||
205 | &self, | ||
206 | ) -> Option<Vec<(TextRange, Result<char, rustc_lexer::unescape::EscapeError>)>>; | ||
207 | |||
208 | fn lex_format_specifier<F>(&self, mut callback: F) | ||
209 | where | ||
210 | F: FnMut(TextRange, FormatSpecifier), | ||
211 | { | ||
212 | let char_ranges = if let Some(char_ranges) = self.char_ranges() { | ||
213 | char_ranges | ||
214 | } else { | ||
215 | return; | ||
216 | }; | ||
217 | let mut chars = char_ranges.iter().peekable(); | ||
218 | |||
219 | while let Some((range, first_char)) = chars.next() { | ||
220 | match first_char { | ||
221 | Ok('{') => { | ||
222 | // Format specifier, see syntax at https://doc.rust-lang.org/std/fmt/index.html#syntax | ||
223 | if let Some((_, Ok('{'))) = chars.peek() { | ||
224 | // Escaped format specifier, `{{` | ||
225 | chars.next(); | ||
226 | continue; | ||
227 | } | ||
228 | |||
229 | callback(*range, FormatSpecifier::Open); | ||
230 | |||
231 | // check for integer/identifier | ||
232 | match chars | ||
233 | .peek() | ||
234 | .and_then(|next| next.1.as_ref().ok()) | ||
235 | .copied() | ||
236 | .unwrap_or_default() | ||
237 | { | ||
238 | '0'..='9' => { | ||
239 | // integer | ||
240 | read_integer(&mut chars, &mut callback); | ||
241 | } | ||
242 | c if c == '_' || c.is_alphabetic() => { | ||
243 | // identifier | ||
244 | read_identifier(&mut chars, &mut callback); | ||
245 | } | ||
246 | _ => {} | ||
247 | } | ||
248 | |||
249 | if let Some((_, Ok(':'))) = chars.peek() { | ||
250 | skip_char_and_emit(&mut chars, FormatSpecifier::Colon, &mut callback); | ||
251 | |||
252 | // check for fill/align | ||
253 | let mut cloned = chars.clone().take(2); | ||
254 | let first = cloned | ||
255 | .next() | ||
256 | .and_then(|next| next.1.as_ref().ok()) | ||
257 | .copied() | ||
258 | .unwrap_or_default(); | ||
259 | let second = cloned | ||
260 | .next() | ||
261 | .and_then(|next| next.1.as_ref().ok()) | ||
262 | .copied() | ||
263 | .unwrap_or_default(); | ||
264 | match second { | ||
265 | '<' | '^' | '>' => { | ||
266 | // alignment specifier, first char specifies fillment | ||
267 | skip_char_and_emit( | ||
268 | &mut chars, | ||
269 | FormatSpecifier::Fill, | ||
270 | &mut callback, | ||
271 | ); | ||
272 | skip_char_and_emit( | ||
273 | &mut chars, | ||
274 | FormatSpecifier::Align, | ||
275 | &mut callback, | ||
276 | ); | ||
277 | } | ||
278 | _ => match first { | ||
279 | '<' | '^' | '>' => { | ||
280 | skip_char_and_emit( | ||
281 | &mut chars, | ||
282 | FormatSpecifier::Align, | ||
283 | &mut callback, | ||
284 | ); | ||
285 | } | ||
286 | _ => {} | ||
287 | }, | ||
288 | } | ||
289 | |||
290 | // check for sign | ||
291 | match chars | ||
292 | .peek() | ||
293 | .and_then(|next| next.1.as_ref().ok()) | ||
294 | .copied() | ||
295 | .unwrap_or_default() | ||
296 | { | ||
297 | '+' | '-' => { | ||
298 | skip_char_and_emit( | ||
299 | &mut chars, | ||
300 | FormatSpecifier::Sign, | ||
301 | &mut callback, | ||
302 | ); | ||
303 | } | ||
304 | _ => {} | ||
305 | } | ||
306 | |||
307 | // check for `#` | ||
308 | if let Some((_, Ok('#'))) = chars.peek() { | ||
309 | skip_char_and_emit( | ||
310 | &mut chars, | ||
311 | FormatSpecifier::NumberSign, | ||
312 | &mut callback, | ||
313 | ); | ||
314 | } | ||
315 | |||
316 | // check for `0` | ||
317 | let mut cloned = chars.clone().take(2); | ||
318 | let first = cloned.next().and_then(|next| next.1.as_ref().ok()).copied(); | ||
319 | let second = cloned.next().and_then(|next| next.1.as_ref().ok()).copied(); | ||
320 | |||
321 | if first == Some('0') && second != Some('$') { | ||
322 | skip_char_and_emit(&mut chars, FormatSpecifier::Zero, &mut callback); | ||
323 | } | ||
324 | |||
325 | // width | ||
326 | match chars | ||
327 | .peek() | ||
328 | .and_then(|next| next.1.as_ref().ok()) | ||
329 | .copied() | ||
330 | .unwrap_or_default() | ||
331 | { | ||
332 | '0'..='9' => { | ||
333 | read_integer(&mut chars, &mut callback); | ||
334 | if let Some((_, Ok('$'))) = chars.peek() { | ||
335 | skip_char_and_emit( | ||
336 | &mut chars, | ||
337 | FormatSpecifier::DollarSign, | ||
338 | &mut callback, | ||
339 | ); | ||
340 | } | ||
341 | } | ||
342 | c if c == '_' || c.is_alphabetic() => { | ||
343 | read_identifier(&mut chars, &mut callback); | ||
344 | // can be either width (indicated by dollar sign, or type in which case | ||
345 | // the next sign has to be `}`) | ||
346 | let next = | ||
347 | chars.peek().and_then(|next| next.1.as_ref().ok()).copied(); | ||
348 | match next { | ||
349 | Some('$') => skip_char_and_emit( | ||
350 | &mut chars, | ||
351 | FormatSpecifier::DollarSign, | ||
352 | &mut callback, | ||
353 | ), | ||
354 | Some('}') => { | ||
355 | skip_char_and_emit( | ||
356 | &mut chars, | ||
357 | FormatSpecifier::Close, | ||
358 | &mut callback, | ||
359 | ); | ||
360 | continue; | ||
361 | } | ||
362 | _ => continue, | ||
363 | }; | ||
364 | } | ||
365 | _ => {} | ||
366 | } | ||
367 | |||
368 | // precision | ||
369 | if let Some((_, Ok('.'))) = chars.peek() { | ||
370 | skip_char_and_emit(&mut chars, FormatSpecifier::Dot, &mut callback); | ||
371 | |||
372 | match chars | ||
373 | .peek() | ||
374 | .and_then(|next| next.1.as_ref().ok()) | ||
375 | .copied() | ||
376 | .unwrap_or_default() | ||
377 | { | ||
378 | '*' => { | ||
379 | skip_char_and_emit( | ||
380 | &mut chars, | ||
381 | FormatSpecifier::Asterisk, | ||
382 | &mut callback, | ||
383 | ); | ||
384 | } | ||
385 | '0'..='9' => { | ||
386 | read_integer(&mut chars, &mut callback); | ||
387 | if let Some((_, Ok('$'))) = chars.peek() { | ||
388 | skip_char_and_emit( | ||
389 | &mut chars, | ||
390 | FormatSpecifier::DollarSign, | ||
391 | &mut callback, | ||
392 | ); | ||
393 | } | ||
394 | } | ||
395 | c if c == '_' || c.is_alphabetic() => { | ||
396 | read_identifier(&mut chars, &mut callback); | ||
397 | if chars.peek().and_then(|next| next.1.as_ref().ok()).copied() | ||
398 | != Some('$') | ||
399 | { | ||
400 | continue; | ||
401 | } | ||
402 | skip_char_and_emit( | ||
403 | &mut chars, | ||
404 | FormatSpecifier::DollarSign, | ||
405 | &mut callback, | ||
406 | ); | ||
407 | } | ||
408 | _ => { | ||
409 | continue; | ||
410 | } | ||
411 | } | ||
412 | } | ||
413 | |||
414 | // type | ||
415 | match chars | ||
416 | .peek() | ||
417 | .and_then(|next| next.1.as_ref().ok()) | ||
418 | .copied() | ||
419 | .unwrap_or_default() | ||
420 | { | ||
421 | '?' => { | ||
422 | skip_char_and_emit( | ||
423 | &mut chars, | ||
424 | FormatSpecifier::QuestionMark, | ||
425 | &mut callback, | ||
426 | ); | ||
427 | } | ||
428 | c if c == '_' || c.is_alphabetic() => { | ||
429 | read_identifier(&mut chars, &mut callback); | ||
430 | } | ||
431 | _ => {} | ||
432 | } | ||
433 | } | ||
434 | |||
435 | if let Some((_, Ok('}'))) = chars.peek() { | ||
436 | skip_char_and_emit(&mut chars, FormatSpecifier::Close, &mut callback); | ||
437 | } else { | ||
438 | continue; | ||
439 | } | ||
440 | } | ||
441 | _ => { | ||
442 | while let Some((_, Ok(next_char))) = chars.peek() { | ||
443 | match next_char { | ||
444 | '{' => break, | ||
445 | _ => {} | ||
446 | } | ||
447 | chars.next(); | ||
448 | } | ||
449 | } | ||
450 | }; | ||
451 | } | ||
452 | |||
453 | fn skip_char_and_emit<'a, I, F>( | ||
454 | chars: &mut std::iter::Peekable<I>, | ||
455 | emit: FormatSpecifier, | ||
456 | callback: &mut F, | ||
457 | ) where | ||
458 | I: Iterator<Item = &'a (TextRange, Result<char, rustc_lexer::unescape::EscapeError>)>, | ||
459 | F: FnMut(TextRange, FormatSpecifier), | ||
460 | { | ||
461 | let (range, _) = chars.next().unwrap(); | ||
462 | callback(*range, emit); | ||
463 | } | ||
464 | |||
465 | fn read_integer<'a, I, F>(chars: &mut std::iter::Peekable<I>, callback: &mut F) | ||
466 | where | ||
467 | I: Iterator<Item = &'a (TextRange, Result<char, rustc_lexer::unescape::EscapeError>)>, | ||
468 | F: FnMut(TextRange, FormatSpecifier), | ||
469 | { | ||
470 | let (mut range, c) = chars.next().unwrap(); | ||
471 | assert!(c.as_ref().unwrap().is_ascii_digit()); | ||
472 | while let Some((r, Ok(next_char))) = chars.peek() { | ||
473 | if next_char.is_ascii_digit() { | ||
474 | chars.next(); | ||
475 | range = range.cover(*r); | ||
476 | } else { | ||
477 | break; | ||
478 | } | ||
479 | } | ||
480 | callback(range, FormatSpecifier::Integer); | ||
481 | } | ||
482 | |||
483 | fn read_identifier<'a, I, F>(chars: &mut std::iter::Peekable<I>, callback: &mut F) | ||
484 | where | ||
485 | I: Iterator<Item = &'a (TextRange, Result<char, rustc_lexer::unescape::EscapeError>)>, | ||
486 | F: FnMut(TextRange, FormatSpecifier), | ||
487 | { | ||
488 | let (mut range, c) = chars.next().unwrap(); | ||
489 | assert!(c.as_ref().unwrap().is_alphabetic() || *c.as_ref().unwrap() == '_'); | ||
490 | while let Some((r, Ok(next_char))) = chars.peek() { | ||
491 | if *next_char == '_' || next_char.is_ascii_digit() || next_char.is_alphabetic() { | ||
492 | chars.next(); | ||
493 | range = range.cover(*r); | ||
494 | } else { | ||
495 | break; | ||
496 | } | ||
497 | } | ||
498 | callback(range, FormatSpecifier::Identifier); | ||
499 | } | ||
500 | } | ||
501 | } | ||
502 | |||
503 | impl HasFormatSpecifier for String { | ||
504 | fn char_ranges( | ||
505 | &self, | ||
506 | ) -> Option<Vec<(TextRange, Result<char, rustc_lexer::unescape::EscapeError>)>> { | ||
507 | let text = self.text().as_str(); | ||
508 | let text = &text[self.text_range_between_quotes()? - self.syntax().text_range().start()]; | ||
509 | let offset = self.text_range_between_quotes()?.start() - self.syntax().text_range().start(); | ||
510 | |||
511 | let mut res = Vec::with_capacity(text.len()); | ||
512 | unescape_literal(text, Mode::Str, &mut |range, unescaped_char| { | ||
513 | res.push(( | ||
514 | TextRange::new(range.start.try_into().unwrap(), range.end.try_into().unwrap()) | ||
515 | + offset, | ||
516 | unescaped_char, | ||
517 | )) | ||
518 | }); | ||
519 | |||
520 | Some(res) | ||
521 | } | ||
522 | } | ||
523 | |||
524 | impl HasFormatSpecifier for RawString { | ||
525 | fn char_ranges( | ||
526 | &self, | ||
527 | ) -> Option<Vec<(TextRange, Result<char, rustc_lexer::unescape::EscapeError>)>> { | ||
528 | let text = self.text().as_str(); | ||
529 | let text = &text[self.text_range_between_quotes()? - self.syntax().text_range().start()]; | ||
530 | let offset = self.text_range_between_quotes()?.start() - self.syntax().text_range().start(); | ||
531 | |||
532 | let mut res = Vec::with_capacity(text.len()); | ||
533 | for (idx, c) in text.char_indices() { | ||
534 | res.push((TextRange::at(idx.try_into().unwrap(), TextSize::of(c)) + offset, Ok(c))); | ||
535 | } | ||
536 | Some(res) | ||
537 | } | ||
538 | } | ||
diff --git a/crates/syntax/src/ast/traits.rs b/crates/syntax/src/ast/traits.rs new file mode 100644 index 000000000..0bdc22d95 --- /dev/null +++ b/crates/syntax/src/ast/traits.rs | |||
@@ -0,0 +1,141 @@ | |||
1 | //! Various traits that are implemented by ast nodes. | ||
2 | //! | ||
3 | //! The implementations are usually trivial, and live in generated.rs | ||
4 | use itertools::Itertools; | ||
5 | |||
6 | use crate::{ | ||
7 | ast::{self, support, AstChildren, AstNode, AstToken}, | ||
8 | syntax_node::SyntaxElementChildren, | ||
9 | SyntaxToken, T, | ||
10 | }; | ||
11 | |||
12 | pub trait NameOwner: AstNode { | ||
13 | fn name(&self) -> Option<ast::Name> { | ||
14 | support::child(self.syntax()) | ||
15 | } | ||
16 | } | ||
17 | |||
18 | pub trait VisibilityOwner: AstNode { | ||
19 | fn visibility(&self) -> Option<ast::Visibility> { | ||
20 | support::child(self.syntax()) | ||
21 | } | ||
22 | } | ||
23 | |||
24 | pub trait LoopBodyOwner: AstNode { | ||
25 | fn loop_body(&self) -> Option<ast::BlockExpr> { | ||
26 | support::child(self.syntax()) | ||
27 | } | ||
28 | |||
29 | fn label(&self) -> Option<ast::Label> { | ||
30 | support::child(self.syntax()) | ||
31 | } | ||
32 | } | ||
33 | |||
34 | pub trait ArgListOwner: AstNode { | ||
35 | fn arg_list(&self) -> Option<ast::ArgList> { | ||
36 | support::child(self.syntax()) | ||
37 | } | ||
38 | } | ||
39 | |||
40 | pub trait ModuleItemOwner: AstNode { | ||
41 | fn items(&self) -> AstChildren<ast::Item> { | ||
42 | support::children(self.syntax()) | ||
43 | } | ||
44 | } | ||
45 | |||
46 | pub trait GenericParamsOwner: AstNode { | ||
47 | fn generic_param_list(&self) -> Option<ast::GenericParamList> { | ||
48 | support::child(self.syntax()) | ||
49 | } | ||
50 | |||
51 | fn where_clause(&self) -> Option<ast::WhereClause> { | ||
52 | support::child(self.syntax()) | ||
53 | } | ||
54 | } | ||
55 | |||
56 | pub trait TypeBoundsOwner: AstNode { | ||
57 | fn type_bound_list(&self) -> Option<ast::TypeBoundList> { | ||
58 | support::child(self.syntax()) | ||
59 | } | ||
60 | |||
61 | fn colon_token(&self) -> Option<SyntaxToken> { | ||
62 | support::token(self.syntax(), T![:]) | ||
63 | } | ||
64 | } | ||
65 | |||
66 | pub trait AttrsOwner: AstNode { | ||
67 | fn attrs(&self) -> AstChildren<ast::Attr> { | ||
68 | support::children(self.syntax()) | ||
69 | } | ||
70 | fn has_atom_attr(&self, atom: &str) -> bool { | ||
71 | self.attrs().filter_map(|x| x.as_simple_atom()).any(|x| x == atom) | ||
72 | } | ||
73 | } | ||
74 | |||
75 | pub trait DocCommentsOwner: AstNode { | ||
76 | fn doc_comments(&self) -> CommentIter { | ||
77 | CommentIter { iter: self.syntax().children_with_tokens() } | ||
78 | } | ||
79 | |||
80 | fn doc_comment_text(&self) -> Option<String> { | ||
81 | self.doc_comments().doc_comment_text() | ||
82 | } | ||
83 | } | ||
84 | |||
85 | impl CommentIter { | ||
86 | pub fn from_syntax_node(syntax_node: &ast::SyntaxNode) -> CommentIter { | ||
87 | CommentIter { iter: syntax_node.children_with_tokens() } | ||
88 | } | ||
89 | |||
90 | /// Returns the textual content of a doc comment block as a single string. | ||
91 | /// That is, strips leading `///` (+ optional 1 character of whitespace), | ||
92 | /// trailing `*/`, trailing whitespace and then joins the lines. | ||
93 | pub fn doc_comment_text(self) -> Option<String> { | ||
94 | let mut has_comments = false; | ||
95 | let docs = self | ||
96 | .filter(|comment| comment.kind().doc.is_some()) | ||
97 | .map(|comment| { | ||
98 | has_comments = true; | ||
99 | let prefix_len = comment.prefix().len(); | ||
100 | |||
101 | let line: &str = comment.text().as_str(); | ||
102 | |||
103 | // Determine if the prefix or prefix + 1 char is stripped | ||
104 | let pos = | ||
105 | if let Some(ws) = line.chars().nth(prefix_len).filter(|c| c.is_whitespace()) { | ||
106 | prefix_len + ws.len_utf8() | ||
107 | } else { | ||
108 | prefix_len | ||
109 | }; | ||
110 | |||
111 | let end = if comment.kind().shape.is_block() && line.ends_with("*/") { | ||
112 | line.len() - 2 | ||
113 | } else { | ||
114 | line.len() | ||
115 | }; | ||
116 | |||
117 | // Note that we do not trim the end of the line here | ||
118 | // since whitespace can have special meaning at the end | ||
119 | // of a line in markdown. | ||
120 | line[pos..end].to_owned() | ||
121 | }) | ||
122 | .join("\n"); | ||
123 | |||
124 | if has_comments { | ||
125 | Some(docs) | ||
126 | } else { | ||
127 | None | ||
128 | } | ||
129 | } | ||
130 | } | ||
131 | |||
132 | pub struct CommentIter { | ||
133 | iter: SyntaxElementChildren, | ||
134 | } | ||
135 | |||
136 | impl Iterator for CommentIter { | ||
137 | type Item = ast::Comment; | ||
138 | fn next(&mut self) -> Option<ast::Comment> { | ||
139 | self.iter.by_ref().find_map(|el| el.into_token().and_then(ast::Comment::cast)) | ||
140 | } | ||
141 | } | ||