use std::fmt;
use self::Token::*;
#[derive(PartialEq, Eq, Debug, Clone, Copy)]
pub enum Positional {
Zero,
One,
Two,
Three,
Four,
Five,
Six,
Seven,
Eight,
Nine,
}
impl Positional {
pub fn as_num(&self) -> u8 {
match *self {
Positional::Zero => 0,
Positional::One => 1,
Positional::Two => 2,
Positional::Three => 3,
Positional::Four => 4,
Positional::Five => 5,
Positional::Six => 6,
Positional::Seven => 7,
Positional::Eight => 8,
Positional::Nine => 9,
}
}
pub fn from_num(num: u8) -> Option<Self> {
match num {
0 => Some(Positional::Zero),
1 => Some(Positional::One),
2 => Some(Positional::Two),
3 => Some(Positional::Three),
4 => Some(Positional::Four),
5 => Some(Positional::Five),
6 => Some(Positional::Six),
7 => Some(Positional::Seven),
8 => Some(Positional::Eight),
9 => Some(Positional::Nine),
_ => None,
}
}
}
impl Into<u8> for Positional {
fn into(self) -> u8 {
self.as_num()
}
}
#[derive(PartialEq, Eq, Debug, Clone)]
pub enum Token {
Newline,
ParenOpen,
ParenClose,
CurlyOpen,
CurlyClose,
SquareOpen,
SquareClose,
Bang,
Tilde,
Pound,
Star,
Question,
Backslash,
Percent,
Dash,
Equals,
Plus,
Colon,
At,
Caret,
Slash,
Comma,
SingleQuote,
DoubleQuote,
Backtick,
Semi,
Amp,
Pipe,
AndIf,
OrIf,
DSemi,
Less,
Great,
DLess,
DGreat,
GreatAnd,
LessAnd,
DLessDash,
Clobber,
LessGreat,
Dollar,
ParamPositional(Positional),
Whitespace(String),
Literal(String),
Name(String),
}
impl fmt::Display for Token {
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(fmt, "{}", self.as_str())
}
}
impl Token {
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn len(&self) -> usize {
self.as_str().len()
}
pub fn is_word_delimiter(&self) -> bool {
match *self {
Newline |
ParenOpen |
ParenClose |
Semi |
Amp |
Less |
Great |
Pipe |
AndIf |
OrIf |
DSemi |
DLess |
DGreat |
GreatAnd |
LessAnd |
DLessDash |
Clobber |
LessGreat |
Whitespace(_) => true,
Bang |
Star |
Question |
Backslash |
SingleQuote |
DoubleQuote |
Backtick |
Percent |
Dash |
Equals |
Plus |
Colon |
At |
Caret |
Slash |
Comma |
CurlyOpen |
CurlyClose |
SquareOpen |
SquareClose |
Dollar |
Tilde |
Pound |
Name(_) |
Literal(_) |
ParamPositional(_) => false,
}
}
pub fn as_str(&self) -> &str {
match *self {
Newline => "\n",
ParenOpen => "(",
ParenClose => ")",
CurlyOpen => "{",
CurlyClose => "}",
SquareOpen => "[",
SquareClose => "]",
Dollar => "$",
Bang => "!",
Semi => ";",
Amp => "&",
Less => "<",
Great => ">",
Pipe => "|",
Tilde => "~",
Pound => "#",
Star => "*",
Question => "?",
Backslash => "\\",
Percent => "%",
Dash => "-",
Equals => "=",
Plus => "+",
Colon => ":",
At => "@",
Caret => "^",
Slash => "/",
Comma => ",",
SingleQuote => "\'",
DoubleQuote => "\"",
Backtick => "`",
AndIf => "&&",
OrIf => "||",
DSemi => ";;",
DLess => "<<",
DGreat => ">>",
GreatAnd => ">&",
LessAnd => "<&",
DLessDash => "<<-",
Clobber => ">|",
LessGreat => "<>",
ParamPositional(Positional::Zero) => "$0",
ParamPositional(Positional::One) => "$1",
ParamPositional(Positional::Two) => "$2",
ParamPositional(Positional::Three) => "$3",
ParamPositional(Positional::Four) => "$4",
ParamPositional(Positional::Five) => "$5",
ParamPositional(Positional::Six) => "$6",
ParamPositional(Positional::Seven) => "$7",
ParamPositional(Positional::Eight) => "$8",
ParamPositional(Positional::Nine) => "$9",
Whitespace(ref s) |
Name(ref s) |
Literal(ref s) => s,
}
}
}