@@ -43,6 +43,12 @@ use nom::error::ErrorKind;
4343// Our 'try readers' are a bit higher level, and are specifically supposed to be returning a valid // value::Value or some sort of failure.
4444//
4545
46+ ////////////////////////////////////////////////////////////////////////////////////////////////////
47+ //
48+ // Utils
49+ //
50+ ////////////////////////////////////////////////////////////////////////////////////////////////////
51+
4652/// Returns the first character of a string slice.
4753///
4854/// If `input` is not empty, then its first char will be returned. Otherwise,
@@ -67,6 +73,15 @@ fn cons_str(head: char, tail: &str) -> String {
6773
6874 ret
6975}
76+ ////////////////////////////////////////////////////////////////////////////////////////////////////
77+ // End Utils
78+ ////////////////////////////////////////////////////////////////////////////////////////////////////
79+
80+ ////////////////////////////////////////////////////////////////////////////////////////////////////
81+ //
82+ // Predicates
83+ //
84+ ////////////////////////////////////////////////////////////////////////////////////////////////////
7085
7186/// Returns whether if a character can be in the tail of an identifier.
7287///
@@ -128,6 +143,30 @@ fn is_period_char(chr: char) -> bool {
128143 chr == '.'
129144}
130145
146+ /// Returns whether if a given character is a whitespace.
147+ ///
148+ /// Clojure defines a whitespace as either a comma or an unicode whitespace.
149+ fn is_clojure_whitespace ( c : char ) -> bool {
150+ c. is_whitespace ( ) || c == ','
151+ }
152+ ////////////////////////////////////////////////////////////////////////////////////////////////////
153+ // End predicates
154+ ////////////////////////////////////////////////////////////////////////////////////////////////////
155+
156+ ///////////////////////////////////////////////////////////////////////////////////////////////////////
157+ //
158+ // Parsers
159+ //
160+ //////////////////////////////////////////////////////////////////////////////////////////////////////
161+
162+ /// Consumes any whitespace from input, if there is any.
163+ /// Always succeeds.
164+ ///
165+ /// A whitespace is either an ASCII whitespace or a comma.
166+ fn consume_clojure_whitespaces_parser ( input : & str ) -> IResult < & str , ( ) > {
167+ named ! ( parser<& str , & str >, take_while!( is_clojure_whitespace) ) ;
168+ parser ( input) . map ( |( rest, _) | ( rest, ( ) ) )
169+ }
131170
132171/// Parses valid Clojure identifiers
133172/// Example Successes: ab, cat, -12+3, |blah|, <well>
@@ -217,6 +256,15 @@ pub fn to_value_parser<I, O: ToValue>(
217256) -> impl Fn ( I ) -> IResult < I , Value > {
218257 move |input : I | parser ( input) . map ( |( rest_input, thing) | ( rest_input, thing. to_value ( ) ) )
219258}
259+ ////////////////////////////////////////////////////////////////////////////////////////////////////
260+ // End Parsers
261+ ////////////////////////////////////////////////////////////////////////////////////////////////////
262+
263+ ////////////////////////////////////////////////////////////////////////////////////////////////////
264+ //
265+ // Try-Readers
266+ //
267+ ////////////////////////////////////////////////////////////////////////////////////////////////////
220268
221269// @TODO make sure whitespace or 'nothing' is at the end, fail for
222270// float like numbers
@@ -261,7 +309,7 @@ pub fn try_read_f64(input: &str) -> IResult<&str, Value> {
261309/// Example Failures:
262310/// :12 :'a
263311pub fn try_read_keyword ( input : & str ) -> IResult < & str , Value > {
264- named ! ( keyword_colon<& str , & str >, preceded!( consume_clojure_whitespaces , tag!( ":" ) ) ) ;
312+ named ! ( keyword_colon<& str , & str >, preceded!( consume_clojure_whitespaces_parser , tag!( ":" ) ) ) ;
265313
266314 let ( rest_input, _) = keyword_colon ( input) ?;
267315 let ( rest_input, symbol) = symbol_parser ( rest_input) ?;
@@ -286,7 +334,7 @@ pub fn try_read_symbol(input: &str) -> IResult<&str, Value> {
286334/// Example Successes:
287335/// "this is pretty straightforward" => Value::String("this is pretty straightforward")
288336pub fn try_read_string ( input : & str ) -> IResult < & str , Value > {
289- named ! ( quotation<& str , & str >, preceded!( consume_clojure_whitespaces , tag!( "\" " ) ) ) ;
337+ named ! ( quotation<& str , & str >, preceded!( consume_clojure_whitespaces_parser , tag!( "\" " ) ) ) ;
290338
291339 let ( rest_input, _) = quotation ( input) ?;
292340
@@ -306,8 +354,8 @@ pub fn try_read_string(input: &str) -> IResult<&str, Value> {
306354/// Example Successes:
307355/// {:a 1} => Value::PersistentListMap {PersistentListMap { MapEntry { :a, 1} .. ]})
308356pub fn try_read_map ( input : & str ) -> IResult < & str , Value > {
309- named ! ( lbracep<& str , & str >, preceded!( consume_clojure_whitespaces , tag!( "{" ) ) ) ;
310- named ! ( rbracep<& str , & str >, preceded!( consume_clojure_whitespaces , tag!( "}" ) ) ) ;
357+ named ! ( lbracep<& str , & str >, preceded!( consume_clojure_whitespaces_parser , tag!( "{" ) ) ) ;
358+ named ! ( rbracep<& str , & str >, preceded!( consume_clojure_whitespaces_parser , tag!( "}" ) ) ) ;
311359 let ( map_inner_input, _) = lbracep ( input) ?;
312360 let mut map_as_vec: Vec < MapEntry > = Vec :: new ( ) ;
313361 let mut rest_input = map_inner_input;
@@ -333,8 +381,8 @@ pub fn try_read_map(input: &str) -> IResult<&str, Value> {
333381/// [1 2 [5 10 15] 3]
334382/// => Value::PersistentVector(PersistentVector { vals: [Rc(Value::I32(1) .. Rc(Value::PersistentVector..)]})
335383pub fn try_read_vector ( input : & str ) -> IResult < & str , Value > {
336- named ! ( lbracketp<& str , & str >, preceded!( consume_clojure_whitespaces , tag!( "[" ) ) ) ;
337- named ! ( rbracketp<& str , & str >, preceded!( consume_clojure_whitespaces , tag!( "]" ) ) ) ;
384+ named ! ( lbracketp<& str , & str >, preceded!( consume_clojure_whitespaces_parser , tag!( "[" ) ) ) ;
385+ named ! ( rbracketp<& str , & str >, preceded!( consume_clojure_whitespaces_parser , tag!( "]" ) ) ) ;
338386 let ( vector_inner_input, _) = lbracketp ( input) ?;
339387 let mut vector_as_vec = Vec :: new ( ) ;
340388 // What's left of our input as we read more of our PersistentVector
@@ -354,8 +402,8 @@ pub fn try_read_vector(input: &str) -> IResult<&str, Value> {
354402}
355403
356404pub fn try_read_list ( input : & str ) -> IResult < & str , Value > {
357- named ! ( lparenp<& str , & str >, preceded!( consume_clojure_whitespaces , tag!( "(" ) ) ) ;
358- named ! ( rparenp<& str , & str >, preceded!( consume_clojure_whitespaces , tag!( ")" ) ) ) ;
405+ named ! ( lparenp<& str , & str >, preceded!( consume_clojure_whitespaces_parser , tag!( "(" ) ) ) ;
406+ named ! ( rparenp<& str , & str >, preceded!( consume_clojure_whitespaces_parser , tag!( ")" ) ) ) ;
359407
360408 let ( list_inner_input, _) = lparenp ( input) ?;
361409 let mut list_as_vec = Vec :: new ( ) ;
@@ -372,7 +420,7 @@ pub fn try_read_list(input: &str) -> IResult<&str, Value> {
372420
373421pub fn try_read ( input : & str ) -> IResult < & str , Value > {
374422 preceded (
375- consume_clojure_whitespaces ,
423+ consume_clojure_whitespaces_parser ,
376424 alt ( (
377425 try_read_map,
378426 try_read_string,
@@ -386,15 +434,15 @@ pub fn try_read(input: &str) -> IResult<&str, Value> {
386434 ) ) ,
387435 ) ( input)
388436}
437+ ////////////////////////////////////////////////////////////////////////////////////////////////////
438+ // End Try-Readers
439+ ////////////////////////////////////////////////////////////////////////////////////////////////////
389440
390- pub fn debug_try_read ( input : & str ) -> IResult < & str , Value > {
391- let reading = try_read ( input) ;
392- match & reading {
393- Ok ( ( _, value) ) => println ! ( "Reading: {}" , value) ,
394- _ => println ! ( "Reading: {:?}" , reading) ,
395- } ;
396- reading
397- }
441+ ////////////////////////////////////////////////////////////////////////////////////////////////////
442+ //
443+ // Readers
444+ //
445+ ///////////////////////////////////////////////////////////////////////////////////////////////////
398446
399447// This is the high level read function that Clojure RS wraps
400448pub fn read < R : BufRead > ( reader : & mut R ) -> Value {
@@ -427,22 +475,6 @@ pub fn read<R: BufRead>(reader: &mut R) -> Value {
427475 }
428476}
429477
430- /// Consumes any whitespace from input, if there is any.
431- /// Always succeeds.
432- ///
433- /// A whitespace is either an ASCII whitespace or a comma.
434- fn consume_clojure_whitespaces ( input : & str ) -> IResult < & str , ( ) > {
435- named ! ( parser<& str , & str >, take_while!( is_clojure_whitespace) ) ;
436- parser ( input) . map ( |( rest, _) | ( rest, ( ) ) )
437- }
438-
439- /// Returns whether if a given character is a whitespace.
440- ///
441- /// Clojure defines a whitespace as either a comma or an unicode whitespace.
442- fn is_clojure_whitespace ( c : char ) -> bool {
443- c. is_whitespace ( ) || c == ','
444- }
445-
446478#[ cfg( test) ]
447479mod tests {
448480
@@ -713,24 +745,24 @@ mod tests {
713745 }
714746
715747 mod consume_clojure_whitespaces_tests {
716- use crate :: reader:: consume_clojure_whitespaces ;
748+ use crate :: reader:: consume_clojure_whitespaces_parser ;
717749 #[ test]
718750 fn consume_whitespaces_from_input ( ) {
719751 let s = ", ,, ,1, 2, 3, 4 5,,6 " ;
720752 assert_eq ! (
721753 Some ( ( "1, 2, 3, 4 5,,6 " , ( ) ) ) ,
722- consume_clojure_whitespaces ( & s) . ok( )
754+ consume_clojure_whitespaces_parser ( & s) . ok( )
723755 ) ;
724756 }
725757 #[ test]
726758 fn consume_whitespaces_from_empty_input ( ) {
727759 let s = "" ;
728- assert_eq ! ( None , consume_clojure_whitespaces ( & s) . ok( ) ) ;
760+ assert_eq ! ( None , consume_clojure_whitespaces_parser ( & s) . ok( ) ) ;
729761 }
730762 #[ test]
731763 fn consume_whitespaces_from_input_no_whitespace ( ) {
732764 let s = "1, 2, 3" ;
733- assert_eq ! ( Some ( ( "1, 2, 3" , ( ) ) ) , consume_clojure_whitespaces ( & s) . ok( ) ) ;
765+ assert_eq ! ( Some ( ( "1, 2, 3" , ( ) ) ) , consume_clojure_whitespaces_parser ( & s) . ok( ) ) ;
734766 }
735767 }
736768
0 commit comments