Skip to content

Commit

Permalink
Support builtin# expr syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed May 14, 2023
1 parent ea7c3dd commit 12505bf
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Expand Up @@ -19,10 +19,10 @@ verbatim = ["syn/parsing"]

[dependencies]
proc-macro2 = { version = "1.0", default-features = false }
syn = { version = "2.0.10", default-features = false, features = ["full"] }
syn = { version = "2.0.16", default-features = false, features = ["full"] }

[dev-dependencies]
syn = { version = "2.0.10", default-features = false, features = ["parsing"] }
syn = { version = "2.0.16", default-features = false, features = ["parsing"] }

[lib]
doc-scrape-examples = false
Expand Down
32 changes: 32 additions & 0 deletions src/expr.rs
Expand Up @@ -665,18 +665,26 @@ impl Printer {
#[cfg(feature = "verbatim")]
fn expr_verbatim(&mut self, tokens: &TokenStream) {
use syn::parse::{Parse, ParseStream, Result};
use syn::{parenthesized, Ident};

enum ExprVerbatim {
Empty,
Builtin(Builtin),
RawReference(RawReference),
}

struct Builtin {
name: Ident,
args: TokenStream,
}

struct RawReference {
mutable: bool,
expr: Expr,
}

mod kw {
syn::custom_keyword!(builtin);
syn::custom_keyword!(raw);
}

Expand All @@ -685,6 +693,14 @@ impl Printer {
let lookahead = input.lookahead1();
if input.is_empty() {
Ok(ExprVerbatim::Empty)
} else if lookahead.peek(kw::builtin) {
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 }))
} else if lookahead.peek(Token![&]) {
input.parse::<Token![&]>()?;
input.parse::<kw::raw>()?;
Expand All @@ -707,6 +723,22 @@ impl Printer {

match expr {
ExprVerbatim::Empty => {}
ExprVerbatim::Builtin(expr) => {
self.word("builtin # ");
self.ident(&expr.name);
self.word("(");
if !expr.args.is_empty() {
self.cbox(INDENT);
self.zerobreak();
self.ibox(0);
self.macro_rules_tokens(expr.args, false);
self.end();
self.zerobreak();
self.offset(-INDENT);
self.end();
}
self.word(")");
}
ExprVerbatim::RawReference(expr) => {
self.word("&raw ");
self.word(if expr.mutable { "mut " } else { "const " });
Expand Down

0 comments on commit 12505bf

Please sign in to comment.