Skip to content

Commit e28ea81

Browse files
bors[bot]matklad
andauthored
Merge #5609
5609: Attrs & Vis r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
2 parents bc8610e + 01d6c38 commit e28ea81

File tree

2 files changed

+90
-115
lines changed

2 files changed

+90
-115
lines changed

crates/ra_syntax/src/ast/generated/nodes.rs

Lines changed: 76 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,23 @@ impl ConstParam {
502502
pub fn default_val(&self) -> Option<Expr> { support::child(&self.syntax) }
503503
}
504504
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
505+
pub struct Literal {
506+
pub(crate) syntax: SyntaxNode,
507+
}
508+
impl Literal {}
509+
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
510+
pub struct TokenTree {
511+
pub(crate) syntax: SyntaxNode,
512+
}
513+
impl TokenTree {
514+
pub fn l_paren_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['(']) }
515+
pub fn r_paren_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![')']) }
516+
pub fn l_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['{']) }
517+
pub fn r_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['}']) }
518+
pub fn l_brack_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['[']) }
519+
pub fn r_brack_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![']']) }
520+
}
521+
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
505522
pub struct ParenType {
506523
pub(crate) syntax: SyntaxNode,
507524
}
@@ -908,11 +925,6 @@ pub struct BinExpr {
908925
impl ast::AttrsOwner for BinExpr {}
909926
impl BinExpr {}
910927
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
911-
pub struct Literal {
912-
pub(crate) syntax: SyntaxNode,
913-
}
914-
impl Literal {}
915-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
916928
pub struct MatchExpr {
917929
pub(crate) syntax: SyntaxNode,
918930
}
@@ -1131,18 +1143,6 @@ impl TuplePat {
11311143
pub fn r_paren_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![')']) }
11321144
}
11331145
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1134-
pub struct TokenTree {
1135-
pub(crate) syntax: SyntaxNode,
1136-
}
1137-
impl TokenTree {
1138-
pub fn l_paren_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['(']) }
1139-
pub fn r_paren_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![')']) }
1140-
pub fn l_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['{']) }
1141-
pub fn r_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['}']) }
1142-
pub fn l_brack_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['[']) }
1143-
pub fn r_brack_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![']']) }
1144-
}
1145-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
11461146
pub struct MacroDef {
11471147
pub(crate) syntax: SyntaxNode,
11481148
}
@@ -1263,16 +1263,6 @@ impl ConstArg {
12631263
pub fn block_expr(&self) -> Option<BlockExpr> { support::child(&self.syntax) }
12641264
}
12651265
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1266-
pub struct MetaItem {
1267-
pub(crate) syntax: SyntaxNode,
1268-
}
1269-
impl MetaItem {
1270-
pub fn path(&self) -> Option<Path> { support::child(&self.syntax) }
1271-
pub fn eq_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![=]) }
1272-
pub fn attr_input(&self) -> Option<AttrInput> { support::child(&self.syntax) }
1273-
pub fn nested_meta_items(&self) -> AstChildren<MetaItem> { support::children(&self.syntax) }
1274-
}
1275-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
12761266
pub enum Item {
12771267
Const(Const),
12781268
Enum(Enum),
@@ -1388,17 +1378,17 @@ pub enum GenericParam {
13881378
}
13891379
impl ast::AttrsOwner for GenericParam {}
13901380
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1381+
pub enum AttrInput {
1382+
Literal(Literal),
1383+
TokenTree(TokenTree),
1384+
}
1385+
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
13911386
pub enum Stmt {
13921387
LetStmt(LetStmt),
13931388
ExprStmt(ExprStmt),
13941389
}
13951390
impl ast::AttrsOwner for Stmt {}
13961391
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1397-
pub enum AttrInput {
1398-
Literal(Literal),
1399-
TokenTree(TokenTree),
1400-
}
1401-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
14021392
pub enum AdtDef {
14031393
Struct(Struct),
14041394
Enum(Enum),
@@ -1892,6 +1882,28 @@ impl AstNode for ConstParam {
18921882
}
18931883
fn syntax(&self) -> &SyntaxNode { &self.syntax }
18941884
}
1885+
impl AstNode for Literal {
1886+
fn can_cast(kind: SyntaxKind) -> bool { kind == LITERAL }
1887+
fn cast(syntax: SyntaxNode) -> Option<Self> {
1888+
if Self::can_cast(syntax.kind()) {
1889+
Some(Self { syntax })
1890+
} else {
1891+
None
1892+
}
1893+
}
1894+
fn syntax(&self) -> &SyntaxNode { &self.syntax }
1895+
}
1896+
impl AstNode for TokenTree {
1897+
fn can_cast(kind: SyntaxKind) -> bool { kind == TOKEN_TREE }
1898+
fn cast(syntax: SyntaxNode) -> Option<Self> {
1899+
if Self::can_cast(syntax.kind()) {
1900+
Some(Self { syntax })
1901+
} else {
1902+
None
1903+
}
1904+
}
1905+
fn syntax(&self) -> &SyntaxNode { &self.syntax }
1906+
}
18951907
impl AstNode for ParenType {
18961908
fn can_cast(kind: SyntaxKind) -> bool { kind == PAREN_TYPE }
18971909
fn cast(syntax: SyntaxNode) -> Option<Self> {
@@ -2354,17 +2366,6 @@ impl AstNode for BinExpr {
23542366
}
23552367
fn syntax(&self) -> &SyntaxNode { &self.syntax }
23562368
}
2357-
impl AstNode for Literal {
2358-
fn can_cast(kind: SyntaxKind) -> bool { kind == LITERAL }
2359-
fn cast(syntax: SyntaxNode) -> Option<Self> {
2360-
if Self::can_cast(syntax.kind()) {
2361-
Some(Self { syntax })
2362-
} else {
2363-
None
2364-
}
2365-
}
2366-
fn syntax(&self) -> &SyntaxNode { &self.syntax }
2367-
}
23682369
impl AstNode for MatchExpr {
23692370
fn can_cast(kind: SyntaxKind) -> bool { kind == MATCH_EXPR }
23702371
fn cast(syntax: SyntaxNode) -> Option<Self> {
@@ -2629,17 +2630,6 @@ impl AstNode for TuplePat {
26292630
}
26302631
fn syntax(&self) -> &SyntaxNode { &self.syntax }
26312632
}
2632-
impl AstNode for TokenTree {
2633-
fn can_cast(kind: SyntaxKind) -> bool { kind == TOKEN_TREE }
2634-
fn cast(syntax: SyntaxNode) -> Option<Self> {
2635-
if Self::can_cast(syntax.kind()) {
2636-
Some(Self { syntax })
2637-
} else {
2638-
None
2639-
}
2640-
}
2641-
fn syntax(&self) -> &SyntaxNode { &self.syntax }
2642-
}
26432633
impl AstNode for MacroDef {
26442634
fn can_cast(kind: SyntaxKind) -> bool { kind == MACRO_DEF }
26452635
fn cast(syntax: SyntaxNode) -> Option<Self> {
@@ -2772,17 +2762,6 @@ impl AstNode for ConstArg {
27722762
}
27732763
fn syntax(&self) -> &SyntaxNode { &self.syntax }
27742764
}
2775-
impl AstNode for MetaItem {
2776-
fn can_cast(kind: SyntaxKind) -> bool { kind == META_ITEM }
2777-
fn cast(syntax: SyntaxNode) -> Option<Self> {
2778-
if Self::can_cast(syntax.kind()) {
2779-
Some(Self { syntax })
2780-
} else {
2781-
None
2782-
}
2783-
}
2784-
fn syntax(&self) -> &SyntaxNode { &self.syntax }
2785-
}
27862765
impl From<Const> for Item {
27872766
fn from(node: Const) -> Item { Item::Const(node) }
27882767
}
@@ -3363,59 +3342,59 @@ impl AstNode for GenericParam {
33633342
}
33643343
}
33653344
}
3366-
impl From<LetStmt> for Stmt {
3367-
fn from(node: LetStmt) -> Stmt { Stmt::LetStmt(node) }
3345+
impl From<Literal> for AttrInput {
3346+
fn from(node: Literal) -> AttrInput { AttrInput::Literal(node) }
33683347
}
3369-
impl From<ExprStmt> for Stmt {
3370-
fn from(node: ExprStmt) -> Stmt { Stmt::ExprStmt(node) }
3348+
impl From<TokenTree> for AttrInput {
3349+
fn from(node: TokenTree) -> AttrInput { AttrInput::TokenTree(node) }
33713350
}
3372-
impl AstNode for Stmt {
3351+
impl AstNode for AttrInput {
33733352
fn can_cast(kind: SyntaxKind) -> bool {
33743353
match kind {
3375-
LET_STMT | EXPR_STMT => true,
3354+
LITERAL | TOKEN_TREE => true,
33763355
_ => false,
33773356
}
33783357
}
33793358
fn cast(syntax: SyntaxNode) -> Option<Self> {
33803359
let res = match syntax.kind() {
3381-
LET_STMT => Stmt::LetStmt(LetStmt { syntax }),
3382-
EXPR_STMT => Stmt::ExprStmt(ExprStmt { syntax }),
3360+
LITERAL => AttrInput::Literal(Literal { syntax }),
3361+
TOKEN_TREE => AttrInput::TokenTree(TokenTree { syntax }),
33833362
_ => return None,
33843363
};
33853364
Some(res)
33863365
}
33873366
fn syntax(&self) -> &SyntaxNode {
33883367
match self {
3389-
Stmt::LetStmt(it) => &it.syntax,
3390-
Stmt::ExprStmt(it) => &it.syntax,
3368+
AttrInput::Literal(it) => &it.syntax,
3369+
AttrInput::TokenTree(it) => &it.syntax,
33913370
}
33923371
}
33933372
}
3394-
impl From<Literal> for AttrInput {
3395-
fn from(node: Literal) -> AttrInput { AttrInput::Literal(node) }
3373+
impl From<LetStmt> for Stmt {
3374+
fn from(node: LetStmt) -> Stmt { Stmt::LetStmt(node) }
33963375
}
3397-
impl From<TokenTree> for AttrInput {
3398-
fn from(node: TokenTree) -> AttrInput { AttrInput::TokenTree(node) }
3376+
impl From<ExprStmt> for Stmt {
3377+
fn from(node: ExprStmt) -> Stmt { Stmt::ExprStmt(node) }
33993378
}
3400-
impl AstNode for AttrInput {
3379+
impl AstNode for Stmt {
34013380
fn can_cast(kind: SyntaxKind) -> bool {
34023381
match kind {
3403-
LITERAL | TOKEN_TREE => true,
3382+
LET_STMT | EXPR_STMT => true,
34043383
_ => false,
34053384
}
34063385
}
34073386
fn cast(syntax: SyntaxNode) -> Option<Self> {
34083387
let res = match syntax.kind() {
3409-
LITERAL => AttrInput::Literal(Literal { syntax }),
3410-
TOKEN_TREE => AttrInput::TokenTree(TokenTree { syntax }),
3388+
LET_STMT => Stmt::LetStmt(LetStmt { syntax }),
3389+
EXPR_STMT => Stmt::ExprStmt(ExprStmt { syntax }),
34113390
_ => return None,
34123391
};
34133392
Some(res)
34143393
}
34153394
fn syntax(&self) -> &SyntaxNode {
34163395
match self {
3417-
AttrInput::Literal(it) => &it.syntax,
3418-
AttrInput::TokenTree(it) => &it.syntax,
3396+
Stmt::LetStmt(it) => &it.syntax,
3397+
Stmt::ExprStmt(it) => &it.syntax,
34193398
}
34203399
}
34213400
}
@@ -3492,12 +3471,12 @@ impl std::fmt::Display for GenericParam {
34923471
std::fmt::Display::fmt(self.syntax(), f)
34933472
}
34943473
}
3495-
impl std::fmt::Display for Stmt {
3474+
impl std::fmt::Display for AttrInput {
34963475
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34973476
std::fmt::Display::fmt(self.syntax(), f)
34983477
}
34993478
}
3500-
impl std::fmt::Display for AttrInput {
3479+
impl std::fmt::Display for Stmt {
35013480
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35023481
std::fmt::Display::fmt(self.syntax(), f)
35033482
}
@@ -3727,6 +3706,16 @@ impl std::fmt::Display for ConstParam {
37273706
std::fmt::Display::fmt(self.syntax(), f)
37283707
}
37293708
}
3709+
impl std::fmt::Display for Literal {
3710+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
3711+
std::fmt::Display::fmt(self.syntax(), f)
3712+
}
3713+
}
3714+
impl std::fmt::Display for TokenTree {
3715+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
3716+
std::fmt::Display::fmt(self.syntax(), f)
3717+
}
3718+
}
37303719
impl std::fmt::Display for ParenType {
37313720
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37323721
std::fmt::Display::fmt(self.syntax(), f)
@@ -3937,11 +3926,6 @@ impl std::fmt::Display for BinExpr {
39373926
std::fmt::Display::fmt(self.syntax(), f)
39383927
}
39393928
}
3940-
impl std::fmt::Display for Literal {
3941-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
3942-
std::fmt::Display::fmt(self.syntax(), f)
3943-
}
3944-
}
39453929
impl std::fmt::Display for MatchExpr {
39463930
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39473931
std::fmt::Display::fmt(self.syntax(), f)
@@ -4062,11 +4046,6 @@ impl std::fmt::Display for TuplePat {
40624046
std::fmt::Display::fmt(self.syntax(), f)
40634047
}
40644048
}
4065-
impl std::fmt::Display for TokenTree {
4066-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
4067-
std::fmt::Display::fmt(self.syntax(), f)
4068-
}
4069-
}
40704049
impl std::fmt::Display for MacroDef {
40714050
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40724051
std::fmt::Display::fmt(self.syntax(), f)
@@ -4127,8 +4106,3 @@ impl std::fmt::Display for ConstArg {
41274106
std::fmt::Display::fmt(self.syntax(), f)
41284107
}
41294108
}
4130-
impl std::fmt::Display for MetaItem {
4131-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
4132-
std::fmt::Display::fmt(self.syntax(), f)
4133-
}
4134-
}

xtask/src/codegen/rust.ungram

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,20 @@ ConstParam =
173173
LifetimeParam =
174174
Attr* 'lifetime'
175175

176+
Visibility =
177+
'pub' ('('
178+
'super'
179+
| 'self'
180+
| 'crate'
181+
| 'in' Path
182+
')')?
183+
184+
Attr =
185+
'#' '!'? '[' Path ('=' input:AttrInput)? ']'
186+
187+
AttrInput =
188+
Literal | TokenTree
189+
176190
ParenType =
177191
'(' TypeRef ')'
178192

@@ -391,9 +405,6 @@ TupleStructPat =
391405
TuplePat =
392406
'(' args:Pat* ')'
393407

394-
Visibility =
395-
'pub' ('(' 'super' | 'self' | 'crate' | 'in' Path ')')?
396-
397408
Name =
398409
'ident'
399410

@@ -416,9 +427,6 @@ MacroStmts =
416427
statements:Stmt*
417428
Expr?
418429

419-
Attr =
420-
'#' '!'? '[' Path ('=' input:AttrInput)? ']'
421-
422430
TypeBound =
423431
'lifetime' | 'const'? TypeRef
424432

@@ -465,9 +473,6 @@ LifetimeArg =
465473
ConstArg =
466474
Literal | BlockExpr BlockExpr
467475

468-
MetaItem =
469-
Path '=' AttrInput nested_meta_items:MetaItem*
470-
471476
AdtDef =
472477
Struct
473478
| Enum
@@ -488,10 +493,6 @@ TypeRef =
488493
| ImplTraitType
489494
| DynTraitType
490495

491-
AttrInput =
492-
Literal
493-
| TokenTree
494-
495496
Stmt =
496497
LetStmt
497498
| ExprStmt

0 commit comments

Comments
 (0)