@@ -297,7 +297,7 @@ impl<'t, 'p> Visitor for TranslatorI<'t, 'p> {
297
297
}
298
298
Ast :: Class ( ast:: Class :: Perl ( ref x) ) => {
299
299
if self . flags ( ) . unicode ( ) {
300
- let cls = self . hir_perl_unicode_class ( x) ;
300
+ let cls = self . hir_perl_unicode_class ( x) ? ;
301
301
let hcls = hir:: Class :: Unicode ( cls) ;
302
302
self . push ( HirFrame :: Expr ( Hir :: class ( hcls) ) ) ;
303
303
} else {
@@ -450,7 +450,7 @@ impl<'t, 'p> Visitor for TranslatorI<'t, 'p> {
450
450
}
451
451
ast:: ClassSetItem :: Perl ( ref x) => {
452
452
if self . flags ( ) . unicode ( ) {
453
- let xcls = self . hir_perl_unicode_class ( x) ;
453
+ let xcls = self . hir_perl_unicode_class ( x) ? ;
454
454
let mut cls = self . pop ( ) . unwrap ( ) . unwrap_class_unicode ( ) ;
455
455
cls. union ( & xcls) ;
456
456
self . push ( HirFrame :: ClassUnicode ( cls) ) ;
@@ -800,47 +800,36 @@ impl<'t, 'p> TranslatorI<'t, 'p> {
800
800
property_value : value,
801
801
} ,
802
802
} ;
803
- match unicode:: class ( query) {
804
- Ok ( mut class) => {
805
- self . unicode_fold_and_negate ( ast_class. negated , & mut class) ;
806
- Ok ( class)
807
- }
808
- Err ( unicode:: Error :: PropertyNotFound ) => {
809
- Err ( self
810
- . error ( ast_class. span , ErrorKind :: UnicodePropertyNotFound ) )
811
- }
812
- Err ( unicode:: Error :: PropertyValueNotFound ) => Err ( self . error (
813
- ast_class. span ,
814
- ErrorKind :: UnicodePropertyValueNotFound ,
815
- ) ) ,
803
+ let mut result = self . convert_unicode_class_error (
804
+ & ast_class. span ,
805
+ unicode:: class ( query) ,
806
+ ) ;
807
+ if let Ok ( ref mut class) = result {
808
+ self . unicode_fold_and_negate ( ast_class. negated , class) ;
816
809
}
810
+ result
817
811
}
818
812
819
813
fn hir_perl_unicode_class (
820
814
& self ,
821
815
ast_class : & ast:: ClassPerl ,
822
- ) -> hir:: ClassUnicode {
816
+ ) -> Result < hir:: ClassUnicode > {
823
817
use ast:: ClassPerlKind :: * ;
824
- use unicode_tables:: perl_word:: PERL_WORD ;
825
818
826
819
assert ! ( self . flags( ) . unicode( ) ) ;
827
- let mut class = match ast_class. kind {
828
- Digit => {
829
- let query = ClassQuery :: Binary ( "Decimal_Number" ) ;
830
- unicode:: class ( query) . unwrap ( )
831
- }
832
- Space => {
833
- let query = ClassQuery :: Binary ( "Whitespace" ) ;
834
- unicode:: class ( query) . unwrap ( )
835
- }
836
- Word => unicode:: hir_class ( PERL_WORD ) ,
820
+ let result = match ast_class. kind {
821
+ Digit => unicode:: perl_digit ( ) ,
822
+ Space => unicode:: perl_space ( ) ,
823
+ Word => unicode:: perl_word ( ) ,
837
824
} ;
825
+ let mut class =
826
+ self . convert_unicode_class_error ( & ast_class. span , result) ?;
838
827
// We needn't apply case folding here because the Perl Unicode classes
839
828
// are already closed under Unicode simple case folding.
840
829
if ast_class. negated {
841
830
class. negate ( ) ;
842
831
}
843
- class
832
+ Ok ( class)
844
833
}
845
834
846
835
fn hir_perl_byte_class (
@@ -863,6 +852,28 @@ impl<'t, 'p> TranslatorI<'t, 'p> {
863
852
class
864
853
}
865
854
855
+ /// Converts the given Unicode specific error to an HIR translation error.
856
+ ///
857
+ /// The span given should approximate the position at which an error would
858
+ /// occur.
859
+ fn convert_unicode_class_error (
860
+ & self ,
861
+ span : & Span ,
862
+ result : unicode:: Result < hir:: ClassUnicode > ,
863
+ ) -> Result < hir:: ClassUnicode > {
864
+ result. map_err ( |err| {
865
+ let sp = span. clone ( ) ;
866
+ match err {
867
+ unicode:: Error :: PropertyNotFound => {
868
+ self . error ( sp, ErrorKind :: UnicodePropertyNotFound )
869
+ }
870
+ unicode:: Error :: PropertyValueNotFound => {
871
+ self . error ( sp, ErrorKind :: UnicodePropertyValueNotFound )
872
+ }
873
+ }
874
+ } )
875
+ }
876
+
866
877
fn unicode_fold_and_negate (
867
878
& self ,
868
879
negated : bool ,
@@ -1017,74 +1028,28 @@ fn hir_ascii_class_bytes(kind: &ast::ClassAsciiKind) -> hir::ClassBytes {
1017
1028
1018
1029
fn ascii_class ( kind : & ast:: ClassAsciiKind ) -> & ' static [ ( char , char ) ] {
1019
1030
use ast:: ClassAsciiKind :: * ;
1020
-
1021
- // The contortions below with `const` appear necessary for older versions
1022
- // of Rust.
1023
- type T = & ' static [ ( char , char ) ] ;
1024
1031
match * kind {
1025
- Alnum => {
1026
- const X : T = & [ ( '0' , '9' ) , ( 'A' , 'Z' ) , ( 'a' , 'z' ) ] ;
1027
- X
1028
- }
1029
- Alpha => {
1030
- const X : T = & [ ( 'A' , 'Z' ) , ( 'a' , 'z' ) ] ;
1031
- X
1032
- }
1033
- Ascii => {
1034
- const X : T = & [ ( '\x00' , '\x7F' ) ] ;
1035
- X
1036
- }
1037
- Blank => {
1038
- const X : T = & [ ( '\t' , '\t' ) , ( ' ' , ' ' ) ] ;
1039
- X
1040
- }
1041
- Cntrl => {
1042
- const X : T = & [ ( '\x00' , '\x1F' ) , ( '\x7F' , '\x7F' ) ] ;
1043
- X
1044
- }
1045
- Digit => {
1046
- const X : T = & [ ( '0' , '9' ) ] ;
1047
- X
1048
- }
1049
- Graph => {
1050
- const X : T = & [ ( '!' , '~' ) ] ;
1051
- X
1052
- }
1053
- Lower => {
1054
- const X : T = & [ ( 'a' , 'z' ) ] ;
1055
- X
1056
- }
1057
- Print => {
1058
- const X : T = & [ ( ' ' , '~' ) ] ;
1059
- X
1060
- }
1061
- Punct => {
1062
- const X : T = & [ ( '!' , '/' ) , ( ':' , '@' ) , ( '[' , '`' ) , ( '{' , '~' ) ] ;
1063
- X
1064
- }
1065
- Space => {
1066
- const X : T = & [
1067
- ( '\t' , '\t' ) ,
1068
- ( '\n' , '\n' ) ,
1069
- ( '\x0B' , '\x0B' ) ,
1070
- ( '\x0C' , '\x0C' ) ,
1071
- ( '\r' , '\r' ) ,
1072
- ( ' ' , ' ' ) ,
1073
- ] ;
1074
- X
1075
- }
1076
- Upper => {
1077
- const X : T = & [ ( 'A' , 'Z' ) ] ;
1078
- X
1079
- }
1080
- Word => {
1081
- const X : T = & [ ( '0' , '9' ) , ( 'A' , 'Z' ) , ( '_' , '_' ) , ( 'a' , 'z' ) ] ;
1082
- X
1083
- }
1084
- Xdigit => {
1085
- const X : T = & [ ( '0' , '9' ) , ( 'A' , 'F' ) , ( 'a' , 'f' ) ] ;
1086
- X
1087
- }
1032
+ Alnum => & [ ( '0' , '9' ) , ( 'A' , 'Z' ) , ( 'a' , 'z' ) ] ,
1033
+ Alpha => & [ ( 'A' , 'Z' ) , ( 'a' , 'z' ) ] ,
1034
+ Ascii => & [ ( '\x00' , '\x7F' ) ] ,
1035
+ Blank => & [ ( '\t' , '\t' ) , ( ' ' , ' ' ) ] ,
1036
+ Cntrl => & [ ( '\x00' , '\x1F' ) , ( '\x7F' , '\x7F' ) ] ,
1037
+ Digit => & [ ( '0' , '9' ) ] ,
1038
+ Graph => & [ ( '!' , '~' ) ] ,
1039
+ Lower => & [ ( 'a' , 'z' ) ] ,
1040
+ Print => & [ ( ' ' , '~' ) ] ,
1041
+ Punct => & [ ( '!' , '/' ) , ( ':' , '@' ) , ( '[' , '`' ) , ( '{' , '~' ) ] ,
1042
+ Space => & [
1043
+ ( '\t' , '\t' ) ,
1044
+ ( '\n' , '\n' ) ,
1045
+ ( '\x0B' , '\x0B' ) ,
1046
+ ( '\x0C' , '\x0C' ) ,
1047
+ ( '\r' , '\r' ) ,
1048
+ ( ' ' , ' ' ) ,
1049
+ ] ,
1050
+ Upper => & [ ( 'A' , 'Z' ) ] ,
1051
+ Word => & [ ( '0' , '9' ) , ( 'A' , 'Z' ) , ( '_' , '_' ) , ( 'a' , 'z' ) ] ,
1052
+ Xdigit => & [ ( '0' , '9' ) , ( 'A' , 'F' ) , ( 'a' , 'f' ) ] ,
1088
1053
}
1089
1054
}
1090
1055
0 commit comments