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:
44
src/libreader/error.rs
Normal file
44
src/libreader/error.rs
Normal 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 ),+ ),
|
||||||
|
})
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -8,12 +8,7 @@
|
|||||||
use std::str::CharIndices;
|
use std::str::CharIndices;
|
||||||
use cretonne::ir::types;
|
use cretonne::ir::types;
|
||||||
use cretonne::ir::{Value, Ebb};
|
use cretonne::ir::{Value, Ebb};
|
||||||
|
use error::Location;
|
||||||
/// The location of a `Token` or `Error`.
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
||||||
pub struct Location {
|
|
||||||
pub line_number: usize,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A Token returned from the `Lexer`.
|
/// A Token returned from the `Lexer`.
|
||||||
///
|
///
|
||||||
@@ -379,6 +374,7 @@ mod tests {
|
|||||||
use super::*;
|
use super::*;
|
||||||
use cretonne::ir::types;
|
use cretonne::ir::types;
|
||||||
use cretonne::ir::{Value, Ebb};
|
use cretonne::ir::{Value, Ebb};
|
||||||
|
use error::Location;
|
||||||
|
|
||||||
fn token<'a>(token: Token<'a>, line: usize) -> Option<Result<LocatedToken<'a>, LocatedError>> {
|
fn token<'a>(token: Token<'a>, line: usize) -> Option<Result<LocatedToken<'a>, LocatedError>> {
|
||||||
Some(super::token(token, Location { line_number: line }))
|
Some(super::token(token, Location { line_number: line }))
|
||||||
|
|||||||
@@ -5,11 +5,13 @@
|
|||||||
|
|
||||||
extern crate cretonne;
|
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 testcommand::{TestCommand, TestOption};
|
||||||
pub use testfile::{TestFile, Details};
|
pub use testfile::{TestFile, Details};
|
||||||
pub use sourcemap::SourceMap;
|
pub use sourcemap::SourceMap;
|
||||||
|
|
||||||
|
mod error;
|
||||||
mod lexer;
|
mod lexer;
|
||||||
mod parser;
|
mod parser;
|
||||||
mod testcommand;
|
mod testcommand;
|
||||||
|
|||||||
@@ -6,12 +6,9 @@
|
|||||||
// ====--------------------------------------------------------------------------------------====//
|
// ====--------------------------------------------------------------------------------------====//
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::result;
|
|
||||||
use std::fmt::{self, Display, Formatter};
|
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use std::u32;
|
use std::u32;
|
||||||
use std::mem;
|
use std::mem;
|
||||||
use lexer::{self, Lexer, Token};
|
|
||||||
use cretonne::ir::{Function, Ebb, Inst, Opcode, Value, Type, FunctionName, StackSlot,
|
use cretonne::ir::{Function, Ebb, Inst, Opcode, Value, Type, FunctionName, StackSlot,
|
||||||
StackSlotData, JumpTable, JumpTableData};
|
StackSlotData, JumpTable, JumpTableData};
|
||||||
use cretonne::ir::types::{VOID, Signature, ArgumentType, ArgumentExtension};
|
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,
|
use cretonne::ir::instructions::{InstructionFormat, InstructionData, VariableArgs, JumpData,
|
||||||
BranchData, ReturnData};
|
BranchData, ReturnData};
|
||||||
use testfile::{TestFile, Details, Comment};
|
use testfile::{TestFile, Details, Comment};
|
||||||
|
use error::{Location, Error, Result};
|
||||||
|
use lexer::{self, Lexer, Token};
|
||||||
use testcommand::TestCommand;
|
use testcommand::TestCommand;
|
||||||
use sourcemap;
|
use sourcemap;
|
||||||
|
|
||||||
pub use lexer::Location;
|
|
||||||
|
|
||||||
/// Parse the entire `text` into a list of functions.
|
/// Parse the entire `text` into a list of functions.
|
||||||
///
|
///
|
||||||
/// Any test commands or ISA declarations are ignored.
|
/// 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> {
|
pub struct Parser<'a> {
|
||||||
lex: Lexer<'a>,
|
lex: Lexer<'a>,
|
||||||
|
|
||||||
@@ -1225,6 +1190,7 @@ mod tests {
|
|||||||
use cretonne::ir::types::{self, ArgumentType, ArgumentExtension};
|
use cretonne::ir::types::{self, ArgumentType, ArgumentExtension};
|
||||||
use cretonne::ir::entities::AnyEntity;
|
use cretonne::ir::entities::AnyEntity;
|
||||||
use testfile::{Details, Comment};
|
use testfile::{Details, Comment};
|
||||||
|
use error::Error;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn argument_type() {
|
fn argument_type() {
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ use cretonne::ir::Function;
|
|||||||
use cretonne::ir::entities::AnyEntity;
|
use cretonne::ir::entities::AnyEntity;
|
||||||
use testcommand::TestCommand;
|
use testcommand::TestCommand;
|
||||||
use sourcemap::SourceMap;
|
use sourcemap::SourceMap;
|
||||||
use parser::Location;
|
use error::Location;
|
||||||
|
|
||||||
/// A parsed test case.
|
/// A parsed test case.
|
||||||
///
|
///
|
||||||
|
|||||||
Reference in New Issue
Block a user