Converts a `ruff` ast integer into a `BigInt`. Unlike small integers, big integers may be stored in one of four possible radix representations.
(int: &ast::Int)
| 10518 | /// Converts a `ruff` ast integer into a `BigInt`. |
| 10519 | /// Unlike small integers, big integers may be stored in one of four possible radix representations. |
| 10520 | fn parse_big_integer(int: &ast::Int) -> Result<BigInt, CodegenErrorType> { |
| 10521 | // TODO: Improve ruff API |
| 10522 | // Can we avoid this copy? |
| 10523 | let s = format!("{int}"); |
| 10524 | let mut s = s.as_str(); |
| 10525 | // See: https://peps.python.org/pep-0515/#literal-grammar |
| 10526 | let radix = match s.get(0..2) { |
| 10527 | Some("0b" | "0B") => { |
| 10528 | s = s.get(2..).unwrap_or(s); |
| 10529 | 2 |
| 10530 | } |
| 10531 | Some("0o" | "0O") => { |
| 10532 | s = s.get(2..).unwrap_or(s); |
| 10533 | 8 |
| 10534 | } |
| 10535 | Some("0x" | "0X") => { |
| 10536 | s = s.get(2..).unwrap_or(s); |
| 10537 | 16 |
| 10538 | } |
| 10539 | _ => 10, |
| 10540 | }; |
| 10541 | |
| 10542 | BigInt::from_str_radix(s, radix).map_err(|e| { |
| 10543 | CodegenErrorType::SyntaxError(format!( |
| 10544 | "unparsed integer literal (radix {radix}): {s} ({e})" |
| 10545 | )) |
| 10546 | }) |
| 10547 | } |
| 10548 | |
| 10549 | // Note: Not a good practice in general. Keep this trait private only for compiler |
| 10550 | trait ToU32 { |
no test coverage detected