Pick this up again, dust it off, get some stuff connected.

This commit is contained in:
2022-02-13 21:30:06 -08:00
parent 91d5d1b4fd
commit 60e7d9a41d
6 changed files with 122 additions and 20 deletions

View File

@@ -12,16 +12,62 @@ use crate::syntax::tokens::Token;
#[cfg(test)]
use crate::util::istring::InternedString;
use lalrpop_util::ParseError;
use std::fmt;
use std::fs;
use std::io;
use std::str::FromStr;
use thiserror::Error;
#[derive(Debug)]
#[derive(Debug, Error)]
pub enum ParserError {
IOError(io::Error),
ParseError(ParseError<Location, Token, LexerError>),
}
impl fmt::Display for ParserError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
ParserError::IOError(e) => write!(f, "IO error: {}", e),
ParserError::ParseError(ParseError::ExtraToken{ token: (loc, tok, _)}) => {
write!(f, "{}: Unexpected additional token ({}) found at end of file", loc, tok)
}
ParserError::ParseError(ParseError::InvalidToken { location }) => {
write!(f, "{}: Unexpected token encountered", location)
}
ParserError::ParseError(ParseError::UnrecognizedEOF { location, expected }) => {
write!(f, "{}: Unexpected EOF{}", location, display_expected(expected))
}
ParserError::ParseError(ParseError::UnrecognizedToken { token: (location, tok, _), expected }) => {
write!(f, "{}: Unexpected token {}{}", location, tok, display_expected(expected))
}
ParserError::ParseError(ParseError::User{ error }) => {
write!(f, "{}: Couldn't process input (lexer error)", error.location)
}
}
}
}
fn display_expected(expected: &Vec<String>) -> String {
match expected.len() {
0 => "".to_string(),
1 => format!("; expected {}", expected[0]),
2 => format!("; expected {} or {}", expected[0], expected[1]),
n => format!("; expected {}or {}", comma_separate(&expected[0..n-1]), expected[n-1])
}
}
fn comma_separate(strings: &[String]) -> String {
let mut result = String::new();
for s in strings.iter() {
result.push_str(&s);
result.push_str(", ");
}
result
}
impl From<io::Error> for ParserError {
fn from(x: io::Error) -> Self {
ParserError::IOError(x)