Allow no_std usage through the "core" feature

This commit is contained in:
Geoffroy Couprie 2015-05-16 20:39:39 +02:00
parent 6e9167bdbf
commit 347962cb71
5 changed files with 57 additions and 2 deletions

View File

@ -9,3 +9,6 @@ repository = "https://github.com/Geal/nom"
readme = "README.md"
documentation = "http://rust.unhandledexpression.com/nom/"
keywords = ["parser", "parser-combinators", "parsing", "streaming"]
[features]
core = []

View File

@ -2,6 +2,10 @@
use self::IResult::*;
#[cfg(feature = "core")]
use std::prelude::v1::*;
use std::boxed::Box;
/*
/// (Experimental) Closure used to hold the temporary state of resumable parsing
pub type IResultClosure<'a,I,O> = Box<FnMut(I) -> IResult<I,O> +'a>;

View File

@ -99,17 +99,46 @@
//! ```
//!
#![cfg_attr(feature = "core", feature(no_std))]
#![cfg_attr(feature = "core", feature(core))]
#![cfg_attr(feature = "core", feature(collections))]
#![cfg_attr(feature = "core", no_std)]
#[macro_use]
#[cfg(feature = "core")]
extern crate core;
#[cfg(feature = "core")]
extern crate collections;
#[cfg(feature = "core")]
mod std {
#[macro_use]
pub use core::{fmt, iter, option, ops, slice, mem};
pub use collections::{boxed, vec, string};
pub mod prelude {
pub use core::prelude as v1;
}
}
pub use self::util::*;
pub use self::internal::*;//{IResult, IResultClosure, GetInput, GetOutput};
pub use self::macros::*;
#[cfg(not(feature = "core"))]
pub use self::producer::*;//{ProducerState,Producer,FileProducer,MemProducer};
#[cfg(not(feature = "core"))]
pub use self::consumer::*;//{ConsumerState,Consumer};
pub use self::nom::*;
#[macro_use] pub mod util;
pub mod internal;
#[macro_use] pub mod macros;
#[macro_use] pub mod producer;
#[macro_use]
#[cfg(not(feature = "core"))]
pub mod producer;
#[cfg(not(feature = "core"))]
pub mod consumer;
#[macro_use] pub mod nom;

View File

@ -6,6 +6,10 @@
//! but the macros system makes no promises.
//!
#[cfg(feature = "core")]
use std::prelude::v1::*;
use std::boxed::Box;
use std::fmt::Debug;
use internal::*;
use internal::IResult::*;
@ -23,6 +27,7 @@ pub fn tag_cl<'a,'b>(rec:&'a[u8]) -> Box<Fn(&'b[u8]) -> IResult<'b, &'b[u8], &'
})
}
#[cfg(not(feature = "core"))]
pub fn print<'a,T: Debug>(input: T) -> IResult<'a,T, ()> {
println!("{:?}", input);
Done(input, ())

View File

@ -1,6 +1,14 @@
use internal::{IResult,Err};
#[cfg(not(feature = "core"))]
use std::collections::HashMap;
#[cfg(feature = "core")]
use std::prelude::v1::*;
use std::vec::Vec;
use std::string::ToString;
#[cfg(not(feature = "core"))]
pub trait HexDisplay {
fn offset(&self, second:&[u8]) -> usize;
@ -13,7 +21,7 @@ pub trait HexDisplay {
static CHARS: &'static[u8] = b"0123456789abcdef";
#[cfg(not(feature = "core"))]
impl HexDisplay for [u8] {
fn offset(&self, second:&[u8]) -> usize {
let fst = self.as_ptr();
@ -168,6 +176,7 @@ pub fn compare_error_paths(e1:Err, e2:Err) -> bool {
return error_to_list(e1) == error_to_list(e2)
}
#[cfg(not(feature = "core"))]
pub fn add_error_pattern<'a,'b,I,O>(h: &mut HashMap<Vec<u32>, &'b str>, res: IResult<'a,I,O>, message: &'b str) -> bool {
if let IResult::Error(e) = res {
h.insert(error_to_list(e), message);
@ -184,6 +193,7 @@ pub fn slice_to_offsets(input: &[u8], s: &[u8]) -> (usize, usize) {
return (off1, off2);
}
#[cfg(not(feature = "core"))]
pub fn prepare_errors<I,O>(input: &[u8], res: IResult<I,O>) -> Option<Vec<(u32, usize, usize)> > {
if let IResult::Error(e) = res {
let mut v:Vec<(u32, usize, usize)> = Vec::new();
@ -216,6 +226,7 @@ pub fn prepare_errors<I,O>(input: &[u8], res: IResult<I,O>) -> Option<Vec<(u32,
}
}
#[cfg(not(feature = "core"))]
pub fn print_error<I,O>(input: &[u8], res: IResult<I,O>) {
if let Some(v) = prepare_errors(input, res) {
let colors = generate_colors(&v);
@ -227,6 +238,7 @@ pub fn print_error<I,O>(input: &[u8], res: IResult<I,O>) {
}
}
#[cfg(not(feature = "core"))]
pub fn generate_colors(v: &Vec<(u32, usize, usize)>) -> HashMap<u32, u8> {
let mut h: HashMap<u32, u8> = HashMap::new();
let mut color = 0;
@ -277,6 +289,7 @@ pub fn write_color(v: &mut Vec<u8>, color: u8) {
v.push('m' as u8);
}
#[cfg(not(feature = "core"))]
pub fn print_codes(colors: HashMap<u32, u8>, names: HashMap<u32, &str>) -> String {
let mut v = Vec::new();
for (code, &color) in &colors {
@ -301,6 +314,7 @@ pub fn print_codes(colors: HashMap<u32, u8>, names: HashMap<u32, &str>) -> Strin
}
#[cfg(not(feature = "core"))]
pub fn print_offsets(input: &[u8], from: usize, offsets: &Vec<(u32, usize, usize)>) -> String {
let mut v = Vec::with_capacity(input.len() * 3);
let mut i = from;