diego.bernardes

diego.bernardes

I’m using NimbleParsec to parse a protocol and I would like to know if there is anyway to force a combinator to use labels.

For example, if I have this integer(min: 1) |> label("protocol"), the library will return an random error and not the protocol one in case I don’t send an integer. I would like to known if there is anyway to encapsulate a logic to guarantee that in case it fails, I can get a label to then pattern match later on the exact part it failed.

Thanks!

Showing Posts 1 to 3

kip

kip

ex_cldr Core Team

Using your example parser, I see:

iex> defmodule Parser do
...>  import NimbleParsec
...>
...>  defparsec :number,
...>    integer(min: 1) 
...>    |> label("protocol")
...> end
{:module, Parser, <<70, 79, 82, 49, 0, 0, 19, ...>>,.....

iex> Parser.number "3"
{:ok, [3], "", %{}, {1, 0}, 1

iex> Parser.number "a"
{:error, "expected protocol", "a", %{}, {1, 0}, 0}

Which seems pretty reasonable to me. I do find returning meaningful parsing errors a bit tricky at times - but typically not an issue in this simple kind of case.

What are you expecting, and what are you seeing?

diego.bernardes

diego.bernardes OP

Sorry about the noise, I found the problem. I’m building a long chain of combinators, and the error was coming from the next one in the chain. I posted just a small snippet to make it easier to frame the question, but it masked out the problem.

This is the combinator now:

  device_id =
    integer(min: 1) |> unwrap_and_tag(:device_id) |> lookahead(string(",")) |> label("device id")

And it matches this message:

"*HQ,1234567890,V1,...

The *HQ, is being match by a previous combinator, and then the next part is the device ID, which is now correctly being parsed.

NimbleParsec is really good, but it can get quite tricky sometimes.

kip

kip

ex_cldr Core Team

Glad you’re up and running. I typically try to make the grammar as explicit as possible and avoid lookahead unless really necessary. Perhaps thats just my preference. For example:

defmodule Parser do
  import NimbleParsec

  preamble =
    string("*HQ")
    |> label("preamble")
  
  device_id =
    integer(min: 1) 
    |> label("device id")

  version =
    string("V")
    |> integer(1)
    |> reduce({Enum, :join, []})
    |> label("version")

  separator =
    string(",")
    |> label("separator")
  
  defparsec :number,
    preamble
    |> ignore(separator)
    |> concat(device_id)
    |> ignore(separator)
    |> concat(version)
  
end

Examples

iex> Parser.number "*HQ,1234567890,V1"
{:ok, ["*HQ", 1234567890, "V1"], "", %{}, {1, 0}, 17}

iex> Parser.number "*HQ,A1234567890,V1"
{:error, "expected device id", "A1234567890,V1", %{}, {1, 0}, 4}

Which still gives an appropriate error message and makes it easy to expand the grammar imo.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews