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
9
votes
3 answers

How do I create a streaming parser in nom?

I've created a few non-trivial parsers in nom, so I'm pretty familiar with it at this point. All the parsers I've created until now always provide the entire input slice to the parser. I'd like to create a streaming parser, which I assume means that…
w.brian
  • 16,296
  • 14
  • 69
  • 118
8
votes
1 answer

How do I make a nom whitespace parser that also skips line-oriented comments?

I'm writing a parser for a text-based format in nom 4.2.2, and I'm using the whitespace facility to skip whitespace. I have to use a custom parser because this format treats some unusual characters as whitespace. Following the example on that page,…
Dan Hulme
  • 14,779
  • 3
  • 46
  • 95
7
votes
1 answer

How do I match a CSV-style quoted string in nom?

A CSV style quoted string, for the purposes of this question, is a string in which: The string starts and ends with exactly one ". Two double quotes inside the string are collapsed to one double quote. "Alo""ha"→Alo"ha. "" on its own is an empty…
Fredrick Brennan
  • 7,079
  • 2
  • 30
  • 61
6
votes
1 answer

How to parse a symmetric quoted string using nom in rust?

How should I parse a quoted string similar to rust's raw strings using nom? I want to parse the following: "A standard string" #"A string containing ["] a quote"# ##"A string containing ["#] a quote and hash "## How would I do this, requiring equal…
Marty
  • 1,077
  • 1
  • 10
  • 14
6
votes
2 answers

Parsing an integer with nom always results in Incomplete

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(("",…
spease
  • 686
  • 6
  • 15
5
votes
2 answers

nom parser borrow checker issue

I have this Rust program using nom 4.2.2. (I have taken the liberty of expanding the nom parser function.) extern crate failure; extern crate nom; use failure::Error; use std::fs::File; use std::io::Read; fn nom_parser(i: &[u8]) ->…
Bittrance
  • 2,202
  • 2
  • 20
  • 29
5
votes
3 answers

How do I use nom to parse a string with sign into an i32?

Using the nom crate, I'm trying to write a parser that can recognize signed i32 number from a String, i.e. can transform the string -42 into the i32 representation. So far I've come up with the following, but I'm failing to parse negative…
Nicolas C
  • 1,584
  • 2
  • 17
  • 33
5
votes
2 answers

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

I'm trying to split a log line on space and commas in order to create a Vector of Tokens 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…
mchlstckl
  • 3,390
  • 2
  • 21
  • 21
5
votes
1 answer

Where does nom's "$i" macro argument come from?

I'm trying to understand how Rust macro captures work and am looking at the nom parser library. Location nom/src/bytes.rs declares macro tag! which captures with ($i:expr, $tag: expr). However throughout the places where tag! is used it is used…
Konrad Eisele
  • 3,088
  • 20
  • 35
5
votes
3 answers

Optional field with strict format

I am trying to build nom parser to examine URLs with ID as UUID rooms/e19c94cf-53eb-4048-9c94-7ae74ff6d912 I created the following: extern crate uuid; use uuid::Uuid; named!(room_uuid<&str, Option>, do_parse!( tag_s!("rooms") >> …
Aleksey
  • 2,289
  • 17
  • 27
4
votes
1 answer

Parse allowing nested parentheses in nom

I'm using nom. I'd like to parse a string that's surrounded by parentheses, and allowing for additional nested parentheses within the string. So (a + b) would parse as a + b, and ((a + b)) would parse as (a + b) This works for the first case, but…
Maximilian
  • 7,512
  • 3
  • 50
  • 63
4
votes
2 answers

How to use nom to parse until a string is found?

It's easy to use nom to parse a string until a character is found. How to use nom to gobble a string until a delimiter or the end? deals with this. How do I do the same with a string (multiple characters) instead of a single delimiter? For example,…
loknomurze
  • 41
  • 2
4
votes
1 answer

How can I use a success combinator in conjunction with a map combinator in nom?

I am writing a nom parser combinator that consumes bytes and returns an Option<&[u8]>. The parser combinator should follow the following rules: Read a single signed, big endian 16 bit integer (s) If s is -1, return None If s is not -1, read s bits…
David Farr
  • 123
  • 1
  • 2
  • 5
4
votes
0 answers

How to implement nom::InputIter for a Vec of tokens?

I'm building a parser using nom and I'd like to use nom combinators to process tokenized input. The stream of tokens is represented by Vec>, with Token being: #[derive(Debug, Clone, PartialEq)] pub enum Token { Identifier(S), …
mpontus
  • 2,143
  • 2
  • 19
  • 21
4
votes
1 answer

How to parse slightly ambiguous data using nom?

In RFC1738, the BNF for domainlabel is the following: domainlabel = alphadigit | alphadigit *[ alphadigit | "-" ] alphadigit That is, it's either an alphadigit, or it's a string where the first/last characters have to be an alphadigit but the…
Listerone
  • 1,381
  • 1
  • 11
  • 25
1
2 3
8 9