Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created June 10, 2020 12:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rust-play/0716ab5363d0459e6264f8ae2bfef449 to your computer and use it in GitHub Desktop.
Save rust-play/0716ab5363d0459e6264f8ae2bfef449 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
fn nom_str_until_eol(i: &str) -> nom::IResult<&str, &str> {
let first_line = i.lines().next();
if let Some(first_line) = first_line {
let rest = &i[first_line.len()..];
let rest = rest.trim();
Ok((rest, first_line))
} else {
Ok((i, ""))
}
}
fn nom_comment(i: &str) -> nom::IResult<&str, &str> {
let(i, _) = tag("%")(i)?;
let(i, comment) = nom_str_until_eol(i)?;
Ok((i, comment))
}
fn nom_other_thing(i: &str) -> nom::IResult<&str, &str> {
let(i, _) = tag("fn")(i)?;
let(i, function_line) = nom_str_until_eol(i)?;
Ok((i, function_line))
}
fn nom_either(i: &str) -> nom::IResult<&str, &str> {
alt(nom_comment, nom_other_thing)(i)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment