11
11
use nom:: combinator:: verify;
12
12
use nom:: {
13
13
branch:: alt, bytes:: complete:: tag, combinator:: opt, map, sequence:: preceded, take_until,
14
- terminated, Err :: Incomplete , IResult ,
14
+ terminated, AsChar , Err :: Incomplete , IResult ,
15
15
} ;
16
16
17
17
use crate :: keyword:: Keyword ;
@@ -23,6 +23,8 @@ use crate::symbol::Symbol;
23
23
use crate :: value:: { ToValue , Value } ;
24
24
use std:: rc:: Rc ;
25
25
26
+ use nom:: Err :: Error ;
27
+ use std:: borrow:: Borrow ;
26
28
use std:: io:: BufRead ;
27
29
//
28
30
// Note; the difference between ours 'parsers'
@@ -389,20 +391,29 @@ pub fn try_read_string(input: &str) -> IResult<&str, Value> {
389
391
}
390
392
391
393
/// Tries to parse &str into Value::Pattern
394
+ /// Reader Macro for Regex
392
395
/// Example Successes:
393
396
/// #"this is pretty straightforward" => Value::Pattern("this is pretty straightforward")
394
397
pub fn try_read_pattern ( input : & str ) -> IResult < & str , Value > {
395
398
named ! ( hash_quotation<& str , & str >, preceded!( consume_clojure_whitespaces_parser, tag!( "#\" " ) ) ) ;
396
399
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
+ ) )
406
417
}
407
418
408
419
// @TODO Perhaps generalize this, or even generalize it as a reader macro
@@ -839,8 +850,8 @@ mod tests {
839
850
#[ test]
840
851
fn try_read_regex_pattern_escaped_quote_test ( ) {
841
852
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
844
855
) ;
845
856
}
846
857
}
0 commit comments