Implement display for Token and PartialToken

Relates to #19
This commit is contained in:
Sebastian Schmidt 2019-03-23 14:22:50 +02:00
parent 5e491f6771
commit 16bde54aa5
2 changed files with 59 additions and 0 deletions

57
src/token/display.rs Normal file
View File

@ -0,0 +1,57 @@
use std::fmt;
use token::{PartialToken, Token};
impl fmt::Display for Token {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
use self::Token::*;
match self {
Plus => write!(f, "+"),
Minus => write!(f, "-"),
Star => write!(f, "*"),
Slash => write!(f, "/"),
Percent => write!(f, "%"),
Hat => write!(f, "^"),
// Logic
Eq => write!(f, "=="),
Neq => write!(f, "!="),
Gt => write!(f, ">"),
Lt => write!(f, "<"),
Geq => write!(f, ">="),
Leq => write!(f, "<="),
And => write!(f, "&&"),
Or => write!(f, "||"),
Not => write!(f, "!"),
// Precedence
LBrace => write!(f, "("),
RBrace => write!(f, ")"),
// Aggregation
Comma => write!(f, ","),
// Values => write!(f, ""), Variables and Functions
Identifier(identifier) => identifier.fmt(f),
Float(float) => float.fmt(f),
Int(int) => int.fmt(f),
Boolean(boolean) => boolean.fmt(f),
}
}
}
impl fmt::Display for PartialToken {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
use self::PartialToken::*;
match self {
Token(token) => token.fmt(f),
Literal(literal) => literal.fmt(f),
Whitespace => write!(f, " "),
Eq => write!(f, "="),
ExclamationMark => write!(f, "!"),
Gt => write!(f, ">"),
Lt => write!(f, "<"),
Ampersand => write!(f, "&"),
VerticalBar => write!(f, "|"),
}
}
}

View File

@ -1,6 +1,8 @@
use error::Error; use error::Error;
use value::{FloatType, IntType}; use value::{FloatType, IntType};
mod display;
#[derive(Clone, PartialEq, Debug)] #[derive(Clone, PartialEq, Debug)]
pub enum Token { pub enum Token {
// Arithmetic // Arithmetic