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 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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
use std::fs;
use std::ops::Range;
use std::path::Path;
use crate::{LexerError, ParolError, ParserError, Span};
use codespan_reporting::diagnostic::{Diagnostic, Label};
use codespan_reporting::files::SimpleFiles;
use codespan_reporting::term::{self, termcolor::StandardStream};
/// *Trait for parol's error reporting*
///
/// Implement this trait and provide an own implementation for [Report::report_user_error] when you
/// want to contribute your own error reporting for your error types.
///
/// If you don't want to report own errors then simply use it's default implementation like this:
/// ```
/// use parol_runtime::Report;
/// use parol_macros::parol;
///
/// struct MyErrorReporter;
/// impl Report for MyErrorReporter {};
///
/// let err = parol!("Crucial problem!"); // Suppose that this error comes from a call of `parse`
/// MyErrorReporter::report_error(&err, "my_file.xyz").unwrap_or_default();
/// ```
pub trait Report {
///
/// Implement this method if you want to provide your own error reporting for your own error
/// types.
/// Doing so you can hook into the error reporting process.
///
/// Examples are `parol`'s `ParolErrorReporter` or `basic_interpreter`'s `BasicErrorReporter`.
///
/// The method's argument value is obtained from a `parol_runtime::ParolError::UserError`'s
/// content. It should return Ok(()) if reporting succeeds and an error value if the reporting
/// itself fails somehow.
///
fn report_user_error(err: &anyhow::Error) -> anyhow::Result<()> {
let writer = StandardStream::stderr(term::termcolor::ColorChoice::Auto);
let config = codespan_reporting::term::Config::default();
let files = SimpleFiles::<String, String>::new();
let result = term::emit(
&mut writer.lock(),
&config,
&files,
&Diagnostic::error()
.with_message("User error")
.with_notes(vec![
err.to_string(),
err.source()
.map_or("No details".to_string(), |s| s.to_string()),
]),
);
result.map_err(|e| anyhow::anyhow!(e))
}
/// You don't need to implement this method because it contains the reporting functionality for
/// errors from parol_runtime itself.
fn report_error<T>(err: &ParolError, file_name: T) -> anyhow::Result<()>
where
T: AsRef<Path>,
{
let writer = StandardStream::stderr(term::termcolor::ColorChoice::Auto);
let config = codespan_reporting::term::Config::default();
let mut files = SimpleFiles::new();
let content = fs::read_to_string(file_name.as_ref()).unwrap_or_default();
let file_id = files.add(file_name.as_ref().display().to_string(), content);
let report_lexer_error = |err: &LexerError| -> anyhow::Result<()> {
match err {
LexerError::DataError(e) => Ok(term::emit(
&mut writer.lock(),
&config,
&files,
&Diagnostic::bug()
.with_message(format!("Data error: {e}"))
.with_code("parol_runtime::lexer::internal_error")
.with_notes(vec!["Error in generated source".to_string()]),
)?),
LexerError::PredictionError { cause } => Ok(term::emit(
&mut writer.lock(),
&config,
&files,
&Diagnostic::error()
.with_message("Error in input")
.with_code("parol_runtime::lookahead::production_prediction_error")
.with_notes(vec![cause.to_string()]),
)?),
LexerError::TokenBufferEmptyError => Ok(term::emit(
&mut writer.lock(),
&config,
&files,
&Diagnostic::bug()
.with_message("No valid token read")
.with_code("parol_runtime::lexer::empty_token_buffer")
.with_notes(vec!["Token buffer is empty".to_string()]),
)?),
LexerError::InternalError(e) => Ok(term::emit(
&mut writer.lock(),
&config,
&files,
&Diagnostic::bug()
.with_message(format!("Internal lexer error: {e}"))
.with_code("parol_runtime::lexer::internal_error"),
)?),
LexerError::LookaheadExceedsMaximum => Ok(term::emit(
&mut writer.lock(),
&config,
&files,
&Diagnostic::bug()
.with_message("Lookahead exceeds maximum".to_string())
.with_code("parol_runtime::lexer::lookahead_exceeds_maximum"),
)?),
LexerError::LookaheadExceedsTokenBufferLength => Ok(term::emit(
&mut writer.lock(),
&config,
&files,
&Diagnostic::bug()
.with_message("Lookahead exceeds token buffer length".to_string())
.with_code("parol_runtime::lexer::lookahead_exceeds_token_buffer_length"),
)?),
LexerError::ScannerStackEmptyError => Ok(term::emit(
&mut writer.lock(),
&config,
&files,
&Diagnostic::bug()
.with_message("Tried to pop from empty scanner stack".to_string())
.with_code("parol_runtime::lexer::pop_from_empty_scanner_stack")
.with_notes(vec![
"Check balance of %push and %pop directives in your grammar"
.to_string(),
]),
)?),
}
};
let report_parser_error = |err: &ParserError| -> anyhow::Result<()> {
match err {
ParserError::TreeError { source } => Ok(term::emit(
&mut writer.lock(),
&config,
&files,
&Diagnostic::bug()
.with_message(format!("Error from syntree crate: {}", source))
.with_code("parol_runtime::parser::syntree_error")
.with_notes(vec!["Internal error".to_string()]),
)?),
ParserError::PredictionErrorWithExpectations {
cause,
unexpected_tokens,
expected_tokens,
source,
..
} => {
if let Some(source) = source {
Self::report_error(source, file_name)?;
}
let range = unexpected_tokens
.iter()
.fold(Range::default(), |mut acc, un| {
let un_span: Span = (Into::<Range<usize>>::into(&un.token)).into();
let acc_span: Span = acc.into();
acc = (acc_span + un_span).into();
acc
});
let unexpected_tokens_labels =
unexpected_tokens.iter().fold(vec![], |mut acc, un| {
acc.push(
Label::secondary(file_id, Into::<Range<usize>>::into(&un.token))
.with_message(un.token_type.clone()),
);
acc
});
Ok(term::emit(
&mut writer.lock(),
&config,
&files,
&Diagnostic::error()
.with_message("Syntax error")
.with_code("parol_runtime::parser::syntax_error")
.with_labels(vec![Label::primary(file_id, range).with_message("Found")])
.with_labels(unexpected_tokens_labels)
.with_notes(vec![
"Expecting one of".to_string(),
expected_tokens.to_string(),
])
.with_notes(vec![cause.to_string()]),
)?)
}
ParserError::UnprocessedInput { last_token, .. } => {
let un_span: Span = (Into::<Range<usize>>::into(&**last_token)).into();
Ok(term::emit(
&mut writer.lock(),
&config,
&files,
&Diagnostic::error()
.with_message("Unprocessed input is left after parsing has finished")
.with_code("parol_runtime::parser::unprocessed_input")
.with_labels(vec![
Label::primary(file_id, un_span).with_message("Unprocessed")
])
.with_notes(vec![
"Unprocessed input could be a problem in your grammar.".to_string(),
]),
)?)
}
ParserError::PopOnEmptyScannerStateStack {
context, source, ..
} => {
report_lexer_error(source)?;
Ok(term::emit(
&mut writer.lock(),
&config,
&files,
&Diagnostic::error()
.with_message(format!(
"{context}Tried to pop from an empty scanner stack"
))
.with_code("parol_runtime::parser::pop_on_empty_scanner_stack"),
)?)
}
ParserError::InternalError(e) => Ok(term::emit(
&mut writer.lock(),
&config,
&files,
&Diagnostic::bug()
.with_message(format!("Internal parser error: {e}"))
.with_code("parol_runtime::parser::internal_error")
.with_notes(vec!["This may be a bug. Please report it!".to_string()]),
)?),
}
};
match err {
ParolError::ParserError(e) => report_parser_error(e),
ParolError::LexerError(e) => report_lexer_error(e),
ParolError::UserError(e) => Self::report_user_error(e),
}
}
}