use num_bigint::{BigInt, Sign};
use num_traits::Num;
use smol_str::SmolStr;
use unescaper::unescape;
use super::{TerminalFalse, TerminalLiteralNumber, TerminalShortString, TerminalTrue};
use crate::node::db::SyntaxGroup;
use crate::node::Terminal;
impl TerminalTrue {
#[inline(always)]
pub fn boolean_value(&self) -> bool {
true
}
}
impl TerminalFalse {
#[inline(always)]
pub fn boolean_value(&self) -> bool {
false
}
}
impl TerminalLiteralNumber {
pub fn numeric_value(&self, db: &dyn SyntaxGroup) -> Option<BigInt> {
self.numeric_value_and_suffix(db).map(|(value, _suffix)| value)
}
pub fn numeric_value_and_suffix(
&self,
db: &dyn SyntaxGroup,
) -> Option<(BigInt, Option<SmolStr>)> {
let text = self.text(db);
let (text, radix) = if let Some(num_no_prefix) = text.strip_prefix("0x") {
(num_no_prefix, 16)
} else if let Some(num_no_prefix) = text.strip_prefix("0o") {
(num_no_prefix, 8)
} else if let Some(num_no_prefix) = text.strip_prefix("0b") {
(num_no_prefix, 2)
} else {
(text.as_str(), 10)
};
if let Ok(value) = BigInt::from_str_radix(text, radix) {
Some((value, None))
} else {
let (text, suffix) = match text.rsplit_once('_') {
Some((text, suffix)) => {
let suffix = if suffix.is_empty() { None } else { Some(suffix) };
(text, suffix)
}
None => (text, None),
};
let value = BigInt::from_str_radix(text, radix).ok()?;
let suffix = suffix.map(SmolStr::new);
Some((value, suffix))
}
}
}
impl TerminalShortString {
pub fn string_value(&self, db: &dyn SyntaxGroup) -> Option<String> {
let text = self.text(db);
let mut text = text.as_str();
if text.starts_with('\'') {
(_, text) = text.split_once('\'').unwrap();
}
if let Some((body, _suffix)) = text.rsplit_once('\'') {
text = body;
}
let text = unescape(text).ok()?;
if !text.is_ascii() {
return None;
}
Some(text)
}
pub fn numeric_value(&self, db: &dyn SyntaxGroup) -> Option<BigInt> {
self.string_value(db).map(|string| BigInt::from_bytes_be(Sign::Plus, string.as_bytes()))
}
pub fn suffix(&self, db: &dyn SyntaxGroup) -> Option<SmolStr> {
let text = self.text(db);
let (_literal, mut suffix) = text[1..].rsplit_once('\'')?;
if suffix.is_empty() {
return None;
}
if suffix.starts_with('_') {
suffix = &suffix[1..];
}
Some(suffix.into())
}
}