Skip to content

Commit

Permalink
Merge branch 'alloc-feature' of https://github.com/kamarkiewicz/nom i…
Browse files Browse the repository at this point in the history
…nto kamarkiewicz-alloc-feature
  • Loading branch information
Geal committed Feb 17, 2018
2 parents c3636fd + 7ab7a1b commit 43ed4e9
Show file tree
Hide file tree
Showing 14 changed files with 70 additions and 11 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ include = [
]

[features]
std = ["memchr/use_std"]
alloc = []
std = ["alloc", "memchr/use_std"]
nightly = []
default = ["std"]
regexp = ["regex"]
Expand Down
5 changes: 5 additions & 0 deletions src/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,7 @@ macro_rules! permutation_iterator (

#[cfg(test)]
mod tests {
#[cfg(feature = "alloc")]
use std::string::{String, ToString};
use internal::{Err, IResult, Needed};
use util::ErrorKind;
Expand Down Expand Up @@ -914,21 +915,25 @@ mod tests {
);
);

#[cfg(feature = "alloc")]
#[derive(Debug, Clone, PartialEq)]
pub struct ErrorStr(String);

#[cfg(feature = "alloc")]
impl From<u32> for ErrorStr {
fn from(i: u32) -> Self {
ErrorStr(format!("custom error code: {}", i))
}
}

#[cfg(feature = "alloc")]
impl<'a> From<&'a str> for ErrorStr {
fn from(i: &'a str) -> Self {
ErrorStr(format!("custom error message: {}", i))
}
}

#[cfg(feature = "alloc")]
#[test]
fn alt() {
fn work(input: &[u8]) -> IResult<&[u8], &[u8], ErrorStr> {
Expand Down
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,14 +341,15 @@
//!
//! **Going further:** read the [guides](https://github.com/Geal/nom/tree/master/doc)!
#![cfg_attr(not(feature = "std"), feature(alloc))]
#![cfg_attr(all(not(feature = "std"), feature = "alloc"), feature(alloc))]
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(feature = "nightly", feature(test))]
#![cfg_attr(feature = "nightly", feature(const_fn))]
#![cfg_attr(feature = "nightly", feature(plugin))]
//#![warn(missing_docs)]
#![cfg_attr(feature = "cargo-clippy", allow(doc_markdown))]

#[cfg(not(feature = "std"))]
#[cfg(all(not(feature = "std"), feature = "alloc"))]
#[macro_use]
extern crate alloc;
#[cfg(feature = "regexp_macros")]
Expand All @@ -362,8 +363,10 @@ extern crate test;

#[cfg(not(feature = "std"))]
mod std {
#[cfg(feature = "alloc")]
#[macro_use]
pub use alloc::{boxed, string, vec};

pub use core::{cmp, convert, fmt, iter, mem, ops, option, result, slice, str};
pub mod prelude {
pub use core::prelude as v1;
Expand Down
4 changes: 4 additions & 0 deletions src/multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

/// `separated_list!(I -> IResult<I,T>, I -> IResult<I,O>) => I -> IResult<I, Vec<O>>`
/// separated_list(sep, X) returns Vec<X> will return Incomplete if there may be more elements
#[cfg(feature = "alloc")]
#[macro_export]
macro_rules! separated_list(
($i:expr, $sep:ident!( $($args:tt)* ), $submac:ident!( $($args2:tt)* )) => (
Expand Down Expand Up @@ -381,6 +382,7 @@ macro_rules! many1(
/// error_position!(&c[..], ErrorKind::Tag)))));
/// # }
/// ```
#[cfg(feature = "alloc")]
#[macro_export]
macro_rules! many_till(
(__impl $i:expr, $submac1:ident!( $($args1:tt)* ), $submac2:ident!( $($args2:tt)* )) => (
Expand Down Expand Up @@ -468,6 +470,7 @@ macro_rules! many_till(
/// assert_eq!(multi(&c[..]),Ok((&b"abcdefgh"[..], res2)));
/// # }
/// ```
#[cfg(feature = "alloc")]
#[macro_export]
macro_rules! many_m_n(
($i:expr, $m:expr, $n: expr, $submac:ident!( $($args:tt)* )) => (
Expand Down Expand Up @@ -557,6 +560,7 @@ macro_rules! many_m_n(
/// # }
/// ```
///
#[cfg(feature = "alloc")]
#[macro_export]
macro_rules! count(
($i:expr, $submac:ident!( $($args:tt)* ), $count: expr) => (
Expand Down
3 changes: 3 additions & 0 deletions src/nom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//! but the macros system makes no promises.
//!

#[cfg(feature = "alloc")]
use std::boxed::Box;

#[cfg(feature = "std")]
Expand All @@ -18,6 +19,7 @@ use traits::{Compare, CompareResult, Offset, Slice};
use util::ErrorKind;
use std::mem::transmute;

#[cfg(feature = "alloc")]
#[inline]
pub fn tag_cl<'a, 'b>(rec: &'a [u8]) -> Box<Fn(&'b [u8]) -> IResult<&'b [u8], &'b [u8]> + 'a> {
Box::new(move |i: &'b [u8]| -> IResult<&'b [u8], &'b [u8]> {
Expand Down Expand Up @@ -950,6 +952,7 @@ mod tests {
use types::{CompleteByteSlice, CompleteStr};

#[test]
#[cfg(feature = "alloc")]
fn tag_closure() {
let x = tag_cl(&b"abcd"[..]);
let r = x(&b"abcdabcdefgh"[..]);
Expand Down
27 changes: 21 additions & 6 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,14 @@ pub trait AsBytes {
impl<'a> AsBytes for &'a str {
#[inline(always)]
fn as_bytes(&self) -> &[u8] {
str::as_bytes(self)
<str as AsBytes>::as_bytes(self)
}
}

impl AsBytes for str {
#[inline(always)]
fn as_bytes(&self) -> &[u8] {
str::as_bytes(self)
self.as_ref()
}
}

Expand Down Expand Up @@ -250,10 +250,18 @@ impl AsChar for char {
fn as_char(self) -> char {
self
}
#[cfg(feature = "alloc")]
#[inline]
fn is_alpha(self) -> bool {
self.is_alphabetic()
}
#[cfg(not(feature = "alloc"))]
#[inline]
fn is_alpha(self) -> bool {
unimplemented!(
"error[E0658]: use of unstable library feature 'core_char_ext': the stable interface is `impl char` in later crate (see issue #32110)"
)
}
#[inline]
fn is_alphanum(self) -> bool {
self.is_alpha() || self.is_dec_digit()
Expand Down Expand Up @@ -283,7 +291,7 @@ impl<'a> AsChar for &'a char {
}
#[inline]
fn is_alpha(self) -> bool {
self.is_alphabetic()
<char as AsChar>::is_alpha(*self)
}
#[inline]
fn is_alphanum(self) -> bool {
Expand Down Expand Up @@ -499,11 +507,11 @@ impl<'a, 'b> Compare<&'b [u8]> for &'a [u8] {
impl<'a, 'b> Compare<&'b str> for &'a [u8] {
#[inline(always)]
fn compare(&self, t: &'b str) -> CompareResult {
self.compare(str::as_bytes(t))
self.compare(AsBytes::as_bytes(t))
}
#[inline(always)]
fn compare_no_case(&self, t: &'b str) -> CompareResult {
self.compare_no_case(str::as_bytes(t))
self.compare_no_case(AsBytes::as_bytes(t))
}
}

Expand All @@ -525,6 +533,7 @@ impl<'a, 'b> Compare<&'b str> for &'a str {
}

//FIXME: this version is too simple and does not use the current locale
#[cfg(feature = "alloc")]
#[inline(always)]
fn compare_no_case(&self, t: &'b str) -> CompareResult {
let pos = self
Expand All @@ -543,6 +552,12 @@ impl<'a, 'b> Compare<&'b str> for &'a str {
}
}
}

#[cfg(not(feature = "alloc"))]
#[inline(always)]
fn compare_no_case(&self, _: &'b str) -> CompareResult {
unimplemented!()
}
}

/// look for self in the given input stream
Expand Down Expand Up @@ -635,7 +650,7 @@ impl<'a, 'b> FindSubstring<&'b [u8]> for &'a [u8] {

impl<'a, 'b> FindSubstring<&'b str> for &'a [u8] {
fn find_substring(&self, substr: &'b str) -> Option<usize> {
self.find_substring(str::as_bytes(substr))
self.find_substring(AsBytes::as_bytes(substr))
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ impl<'a> Offset for CompleteStr<'a> {

impl<'a> AsBytes for CompleteStr<'a> {
fn as_bytes(&self) -> &[u8] {
self.0.as_bytes()
AsBytes::as_bytes(self.0)
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use verbose_errors::Context;
#[cfg(feature = "std")]
use std::collections::HashMap;

#[cfg(feature = "alloc")]
use std::vec::Vec;
#[cfg(feature = "alloc")]
use std::string::ToString;

#[cfg(feature = "std")]
Expand Down Expand Up @@ -285,13 +287,15 @@ pub fn code_from_offset<E>(v: &[(ErrorKind<E>, usize, usize)], offset: usize) ->
}
}

#[cfg(feature = "alloc")]
pub fn reset_color(v: &mut Vec<u8>) {
v.push(0x1B);
v.push(b'[');
v.push(0);
v.push(b'm');
}

#[cfg(feature = "alloc")]
pub fn write_color(v: &mut Vec<u8>, color: u8) {
v.push(0x1B);
v.push(b'[');
Expand Down
8 changes: 8 additions & 0 deletions src/whitespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,7 @@ macro_rules! switch_sep (
);

#[doc(hidden)]
#[cfg(feature = "alloc")]
#[macro_export]
macro_rules! separated_list_sep (
($i:expr, $separator:path, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => (
Expand Down Expand Up @@ -891,6 +892,7 @@ macro_rules! ws (
#[cfg(test)]
#[allow(dead_code)]
mod tests {
#[cfg(feature = "alloc")]
use std::string::{String, ToString};
use internal::{Err, IResult, Needed};
use super::sp;
Expand Down Expand Up @@ -1041,21 +1043,25 @@ mod tests {
assert_eq!(perm(e), Err(Err::Incomplete(Needed::Size(4))));
}

#[cfg(feature = "alloc")]
#[derive(Debug, Clone, PartialEq)]
pub struct ErrorStr(String);

#[cfg(feature = "alloc")]
impl From<u32> for ErrorStr {
fn from(i: u32) -> Self {
ErrorStr(format!("custom error code: {}", i))
}
}

#[cfg(feature = "alloc")]
impl<'a> From<&'a str> for ErrorStr {
fn from(i: &'a str) -> Self {
ErrorStr(format!("custom error message: {}", i))
}
}

#[cfg(feature = "alloc")]
#[test]
fn alt() {
fn work(input: &[u8]) -> IResult<&[u8], &[u8], ErrorStr> {
Expand Down Expand Up @@ -1159,6 +1165,7 @@ mod tests {

// test whitespace parser generation for alt
named!(space, tag!(" "));
#[cfg(feature = "alloc")]
named!(pipeline_statement<&[u8], ()>,
ws!(
do_parse!(
Expand All @@ -1178,6 +1185,7 @@ mod tests {
)
);

#[cfg(feature = "alloc")]
named!(
fail<&[u8]>,
map!(many_till!(take!(1), ws!(tag!("."))), |(r, _)| r[0])
Expand Down
1 change: 1 addition & 0 deletions tests/custom_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ fn test3(input: &str) -> IResult<&str, &str, CustomError> {
})
}

#[cfg(feature = "alloc")]
fn test4(input: &str) -> IResult<&str, Vec<&str>, CustomError> {
count!(input, test1, 4)
}
4 changes: 4 additions & 0 deletions tests/inference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#![allow(unused_comparisons)]
#![allow(unused_doc_comment)]
#![allow(unused_variables)]
#![allow(unused_imports)]

#[macro_use]
extern crate nom;
Expand All @@ -15,6 +16,7 @@ use nom::{alpha, is_digit};
named!(multi<&[u8], () >, fold_many0!( take_while1!( is_digit ), (), |_, _| {}));

// issue #561
#[cfg(feature = "alloc")]
named!(
value<Vec<Vec<&str>>>,
do_parse!(
Expand All @@ -32,6 +34,7 @@ named!(
);

// issue #534
#[cfg(feature = "alloc")]
fn wrap_suffix(input: &Option<Vec<&[u8]>>) -> Option<String> {
if input.is_some() {
///
Expand All @@ -44,6 +47,7 @@ fn wrap_suffix(input: &Option<Vec<&[u8]>>) -> Option<String> {
}
}

#[cfg(feature = "alloc")]
named!(parse_suffix<&[u8],Option<String>>,do_parse!(
u: opt!(many1!(alt_complete!(
tag!("%") | tag!("#") | tag!("@") | alpha
Expand Down
4 changes: 3 additions & 1 deletion tests/issues.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#[macro_use]
extern crate nom;

use nom::{space, Err, IResult, Needed, be_u16, le_u64};
use nom::{space, Err, IResult, Needed, le_u64};
use nom::types::CompleteByteSlice;

#[allow(dead_code)]
Expand Down Expand Up @@ -105,6 +105,7 @@ mod parse_int {

#[test]
fn usize_length_bytes_issue() {
use nom::be_u16;
let _: IResult<&[u8], &[u8], u32> = length_bytes!(b"012346", be_u16);
}

Expand Down Expand Up @@ -148,6 +149,7 @@ named!(issue_308(&str) -> bool,
) >>
(b) ));

#[cfg(feature = "alloc")]
fn issue_302(input: &[u8]) -> IResult<&[u8], Option<Vec<u64>>> {
do_parse!(input, entries: cond!(true, count!(le_u64, 3)) >> (entries))
}
Expand Down
1 change: 1 addition & 0 deletions tests/json.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg(feature = "alloc")]
//#![feature(trace_macros)]

#[macro_use]
Expand Down
Loading

0 comments on commit 43ed4e9

Please sign in to comment.