10
10
11
11
use nom:: {
12
12
branch:: alt, bytes:: complete:: tag, map, sequence:: preceded, take_until, terminated,
13
- Err :: Incomplete , IResult , Needed ,
13
+ combinator :: opt , Err :: Incomplete , IResult , Needed ,
14
14
} ;
15
15
16
16
use crate :: keyword:: Keyword ;
@@ -104,7 +104,7 @@ fn cons_str(head: char, tail: &str) -> String {
104
104
/// - `*`,
105
105
/// - `!`,
106
106
fn is_identifier_char ( chr : char ) -> bool {
107
- chr. is_alphanumeric ( ) || "|?<>+-_=^%&$*!" . contains ( chr)
107
+ chr. is_alphanumeric ( ) || "|?<>+-_=^%&$*!. " . contains ( chr)
108
108
}
109
109
110
110
/// Returns whether if a character can be in the head of an identifier.
@@ -128,7 +128,7 @@ fn is_identifier_char(chr: char) -> bool {
128
128
/// - `*`,
129
129
/// - `!`,
130
130
fn is_non_numeric_identifier_char ( chr : char ) -> bool {
131
- chr. is_alphabetic ( ) || "|?<>+-_=^%&$*!" . contains ( chr)
131
+ chr. is_alphabetic ( ) || "|?<>+-_=^%&$*!. " . contains ( chr)
132
132
}
133
133
134
134
/// Returns true if given character is a minus character
@@ -192,9 +192,22 @@ pub fn identifier_parser(input: &str) -> IResult<&str, String> {
192
192
identifier ( input)
193
193
}
194
194
195
- /// Parses valid Clojure symbols, whose name is a valid identifier
195
+ /// Parses valid Clojure symbol
196
+ /// Example Successes: a , b , |ab123|
197
+ /// namespace.subnamespace/a cat/b a.b.c/|ab123|
196
198
pub fn symbol_parser ( input : & str ) -> IResult < & str , Symbol > {
197
- identifier_parser ( input) . map ( |( rest_input, name) | ( rest_input, Symbol :: intern ( & name) ) )
199
+ named ! ( namespace_parser <& str , String >,
200
+ do_parse!(
201
+ ns: identifier_parser >>
202
+ tag!( "/" ) >>
203
+ ( ns) ) ) ;
204
+
205
+ let ( rest_input, ns) = opt ( namespace_parser) ( input) ?;
206
+ let ( rest_input, name) = identifier_parser ( rest_input) ?;
207
+ match ns {
208
+ Some ( ns) => Ok ( ( rest_input, Symbol :: intern_with_ns ( & ns, & name) ) ) ,
209
+ None => Ok ( ( rest_input, Symbol :: intern ( & name) ) )
210
+ }
198
211
}
199
212
200
213
/// Parses valid integers
@@ -549,6 +562,7 @@ mod tests {
549
562
}
550
563
551
564
mod symbol_parser_tests {
565
+ use crate :: reader:: try_read_symbol;
552
566
use crate :: reader:: symbol_parser;
553
567
use crate :: symbol:: Symbol ;
554
568
@@ -557,9 +571,7 @@ mod tests {
557
571
assert_eq ! (
558
572
Some ( (
559
573
" this" ,
560
- Symbol {
561
- name: String :: from( "input->output?" )
562
- }
574
+ Symbol :: intern( "input->output?" )
563
575
) ) ,
564
576
symbol_parser( "input->output? this" ) . ok( )
565
577
) ;
@@ -574,6 +586,21 @@ mod tests {
574
586
fn identifier_parser_does_not_parse_empty_input ( ) {
575
587
assert_eq ! ( None , symbol_parser( "" ) . ok( ) ) ;
576
588
}
589
+
590
+ #[ test]
591
+ fn symbol_parser_normal_symbol_test ( ) {
592
+ assert_eq ! (
593
+ Symbol :: intern( "a" ) ,
594
+ symbol_parser( "a " ) . ok( ) . unwrap( ) . 1
595
+ ) ;
596
+ }
597
+ #[ test]
598
+ fn symbol_parser_namespace_qualified_symbol_test ( ) {
599
+ assert_eq ! (
600
+ Symbol :: intern_with_ns( "clojure.core" , "a" ) ,
601
+ symbol_parser( "clojure.core/a " ) . ok( ) . unwrap( ) . 1
602
+ ) ;
603
+ }
577
604
}
578
605
579
606
mod double_parser_tests {
@@ -653,9 +680,7 @@ mod tests {
653
680
#[ test]
654
681
fn try_read_minus_as_valid_symbol_test ( ) {
655
682
assert_eq ! (
656
- Value :: Symbol ( Symbol {
657
- name: String :: from( "-" )
658
- } ) ,
683
+ Value :: Symbol ( Symbol :: intern( "-" ) ) ,
659
684
try_read_symbol( "- " ) . unwrap( ) . 1
660
685
) ;
661
686
}
@@ -671,7 +696,7 @@ mod tests {
671
696
use crate :: value:: Value ;
672
697
use crate :: value:: Value :: { PersistentList , PersistentListMap , PersistentVector } ;
673
698
use std:: rc:: Rc ;
674
-
699
+
675
700
#[ test]
676
701
fn try_read_empty_map_test ( ) {
677
702
assert_eq ! (
@@ -706,29 +731,23 @@ mod tests {
706
731
#[ test]
707
732
fn try_read_valid_symbol_test ( ) {
708
733
assert_eq ! (
709
- Value :: Symbol ( Symbol {
710
- name: String :: from( "my-symbol" )
711
- } ) ,
734
+ Value :: Symbol ( Symbol :: intern( "my-symbol" ) ) ,
712
735
try_read( "my-symbol " ) . ok( ) . unwrap( ) . 1
713
736
) ;
714
737
}
715
738
716
739
#[ test]
717
740
fn try_read_minus_as_valid_symbol_test ( ) {
718
741
assert_eq ! (
719
- Value :: Symbol ( Symbol {
720
- name: String :: from( "-" )
721
- } ) ,
742
+ Value :: Symbol ( Symbol :: intern( "-" ) ) ,
722
743
try_read( "- " ) . ok( ) . unwrap( ) . 1
723
744
) ;
724
745
}
725
746
726
747
#[ test]
727
748
fn try_read_minus_prefixed_as_valid_symbol_test ( ) {
728
749
assert_eq ! (
729
- Value :: Symbol ( Symbol {
730
- name: String :: from( "-prefixed" )
731
- } ) ,
750
+ Value :: Symbol ( Symbol :: intern( "-prefixed" ) ) ,
732
751
try_read( "-prefixed " ) . ok( ) . unwrap( ) . 1
733
752
) ;
734
753
}
0 commit comments