From a5bf838047cc9b6c654bd37d38a4fd74ec2f2d2c Mon Sep 17 00:00:00 2001 From: Aaron Hill Date: Mon, 27 Jul 2020 16:08:15 -0400 Subject: [PATCH] Strip None-delimited groups from individual tokens In PR #10, I didn't properly handle all of the cases where `None`-delimited groups can appear. This makes the tests pass with the Nightly build from the previous PR. --- impl/src/lib.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/impl/src/lib.rs b/impl/src/lib.rs index baff0f1..0284e03 100644 --- a/impl/src/lib.rs +++ b/impl/src/lib.rs @@ -12,8 +12,8 @@ use proc_macro::*; /// 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 { + let mut tokens = input.clone().into_iter(); if let Some(TokenTree::Group(group)) = tokens.next() { if group.delimiter() == Delimiter::None { input = group.stream(); @@ -24,6 +24,10 @@ fn ignore_groups(mut input: TokenStream) -> TokenStream { } } +fn strip_groups(token: TokenTree) -> TokenTree { + ignore_groups(vec![token].into_iter().collect()).into_iter().next().unwrap() +} + #[cfg(feature = "rand")] #[proc_macro_attribute] pub fn obfstr_attribute(args: TokenStream, input: TokenStream) -> TokenStream { @@ -50,7 +54,7 @@ fn obfstr_impl(mut input: TokenStream) -> TokenStream { // Optional L ident prefix to indicate wide strings let mut wide = false; - if let Some(TokenTree::Ident(ident)) = &token { + if let Some(TokenTree::Ident(ident)) = &token.clone().map(strip_groups) { if ident.to_string() == "L" { wide = true; token = tt.next(); @@ -58,9 +62,9 @@ fn obfstr_impl(mut input: TokenStream) -> TokenStream { } // Followed by a string literal - let string = match token { + let string = match token.map(strip_groups) { Some(TokenTree::Literal(lit)) => string_parse(lit), - Some(tt) => panic!("expected a string literal: `{}`", tt), + Some(tt) => panic!("expected a string literal: `{:?}`", tt), None => panic!("expected a string literal"), };