1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
/*
cc6502 - a subset of C compiler for the 6502 processor
Copyright (C) 2023 Bruno STEUX
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
Contact info: bruno.steux@gmail.com
*/
use std::error;
use std::fmt;
use std::io;
/// Errors returned from preprocessing and compilation.
///
/// Error inherits from fmt::Display and so can be very easily formatted and printed.
///
#[derive(Debug)]
pub enum Error {
/// An error from the Rust standard I/O library.
Io(io::Error),
/// An error caused by malformed preprocessor syntax, with a line showing where the error
/// occurred and a string explaining the error further.
Syntax { filename: String, included_in: Option<(String, u32)>, line: u32, msg: String },
Compiler { filename: String, included_in: Option<(String, u32)>, line: u32, msg: String },
Unimplemented { feature: &'static str },
Configuration { error: String }
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
&Error::Io(ref e) => e.fmt(f),
Error::Syntax { filename, included_in, msg, line } => {
match included_in {
Some(include) => write!(f, "Syntax error: {} on line {} of {} (included in {} on line {})", msg, line, filename, include.0, include.1),
None => write!(f, "Syntax error: {} on line {} of {}", msg, line, filename)
}
},
Error::Compiler { filename, included_in, line, msg } => {
match included_in {
Some(include) => write!(f, "Compiler error: {} on line {} of {} (included in {} on line {})", msg, line, filename, include.0, include.1),
None => write!(f, "Compiler error: {} on line {} of {}", msg, line, filename)
}
},
Error::Unimplemented { feature } => {
write!(f, "Compiler error: Unimplemented feature - {}", feature)
},
Error::Configuration { error } => {
write!(f, "Compiler error: Bad configuration - {}", error)
},
}
}
}
impl error::Error for Error {
fn cause(&self) -> Option<&dyn error::Error> {
match self {
Error::Io(ref e) => Some(e),
Error::Syntax { .. } => None,
Error::Compiler { .. } => None,
Error::Unimplemented { .. } => None,
Error::Configuration { .. } => None,
}
}
}
impl From<io::Error> for Error {
fn from(other: io::Error) -> Self {
Error::Io(other)
}
}
impl From<std::str::Utf8Error> for Error {
fn from(_other: std::str::Utf8Error) -> Self {
Error::Syntax {
filename: "internal buffer".to_string(),
included_in: None,
line: 0,
msg: "Utf8 conversion error".to_string(),
}
}
}