Errors - Added ErrorKind::Scan
This commit is contained in:
parent
d0d4038272
commit
d3398e5f79
1 changed files with 18 additions and 3 deletions
|
@ -6,6 +6,8 @@ use crate::tokens::{Token, TokenType};
|
||||||
/// The type of an error.
|
/// The type of an error.
|
||||||
#[derive(Clone, Copy, Debug)]
|
#[derive(Clone, Copy, Debug)]
|
||||||
pub enum ErrorKind {
|
pub enum ErrorKind {
|
||||||
|
/// The error occurred while scanning the source code.
|
||||||
|
Scan,
|
||||||
/// The error occurred while parsing the source code.
|
/// The error occurred while parsing the source code.
|
||||||
Parse,
|
Parse,
|
||||||
/// The error occurred while trying to run the program.
|
/// The error occurred while trying to run the program.
|
||||||
|
@ -15,7 +17,7 @@ pub enum ErrorKind {
|
||||||
impl fmt::Display for ErrorKind {
|
impl fmt::Display for ErrorKind {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
f.write_str(match self {
|
f.write_str(match self {
|
||||||
ErrorKind::Parse => "Parse",
|
ErrorKind::Scan | ErrorKind::Parse => "Parse",
|
||||||
ErrorKind::Runtime => "Runtime",
|
ErrorKind::Runtime => "Runtime",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -32,8 +34,21 @@ pub struct SloxError {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SloxError {
|
impl SloxError {
|
||||||
/// Initialize an error record.
|
/// Initialize a record for a scanner error.
|
||||||
pub fn new(kind: ErrorKind, token: &Token, message: String) -> Self {
|
pub fn scanner_error(line: usize, ch: Option<char>, message: String) -> Self {
|
||||||
|
Self {
|
||||||
|
kind: ErrorKind::Scan,
|
||||||
|
line,
|
||||||
|
pos: match ch {
|
||||||
|
None => "at end of input".to_owned(),
|
||||||
|
Some(ch) => format!("near {:?}", ch)
|
||||||
|
},
|
||||||
|
message,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Initialize an error record using a token.
|
||||||
|
pub fn with_token(kind: ErrorKind, token: &Token, message: String) -> Self {
|
||||||
Self {
|
Self {
|
||||||
kind,
|
kind,
|
||||||
line: token.line,
|
line: token.line,
|
||||||
|
|
Loading…
Reference in a new issue