1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#[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>()
}
}