Skip to content

Commit

Permalink
rename simple_errors to error module
Browse files Browse the repository at this point in the history
remove some dead code
  • Loading branch information
Geal committed Apr 15, 2019
1 parent 9e2bd27 commit b306b84
Show file tree
Hide file tree
Showing 23 changed files with 154 additions and 508 deletions.
2 changes: 1 addition & 1 deletion benches/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ extern crate jemallocator;
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;

use criterion::Criterion;
use nom::digit;
use nom::character::digit;

// Parser definition

Expand Down
2 changes: 1 addition & 1 deletion benches/ini.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ extern crate jemallocator;
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;

use criterion::*;
use nom::{alphanumeric, multispace, space};
use nom::character::{alphanumeric, multispace, space};

use std::str;
use std::collections::HashMap;
Expand Down
2 changes: 1 addition & 1 deletion benches/ini_complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ extern crate jemallocator;
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;

use criterion::*;
use nom::{alphanumeric, multispace, space};
use nom::character::{alphanumeric, multispace, space};
use nom::types::CompleteByteSlice;

use std::str;
Expand Down
2 changes: 1 addition & 1 deletion benches/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ extern crate jemallocator;
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;

use criterion::Criterion;
use nom::{ErrorKind, alphanumeric, number::recognize_float};
use nom::{ErrorKind, character::alphanumeric, number::recognize_float};


use std::str;
Expand Down
5 changes: 3 additions & 2 deletions examples/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ extern crate jemallocator;
#[global_allocator]
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;

use nom::{Err, ErrorKind, IResult, Offset, ParseError, VerboseError, VerboseErrorKind};
use nom::{Err, ErrorKind, IResult, Offset, error::{VerboseError, VerboseErrorKind}};
use nom::{character::alphanumeric, take_while, tag, separated_listc, alt};
use nom::{delimited, preceded, separated_list, terminated, context};
use nom::{delimited, preceded, separated_list, terminated, error::context};
use nom::character::complete::char;
use nom::number::recognize_float;
use nom::error::ParseError;
use std::str;
use std::iter::repeat;
use std::collections::HashMap;
Expand Down
3 changes: 2 additions & 1 deletion src/branch/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,8 @@ mod tests {
#[cfg(feature = "alloc")]
use lib::std::string::{String, ToString};
use lib::std::fmt::Debug;
use internal::{Err, IResult, Needed, ParseError};
use internal::{Err, IResult, Needed};
use error::ParseError;
use util::ErrorKind;

// reproduce the tag and take macros, because of module import order
Expand Down
3 changes: 2 additions & 1 deletion src/branch/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#[macro_use]
mod macros;

use internal::{Err, IResult, ParseError};
use internal::{Err, IResult};
use error::ParseError;
use util::ErrorKind;

pub fn or<I: Clone, O, E: ParseError<I>, F, G>(f: F, g: G) -> impl Fn(I) -> IResult<I, O, E>
Expand Down
4 changes: 3 additions & 1 deletion src/bytes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use traits::{
need_more, need_more_err, AtEof, Compare, CompareResult, FindSubstring, FindToken, InputIter, InputLength, InputTake,
InputTakeAtPosition, Slice, ToUsize,
};
use {Err, ErrorKind, IResult, Needed, ParseError};
use internal::{Err, IResult, Needed};
use util::ErrorKind;
use error::ParseError;

//FIXME: streaming
pub fn tag<'a, T: 'a, Input:'a, Error: ParseError<Input>>(tag: T) -> impl FnOnce(Input) -> IResult<Input, Input, Error>
Expand Down
3 changes: 2 additions & 1 deletion src/character/complete.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use internal::{Err, IResult, ParseError};
use internal::{Err, IResult};
use error::ParseError;
use ::lib::std::ops::RangeFrom;
use traits::{AsChar, AtEof, FindToken, InputIter, InputLength, Slice};
use util::ErrorKind;
Expand Down
6 changes: 4 additions & 2 deletions src/character/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ mod macros;
pub mod streaming;
pub mod complete;

use internal::{Err, IResult, Needed, ParseError};
use internal::{Err, IResult, Needed};
use error::ParseError;
use ::lib::std::ops::{Range, RangeFrom, RangeTo};
use traits::{need_more, AsChar, AtEof, FindToken, InputIter, Slice};
use traits::{InputLength, InputTakeAtPosition};
Expand Down Expand Up @@ -416,7 +417,8 @@ where
#[cfg(test)]
mod tests {
use super::*;
use internal::{Err, IResult, Needed, ParseError};
use internal::{Err, IResult, Needed};
use error::ParseError;
use types::{CompleteByteSlice, CompleteStr};


Expand Down
3 changes: 2 additions & 1 deletion src/character/streaming.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use internal::{Err, IResult, Needed, ParseError};
use internal::{Err, IResult, Needed};
use error::ParseError;
use ::lib::std::ops::RangeFrom;
use traits::{AsChar, AtEof, FindToken, InputIter, InputLength, Slice};
use util::ErrorKind;
Expand Down
148 changes: 106 additions & 42 deletions src/simple_errors.rs → src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,66 +6,130 @@
//! It contains an error code and the input position that triggered it.
//!
/*
#[derive(Debug, Clone, PartialEq)]
pub enum Context<I, E = u32> {
Code(I, ErrorKind<E>),
use internal::{Err, IResult};
use util::ErrorKind;

pub trait ParseError<I>: Sized {
fn from_error_kind(input: I, kind: ErrorKind) -> Self;

fn append(input: I, kind: ErrorKind, other: Self) -> Self;

fn from_char(input: I, _: char) -> Self {
Self::from_error_kind(input, ErrorKind::Char)
}

fn or(self, other: Self) -> Self {
other
}

fn add_context(_input: I, _ctx: &'static str, other: Self) -> Self {
other
}
}

impl<I, H: From<I>, F, E: From<F>> Convert<Context<I, F>> for Context<H, E> {
fn convert(c: Context<I, F>) -> Self {
let Context::Code(i, e) = c;
impl<I> ParseError<I> for (I, ErrorKind) {
fn from_error_kind(input: I, kind: ErrorKind) -> Self {
(input, kind)
}

Context::Code(i.into(), ErrorKind::convert(e))
fn append(_: I, _: ErrorKind, other: Self) -> Self {
other
}
}

*/
impl<I> ParseError<I> for () {
fn from_error_kind(_: I, _: ErrorKind) -> Self { }

/*
impl<I,O,E> IResult<I,O,E> {
/// Maps a `IResult<I, O, E>` to `IResult<I, O, N>` by appling a function
/// to a contained `Error` value, leaving `Done` and `Incomplete` value
/// untouched.
#[inline]
pub fn map_err<N, F>(self, f: F) -> IResult<I, O, N>
where F: FnOnce(Err<E>) -> Err<N> {
match self {
Error(e) => Error(f(e)),
Incomplete(n) => Incomplete(n),
Done(i, o) => Done(i, o),
fn append(_: I, _: ErrorKind, _: Self) -> Self { }
}

pub fn make_error<I, E: ParseError<I>>(input: I, kind: ErrorKind) -> E {
E::from_error_kind(input, kind)
}

pub fn append_error<I, E: ParseError<I>>(input: I, kind: ErrorKind, other: E) -> E {
E::append(input, kind, other)
}

#[cfg(feature = "alloc")]
#[derive(Clone,Debug,PartialEq)]
pub struct VerboseError<I> {
pub errors: ::lib::std::vec::Vec<(I, VerboseErrorKind)>,
}

#[cfg(feature = "alloc")]
#[derive(Clone,Debug,PartialEq)]
pub enum VerboseErrorKind {
Context(&'static str),
Char(char),
Nom(ErrorKind),
}

#[cfg(feature = "alloc")]
impl<I> ParseError<I> for VerboseError<I> {
fn from_error_kind(input: I, kind: ErrorKind) -> Self {
VerboseError {
errors: vec![(input, VerboseErrorKind::Nom(kind))]
}
}

/// Unwrap the contained `Error(E)` value, or panic if the `IResult` is not
/// `Error`.
pub fn unwrap_err(self) -> Err<E> {
match self {
Error(e) => e,
Done(_, _) => panic!("unwrap_err() called on an IResult that is Done"),
Incomplete(_) => panic!("unwrap_err() called on an IResult that is Incomplete"),
}
fn append(input: I, kind: ErrorKind, mut other: Self) -> Self {
other.errors.push((input, VerboseErrorKind::Nom(kind)));
other
}

/// Convert the IResult to a std::result::Result
pub fn to_full_result(self) -> Result<O, IError<E>> {
match self {
Done(_, o) => Ok(o),
Incomplete(n) => Err(IError::Incomplete(n)),
Error(e) => Err(IError::Error(e))
fn from_char(input: I, c: char) -> Self {
VerboseError {
errors: vec![(input, VerboseErrorKind::Char(c))]
}
}

/// Convert the IResult to a std::result::Result, or panic if the `IResult` is `Incomplete`
pub fn to_result(self) -> Result<O, Err<E>> {
match self {
Done(_, o) => Ok(o),
Error(e) => Err(e),
Incomplete(_) => panic!("to_result() called on an IResult that is Incomplete")
}
fn add_context(input: I, ctx: &'static str, mut other: Self) -> Self {
other.errors.push((input, VerboseErrorKind::Context(ctx)));
other
}
}

#[cfg(feature = "alloc")]
pub fn context<I: Clone, E: ParseError<I>, F, O>(context: &'static str, f: F) -> impl FnOnce(I) -> IResult<I, O, E>
where
F: Fn(I) -> IResult<I, O, E> {

move |i: I| {
match f(i.clone()) {
Ok(o) => Ok(o),
Err(Err::Incomplete(i)) => Err(Err::Incomplete(i)),
Err(Err::Error(e)) | Err(Err::Failure(e)) => {
Err(Err::Failure(E::add_context(i, context, e)))
}
}
}

}

/// creates a parse error from a `nom::ErrorKind`
/// and the position in the input
#[allow(unused_variables)]
#[macro_export(local_inner_macros)]
macro_rules! error_position(
($input:expr, $code:expr) => ({
$crate::error::make_error($input, $code)
});
);

/// creates a parse error from a `nom::ErrorKind`,
/// the position in the input and the next error in
/// the parsing tree.
#[allow(unused_variables)]
#[macro_export(local_inner_macros)]
macro_rules! error_node_position(
($input:expr, $code:expr, $next:expr) => ({
$crate::error::append_error($input, $code, $next)
});
);

/*
#[cfg(feature = "std")]
use $crate::lib::std::any::Any;
#[cfg(feature = "std")]
Expand Down
Loading

0 comments on commit b306b84

Please sign in to comment.