Skip to content

Commit

Permalink
Strip None-delimited groups from individual tokens
Browse files Browse the repository at this point in the history
In PR CasualX#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.
  • Loading branch information
Aaron1011 committed Jul 27, 2020
1 parent 35ab9db commit a5bf838
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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 {
Expand All @@ -50,17 +54,17 @@ 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();
}
}

// 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"),
};

Expand Down

0 comments on commit a5bf838

Please sign in to comment.