Implement ident substraction

This commit is contained in:
ConnorSkees 2020-02-02 14:46:58 -05:00
parent ffff80109b
commit 89060a0e83
4 changed files with 188 additions and 1 deletions

View File

@ -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 {

View File

@ -413,6 +413,10 @@ impl<'a> Lexer<'a> {
return TokenKind::Keyword(kw);
}
if string == "-" {
return TokenKind::Symbol(Symbol::Minus);
}
TokenKind::Ident(string)
}
}

View File

@ -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),

View File

@ -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"
);