diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-12-16 17:08:03 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2020-12-16 17:08:03 +0000 |
commit | 067067a6c11bb5afda98f5af14bfdec4744e7812 (patch) | |
tree | 1c0b6c4c78ee040ebdf818dada804fce311382a6 /crates/syntax/src/ast | |
parent | 63bbdb31e5148c804bbf940963c9c8f3481ad258 (diff) | |
parent | dd496223f50232fe98312ee8edc89eb4b5ee3d85 (diff) |
Merge #6896
6896: Node-ify lifetimes r=jonas-schievink a=Veykril
Let's see if this passes the tests 🤞
Depends on https://github.com/rust-analyzer/ungrammar/pull/15
Co-authored-by: Jonas Schievink <[email protected]>
Co-authored-by: Jonas Schievink <[email protected]>
Co-authored-by: Lukas Wirth <[email protected]>
Diffstat (limited to 'crates/syntax/src/ast')
-rw-r--r-- | crates/syntax/src/ast/generated/nodes.rs | 62 | ||||
-rw-r--r-- | crates/syntax/src/ast/node_ext.rs | 12 |
2 files changed, 44 insertions, 30 deletions
diff --git a/crates/syntax/src/ast/generated/nodes.rs b/crates/syntax/src/ast/generated/nodes.rs index 6eae323f4..1588ba93e 100644 --- a/crates/syntax/src/ast/generated/nodes.rs +++ b/crates/syntax/src/ast/generated/nodes.rs | |||
@@ -20,6 +20,15 @@ impl NameRef { | |||
20 | pub fn ident_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![ident]) } | 20 | pub fn ident_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![ident]) } |
21 | } | 21 | } |
22 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 22 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
23 | pub struct Lifetime { | ||
24 | pub(crate) syntax: SyntaxNode, | ||
25 | } | ||
26 | impl Lifetime { | ||
27 | pub fn lifetime_ident_token(&self) -> Option<SyntaxToken> { | ||
28 | support::token(&self.syntax, T![lifetime_ident]) | ||
29 | } | ||
30 | } | ||
31 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
23 | pub struct Path { | 32 | pub struct Path { |
24 | pub(crate) syntax: SyntaxNode, | 33 | pub(crate) syntax: SyntaxNode, |
25 | } | 34 | } |
@@ -105,9 +114,7 @@ pub struct LifetimeArg { | |||
105 | pub(crate) syntax: SyntaxNode, | 114 | pub(crate) syntax: SyntaxNode, |
106 | } | 115 | } |
107 | impl LifetimeArg { | 116 | impl LifetimeArg { |
108 | pub fn lifetime_token(&self) -> Option<SyntaxToken> { | 117 | pub fn lifetime(&self) -> Option<Lifetime> { support::child(&self.syntax) } |
109 | support::token(&self.syntax, T![lifetime]) | ||
110 | } | ||
111 | } | 118 | } |
112 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 119 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
113 | pub struct ConstArg { | 120 | pub struct ConstArg { |
@@ -487,9 +494,7 @@ pub struct SelfParam { | |||
487 | impl ast::AttrsOwner for SelfParam {} | 494 | impl ast::AttrsOwner for SelfParam {} |
488 | impl SelfParam { | 495 | impl SelfParam { |
489 | pub fn amp_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![&]) } | 496 | pub fn amp_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![&]) } |
490 | pub fn lifetime_token(&self) -> Option<SyntaxToken> { | 497 | pub fn lifetime(&self) -> Option<Lifetime> { support::child(&self.syntax) } |
491 | support::token(&self.syntax, T![lifetime]) | ||
492 | } | ||
493 | pub fn mut_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![mut]) } | 498 | pub fn mut_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![mut]) } |
494 | pub fn self_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![self]) } | 499 | pub fn self_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![self]) } |
495 | pub fn colon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![:]) } | 500 | pub fn colon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![:]) } |
@@ -605,9 +610,7 @@ pub struct LifetimeParam { | |||
605 | impl ast::AttrsOwner for LifetimeParam {} | 610 | impl ast::AttrsOwner for LifetimeParam {} |
606 | impl ast::TypeBoundsOwner for LifetimeParam {} | 611 | impl ast::TypeBoundsOwner for LifetimeParam {} |
607 | impl LifetimeParam { | 612 | impl LifetimeParam { |
608 | pub fn lifetime_token(&self) -> Option<SyntaxToken> { | 613 | pub fn lifetime(&self) -> Option<Lifetime> { support::child(&self.syntax) } |
609 | support::token(&self.syntax, T![lifetime]) | ||
610 | } | ||
611 | } | 614 | } |
612 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 615 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
613 | pub struct TypeParam { | 616 | pub struct TypeParam { |
@@ -628,9 +631,7 @@ impl ast::TypeBoundsOwner for WherePred {} | |||
628 | impl WherePred { | 631 | impl WherePred { |
629 | pub fn for_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![for]) } | 632 | pub fn for_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![for]) } |
630 | pub fn generic_param_list(&self) -> Option<GenericParamList> { support::child(&self.syntax) } | 633 | pub fn generic_param_list(&self) -> Option<GenericParamList> { support::child(&self.syntax) } |
631 | pub fn lifetime_token(&self) -> Option<SyntaxToken> { | 634 | pub fn lifetime(&self) -> Option<Lifetime> { support::child(&self.syntax) } |
632 | support::token(&self.syntax, T![lifetime]) | ||
633 | } | ||
634 | pub fn ty(&self) -> Option<Type> { support::child(&self.syntax) } | 635 | pub fn ty(&self) -> Option<Type> { support::child(&self.syntax) } |
635 | } | 636 | } |
636 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 637 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
@@ -706,9 +707,7 @@ pub struct BreakExpr { | |||
706 | impl ast::AttrsOwner for BreakExpr {} | 707 | impl ast::AttrsOwner for BreakExpr {} |
707 | impl BreakExpr { | 708 | impl BreakExpr { |
708 | pub fn break_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![break]) } | 709 | pub fn break_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![break]) } |
709 | pub fn lifetime_token(&self) -> Option<SyntaxToken> { | 710 | pub fn lifetime(&self) -> Option<Lifetime> { support::child(&self.syntax) } |
710 | support::token(&self.syntax, T![lifetime]) | ||
711 | } | ||
712 | pub fn expr(&self) -> Option<Expr> { support::child(&self.syntax) } | 711 | pub fn expr(&self) -> Option<Expr> { support::child(&self.syntax) } |
713 | } | 712 | } |
714 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 713 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
@@ -752,9 +751,7 @@ impl ContinueExpr { | |||
752 | pub fn continue_token(&self) -> Option<SyntaxToken> { | 751 | pub fn continue_token(&self) -> Option<SyntaxToken> { |
753 | support::token(&self.syntax, T![continue]) | 752 | support::token(&self.syntax, T![continue]) |
754 | } | 753 | } |
755 | pub fn lifetime_token(&self) -> Option<SyntaxToken> { | 754 | pub fn lifetime(&self) -> Option<Lifetime> { support::child(&self.syntax) } |
756 | support::token(&self.syntax, T![lifetime]) | ||
757 | } | ||
758 | } | 755 | } |
759 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 756 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
760 | pub struct EffectExpr { | 757 | pub struct EffectExpr { |
@@ -937,9 +934,8 @@ pub struct Label { | |||
937 | pub(crate) syntax: SyntaxNode, | 934 | pub(crate) syntax: SyntaxNode, |
938 | } | 935 | } |
939 | impl Label { | 936 | impl Label { |
940 | pub fn lifetime_token(&self) -> Option<SyntaxToken> { | 937 | pub fn lifetime(&self) -> Option<Lifetime> { support::child(&self.syntax) } |
941 | support::token(&self.syntax, T![lifetime]) | 938 | pub fn colon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![:]) } |
942 | } | ||
943 | } | 939 | } |
944 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 940 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
945 | pub struct RecordExprFieldList { | 941 | pub struct RecordExprFieldList { |
@@ -1100,9 +1096,7 @@ pub struct RefType { | |||
1100 | } | 1096 | } |
1101 | impl RefType { | 1097 | impl RefType { |
1102 | pub fn amp_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![&]) } | 1098 | pub fn amp_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![&]) } |
1103 | pub fn lifetime_token(&self) -> Option<SyntaxToken> { | 1099 | pub fn lifetime(&self) -> Option<Lifetime> { support::child(&self.syntax) } |
1104 | support::token(&self.syntax, T![lifetime]) | ||
1105 | } | ||
1106 | pub fn mut_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![mut]) } | 1100 | pub fn mut_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![mut]) } |
1107 | pub fn ty(&self) -> Option<Type> { support::child(&self.syntax) } | 1101 | pub fn ty(&self) -> Option<Type> { support::child(&self.syntax) } |
1108 | } | 1102 | } |
@@ -1129,9 +1123,7 @@ pub struct TypeBound { | |||
1129 | pub(crate) syntax: SyntaxNode, | 1123 | pub(crate) syntax: SyntaxNode, |
1130 | } | 1124 | } |
1131 | impl TypeBound { | 1125 | impl TypeBound { |
1132 | pub fn lifetime_token(&self) -> Option<SyntaxToken> { | 1126 | pub fn lifetime(&self) -> Option<Lifetime> { support::child(&self.syntax) } |
1133 | support::token(&self.syntax, T![lifetime]) | ||
1134 | } | ||
1135 | pub fn question_mark_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![?]) } | 1127 | pub fn question_mark_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![?]) } |
1136 | pub fn ty(&self) -> Option<Type> { support::child(&self.syntax) } | 1128 | pub fn ty(&self) -> Option<Type> { support::child(&self.syntax) } |
1137 | } | 1129 | } |
@@ -1438,6 +1430,17 @@ impl AstNode for NameRef { | |||
1438 | } | 1430 | } |
1439 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | 1431 | fn syntax(&self) -> &SyntaxNode { &self.syntax } |
1440 | } | 1432 | } |
1433 | impl AstNode for Lifetime { | ||
1434 | fn can_cast(kind: SyntaxKind) -> bool { kind == LIFETIME } | ||
1435 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1436 | if Self::can_cast(syntax.kind()) { | ||
1437 | Some(Self { syntax }) | ||
1438 | } else { | ||
1439 | None | ||
1440 | } | ||
1441 | } | ||
1442 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1443 | } | ||
1441 | impl AstNode for Path { | 1444 | impl AstNode for Path { |
1442 | fn can_cast(kind: SyntaxKind) -> bool { kind == PATH } | 1445 | fn can_cast(kind: SyntaxKind) -> bool { kind == PATH } |
1443 | fn cast(syntax: SyntaxNode) -> Option<Self> { | 1446 | fn cast(syntax: SyntaxNode) -> Option<Self> { |
@@ -3524,6 +3527,11 @@ impl std::fmt::Display for NameRef { | |||
3524 | std::fmt::Display::fmt(self.syntax(), f) | 3527 | std::fmt::Display::fmt(self.syntax(), f) |
3525 | } | 3528 | } |
3526 | } | 3529 | } |
3530 | impl std::fmt::Display for Lifetime { | ||
3531 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3532 | std::fmt::Display::fmt(self.syntax(), f) | ||
3533 | } | ||
3534 | } | ||
3527 | impl std::fmt::Display for Path { | 3535 | impl std::fmt::Display for Path { |
3528 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | 3536 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
3529 | std::fmt::Display::fmt(self.syntax(), f) | 3537 | std::fmt::Display::fmt(self.syntax(), f) |
diff --git a/crates/syntax/src/ast/node_ext.rs b/crates/syntax/src/ast/node_ext.rs index 40dec3c7f..c45cb514a 100644 --- a/crates/syntax/src/ast/node_ext.rs +++ b/crates/syntax/src/ast/node_ext.rs | |||
@@ -12,6 +12,12 @@ use crate::{ | |||
12 | SmolStr, SyntaxElement, SyntaxToken, T, | 12 | SmolStr, SyntaxElement, SyntaxToken, T, |
13 | }; | 13 | }; |
14 | 14 | ||
15 | impl ast::Lifetime { | ||
16 | pub fn text(&self) -> &SmolStr { | ||
17 | text_of_first_token(self.syntax()) | ||
18 | } | ||
19 | } | ||
20 | |||
15 | impl ast::Name { | 21 | impl ast::Name { |
16 | pub fn text(&self) -> &SmolStr { | 22 | pub fn text(&self) -> &SmolStr { |
17 | text_of_first_token(self.syntax()) | 23 | text_of_first_token(self.syntax()) |
@@ -393,7 +399,7 @@ pub enum TypeBoundKind { | |||
393 | /// for<'a> ... | 399 | /// for<'a> ... |
394 | ForType(ast::ForType), | 400 | ForType(ast::ForType), |
395 | /// 'a | 401 | /// 'a |
396 | Lifetime(SyntaxToken), | 402 | Lifetime(ast::Lifetime), |
397 | } | 403 | } |
398 | 404 | ||
399 | impl ast::TypeBound { | 405 | impl ast::TypeBound { |
@@ -402,7 +408,7 @@ impl ast::TypeBound { | |||
402 | TypeBoundKind::PathType(path_type) | 408 | TypeBoundKind::PathType(path_type) |
403 | } else if let Some(for_type) = support::children(self.syntax()).next() { | 409 | } else if let Some(for_type) = support::children(self.syntax()).next() { |
404 | TypeBoundKind::ForType(for_type) | 410 | TypeBoundKind::ForType(for_type) |
405 | } else if let Some(lifetime) = self.lifetime_token() { | 411 | } else if let Some(lifetime) = self.lifetime() { |
406 | TypeBoundKind::Lifetime(lifetime) | 412 | TypeBoundKind::Lifetime(lifetime) |
407 | } else { | 413 | } else { |
408 | unreachable!() | 414 | unreachable!() |
@@ -440,7 +446,7 @@ impl ast::LifetimeParam { | |||
440 | .children_with_tokens() | 446 | .children_with_tokens() |
441 | .filter_map(|it| it.into_token()) | 447 | .filter_map(|it| it.into_token()) |
442 | .skip_while(|x| x.kind() != T![:]) | 448 | .skip_while(|x| x.kind() != T![:]) |
443 | .filter(|it| it.kind() == T![lifetime]) | 449 | .filter(|it| it.kind() == T![lifetime_ident]) |
444 | } | 450 | } |
445 | } | 451 | } |
446 | 452 | ||