From 4602a8f2f57b79555887c583c61b9ae4a9dbf75c Mon Sep 17 00:00:00 2001 From: Ivan Kalinin Date: Fri, 14 Feb 2025 17:21:06 +0100 Subject: [PATCH] Fix hex and binary parsing --- asm/src/ast.rs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/asm/src/ast.rs b/asm/src/ast.rs index 6e10d36..17bef2e 100644 --- a/asm/src/ast.rs +++ b/asm/src/ast.rs @@ -219,19 +219,32 @@ fn instr_ident<'a>() -> impl Parser<'a, &'a str, &'a str, extra::Err() -> impl Parser<'a, &'a str, BigInt, extra::Err> + Clone { - fn parse_int(s: &str, radix: u32, span: Span) -> Result { + fn parse_int(mut s: &str, radix: u32, span: Span) -> Result { + if !s.is_empty() { + s = s.trim_start_matches('0'); + if s.is_empty() { + s = "0"; + } + } + match BigInt::from_str_radix(s, radix) { Ok(n) => Ok(n), Err(e) => Err(ParserError::InvalidInt { span, inner: e }), } } + let num_slice = any() + .filter(|&c: &char| !c.is_whitespace() && c != ',' && c != '}' && c != '{') + .repeated() + .at_least(1) + .to_slice(); + let number = choice(( just("0x") - .ignore_then(text::int(16)) + .ignore_then(num_slice.clone()) .try_map(|s, span| parse_int(s, 16, span)), just("0b") - .ignore_then(text::int(2)) + .ignore_then(num_slice) .try_map(|s, span| parse_int(s, 2, span)), text::int(10).try_map(|s, span| parse_int(s, 10, span)), ));