Newer
Older
use crate::lexer::{ScriptTokenType, Span};
use crate::utilities::offset_to_line_column;
use lalrpop_util::ParseError as BaseLalrError;
use std::error::Error;
use std::fmt::{Display, Formatter};
use std::process::{ExitCode, Termination};
pub type LalrError<'a> = BaseLalrError<usize, ScriptTokenType, TokenError<'a>>;
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
#[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>,
}
pub enum ForgeErrorKind<'a> {
IncompleteInput,
LexerError(nom::error::Error<Span<'a>>),
UnexpectedToken {
found: ScriptToken<'a>,
expected: peg::error::ExpectedSet,
},
InvalidToken {
location: TokenIndex,
},
UnexpectedEof {
expected: Vec<String>,
},
UnrecognizedToken {
span: ErrorSpan,
expected: Vec<String>,
},
ExpectedEof {
span: ErrorSpan,
},
Custom(String),
pub type TokenIndex = usize;
pub type ErrorSpan = (TokenIndex, TokenIndex);
pub struct ForgeError<'a> {
pub kind: ForgeErrorKind<'a>,
}
impl<'a> Termination for ForgeError<'a> {
fn report(self) -> ExitCode {
match self.kind {
ForgeErrorKind::IncompleteInput => ExitCode::from(101),
ForgeErrorKind::LexerError(_) => ExitCode::from(102),
ForgeErrorKind::UnexpectedToken { .. } => ExitCode::from(103),
ForgeErrorKind::InvalidToken { .. } => ExitCode::from(104),
ForgeErrorKind::UnexpectedEof { .. } => ExitCode::from(105),
ForgeErrorKind::UnrecognizedToken { .. } => ExitCode::from(106),
ForgeErrorKind::ExpectedEof { .. } => ExitCode::from(107),
ForgeErrorKind::Custom(_) => ExitCode::from(1),
}
}
}
impl<'a> From<ForgeErrorKind<'a>> for ForgeError<'a> {
fn from(value: ForgeErrorKind<'a>) -> Self {
Self { kind: value }
}
}
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),
},
}
}
}
impl<'a> From<LalrError<'a>> for ForgeError<'a> {
fn from(value: LalrError<'a>) -> Self {
match value {
LalrError::InvalidToken { location } => {
ForgeErrorKind::InvalidToken { location }.into()
}
LalrError::UnrecognizedEof { expected, .. } => {
ForgeErrorKind::UnexpectedEof { expected }.into()
}
LalrError::UnrecognizedToken { token, expected } => ForgeErrorKind::UnrecognizedToken {
expected,
token: token.1,
span: (token.0, token.2),
}
.into(),
LalrError::ExtraToken { token } => ForgeErrorKind::ExpectedEof {
token: token.1,
span: (token.0, token.2),
}
.into(),
LalrError::User { error } => ForgeErrorKind::Custom(format!("{}", error)).into(),
}
}
}
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
194
195
196
197
198
199
200
201
202
203
204
205
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);
}
pub fn print_forge_error<'a>(source: &'a str, fe: &'a ForgeError) {
eprintln!("{}", format_forge_error(source, fe));
}
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!(
"{}",
SourcePrinter(
source,
format!("{}", found.token_type),
HighlightConfig {
line: found.position.location_line() as usize,
column: found.position.get_column(),
highlight_len: found.token_type.len(),
},
Some(format!(
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
found.token_type,
expected
.tokens()
.collect::<Vec<&str>>()
.as_slice()
.join(", ")
))
),
)
}
ForgeErrorKind::InvalidToken { location } => {
let (line, column) = offset_to_line_column(source, *location);
format!(
"{}",
SourcePrinter(
source,
String::from("invalid token"),
HighlightConfig {
line,
column,
highlight_len: 1,
},
None,
)
)
}
ForgeErrorKind::UnexpectedEof { expected } => {
format!(
"| Unexpected end of file, expected one of {}",
expected.as_slice().join(", ")
)
}
ForgeErrorKind::UnrecognizedToken {
token,
span,
expected,
} => {
let (line, column) = offset_to_line_column(source, span.0);
format!(
"{}",
SourcePrinter(
source,
format!("{}", token),
HighlightConfig {
line,
column,
highlight_len: token.len(),
},
Some(format!(
token,
expected.as_slice().join(", ")
))
),
)
}
ForgeErrorKind::ExpectedEof { token, span } => {
let (line, column) = offset_to_line_column(source, span.0);
format!(
"{}",
SourcePrinter(
source,
format!("{}", token),
HighlightConfig {
line,
column,
highlight_len: token.len(),
},
),
)
}
ForgeErrorKind::Custom(msg) => format!("| {}", msg),
}
}
#[derive(Clone, Copy)]
pub struct HighlightConfig {
line: usize,
column: usize,
highlight_len: usize,
}
struct SourcePrinter<'a>(&'a str, String, HighlightConfig, Option<String>);
impl<'a> Display for SourcePrinter<'a> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
highlight_source_snippet(self.0, &self.1, self.2, self.3.clone(), f)
}
}
pub fn highlight_source_snippet(
source: &str,
name: impl Display,
highlight: HighlightConfig,
additional: Option<String>,
f: &mut Formatter<'_>,
) -> std::fmt::Result {
let HighlightConfig {
line,
column,
highlight_len,
} = highlight;
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, name)?;
if let Some(prev) = previous_line {
writeln!(
f,
"| [{:>width$}] {}",
width = number_length
)?;
}
writeln!(
f,
"| [{:>width$}] {}",
line,
source_line,
width = number_length
)?;
writeln!(
f,
"| {} {}{}",
vec![" "; number_length + 2].join(""),
vec![" "; column - 1].join(""),
vec!["^"; highlight_len].join(""),
)?;
if let Some(next) = next_line {
"| [{:>width$}] {}",
line + 1,
next,
width = number_length
if let Some(extra) = additional {
writeln!(f, "|\n| {}", extra)
} else {
writeln!(f, "|")