Skip to content

Instantly share code, notes, and snippets.

@toksdotdev
Last active March 28, 2022 13:35
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 toksdotdev/a3ce31b51223129a716b25ebe771017d to your computer and use it in GitHub Desktop.
Save toksdotdev/a3ce31b51223129a716b25ebe771017d to your computer and use it in GitHub Desktop.
Simple macro to assert on nom parser and nom_locate result
/// Perform assertion on a location span type.
macro_rules! assert_span {
($actual: expr, line = $value: expr) => {
assert_eq!($actual.location_line(), $value);
};
($actual: expr, fragment = $value: expr) => {
assert_eq!(*$actual.fragment(), $value);
};
($actual: expr, column = $value: expr) => {
assert_eq!($actual.get_column(), $value);
};
}
/// Perform assertion on a parser result.
///
/// ```
/// assert_lexed!(
/// lex_plus("+ remaining fragment".into()),
/// Token::Plus,
/// fragment = " remaining fragment",
/// line = 0,
/// column = 2
/// );
/// ```
macro_rules! assert_lexed {
($actual: expr, $expected_token: expr $(, $key: ident = $value: expr)*) => {
assert_eq!($actual.unwrap().1, $expected_token);
$(assert_span!($actual.unwrap().0, $key = $value);)*
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment