use crate::base::Position;
#[derive(Debug)]
pub struct ParseResult<T> {
pub content: Option<T>,
pub errors: Vec<ParseError>,
}
#[derive(Debug, Clone)]
pub struct ParseError {
pub message: String,
pub position: Position,
pub kind: ParseErrorKind,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ParseErrorKind {
SyntaxError,
AstError,
IoError,
}
impl<T> ParseResult<T> {
pub fn success(content: T) -> Self {
Self {
content: Some(content),
errors: vec![],
}
}
pub fn with_errors(errors: Vec<ParseError>) -> Self {
Self {
content: None,
errors,
}
}
pub fn is_ok(&self) -> bool {
self.errors.is_empty()
}
pub fn has_errors(&self) -> bool {
!self.errors.is_empty()
}
}
impl ParseError {
pub fn syntax_error(message: impl Into<String>, line: usize, column: usize) -> Self {
Self {
message: message.into(),
position: Position::new(line, column),
kind: ParseErrorKind::SyntaxError,
}
}
pub fn ast_error(message: impl Into<String>, line: usize, column: usize) -> Self {
Self {
message: message.into(),
position: Position::new(line, column),
kind: ParseErrorKind::AstError,
}
}
}