Skip to content

Commit 7200b99

Browse files
bors[bot]Veykril
andauthored
Merge #8097
8097: Parse extended_key_value_attributes r=jonas-schievink a=Veykril Companion PR rust-analyzer/ungrammar#31 Co-authored-by: Lukas Wirth <[email protected]>
2 parents 98d29d4 + 4771a56 commit 7200b99

File tree

7 files changed

+32
-41
lines changed

7 files changed

+32
-41
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/hir_def/src/attr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ pub enum AttrInput {
458458
impl Attr {
459459
fn from_src(ast: ast::Attr, hygiene: &Hygiene, index: u32) -> Option<Attr> {
460460
let path = ModPath::from_src(ast.path()?, hygiene)?;
461-
let input = if let Some(lit) = ast.literal() {
461+
let input = if let Some(ast::Expr::Literal(lit)) = ast.expr() {
462462
let value = match lit.kind() {
463463
ast::LiteralKind::String(string) => string.value()?.into(),
464464
_ => lit.syntax().first_token()?.text().trim_matches('"').into(),

crates/ide/src/syntax_highlighting/inject.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,9 +295,9 @@ pub(super) fn doc_comment(
295295
}
296296

297297
fn find_doc_string_in_attr(attr: &hir::Attr, it: &ast::Attr) -> Option<ast::String> {
298-
match it.literal() {
298+
match it.expr() {
299299
// #[doc = lit]
300-
Some(lit) => match lit.kind() {
300+
Some(ast::Expr::Literal(lit)) => match lit.kind() {
301301
ast::LiteralKind::String(it) => Some(it),
302302
_ => None,
303303
},
@@ -315,6 +315,7 @@ fn find_doc_string_in_attr(attr: &hir::Attr, it: &ast::Attr) -> Option<ast::Stri
315315
string.text().get(1..string.text().len() - 1).map_or(false, |it| it == text)
316316
})
317317
}
318+
_ => return None,
318319
}
319320
}
320321

crates/parser/src/grammar/attributes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ fn attr(p: &mut Parser, inner: bool) {
3030
match p.current() {
3131
T![=] => {
3232
p.bump(T![=]);
33-
if expressions::literal(p).is_none() {
34-
p.error("expected literal");
33+
if expressions::expr(p).0.is_none() {
34+
p.error("expected expression");
3535
}
3636
}
3737
T!['('] | T!['['] | T!['{'] => items::token_tree(p),

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

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ impl Attr {
152152
pub fn l_brack_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['[']) }
153153
pub fn path(&self) -> Option<Path> { support::child(&self.syntax) }
154154
pub fn eq_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![=]) }
155-
pub fn literal(&self) -> Option<Literal> { support::child(&self.syntax) }
155+
pub fn expr(&self) -> Option<Expr> { support::child(&self.syntax) }
156156
pub fn token_tree(&self) -> Option<TokenTree> { support::child(&self.syntax) }
157157
pub fn r_brack_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![']']) }
158158
}
@@ -632,12 +632,6 @@ impl WherePred {
632632
pub fn ty(&self) -> Option<Type> { support::child(&self.syntax) }
633633
}
634634
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
635-
pub struct Literal {
636-
pub(crate) syntax: SyntaxNode,
637-
}
638-
impl ast::AttrsOwner for Literal {}
639-
impl Literal {}
640-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
641635
pub struct ExprStmt {
642636
pub(crate) syntax: SyntaxNode,
643637
}
@@ -805,6 +799,12 @@ impl IndexExpr {
805799
pub fn r_brack_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![']']) }
806800
}
807801
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
802+
pub struct Literal {
803+
pub(crate) syntax: SyntaxNode,
804+
}
805+
impl ast::AttrsOwner for Literal {}
806+
impl Literal {}
807+
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
808808
pub struct LoopExpr {
809809
pub(crate) syntax: SyntaxNode,
810810
}
@@ -2072,17 +2072,6 @@ impl AstNode for WherePred {
20722072
}
20732073
fn syntax(&self) -> &SyntaxNode { &self.syntax }
20742074
}
2075-
impl AstNode for Literal {
2076-
fn can_cast(kind: SyntaxKind) -> bool { kind == LITERAL }
2077-
fn cast(syntax: SyntaxNode) -> Option<Self> {
2078-
if Self::can_cast(syntax.kind()) {
2079-
Some(Self { syntax })
2080-
} else {
2081-
None
2082-
}
2083-
}
2084-
fn syntax(&self) -> &SyntaxNode { &self.syntax }
2085-
}
20862075
impl AstNode for ExprStmt {
20872076
fn can_cast(kind: SyntaxKind) -> bool { kind == EXPR_STMT }
20882077
fn cast(syntax: SyntaxNode) -> Option<Self> {
@@ -2259,6 +2248,17 @@ impl AstNode for IndexExpr {
22592248
}
22602249
fn syntax(&self) -> &SyntaxNode { &self.syntax }
22612250
}
2251+
impl AstNode for Literal {
2252+
fn can_cast(kind: SyntaxKind) -> bool { kind == LITERAL }
2253+
fn cast(syntax: SyntaxNode) -> Option<Self> {
2254+
if Self::can_cast(syntax.kind()) {
2255+
Some(Self { syntax })
2256+
} else {
2257+
None
2258+
}
2259+
}
2260+
fn syntax(&self) -> &SyntaxNode { &self.syntax }
2261+
}
22622262
impl AstNode for LoopExpr {
22632263
fn can_cast(kind: SyntaxKind) -> bool { kind == LOOP_EXPR }
22642264
fn cast(syntax: SyntaxNode) -> Option<Self> {
@@ -3887,11 +3887,6 @@ impl std::fmt::Display for WherePred {
38873887
std::fmt::Display::fmt(self.syntax(), f)
38883888
}
38893889
}
3890-
impl std::fmt::Display for Literal {
3891-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
3892-
std::fmt::Display::fmt(self.syntax(), f)
3893-
}
3894-
}
38953890
impl std::fmt::Display for ExprStmt {
38963891
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
38973892
std::fmt::Display::fmt(self.syntax(), f)
@@ -3972,6 +3967,11 @@ impl std::fmt::Display for IndexExpr {
39723967
std::fmt::Display::fmt(self.syntax(), f)
39733968
}
39743969
}
3970+
impl std::fmt::Display for Literal {
3971+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
3972+
std::fmt::Display::fmt(self.syntax(), f)
3973+
}
3974+
}
39753975
impl std::fmt::Display for LoopExpr {
39763976
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39773977
std::fmt::Display::fmt(self.syntax(), f)

crates/syntax/src/ast/node_ext.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,6 @@ impl ast::Attr {
109109
Some((self.simple_name()?, tt))
110110
}
111111

112-
pub fn as_simple_key_value(&self) -> Option<(SmolStr, SmolStr)> {
113-
let lit = self.literal()?;
114-
let key = self.simple_name()?;
115-
let value_token = lit.syntax().first_token()?;
116-
117-
let value: SmolStr = ast::String::cast(value_token)?.value()?.into();
118-
119-
Some((key, value))
120-
}
121-
122112
pub fn simple_name(&self) -> Option<SmolStr> {
123113
let path = self.path()?;
124114
match (path.segment(), path.qualifier()) {

xtask/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ anyhow = "1.0.26"
1111
flate2 = "1.0"
1212
proc-macro2 = "1.0.8"
1313
quote = "1.0.2"
14-
ungrammar = "=1.12"
14+
ungrammar = "=1.13"
1515
walkdir = "2.3.1"
1616
write-json = "0.1.0"
1717
xshell = "0.1"

0 commit comments

Comments
 (0)