From dd496223f50232fe98312ee8edc89eb4b5ee3d85 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 15 Dec 2020 19:23:51 +0100 Subject: Node-ify lifetimes --- crates/syntax/src/ast.rs | 2 +- crates/syntax/src/ast/generated/nodes.rs | 62 ++++++++++++++++++-------------- crates/syntax/src/ast/node_ext.rs | 12 +++++-- crates/syntax/src/parsing/lexer.rs | 4 +-- 4 files changed, 47 insertions(+), 33 deletions(-) (limited to 'crates/syntax/src') 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 let pred = predicates.next().unwrap(); let mut bounds = pred.type_bound_list().unwrap().bounds(); - assert_eq!("'a", pred.lifetime_token().unwrap().text()); + assert_eq!("'a", pred.lifetime().unwrap().lifetime_ident_token().unwrap().text()); assert_bound("'b", bounds.next()); 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 { pub fn ident_token(&self) -> Option { support::token(&self.syntax, T![ident]) } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct Lifetime { + pub(crate) syntax: SyntaxNode, +} +impl Lifetime { + pub fn lifetime_ident_token(&self) -> Option { + support::token(&self.syntax, T![lifetime_ident]) + } +} +#[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct Path { pub(crate) syntax: SyntaxNode, } @@ -105,9 +114,7 @@ pub struct LifetimeArg { pub(crate) syntax: SyntaxNode, } impl LifetimeArg { - pub fn lifetime_token(&self) -> Option { - support::token(&self.syntax, T![lifetime]) - } + pub fn lifetime(&self) -> Option { support::child(&self.syntax) } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct ConstArg { @@ -487,9 +494,7 @@ pub struct SelfParam { impl ast::AttrsOwner for SelfParam {} impl SelfParam { pub fn amp_token(&self) -> Option { support::token(&self.syntax, T![&]) } - pub fn lifetime_token(&self) -> Option { - support::token(&self.syntax, T![lifetime]) - } + pub fn lifetime(&self) -> Option { support::child(&self.syntax) } pub fn mut_token(&self) -> Option { support::token(&self.syntax, T![mut]) } pub fn self_token(&self) -> Option { support::token(&self.syntax, T![self]) } pub fn colon_token(&self) -> Option { support::token(&self.syntax, T![:]) } @@ -605,9 +610,7 @@ pub struct LifetimeParam { impl ast::AttrsOwner for LifetimeParam {} impl ast::TypeBoundsOwner for LifetimeParam {} impl LifetimeParam { - pub fn lifetime_token(&self) -> Option { - support::token(&self.syntax, T![lifetime]) - } + pub fn lifetime(&self) -> Option { support::child(&self.syntax) } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct TypeParam { @@ -628,9 +631,7 @@ impl ast::TypeBoundsOwner for WherePred {} impl WherePred { pub fn for_token(&self) -> Option { support::token(&self.syntax, T![for]) } pub fn generic_param_list(&self) -> Option { support::child(&self.syntax) } - pub fn lifetime_token(&self) -> Option { - support::token(&self.syntax, T![lifetime]) - } + pub fn lifetime(&self) -> Option { support::child(&self.syntax) } pub fn ty(&self) -> Option { support::child(&self.syntax) } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] @@ -706,9 +707,7 @@ pub struct BreakExpr { impl ast::AttrsOwner for BreakExpr {} impl BreakExpr { pub fn break_token(&self) -> Option { support::token(&self.syntax, T![break]) } - pub fn lifetime_token(&self) -> Option { - support::token(&self.syntax, T![lifetime]) - } + pub fn lifetime(&self) -> Option { support::child(&self.syntax) } pub fn expr(&self) -> Option { support::child(&self.syntax) } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] @@ -752,9 +751,7 @@ impl ContinueExpr { pub fn continue_token(&self) -> Option { support::token(&self.syntax, T![continue]) } - pub fn lifetime_token(&self) -> Option { - support::token(&self.syntax, T![lifetime]) - } + pub fn lifetime(&self) -> Option { support::child(&self.syntax) } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct EffectExpr { @@ -937,9 +934,8 @@ pub struct Label { pub(crate) syntax: SyntaxNode, } impl Label { - pub fn lifetime_token(&self) -> Option { - support::token(&self.syntax, T![lifetime]) - } + pub fn lifetime(&self) -> Option { support::child(&self.syntax) } + pub fn colon_token(&self) -> Option { support::token(&self.syntax, T![:]) } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct RecordExprFieldList { @@ -1100,9 +1096,7 @@ pub struct RefType { } impl RefType { pub fn amp_token(&self) -> Option { support::token(&self.syntax, T![&]) } - pub fn lifetime_token(&self) -> Option { - support::token(&self.syntax, T![lifetime]) - } + pub fn lifetime(&self) -> Option { support::child(&self.syntax) } pub fn mut_token(&self) -> Option { support::token(&self.syntax, T![mut]) } pub fn ty(&self) -> Option { support::child(&self.syntax) } } @@ -1129,9 +1123,7 @@ pub struct TypeBound { pub(crate) syntax: SyntaxNode, } impl TypeBound { - pub fn lifetime_token(&self) -> Option { - support::token(&self.syntax, T![lifetime]) - } + pub fn lifetime(&self) -> Option { support::child(&self.syntax) } pub fn question_mark_token(&self) -> Option { support::token(&self.syntax, T![?]) } pub fn ty(&self) -> Option { support::child(&self.syntax) } } @@ -1438,6 +1430,17 @@ impl AstNode for NameRef { } fn syntax(&self) -> &SyntaxNode { &self.syntax } } +impl AstNode for Lifetime { + fn can_cast(kind: SyntaxKind) -> bool { kind == LIFETIME } + fn cast(syntax: SyntaxNode) -> Option { + if Self::can_cast(syntax.kind()) { + Some(Self { syntax }) + } else { + None + } + } + fn syntax(&self) -> &SyntaxNode { &self.syntax } +} impl AstNode for Path { fn can_cast(kind: SyntaxKind) -> bool { kind == PATH } fn cast(syntax: SyntaxNode) -> Option { @@ -3524,6 +3527,11 @@ impl std::fmt::Display for NameRef { std::fmt::Display::fmt(self.syntax(), f) } } +impl std::fmt::Display for Lifetime { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + std::fmt::Display::fmt(self.syntax(), f) + } +} impl std::fmt::Display for Path { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 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::{ SmolStr, SyntaxElement, SyntaxToken, T, }; +impl ast::Lifetime { + pub fn text(&self) -> &SmolStr { + text_of_first_token(self.syntax()) + } +} + impl ast::Name { pub fn text(&self) -> &SmolStr { text_of_first_token(self.syntax()) @@ -393,7 +399,7 @@ pub enum TypeBoundKind { /// for<'a> ... ForType(ast::ForType), /// 'a - Lifetime(SyntaxToken), + Lifetime(ast::Lifetime), } impl ast::TypeBound { @@ -402,7 +408,7 @@ impl ast::TypeBound { TypeBoundKind::PathType(path_type) } else if let Some(for_type) = support::children(self.syntax()).next() { TypeBoundKind::ForType(for_type) - } else if let Some(lifetime) = self.lifetime_token() { + } else if let Some(lifetime) = self.lifetime() { TypeBoundKind::Lifetime(lifetime) } else { unreachable!() @@ -440,7 +446,7 @@ impl ast::LifetimeParam { .children_with_tokens() .filter_map(|it| it.into_token()) .skip_while(|x| x.kind() != T![:]) - .filter(|it| it.kind() == T![lifetime]) + .filter(|it| it.kind() == T![lifetime_ident]) } } 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( rustc_lexer::TokenKind::RawIdent => IDENT, rustc_lexer::TokenKind::Literal { kind, .. } => return match_literal_kind(&kind), - rustc_lexer::TokenKind::Lifetime { starts_with_number: false } => LIFETIME, + rustc_lexer::TokenKind::Lifetime { starts_with_number: false } => LIFETIME_IDENT, rustc_lexer::TokenKind::Lifetime { starts_with_number: true } => { - return (LIFETIME, Some("Lifetime name cannot start with a number")) + return (LIFETIME_IDENT, Some("Lifetime name cannot start with a number")) } rustc_lexer::TokenKind::Semi => T![;], -- cgit v1.2.3