Add base to AST BigintLiteral (#1416)

This commit is contained in:
Jon Surrell 2023-11-19 04:11:19 +01:00 committed by GitHub
parent b945307ab3
commit cb804d3cd2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 33 additions and 8 deletions

View file

@ -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)]

View file

@ -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(

View file

@ -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) => {

View file

@ -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> {

View file

@ -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
}
}