Implement builtin#format_args, using rustc's format_args parser

This commit is contained in:
Lukas Wirth 2023-09-05 19:06:15 +02:00
parent 3431d586e5
commit abe8f1ece4
19 changed files with 1740 additions and 14 deletions

View file

@ -382,7 +382,13 @@ AsmExpr =
Attr* 'builtin' '#' 'asm' '(' Expr ')'
FormatArgsExpr =
Attr* 'builtin' '#' 'format_args' '(' ')'
Attr* 'builtin' '#' 'format_args' '('
template:Expr
(',' args:(FormatArgsArg (',' FormatArgsArg)* ','?)? )?
')'
FormatArgsArg =
(Name '=')? Expr
MacroExpr =
MacroCall

View file

@ -931,6 +931,9 @@ impl FormatArgsExpr {
support::token(&self.syntax, T![format_args])
}
pub fn l_paren_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['(']) }
pub fn template(&self) -> Option<Expr> { support::child(&self.syntax) }
pub fn comma_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![,]) }
pub fn args(&self) -> AstChildren<FormatArgsArg> { support::children(&self.syntax) }
pub fn r_paren_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![')']) }
}
@ -1163,6 +1166,16 @@ impl UnderscoreExpr {
pub fn underscore_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![_]) }
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct FormatArgsArg {
pub(crate) syntax: SyntaxNode,
}
impl ast::HasName for FormatArgsArg {}
impl FormatArgsArg {
pub fn eq_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![=]) }
pub fn expr(&self) -> Option<Expr> { support::child(&self.syntax) }
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct StmtList {
pub(crate) syntax: SyntaxNode,
@ -2855,6 +2868,17 @@ impl AstNode for UnderscoreExpr {
}
fn syntax(&self) -> &SyntaxNode { &self.syntax }
}
impl AstNode for FormatArgsArg {
fn can_cast(kind: SyntaxKind) -> bool { kind == FORMAT_ARGS_ARG }
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 StmtList {
fn can_cast(kind: SyntaxKind) -> bool { kind == STMT_LIST }
fn cast(syntax: SyntaxNode) -> Option<Self> {
@ -4254,6 +4278,7 @@ impl AstNode for AnyHasName {
| VARIANT
| CONST_PARAM
| TYPE_PARAM
| FORMAT_ARGS_ARG
| IDENT_PAT
)
}
@ -4860,6 +4885,11 @@ impl std::fmt::Display for UnderscoreExpr {
std::fmt::Display::fmt(self.syntax(), f)
}
}
impl std::fmt::Display for FormatArgsArg {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(self.syntax(), f)
}
}
impl std::fmt::Display for StmtList {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(self.syntax(), f)

View file

@ -169,6 +169,7 @@ pub(crate) const KINDS_SRC: KindsSrc<'_> = KindsSrc {
"OFFSET_OF_EXPR",
"ASM_EXPR",
"FORMAT_ARGS_EXPR",
"FORMAT_ARGS_ARG",
// postfix
"CALL_EXPR",
"INDEX_EXPR",