99// except according to those terms.
1010use self :: WhichLine :: * ;
1111
12- use std:: ascii:: AsciiExt ;
1312use std:: io:: { BufferedReader , File } ;
14- use regex:: Regex ;
1513
1614pub struct ExpectedError {
1715 pub line : uint ,
1816 pub kind : String ,
1917 pub msg : String ,
2018}
2119
20+ #[ derive( PartialEq , Show ) ]
21+ enum WhichLine { ThisLine , FollowPrevious ( uint ) , AdjustBackward ( uint ) }
22+
2223/// Looks for either "//~| KIND MESSAGE" or "//~^^... KIND MESSAGE"
2324/// The former is a "follow" that inherits its target from the preceding line;
2425/// the latter is an "adjusts" that goes that many lines up.
2526///
2627/// Goal is to enable tests both like: //~^^^ ERROR go up three
2728/// and also //~^ ERROR message one for the preceding line, and
2829/// //~| 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-
3630// 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 > {
3832 let mut rdr = BufferedReader :: new ( File :: open ( testfile) . unwrap ( ) ) ;
3933
4034 // `last_nonfollow_error` tracks the most recently seen
@@ -50,7 +44,7 @@ pub fn load_errors(re: &Regex, testfile: &Path) -> Vec<ExpectedError> {
5044 rdr. lines ( ) . enumerate ( ) . filter_map ( |( line_no, ln) | {
5145 parse_expected ( last_nonfollow_error,
5246 line_no + 1 ,
53- ln. unwrap ( ) . as_slice ( ) , re )
47+ ln. unwrap ( ) . as_slice ( ) )
5448 . map ( |( which, error) | {
5549 match which {
5650 FollowPrevious ( _) => { }
@@ -63,30 +57,39 @@ pub fn load_errors(re: &Regex, testfile: &Path) -> Vec<ExpectedError> {
6357
6458fn parse_expected ( last_nonfollow_error : Option < uint > ,
6559 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 ( ) ;
7377
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+ } ;
8690
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, } ) )
9295}
0 commit comments