9
9
// except according to those terms.
10
10
use self :: WhichLine :: * ;
11
11
12
- use std:: ascii:: AsciiExt ;
13
12
use std:: io:: { BufferedReader , File } ;
14
- use regex:: Regex ;
15
13
16
14
pub struct ExpectedError {
17
15
pub line : uint ,
18
16
pub kind : String ,
19
17
pub msg : String ,
20
18
}
21
19
20
+ #[ derive( PartialEq , Show ) ]
21
+ enum WhichLine { ThisLine , FollowPrevious ( uint ) , AdjustBackward ( uint ) }
22
+
22
23
/// Looks for either "//~| KIND MESSAGE" or "//~^^... KIND MESSAGE"
23
24
/// The former is a "follow" that inherits its target from the preceding line;
24
25
/// the latter is an "adjusts" that goes that many lines up.
25
26
///
26
27
/// Goal is to enable tests both like: //~^^^ ERROR go up three
27
28
/// and also //~^ ERROR message one for the preceding line, and
28
29
/// //~| ERROR message two for that same line.
29
-
30
- pub static EXPECTED_PATTERN : & ' static str =
31
- r"//~(?P<follow>\|)?(?P<adjusts>\^*)\s*(?P<kind>\S*)\s*(?P<msg>.*)" ;
32
-
33
- #[ derive( PartialEq , Show ) ]
34
- enum WhichLine { ThisLine , FollowPrevious ( uint ) , AdjustBackward ( uint ) }
35
-
36
30
// Load any test directives embedded in the file
37
- pub fn load_errors ( re : & Regex , testfile : & Path ) -> Vec < ExpectedError > {
31
+ pub fn load_errors ( testfile : & Path ) -> Vec < ExpectedError > {
38
32
let mut rdr = BufferedReader :: new ( File :: open ( testfile) . unwrap ( ) ) ;
39
33
40
34
// `last_nonfollow_error` tracks the most recently seen
@@ -50,7 +44,7 @@ pub fn load_errors(re: &Regex, testfile: &Path) -> Vec<ExpectedError> {
50
44
rdr. lines ( ) . enumerate ( ) . filter_map ( |( line_no, ln) | {
51
45
parse_expected ( last_nonfollow_error,
52
46
line_no + 1 ,
53
- ln. unwrap ( ) . as_slice ( ) , re )
47
+ ln. unwrap ( ) . as_slice ( ) )
54
48
. map ( |( which, error) | {
55
49
match which {
56
50
FollowPrevious ( _) => { }
@@ -63,30 +57,39 @@ pub fn load_errors(re: &Regex, testfile: &Path) -> Vec<ExpectedError> {
63
57
64
58
fn parse_expected ( last_nonfollow_error : Option < uint > ,
65
59
line_num : uint ,
66
- line : & str ,
67
- re : & Regex ) -> Option < ( WhichLine , ExpectedError ) > {
68
- re. captures ( line) . and_then ( |caps| {
69
- let adjusts = caps. name ( "adjusts" ) . unwrap_or ( "" ) . len ( ) ;
70
- let kind = caps. name ( "kind" ) . unwrap_or ( "" ) . to_ascii_lowercase ( ) ;
71
- let msg = caps. name ( "msg" ) . unwrap_or ( "" ) . trim ( ) . to_string ( ) ;
72
- let follow = caps. name ( "follow" ) . unwrap_or ( "" ) . len ( ) > 0 ;
60
+ line : & str ) -> Option < ( WhichLine , ExpectedError ) > {
61
+ let start = match line. find_str ( "//~" ) { Some ( i) => i, None => return None } ;
62
+ let ( follow, adjusts) = if line. char_at ( start + 3 ) == '|' {
63
+ ( true , 0 )
64
+ } else {
65
+ ( false , line[ start + 3 ..] . chars ( ) . take_while ( |c| * c == '^' ) . count ( ) )
66
+ } ;
67
+ let kind_start = start + 3 + adjusts + ( follow as usize ) ;
68
+ let letters = line[ kind_start..] . chars ( ) ;
69
+ let kind = letters. skip_while ( |c| c. is_whitespace ( ) )
70
+ . take_while ( |c| !c. is_whitespace ( ) )
71
+ . map ( |c| c. to_lowercase ( ) )
72
+ . collect :: < String > ( ) ;
73
+ let letters = line[ kind_start..] . chars ( ) ;
74
+ let msg = letters. skip_while ( |c| c. is_whitespace ( ) )
75
+ . skip_while ( |c| !c. is_whitespace ( ) )
76
+ . collect :: < String > ( ) . trim ( ) . to_string ( ) ;
73
77
74
- let ( which, line) = if follow {
75
- assert ! ( adjusts == 0 , "use either //~| or //~^, not both." ) ;
76
- let line = last_nonfollow_error. unwrap_or_else ( || {
77
- panic ! ( "encountered //~| without preceding //~^ line." )
78
- } ) ;
79
- ( FollowPrevious ( line) , line)
80
- } else {
81
- let which =
82
- if adjusts > 0 { AdjustBackward ( adjusts) } else { ThisLine } ;
83
- let line = line_num - adjusts;
84
- ( which, line)
85
- } ;
78
+ let ( which, line) = if follow {
79
+ assert ! ( adjusts == 0 , "use either //~| or //~^, not both." ) ;
80
+ let line = last_nonfollow_error. unwrap_or_else ( || {
81
+ panic ! ( "encountered //~| without preceding //~^ line." )
82
+ } ) ;
83
+ ( FollowPrevious ( line) , line)
84
+ } else {
85
+ let which =
86
+ if adjusts > 0 { AdjustBackward ( adjusts) } else { ThisLine } ;
87
+ let line = line_num - adjusts;
88
+ ( which, line)
89
+ } ;
86
90
87
- debug ! ( "line={} which={:?} kind={:?} msg={:?}" , line_num, which, kind, msg) ;
88
- Some ( ( which, ExpectedError { line : line,
89
- kind : kind,
90
- msg : msg, } ) )
91
- } )
91
+ debug ! ( "line={} which={:?} kind={:?} msg={:?}" , line_num, which, kind, msg) ;
92
+ Some ( ( which, ExpectedError { line : line,
93
+ kind : kind,
94
+ msg : msg, } ) )
92
95
}
0 commit comments