1111use nom:: combinator:: verify;
1212use nom:: {
1313 branch:: alt, bytes:: complete:: tag, combinator:: opt, map, sequence:: preceded, take_until,
14- terminated, Err :: Incomplete , IResult ,
14+ terminated, AsChar , Err :: Incomplete , IResult ,
1515} ;
1616
1717use crate :: keyword:: Keyword ;
@@ -23,6 +23,8 @@ use crate::symbol::Symbol;
2323use crate :: value:: { ToValue , Value } ;
2424use std:: rc:: Rc ;
2525
26+ use nom:: Err :: Error ;
27+ use std:: borrow:: Borrow ;
2628use std:: io:: BufRead ;
2729//
2830// Note; the difference between ours 'parsers'
@@ -389,20 +391,29 @@ pub fn try_read_string(input: &str) -> IResult<&str, Value> {
389391}
390392
391393/// Tries to parse &str into Value::Pattern
394+ /// Reader Macro for Regex
392395/// Example Successes:
393396/// #"this is pretty straightforward" => Value::Pattern("this is pretty straightforward")
394397pub fn try_read_pattern ( input : & str ) -> IResult < & str , Value > {
395398 named ! ( hash_quotation<& str , & str >, preceded!( consume_clojure_whitespaces_parser, tag!( "#\" " ) ) ) ;
396399
397- let ( rest_input, _) = hash_quotation ( & input. escape_default ( ) . to_string ( ) ) ?;
398- named ! (
399- pattern_parser<& str , regex:: Regex >,
400- map!(
401- terminated!( take_until!( "\" " ) , tag( "\" " ) ) ,
402- |v| regex:: Regex :: new( v) . unwrap( )
403- )
404- ) ;
405- to_value_parser ( pattern_parser) ( rest_input)
400+ let ( rest_input, _) = hash_quotation ( input) ?;
401+
402+ let mut iterator = rest_input. clone ( ) . chars ( ) . into_iter ( ) ;
403+ let mut prev: char = iterator. next ( ) . unwrap ( ) ;
404+ let mut till_quote: String = String :: from ( prev. to_string ( ) ) ;
405+ while let ch = iterator. next ( ) . unwrap ( ) {
406+ if ( ch. is_whitespace ( ) && prev == '"' ) {
407+ till_quote = till_quote. trim_end_matches ( "\" " ) . to_string ( ) ;
408+ break ;
409+ } ;
410+ till_quote = String :: from ( till_quote + ch. to_string ( ) . as_str ( ) ) ;
411+ prev = ch;
412+ }
413+ Ok ( (
414+ & till_quote,
415+ Value :: Pattern ( regex:: Regex :: new ( & till_quote) . unwrap ( ) ) ,
416+ ) )
406417}
407418
408419// @TODO Perhaps generalize this, or even generalize it as a reader macro
@@ -839,8 +850,8 @@ mod tests {
839850 #[ test]
840851 fn try_read_regex_pattern_escaped_quote_test ( ) {
841852 assert_eq ! (
842- Value :: Pattern ( regex:: Regex :: new( "hel \" lo " ) . unwrap( ) ) ,
843- try_read( "#\" hel \" lo \" " ) . ok( ) . unwrap( ) . 1
853+ Value :: Pattern ( regex:: Regex :: new( "h \" e \" l \" l \" o " ) . unwrap( ) ) ,
854+ try_read( "#\" h \" e \" l \" l \" o \" something " ) . ok( ) . unwrap( ) . 1
844855 ) ;
845856 }
846857 }
0 commit comments