Skip to content

Commit c71052f

Browse files
committed
some progress, unit tests that are broken because of broken implementation
1 parent cf165fd commit c71052f

File tree

1 file changed

+76
-18
lines changed

1 file changed

+76
-18
lines changed

src/reader.rs

Lines changed: 76 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -399,20 +399,49 @@ pub fn try_read_pattern(input: &str) -> IResult<&str, Value> {
399399

400400
let (rest_input, _) = hash_quotation(input)?;
401401

402-
let mut iterator = rest_input.clone().chars().into_iter();
402+
// println!("regexquoted: {:#?}", regex::Regex::quote(rest_input));
403+
let mut iterator = rest_input.escape_default();
403404
let mut prev: char = iterator.next().unwrap();
405+
let mut prev_prev_was_escape = false;
406+
let mut is_escaping = false;
404407
let mut till_quote: String = String::from(prev.to_string());
408+
println!("first char: {:#?}", till_quote);
405409
while let ch = iterator.next().unwrap() {
406-
if (ch.is_whitespace() && prev == '"') {
410+
if ch == '\\' && prev == '\\' {
411+
is_escaping = true;
412+
}
413+
println!(
414+
"LOOP: next char to handle: {:#?} prev: {:#?} is escaping {} and prev prev was escaping {}",
415+
ch, prev, is_escaping, prev_prev_was_escape
416+
);
417+
if ch == '\"' && prev == '\\' && !prev_prev_was_escape {
418+
println!(
419+
"GONNA END: next char to handle: {:#?} prev: {:#?} is escaping {}",
420+
ch, prev, is_escaping
421+
);
407422
till_quote = till_quote.trim_end_matches("\"").to_string();
408423
break;
409424
};
410-
till_quote = String::from(till_quote + ch.to_string().as_str());
425+
if ch == '\"' && is_escaping {
426+
till_quote = String::from(till_quote + ch.to_string().as_str());
427+
} else if ch != '\\' {
428+
till_quote = String::from(till_quote + ch.to_string().as_str());
429+
//is_escaping = false;
430+
prev_prev_was_escape = false;
431+
}
432+
prev_prev_was_escape = is_escaping;
411433
prev = ch;
412434
}
435+
println!("till quote: {} {:#?}", till_quote, till_quote);
436+
let to_trim = till_quote.to_owned() + "\"";
437+
println!(
438+
"rest input trimmed: {}",
439+
rest_input.trim_start_matches(&to_trim)
440+
);
441+
let regex = regex::Regex::new(till_quote.as_str()).unwrap();
413442
Ok((
414-
&till_quote,
415-
Value::Pattern(regex::Regex::new(&till_quote).unwrap()),
443+
rest_input.trim_start_matches(&to_trim),
444+
Value::Pattern(regex),
416445
))
417446
}
418447

@@ -839,20 +868,49 @@ mod tests {
839868
assert_eq!(Value::Boolean(false), try_read("false ").ok().unwrap().1)
840869
}
841870

842-
#[test]
843-
fn try_read_regex_pattern_test() {
844-
assert_eq!(
845-
Value::Pattern(regex::Regex::new("hello").unwrap()),
846-
try_read("#\"hello\" ").ok().unwrap().1
847-
);
848-
}
871+
mod regex_tests {
872+
use crate::reader::try_read;
873+
use crate::value::Value;
849874

850-
#[test]
851-
fn try_read_regex_pattern_escaped_quote_test() {
852-
assert_eq!(
853-
Value::Pattern(regex::Regex::new("h\"e\"l\"l\"o").unwrap()),
854-
try_read("#\"h\"e\"l\"l\"o\" something").ok().unwrap().1
855-
);
875+
#[test]
876+
fn try_read_simple_regex_pattern_test() {
877+
assert_eq!(
878+
Value::Pattern(regex::Regex::new("a").unwrap()),
879+
try_read(r###"#"a" "###).ok().unwrap().1
880+
);
881+
}
882+
883+
#[test]
884+
fn try_read_regex_pattern_test() {
885+
assert_eq!(
886+
Value::Pattern(regex::Regex::new("hello").unwrap()),
887+
try_read("#\"hello\" ").ok().unwrap().1
888+
);
889+
}
890+
891+
#[test]
892+
fn try_read_regex_pattern_escaped_quote_test() {
893+
assert_eq!(
894+
Value::Pattern(regex::Regex::new("h\"e\"l\"l\"o").unwrap()),
895+
try_read(r#"#"h\"e\"l\"l\"o\"" something"#).ok().unwrap().1
896+
);
897+
}
898+
899+
#[test]
900+
fn try_read_regex_pattern_escaped_quote_prefixed_by_whitespace_test() {
901+
assert_eq!(
902+
Value::Pattern(regex::Regex::new("h\"e\"l\"l \"o").unwrap()),
903+
try_read("#\"h\"e\"l\"l \"o\" something").ok().unwrap().1
904+
);
905+
}
906+
907+
#[test]
908+
fn try_read_regex_pattern_escaped_quote_suffixed_by_whitespace_test() {
909+
assert_eq!(
910+
Value::Pattern(regex::Regex::new("h\"e\"l\" l \"o").unwrap()),
911+
try_read("#\"h\"e\"l\" l \"o\" something").ok().unwrap().1
912+
);
913+
}
856914
}
857915
}
858916

0 commit comments

Comments
 (0)