Merge pull request #2410 from topecongiro/skip-repeat-macro

Skip rewriting macro def with repeat
This commit is contained in:
Nick Cameron 2018-02-04 14:33:03 +13:00 committed by GitHub
commit 346238f497
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 3 deletions

View file

@ -318,7 +318,10 @@ pub fn rewrite_macro_def(
// variables for new names with the same length first. // variables for new names with the same length first.
let old_body = context.snippet(branch.body).trim(); 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, // We'll hack the indent below, take this into account when formatting,
let mut config = context.config.clone(); 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 // Replaces `$foo` with `zfoo`. We must check for name overlap to ensure we
// aren't causing problems. // aren't causing problems.
// This should also work for escaped `$` variables, where we leave earlier `$`s. // This should also work for escaped `$` variables, where we leave earlier `$`s.
fn replace_names(input: &str) -> (String, HashMap<String, String>) { fn replace_names(input: &str) -> Option<(String, HashMap<String, String>)> {
// Each substitution will require five or six extra bytes. // Each substitution will require five or six extra bytes.
let mut result = String::with_capacity(input.len() + 64); let mut result = String::with_capacity(input.len() + 64);
let mut substs = HashMap::new(); let mut substs = HashMap::new();
@ -409,6 +412,9 @@ fn replace_names(input: &str) -> (String, HashMap<String, String>) {
dollar_count = 0; dollar_count = 0;
cur_name = String::new(); cur_name = String::new();
} else if c == '(' && cur_name.is_empty() {
// FIXME: Support macro def with repeat.
return None;
} else if c.is_alphanumeric() { } else if c.is_alphanumeric() {
cur_name.push(c); cur_name.push(c);
} }
@ -433,7 +439,7 @@ fn replace_names(input: &str) -> (String, HashMap<String, String>) {
debug!("replace_names `{}` {:?}", result, substs); debug!("replace_names `{}` {:?}", result, substs);
(result, substs) Some((result, substs))
} }
// This is a bit sketchy. The token rules probably need tweaking, but it works // This is a bit sketchy. The token rules probably need tweaking, but it works

View file

@ -332,3 +332,7 @@ macro foo() {
bar(); bar();
} }
} }
macro lex_err($kind: ident $(, $body: expr)*) {
Err(QlError::LexError(LexError::$kind($($body,)*)))
}

View file

@ -905,3 +905,7 @@ macro foo() {
bar(); bar();
} }
} }
macro lex_err($kind: ident $(, $body: expr)*) {
Err(QlError::LexError(LexError::$kind($($body,)*)))
}