Created
June 26, 2016 07:20
-
-
Save GGist/0c45b1b0603549a55dbc672e4bd19448 to your computer and use it in GitHub Desktop.
Reddit Nom Help
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "nom-test" | |
version = "0.1.0" | |
authors = ["Andrew <[email protected]>"] | |
[dependencies] | |
nom = "1.2.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[macro_use] | |
extern crate nom; | |
use nom::{IResult, alphanumeric}; | |
named!(pub atom_specials, | |
alt!( | |
tag!("(") | |
| tag!(")") | |
| tag!("{") | |
| tag!(" ") | |
| tag!("(") | |
| tag!("a") | |
) | |
); | |
pub fn atom_chars(bytes: &[u8]) -> IResult<&[u8], &[u8]> { | |
cond_reduce!(bytes, !atom_specials(bytes).is_done(), chain!( | |
alpha: peek!(alphanumeric) ~ | |
_____: take!(1) , | |
|| { &alpha[..1] } | |
)) | |
} | |
#[cfg(test)] | |
mod tests { | |
use nom::{IResult}; | |
#[test] | |
fn test_atom_specials() { | |
let input = &b"()"[..]; | |
let output = vec![&b"("[..], &b")"[..]]; | |
let received = many1!(input, super::atom_specials); | |
let expected: IResult<&[u8], Vec<&[u8]>> = IResult::Done(&b""[..], output); | |
assert_eq!(received, expected); | |
} | |
#[test] | |
fn test_atom_chars() { | |
let input = &b"0Z"[..]; | |
let output = vec![&b"0"[..], &b"Z"[..]]; | |
let received = many1!(input, super::atom_chars); | |
let expected: IResult<&[u8], Vec<&[u8]>> = IResult::Done(&b""[..], output); | |
assert_eq!(received, expected); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment