|
1 |
| -// Copyright 2012 The Rust Project Developers. See the COPYRIGHT |
| 1 | +// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT |
2 | 2 | // file at the top-level directory of this distribution and at
|
3 | 3 | // http://rust-lang.org/COPYRIGHT.
|
4 | 4 | //
|
|
9 | 9 | // except according to those terms.
|
10 | 10 |
|
11 | 11 | use std::io::{BufferedReader, File};
|
| 12 | +use regex::Regex; |
12 | 13 |
|
13 | 14 | pub struct ExpectedError {
|
14 | 15 | pub line: uint,
|
15 | 16 | pub kind: StrBuf,
|
16 | 17 | pub msg: StrBuf,
|
17 | 18 | }
|
18 | 19 |
|
19 |
| -// Load any test directives embedded in the file |
20 |
| -pub fn load_errors(testfile: &Path) -> Vec<ExpectedError> { |
| 20 | +pub static EXPECTED_PATTERN : &'static str = r"//~(?P<adjusts>\^*)\s*(?P<kind>\S*)\s*(?P<msg>.*)"; |
21 | 21 |
|
22 |
| - let mut error_patterns = Vec::new(); |
| 22 | +// Load any test directives embedded in the file |
| 23 | +pub fn load_errors(re: &Regex, testfile: &Path) -> Vec<ExpectedError> { |
23 | 24 | let mut rdr = BufferedReader::new(File::open(testfile).unwrap());
|
24 |
| - let mut line_num = 1u; |
25 |
| - for ln in rdr.lines() { |
26 |
| - error_patterns.push_all_move(parse_expected(line_num, |
27 |
| - ln.unwrap().to_strbuf())); |
28 |
| - line_num += 1u; |
29 |
| - } |
30 |
| - return error_patterns; |
31 |
| -} |
32 |
| - |
33 |
| -fn parse_expected(line_num: uint, line: StrBuf) -> Vec<ExpectedError> { |
34 |
| - let line = line.as_slice().trim().to_strbuf(); |
35 |
| - let error_tag = "//~".to_strbuf(); |
36 |
| - let mut idx; |
37 |
| - match line.as_slice().find_str(error_tag.as_slice()) { |
38 |
| - None => return Vec::new(), |
39 |
| - Some(nn) => { idx = (nn as uint) + error_tag.len(); } |
40 |
| - } |
41 |
| - |
42 |
| - // "//~^^^ kind msg" denotes a message expected |
43 |
| - // three lines above current line: |
44 |
| - let mut adjust_line = 0u; |
45 |
| - let len = line.len(); |
46 |
| - while idx < len && line.as_slice()[idx] == ('^' as u8) { |
47 |
| - adjust_line += 1u; |
48 |
| - idx += 1u; |
49 |
| - } |
50 | 25 |
|
51 |
| - // Extract kind: |
52 |
| - while idx < len && line.as_slice()[idx] == (' ' as u8) { |
53 |
| - idx += 1u; |
54 |
| - } |
55 |
| - let start_kind = idx; |
56 |
| - while idx < len && line.as_slice()[idx] != (' ' as u8) { |
57 |
| - idx += 1u; |
58 |
| - } |
59 |
| - |
60 |
| - let kind = line.as_slice().slice(start_kind, idx); |
61 |
| - let kind = kind.to_ascii().to_lower().into_str().to_strbuf(); |
62 |
| - |
63 |
| - // Extract msg: |
64 |
| - while idx < len && line.as_slice()[idx] == (' ' as u8) { |
65 |
| - idx += 1u; |
66 |
| - } |
67 |
| - let msg = line.as_slice().slice(idx, len).to_strbuf(); |
68 |
| - |
69 |
| - debug!("line={} kind={} msg={}", line_num - adjust_line, kind, msg); |
| 26 | + rdr.lines().enumerate().filter_map(|(line_no, ln)| { |
| 27 | + parse_expected(line_no + 1, ln.unwrap(), re) |
| 28 | + }).collect() |
| 29 | +} |
70 | 30 |
|
71 |
| - return vec!(ExpectedError{ |
72 |
| - line: line_num - adjust_line, |
73 |
| - kind: kind, |
74 |
| - msg: msg, |
75 |
| - }); |
| 31 | +fn parse_expected(line_num: uint, line: &str, re: &Regex) -> Option<ExpectedError> { |
| 32 | + re.captures(line).and_then(|caps| { |
| 33 | + let adjusts = caps.name("adjusts").len(); |
| 34 | + let kind = caps.name("kind").to_ascii().to_lower().into_str().to_strbuf(); |
| 35 | + let msg = caps.name("msg").trim().to_strbuf(); |
| 36 | + |
| 37 | + debug!("line={} kind={} msg={}", line_num, kind, msg); |
| 38 | + Some(ExpectedError { |
| 39 | + line: line_num - adjusts, |
| 40 | + kind: kind, |
| 41 | + msg: msg, |
| 42 | + }) |
| 43 | + }) |
76 | 44 | }
|
0 commit comments