Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
Alberto
@0X1A
No, methods can't be passed to alt as it expects functions with only one argument
Denis Lisov
@tanriol
You can pass closures like |input| my_struct.method_parser(input)
Alberto
@0X1A
Gracias :pray:
Geoff Shannon
@RadicalZephyr
hi all, I'm trying to write a combinator that ensures that the first character is alphabetic, but the remainder of the string is alphabetic. Is there a better way than parsing with alphanumeric1 and then checking the first character is alphabetic?
Michael P. Jung
@bikeshedder
@RadicalZephyr You mean something like that?
named!(parse_identifier<CompleteStr, String>, do_parse!(
    first: take_while1!(char::is_alphabetic) >>
    rest: take_while!(char::is_alphanumeric) >>
    (first.0.into() + rest.0)
));
Michael P. Jung
@bikeshedder
I just realized I'm still using nom 4 and CompleteStr is no longer a thing. whops
Gotta try migrating to nom 5. Looks like I'm going to like the changes. Less macro magic.
Denis Lisov
@tanriol
@RadicalZephyr Something like recognize(tuple((alpha1, alphanumeric0)))?
Geoff Shannon
@RadicalZephyr
@tanriol heh, I got too focused on ensuring that only the first character was alphanumeric, but that do exactly what I want! thanks!
Michael P. Jung
@bikeshedder
Is there anywhere a migration guide for nom 4->5. When starting with nom 4 I got confused by the CompleteStr and with nom 5 it seams everything has changed.
Denis Lisov
@tanriol
Something like this?
jtenner
@jtenner
Hey guys. How can I assert the end of input?
    input.len() == 0;
Just want to be idiomatic :)
jtenner
@jtenner
I think I have a fundamental misunderstanding here.
jtenner
@jtenner
I come from a peg background, and I know what to expect in terms of parsing logic.
I'm just having a hard time with the rust api and rust itself.
fn parse_expression(expression: &str) -> IResult<&str, DiceNode, &str> {
    let (input, taken) = space0(expression)?;
It's saying that space0() has a problem
jtenner
@jtenner
I just need to consume some whitespace lol
Geoff Shannon
@RadicalZephyr
@jtenner I think to assert end of input the ‘combinator::complete’ combinator is what you want in addition to whatever combinators actually consume the input
jtenner
@jtenner
Okay!
Can I personally message you? I think I need a human to explain this to me like I'm five.
Geoff Shannon
@RadicalZephyr
Sure
Denis Lisov
@tanriol
Probably not complete, but all_consuming
Alberto
@0X1A
@tanriol Hello again, seems alt kinda breaks when using it with a &mut self
The trait is only for Fn and not FnMut and I'm not sure the borrow checker will like passing around FnMut with mutable self with the way alt works
Bruno Bigras
@bbigras
Is it possible to use nom with a BufReader? or maybe on a file directly.
Alberto
@0X1A
@bbigras Sure, just pass the data to a parser function
Alberto
@0X1A
I know it might not be so clear from the documentation but all you have to do is crack open a file, read as str or bytes and pass that str/bytes to nom functions
Bruno Bigras
@bbigras
yeah I already do that. but I was wondering if I could use nom without having to read into a variable first.
especially since I want to read a log file and follow it like tail does.
Alberto
@0X1A
Ah, no. Nom doesn't have any kind of convenience functions like that
Bruno Bigras
@bbigras
I already do it with nom 4 in some project but I was wondering if nom supported the Read trait or that maybe BufReader was usable directly.
Bruno Bigras
@bbigras
In other words, I think I wanted nom to use a BufReader directly and consume it. Right now I use a buffer that I fill and empty if nom can parse a part of it.
David Holroyd
@dholroyd
hi! is there an existing nom5 combinator for character ranges?
would like to express, for example,
Char       ::=       #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]    /* any Unicode character, excluding the surrogate blocks, FFFE, and FFFF. */
...and variants on that
I think I could copy'n'paste my own parser based on an example from nom::character::complete::, but maybe I've missed an easier way! :)
jtenner
@jtenner
Seems like the docs could use some work
@RadicalZephyr showed me a way to do productions yesterday.
fn production_something<'a, E: ParseError<&'a str>>(input: &'a str) -> IResult<&'a str, ReturnType, E> {
  try_parse_something::<'a, E>(input)
    .or_else(|_| try_parse_something_else::<'a, E>(input))
    .or_else(|_| try_parse_this_thing::<'a, E>(input))
}

fn try_parse_something<'a, E: ParseError<&'a str>>(input: &'a str) -> IResult<&'a str, ReturnType, E> {
  Ok((input, value))
}
jtenner
@jtenner
@dholroyd hope that helps
David Holroyd
@dholroyd
not quite the problem I was chasing, but thank you!
I just wrote this,
pub fn char_in<I, R, Error: ParseError<I>>(range: R) -> impl Fn(I) -> IResult<I, char, Error>
    where
        I: Slice<RangeFrom<usize>> + InputIter,
        <I as InputIter>::Item: AsChar + Copy,
        R: RangeBounds<char>,
{
    move |i: I| match (i).iter_elements().next().map(|c| (c, range.contains(&c.as_char()))) {
        Some((c, true)) => Ok((i.slice(c.len()..), c.as_char())),
        _ => Err(nom::Err::Error(Error::from_error_kind(i, ErrorKind::OneOf))),
    }
}
type-checks, but not actually tested :)
...I was wondering if this is already built into nom, if you know where to look :)
jtenner
@jtenner
the or_else() chains were the production values.
I come from peg grammars so the idea of parsing a production vs parsing a production instance was new to me
David Holroyd
@dholroyd
So, I think the function I quoted above works (although I'd still be happy to hear if there's an inbuilt thing for this), however it raises another question for me,