From 16bde54aa53b83f45a5508b302a9c3b6aee53235 Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Sat, 23 Mar 2019 14:22:50 +0200 Subject: [PATCH] Implement display for Token and PartialToken Relates to #19 --- src/token/display.rs | 57 ++++++++++++++++++++++++++++++++++++++++++++ src/token/mod.rs | 2 ++ 2 files changed, 59 insertions(+) create mode 100644 src/token/display.rs diff --git a/src/token/display.rs b/src/token/display.rs new file mode 100644 index 0000000..345fe8e --- /dev/null +++ b/src/token/display.rs @@ -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, "|"), + } + } +} diff --git a/src/token/mod.rs b/src/token/mod.rs index 2dd0543..1d4db38 100644 --- a/src/token/mod.rs +++ b/src/token/mod.rs @@ -1,6 +1,8 @@ use error::Error; use value::{FloatType, IntType}; +mod display; + #[derive(Clone, PartialEq, Debug)] pub enum Token { // Arithmetic