Skip to content
This repository was archived by the owner on Dec 27, 2022. It is now read-only.

Commit 4df5a94

Browse files
committed
Remove assumptions about whitespace between tokens
1 parent f16b7d1 commit 4df5a94

File tree

1 file changed

+76
-34
lines changed

1 file changed

+76
-34
lines changed

src/lib.rs

Lines changed: 76 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -282,25 +282,46 @@ macro_rules! proc_macro_expr_impl {
282282
#[proc_macro_derive($func)]
283283
pub fn $func(input: $func::TokenStream) -> $func::TokenStream {
284284
let source = input.to_string();
285-
let source = source.trim();
285+
let mut tokens = source.trim();
286286

287-
let prefix = "#[allow(unused)]\nenum ProcMacroHack {";
288-
let suffix = "}";
289-
assert!(source.starts_with(prefix));
290-
assert!(source.ends_with(suffix));
291-
let source = &source[prefix.len() .. source.len() - suffix.len()].trim();
287+
for &prefix in &[
288+
"#",
289+
"[",
290+
"allow",
291+
"(",
292+
"unused",
293+
")",
294+
"]",
295+
"enum",
296+
"ProcMacroHack",
297+
"{",
298+
"Input",
299+
"=",
300+
"(",
301+
"stringify",
302+
"!",
303+
"(",
304+
] {
305+
assert!(tokens.starts_with(prefix));
306+
tokens = &tokens[prefix.len()..].trim();
307+
}
292308

293-
let prefix = "Input =";
294-
let suffix = "0).1,";
295-
assert!(source.starts_with(prefix));
296-
assert!(source.ends_with(suffix));
297-
let source = &source[prefix.len() .. source.len() - suffix.len()].trim();
298-
299-
let prefix = "(stringify!(";
300-
let suffix = "),";
301-
assert!(source.starts_with(prefix));
302-
assert!(source.ends_with(suffix));
303-
let tokens = &source[prefix.len() .. source.len() - suffix.len()].trim();
309+
for &suffix in &[
310+
"}",
311+
",",
312+
"1",
313+
".",
314+
")",
315+
"0",
316+
",",
317+
")",
318+
] {
319+
if suffix == "," && !tokens.ends_with(suffix) {
320+
continue;
321+
}
322+
assert!(tokens.ends_with(suffix));
323+
tokens = &tokens[..tokens.len() - suffix.len()].trim();
324+
}
304325

305326
fn func($input: &str) -> String $body
306327

@@ -350,25 +371,46 @@ macro_rules! proc_macro_item_impl {
350371
#[proc_macro_derive($func)]
351372
pub fn $func(input: $func::TokenStream) -> $func::TokenStream {
352373
let source = input.to_string();
353-
let source = source.trim();
354-
355-
let prefix = "#[allow(unused)]\nenum ProcMacroHack {";
356-
let suffix = "}";
357-
assert!(source.starts_with(prefix));
358-
assert!(source.ends_with(suffix));
359-
let source = &source[prefix.len() .. source.len() - suffix.len()].trim();
374+
let mut tokens = source.trim();
360375

361-
let prefix = "Input =";
362-
let suffix = "0).1,";
363-
assert!(source.starts_with(prefix));
364-
assert!(source.ends_with(suffix));
365-
let source = &source[prefix.len() .. source.len() - suffix.len()].trim();
376+
for &prefix in &[
377+
"#",
378+
"[",
379+
"allow",
380+
"(",
381+
"unused",
382+
")",
383+
"]",
384+
"enum",
385+
"ProcMacroHack",
386+
"{",
387+
"Input",
388+
"=",
389+
"(",
390+
"stringify",
391+
"!",
392+
"(",
393+
] {
394+
assert!(tokens.starts_with(prefix));
395+
tokens = &tokens[prefix.len()..].trim();
396+
}
366397

367-
let prefix = "(stringify!(";
368-
let suffix = "),";
369-
assert!(source.starts_with(prefix));
370-
assert!(source.ends_with(suffix));
371-
let tokens = &source[prefix.len() .. source.len() - suffix.len()].trim();
398+
for &suffix in &[
399+
"}",
400+
",",
401+
"1",
402+
".",
403+
")",
404+
"0",
405+
",",
406+
")",
407+
] {
408+
if suffix == "," && !tokens.ends_with(suffix) {
409+
continue;
410+
}
411+
assert!(tokens.ends_with(suffix));
412+
tokens = &tokens[..tokens.len() - suffix.len()].trim();
413+
}
372414

373415
fn func($input: &str) -> String $body
374416

0 commit comments

Comments
 (0)