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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use thiserror::Error;
#[derive(Clone)]
pub struct ParseError {
pub source: String,
pub location: usize,
reason: ParseErrorReason,
}
impl std::fmt::Display for ParseError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let reason = &self.reason;
let source = &self.source;
let p = self.location.min(source.len());
let line_start = match source[..p].rfind('\n') {
Some(i) => i + 1,
None => 0,
};
let line_num = source[..line_start].chars().filter(|c| *c == '\n').count() + 1;
let column_num = p - line_start + 1;
write!(f, "{reason} at line {line_num} column {column_num}")
}
}
impl std::fmt::Debug for ParseError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ParseError")
.field("source", &self.source)
.field("location", &self.location)
.field("reason", &self.reason)
.field("summary", &format!("{self}"))
.finish()
}
}
impl std::error::Error for ParseError {}
#[derive(Clone, Debug, Error)]
enum ParseErrorReason {
#[error("extra unparsed text after match")]
Extra,
#[error("line() can't match here because this is not at the start of a line")]
NotAtLineStart,
#[error("section() can't match here because this is not at the start of a section")]
NotAtSectionStart,
#[error("line(pattern) matched part of the line, but not all of it")]
LineExtra,
#[error("section(pattern) matched part of the section, but not all of it")]
SectionExtra,
#[error("expected {0:?}")]
Expected(String),
#[error("failed to parse {input:?} as type {type_name}: {message}")]
FromStrFailed {
input: String,
type_name: &'static str,
message: String,
},
#[error("nothing matches this pattern")]
CannotMatch,
}
impl ParseError {
pub fn new_extra(source: &str, location: usize) -> Self {
ParseError {
source: source.to_string(),
location,
reason: ParseErrorReason::Extra,
}
}
pub fn new_bad_line_start(source: &str, location: usize) -> Self {
ParseError {
source: source.to_string(),
location,
reason: ParseErrorReason::NotAtLineStart,
}
}
pub fn new_bad_section_start(source: &str, location: usize) -> Self {
ParseError {
source: source.to_string(),
location,
reason: ParseErrorReason::NotAtSectionStart,
}
}
pub fn new_line_extra(source: &str, location: usize) -> Self {
ParseError {
source: source.to_string(),
location,
reason: ParseErrorReason::LineExtra,
}
}
pub fn new_section_extra(source: &str, location: usize) -> Self {
ParseError {
source: source.to_string(),
location,
reason: ParseErrorReason::SectionExtra,
}
}
pub fn new_expected(source: &str, location: usize, expected: &str) -> Self {
ParseError {
source: source.to_string(),
location,
reason: ParseErrorReason::Expected(expected.to_string()),
}
}
pub fn new_from_str_failed(
source: &str,
start: usize,
end: usize,
type_name: &'static str,
message: String,
) -> Self {
ParseError {
source: source.to_string(),
location: start,
reason: ParseErrorReason::FromStrFailed {
input: source[start..end].to_string(),
type_name,
message,
},
}
}
pub fn new_cannot_match(source: &str, location: usize) -> Self {
ParseError {
source: source.to_string(),
location,
reason: ParseErrorReason::CannotMatch,
}
}
pub(crate) fn adjust_location(&mut self, offset: usize) {
self.location += offset;
}
}
pub(crate) type Result<T> = std::result::Result<T, ParseError>;