2
\$\begingroup\$

I'm attempting to make a parser using nom in Rust. Nom implements parser combinators, so more complex parsers are built from simpler parsers. Because of this, I've started with a simple ID token parser, but before I keep going, I'd like to know if there are any obvious (to anyone besides me) design problems with the code I have.

My current ID parser, shown below, scans the input for a sequence of alphanumeric characters that starts with a letter.

#[macro_use]
extern crate nom;

use nom::{IResult, alpha, alphanumeric, space};

named!(id_pre (&str) -> &str, call!(alpha));

named!(id (&str) -> String,
    do_parse!(
        prefix: map!(id_pre, str::to_owned) >>
        suffix: opt!(alphanumeric) >>
        (match suffix {
            Some(suffix) => prefix + suffix,
            _ => prefix,
        })
    )
);

named!(id_list (&str) -> Vec<String>, separated_list!(space, id));

fn main() {
    let a = id_list("Hello world2\n");
    print!("{:?}\n", a)
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.