1use crate::whitespace::Whitespace;
2
3#[derive(Clone, Debug, PartialEq, Eq)]
4pub enum Symbol {
6 Word(String),
9 Alpha(char),
11 Digit(usize),
13 Underscore,
15 Dash,
17 Assignment,
19 Plus,
21 LeftBrace,
23 RightBrace,
25 LeftBracket,
27 RightBracket,
29 LeftParenthesis,
31 RightParenthesis,
33 Colon,
35 Semicolon,
37 Join,
39 Directive,
41 Escape,
43 Slash,
45 Comma,
47 Decimal,
49 DoubleQuote,
51 SingleQuote,
53 LeftAngle,
55 RightAngle,
57
58 Unicode(String),
60
61 Newline,
63 Whitespace(Whitespace),
65 Comment(String),
73
74 Eoi,
76 Void,
78}
79
80impl Symbol {
81 pub fn from_word<S: Into<String>>(word: S) -> Self {
83 Self::Word(word.into())
84 }
85
86 #[must_use]
87 pub const fn is_whitespace(&self) -> bool {
89 matches!(&self, Self::Whitespace(_) | Self::Comment(_))
90 }
91
92 #[must_use]
93 pub const fn is_newline(&self) -> bool {
95 matches!(&self, Self::Newline)
96 }
97
98 #[must_use]
99 pub fn output(&self) -> String {
101 match *self {
102 Self::Join => String::new(),
103 _ => self.to_string(),
104 }
105 }
106}
107
108impl ToString for Symbol {
109 fn to_string(&self) -> String {
110 match self {
111 Self::Word(w) => w.clone(),
112 Self::Alpha(c) => c.to_string(),
113 Self::Digit(d) => d.to_string(),
114 Self::Underscore => "_".to_string(),
115 Self::Dash => "-".to_string(),
116 Self::Assignment => "=".to_string(),
117 Self::Plus => "+".to_string(),
118 Self::LeftBrace => "{".to_string(),
119 Self::RightBrace => "}".to_string(),
120 Self::LeftBracket => "[".to_string(),
121 Self::RightBracket => "]".to_string(),
122 Self::LeftParenthesis => "(".to_string(),
123 Self::RightParenthesis => ")".to_string(),
124 Self::Colon => ":".to_string(),
125 Self::Semicolon => ";".to_string(),
126 Self::Join => "##".to_string(),
127 Self::Directive => "#".to_string(),
128 Self::Escape => "\\".to_string(),
129 Self::Slash => "/".to_string(),
130 Self::Comma => ",".to_string(),
131 Self::Decimal => ".".to_string(),
132 Self::DoubleQuote => "\"".to_string(),
133 Self::SingleQuote => "'".to_string(),
134 Self::LeftAngle => "<".to_string(),
135 Self::RightAngle => ">".to_string(),
136 Self::Unicode(s) => s.to_string(),
137 Self::Newline => "\n".to_string(),
138 Self::Whitespace(w) => w.to_string(),
139 Self::Eoi | Self::Void | Self::Comment(_) => String::new(),
140 }
141 }
142}