-
-
Notifications
You must be signed in to change notification settings - Fork 418
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
2,194 additions
and
1,686 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
//! Error and result implementation for the parser. | ||
use crate::syntax::ast::{ | ||
keyword::Keyword, | ||
node::Node, | ||
pos::Position, | ||
token::{Token, TokenKind}, | ||
}; | ||
use std::fmt; | ||
|
||
/// Result of a parsing operation. | ||
pub type ParseResult = Result<Node, ParseError>; | ||
|
||
/// `ParseError` is an enum which represents errors encounted during parsing an expression | ||
#[derive(Debug, Clone)] | ||
pub enum ParseError { | ||
/// When it expected a certain kind of token, but got another as part of something | ||
Expected(Vec<TokenKind>, Token, Option<&'static str>), | ||
/// When it expected a certain expression, but got another | ||
ExpectedExpr(&'static str, Node, Position), | ||
/// When it didn't expect this keyword | ||
UnexpectedKeyword(Keyword, Position), | ||
/// When a token is unexpected | ||
Unexpected(Token, Option<&'static str>), | ||
/// When there is an abrupt end to the parsing | ||
AbruptEnd, | ||
/// Out of range error, attempting to set a position where there is no token | ||
RangeError, | ||
/// Catch all General Error | ||
General(&'static str, Option<Position>), | ||
} | ||
|
||
impl fmt::Display for ParseError { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
match self { | ||
ParseError::Expected(expected, actual, routine) => write!( | ||
f, | ||
"Expected {}, got '{}'{} at line {}, col {}", | ||
if expected.len() == 1 { | ||
format!( | ||
"token '{}'", | ||
expected.first().map(TokenKind::to_string).unwrap() | ||
) | ||
} else { | ||
format!( | ||
"one of {}", | ||
expected | ||
.iter() | ||
.enumerate() | ||
.map(|(i, t)| { | ||
format!( | ||
"{}'{}'", | ||
if i == 0 { | ||
"" | ||
} else if i == expected.len() - 1 { | ||
" or " | ||
} else { | ||
", " | ||
}, | ||
t | ||
) | ||
}) | ||
.collect::<String>() | ||
) | ||
}, | ||
actual, | ||
if let Some(routine) = routine { | ||
format!(" in {}", routine) | ||
} else { | ||
String::new() | ||
}, | ||
actual.pos.line_number, | ||
actual.pos.column_number | ||
), | ||
ParseError::ExpectedExpr(expected, actual, pos) => write!( | ||
f, | ||
"Expected expression '{}', got '{}' at line {}, col {}", | ||
expected, actual, pos.line_number, pos.column_number | ||
), | ||
ParseError::UnexpectedKeyword(keyword, pos) => write!( | ||
f, | ||
"Unexpected keyword: '{}' at line {}, col {}", | ||
keyword, pos.line_number, pos.column_number | ||
), | ||
ParseError::Unexpected(tok, msg) => write!( | ||
f, | ||
"Unexpected Token '{}'{} at line {}, col {}", | ||
tok, | ||
if let Some(m) = msg { | ||
format!(", {}", m) | ||
} else { | ||
String::new() | ||
}, | ||
tok.pos.line_number, | ||
tok.pos.column_number | ||
), | ||
ParseError::AbruptEnd => write!(f, "Abrupt End"), | ||
ParseError::General(msg, pos) => write!( | ||
f, | ||
"{}{}", | ||
msg, | ||
if let Some(pos) = pos { | ||
format!(" at line {}, col {}", pos.line_number, pos.column_number) | ||
} else { | ||
String::new() | ||
} | ||
), | ||
ParseError::RangeError => write!(f, "RangeError!"), | ||
} | ||
} | ||
} |
Oops, something went wrong.