Skip to content

Commit 346238f

Browse files
authored
Merge pull request #2410 from topecongiro/skip-repeat-macro
Skip rewriting macro def with repeat
2 parents ca09746 + 61b23a4 commit 346238f

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

src/macros.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,10 @@ pub fn rewrite_macro_def(
318318
// variables for new names with the same length first.
319319

320320
let old_body = context.snippet(branch.body).trim();
321-
let (body_str, substs) = replace_names(old_body);
321+
let (body_str, substs) = match replace_names(old_body) {
322+
Some(result) => result,
323+
None => return snippet,
324+
};
322325

323326
// We'll hack the indent below, take this into account when formatting,
324327
let mut config = context.config.clone();
@@ -377,7 +380,7 @@ pub fn rewrite_macro_def(
377380
// Replaces `$foo` with `zfoo`. We must check for name overlap to ensure we
378381
// aren't causing problems.
379382
// This should also work for escaped `$` variables, where we leave earlier `$`s.
380-
fn replace_names(input: &str) -> (String, HashMap<String, String>) {
383+
fn replace_names(input: &str) -> Option<(String, HashMap<String, String>)> {
381384
// Each substitution will require five or six extra bytes.
382385
let mut result = String::with_capacity(input.len() + 64);
383386
let mut substs = HashMap::new();
@@ -409,6 +412,9 @@ fn replace_names(input: &str) -> (String, HashMap<String, String>) {
409412

410413
dollar_count = 0;
411414
cur_name = String::new();
415+
} else if c == '(' && cur_name.is_empty() {
416+
// FIXME: Support macro def with repeat.
417+
return None;
412418
} else if c.is_alphanumeric() {
413419
cur_name.push(c);
414420
}
@@ -433,7 +439,7 @@ fn replace_names(input: &str) -> (String, HashMap<String, String>) {
433439

434440
debug!("replace_names `{}` {:?}", result, substs);
435441

436-
(result, substs)
442+
Some((result, substs))
437443
}
438444

439445
// This is a bit sketchy. The token rules probably need tweaking, but it works

tests/source/macros.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,3 +332,7 @@ macro foo() {
332332
bar();
333333
}
334334
}
335+
336+
macro lex_err($kind: ident $(, $body: expr)*) {
337+
Err(QlError::LexError(LexError::$kind($($body,)*)))
338+
}

tests/target/macros.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -905,3 +905,7 @@ macro foo() {
905905
bar();
906906
}
907907
}
908+
909+
macro lex_err($kind: ident $(, $body: expr)*) {
910+
Err(QlError::LexError(LexError::$kind($($body,)*)))
911+
}

0 commit comments

Comments
 (0)