Last active
March 28, 2022 13:35
-
-
Save toksdotdev/a3ce31b51223129a716b25ebe771017d to your computer and use it in GitHub Desktop.
Simple macro to assert on nom parser and nom_locate result
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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