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

How to use nom to gobble a string until a delimiter or the end?

I'm learning nom, and as a test example I'm trying to parse a string until a delimiter. If my delimiter is /, then I want to match everything until that delimiter. For that, a parser like this works: named!(gobbledygook, take_until!("/")); I also…
janneb
  • 36,249
  • 2
  • 81
  • 97
1
vote
1 answer

Using nom to parse literal and return value

I'm new to Rust and have been trying to wrap my head around this for three hours and I think I'm going insane. All I want is a parser that takes the string "true" and returns an enum Value::Const(true). This is what I have so far: // parser.rs use…
Lanbo
  • 15,118
  • 16
  • 70
  • 147
1
vote
2 answers

Matching template filter expressions with nom

I'm working on a templating engine where some of the syntax could be like: {{ somevar|filter }} In place of somevar could be an arbitrary "expression", which is to say, either a variable name like somevar, or a nested filter expression (like {{…
djc
  • 11,603
  • 5
  • 41
  • 54
0
votes
1 answer

Function `impl Parser` trait no working with `alt` combinator in nom

I use the alt function of nom to build a parser for bot commands, namely parse_command in the example. # src/main.rs use nom::{ IResult, bytes::complete::{tag}, branch::alt, combinator::map, }; #[derive(Debug, PartialEq)] pub enum Command…
Pamplemousse
  • 154
  • 1
  • 13
0
votes
1 answer

How to count comments using nom parser?

I'm trying to wrap my head around the nom package. The simple problem I'm trying to solve is to write a parser that can count the comment lines in a file. I've two types of comments to parse for: Single line comments using // Multi-line comments…
jks612
  • 1,224
  • 1
  • 11
  • 20
0
votes
1 answer

Necessary trait bounds on generic function implementing nom parser

Goal: To have the map(..) extracted into a function, so that it can be used in an alt(..) statement. There are probably some mistakes here. The code: fn extract_unsigned_value<'a, I, K, F, P, O>(key: K, parser: P, c: F) -> impl FnMut(I) ->…
user3054986
  • 417
  • 7
  • 18
0
votes
1 answer

Is there a way to easily require and parse a specific length of input with nom?

Given a sequence of numbers, b"12345678...", is there an easy way to have nom parse just the first n digits of the input into a type, such as u16, then the subsequent m digits into another type, and so on. Presently, nom consumes the entire…
George
  • 2,451
  • 27
  • 37
0
votes
1 answer

rust nom separated list whitespace sometimes optional

I'm trying to write a parser using rust and nom which can parse separated lists of tokens with the separator usually surrounded by spaces, but not if the thing between the tokens is in parenthesis. For example, this is a valid expression: a and b as…
ojii
  • 4,729
  • 2
  • 23
  • 34
0
votes
1 answer

Writing tests for nom parsers with VerboseError and convert_error

I'm trying to write tests for nom parsers that provide better messages when they fail. I have a macro similar to assert_eq! called assert_parses_to! that I want to provide a user-friendly display string when the parser…
Jay
  • 2,861
  • 3
  • 29
  • 51
0
votes
1 answer

How to build a numbered list parser in nom?

I'd like to parse a numbered list using nom in Rust. For example, 1. Milk 2. Bread 3. Bacon. I could use separated_list1 with an appropriate separator parser and element parser. fn parser(input: &str) -> IResult<&str, Vec<&str>> { preceded( …
mdcq
  • 1,593
  • 13
  • 30
0
votes
1 answer

Rust Nom How to use ? in parsers

It's the first time I have used a parser combinator, so maybe I just have a misunderstanding about how I should use a parser combinator. I have the following code which works in…
VSDekar
  • 1,741
  • 2
  • 21
  • 36
0
votes
0 answers

any way to tell yarn to compile a package for darwin or linux?

I need something like yarn install foo -arch linux-arm I need this because I am installing dependencies locally but copying them to a ubuntu container which means that I need to install the Darwin-arm them copy it inside the linux-arm.
Cesarvspr
  • 301
  • 3
  • 9
0
votes
1 answer

Parse hex and decimal number

Just started to use the crate nom. Tried the simplest example: use nom::{ bytes::complete::tag, character::complete::{hex_digit1, space0}, combinator::{map, opt}, error::Error, sequence::{delimited, pair}, …
DraganS
  • 2,621
  • 1
  • 27
  • 40
0
votes
1 answer

Parse eof or a character in nom

I'm parsing with the nom library. I would like to match on something that is followed by an 'end', without consuming it. A end for me is either eof or a char that satisfies a function f: Fn(char) -> bool. I can use f with…
0
votes
1 answer

Why tuple needs type annotation in nom library?

I'm writing a parser using nom library in rust. When using tuple I encounter a problem. This snippet works fine: use std::str; use nom::bytes::complete::take_while1; use nom::bytes::complete::{tag, take_while}; use nom::character::complete::{char,…
Amir reza Riahi
  • 1,540
  • 2
  • 8
  • 34
1 2 3
8 9