by

Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
James Sewell
@jamessewell
Also are there any good examples of using nom with a TCP stream?
Found the IRC example that answers my first question
:)
James Sewell
@jamessewell
Actually are there any examples of doing binary using nom 5.0?
Denis Lisov
@tanriol
@jamessewell In nom 5 macros are (mostly) considered deprecated, so functions are the way forward.
James Sewell
@jamessewell
Ok so it’s mostly (input, output) = function(input) kinda stuff?
dangrabcad
@dangrabcad
There's some of that but IME most of the code is chains of combinators
alt((map(foo, f), map(bar, b), map(baz, |x| x.getFoo()))) kind of thing
James Sewell
@jamessewell
I don’t suppose there is any Tokio code with nom 5 out there anyone knows of?
thealchemist17
@thealchemist17
i need to parse "x: 2.0" x as str and 2.0 as f32, can u help me? thanks
dangrabcad
@dangrabcad
@thealchemist17 Sure! How far have you got and where are you stuck?
thealchemist17
@thealchemist17
This message was deleted
thealchemist17
@thealchemist17
fn declare_parser(input: &str) -> IResult<&str, f32> {
    preceded(tag("x: "), float)(input)
i can parse "x: 2.0" but i can't parse all expressions like "var: f32"
@dangrabcad
dangrabcad
@dangrabcad
I'd suggest something like map(tuple((var_name, char(':'), space0, float)), |name, _, _, value| (name, value)), where var_name is a parser that can match "x" or whatever are valid things to go on the left
tuple applies a bunch of parsers in sequence and gives you all the results in a tuple
the map call just takes the tuple and throws away the ones you don't care about (the colon and space), giving you a pair of type (&str, f32)
that's assuming you can have any amount of whitespace after the colon but none before it, and assuming you want to keep the "x" or whatever was on the left
depending on your format, you could make var_name be alpha1 or a regex or take_until(char(':'))
thealchemist17
@thealchemist17
thanks @dangrabcad
Wisnu
@cakraww_twitter

Hi all, I want to parse this string and take the content between dash separators:

a
---
b
---
c

to become: ["a", "b", "c"].
how to take until some tag OR end of string? take_until("---") will blow up after parsing c because it hit end of string and didn't find the dashes.

peckpeck
@peckpeck
@dangrabcad i don't think there is an easy way to have warnings instead of errors results in parsers because in case of parsing error you don't know what is ok and what is not
what i did is be liberal during parsing and then call a check function that returns a warning vector for the parsed structue
dangrabcad
@dangrabcad
@cakraww_twitter Does separated_list do what you want?
Wisnu
@cakraww_twitter

Sorry, actually what I want to do is to parse this:

-------
heading
-------
content1

---------------
another heading
---------------
content2
----
this is still content.
because it spans on more than one line.
----

---------------
this is heading
---------------
content3

and the expected result is:

[ Block { title: "heading", content: "content1" }
, Block { title: "another heading", content: "content2\n----\nthis is still content.\nbecause it spans on more than one line.\n----" }
, Block { title: "this is heading", content: "content3" }
]

What I have tried is with: many1( tuple((parse_heading, parse_content )) ). Ideally the parse_content is something like "take as long as parse_heading doesn't match; OR end of string is reached" but I don't how to do it in nom. There is combinator::not but it returns unit (). Any idea?

dangrabcad
@dangrabcad
How do you tell the difference between the dashes within a block and the dashes delimiting the block and heading?
Wisnu
@cakraww_twitter
heading is always one line that's sandwiched between two dash lines. if it has more than one line inside the sandwich, it's not a heading. in above example, content2 is not a heading because the dash line before it has been consumed by another heading.
dangrabcad
@dangrabcad
but the four dashes under "because it spans..." could be the start of the next heading
Wisnu
@cakraww_twitter
yes, if there is non-empty string below the four dashes, it will become the heading instead of this is heading.
anyway I found the solution using regex 😅
something like below but with some modification to capture the heading as well:
let re = Regex::new(r"-+\n([^\n]+)\n-+").unwrap();
let contents: Vec<_> = re.split(txt).map(|s| s.trim()).collect();
Håkon Jordet
@Bunogi
Hi, how would I allow for whitespace using with nom 5.0.0? I see that in the past you could use the ws! macro but it it's deprecated
s/with//
Denis Lisov
@tanriol
By manually inserting the whitespace parser everywhere it's needed :-(
Håkon Jordet
@Bunogi
welp, I'll just suck it up then
or well keep doing it
peckpeck
@peckpeck
@Bunogi look at nomplus https://github.com/peckpeck/nomplus that i wrote there is a sp! and a wsequence! macro that may help you
Håkon Jordet
@Bunogi
oh right I could have written a macro to do this easier but now it's too late I guess. Would also rather not use a crate that's not on crates.io if I can help it
whentze
@whentze
hey, small question
i'm trying to build a parser for a text-based format. one of the basic building blocks of this format is an "operator", which is one of ~50 possible atoms
what's the best way to parse these into a big enum?
the s_expression example uses one_of, but that only works for single chars. what if my operators are things like "add" or "sub"?
the operator names are not all the same length, but none of them are prefixes of other operator names
Jeremy Day
@z2oh
I tend to use alts for this. The unfortunate thing is that they are implemented up to a fixed number of branches, and I believe this number is less than 50. You can always have nested alts though, or try to group your operators into some kind of semantic meaning to make the code less ugly.
whentze
@whentze
but what to use inside the alt?
tag + map?
Jeremy Day
@z2oh
Yes
whentze
@whentze
i see
Jeremy Day
@z2oh
Or, tag with value to avoid having to execute a closure
whentze
@whentze
is the alt! macro also limited in the same way?
oh, tag can take a value directly?
Jeremy Day
@z2oh
I'm referring to the value combinator: https://docs.rs/nom/5.0.0/nom/combinator/fn.value.html
which throws away the parsed result and replaces it with some value (an instance of an Operator enum variant, for example)