Skip to content

Commit 6f5b530

Browse files
bors[bot]matklad
andauthored
Merge #5611
5611: Finalize attribute grammar r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
2 parents e28ea81 + fbe60a2 commit 6f5b530

File tree

6 files changed

+48
-100
lines changed

6 files changed

+48
-100
lines changed

crates/ra_assists/src/handlers/add_custom_impl.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ use crate::{
2929
// }
3030
// ```
3131
pub(crate) fn add_custom_impl(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
32-
let input = ctx.find_node_at_offset::<ast::AttrInput>()?;
33-
let attr = input.syntax().parent().and_then(ast::Attr::cast)?;
32+
let attr = ctx.find_node_at_offset::<ast::Attr>()?;
33+
let input = attr.token_tree()?;
3434

3535
let attr_name = attr
3636
.syntax()

crates/ra_hir_def/src/attr.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -151,18 +151,15 @@ pub enum AttrInput {
151151
impl Attr {
152152
fn from_src(ast: ast::Attr, hygiene: &Hygiene) -> Option<Attr> {
153153
let path = ModPath::from_src(ast.path()?, hygiene)?;
154-
let input = match ast.input() {
155-
None => None,
156-
Some(ast::AttrInput::Literal(lit)) => {
157-
// FIXME: escape? raw string?
158-
let value = lit.syntax().first_token()?.text().trim_matches('"').into();
159-
Some(AttrInput::Literal(value))
160-
}
161-
Some(ast::AttrInput::TokenTree(tt)) => {
162-
Some(AttrInput::TokenTree(ast_to_token_tree(&tt)?.0))
163-
}
154+
let input = if let Some(lit) = ast.literal() {
155+
// FIXME: escape? raw string?
156+
let value = lit.syntax().first_token()?.text().trim_matches('"').into();
157+
Some(AttrInput::Literal(value))
158+
} else if let Some(tt) = ast.token_tree() {
159+
Some(AttrInput::TokenTree(ast_to_token_tree(&tt)?.0))
160+
} else {
161+
None
164162
};
165-
166163
Some(Attr { path, input })
167164
}
168165
}

crates/ra_ide/src/completion/complete_attribute.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,18 @@ use crate::completion::{
1313

1414
pub(super) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
1515
let attribute = ctx.attribute_under_caret.as_ref()?;
16-
match (attribute.path(), attribute.input()) {
17-
(Some(path), Some(ast::AttrInput::TokenTree(token_tree)))
18-
if path.to_string() == "derive" =>
19-
{
16+
match (attribute.path(), attribute.token_tree()) {
17+
(Some(path), Some(token_tree)) if path.to_string() == "derive" => {
2018
complete_derive(acc, ctx, token_tree)
2119
}
22-
(Some(path), Some(ast::AttrInput::TokenTree(token_tree)))
20+
(Some(path), Some(token_tree))
2321
if ["allow", "warn", "deny", "forbid"]
2422
.iter()
2523
.any(|lint_level| lint_level == &path.to_string()) =>
2624
{
2725
complete_lint(acc, ctx, token_tree)
2826
}
29-
(_, Some(ast::AttrInput::TokenTree(_token_tree))) => {}
27+
(_, Some(_token_tree)) => {}
3028
_ => complete_attribute_start(acc, ctx, attribute),
3129
}
3230
Some(())

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

Lines changed: 20 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ impl Attr {
2424
pub fn l_brack_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['[']) }
2525
pub fn path(&self) -> Option<Path> { support::child(&self.syntax) }
2626
pub fn eq_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![=]) }
27-
pub fn input(&self) -> Option<AttrInput> { support::child(&self.syntax) }
27+
pub fn literal(&self) -> Option<Literal> { support::child(&self.syntax) }
28+
pub fn token_tree(&self) -> Option<TokenTree> { support::child(&self.syntax) }
2829
pub fn r_brack_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![']']) }
2930
}
3031
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
@@ -321,9 +322,9 @@ pub struct ParamList {
321322
}
322323
impl ParamList {
323324
pub fn l_paren_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['(']) }
324-
pub fn params(&self) -> AstChildren<Param> { support::children(&self.syntax) }
325325
pub fn self_param(&self) -> Option<SelfParam> { support::child(&self.syntax) }
326326
pub fn comma_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![,]) }
327+
pub fn params(&self) -> AstChildren<Param> { support::children(&self.syntax) }
327328
pub fn r_paren_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![')']) }
328329
}
329330
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
@@ -356,17 +357,6 @@ impl BlockExpr {
356357
pub fn r_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['}']) }
357358
}
358359
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
359-
pub struct Param {
360-
pub(crate) syntax: SyntaxNode,
361-
}
362-
impl ast::AttrsOwner for Param {}
363-
impl ast::TypeAscriptionOwner for Param {}
364-
impl Param {
365-
pub fn pat(&self) -> Option<Pat> { support::child(&self.syntax) }
366-
pub fn colon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![:]) }
367-
pub fn dotdotdot_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![...]) }
368-
}
369-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
370360
pub struct SelfParam {
371361
pub(crate) syntax: SyntaxNode,
372362
}
@@ -382,6 +372,17 @@ impl SelfParam {
382372
pub fn colon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![:]) }
383373
}
384374
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
375+
pub struct Param {
376+
pub(crate) syntax: SyntaxNode,
377+
}
378+
impl ast::AttrsOwner for Param {}
379+
impl ast::TypeAscriptionOwner for Param {}
380+
impl Param {
381+
pub fn pat(&self) -> Option<Pat> { support::child(&self.syntax) }
382+
pub fn colon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![:]) }
383+
pub fn dotdotdot_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![...]) }
384+
}
385+
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
385386
pub struct TypeBoundList {
386387
pub(crate) syntax: SyntaxNode,
387388
}
@@ -1378,11 +1379,6 @@ pub enum GenericParam {
13781379
}
13791380
impl ast::AttrsOwner for GenericParam {}
13801381
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1381-
pub enum AttrInput {
1382-
Literal(Literal),
1383-
TokenTree(TokenTree),
1384-
}
1385-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
13861382
pub enum Stmt {
13871383
LetStmt(LetStmt),
13881384
ExprStmt(ExprStmt),
@@ -1728,8 +1724,8 @@ impl AstNode for BlockExpr {
17281724
}
17291725
fn syntax(&self) -> &SyntaxNode { &self.syntax }
17301726
}
1731-
impl AstNode for Param {
1732-
fn can_cast(kind: SyntaxKind) -> bool { kind == PARAM }
1727+
impl AstNode for SelfParam {
1728+
fn can_cast(kind: SyntaxKind) -> bool { kind == SELF_PARAM }
17331729
fn cast(syntax: SyntaxNode) -> Option<Self> {
17341730
if Self::can_cast(syntax.kind()) {
17351731
Some(Self { syntax })
@@ -1739,8 +1735,8 @@ impl AstNode for Param {
17391735
}
17401736
fn syntax(&self) -> &SyntaxNode { &self.syntax }
17411737
}
1742-
impl AstNode for SelfParam {
1743-
fn can_cast(kind: SyntaxKind) -> bool { kind == SELF_PARAM }
1738+
impl AstNode for Param {
1739+
fn can_cast(kind: SyntaxKind) -> bool { kind == PARAM }
17441740
fn cast(syntax: SyntaxNode) -> Option<Self> {
17451741
if Self::can_cast(syntax.kind()) {
17461742
Some(Self { syntax })
@@ -3342,34 +3338,6 @@ impl AstNode for GenericParam {
33423338
}
33433339
}
33443340
}
3345-
impl From<Literal> for AttrInput {
3346-
fn from(node: Literal) -> AttrInput { AttrInput::Literal(node) }
3347-
}
3348-
impl From<TokenTree> for AttrInput {
3349-
fn from(node: TokenTree) -> AttrInput { AttrInput::TokenTree(node) }
3350-
}
3351-
impl AstNode for AttrInput {
3352-
fn can_cast(kind: SyntaxKind) -> bool {
3353-
match kind {
3354-
LITERAL | TOKEN_TREE => true,
3355-
_ => false,
3356-
}
3357-
}
3358-
fn cast(syntax: SyntaxNode) -> Option<Self> {
3359-
let res = match syntax.kind() {
3360-
LITERAL => AttrInput::Literal(Literal { syntax }),
3361-
TOKEN_TREE => AttrInput::TokenTree(TokenTree { syntax }),
3362-
_ => return None,
3363-
};
3364-
Some(res)
3365-
}
3366-
fn syntax(&self) -> &SyntaxNode {
3367-
match self {
3368-
AttrInput::Literal(it) => &it.syntax,
3369-
AttrInput::TokenTree(it) => &it.syntax,
3370-
}
3371-
}
3372-
}
33733341
impl From<LetStmt> for Stmt {
33743342
fn from(node: LetStmt) -> Stmt { Stmt::LetStmt(node) }
33753343
}
@@ -3471,11 +3439,6 @@ impl std::fmt::Display for GenericParam {
34713439
std::fmt::Display::fmt(self.syntax(), f)
34723440
}
34733441
}
3474-
impl std::fmt::Display for AttrInput {
3475-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
3476-
std::fmt::Display::fmt(self.syntax(), f)
3477-
}
3478-
}
34793442
impl std::fmt::Display for Stmt {
34803443
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34813444
std::fmt::Display::fmt(self.syntax(), f)
@@ -3636,12 +3599,12 @@ impl std::fmt::Display for BlockExpr {
36363599
std::fmt::Display::fmt(self.syntax(), f)
36373600
}
36383601
}
3639-
impl std::fmt::Display for Param {
3602+
impl std::fmt::Display for SelfParam {
36403603
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
36413604
std::fmt::Display::fmt(self.syntax(), f)
36423605
}
36433606
}
3644-
impl std::fmt::Display for SelfParam {
3607+
impl std::fmt::Display for Param {
36453608
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
36463609
std::fmt::Display::fmt(self.syntax(), f)
36473610
}

crates/ra_syntax/src/ast/node_ext.rs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use itertools::Itertools;
77
use ra_parser::SyntaxKind;
88

99
use crate::{
10-
ast::{self, support, AstNode, AttrInput, NameOwner, SyntaxNode},
10+
ast::{self, support, AstNode, NameOwner, SyntaxNode},
1111
SmolStr, SyntaxElement, SyntaxToken, T,
1212
};
1313

@@ -39,29 +39,23 @@ pub enum AttrKind {
3939

4040
impl ast::Attr {
4141
pub fn as_simple_atom(&self) -> Option<SmolStr> {
42-
match self.input() {
43-
None => self.simple_name(),
44-
Some(_) => None,
42+
if self.eq_token().is_some() || self.token_tree().is_some() {
43+
return None;
4544
}
45+
self.simple_name()
4646
}
4747

4848
pub fn as_simple_call(&self) -> Option<(SmolStr, ast::TokenTree)> {
49-
match self.input() {
50-
Some(AttrInput::TokenTree(tt)) => Some((self.simple_name()?, tt)),
51-
_ => None,
52-
}
49+
let tt = self.token_tree()?;
50+
Some((self.simple_name()?, tt))
5351
}
5452

5553
pub fn as_simple_key_value(&self) -> Option<(SmolStr, SmolStr)> {
56-
match self.input() {
57-
Some(AttrInput::Literal(lit)) => {
58-
let key = self.simple_name()?;
59-
// FIXME: escape? raw string?
60-
let value = lit.syntax().first_token()?.text().trim_matches('"').into();
61-
Some((key, value))
62-
}
63-
_ => None,
64-
}
54+
let lit = self.literal()?;
55+
let key = self.simple_name()?;
56+
// FIXME: escape? raw string?
57+
let value = lit.syntax().first_token()?.text().trim_matches('"').into();
58+
Some((key, value))
6559
}
6660

6761
pub fn simple_name(&self) -> Option<SmolStr> {

xtask/src/codegen/rust.ungram

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,8 @@ Abi =
5454

5555
ParamList =
5656
'('(
57-
(Param (',' Param)* ','?)?
58-
| SelfParam ','?
59-
| SelfParam ',' (Param (',' Param)* ','?)
57+
SelfParam
58+
| (SelfParam ',')? (Param (',' Param)* ','?)?
6059
)')'
6160

6261
SelfParam =
@@ -182,10 +181,7 @@ Visibility =
182181
')')?
183182

184183
Attr =
185-
'#' '!'? '[' Path ('=' input:AttrInput)? ']'
186-
187-
AttrInput =
188-
Literal | TokenTree
184+
'#' '!'? '[' Path ('=' Literal | TokenTree)? ']'
189185

190186
ParenType =
191187
'(' TypeRef ')'

0 commit comments

Comments
 (0)