Introduce GenericParam

This commit is contained in:
Aleksey Kladov 2020-07-30 18:52:02 +02:00
parent 917c89c103
commit 3dce34aaf8
5 changed files with 170 additions and 103 deletions

View File

@ -231,6 +231,7 @@ pub enum SyntaxKind {
LET_STMT,
EXPR_STMT,
GENERIC_PARAM_LIST,
GENERIC_PARAM,
LIFETIME_PARAM,
TYPE_PARAM,
CONST_PARAM,

View File

@ -312,9 +312,7 @@ pub struct GenericParamList {
}
impl GenericParamList {
pub fn l_angle_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![<]) }
pub fn type_params(&self) -> AstChildren<TypeParam> { support::children(&self.syntax) }
pub fn lifetime_params(&self) -> AstChildren<LifetimeParam> { support::children(&self.syntax) }
pub fn const_params(&self) -> AstChildren<ConstParam> { support::children(&self.syntax) }
pub fn generic_params(&self) -> AstChildren<GenericParam> { support::children(&self.syntax) }
pub fn r_angle_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![>]) }
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
@ -470,6 +468,40 @@ pub fn extern_items(&self) -> AstChildren<ExternItem> { support::children(&self.
pub fn r_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['}']) }
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct LifetimeParam {
pub(crate) syntax: SyntaxNode,
}
impl ast::AttrsOwner for LifetimeParam {}
impl LifetimeParam {
pub fn lifetime_token(&self) -> Option<SyntaxToken> {
support::token(&self.syntax, T![lifetime])
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct TypeParam {
pub(crate) syntax: SyntaxNode,
}
impl ast::AttrsOwner for TypeParam {}
impl ast::NameOwner for TypeParam {}
impl ast::TypeBoundsOwner for TypeParam {}
impl TypeParam {
pub fn eq_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![=]) }
pub fn default_type(&self) -> Option<TypeRef> { support::child(&self.syntax) }
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ConstParam {
pub(crate) syntax: SyntaxNode,
}
impl ast::AttrsOwner for ConstParam {}
impl ast::NameOwner for ConstParam {}
impl ast::TypeAscriptionOwner for ConstParam {}
impl ConstParam {
pub fn const_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![const]) }
pub fn colon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![:]) }
pub fn eq_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![=]) }
pub fn default_val(&self) -> Option<Expr> { support::child(&self.syntax) }
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ParenType {
pub(crate) syntax: SyntaxNode,
}
@ -1133,40 +1165,6 @@ pub fn statements(&self) -> AstChildren<Stmt> { support::children(&self.syntax)
pub fn expr(&self) -> Option<Expr> { support::child(&self.syntax) }
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct TypeParam {
pub(crate) syntax: SyntaxNode,
}
impl ast::AttrsOwner for TypeParam {}
impl ast::NameOwner for TypeParam {}
impl ast::TypeBoundsOwner for TypeParam {}
impl TypeParam {
pub fn eq_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![=]) }
pub fn default_type(&self) -> Option<TypeRef> { support::child(&self.syntax) }
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct LifetimeParam {
pub(crate) syntax: SyntaxNode,
}
impl ast::AttrsOwner for LifetimeParam {}
impl LifetimeParam {
pub fn lifetime_token(&self) -> Option<SyntaxToken> {
support::token(&self.syntax, T![lifetime])
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ConstParam {
pub(crate) syntax: SyntaxNode,
}
impl ast::AttrsOwner for ConstParam {}
impl ast::NameOwner for ConstParam {}
impl ast::TypeAscriptionOwner for ConstParam {}
impl ConstParam {
pub fn const_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![const]) }
pub fn colon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![:]) }
pub fn eq_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![=]) }
pub fn default_val(&self) -> Option<Expr> { support::child(&self.syntax) }
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct TypeBound {
pub(crate) syntax: SyntaxNode,
}
@ -1383,6 +1381,13 @@ pub enum ExternItem {
impl ast::AttrsOwner for ExternItem {}
impl ast::NameOwner for ExternItem {}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum GenericParam {
LifetimeParam(LifetimeParam),
TypeParam(TypeParam),
ConstParam(ConstParam),
}
impl ast::AttrsOwner for GenericParam {}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Stmt {
LetStmt(LetStmt),
ExprStmt(ExprStmt),
@ -1854,6 +1859,39 @@ fn cast(syntax: SyntaxNode) -> Option<Self> {
}
fn syntax(&self) -> &SyntaxNode { &self.syntax }
}
impl AstNode for LifetimeParam {
fn can_cast(kind: SyntaxKind) -> bool { kind == LIFETIME_PARAM }
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 TypeParam {
fn can_cast(kind: SyntaxKind) -> bool { kind == TYPE_PARAM }
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 ConstParam {
fn can_cast(kind: SyntaxKind) -> bool { kind == CONST_PARAM }
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 ParenType {
fn can_cast(kind: SyntaxKind) -> bool { kind == PAREN_TYPE }
fn cast(syntax: SyntaxNode) -> Option<Self> {
@ -2635,39 +2673,6 @@ fn cast(syntax: SyntaxNode) -> Option<Self> {
}
fn syntax(&self) -> &SyntaxNode { &self.syntax }
}
impl AstNode for TypeParam {
fn can_cast(kind: SyntaxKind) -> bool { kind == TYPE_PARAM }
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 LifetimeParam {
fn can_cast(kind: SyntaxKind) -> bool { kind == LIFETIME_PARAM }
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 ConstParam {
fn can_cast(kind: SyntaxKind) -> bool { kind == CONST_PARAM }
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 TypeBound {
fn can_cast(kind: SyntaxKind) -> bool { kind == TYPE_BOUND }
fn cast(syntax: SyntaxNode) -> Option<Self> {
@ -3325,6 +3330,39 @@ fn syntax(&self) -> &SyntaxNode {
}
}
}
impl From<LifetimeParam> for GenericParam {
fn from(node: LifetimeParam) -> GenericParam { GenericParam::LifetimeParam(node) }
}
impl From<TypeParam> for GenericParam {
fn from(node: TypeParam) -> GenericParam { GenericParam::TypeParam(node) }
}
impl From<ConstParam> for GenericParam {
fn from(node: ConstParam) -> GenericParam { GenericParam::ConstParam(node) }
}
impl AstNode for GenericParam {
fn can_cast(kind: SyntaxKind) -> bool {
match kind {
LIFETIME_PARAM | TYPE_PARAM | CONST_PARAM => true,
_ => false,
}
}
fn cast(syntax: SyntaxNode) -> Option<Self> {
let res = match syntax.kind() {
LIFETIME_PARAM => GenericParam::LifetimeParam(LifetimeParam { syntax }),
TYPE_PARAM => GenericParam::TypeParam(TypeParam { syntax }),
CONST_PARAM => GenericParam::ConstParam(ConstParam { syntax }),
_ => return None,
};
Some(res)
}
fn syntax(&self) -> &SyntaxNode {
match self {
GenericParam::LifetimeParam(it) => &it.syntax,
GenericParam::TypeParam(it) => &it.syntax,
GenericParam::ConstParam(it) => &it.syntax,
}
}
}
impl From<LetStmt> for Stmt {
fn from(node: LetStmt) -> Stmt { Stmt::LetStmt(node) }
}
@ -3449,6 +3487,11 @@ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(self.syntax(), f)
}
}
impl std::fmt::Display for GenericParam {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(self.syntax(), f)
}
}
impl std::fmt::Display for Stmt {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(self.syntax(), f)
@ -3669,6 +3712,21 @@ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(self.syntax(), f)
}
}
impl std::fmt::Display for LifetimeParam {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(self.syntax(), f)
}
}
impl std::fmt::Display for TypeParam {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(self.syntax(), f)
}
}
impl std::fmt::Display for ConstParam {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(self.syntax(), f)
}
}
impl std::fmt::Display for ParenType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(self.syntax(), f)
@ -4024,21 +4082,6 @@ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(self.syntax(), f)
}
}
impl std::fmt::Display for TypeParam {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(self.syntax(), f)
}
}
impl std::fmt::Display for LifetimeParam {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(self.syntax(), f)
}
}
impl std::fmt::Display for ConstParam {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(self.syntax(), f)
}
}
impl std::fmt::Display for TypeBound {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(self.syntax(), f)

View File

@ -473,6 +473,27 @@ pub fn right_delimiter_token(&self) -> Option<SyntaxToken> {
}
}
impl ast::GenericParamList {
pub fn lifetime_params(&self) -> impl Iterator<Item = ast::LifetimeParam> {
self.generic_params().filter_map(|param| match param {
ast::GenericParam::LifetimeParam(it) => Some(it),
ast::GenericParam::TypeParam(_) | ast::GenericParam::ConstParam(_) => None,
})
}
pub fn type_params(&self) -> impl Iterator<Item = ast::TypeParam> {
self.generic_params().filter_map(|param| match param {
ast::GenericParam::TypeParam(it) => Some(it),
ast::GenericParam::LifetimeParam(_) | ast::GenericParam::ConstParam(_) => None,
})
}
pub fn const_params(&self) -> impl Iterator<Item = ast::ConstParam> {
self.generic_params().filter_map(|param| match param {
ast::GenericParam::ConstParam(it) => Some(it),
ast::GenericParam::TypeParam(_) | ast::GenericParam::LifetimeParam(_) => None,
})
}
}
impl ast::DocCommentsOwner for ast::SourceFile {}
impl ast::DocCommentsOwner for ast::Fn {}
impl ast::DocCommentsOwner for ast::Struct {}

View File

@ -204,6 +204,7 @@ pub(crate) struct KindsSrc<'a> {
"LET_STMT",
"EXPR_STMT",
"GENERIC_PARAM_LIST",
"GENERIC_PARAM",
"LIFETIME_PARAM",
"TYPE_PARAM",
"CONST_PARAM",

View File

@ -154,6 +154,25 @@ ExternItemList =
ExternItem =
Fn | Static | MacroCall
GenericParamList =
'<' (GenericParam (',' GenericParam)* ','?)? '>'
GenericParam =
LifetimeParam
| TypeParam
| ConstParam
TypeParam =
Attr* Name (':' TypeBoundList?)?
('=' default_type:TypeRef)?
ConstParam =
Attr* 'const' Name ':' ascribed_type:TypeRef
('=' default_val:Expr)?
LifetimeParam =
Attr* 'lifetime'
ParenType =
'(' TypeRef ')'
@ -400,24 +419,6 @@ MacroStmts =
Attr =
'#' '!'? '[' Path ('=' input:AttrInput)? ']'
GenericParamList =
'<'
TypeParam*
LifetimeParam*
ConstParam*
'>'
TypeParam =
Attr* Name (':' TypeBoundList?)?
('=' default_type:TypeRef)?
ConstParam =
Attr* 'const' Name ':' ascribed_type:TypeRef
('=' default_val:Expr)?
LifetimeParam =
Attr* 'lifetime'
TypeBound =
'lifetime' | 'const'? TypeRef