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
122
123
124
125
126
127
use super::position::{File, FilePos, Pos};
use std::cell::{Ref, RefCell};
use std::fmt;
use std::rc::Rc;
#[derive(Clone, Debug)]
pub struct Error {
pub pos: FilePos,
pub msg: String,
pub soft: bool,
pub by_parser: bool, order: usize, }
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let p = if self.by_parser { "[Parser]" } else { "[TC]" };
write!(f, "{} {} {}\n", p, self.pos, self.msg)?;
Ok(())
}
}
impl std::error::Error for Error {}
#[derive(Clone, Debug)]
pub struct ErrorList {
errors: Rc<RefCell<Vec<Error>>>,
}
impl fmt::Display for ErrorList {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Result: {} errors\n", self.errors.borrow().len())?;
for e in self.errors.borrow().iter() {
e.fmt(f)?;
}
Ok(())
}
}
impl std::error::Error for ErrorList {}
impl ErrorList {
pub fn new() -> ErrorList {
ErrorList {
errors: Rc::new(RefCell::new(vec![])),
}
}
pub fn add(&self, p: Option<FilePos>, msg: String, soft: bool, by_parser: bool) {
let fp = p.unwrap_or(FilePos::null());
let order = if msg.starts_with('\t') {
self.errors
.borrow()
.iter()
.rev()
.find(|x| !x.msg.starts_with('\t'))
.unwrap()
.pos
.offset
} else {
fp.offset
};
self.errors.borrow_mut().push(Error {
pos: fp,
msg: msg,
soft: soft,
by_parser: by_parser,
order: order,
});
}
pub fn len(&self) -> usize {
self.errors.borrow().len()
}
pub fn sort(&self) {
self.errors.borrow_mut().sort_by_key(|e| e.order);
}
pub fn borrow(&self) -> Ref<Vec<Error>> {
self.errors.borrow()
}
}
#[derive(Clone, Debug)]
pub struct FilePosErrors<'a> {
file: &'a File,
elist: &'a ErrorList,
}
impl<'a> FilePosErrors<'a> {
pub fn new(file: &'a File, elist: &'a ErrorList) -> FilePosErrors<'a> {
FilePosErrors {
file: file,
elist: elist,
}
}
pub fn add(&self, pos: Pos, msg: String, soft: bool) {
let p = self.file.position(pos);
self.elist.add(Some(p), msg, soft, false);
}
pub fn add_str(&self, pos: Pos, s: &str, soft: bool) {
self.add(pos, s.to_string(), soft);
}
pub fn parser_add(&self, pos: Pos, msg: String) {
let p = self.file.position(pos);
self.elist.add(Some(p), msg, false, true);
}
pub fn parser_add_str(&self, pos: Pos, s: &str) {
self.parser_add(pos, s.to_string());
}
}