Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added EOF parser. #31

Merged
merged 1 commit into from
May 5, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ There is already a large list of parsers available, like:
- **multispace**: will return the longest array containing space, \r or \n
- **be_u8**, **be_u16**, **be_u32**, **be_u64** to parse big endian unsigned integers of multiple sizes
- **be_f32**, **be_f64** to parse big endian floating point numbers
- **eof**: a parser that is successful only if the input is over. In any other case, it returns an error.

#### Making new parsers with macros

Expand Down
20 changes: 20 additions & 0 deletions src/nom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,14 @@ pub fn be_f64(input: &[u8]) -> IResult<&[u8], f64> {
}
}

pub fn eof(input:&[u8]) -> IResult<&[u8], &[u8]> {
if input.len() == 0 {
Done(input, input)
} else {
Error(Position(0, input))
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -353,4 +361,16 @@ mod tests {
assert_eq!(Incomplete(Needed::Size(9)), res3);
}

#[test]
fn end_of_input() {
let not_over = &b"Hello, world!"[..];
let is_over = &b""[..];

let res_not_over = eof(not_over);
assert_eq!(res_not_over, Error(Position(0, not_over)));

let res_over = eof(is_over);
assert_eq!(res_over, Done(is_over, is_over));
}

}