use crate::{
parser::lexer::{IntegerWithVariant, Token},
values::core_values::{
decimal::{
Decimal,
typed_decimal::{DecimalTypeVariant, TypedDecimal},
},
error::NumberParseError,
integer::{
Integer,
typed_integer::{IntegerTypeVariant, TypedInteger},
},
},
};
use core::{
iter::Peekable,
str::{Chars, FromStr},
};
use crate::prelude::*;
pub enum IntegerOrDecimal {
Integer(Integer),
Decimal(Decimal),
TypedInteger(TypedInteger),
TypedDecimal(TypedDecimal),
}
pub enum IntegerOrTypedInteger {
Integer(Integer),
TypedInteger(TypedInteger),
}
pub fn parse_integer_literal(
lit: String,
) -> Result<IntegerOrDecimal, NumberParseError> {
let mut chars = lit.chars().peekable();
let integer_part = consume_digits_with_underscores(&mut chars);
let mut exponent_part = String::new();
if let Some(&c) = chars.peek()
&& (c == 'e' || c == 'E')
{
chars.next();
if let Some(&c2) = chars.peek() {
if c2 == '-' {
exponent_part.push(c2);
chars.next();
} else if c2 == '+' {
chars.next();
}
}
exponent_part += &consume_digits_with_underscores(&mut chars);
}
let variant_part: String = chars.collect();
if exponent_part.is_empty() {
if variant_part.is_empty() {
Integer::try_from_string(&integer_part)
.map(IntegerOrDecimal::Integer)
}
else {
if let Ok(integer_variant) =
IntegerTypeVariant::from_str(&variant_part)
{
TypedInteger::try_from_string_and_variant(
&integer_part,
integer_variant,
)
.map(IntegerOrDecimal::TypedInteger)
}
else if let Ok(decimal_variant) =
DecimalTypeVariant::from_str(&variant_part)
{
TypedDecimal::try_from_string_and_variant(
&integer_part,
decimal_variant,
)
.map(IntegerOrDecimal::TypedDecimal)
} else {
unreachable!()
}
}
}
else {
let full_number = format!("{}e{}", integer_part, exponent_part);
if variant_part.is_empty() {
Decimal::try_from_string(&full_number)
.map(IntegerOrDecimal::Decimal)
}
else if let Ok(decimal_variant) =
DecimalTypeVariant::from_str(&variant_part)
{
TypedDecimal::try_from_string_and_variant(
&full_number,
decimal_variant,
)
.map(IntegerOrDecimal::TypedDecimal)
}
else {
Err(NumberParseError::InvalidFormat)
}
}
}
fn consume_digits_with_underscores(chars: &mut Peekable<Chars>) -> String {
let mut part = String::new();
while let Some(&c) = chars.peek() {
if c.is_ascii_digit() {
part.push(c);
chars.next();
} else if c == '_' {
chars.next();
} else {
break;
}
}
part
}
pub fn parse_integer_with_variant(
integer_with_variant: IntegerWithVariant,
token: Token,
) -> Result<IntegerOrTypedInteger, NumberParseError> {
let radix = match token {
Token::BinaryIntegerLiteral(_) => 2,
Token::OctalIntegerLiteral(_) => 8,
Token::HexadecimalIntegerLiteral(_) => 16,
_ => unreachable!(),
};
match integer_with_variant.variant {
Some(var) => TypedInteger::try_from_string_radix_with_variant(
&integer_with_variant.value[2..],
radix,
var,
)
.map(IntegerOrTypedInteger::TypedInteger),
None => {
Integer::from_string_radix(&integer_with_variant.value[2..], radix)
.map(IntegerOrTypedInteger::Integer)
}
}
}
pub fn unescape_text(text: &str) -> String {
let escaped = text[1..text.len() - 1]
.replace(r#"\""#, "\"") .replace(r#"\'"#, "'") .replace(r#"\n"#, "\n") .replace(r#"\r"#, "\r") .replace(r#"\t"#, "\t") .replace(r#"\b"#, "\x08") .replace(r#"\f"#, "\x0C") .replace(r#"\\"#, "\\") .to_string();
decode_json_unicode_escapes(&escaped)
}
fn decode_json_unicode_escapes(input: &str) -> String {
let mut output = String::new();
let mut chars = input.chars().peekable();
while let Some(ch) = chars.next() {
if ch == '\\' && chars.peek() == Some(&'u') {
chars.next();
let mut code_unit = String::new();
for _ in 0..4 {
if let Some(c) = chars.next() {
code_unit.push(c);
} else {
output.push_str("\\u");
output.push_str(&code_unit);
break;
}
}
if let Ok(first_unit) = u16::from_str_radix(&code_unit, 16) {
if (0xD800..=0xDBFF).contains(&first_unit) {
if chars.next() == Some('\\') && chars.next() == Some('u') {
let mut low_code = String::new();
for _ in 0..4 {
if let Some(c) = chars.next() {
low_code.push(c);
} else {
output.push_str(&format!(
"\\u{first_unit:04X}\\u{low_code}"
));
break;
}
}
if let Ok(second_unit) =
u16::from_str_radix(&low_code, 16)
&& (0xDC00..=0xDFFF).contains(&second_unit)
{
let combined = 0x10000
+ (((first_unit - 0xD800) as u32) << 10)
+ ((second_unit - 0xDC00) as u32);
if let Some(c) = char::from_u32(combined) {
output.push(c);
continue;
}
}
output.push_str(&format!(
"\\u{first_unit:04X}\\u{low_code}"
));
} else {
output.push_str(&format!("\\u{first_unit:04X}"));
}
} else {
if let Some(c) = char::from_u32(first_unit as u32) {
output.push(c);
} else {
output.push_str(&format!("\\u{first_unit:04X}"));
}
}
} else {
output.push_str(&format!("\\u{code_unit}"));
}
} else {
output.push(ch);
}
}
output
}