1use std::fmt;
4use self::Token::*;
5
6#[derive(PartialEq, Eq, Debug, Clone, Copy)]
8pub enum Positional {
9 Zero,
11 One,
13 Two,
15 Three,
17 Four,
19 Five,
21 Six,
23 Seven,
25 Eight,
27 Nine,
29}
30
31impl Positional {
32 pub fn as_num(&self) -> u8 {
34 match *self {
35 Positional::Zero => 0,
36 Positional::One => 1,
37 Positional::Two => 2,
38 Positional::Three => 3,
39 Positional::Four => 4,
40 Positional::Five => 5,
41 Positional::Six => 6,
42 Positional::Seven => 7,
43 Positional::Eight => 8,
44 Positional::Nine => 9,
45 }
46 }
47
48 pub fn from_num(num: u8) -> Option<Self> {
50 match num {
51 0 => Some(Positional::Zero),
52 1 => Some(Positional::One),
53 2 => Some(Positional::Two),
54 3 => Some(Positional::Three),
55 4 => Some(Positional::Four),
56 5 => Some(Positional::Five),
57 6 => Some(Positional::Six),
58 7 => Some(Positional::Seven),
59 8 => Some(Positional::Eight),
60 9 => Some(Positional::Nine),
61 _ => None,
62 }
63 }
64}
65
66impl Into<u8> for Positional {
67 fn into(self) -> u8 {
68 self.as_num()
69 }
70}
71
72#[derive(PartialEq, Eq, Debug, Clone)]
74pub enum Token {
75 Newline,
77
78 ParenOpen,
80 ParenClose,
82 CurlyOpen,
84 CurlyClose,
86 SquareOpen,
88 SquareClose,
90
91 Bang,
93 Tilde,
95 Pound,
97 Star,
99 Question,
101 Backslash,
103 Percent,
105 Dash,
107 Equals,
109 Plus,
111 Colon,
113 At,
115 Caret,
117 Slash,
119 Comma,
121
122 SingleQuote,
124 DoubleQuote,
126 Backtick,
128
129 Semi,
131 Amp,
133 Pipe,
135 AndIf,
137 OrIf,
139 DSemi,
141
142 Less,
144 Great,
146 DLess,
148 DGreat,
150 GreatAnd,
152 LessAnd,
154 DLessDash,
156 Clobber,
158 LessGreat,
160
161 Dollar,
163 ParamPositional(Positional),
168
169 Whitespace(String),
171
172 Literal(String),
174 Name(String),
177}
178
179impl fmt::Display for Token {
180 fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
181 write!(fmt, "{}", self.as_str())
182 }
183}
184
185impl Token {
186 pub fn is_empty(&self) -> bool {
188 self.len() == 0
189 }
190
191 pub fn len(&self) -> usize {
193 self.as_str().len()
194 }
195
196 pub fn is_word_delimiter(&self) -> bool {
199 match *self {
200 Newline |
201 ParenOpen |
202 ParenClose |
203 Semi |
204 Amp |
205 Less |
206 Great |
207 Pipe |
208 AndIf |
209 OrIf |
210 DSemi |
211 DLess |
212 DGreat |
213 GreatAnd |
214 LessAnd |
215 DLessDash |
216 Clobber |
217 LessGreat |
218 Whitespace(_) => true,
219
220 Bang |
221 Star |
222 Question |
223 Backslash |
224 SingleQuote |
225 DoubleQuote |
226 Backtick |
227 Percent |
228 Dash |
229 Equals |
230 Plus |
231 Colon |
232 At |
233 Caret |
234 Slash |
235 Comma |
236 CurlyOpen |
237 CurlyClose |
238 SquareOpen |
239 SquareClose |
240 Dollar |
241 Tilde |
242 Pound |
243 Name(_) |
244 Literal(_) |
245 ParamPositional(_) => false,
246 }
247 }
248
249 pub fn as_str(&self) -> &str {
251 match *self {
252 Newline => "\n",
253 ParenOpen => "(",
254 ParenClose => ")",
255 CurlyOpen => "{",
256 CurlyClose => "}",
257 SquareOpen => "[",
258 SquareClose => "]",
259 Dollar => "$",
260 Bang => "!",
261 Semi => ";",
262 Amp => "&",
263 Less => "<",
264 Great => ">",
265 Pipe => "|",
266 Tilde => "~",
267 Pound => "#",
268 Star => "*",
269 Question => "?",
270 Backslash => "\\",
271 Percent => "%",
272 Dash => "-",
273 Equals => "=",
274 Plus => "+",
275 Colon => ":",
276 At => "@",
277 Caret => "^",
278 Slash => "/",
279 Comma => ",",
280 SingleQuote => "\'",
281 DoubleQuote => "\"",
282 Backtick => "`",
283 AndIf => "&&",
284 OrIf => "||",
285 DSemi => ";;",
286 DLess => "<<",
287 DGreat => ">>",
288 GreatAnd => ">&",
289 LessAnd => "<&",
290 DLessDash => "<<-",
291 Clobber => ">|",
292 LessGreat => "<>",
293
294 ParamPositional(Positional::Zero) => "$0",
295 ParamPositional(Positional::One) => "$1",
296 ParamPositional(Positional::Two) => "$2",
297 ParamPositional(Positional::Three) => "$3",
298 ParamPositional(Positional::Four) => "$4",
299 ParamPositional(Positional::Five) => "$5",
300 ParamPositional(Positional::Six) => "$6",
301 ParamPositional(Positional::Seven) => "$7",
302 ParamPositional(Positional::Eight) => "$8",
303 ParamPositional(Positional::Nine) => "$9",
304
305 Whitespace(ref s) |
306 Name(ref s) |
307 Literal(ref s) => s,
308 }
309 }
310}