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
1
vote
1 answer

Is there a way to dynamically `alt` an iterator of parsers?

I'm looking to create a function like alt that instead of consuming parsers from a tuple, it consumes parsers from an iterator. The trouble I'm having is with the bound FnMut on the parameterized parsers. Parser is implemented for FnMut, but not…
Wayne Van Son
  • 310
  • 1
  • 8
1
vote
1 answer

Nom 7 doesn't backtrack when `alt` branch fails to parse

I am encountering some odd behavior in Nom 7 that I don't understand. I was under the impression that a failed parse in a branch of alt would backtrack and try the next branch of the alt, but this seems not to be the case. In my case, I am trying to…
BallpointBen
  • 9,406
  • 1
  • 32
  • 62
1
vote
2 answers

Custom Nom parser error without custom ErrorKind

I have a small parser that fails if the number it parses is out of bounds, use nom::IResult; use nom::bytes::complete::tag; use nom::character::complete::digit1; use nom::combinator::fail; fn dup(s: &str) -> IResult<&str, u64> { let (s, _) =…
sshine
  • 15,635
  • 1
  • 41
  • 66
1
vote
1 answer

Extract substring between `\"` with nom

From input "\"Name 1\" something else" I want to extract "Name 1" and the remaining string as " something else". Notice the escaped \". My current solutions is use nom::bytes::complete::{tag, is_not}; use nom::sequence::pair; use nom::IResult; fn…
Daniel
  • 980
  • 9
  • 20
1
vote
1 answer

Keep the LocatedSpan of an "outer" parser with nom in Rust

I am trying to write a parser using the nom crate (and the nom_locate) that can parse strings such as u{12a}, i.e.: u\{([0-9a-fA-F]{1,6})\} I wrote the following parser combinator: use nom::bytes::complete::{take_while_m_n}; use…
Victor
  • 13,914
  • 19
  • 78
  • 147
1
vote
1 answer

How to write fallible nom parsers

If I want to write a nom parser that could fail internally, how do I propagate the error? As an example, something to parse a NaiveDate might look like: fn parse_date(i: &str) -> IResult<&str, NaiveDate> { map(take(10), |s|…
Increasingly Idiotic
  • 5,700
  • 5
  • 35
  • 73
1
vote
2 answers

How to implement "take while, but at most N characters" in nom?

How can I give nom's take_while an upper limit on the number of characters it should match? I want to take characters according to a certain condition, but at most N. Since this will be a very performance critical part of the parser, I'd like to…
bluenote10
  • 23,414
  • 14
  • 122
  • 178
1
vote
0 answers

Is there good solution to parse Scheme's multiline string literal syntax with nom?

I am trying to write Scheme programming language syntax parser to learn nom. Now I am facing a problem with parsing string literals, is there a good solution? The problem is following: Scheme can include code point literal in string. (e.g. \x03bb;…
ryutei
  • 11
  • 1
1
vote
1 answer

Cannot infer type for type parameter Error when parsing with nom

I have this test with nom version 7.1: use nom::bytes::complete::tag; #[test] fn test() { let (s, t) = tag("1")("123").unwrap(); } Running cargo test gives error[E0283]: type annotations needed --> src/main.rs:5:18 | 5 | let (s, t) =…
cilmutulta
  • 21
  • 4
1
vote
1 answer

Rust compiler not recognizing a generic trait bound implemented for a struct

I am using Nom, the parser combinator, to write a TOML parser. The parser function I am having trouble with parses a date time string using the chrono crate. fn offset_datetime<'a, E: ParseError<&'a str>>(input: &'a str) -> IResult<&'a str,…
ErrantSky
  • 13
  • 2
1
vote
2 answers

Why can't I use the same parser twice in a tuple()?

I have written the following nom parser code: use nom::character::complete::digit1; use nom::combinator::map_res; use nom::error::Error; use nom::sequence::tuple; use nom::Err; type E<'a> = Err>; fn parse_double_int(input: &str) ->…
insitu
  • 4,488
  • 3
  • 25
  • 42
1
vote
1 answer

Read binary u32 using nom

I'm having a hard time finding any useful examples of how to use nom to parse a binary file, since it seems that the documentation is heavily biased towards &str input parsers. I just want to make a function here that will read 4 bytes, turn it into…
Colin Basnett
  • 4,052
  • 2
  • 30
  • 49
1
vote
0 answers

Streaming version for Rust (nom 7) multiline parser

I'm trying to learn NOM, but I'm also new in Rust. I have a text file with words on each line. I want to split it to 2 files: valid ASCII (without control codes) and everything else. extern crate nom; use std::fs::File; use std::io::{prelude::*,…
Quiz
  • 514
  • 1
  • 7
  • 13
1
vote
1 answer

How to use Rust nom to get the last occurrence of a string?

I'm learning Rust as well as the nom crate. I have a string and it could be one of * abc * abc, test.txt * abc, def, test.txt * abc, test.txt, test.txt I want to write a parser that gets the ending file name and all other parts as a tuple. So for…
Just a learner
  • 26,690
  • 50
  • 155
  • 234
1
vote
1 answer

creating an error to match nom::IResult in nom 6.2.1

In nom 5.0 this compiles fine: fn escaped_space(i: &str) -> nom::IResult<&str, &str> { nom::combinator::value(" ", nom::bytes::complete::tag("040"))(i) } assert_eq!(escaped_space(" "), Err(nom::Err::Error((" ",…
ss7
  • 2,902
  • 7
  • 41
  • 90
1 2 3
8 9