mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
Add base to AST BigintLiteral (#1416)
This commit is contained in:
parent
b945307ab3
commit
cb804d3cd2
5 changed files with 33 additions and 8 deletions
|
|
@ -8,7 +8,7 @@ use std::{
|
|||
use bitflags::bitflags;
|
||||
use num_bigint::BigInt;
|
||||
use oxc_span::{Atom, Span};
|
||||
use oxc_syntax::NumberBase;
|
||||
use oxc_syntax::{BigintBase, NumberBase};
|
||||
#[cfg(feature = "serde")]
|
||||
use serde::Serialize;
|
||||
|
||||
|
|
@ -111,6 +111,8 @@ pub struct BigintLiteral {
|
|||
pub span: Span,
|
||||
#[cfg_attr(feature = "serde", serde(serialize_with = "crate::serialize::serialize_bigint"))]
|
||||
pub value: BigInt,
|
||||
#[cfg_attr(feature = "serde", serde(skip))]
|
||||
pub base: BigintBase,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Hash)]
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ use oxc_syntax::{
|
|||
operator::{
|
||||
AssignmentOperator, BinaryOperator, LogicalOperator, UnaryOperator, UpdateOperator,
|
||||
},
|
||||
NumberBase,
|
||||
BigintBase, NumberBase,
|
||||
};
|
||||
|
||||
#[allow(clippy::wildcard_imports)]
|
||||
|
|
@ -126,8 +126,8 @@ impl<'a> AstBuilder<'a> {
|
|||
BooleanLiteral { span, value }
|
||||
}
|
||||
|
||||
pub fn bigint_literal(&self, span: Span, value: BigInt) -> BigintLiteral {
|
||||
BigintLiteral { span, value }
|
||||
pub fn bigint_literal(&self, span: Span, value: BigInt, base: BigintBase) -> BigintLiteral {
|
||||
BigintLiteral { span, value, base }
|
||||
}
|
||||
|
||||
pub fn template_literal(
|
||||
|
|
|
|||
|
|
@ -678,7 +678,8 @@ impl<'a> Compressor<'a> {
|
|||
use std::ops::Neg;
|
||||
|
||||
let value = big_int_literal.value.clone().neg();
|
||||
let literal = self.ast.bigint_literal(unary_expr.span, value);
|
||||
let literal =
|
||||
self.ast.bigint_literal(unary_expr.span, value, big_int_literal.base);
|
||||
return Some(self.ast.literal_bigint_expression(literal));
|
||||
}
|
||||
Expression::Identifier(ident) => {
|
||||
|
|
@ -706,7 +707,8 @@ impl<'a> Compressor<'a> {
|
|||
}
|
||||
Expression::BigintLiteral(big_int_literal) => {
|
||||
let value = big_int_literal.value.clone().not();
|
||||
let leteral = self.ast.bigint_literal(unary_expr.span, value);
|
||||
let leteral =
|
||||
self.ast.bigint_literal(unary_expr.span, value, big_int_literal.base);
|
||||
return Some(self.ast.literal_bigint_expression(leteral));
|
||||
}
|
||||
Expression::Identifier(ident) => {
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ use oxc_allocator::Box;
|
|||
use oxc_ast::ast::*;
|
||||
use oxc_diagnostics::Result;
|
||||
use oxc_span::{Atom, Span};
|
||||
use oxc_syntax::{operator::BinaryOperator, precedence::Precedence, NumberBase};
|
||||
use oxc_syntax::{operator::BinaryOperator, precedence::Precedence, BigintBase, NumberBase};
|
||||
|
||||
use super::{
|
||||
function::IsParenthesizedArrowFunction,
|
||||
|
|
@ -294,12 +294,19 @@ impl<'a> Parser<'a> {
|
|||
|
||||
pub(crate) fn parse_literal_bigint(&mut self) -> Result<BigintLiteral> {
|
||||
let span = self.start_span();
|
||||
let base = match self.cur_kind() {
|
||||
Kind::Decimal => BigintBase::Decimal,
|
||||
Kind::Binary => BigintBase::Binary,
|
||||
Kind::Octal => BigintBase::Octal,
|
||||
Kind::Hex => BigintBase::Hex,
|
||||
_ => return Err(self.unexpected()),
|
||||
};
|
||||
let value = match self.cur_kind() {
|
||||
kind if kind.is_number() => self.cur_token().value.as_bigint(),
|
||||
_ => return Err(self.unexpected()),
|
||||
};
|
||||
self.bump_any();
|
||||
Ok(BigintLiteral { span: self.end_span(span), value })
|
||||
Ok(BigintLiteral { span: self.end_span(span), value, base })
|
||||
}
|
||||
|
||||
pub(crate) fn parse_literal_regexp(&mut self) -> Result<RegExpLiteral> {
|
||||
|
|
|
|||
|
|
@ -26,3 +26,17 @@ impl NumberBase {
|
|||
matches!(self, Self::Float | Self::Decimal)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
|
||||
pub enum BigintBase {
|
||||
Decimal,
|
||||
Binary,
|
||||
Octal,
|
||||
Hex,
|
||||
}
|
||||
|
||||
impl BigintBase {
|
||||
pub fn is_base_10(&self) -> bool {
|
||||
self == &Self::Decimal
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue