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