Skip to content

Commit

Permalink
Fix hex and binary parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexagon committed Feb 14, 2025
1 parent 4e4943a commit 4602a8f
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions asm/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,19 +219,32 @@ fn instr_ident<'a>() -> impl Parser<'a, &'a str, &'a str, extra::Err<ParserError
}

fn nat<'a>() -> impl Parser<'a, &'a str, BigInt, extra::Err<ParserError>> + Clone {
fn parse_int(s: &str, radix: u32, span: Span) -> Result<BigInt, ParserError> {
fn parse_int(mut s: &str, radix: u32, span: Span) -> Result<BigInt, ParserError> {
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())

Check failure on line 244 in asm/src/ast.rs

View workflow job for this annotation

GitHub Actions / Lints

using `clone` on type `ToSlice<Repeated<Filter<Any<&str, Full<ParserError, (), ()>>, {[email protected]:237:17}>, char, &str, Full<ParserError, (), ()>>, ()>` which implements the `Copy` trait
.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)),
));
Expand Down

0 comments on commit 4602a8f

Please sign in to comment.