Skip to content

Handle Groups with Delimiter::None #50

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

Merged
merged 1 commit into from
Jun 1, 2020
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
25 changes: 23 additions & 2 deletions hex-literal/hex-literal-impl/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
extern crate proc_macro;

use proc_macro::{TokenStream, TokenTree};
use proc_macro::{TokenStream, TokenTree, Delimiter};
use proc_macro_hack::proc_macro_hack;

fn is_hex_char(c: &char) -> bool {
Expand All @@ -17,8 +17,29 @@ fn is_format_char(c: &char) -> bool {
}
}


/// Strips any outer `Delimiter::None` groups from the input,
/// returning a `TokenStream` consisting of the innermost
/// non-empty-group `TokenTree`.
/// This is used to handle a proc macro being invoked
/// by a `macro_rules!` expansion.
/// See https://github.com/rust-lang/rust/issues/72545 for background
fn ignore_groups(mut input: TokenStream) -> TokenStream {
let mut tokens = input.clone().into_iter();
loop {
if let Some(TokenTree::Group(group)) = tokens.next() {
if group.delimiter() == Delimiter::None {
input = group.stream();
continue;
}
}
return input;
}
}

#[proc_macro_hack]
pub fn hex(input: TokenStream) -> TokenStream {
pub fn hex(mut input: TokenStream) -> TokenStream {
input = ignore_groups(input);
let mut ts = input.into_iter();
let input = match (ts.next(), ts.next()) {
(Some(TokenTree::Literal(literal)), None) => literal.to_string(),
Expand Down