1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
//! 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](https://github.com/Geal/nom) //! //! There are a few [guides](http://rust.unhandledexpression.com/nom/home.html) with more details //! about [the design of nom](http://rust.unhandledexpression.com/nom/how_nom_macros_work.html), //! [how to write parsers](http://rust.unhandledexpression.com/nom/making_a_new_parser_from_scratch.html), //! or the [error management system](http://rust.unhandledexpression.com/nom/error_management.html). //! //! If you are upgrading to nom 2.0, please read the //! [migration document](http://rust.unhandledexpression.com/nom/upgrading_to_nom_2.html). //! //! See also the [FAQ](http://rust.unhandledexpression.com/nom/FAQ.html). //! //! # Example //! //! ``` //! #[macro_use] //! extern crate nom; //! //! use nom::{IResult,digit}; //! //! // Parser definition //! //! use std::str; //! use std::str::FromStr; //! //! // We parse any expr surrounded by parens, ignoring all whitespaces around those //! named!(parens<i64>, ws!(delimited!( tag!("("), expr, tag!(")") )) ); //! //! // We transform an integer string into a i64, ignoring surrounding whitespaces //! // We look for a digit suite, and try to convert it. //! // If either str::from_utf8 or FromStr::from_str fail, //! // we fallback to the parens parser defined above //! named!(factor<i64>, alt!( //! map_res!( //! map_res!( //! ws!(digit), //! str::from_utf8 //! ), //! FromStr::from_str //! ) //! | parens //! ) //! ); //! //! // We read an initial factor and for each time we find //! // a * or / operator followed by another factor, we do //! // the math by folding everything //! named!(term <i64>, do_parse!( //! init: factor >> //! res: fold_many0!( //! pair!(alt!(tag!("*") | tag!("/")), factor), //! init, //! |acc, (op, val): (&[u8], i64)| { //! if (op[0] as char) == '*' { acc * val } else { acc / val } //! } //! ) >> //! (res) //! ) //! ); //! //! named!(expr <i64>, do_parse!( //! init: term >> //! res: fold_many0!( //! pair!(alt!(tag!("+") | tag!("-")), term), //! init, //! |acc, (op, val): (&[u8], i64)| { //! if (op[0] as char) == '+' { acc + val } else { acc - val } //! } //! ) >> //! (res) //! ) //! ); //! //! 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)); //! } //! ``` #![cfg_attr(not(feature = "std"), feature(no_std))] #![cfg_attr(not(feature = "std"), feature(collections))] #![cfg_attr(not(feature = "std"), no_std)] #![cfg_attr(feature = "nightly", feature(test))] #![cfg_attr(feature = "nightly", feature(const_fn))] #![cfg_attr(feature = "nightly", feature(plugin))] #![cfg_attr(feature = "nightly", plugin(compiler_error))] //#![warn(missing_docs)] #[cfg(not(feature = "std"))] extern crate collections; #[cfg(feature = "regexp")] extern crate regex; #[cfg(feature = "regexp_macros")] #[macro_use] extern crate lazy_static; extern crate memchr; #[cfg(feature = "nightly")] extern crate test; #[cfg(not(feature = "nightly"))] #[allow(unused_macros)] macro_rules! compiler_error { ($e:expr) => { INVALID_NOM_SYNTAX_PLEASE_SEE_FAQ //https://github.com/Geal/nom/blob/master/doc/FAQ.md#using-nightly-to-get-better-error-messages } } #[cfg(not(feature = "std"))] mod std { #[macro_use] pub use core::{fmt, cmp, iter, option, result, ops, slice, str, mem, convert}; pub use collections::{boxed, vec, string}; pub mod prelude { pub use core::prelude as v1; } } pub use self::util::*; pub use self::traits::*; #[cfg(feature = "verbose-errors")] pub use self::verbose_errors::*; #[cfg(not(feature = "verbose-errors"))] pub use self::simple_errors::*; pub use self::internal::*; pub use self::macros::*; pub use self::branch::*; pub use self::sequence::*; pub use self::multi::*; pub use self::methods::*; pub use self::bytes::*; pub use self::bits::*; pub use self::nom::*; pub use self::character::*; pub use self::whitespace::*; #[cfg(feature = "regexp")] pub use self::regexp::*; #[cfg(feature = "std")] #[cfg(feature = "stream")] pub use self::stream::*; pub use self::str::*; #[macro_use] mod util; mod traits; #[cfg(feature = "verbose-errors")] #[macro_use] pub mod verbose_errors; #[cfg(not(feature = "verbose-errors"))] #[macro_use] pub mod simple_errors; #[macro_use] mod internal; #[macro_use] mod macros; #[macro_use] mod branch; #[macro_use] mod sequence; #[macro_use] mod multi; #[macro_use] pub mod methods; #[macro_use] mod bytes; #[macro_use] pub mod bits; #[macro_use] mod nom; #[macro_use] mod character; #[macro_use] pub mod whitespace; #[cfg(feature = "regexp")] #[macro_use] mod regexp; #[macro_use] #[cfg(feature = "std")] #[cfg(feature = "stream")] mod stream; mod str;