Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve attributes on verbatim Expr #54

Merged
merged 1 commit into from
Jun 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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