@@ -364,8 +364,8 @@ pub fn try_read_symbol(input: &str) -> IResult<&str, Value> {
364364/// Example Successes:
365365/// nil => Value::Nil
366366pub fn try_read_nil ( input : & str ) -> IResult < & str , Value > {
367- let ( rest_input, _) = verify ( identifier_parser, |ident : & str | ident == "nil" ) ( input) ?;
368- Ok ( ( rest_input, Value :: Nil ) )
367+ let ( rest_input, _) = verify ( identifier_parser, |ident : & str | ident == "nil" ) ( input) ?;
368+ Ok ( ( rest_input, Value :: Nil ) )
369369}
370370
371371// @TODO allow escaped strings
@@ -388,6 +388,23 @@ pub fn try_read_string(input: &str) -> IResult<&str, Value> {
388388 to_value_parser ( string_parser) ( rest_input)
389389}
390390
391+ /// Tries to parse &str into Value::Pattern
392+ /// Example Successes:
393+ /// #"this is pretty straightforward" => Value::Pattern("this is pretty straightforward")
394+ pub fn try_read_pattern ( input : & str ) -> IResult < & str , Value > {
395+ named ! ( hash_quotation<& str , & str >, preceded!( consume_clojure_whitespaces_parser, tag!( "#\" " ) ) ) ;
396+
397+ let ( rest_input, _) = hash_quotation ( input) ?;
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)
406+ }
407+
391408// @TODO Perhaps generalize this, or even generalize it as a reader macro
392409/// Tries to parse &str into Value::PersistentListMap, or some other Value::..Map
393410/// Example Successes:
@@ -492,6 +509,7 @@ pub fn try_read(input: &str) -> IResult<&str, Value> {
492509 try_read_keyword,
493510 try_read_list,
494511 try_read_vector,
512+ try_read_pattern,
495513 ) ) ,
496514 ) ( input)
497515}
@@ -726,8 +744,8 @@ mod tests {
726744 use crate :: persistent_vector;
727745 use crate :: reader:: try_read;
728746 use crate :: symbol:: Symbol ;
729- use crate :: value:: Value ;
730747 use crate :: value:: Value :: { PersistentList , PersistentListMap , PersistentVector } ;
748+ use crate :: value:: { ToValue , Value } ;
731749
732750 #[ test]
733751 fn try_read_empty_map_test ( ) {
@@ -809,6 +827,14 @@ mod tests {
809827 fn try_read_bool_false_test ( ) {
810828 assert_eq ! ( Value :: Boolean ( false ) , try_read( "false " ) . ok( ) . unwrap( ) . 1 )
811829 }
830+
831+ #[ test]
832+ fn try_read_regex_pattern_test ( ) {
833+ assert_eq ! (
834+ Value :: Pattern ( regex:: Regex :: new( "hello" ) . unwrap( ) ) ,
835+ try_read( "#\" hello\" " ) . ok( ) . unwrap( ) . 1
836+ ) ;
837+ }
812838 }
813839
814840 mod consume_clojure_whitespaces_tests {
0 commit comments