Extract Result and Error into their own module.

Also include the err! macro and make it usable outside the module.
This commit is contained in:
Jakob Stoklund Olesen
2016-09-19 10:08:21 -07:00
parent 17c2b5213a
commit feef2ecf3f
5 changed files with 53 additions and 45 deletions

44
src/libreader/error.rs Normal file
View File

@@ -0,0 +1,44 @@
//! Define the `Location`, `Error`, and `Result` types.
#![macro_use]
use std::fmt;
use std::result;
/// The location of a `Token` or `Error`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Location {
pub line_number: usize,
}
/// A parse error is returned when the parse failed.
#[derive(Debug)]
pub struct Error {
pub location: Location,
pub message: String,
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}: {}", self.location.line_number, self.message)
}
}
pub type Result<T> = result::Result<T, Error>;
// Create an `Err` variant of `Result<X>` from a location and `format!` args.
macro_rules! err {
( $loc:expr, $msg:expr ) => {
Err($crate::Error {
location: $loc.clone(),
message: String::from($msg),
})
};
( $loc:expr, $fmt:expr, $( $arg:expr ),+ ) => {
Err($crate::Error {
location: $loc.clone(),
message: format!( $fmt, $( $arg ),+ ),
})
};
}

View File

@@ -8,12 +8,7 @@
use std::str::CharIndices;
use cretonne::ir::types;
use cretonne::ir::{Value, Ebb};
/// The location of a `Token` or `Error`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Location {
pub line_number: usize,
}
use error::Location;
/// A Token returned from the `Lexer`.
///
@@ -379,6 +374,7 @@ mod tests {
use super::*;
use cretonne::ir::types;
use cretonne::ir::{Value, Ebb};
use error::Location;
fn token<'a>(token: Token<'a>, line: usize) -> Option<Result<LocatedToken<'a>, LocatedError>> {
Some(super::token(token, Location { line_number: line }))

View File

@@ -5,11 +5,13 @@
extern crate cretonne;
pub use parser::{Result, parse_functions, parse_test};
pub use error::{Location, Result, Error};
pub use parser::{parse_functions, parse_test};
pub use testcommand::{TestCommand, TestOption};
pub use testfile::{TestFile, Details};
pub use sourcemap::SourceMap;
mod error;
mod lexer;
mod parser;
mod testcommand;

View File

@@ -6,12 +6,9 @@
// ====--------------------------------------------------------------------------------------====//
use std::collections::HashMap;
use std::result;
use std::fmt::{self, Display, Formatter};
use std::str::FromStr;
use std::u32;
use std::mem;
use lexer::{self, Lexer, Token};
use cretonne::ir::{Function, Ebb, Inst, Opcode, Value, Type, FunctionName, StackSlot,
StackSlotData, JumpTable, JumpTableData};
use cretonne::ir::types::{VOID, Signature, ArgumentType, ArgumentExtension};
@@ -20,11 +17,11 @@ use cretonne::ir::entities::{AnyEntity, NO_EBB, NO_INST, NO_VALUE};
use cretonne::ir::instructions::{InstructionFormat, InstructionData, VariableArgs, JumpData,
BranchData, ReturnData};
use testfile::{TestFile, Details, Comment};
use error::{Location, Error, Result};
use lexer::{self, Lexer, Token};
use testcommand::TestCommand;
use sourcemap;
pub use lexer::Location;
/// Parse the entire `text` into a list of functions.
///
/// Any test commands or ISA declarations are ignored.
@@ -43,38 +40,6 @@ pub fn parse_test<'a>(text: &'a str) -> Result<TestFile<'a>> {
})
}
/// A parse error is returned when the parse failed.
#[derive(Debug)]
pub struct Error {
pub location: Location,
pub message: String,
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{}: {}", self.location.line_number, self.message)
}
}
pub type Result<T> = result::Result<T, Error>;
// Create an `Err` variant of `Result<X>` from a location and `format!` args.
macro_rules! err {
( $loc:expr, $msg:expr ) => {
Err(Error {
location: $loc.clone(),
message: String::from($msg),
})
};
( $loc:expr, $fmt:expr, $( $arg:expr ),+ ) => {
Err(Error {
location: $loc.clone(),
message: format!( $fmt, $( $arg ),+ ),
})
};
}
pub struct Parser<'a> {
lex: Lexer<'a>,
@@ -1225,6 +1190,7 @@ mod tests {
use cretonne::ir::types::{self, ArgumentType, ArgumentExtension};
use cretonne::ir::entities::AnyEntity;
use testfile::{Details, Comment};
use error::Error;
#[test]
fn argument_type() {

View File

@@ -8,7 +8,7 @@ use cretonne::ir::Function;
use cretonne::ir::entities::AnyEntity;
use testcommand::TestCommand;
use sourcemap::SourceMap;
use parser::Location;
use error::Location;
/// A parsed test case.
///