Newer
Older
use crate::lexer::Span;
use crate::parse::ScriptToken;
use peg::error::ExpectedSet;
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
use std::error::Error;
use std::fmt::{Display, Formatter};
#[derive(Debug)]
pub enum TokenErrorKind<'a> {
Incomplete,
NomError(nom::error::Error<Span<'a>>),
}
#[derive(Debug)]
pub struct TokenError<'a> {
pub kind: TokenErrorKind<'a>,
}
impl<'a> From<TokenErrorKind<'a>> for TokenError<'a> {
fn from(value: TokenErrorKind<'a>) -> Self {
TokenError { kind: value }
}
}
impl<'a> Display for TokenError<'a> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match &self.kind {
TokenErrorKind::Incomplete => write!(f, "Incomplete Program"),
TokenErrorKind::NomError(err) => write!(f, "{}", err),
}
}
}
impl<'a> Error for TokenError<'a> {}
impl<'a> From<nom::Err<nom::error::Error<Span<'a>>>> for TokenError<'a> {
fn from(value: nom::Err<nom::error::Error<Span<'a>>>) -> Self {
match value {
nom::Err::Error(err) => TokenErrorKind::NomError(err).into(),
nom::Err::Failure(err) => TokenErrorKind::NomError(err).into(),
nom::Err::Incomplete(_) => TokenErrorKind::Incomplete.into(),
}
}
}
#[derive(Clone, Debug)]
pub enum ParseErrorKind<'a> {
Unexpected {
found: ScriptToken<'a>,
expected: peg::error::ExpectedSet,
},
}
#[derive(Clone, Debug)]
pub struct ParseError<'a> {
pub kind: ParseErrorKind<'a>,
}
#[derive(Debug)]
pub enum ForgeErrorKind<'a> {
IncompleteInput,
LexerError(nom::error::Error<Span<'a>>),
UnexpectedToken {
found: ScriptToken<'a>,
expected: peg::error::ExpectedSet,
},
}
#[derive(Debug)]
pub struct ForgeError<'a> {
pub kind: ForgeErrorKind<'a>,
}
impl<'a> From<ParseError<'a>> for ForgeError<'a> {
fn from(value: ParseError<'a>) -> Self {
match value.kind {
ParseErrorKind::Unexpected { found, expected } => ForgeError {
kind: ForgeErrorKind::UnexpectedToken { found, expected },
},
}
}
}
impl<'a> From<TokenError<'a>> for ForgeError<'a> {
fn from(value: TokenError<'a>) -> Self {
match value.kind {
TokenErrorKind::Incomplete => ForgeError {
kind: ForgeErrorKind::IncompleteInput,
},
TokenErrorKind::NomError(span) => ForgeError {
kind: ForgeErrorKind::LexerError(span),
},
}
}
}
pub type ForgeResult<'a, T> = Result<T, ForgeError<'a>>;
pub fn print_unexpected_token<'a>(
source: &'a str,
token: &'a ScriptToken<'a>,
expected: &'a peg::error::ExpectedSet,
) {
let line = token.position.location_line() as usize;
let column = token.position.get_column();
let previous_line = if line > 1 {
source.lines().nth(line - 2)
} else {
None
};
let source_line = source.lines().nth(line - 1).expect("Missing line");
let next_line = source.lines().nth(line);
let largest_line_num = line.max(line.saturating_sub(1)).max(line.saturating_add(1));
let number_length = format!("{}", largest_line_num).len();
eprintln!("| Script error on line {} at \"{}\"\n|", line, token);
if let Some(prev) = previous_line {
eprintln!("| [{:>width$}] {}", line - 1, prev, width = number_length);
}
eprintln!(
"| [{:>width$}] {}",
line,
source_line,
width = number_length
);
eprintln!(
"| {} {}{}",
vec![" "; number_length + 2].join(""),
vec![" "; column - 1].join(""),
vec!["^"; token.token_type.len()].join(""),
);
if let Some(next) = next_line {
eprintln!("| [{:>width$}] {}", line + 1, next, width = number_length);
}
eprintln!("|\n| Failed To Parse: expected {}", expected);
}
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
pub struct ErrorFormat<'a>(pub &'a str, pub &'a ScriptToken<'a>, pub &'a ExpectedSet);
impl<'a> Display for ErrorFormat<'a> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let ErrorFormat(source, token, expected) = self;
let line = token.position.location_line() as usize;
let column = token.position.get_column();
let previous_line = if line > 1 {
source.lines().nth(line - 2)
} else {
None
};
let source_line = source.lines().nth(line - 1).expect("Missing line");
let next_line = source.lines().nth(line);
let largest_line_num = line.max(line.saturating_sub(1)).max(line.saturating_add(1));
let number_length = format!("{}", largest_line_num).len();
writeln!(f, "| Script error on line {} at \"{}\"\n|", line, token)?;
if let Some(prev) = previous_line {
writeln!(
f,
"| [{:>width$}] {}",
line - 1,
prev,
width = number_length
)?;
}
writeln!(
f,
"| [{:>width$}] {}",
line,
source_line,
width = number_length
)?;
writeln!(
f,
"| {} {}{}",
vec![" "; number_length + 2].join(""),
vec![" "; column - 1].join(""),
vec!["^"; token.token_type.len()].join(""),
)?;
if let Some(next) = next_line {
writeln!(
f,
"| [{:>width$}] {}",
line + 1,
next,
width = number_length
)?;
}
writeln!(f, "|\n| Failed To Parse: expected {}", expected)
}
}
pub fn print_forge_error<'a>(source: &'a str, fe: &'a ForgeError) {
match &fe.kind {
ForgeErrorKind::IncompleteInput => eprintln!("| Unexpected end of file"),
ForgeErrorKind::LexerError(err) => eprintln!("| {}", err),
ForgeErrorKind::UnexpectedToken { found, expected } => {
print_unexpected_token(source, found, expected)
}
}
}
pub fn format_forge_error<'a>(source: &'a str, fe: &'a ForgeError) -> String {
match &fe.kind {
ForgeErrorKind::IncompleteInput => String::from("| Unexpected end of file"),
ForgeErrorKind::LexerError(err) => format!("| {}", err),
ForgeErrorKind::UnexpectedToken { found, expected } => {
format!("{}", ErrorFormat(source, found, expected))
}
}
}