nom

Parsing number with nom 5.0

情到浓时终转凉″ 提交于 2020-12-13 03:04:21
问题 I'm trying to parse a large file (tens of GB) streaming using Nom 5.0. One piece of the parser tries to parse numbers: use nom::IResult; use nom::character::streaming::{char, digit1}; // use nom::character::complete::{char, digit1}; use nom::combinator::{map, opt}; use nom::multi::many1; use nom::sequence::{preceded, tuple}; pub fn number(input: &str) -> IResult<&str, &str> { map( tuple(( opt(char('-')), many1(digit1), opt(preceded(char('.'), many1(digit1))) )), |_| "0" )(input) } (Obviously,

How can I combine nom parsers to get a more bit-oriented interface to the data?

不问归期 提交于 2020-08-08 04:02:53
问题 I'm working on decoding AIS messages in Rust using nom. AIS messages are made up of a bit vector; the various fields in each message are an arbitrary number of bits long, and they don't always align on byte boundaries. This bit vector is then ASCII encoded, and embedded in an NMEA sentence. From http://catb.org/gpsd/AIVDM.html: The data payload is an ASCII-encoded bit vector. Each character represents six bits of data. To recover the six bits, subtract 48 from the ASCII character value; if

Parsing camel case strings with nom

孤者浪人 提交于 2020-01-04 06:43:11
问题 I want to parse a string like "ParseThis" or "parseThis" into a vector of strings like ["Parse", "This"] or ["parse", "this"] using the nom crate. All attempts I've tried do not return the expected result. It's possible that I don't understand yet how to use all the functions in nom. I tried: named!(camel_case<(&str)>, map_res!( take_till!(is_not_uppercase), std::str::from_utf8)); named!(p_camel_case<&[u8], Vec<&str>>, many0!(camel_case)); But p_camel_case just returns a Error(Many0) for

Parsing an integer with nom always results in Incomplete

戏子无情 提交于 2019-11-28 14:00:14
Everything I try gives me Incomplete(Size(1)) . My best guess right now is: named!(my_u64(&str) -> u64, map_res!(recognize!(nom::digit), u64::from_str) ); Test: #[cfg(test)] mod test { #[test] fn my_u64() { assert_eq!(Ok(("", 0)), super::my_u64("0")); } } Sometimes in my variations (e.g. adding complete! ) I've been able to get it to parse if I add a character onto the end. I'd like to get a working parse for this (ultimately my hope is that this will allow me to create a parser for a u64 wrapper type) but bigger picture I'd like to get a grasp of how to build a parser properly myself. Nom 4

Why does my nom parser not consume the entire input, leaving the last piece unparsed?

*爱你&永不变心* 提交于 2019-11-28 08:33:11
问题 I'm trying to split a log line on space and commas in order to create a Vector of Token s of Field and Separator as shown in the code below. My problem is that nom doesn't seem to consume the entire log line, it leaves the last part unparsed - in this case 08:33:58) . main.rs #![feature(rust_2018_preview)] #[macro_use] extern crate nom; #[derive(Debug, PartialEq)] pub enum Token<'a> { Separator(&'a [u8]), Field(&'a [u8]), } named!(separator, is_a!(" ,")); named!(not_sep, is_not!(" ,")); named

Parsing an integer with nom always results in Incomplete

允我心安 提交于 2019-11-27 08:06:48
问题 Everything I try gives me Incomplete(Size(1)) . My best guess right now is: named!(my_u64(&str) -> u64, map_res!(recognize!(nom::digit), u64::from_str) ); Test: #[cfg(test)] mod test { #[test] fn my_u64() { assert_eq!(Ok(("", 0)), super::my_u64("0")); } } Sometimes in my variations (e.g. adding complete! ) I've been able to get it to parse if I add a character onto the end. I'd like to get a working parse for this (ultimately my hope is that this will allow me to create a parser for a u64