2015-12-31 12:23:09 +00:00
|
|
|
//#![feature(trace_macros)]
|
2015-08-04 07:07:55 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate nom;
|
|
|
|
|
2017-05-09 11:00:21 +00:00
|
|
|
use nom::{IResult,Needed,space,digit,be_u16};
|
2015-08-04 07:07:55 +00:00
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
struct Range {
|
|
|
|
start: char,
|
|
|
|
end: char
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn take_char(input: &[u8]) -> IResult<&[u8], char> {
|
|
|
|
if input.len() > 0 {
|
|
|
|
IResult::Done(&input[1..], input[0] as char)
|
|
|
|
} else {
|
|
|
|
IResult::Incomplete(Needed::Size(1))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//trace_macros!(true);
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
named!(range<&[u8], Range>,
|
|
|
|
alt!(
|
2016-10-18 10:28:58 +00:00
|
|
|
do_parse!(
|
|
|
|
start: take_char >>
|
|
|
|
tag!("-") >>
|
|
|
|
end: take_char >>
|
|
|
|
(Range {
|
|
|
|
start: start,
|
|
|
|
end: end,
|
|
|
|
})
|
2015-08-04 07:07:55 +00:00
|
|
|
) |
|
|
|
|
map!(
|
|
|
|
take_char,
|
|
|
|
|c| {
|
|
|
|
Range {
|
|
|
|
start: c,
|
2016-10-18 10:28:58 +00:00
|
|
|
end: c,
|
2015-08-04 07:07:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
named!(literal<&[u8], Vec<char> >,
|
|
|
|
map!(
|
|
|
|
many1!(take_char),
|
|
|
|
|cs| {
|
|
|
|
cs
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_58() {
|
|
|
|
range(&b"abcd"[..]);
|
|
|
|
literal(&b"abcd"[..]);
|
|
|
|
}
|
|
|
|
|
|
|
|
//trace_macros!(false);
|
2015-12-30 15:24:10 +00:00
|
|
|
|
2017-05-09 11:00:21 +00:00
|
|
|
#[cfg(feature = "std")]
|
|
|
|
mod parse_int {
|
|
|
|
use nom::HexDisplay;
|
|
|
|
use nom::{IResult,Needed,space,digit,be_u16};
|
|
|
|
use std::str;
|
|
|
|
|
|
|
|
named!(parse_ints< Vec<i32> >, many0!(spaces_or_int));
|
|
|
|
|
|
|
|
fn spaces_or_int(input: &[u8]) -> IResult<&[u8], i32>{
|
|
|
|
println!("{}", input.to_hex(8));
|
|
|
|
do_parse!(input,
|
|
|
|
opt!(complete!(space)) >>
|
|
|
|
res: map!(complete!(digit),
|
|
|
|
|x| {
|
|
|
|
println!("x: {:?}", x);
|
|
|
|
let result = str::from_utf8(x).unwrap();
|
|
|
|
println!("Result: {}", result);
|
|
|
|
println!("int is empty?: {}", x.is_empty());
|
|
|
|
match result.parse(){
|
|
|
|
Ok(i) => i,
|
|
|
|
Err(_) => panic!("UH OH! NOT A DIGIT!")
|
|
|
|
}
|
|
|
|
}) >>
|
|
|
|
(res)
|
|
|
|
)
|
|
|
|
}
|
2015-12-30 15:24:10 +00:00
|
|
|
|
2017-05-09 11:00:21 +00:00
|
|
|
#[test]
|
|
|
|
fn issue_142(){
|
|
|
|
let subject = parse_ints(&b"12 34 5689"[..]);
|
|
|
|
let expected = IResult::Done(&b""[..], vec![12, 34, 5689]);
|
|
|
|
assert_eq!(subject, expected);
|
|
|
|
|
|
|
|
let subject = parse_ints(&b"12 34 5689 "[..]);
|
|
|
|
let expected = IResult::Done(&b" "[..], vec![12, 34, 5689]);
|
|
|
|
assert_eq!(subject, expected)
|
|
|
|
}
|
2015-12-30 15:24:10 +00:00
|
|
|
}
|
2015-12-31 12:23:09 +00:00
|
|
|
|
2016-03-28 08:07:03 +00:00
|
|
|
#[test]
|
|
|
|
fn usize_length_bytes_issue(){
|
|
|
|
length_bytes!(b"012346", be_u16);
|
|
|
|
}
|
|
|
|
|
2015-12-31 12:23:09 +00:00
|
|
|
/*
|
|
|
|
DOES NOT COMPILE
|
|
|
|
#[test]
|
|
|
|
fn issue_152() {
|
|
|
|
named!(take4, take!(4));
|
|
|
|
named!(xyz, tag!("XYZ"));
|
|
|
|
named!(abc, tag!("abc"));
|
|
|
|
|
|
|
|
|
|
|
|
named!(sw,
|
|
|
|
switch!(take4,
|
|
|
|
b"abcd" => xyz |
|
|
|
|
b"efgh" => abc
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
*/
|
2016-04-11 21:59:50 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn take_till_issue() {
|
|
|
|
named!(nothing,
|
|
|
|
take_till!(call!(|_| true))
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(nothing(b""), IResult::Done(&b""[..], &b""[..]));
|
|
|
|
assert_eq!(nothing(b"abc"), IResult::Done(&b"abc"[..], &b""[..]));
|
|
|
|
}
|