From 89060a0e839c3936538f45c271f8a9ad920150fb Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Sun, 2 Feb 2020 14:46:58 -0500 Subject: [PATCH] Implement ident substraction --- src/common.rs | 11 +++++ src/lexer.rs | 4 ++ src/value.rs | 59 ++++++++++++++++++++++++- tests/values.rs | 115 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 188 insertions(+), 1 deletion(-) diff --git a/src/common.rs b/src/common.rs index 3ca09a5..0a087f5 100644 --- a/src/common.rs +++ b/src/common.rs @@ -371,6 +371,17 @@ pub(crate) enum QuoteKind { None, } +impl Display for QuoteKind { + #[inline] + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::Single => write!(f, "'"), + Self::Double => write!(f, "\""), + Self::None => write!(f, ""), + } + } +} + impl QuoteKind { pub fn as_str(self) -> &'static str { match self { diff --git a/src/lexer.rs b/src/lexer.rs index cb8e02d..cfaf068 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -413,6 +413,10 @@ impl<'a> Lexer<'a> { return TokenKind::Keyword(kw); } + if string == "-" { + return TokenKind::Symbol(Symbol::Minus); + } + TokenKind::Ident(string) } } diff --git a/src/value.rs b/src/value.rs index 0d433af..4c84988 100644 --- a/src/value.rs +++ b/src/value.rs @@ -1,7 +1,7 @@ #![allow(dead_code, unused_variables)] use std::fmt::{self, Display}; use std::iter::{Iterator, Peekable}; -use std::ops::Add; +use std::ops::{Add, Sub}; use crate::args::eat_call_args; use crate::builtin::GLOBAL_FUNCTIONS; @@ -127,6 +127,62 @@ impl Add for Value { } } +impl Sub for Value { + type Output = Self; + + fn sub(self, other: Self) -> Self { + match self { + // Self::Important => todo!(), + // Self::True => todo!(), + // Self::False => todo!(), + // Self::Null => todo!(), + Self::Dimension(num, unit) => match other { + // Self::Dimension(num2, unit2) => Value::Dimension(num - num2, unit), + _ => todo!(), + }, + // Self::List(..) => todo!(), + // Self::Color(..) => todo!(), + // Self::BinaryOp(..) => todo!(), + // Self::Paren(..) => todo!(), + Self::Ident(s1, quotes1) => match other { + Self::Ident(s2, quotes2) => { + let quotes1 = match quotes1 { + QuoteKind::Double | QuoteKind::Single => QuoteKind::Double, + QuoteKind::None => QuoteKind::None, + }; + let quotes2 = match quotes2 { + QuoteKind::Double | QuoteKind::Single => QuoteKind::Double, + QuoteKind::None => QuoteKind::None, + }; + Value::Ident( + format!("{}{}{}-{}{}{}", quotes1, s1, quotes1, quotes2, s2, quotes2), + QuoteKind::None, + ) + } + Self::Important | Self::True | Self::False | Self::Dimension(..) => { + let quotes = match quotes1 { + QuoteKind::Double | QuoteKind::Single => QuoteKind::Double, + QuoteKind::None => QuoteKind::None, + }; + Value::Ident( + format!("{}{}{}-{}", quotes, s1, quotes, other), + QuoteKind::None, + ) + } + Self::Null => { + let quotes = match quotes1 { + QuoteKind::Double | QuoteKind::Single => QuoteKind::Double, + QuoteKind::None => QuoteKind::None, + }; + Value::Ident(format!("{}{}{}-", quotes, s1, quotes), QuoteKind::None) + } + _ => todo!(), + }, + _ => todo!(), + } + } +} + impl Display for Value { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { @@ -143,6 +199,7 @@ impl Display for Value { Self::Color(c) => write!(f, "{}", c), Self::BinaryOp(lhs, op, rhs) => match op { Op::Plus => write!(f, "{}", *lhs.clone() + *rhs.clone()), + Op::Minus => write!(f, "{}", *lhs.clone() - *rhs.clone()), _ => write!(f, "{}{}{}", lhs, op, rhs), }, Self::Paren(val) => write!(f, "{}", val), diff --git a/tests/values.rs b/tests/values.rs index c60ea4d..80a5d2e 100644 --- a/tests/values.rs +++ b/tests/values.rs @@ -164,3 +164,118 @@ test!( "a {\n color: 'red' + 1px;\n}\n", "a {\n color: \"red1px\";\n}\n" ); +test!( + subs_idents, + "a {\n color: red - blue;\n}\n", + "a {\n color: red-blue;\n}\n" +); +test!( + subs_dbl_quoted_idents, + "a {\n color: \"red\" - \"blue\";\n}\n", + "a {\n color: \"red\"-\"blue\";\n}\n" +); +test!( + subs_sgl_quoted_idents, + "a {\n color: 'red' - 'blue';\n}\n", + "a {\n color: \"red\"-\"blue\";\n}\n" +); +test!( + subs_dbl_and_un_quoted_idents, + "a {\n color: \"red\" - blue;\n}\n", + "a {\n color: \"red\"-blue;\n}\n" +); +test!( + subs_sgl_and_un_quoted_idents, + "a {\n color: 'red' - blue;\n}\n", + "a {\n color: \"red\"-blue;\n}\n" +); +test!( + subs_un_and_dbl_quoted_idents, + "a {\n color: red - \"blue\";\n}\n", + "a {\n color: red-\"blue\";\n}\n" +); +test!( + subs_un_and_sgl_quoted_idents, + "a {\n color: red - 'blue';\n}\n", + "a {\n color: red-\"blue\";\n}\n" +); +test!( + subs_sgl_and_dbl_quoted_idents, + "a {\n color: 'red' - \"blue\";\n}\n", + "a {\n color: \"red\"-\"blue\";\n}\n" +); +test!( + subs_dbl_and_sgl_quoted_idents, + "a {\n color: \"red\" - 'blue';\n}\n", + "a {\n color: \"red\"-\"blue\";\n}\n" +); +test!( + subs_ident_true, + "a {\n color: red - true;\n}\n", + "a {\n color: red-true;\n}\n" +); +test!( + subs_dbl_quoted_ident_true, + "a {\n color: \"red\" - true;\n}\n", + "a {\n color: \"red\"-true;\n}\n" +); +test!( + subs_ident_false, + "a {\n color: red - false;\n}\n", + "a {\n color: red-false;\n}\n" +); +test!( + subs_dbl_quoted_ident_false, + "a {\n color: \"red\" - false;\n}\n", + "a {\n color: \"red\"-false;\n}\n" +); +test!( + subs_ident_important, + "a {\n color: red - !important;\n}\n", + "a {\n color: red-!important;\n}\n" +); +test!( + subs_ident_null, + "a {\n color: red - null;\n}\n", + "a {\n color: red-;\n}\n" +); +test!( + subs_dbl_quoted_ident_null, + "a {\n color: \"red\" - null;\n}\n", + "a {\n color: \"red\"-;\n}\n" +); +test!( + subs_sgl_quoted_ident_null, + "a {\n color: 'red' - null;\n}\n", + "a {\n color: \"red\"-;\n}\n" +); +test!( + subs_ident_number, + "a {\n color: red - 1;\n}\n", + "a {\n color: red-1;\n}\n" +); +test!( + subs_dbl_quoted_ident_number, + "a {\n color: \"red\" - 1;\n}\n", + "a {\n color: \"red\"-1;\n}\n" +); +test!( + subs_sgl_quoted_ident_number, + "a {\n color: 'red' - 1;\n}\n", + "a {\n color: \"red\"-1;\n}\n" +); +test!( + subs_ident_dimension, + "a {\n color: red - 1px;\n}\n", + "a {\n color: red-1px;\n}\n" +); +test!( + subs_dbl_quoted_ident_dimension, + "a {\n color: \"red\" - 1px;\n}\n", + "a {\n color: \"red\"-1px;\n}\n" +); +test!( + subs_sgl_quoted_ident_dimension, + "a {\n color: 'red' - 1px;\n}\n", + "a {\n color: \"red\"-1px;\n}\n" +);