Questions tagged [nom]

Nom is a parser combinators library written in Rust.

Nom is a parser combinators library written in Rust. Its goal is to provide tools to build safe parsers without compromising the speed or memory consumption. To that end, it uses extensively Rust's strong typing, zero copy parsing, push streaming, pull streaming, and provides macros and traits to abstract most of the error prone plumbing.

128 questions
3
votes
1 answer

Conditionally parsing an array based on previous elements with nom

I need to parse an array of 32 bit ints (little endian), from an array of u8s, however the next int only exists if the 31st bit of the current int is set. If the rest don't exist then the rest of the array should be set to zeroes. I'm not sure how…
Ross MacArthur
  • 4,735
  • 1
  • 24
  • 36
3
votes
1 answer

What is the right way to return a simple custom error with nom errorkind?

A simple error handler added to nom The first one compiles with no errors, the second one errors out use nom::*; use std::str; // This works named!( field<&str>, map!( complete!(add_return_error!( ErrorKind::Custom(1), …
Delta_Fore
  • 3,079
  • 4
  • 26
  • 46
3
votes
0 answers

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

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…
squidpickles
  • 1,737
  • 19
  • 27
3
votes
1 answer

How to match exactly one byte using nom?

I want to match exactly one alphabetic character (a-zA-Z) with nom. I know I can match greedily using take_while! with something like this: // match one or more alphabetical characters pub fn alpha_many(input: &[u8]) -> IResult<&[u8], &[u8]> { …
little-dude
  • 1,544
  • 2
  • 17
  • 33
3
votes
1 answer

Adding state to a nom parser

I wrote a parser in nom that is completely stateless, now I need to wrap it in a few stateful layers. I have a top-level parsing function named alt_fn that will provide me the next bit of parsed output as an enum variant, the details of which…
Camden Narzt
  • 2,271
  • 1
  • 23
  • 42
3
votes
1 answer

Parsing camel case strings with nom

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…
Max
  • 15,693
  • 14
  • 81
  • 131
2
votes
1 answer

How to build a negative lookahead parser in nom?

How can I create a negative lookahead parser for nom? For example, I'd like to parse "hello", except if it's followed by " world". The equivalent regex would be hello(?! world). I tried to combine the cond, not and peek parsers fn parser(input:…
mdcq
  • 1,593
  • 13
  • 30
2
votes
0 answers

Nom errors over bytes dont actually implement Error

I am building a pretty simple parser over &[u8]. Unfortunately, when I try to convert the errors I get into anyhow errors, it informs me that the various nom Error types (nom::error::Error and nom::error::VerboseError) don't implement Display, and…
kazagistar
  • 1,537
  • 8
  • 20
2
votes
1 answer

Rust Nom take_until with parser and not pattern

Using latest (v7) nom crate. Trying to build a parser capable of extracting code blocks from markdown. In the flavor of markdown I need to support, a code block only ends if there is three grave/backtick characters on a line by themselves, excepting…
Keozon
  • 998
  • 10
  • 25
2
votes
1 answer

Rust + nom: how to wrap a parser to emit a token?

So I'm trying to port a parser written in Javascript with parsimmon. In the parser I feed a string source input and get tokens. A token is simply an object with a type and a pair of offset values to mark its location in the source: enum TokenType { …
hillin
  • 1,603
  • 15
  • 21
2
votes
1 answer

How can I specify types for a parsing function?

I'm trying to parse indentation levels with the following function: fn get_indent_level(input: &str) -> usize { let (_, result) = many0_count(alt((tag("L_ "), tag("| "), tag(" "))))(input).unwrap_or_default(); result } I got this…
user130268
  • 1,341
  • 2
  • 12
  • 20
2
votes
1 answer

A weird question about parsing http header by the crate Nom in rust

I wrote some code that uses NOM to parse HTTP headers, but got some strange results, as shown in the example. In the main function, string input1 has only one more semicolon at the end than input2, but they got completely different results.…
2
votes
0 answers

Choosing between function traits and Parser trait in Rust nom

I have experience using Haskell parsec libraries, and in Haskell, we just use functions everywhere. Rust nom provides Parser trait and any type that implements it can be used like instance.parse(intput). But combinators like map do not return…
Shuumatsu
  • 592
  • 2
  • 5
  • 12
2
votes
1 answer

Iterating over Multiple Lines Using the Rust NOM Parsing Library

I'm trying to learn NOM for a project in Rust. I have a text file that consists of: [tag="#43674"]char[/tag] with multiple tags back to back on each line. I'm trying to pull the '#43674' and 'char', store them in a tuple (x, y) and then push those…
ZU420
  • 23
  • 5
2
votes
1 answer

How do I use a parser within a parser in nom?

I'm trying to parse a format that contains some data in a compressed format. How do I parse the decompressed part as part of the regular parsing? Currently I'm running into issues with the borrow checker because the error part of nom would like to…
Reactormonk
  • 21,472
  • 14
  • 74
  • 123
1 2
3
8 9