#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(u8)]
pub enum GroupKind {
Paren,
Brace,
Bracket,
}
#[derive(Clone, Debug)]
pub enum Atom<'a> {
Integral(ANum<'a>),
Decimal(ADecimal<'a>),
Bytes(ABytes<'a>),
String(AStr<'a>),
Ident(&'a str),
}
impl<'a> Atom<'a> {
pub fn number(&self) -> Option<&ANum<'a>> {
match self {
Atom::Integral(num) => Some(num),
_ => None,
}
}
pub fn decimal(&self) -> Option<&ADecimal<'a>> {
match self {
Atom::Decimal(dec) => Some(dec),
_ => None,
}
}
pub fn bytes(&self) -> Option<&ABytes<'a>> {
match self {
Atom::Bytes(bytes) => Some(bytes),
_ => None,
}
}
pub fn string(&self) -> Option<&AStr<'a>> {
match self {
Atom::String(str) => Some(str),
_ => None,
}
}
pub fn ident(&self) -> Option<&'a str> {
match self {
Atom::Ident(ident) => Some(ident),
_ => None,
}
}
}
#[derive(Clone, Debug)]
pub struct AStr<'a> {
pub has_escape: bool,
pub raw_data: &'a str,
}
impl<'a> AStr<'a> {
pub fn to_string(&self) -> String {
self.raw_data.to_string()
}
}
#[derive(Clone, Debug)]
pub struct ABytes<'a>(pub &'a str);
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum ANumBase {
Binary = 2,
Decimal = 10,
Hexadecimal = 16,
}
impl ANumBase {
pub fn to_radix(self) -> u32 {
self as u32
}
pub fn from_radix(v: u32) -> Option<Self> {
if v == 2 {
Some(Self::Binary)
} else if v == 10 {
Some(Self::Decimal)
} else if v == 16 {
Some(Self::Hexadecimal)
} else {
None
}
}
}
#[derive(Clone, Debug)]
pub struct ANum<'a> {
pub base: ANumBase,
pub dat: &'a str,
}
impl<'a> ANum<'a> {
pub fn base(&self) -> ANumBase {
self.base
}
pub fn radix(&self) -> u32 {
self.base.to_radix()
}
pub fn raw_data(&self) -> &'a str {
self.dat
}
pub fn digits(&self) -> String {
self.dat.chars().filter(|c| *c != '_').collect::<String>()
}
pub fn to_u8(&self) -> Result<u8, core::num::ParseIntError> {
u8::from_str_radix(&self.digits(), self.base.to_radix())
}
pub fn to_u16(&self) -> Result<u16, core::num::ParseIntError> {
u16::from_str_radix(&self.digits(), self.base.to_radix())
}
pub fn to_u32(&self) -> Result<u32, core::num::ParseIntError> {
u32::from_str_radix(&self.digits(), self.base.to_radix())
}
pub fn to_u64(&self) -> Result<u64, core::num::ParseIntError> {
u64::from_str_radix(&self.digits(), self.base.to_radix())
}
pub fn to_u128(&self) -> Result<u128, core::num::ParseIntError> {
u128::from_str_radix(&self.digits(), self.base.to_radix())
}
}
#[derive(Clone, Debug)]
pub struct ADecimal<'a> {
pub raw_integral: &'a str,
pub raw_fractional: &'a str,
}
impl<'a> ADecimal<'a> {
pub fn integral(&self) -> String {
self.raw_integral
.chars()
.filter(|c| *c != '_')
.collect::<String>()
}
pub fn fractional(&self) -> String {
self.raw_fractional
.chars()
.filter(|c| *c != '_')
.collect::<String>()
}
}