Add hexadecimal parsing for uimm8

This is to make it more clear in signed contexts that the uimm8 (e.g. 255) is in fact being used for its bits (e.g. 0xff).
This commit is contained in:
Andrew Brown
2019-10-24 17:05:56 -07:00
parent 3a94e8d46a
commit 6f35273055

View File

@@ -665,9 +665,15 @@ impl<'a> Parser<'a> {
if let Some(Token::Integer(text)) = self.token() {
self.consume();
// Lexer just gives us raw text that looks like an integer.
if text.starts_with("0x") {
// Parse it as a u8 in hexadecimal form.
u8::from_str_radix(&text[2..], 16)
.map_err(|_| self.error("unable to parse u8 as a hexadecimal immediate"))
} else {
// Parse it as a u8 to check for overflow and other issues.
text.parse()
.map_err(|_| self.error("expected u8 decimal immediate"))
}
} else {
err!(self.loc, err_msg)
}
@@ -3255,6 +3261,18 @@ mod tests {
);
}
#[test]
fn u8_as_hex() {
fn parse_as_uimm8(text: &str) -> ParseResult<u8> {
Parser::new(text).match_uimm8("unable to parse u8")
}
assert_eq!(parse_as_uimm8("0").unwrap(), 0);
assert_eq!(parse_as_uimm8("0xff").unwrap(), 255);
assert!(parse_as_uimm8("-1").is_err());
assert!(parse_as_uimm8("0xffa").is_err());
}
#[test]
fn uimm128() {
macro_rules! parse_as_constant_data {