From 61b23a4293e643202d0e92439d9902c869259333 Mon Sep 17 00:00:00 2001 From: Seiichi Uchida Date: Sun, 4 Feb 2018 08:52:50 +0900 Subject: [PATCH] Skip rewriting macro def with repeat --- src/macros.rs | 12 +++++++++--- tests/source/macros.rs | 4 ++++ tests/target/macros.rs | 4 ++++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/macros.rs b/src/macros.rs index b0080695449..7df793ca79d 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -318,7 +318,10 @@ pub fn rewrite_macro_def( // variables for new names with the same length first. let old_body = context.snippet(branch.body).trim(); - let (body_str, substs) = replace_names(old_body); + let (body_str, substs) = match replace_names(old_body) { + Some(result) => result, + None => return snippet, + }; // We'll hack the indent below, take this into account when formatting, let mut config = context.config.clone(); @@ -377,7 +380,7 @@ pub fn rewrite_macro_def( // Replaces `$foo` with `zfoo`. We must check for name overlap to ensure we // aren't causing problems. // This should also work for escaped `$` variables, where we leave earlier `$`s. -fn replace_names(input: &str) -> (String, HashMap) { +fn replace_names(input: &str) -> Option<(String, HashMap)> { // Each substitution will require five or six extra bytes. let mut result = String::with_capacity(input.len() + 64); let mut substs = HashMap::new(); @@ -409,6 +412,9 @@ fn replace_names(input: &str) -> (String, HashMap) { dollar_count = 0; cur_name = String::new(); + } else if c == '(' && cur_name.is_empty() { + // FIXME: Support macro def with repeat. + return None; } else if c.is_alphanumeric() { cur_name.push(c); } @@ -433,7 +439,7 @@ fn replace_names(input: &str) -> (String, HashMap) { debug!("replace_names `{}` {:?}", result, substs); - (result, substs) + Some((result, substs)) } // This is a bit sketchy. The token rules probably need tweaking, but it works diff --git a/tests/source/macros.rs b/tests/source/macros.rs index dca1e0e7033..ffd5c19cc8c 100644 --- a/tests/source/macros.rs +++ b/tests/source/macros.rs @@ -332,3 +332,7 @@ macro foo() { bar(); } } + +macro lex_err($kind: ident $(, $body: expr)*) { + Err(QlError::LexError(LexError::$kind($($body,)*))) +} diff --git a/tests/target/macros.rs b/tests/target/macros.rs index 75238b11ab7..a79eb8fb294 100644 --- a/tests/target/macros.rs +++ b/tests/target/macros.rs @@ -905,3 +905,7 @@ macro foo() { bar(); } } + +macro lex_err($kind: ident $(, $body: expr)*) { + Err(QlError::LexError(LexError::$kind($($body,)*))) +}