corewars_parser/
error.rs

1//! Error types for the corewars library
2
3use std::num::TryFromIntError;
4
5use thiserror::Error as ThisError;
6
7use corewars_core::load_file::Opcode;
8
9// TODO: use pest spans for error reporting? Or at least line number
10
11/// An error that occurred while parsing a warrior.
12#[derive(ThisError, Debug, PartialEq)]
13pub enum Error {
14    /// The warrior contained a reference to a label that doesn't exist.
15    #[error("no such label {label:?}")]
16    LabelNotFound { label: String, line: Option<usize> },
17
18    /// An invalid warrior origin (not a positive integer) was specified.
19    #[error("invalid origin specified")]
20    InvalidOrigin(#[from] TryFromIntError),
21
22    /// The input string was ill-formed Redcode syntax.
23    #[error("invalid syntax")]
24    InvalidSyntax(#[from] super::grammar::SyntaxError),
25
26    /// The given opcode was not given enough arguments.
27    #[error("expected additional arguments for {opcode} opcode")]
28    InvalidArguments { opcode: Opcode },
29}
30
31/// A warning that occurred while parsing a warrior.
32#[derive(ThisError, Debug, PartialEq)]
33pub enum Warning {
34    /// Attempt to define the warrior origin more than once.
35    #[error("origin already defined as {old:?}, new definition {new:?} will be ignored")]
36    OriginRedefinition { old: String, new: String },
37
38    /// Empty EQU substitution.
39    #[error("right-hand side of substitution for label {0:?} is empty")]
40    EmptySubstitution(String),
41
42    /// Offset label declaration with no instruction.
43    #[error("no instruction offset for label {0:?}, it will not b")]
44    EmptyOffset(String),
45}