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:
@@ -665,9 +665,15 @@ impl<'a> Parser<'a> {
|
|||||||
if let Some(Token::Integer(text)) = self.token() {
|
if let Some(Token::Integer(text)) = self.token() {
|
||||||
self.consume();
|
self.consume();
|
||||||
// Lexer just gives us raw text that looks like an integer.
|
// 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.
|
// Parse it as a u8 to check for overflow and other issues.
|
||||||
text.parse()
|
text.parse()
|
||||||
.map_err(|_| self.error("expected u8 decimal immediate"))
|
.map_err(|_| self.error("expected u8 decimal immediate"))
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
err!(self.loc, err_msg)
|
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]
|
#[test]
|
||||||
fn uimm128() {
|
fn uimm128() {
|
||||||
macro_rules! parse_as_constant_data {
|
macro_rules! parse_as_constant_data {
|
||||||
|
|||||||
Reference in New Issue
Block a user