#Nom concatenate parsers

3 messages · Page 1 of 1 (latest)

stuck sparrow
#

Let me start with the problem I'm trying to solve so you can tell me if I'm going about this all wrong.
I'm writing a Gerber parser.
Here's the PEG file: https://www.ucamco.com/files/downloads/file_en/457/gerber-layer-format-peg-parsing-expression-grammar_en.ebnf?0dcf4254cd886c28568dd609fda74dcb

Near the bottom you'll see a name = /[._a-zA-Z$][._a-zA-Z0-9]*/;. That's the parser I'm trying to write right now.

Essentially, I need two parsers here.
The first takes the first letter of the name, which can be .$ or an alphabetical character.
Then the second takes the rest of the name, which can be .
or any alphanumerical character.

Writing those two individual parsers is easy, but I'd like their outputs to be concatenated so I only have a single span coming out of the final name parser.
How can I concatenate those spans?

(I don't think it's important here, but I will note that I'm using nom_locate so I don't have string slices coming out of the parsers)

stuck sparrow
#

Here, I wrote out 90% of the function.

fn parse_name(input: Span) -> IResult<Span, Span> {
    // name      = /[._a-zA-Z$][._a-zA-Z0-9]*/;

    let first_char = map_parser(
        take(1usize),
        take_while1(|c| matches!(c, '.' | '_' | '$' | 'a'..='z' | 'A'..='Z')),
    );

    let rest_of_name = take_while(|c| matches!(c, '.' | '_' | 'a'..='z' | 'A'..='Z' | '0'..='9'));

    let (input, (first_char, rest_of_name)) = tuple((first_char, rest_of_name))(input)?;

    todo!()
}
loud dragon
#

if you don't get an answer here, you might consider asking in #lang-dev 🙂