baby_emulator/assembler/parser/
errors.rs1use super::Instruction;
15
16
17pub trait ParseError {
19 fn describe(&self, line_breaks: bool) -> String;
25}
26
27#[derive(PartialEq, Debug)]
29pub enum ValueParseError {
30 InvalidValue(String),
32 InvalidHex(String),
34 InvalidDecimal(String),
36 InvalidOctal(String),
38 InvalidBinary(String),
40 InvalidTagName(String),
42}
43
44impl ParseError for ValueParseError {
45 fn describe(&self, _line_breaks: bool) -> String {
46 match self {
47 ValueParseError::InvalidValue(v) => format!("The value: {}; is an invalid value. ", v),
48 ValueParseError::InvalidHex(v) => format!("The value: {}; is invalid hex value. ", v),
49 ValueParseError::InvalidDecimal(v) => format!("The value: {}; is invalid decimal value. ", v),
50 ValueParseError::InvalidOctal(v) => format!("The value: {}; is invalid octal value. ", v),
51 ValueParseError::InvalidBinary(v) => format!("The value: {}; is invalid binary value. ", v),
52 ValueParseError::InvalidTagName(v) => format!("The value: {}; is invalid tag name. ", v),
53 }
54 }
55}
56
57#[derive(PartialEq, Debug)]
59pub enum InstructionError {
60 UnkownInstruction(String),
62 OperandValueParseError(Instruction, ValueParseError)
64}
65
66impl ParseError for InstructionError {
67 fn describe(&self, line_breaks: bool) -> String {
68 let line_break = if line_breaks { "\n" } else { "" };
69 match self {
70 InstructionError::UnkownInstruction(v) => format!("The specified instruction {} is not known. ", v),
71 InstructionError::OperandValueParseError(c, v) => format!("Failed to parse operand for {}. {line_break} {}", c.describe(), v.describe(line_breaks))
72 }
73 }
74}
75
76#[derive(PartialEq, Debug)]
78pub enum AbsoluteError {
79 ValueError(ValueParseError)
81}
82
83impl ParseError for AbsoluteError {
84 fn describe(&self, line_breaks: bool) -> String {
85 let line_break = if line_breaks { "\n" } else { "" };
86 match self {
87 AbsoluteError::ValueError(v) => format!("Failed to parse value for absolute value declaration. {line_break} {}", v.describe(line_breaks))
88 }
89 }
90}
91
92#[derive(PartialEq, Debug)]
94pub enum TagError {
95 TagNameWhitespace(String)
97}
98
99impl ParseError for TagError {
100 fn describe(&self, _line_breaks: bool) -> String {
101 match self {
102 TagError::TagNameWhitespace(v) => format!("The tag name `{}` is invalid. ", v)
103 }
104 }
105}
106
107#[derive(PartialEq, Debug)]
109pub enum LineParseError {
110 TagError(TagError),
112 AbsoluteError(AbsoluteError),
114 InstructionError(InstructionError),
116}
117
118impl ParseError for LineParseError {
119 fn describe(&self, line_breaks: bool) -> String {
120 let line_break = if line_breaks { "\n" } else { "" };
121 match self {
122 LineParseError::TagError(v) => format!("Error parsing a tag line, {}", v.describe(line_breaks)),
123 LineParseError::AbsoluteError(v) => format!("Error parsing an absolute value line. {line_break} {}", v.describe(line_breaks)),
124 LineParseError::InstructionError(v) => format!("Error parsing a instruction line. {line_break} {}", v.describe(line_breaks)),
125 }
126 }
127}