Node-ify lifetimes

This commit is contained in:
Lukas Wirth 2020-12-15 19:23:51 +01:00
parent d34611633b
commit dd496223f5
63 changed files with 420 additions and 274 deletions

View file

@ -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());

View file

@ -20,6 +20,15 @@ impl NameRef {
pub fn ident_token(&self) -> Option<SyntaxToken> { 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<SyntaxToken> {
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<SyntaxToken> {
support::token(&self.syntax, T![lifetime])
}
pub fn lifetime(&self) -> Option<Lifetime> { 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<SyntaxToken> { support::token(&self.syntax, T![&]) }
pub fn lifetime_token(&self) -> Option<SyntaxToken> {
support::token(&self.syntax, T![lifetime])
}
pub fn lifetime(&self) -> Option<Lifetime> { support::child(&self.syntax) }
pub fn mut_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![mut]) }
pub fn self_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![self]) }
pub fn colon_token(&self) -> Option<SyntaxToken> { 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<SyntaxToken> {
support::token(&self.syntax, T![lifetime])
}
pub fn lifetime(&self) -> Option<Lifetime> { 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<SyntaxToken> { support::token(&self.syntax, T![for]) }
pub fn generic_param_list(&self) -> Option<GenericParamList> { support::child(&self.syntax) }
pub fn lifetime_token(&self) -> Option<SyntaxToken> {
support::token(&self.syntax, T![lifetime])
}
pub fn lifetime(&self) -> Option<Lifetime> { support::child(&self.syntax) }
pub fn ty(&self) -> Option<Type> { 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<SyntaxToken> { support::token(&self.syntax, T![break]) }
pub fn lifetime_token(&self) -> Option<SyntaxToken> {
support::token(&self.syntax, T![lifetime])
}
pub fn lifetime(&self) -> Option<Lifetime> { support::child(&self.syntax) }
pub fn expr(&self) -> Option<Expr> { support::child(&self.syntax) }
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
@ -752,9 +751,7 @@ impl ContinueExpr {
pub fn continue_token(&self) -> Option<SyntaxToken> {
support::token(&self.syntax, T![continue])
}
pub fn lifetime_token(&self) -> Option<SyntaxToken> {
support::token(&self.syntax, T![lifetime])
}
pub fn lifetime(&self) -> Option<Lifetime> { 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<SyntaxToken> {
support::token(&self.syntax, T![lifetime])
}
pub fn lifetime(&self) -> Option<Lifetime> { support::child(&self.syntax) }
pub fn colon_token(&self) -> Option<SyntaxToken> { 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<SyntaxToken> { support::token(&self.syntax, T![&]) }
pub fn lifetime_token(&self) -> Option<SyntaxToken> {
support::token(&self.syntax, T![lifetime])
}
pub fn lifetime(&self) -> Option<Lifetime> { support::child(&self.syntax) }
pub fn mut_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![mut]) }
pub fn ty(&self) -> Option<Type> { support::child(&self.syntax) }
}
@ -1129,9 +1123,7 @@ pub struct TypeBound {
pub(crate) syntax: SyntaxNode,
}
impl TypeBound {
pub fn lifetime_token(&self) -> Option<SyntaxToken> {
support::token(&self.syntax, T![lifetime])
}
pub fn lifetime(&self) -> Option<Lifetime> { support::child(&self.syntax) }
pub fn question_mark_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![?]) }
pub fn ty(&self) -> Option<Type> { 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<Self> {
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<Self> {
@ -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)

View file

@ -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])
}
}

View file

@ -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![;],