Crate nom [−] [src]
nom, eating data byte by byte
nom is a parser combinator library with a focus on safe parsing, streaming patterns, and as much as possible zero copy.
The code is available on Github
Example
#[macro_use] extern crate nom; use nom::{IResult,digit}; use nom::IResult::*; // Parser definition use std::str; use std::str::FromStr; named!(parens<i64>, delimited!( char!('('), expr, char!(')') ) ); named!(i64_digit<i64>, map_res!( map_res!( digit, str::from_utf8 ), FromStr::from_str ) ); // We transform an integer string into a i64 // we look for a digit suite, and try to convert it. // if either str::from_utf8 or FromStr::from_str fail, // the parser will fail named!(factor<i64>, alt!( i64_digit | parens ) ); // we define acc as mutable to update its value whenever a new term is found named!(term <i64>, chain!( mut acc: factor ~ many0!( alt!( tap!(mul: preceded!(tag!("*"), factor) => acc = acc * mul) | tap!(div: preceded!(tag!("/"), factor) => acc = acc / div) ) ), || { return acc } ) ); named!(expr <i64>, chain!( mut acc: term ~ many0!( alt!( tap!(add: preceded!(tag!("+"), term) => acc = acc + add) | tap!(sub: preceded!(tag!("-"), term) => acc = acc - sub) ) ), || { return acc } ) ); fn main() { assert_eq!(expr(b"1+2"), IResult::Done(&b""[..], 3)); assert_eq!(expr(b"12+6-4+3"), IResult::Done(&b""[..], 17)); assert_eq!(expr(b"1+2*3+4"), IResult::Done(&b""[..], 11)); assert_eq!(expr(b"(2)"), IResult::Done(&b""[..], 2)); assert_eq!(expr(b"2*(3+4)"), IResult::Done(&b""[..], 14)); assert_eq!(expr(b"2*2/(5-1)+3"), IResult::Done(&b""[..], 4)); }
Macros
| add_error! |
Add an error if the child parser fails |
| alt! |
|
| alt_complete! |
This is a combination of the |
| apply! |
emulate function currying: |
| apply_m! |
emulate function currying for method calls on structs
|
| bits! |
|
| call! |
Used to wrap common expressions and function as macros |
| call_m! |
Used to called methods then move self back into self |
| chain! |
|
| char! |
matches one character: `char!(char) => &[u8] -> IResult<&[u8], char> |
| closure! |
Wraps a parser in a closure |
| complete! |
replaces a |
| cond! |
|
| cond_reduce! |
|
| consumer_from_parser! | |
| count! |
|
| count_fixed! |
|
| dbg! |
Prints a message if the parser fails |
| dbg_dmp! |
Prints a message and the input if the parser fails |
| delimited! |
|
| error! |
Prevents backtracking if the child parser fails |
| escaped! |
|
| escaped_transform! |
|
| expr_opt! |
|
| expr_res! |
|
| fix_error! |
translate parser result from IResult to IResult woth a custom type |
| flat_map! |
|
| i16! |
if parameter is true, parse a big endian i16 integer, otherwise a little endian i16 integer |
| i32! |
if parameter is true, parse a big endian i32 integer, otherwise a little endian i32 integer |
| i64! |
if parameter is true, parse a big endian i64 integer, otherwise a little endian i64 integer |
| is_a! |
|
| is_a_s! |
|
| is_not! |
|
| is_not_s! |
|
| length_bytes! |
`length_bytes!(&[T] -> IResult<&[T], nb>) => &[T] -> IResult<&[T], &[T]> gets a number from the first parser, then extracts that many bytes from the remaining stream |
| length_value! |
|
| many0! |
|
| many1! |
|
| many_m_n! |
|
| map! |
|
| map_opt! |
|
| map_res! |
|
| method! |
Makes a method from a parser combination |
| named! |
Makes a function from a parser combination |
| none_of! |
matches anything but the provided characters |
| one_of! |
matches one of the provided characters |
| opt! |
|
| opt_res! |
|
| pair! |
|
| peek! |
|
| preceded! |
|
| recognize! |
|
| separated_list! |
|
| separated_nonempty_list! |
|
| separated_pair! |
|
| switch! |
|
| tag! |
|
| tag_bits! |
matches an integer pattern to a bitstream. The number of bits of the input to compare must be specified |
| tag_s! |
|
| take! |
|
| take_bits! |
|
| take_s! |
|
| take_str! |
|
| take_till! |
|
| take_till_s! |
|
| take_until! |
|
| take_until_and_consume! |
|
| take_until_and_consume_s! |
|
| take_until_either! |
|
| take_until_either_and_consume! |
|
| take_until_s! |
|
| take_while! |
|
| take_while1! |
|
| take_while1_s! |
|
| take_while_s! |
|
| tap! |
|
| terminated! |
|
| try_parse! |
A bit like |
| tuple! |
|
| u16! |
if parameter is true, parse a big endian u16 integer, otherwise a little endian u16 integer |
| u32! |
if parameter is true, parse a big endian u32 integer, otherwise a little endian u32 integer |
| u64! |
if parameter is true, parse a big endian u64 integer, otherwise a little endian u64 integer |
| value! |
|
Structs
| ChainConsumer |
ChainConsumer takes a consumer C1 R -> S, and a consumer C2 S -> T, and makes a consumer R -> T by applying C2 on C1's result |
| FileProducer | |
| MapConsumer |
MapConsumer takes a function S -> T and applies it on a consumer producing values of type S |
| MemProducer |
A MemProducer generates values from an in memory byte buffer |
| ProducerRepeat |
ProducerRepeat takes a single value, and generates it at each step |
Enums
| ConsumerState |
Stores a consumer's current computation state |
| Err |
Contains the error that a parser can return |
| ErrorKind |
indicates which parser returned an error |
| FileProducerState | |
| IResult |
Holds the result of parsing functions |
| Input | |
| Move | |
| Needed |
Contains information on needed data if a parser returned |
Traits
| AsBytes | |
| AsChar | |
| Consumer |
The Consumer trait wraps a computation and its state |
| GetInput | |
| GetOutput | |
| HexDisplay |
useful functions to calculate the offset between slices and show a hexdump of a slice |
| InputLength | |
| IterIndices | |
| Producer |
The producer wraps a data source, like file or network, and applies a consumer on it |
Functions
| add_error_pattern | |
| alpha |
Recognizes lowercase and uppercase alphabetic characters: a-zA-Z |
| alphanumeric |
Recognizes numerical and alphabetic characters: 0-9a-zA-Z |
| anychar | |
| be_f32 |
Recognizes big endian 4 bytes floating point number |
| be_f64 |
Recognizes big endian 8 bytes floating point number |
| be_i16 |
Recognizes big endian signed 2 bytes integer |
| be_i32 |
Recognizes big endian signed 4 bytes integer |
| be_i64 |
Recognizes big endian signed 8 bytes integer |
| be_i8 |
Recognizes a signed 1 byte integer (equivalent to take!(1) |
| be_u16 |
Recognizes big endian unsigned 2 bytes integer |
| be_u32 |
Recognizes big endian unsigned 4 bytes integer |
| be_u64 |
Recognizes big endian unsigned 8 bytes integer |
| be_u8 |
Recognizes an unsigned 1 byte integer (equivalent to take!(1) |
| begin | |
| code_from_offset | |
| compare_error_paths | |
| crlf | |
| digit |
Recognizes numerical characters: 0-9 |
| eof |
Recognizes empty input buffers |
| eol | |
| error_to_list | |
| error_to_u32 | |
| generate_colors | |
| hex_digit |
Recognizes hexadecimal numerical characters: 0-9, A-F, a-f |
| hex_u32 |
Recognizes a hex-encoded integer |
| is_alphabetic | |
| is_alphanumeric | |
| is_digit | |
| is_hex_digit | |
| is_oct_digit | |
| is_space | |
| le_i16 |
Recognizes little endian signed 2 bytes integer |
| le_i32 |
Recognizes little endian signed 4 bytes integer |
| le_i64 |
Recognizes little endian signed 8 bytes integer |
| le_i8 |
Recognizes a signed 1 byte integer (equivalent to take!(1) |
| le_u16 |
Recognizes little endian unsigned 2 bytes integer |
| le_u32 |
Recognizes little endian unsigned 4 bytes integer |
| le_u64 |
Recognizes little endian unsigned 8 bytes integer |
| le_u8 |
Recognizes an unsigned 1 byte integer (equivalent to take!(1) |
| length_value | |
| line_ending |
Recognizes a line feed |
| multispace |
Recognizes spaces, tabs, carriage returns and line feeds |
| newline | |
| non_empty |
Recognizes non empty buffers |
| not_line_ending | |
| oct_digit |
Recognizes octal characters: 0-7 |
| prepare_errors | |
| print_codes | |
| print_error | |
| print_offsets | |
| reset_color | |
| rest |
Return the remaining input. |
| shift | |
| sized_buffer | |
| slice_to_offsets | |
| space |
Recognizes spaces and tabs |
| tab | |
| tag_cl | |
| write_color |