Skip to content

Commit be3ff0d

Browse files
committed
try_read_pattern refactor to use new escaped strings
1 parent 0b12e6d commit be3ff0d

File tree

1 file changed

+15
-48
lines changed

1 file changed

+15
-48
lines changed

src/reader.rs

Lines changed: 15 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -401,57 +401,24 @@ pub fn try_read_string(input: &str) -> IResult<&str, Value> {
401401
to_value_parser(string_parser)(rest_input)
402402
}
403403

404-
/// Tries to parse &str into Value::Pattern
405-
/// Reader Macro for Regex
406-
/// Example Successes:
407-
/// #"this is pretty straightforward" => Value::Pattern("this is pretty straightforward")
408404
pub fn try_read_pattern(input: &str) -> IResult<&str, Value> {
409-
named!(hash_quotation<&str, &str>, preceded!(consume_clojure_whitespaces_parser, tag!("#\"")));
410-
411-
let (rest_input, _) = hash_quotation(input)?;
412-
413-
// println!("regexquoted: {:#?}", regex::Regex::quote(rest_input));
414-
let mut iterator = rest_input.escape_default();
415-
let mut prev: char = iterator.next().unwrap();
416-
let mut prev_prev_was_escape = false;
417-
let mut is_escaping = false;
418-
let mut till_quote: String = String::from(prev.to_string());
419-
println!("first char: {:#?}", till_quote);
420-
while let ch = iterator.next().unwrap() {
421-
if ch == '\\' && prev == '\\' {
422-
is_escaping = true;
423-
}
424-
println!(
425-
"LOOP: next char to handle: {:#?} prev: {:#?} is escaping {} and prev prev was escaping {}",
426-
ch, prev, is_escaping, prev_prev_was_escape
427-
);
428-
if ch == '\"' && prev == '\\' && !prev_prev_was_escape {
429-
println!(
430-
"GONNA END: next char to handle: {:#?} prev: {:#?} is escaping {}",
431-
ch, prev, is_escaping
432-
);
433-
till_quote = till_quote.trim_end_matches("\"").to_string();
434-
break;
435-
};
436-
if ch == '\"' && is_escaping {
437-
till_quote = String::from(till_quote + ch.to_string().as_str());
438-
} else if ch != '\\' {
439-
till_quote = String::from(till_quote + ch.to_string().as_str());
440-
//is_escaping = false;
441-
prev_prev_was_escape = false;
442-
}
443-
prev_prev_was_escape = is_escaping;
444-
prev = ch;
405+
named!(hash_parser<&str, &str>, preceded!(consume_clojure_whitespaces_parser, tag!("#")));
406+
407+
let (rest_input, _) = hash_parser(input)?;
408+
let (rest_input,regex_string_val) = try_read_string(rest_input)?;
409+
410+
let mut regex_string = String::from("");
411+
412+
// @TODO separate try_read_string into a parser, so we don't have to read a Value
413+
// and then unwrap it
414+
match regex_string_val {
415+
Value::String(reg_str) => { regex_string = reg_str; },
416+
_ => { panic!("try_read_string returned something that wasn't string"); }
445417
}
446-
println!("till quote: {} {:#?}", till_quote, till_quote);
447-
let to_trim = till_quote.to_owned() + "\"";
448-
println!(
449-
"rest input trimmed: {}",
450-
rest_input.trim_start_matches(&to_trim)
451-
);
452-
let regex = regex::Regex::new(till_quote.as_str()).unwrap();
418+
419+
let regex = regex::Regex::new(regex_string.as_str()).unwrap();
453420
Ok((
454-
rest_input.trim_start_matches(&to_trim),
421+
rest_input,
455422
Value::Pattern(regex),
456423
))
457424
}

0 commit comments

Comments
 (0)