Merge pull request #3241 from Skgland/parse-error-improvements

change `ParserError` type
This commit is contained in:
Mark Thom
2026-03-09 22:07:03 -07:00
committed by GitHub
8 changed files with 173 additions and 193 deletions

View File

@@ -20,7 +20,7 @@ pub type MachineStubGen = Box<dyn Fn(&mut MachineState) -> MachineStub>;
#[derive(Debug)]
pub(crate) struct MachineError {
stub: MachineStub,
location: Option<(usize, usize)>, // line_num, col_num
location: Option<Location>,
}
// from 7.12.2 b) of 13211-1:1995
@@ -753,14 +753,15 @@ impl MachineState {
}
pub(super) fn error_form(&mut self, err: MachineError, src: MachineStub) -> MachineStub {
if let Some((line_num, _col_num)) = err.location {
if let Some(location) = err.location {
let line = location.line();
functor!(
atom!("error"),
[
functor((err.stub)),
functor(
(atom!(":")),
[functor(src), number(line_num, (&mut self.arena))]
[functor(src), number(line, (&mut self.arena))]
)
]
)
@@ -853,9 +854,9 @@ impl From<ParserError> for CompilationError {
}
impl CompilationError {
pub(crate) fn line_and_col_num(&self) -> Option<(usize, usize)> {
pub(crate) fn line_and_col_num(&self) -> Option<Location> {
match self {
CompilationError::ParserError(err) => err.line_and_col_num(),
CompilationError::ParserError(err) => err.location(),
_ => None,
}
}

View File

@@ -1952,7 +1952,7 @@ impl MachineState {
) -> Result<Stream, ParserError> {
match stream.peek_char() {
None => Ok(stream), // empty stream is handled gracefully by Lexer::eof
Some(Err(e)) => Err(ParserError::IO(e)),
Some(Err(e)) => Err(ParserError::from(e)),
Some(Ok(c)) => {
if c == '\u{feff}' {
// skip UTF-8 BOM

View File

@@ -1016,7 +1016,7 @@ impl MachineState {
self.unify_fixnum(n, nx);
}
_ => {
let err = ParserError::ParseBigInt(0, 0);
let err = parser.lexer.parse_big_int_error();
let err = self.syntax_error(err);
return Err(self.error_form(err, stub_gen()));
@@ -1026,9 +1026,7 @@ impl MachineState {
return Ok(());
}
Ok(c) => {
let (line_num, col_num) = (lexer.line_num, lexer.col_num);
let err = ParserError::UnexpectedChar(c, line_num, col_num);
let err = lexer.unexpected_char(c);
let err = self.syntax_error(err);
return Err(self.error_form(err, stub_gen()));
@@ -9214,7 +9212,7 @@ impl Machine {
stream.add_lines_read(parser.lines_read());
}
Ok(true) => {
stream.add_lines_read(parser.lexer.line_num);
stream.add_lines_read(parser.lexer.location.line());
self.machine_st.fail = true;
}
Err(err) => {