From feef2ecf3f556c88f50acf0341d34499ed9d7c4c Mon Sep 17 00:00:00 2001 From: Jakob Stoklund Olesen Date: Mon, 19 Sep 2016 10:08:21 -0700 Subject: [PATCH] Extract Result and Error into their own module. Also include the err! macro and make it usable outside the module. --- src/libreader/error.rs | 44 +++++++++++++++++++++++++++++++++++++++ src/libreader/lexer.rs | 8 ++----- src/libreader/lib.rs | 4 +++- src/libreader/parser.rs | 40 +++-------------------------------- src/libreader/testfile.rs | 2 +- 5 files changed, 53 insertions(+), 45 deletions(-) create mode 100644 src/libreader/error.rs diff --git a/src/libreader/error.rs b/src/libreader/error.rs new file mode 100644 index 0000000000..edc6e40649 --- /dev/null +++ b/src/libreader/error.rs @@ -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 = result::Result; + +// Create an `Err` variant of `Result` 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 ),+ ), + }) + }; +} diff --git a/src/libreader/lexer.rs b/src/libreader/lexer.rs index 6b5e881a1f..7631e306ad 100644 --- a/src/libreader/lexer.rs +++ b/src/libreader/lexer.rs @@ -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, LocatedError>> { Some(super::token(token, Location { line_number: line })) diff --git a/src/libreader/lib.rs b/src/libreader/lib.rs index 79f9b23521..f0f04fa381 100644 --- a/src/libreader/lib.rs +++ b/src/libreader/lib.rs @@ -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; diff --git a/src/libreader/parser.rs b/src/libreader/parser.rs index 6fea02b22b..68a5e72a4f 100644 --- a/src/libreader/parser.rs +++ b/src/libreader/parser.rs @@ -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> { }) } -/// 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 = result::Result; - -// Create an `Err` variant of `Result` 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() { diff --git a/src/libreader/testfile.rs b/src/libreader/testfile.rs index 8a9f8d3451..d63b1f9f9d 100644 --- a/src/libreader/testfile.rs +++ b/src/libreader/testfile.rs @@ -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. ///