More parser implementation

This commit is contained in:
2022-03-13 15:44:48 -05:00
parent 19eb4d57f3
commit 643e8d5749
6 changed files with 231 additions and 89 deletions
+21 -5
View File
@@ -1,10 +1,26 @@
use vcore::opcodes::InvalidOpcode;
use vcore::opcodes::{InvalidMnemonic};
use pest::iterators::Pair;
use std::fmt::{Display, Formatter};
use crate::vasm_parser::Rule;
#[derive(Debug, PartialEq)]
pub struct ParseError<'a>(pub &'a str);
pub enum ParseError<'a> {
LineParseFailure,
InvalidInstruction(&'a str)
}
impl<'a> From<InvalidOpcode> for ParseError<'a> {
fn from(_: InvalidOpcode) -> Self {
Self("Invalid opcode")
impl<'a> From<InvalidMnemonic<'a>> for ParseError<'a> {
fn from(err: InvalidMnemonic<'a>) -> Self {
Self::InvalidInstruction(err.0)
}
}
impl<'a> Display for ParseError<'a> {
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)
}
}
}