aboutsummaryrefslogtreecommitdiff
path: root/crates/syntax/src
diff options
context:
space:
mode:
authorLukas Wirth <[email protected]>2020-12-15 18:23:51 +0000
committerLukas Wirth <[email protected]>2020-12-16 13:16:09 +0000
commitdd496223f50232fe98312ee8edc89eb4b5ee3d85 (patch)
tree4d50c04ca78f9458ab536ff1edee76eba6ab1957 /crates/syntax/src
parentd34611633b3b2404188b9e12b08c5def589808c2 (diff)
Node-ify lifetimes
Diffstat (limited to 'crates/syntax/src')
-rw-r--r--crates/syntax/src/ast.rs2
-rw-r--r--crates/syntax/src/ast/generated/nodes.rs62
-rw-r--r--crates/syntax/src/ast/node_ext.rs12
-rw-r--r--crates/syntax/src/parsing/lexer.rs4
4 files changed, 47 insertions, 33 deletions
diff --git a/crates/syntax/src/ast.rs b/crates/syntax/src/ast.rs
index 70c568ea1..83de067d9 100644
--- a/crates/syntax/src/ast.rs
+++ b/crates/syntax/src/ast.rs
@@ -311,7 +311,7 @@ where
311 let pred = predicates.next().unwrap(); 311 let pred = predicates.next().unwrap();
312 let mut bounds = pred.type_bound_list().unwrap().bounds(); 312 let mut bounds = pred.type_bound_list().unwrap().bounds();
313 313
314 assert_eq!("'a", pred.lifetime_token().unwrap().text()); 314 assert_eq!("'a", pred.lifetime().unwrap().lifetime_ident_token().unwrap().text());
315 315
316 assert_bound("'b", bounds.next()); 316 assert_bound("'b", bounds.next());
317 assert_bound("'c", bounds.next()); 317 assert_bound("'c", bounds.next());
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)]
23pub struct Lifetime {
24 pub(crate) syntax: SyntaxNode,
25}
26impl 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)]
23pub struct Path { 32pub 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}
107impl LifetimeArg { 116impl 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)]
113pub struct ConstArg { 120pub struct ConstArg {
@@ -487,9 +494,7 @@ pub struct SelfParam {
487impl ast::AttrsOwner for SelfParam {} 494impl ast::AttrsOwner for SelfParam {}
488impl SelfParam { 495impl 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 {
605impl ast::AttrsOwner for LifetimeParam {} 610impl ast::AttrsOwner for LifetimeParam {}
606impl ast::TypeBoundsOwner for LifetimeParam {} 611impl ast::TypeBoundsOwner for LifetimeParam {}
607impl LifetimeParam { 612impl 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)]
613pub struct TypeParam { 616pub struct TypeParam {
@@ -628,9 +631,7 @@ impl ast::TypeBoundsOwner for WherePred {}
628impl WherePred { 631impl 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 {
706impl ast::AttrsOwner for BreakExpr {} 707impl ast::AttrsOwner for BreakExpr {}
707impl BreakExpr { 708impl 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)]
760pub struct EffectExpr { 757pub struct EffectExpr {
@@ -937,9 +934,8 @@ pub struct Label {
937 pub(crate) syntax: SyntaxNode, 934 pub(crate) syntax: SyntaxNode,
938} 935}
939impl Label { 936impl 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)]
945pub struct RecordExprFieldList { 941pub struct RecordExprFieldList {
@@ -1100,9 +1096,7 @@ pub struct RefType {
1100} 1096}
1101impl RefType { 1097impl 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}
1131impl TypeBound { 1125impl 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}
1433impl 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}
1441impl AstNode for Path { 1444impl 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}
3530impl 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}
3527impl std::fmt::Display for Path { 3535impl 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
15impl ast::Lifetime {
16 pub fn text(&self) -> &SmolStr {
17 text_of_first_token(self.syntax())
18 }
19}
20
15impl ast::Name { 21impl 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
399impl ast::TypeBound { 405impl 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
diff --git a/crates/syntax/src/parsing/lexer.rs b/crates/syntax/src/parsing/lexer.rs
index 8afd7e53b..0cbba73c5 100644
--- a/crates/syntax/src/parsing/lexer.rs
+++ b/crates/syntax/src/parsing/lexer.rs
@@ -146,9 +146,9 @@ fn rustc_token_kind_to_syntax_kind(
146 rustc_lexer::TokenKind::RawIdent => IDENT, 146 rustc_lexer::TokenKind::RawIdent => IDENT,
147 rustc_lexer::TokenKind::Literal { kind, .. } => return match_literal_kind(&kind), 147 rustc_lexer::TokenKind::Literal { kind, .. } => return match_literal_kind(&kind),
148 148
149 rustc_lexer::TokenKind::Lifetime { starts_with_number: false } => LIFETIME, 149 rustc_lexer::TokenKind::Lifetime { starts_with_number: false } => LIFETIME_IDENT,
150 rustc_lexer::TokenKind::Lifetime { starts_with_number: true } => { 150 rustc_lexer::TokenKind::Lifetime { starts_with_number: true } => {
151 return (LIFETIME, Some("Lifetime name cannot start with a number")) 151 return (LIFETIME_IDENT, Some("Lifetime name cannot start with a number"))
152 } 152 }
153 153
154 rustc_lexer::TokenKind::Semi => T![;], 154 rustc_lexer::TokenKind::Semi => T![;],