From 3dce34aaf83c67836c94a526cb90eba8f36e3985 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 18:52:02 +0200 Subject: [PATCH] Introduce GenericParam --- crates/ra_parser/src/syntax_kind/generated.rs | 1 + crates/ra_syntax/src/ast/generated/nodes.rs | 213 +++++++++++------- crates/ra_syntax/src/ast/node_ext.rs | 21 ++ xtask/src/ast_src.rs | 1 + xtask/src/codegen/rust.ungram | 37 +-- 5 files changed, 170 insertions(+), 103 deletions(-) diff --git a/crates/ra_parser/src/syntax_kind/generated.rs b/crates/ra_parser/src/syntax_kind/generated.rs index d1b22f08a9..be4da67bcb 100644 --- a/crates/ra_parser/src/syntax_kind/generated.rs +++ b/crates/ra_parser/src/syntax_kind/generated.rs @@ -231,6 +231,7 @@ pub enum SyntaxKind { LET_STMT, EXPR_STMT, GENERIC_PARAM_LIST, + GENERIC_PARAM, LIFETIME_PARAM, TYPE_PARAM, CONST_PARAM, diff --git a/crates/ra_syntax/src/ast/generated/nodes.rs b/crates/ra_syntax/src/ast/generated/nodes.rs index 9345a5e8f4..e3b3f44ccf 100644 --- a/crates/ra_syntax/src/ast/generated/nodes.rs +++ b/crates/ra_syntax/src/ast/generated/nodes.rs @@ -312,9 +312,7 @@ pub struct GenericParamList { } impl GenericParamList { pub fn l_angle_token(&self) -> Option { support::token(&self.syntax, T![<]) } - pub fn type_params(&self) -> AstChildren { support::children(&self.syntax) } - pub fn lifetime_params(&self) -> AstChildren { support::children(&self.syntax) } - pub fn const_params(&self) -> AstChildren { support::children(&self.syntax) } + pub fn generic_params(&self) -> AstChildren { support::children(&self.syntax) } pub fn r_angle_token(&self) -> Option { support::token(&self.syntax, T![>]) } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] @@ -470,6 +468,40 @@ impl ExternItemList { pub fn r_curly_token(&self) -> Option { 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 { + 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 { support::token(&self.syntax, T![=]) } + pub fn default_type(&self) -> Option { 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 { support::token(&self.syntax, T![const]) } + pub fn colon_token(&self) -> Option { support::token(&self.syntax, T![:]) } + pub fn eq_token(&self) -> Option { support::token(&self.syntax, T![=]) } + pub fn default_val(&self) -> Option { support::child(&self.syntax) } +} +#[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct ParenType { pub(crate) syntax: SyntaxNode, } @@ -1133,40 +1165,6 @@ impl MacroStmts { pub fn expr(&self) -> Option { 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 { support::token(&self.syntax, T![=]) } - pub fn default_type(&self) -> Option { 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 { - 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 { support::token(&self.syntax, T![const]) } - pub fn colon_token(&self) -> Option { support::token(&self.syntax, T![:]) } - pub fn eq_token(&self) -> Option { support::token(&self.syntax, T![=]) } - pub fn default_val(&self) -> Option { 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 @@ impl AstNode for ExternItemList { } fn syntax(&self) -> &SyntaxNode { &self.syntax } } +impl AstNode for LifetimeParam { + fn can_cast(kind: SyntaxKind) -> bool { kind == LIFETIME_PARAM } + 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 TypeParam { + fn can_cast(kind: SyntaxKind) -> bool { kind == TYPE_PARAM } + 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 ConstParam { + fn can_cast(kind: SyntaxKind) -> bool { kind == CONST_PARAM } + 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 ParenType { fn can_cast(kind: SyntaxKind) -> bool { kind == PAREN_TYPE } fn cast(syntax: SyntaxNode) -> Option { @@ -2635,39 +2673,6 @@ impl AstNode for MacroStmts { } fn syntax(&self) -> &SyntaxNode { &self.syntax } } -impl AstNode for TypeParam { - fn can_cast(kind: SyntaxKind) -> bool { kind == TYPE_PARAM } - 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 LifetimeParam { - fn can_cast(kind: SyntaxKind) -> bool { kind == LIFETIME_PARAM } - 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 ConstParam { - fn can_cast(kind: SyntaxKind) -> bool { kind == CONST_PARAM } - 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 TypeBound { fn can_cast(kind: SyntaxKind) -> bool { kind == TYPE_BOUND } fn cast(syntax: SyntaxNode) -> Option { @@ -3325,6 +3330,39 @@ impl AstNode for ExternItem { } } } +impl From for GenericParam { + fn from(node: LifetimeParam) -> GenericParam { GenericParam::LifetimeParam(node) } +} +impl From for GenericParam { + fn from(node: TypeParam) -> GenericParam { GenericParam::TypeParam(node) } +} +impl From 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 { + 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 for Stmt { fn from(node: LetStmt) -> Stmt { Stmt::LetStmt(node) } } @@ -3449,6 +3487,11 @@ impl std::fmt::Display for ExternItem { 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 @@ impl std::fmt::Display for ExternItemList { 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 @@ impl std::fmt::Display for MacroStmts { 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) diff --git a/crates/ra_syntax/src/ast/node_ext.rs b/crates/ra_syntax/src/ast/node_ext.rs index 313f522261..d2ee9586d2 100644 --- a/crates/ra_syntax/src/ast/node_ext.rs +++ b/crates/ra_syntax/src/ast/node_ext.rs @@ -473,6 +473,27 @@ impl ast::TokenTree { } } +impl ast::GenericParamList { + pub fn lifetime_params(&self) -> impl Iterator { + 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 { + 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 { + 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 {} diff --git a/xtask/src/ast_src.rs b/xtask/src/ast_src.rs index c7087a14ec..114898e383 100644 --- a/xtask/src/ast_src.rs +++ b/xtask/src/ast_src.rs @@ -204,6 +204,7 @@ pub(crate) const KINDS_SRC: KindsSrc = KindsSrc { "LET_STMT", "EXPR_STMT", "GENERIC_PARAM_LIST", + "GENERIC_PARAM", "LIFETIME_PARAM", "TYPE_PARAM", "CONST_PARAM", diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index cdd3adf457..4025f7f96c 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -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