implement some of the more esoteric binary ops

addition, subtraction, and multiplication of functions and maps, along
with correct error messages and tests
This commit is contained in:
ConnorSkees 2020-05-22 23:30:38 -04:00
parent 33c5acc35f
commit 2a79fa2a40
5 changed files with 108 additions and 38 deletions

View File

@ -186,9 +186,9 @@ impl Value {
return Err((
format!(
"Undefined operation \"{} {} {}\".",
v.to_css_string(span)?,
v.inspect(span)?,
op,
other.to_css_string(span)?
other.inspect(span)?
),
span,
)
@ -217,9 +217,9 @@ impl Value {
return Err((
format!(
"Undefined operation \"{} {} {}\".",
self.to_css_string(span)?,
self.inspect(span)?,
op,
other.to_css_string(span)?
other.inspect(span)?
),
span,
)
@ -256,14 +256,14 @@ impl Value {
}
let precedence = Op::Plus.precedence();
Ok(match self {
Self::Map(..) => {
Self::Map(..) | Self::Function(..) => {
return Err((
format!("{} isn't a valid CSS value.", self.inspect(span)?),
span,
)
.into())
}
Self::Function(..) | Self::ArgList(..) => todo!(),
Self::ArgList(..) => todo!(),
Self::Important | Self::True | Self::False => match other {
Self::String(s, QuoteKind::Quoted) => Value::String(
format!("{}{}", self.to_css_string(span)?, s),
@ -316,13 +316,20 @@ impl Value {
format!("{}{}{}", num, unit, other.to_css_string(span)?),
QuoteKind::None,
),
Self::Map(..) | Self::Function(..) => {
return Err((
format!("{} isn't a valid CSS value.", other.inspect(span)?),
span,
)
.into())
}
_ => {
return Err((
format!(
"Undefined operation \"{}{} + {}\".",
num,
unit,
other.to_css_string(span)?
other.inspect(span)?
),
span,
)
@ -338,11 +345,7 @@ impl Value {
),
_ => {
return Err((
format!(
"Undefined operation \"{} + {}\".",
c,
other.to_css_string(span)?
),
format!("Undefined operation \"{} + {}\".", c, other.inspect(span)?),
span,
)
.into())
@ -396,7 +399,9 @@ impl Value {
}
let precedence = Op::Mul.precedence();
Ok(match self {
Self::Null => todo!(),
Self::Null => {
Value::String(format!("-{}", other.to_css_string(span)?), QuoteKind::None)
}
Self::Dimension(num, unit) => match other {
Self::Dimension(num2, unit2) => {
if !unit.comparable(&unit2) {
@ -428,6 +433,13 @@ impl Value {
format!("{}{}-{}", num, unit, other.to_css_string(span)?),
QuoteKind::None,
),
Self::Map(..) | Self::Function(..) => {
return Err((
format!("{} isn't a valid CSS value.", other.inspect(span)?),
span,
)
.into())
}
_ => todo!(),
},
Self::Color(c) => match other {
@ -437,11 +449,7 @@ impl Value {
Self::Null => Value::String(format!("{}-", c), QuoteKind::None),
Self::Dimension(..) | Self::Color(..) => {
return Err((
format!(
"Undefined operation \"{} - {}\".",
c,
other.to_css_string(span)?
),
format!("Undefined operation \"{} - {}\".", c, other.inspect(span)?),
span,
)
.into())
@ -544,7 +552,7 @@ impl Value {
"Undefined operation \"{}{} * {}\".",
num,
unit,
other.to_css_string(span)?
other.inspect(span)?
),
span,
)
@ -576,8 +584,8 @@ impl Value {
return Err((
format!(
"Undefined operation \"{} * {}\".",
self.to_css_string(span)?,
other.to_css_string(span)?
self.inspect(span)?,
other.inspect(span)?
),
span,
)
@ -628,11 +636,7 @@ impl Value {
Self::Null => Value::String(format!("{}/", c), QuoteKind::None),
Self::Dimension(..) | Self::Color(..) => {
return Err((
format!(
"Undefined operation \"{} / {}\".",
c,
other.to_css_string(span)?
),
format!("Undefined operation \"{} / {}\".", c, other.inspect(span)?),
span,
)
.into())
@ -720,8 +724,8 @@ impl Value {
return Err((
format!(
"Undefined operation \"{} % {}\".",
Value::Dimension(n, u).to_css_string(span)?,
other.to_css_string(span)?
Value::Dimension(n, u).inspect(span)?,
other.inspect(span)?
),
span,
)
@ -732,8 +736,8 @@ impl Value {
return Err((
format!(
"Undefined operation \"{} % {}\".",
self.to_css_string(span)?,
other.to_css_string(span)?
self.inspect(span)?,
other.inspect(span)?
),
span,
)

View File

@ -278,3 +278,21 @@ test!(
"a {\n color: 1+/2;\n}\n",
"a {\n color: 1/2;\n}\n"
);
error!(
map_lhs_add,
"a {color: (a: b) + 1;}", "Error: (a: b) isn't a valid CSS value."
);
error!(
map_rhs_add,
"a {color: 1 + (a: b);}", "Error: (a: b) isn't a valid CSS value."
);
error!(
function_lhs_add,
"a {color: get-function(lighten) + 1;}",
"Error: get-function(\"lighten\") isn't a valid CSS value."
);
error!(
function_rhs_add,
"a {color: 1 + get-function(lighten);}",
"Error: get-function(\"lighten\") isn't a valid CSS value."
);

View File

@ -167,11 +167,3 @@ error!(
operator_mul,
"a {color: 5 - *;}", "Error: Expected expression."
);
error!(
map_lhs_add,
"a {color: (a: b) + 1;}", "Error: (a: b) isn't a valid CSS value."
);
error!(
map_rhs_add,
"a {color: 1 + (a: b);}", "Error: (a: b) isn't a valid CSS value."
);

23
tests/multiplication.rs Normal file
View File

@ -0,0 +1,23 @@
#![cfg(test)]
#[macro_use]
mod macros;
error!(
map_lhs_mul,
"a {color: (a: b) * 1;}", "Error: Undefined operation \"(a: b) * 1\"."
);
error!(
map_rhs_mul,
"a {color: 1 * (a: b);}", "Error: Undefined operation \"1 * (a: b)\"."
);
error!(
function_lhs_mul,
"a {color: get-function(lighten) * 1;}",
"Error: Undefined operation \"get-function(\"lighten\") * 1\"."
);
error!(
function_rhs_mul,
"a {color: 1 * get-function(lighten);}",
"Error: Undefined operation \"1 * get-function(\"lighten\")\"."
);

View File

@ -214,3 +214,36 @@ test!(
"a {\n color: 1em- 0.0em;\n}\n",
"a {\n color: 1em- 0em;\n}\n"
);
test!(
null_minus_number,
"a {\n color: null - 1;\n}\n",
"a {\n color: -1;\n}\n"
);
test!(
null_minus_unquoted_string,
"a {\n color: null - foo;\n}\n",
"a {\n color: -foo;\n}\n"
);
error!(
null_minus_function,
"a {\n color: null - get-function(lighten);\n}\n",
"Error: get-function(\"lighten\") isn't a valid CSS value."
);
error!(
map_lhs_sub,
"a {color: (a: b) - 1;}", "Error: (a: b) isn't a valid CSS value."
);
error!(
map_rhs_sub,
"a {color: 1 - (a: b);}", "Error: (a: b) isn't a valid CSS value."
);
error!(
function_lhs_sub,
"a {color: get-function(lighten) - 1;}",
"Error: get-function(\"lighten\") isn't a valid CSS value."
);
error!(
function_rhs_sub,
"a {color: 1 - get-function(lighten);}",
"Error: get-function(\"lighten\") isn't a valid CSS value."
);