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
use crate::arena::{Arena, ArenaId};
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum TokenType {
Variable,
Constant,
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Token {
name: String,
token_type: TokenType,
}
impl Token {
pub fn new<T: Into<String>>(name: T, token_type: TokenType) -> Self {
Self {
name: name.into(),
token_type,
}
}
pub fn is_variable(&self) -> bool {
match &self.token_type {
TokenType::Variable => true,
_ => false,
}
}
pub fn is_constant(&self) -> bool {
match &self.token_type {
TokenType::Constant => true,
_ => false,
}
}
pub fn name(&self) -> &String {
&self.name
}
}
impl std::fmt::Display for Token {
fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
write!(f, "{}", self.name())
}
}
impl Arena<Token> {
pub fn render(&self, tokens: &[ArenaId]) -> String {
assert!(self.is_valid_slice(tokens));
let mut st = String::new();
for token in tokens {
st.push_str(&format!("{}", self.get(*token).unwrap()));
}
st
}
}