Files
vulcan/vasm_core/src/parse_error.rs
T

126 lines
3.6 KiB
Rust
Raw Normal View History

2022-03-13 15:44:48 -05:00
use std::fmt::{Display, Formatter};
2022-03-14 01:04:08 -05:00
use vcore::opcodes::InvalidMnemonic;
2022-03-12 15:39:29 -06:00
#[derive(Debug, PartialEq, Clone)]
pub struct Location {
pub line_num: usize,
pub file: String,
}
2022-04-02 00:15:33 -05:00
#[derive(Debug, PartialEq, Clone)]
2022-04-05 18:05:24 -05:00
pub enum ParseError {
2022-03-13 15:44:48 -05:00
LineParseFailure,
2022-04-05 18:05:24 -05:00
InvalidInstruction(String),
2022-03-13 15:44:48 -05:00
}
2022-03-12 15:39:29 -06:00
impl Display for Location {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}:{}", self.file, self.line_num)
}
}
impl Default for Location {
fn default() -> Self {
Location {
line_num: 0,
file: "<none>".to_string(),
}
}
}
impl From<usize> for Location {
fn from(line: usize) -> Self {
Self {
line_num: line,
file: "<none>".to_string(),
}
}
}
2022-04-05 18:05:24 -05:00
impl<'a> From<InvalidMnemonic<'a>> for ParseError {
2022-03-13 15:44:48 -05:00
fn from(err: InvalidMnemonic<'a>) -> Self {
2022-04-05 18:05:24 -05:00
Self::InvalidInstruction(err.0.into())
2022-03-12 15:39:29 -06:00
}
}
2022-03-13 15:44:48 -05:00
2022-04-05 18:05:24 -05:00
impl Display for ParseError {
2022-03-13 15:44:48 -05:00
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
use ParseError::*;
match self {
LineParseFailure => write!(f, "Failed to parse line"),
InvalidInstruction(p) => write!(f, "Cannot parse {} as instruction", p),
2022-03-13 15:44:48 -05:00
}
}
2022-03-14 01:04:08 -05:00
}
2022-04-15 01:33:36 -05:00
#[derive(Debug, Clone, PartialEq)]
pub enum AssembleError {
ParseError(Location, ParseError),
EquResolveError(Location, String, EvalError),
EquDuplicateError(Location, String),
OrgResolveError(Location, EvalError),
ArgError(Location, EvalError),
2022-04-15 01:33:36 -05:00
NoCode,
IncludeError(Location, String),
2022-06-01 16:11:34 -05:00
FileError(String),
MacroError(Location),
2022-04-15 01:33:36 -05:00
}
impl Display for AssembleError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
AssembleError::EquResolveError(line, name, err) => {
write!(f, "Cannot resolve .equ {} at {}: {}", name, line, err)
2022-04-15 01:33:36 -05:00
}
AssembleError::EquDuplicateError(line, name) => {
write!(f, "Duplicate .equ {} at {}", name, line)
2022-04-15 01:33:36 -05:00
}
AssembleError::OrgResolveError(line, err) => {
write!(f, "Cannot resolve .org at {}: {}", line, err)
2022-04-15 01:33:36 -05:00
}
AssembleError::ArgError(line, err) => {
write!(f, "Cannot calculate argument at {}: {}", line, err)
2022-04-15 01:33:36 -05:00
}
AssembleError::NoCode => {
write!(f, "No output would be generated by this code")
}
AssembleError::ParseError(line, err) => {
write!(f, "Parse error at {}: {}", line, err)
2022-04-15 01:33:36 -05:00
}
AssembleError::IncludeError(line, file) => {
write!(f, "Cannot read \"{}\" at {}", file, line)
2022-04-15 01:33:36 -05:00
}
AssembleError::MacroError(line) => {
write!(f, "Malformed macro control structure at {}", line)
2022-04-15 01:33:36 -05:00
}
2022-06-01 16:11:34 -05:00
AssembleError::FileError(file) => {
write!(f, "File read error in {}", file)
}
2022-04-15 01:33:36 -05:00
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum EvalError {
MissingLabel(String),
UnknownAddress(usize),
OffsetError(usize, i32),
}
impl Display for EvalError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
EvalError::MissingLabel(label) => write!(f, "Unable to resolve label {}", label),
EvalError::UnknownAddress(line_num) => write!(
f,
"Unable to calculate starting address of line {}",
line_num
),
EvalError::OffsetError(line_num, offset) => {
write!(f, "Invalid line offset {} on line {}", offset, line_num)
}
}
}
}