Skip to content

Commit 3ab5a45

Browse files
authored
Strip None-delimited groups from individual tokens (#13)
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.
1 parent 35ab9db commit 3ab5a45

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

impl/src/lib.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ use proc_macro::*;
1212
/// by a `macro_rules!` expansion.
1313
/// See https://github.com/rust-lang/rust/issues/72545 for background
1414
fn ignore_groups(mut input: TokenStream) -> TokenStream {
15-
let mut tokens = input.clone().into_iter();
1615
loop {
16+
let mut tokens = input.clone().into_iter();
1717
if let Some(TokenTree::Group(group)) = tokens.next() {
1818
if group.delimiter() == Delimiter::None {
1919
input = group.stream();
@@ -24,6 +24,10 @@ fn ignore_groups(mut input: TokenStream) -> TokenStream {
2424
}
2525
}
2626

27+
fn strip_groups(token: TokenTree) -> TokenTree {
28+
ignore_groups(vec![token].into_iter().collect()).into_iter().next().unwrap()
29+
}
30+
2731
#[cfg(feature = "rand")]
2832
#[proc_macro_attribute]
2933
pub fn obfstr_attribute(args: TokenStream, input: TokenStream) -> TokenStream {
@@ -50,17 +54,17 @@ fn obfstr_impl(mut input: TokenStream) -> TokenStream {
5054

5155
// Optional L ident prefix to indicate wide strings
5256
let mut wide = false;
53-
if let Some(TokenTree::Ident(ident)) = &token {
57+
if let Some(TokenTree::Ident(ident)) = &token.clone().map(strip_groups) {
5458
if ident.to_string() == "L" {
5559
wide = true;
5660
token = tt.next();
5761
}
5862
}
5963

6064
// Followed by a string literal
61-
let string = match token {
65+
let string = match token.map(strip_groups) {
6266
Some(TokenTree::Literal(lit)) => string_parse(lit),
63-
Some(tt) => panic!("expected a string literal: `{}`", tt),
67+
Some(tt) => panic!("expected a string literal: `{:?}`", tt),
6468
None => panic!("expected a string literal"),
6569
};
6670

0 commit comments

Comments
 (0)