1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
extern crate regex;
use regex::Regex;
const WHITESPACE: char = ' ';
const NEWLINE: char = '\n';
const FIRST_LINE: usize = 0;
pub fn get_error_text_with_error_code(s: String) -> Vec<String> {
get_error_text(s, true)
}
pub fn get_error_text_without_error_code(s: String) -> Vec<String> {
get_error_text(s, false)
}
fn get_error_text(s: String, with_error: bool) -> Vec<String> {
let mut descriptions: Vec<String> = Vec::new();
let errors = structure_compiler_output(s);
for entry in errors.into_iter() {
let temp: Vec<&str> = entry.split(NEWLINE).collect();
descriptions.push(cut_out(String::from(temp[FIRST_LINE]), with_error));
}
descriptions
}
pub fn structure_compiler_output(output: String) -> Vec<String> {
let mut errors: Vec<String> = Vec::new();
let re = Regex::new(r"(?m)error\[E\d{4}\]:").unwrap();
for entry in output.split("\n\n") {
let stringfied_entry = String::from(entry);
if re.is_match(stringfied_entry.as_str()) {
errors.push(stringfied_entry);
}
}
if errors.is_empty() && re.is_match(output.as_str()) {
return vec![output];
} else {
errors
}
}
fn cut_out(i: String, with_error: bool) -> String {
let mut erg = String::new();
let re = Regex::new(r"(?m)<(?m).*>").unwrap();
let p = re.replace_all(i.as_str(), "");
let mut words = p.split_whitespace().collect::<Vec<&str>>();
let mut error: String = String::from("");
words.pop();
error.push_str(words.remove(0));
error.push(WHITESPACE);
for s in words.into_iter() {
let mut word = s.chars()
.filter(|c| (*c).is_alphabetic())
.collect::<String>();
if !word.is_empty() {
erg.push_str(String::from(word).trim());
erg.push(WHITESPACE);
}
}
if with_error {
return {
let mut x = error;
x.push_str(erg.as_str());
String::from(x.trim())
};
} else {
String::from(erg.trim())
}
}