Skip to content

Commit

Permalink
Merge pull request #54 from dtolnay/verbatimattr
Browse files Browse the repository at this point in the history
Preserve attributes on verbatim Expr
  • Loading branch information
dtolnay committed Jun 25, 2023
2 parents 4a2318e + 596ceaf commit 6f7a9ee
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,7 @@ impl Printer {

#[cfg(feature = "verbatim")]
fn expr_verbatim(&mut self, tokens: &TokenStream) {
use syn::parse::discouraged::Speculative;
use syn::parse::{Parse, ParseStream, Result};
use syn::{parenthesized, Ident};

Expand All @@ -677,11 +678,13 @@ impl Printer {
}

struct Builtin {
attrs: Vec<Attribute>,
name: Ident,
args: TokenStream,
}

struct RawReference {
attrs: Vec<Attribute>,
mutable: bool,
expr: Expr,
}
Expand All @@ -693,26 +696,34 @@ impl Printer {

impl Parse for ExprVerbatim {
fn parse(input: ParseStream) -> Result<Self> {
let lookahead = input.lookahead1();
let ahead = input.fork();
let attrs = ahead.call(Attribute::parse_outer)?;
let lookahead = ahead.lookahead1();
if input.is_empty() {
Ok(ExprVerbatim::Empty)
} else if lookahead.peek(kw::builtin) {
input.advance_to(&ahead);
input.parse::<kw::builtin>()?;
input.parse::<Token![#]>()?;
let name: Ident = input.parse()?;
let args;
parenthesized!(args in input);
let args: TokenStream = args.parse()?;
Ok(ExprVerbatim::Builtin(Builtin { name, args }))
Ok(ExprVerbatim::Builtin(Builtin { attrs, name, args }))
} else if lookahead.peek(Token![&]) {
input.advance_to(&ahead);
input.parse::<Token![&]>()?;
input.parse::<kw::raw>()?;
let mutable = input.parse::<Option<Token![mut]>>()?.is_some();
if !mutable {
input.parse::<Token![const]>()?;
}
let expr: Expr = input.parse()?;
Ok(ExprVerbatim::RawReference(RawReference { mutable, expr }))
Ok(ExprVerbatim::RawReference(RawReference {
attrs,
mutable,
expr,
}))
} else if lookahead.peek(Token![...]) {
input.parse::<Token![...]>()?;
Ok(ExprVerbatim::Ellipsis)
Expand All @@ -733,6 +744,7 @@ impl Printer {
self.word("...");
}
ExprVerbatim::Builtin(expr) => {
self.outer_attrs(&expr.attrs);
self.word("builtin # ");
self.ident(&expr.name);
self.word("(");
Expand All @@ -749,6 +761,7 @@ impl Printer {
self.word(")");
}
ExprVerbatim::RawReference(expr) => {
self.outer_attrs(&expr.attrs);
self.word("&raw ");
self.word(if expr.mutable { "mut " } else { "const " });
self.expr(&expr.expr);
Expand Down

0 comments on commit 6f7a9ee

Please sign in to comment.