2020-04-12 19:37:12 -04:00
|
|
|
use codemap::Span;
|
2020-02-08 16:07:37 -05:00
|
|
|
|
2020-04-01 15:32:52 -04:00
|
|
|
use crate::common::{Op, QuoteKind};
|
2020-02-17 10:27:04 -05:00
|
|
|
use crate::error::SassResult;
|
2020-03-19 16:24:31 -04:00
|
|
|
use crate::unit::{Unit, UNIT_CONVERSION_TABLE};
|
2020-02-08 16:07:37 -05:00
|
|
|
use crate::value::Value;
|
2020-02-17 10:39:32 -05:00
|
|
|
|
2020-04-12 19:37:12 -04:00
|
|
|
impl Value {
|
|
|
|
pub fn add(self, mut other: Self, span: Span) -> SassResult<Self> {
|
2020-04-01 15:32:52 -04:00
|
|
|
if let Self::Paren(..) = other {
|
2020-04-12 19:37:12 -04:00
|
|
|
other = other.eval(span)?.node
|
2020-04-01 15:32:52 -04:00
|
|
|
}
|
|
|
|
let precedence = Op::Plus.precedence();
|
2020-02-17 10:27:04 -05:00
|
|
|
Ok(match self {
|
2020-04-03 21:38:34 -04:00
|
|
|
Self::Function(..) | Self::ArgList(..) | Self::Map(..) => todo!(),
|
2020-02-08 16:07:37 -05:00
|
|
|
Self::Important | Self::True | Self::False => match other {
|
|
|
|
Self::Ident(s, QuoteKind::Double) | Self::Ident(s, QuoteKind::Single) => {
|
2020-04-12 19:37:12 -04:00
|
|
|
Value::Ident(
|
|
|
|
format!("{}{}", self.to_css_string(span)?, s),
|
|
|
|
QuoteKind::Double,
|
|
|
|
)
|
2020-02-08 16:07:37 -05:00
|
|
|
}
|
2020-04-12 19:37:12 -04:00
|
|
|
Self::Null => Value::Ident(self.to_css_string(span)?, QuoteKind::None),
|
|
|
|
_ => Value::Ident(
|
|
|
|
format!(
|
|
|
|
"{}{}",
|
|
|
|
self.to_css_string(span)?,
|
|
|
|
other.to_css_string(span)?
|
|
|
|
),
|
|
|
|
QuoteKind::None,
|
|
|
|
),
|
2020-02-08 16:07:37 -05:00
|
|
|
},
|
|
|
|
Self::Null => match other {
|
|
|
|
Self::Null => Self::Null,
|
2020-04-12 19:37:12 -04:00
|
|
|
_ => Value::Ident(format!("{}", other.to_css_string(span)?), QuoteKind::None),
|
2020-02-08 16:07:37 -05:00
|
|
|
},
|
|
|
|
Self::Dimension(num, unit) => match other {
|
2020-03-01 14:53:52 -05:00
|
|
|
Self::Dimension(num2, unit2) => {
|
|
|
|
if !unit.comparable(&unit2) {
|
2020-04-12 19:37:12 -04:00
|
|
|
return Err(
|
|
|
|
(format!("Incompatible units {} and {}.", unit2, unit), span).into(),
|
|
|
|
);
|
2020-03-01 14:53:52 -05:00
|
|
|
}
|
|
|
|
if unit == unit2 {
|
|
|
|
Value::Dimension(num + num2, unit)
|
2020-03-18 10:08:40 -04:00
|
|
|
} else if unit == Unit::None {
|
|
|
|
Value::Dimension(num + num2, unit2)
|
|
|
|
} else if unit2 == Unit::None {
|
|
|
|
Value::Dimension(num + num2, unit)
|
2020-03-01 14:53:52 -05:00
|
|
|
} else {
|
2020-03-18 10:08:40 -04:00
|
|
|
Value::Dimension(
|
|
|
|
num + num2
|
|
|
|
* UNIT_CONVERSION_TABLE[&unit.to_string()][&unit2.to_string()]
|
|
|
|
.clone(),
|
|
|
|
unit,
|
|
|
|
)
|
2020-03-01 14:53:52 -05:00
|
|
|
}
|
|
|
|
}
|
2020-03-23 12:12:08 -04:00
|
|
|
Self::Ident(s, q) => Value::Ident(format!("{}{}{}", num, unit, s), q.normalize()),
|
2020-02-17 10:39:32 -05:00
|
|
|
Self::Null => Value::Ident(format!("{}{}", num, unit), QuoteKind::None),
|
2020-04-12 19:37:12 -04:00
|
|
|
Self::List(..) => Value::Ident(
|
|
|
|
format!("{}{}{}", num, unit, other.to_css_string(span)?),
|
|
|
|
QuoteKind::None,
|
|
|
|
),
|
2020-02-29 20:01:43 -05:00
|
|
|
_ => {
|
2020-04-12 19:37:12 -04:00
|
|
|
return Err((
|
|
|
|
format!(
|
|
|
|
"Undefined operation \"{}{} + {}\".",
|
|
|
|
num,
|
|
|
|
unit,
|
|
|
|
other.to_css_string(span)?
|
|
|
|
),
|
|
|
|
span,
|
2020-02-29 20:01:43 -05:00
|
|
|
)
|
2020-04-12 19:37:12 -04:00
|
|
|
.into())
|
2020-02-29 20:01:43 -05:00
|
|
|
}
|
2020-02-08 16:07:37 -05:00
|
|
|
},
|
2020-03-01 17:06:55 -05:00
|
|
|
Self::Color(c) => match other {
|
2020-03-23 12:12:08 -04:00
|
|
|
Self::Ident(s, q) => Value::Ident(format!("{}{}", c, s), q.normalize()),
|
2020-02-09 10:49:37 -05:00
|
|
|
Self::Null => Value::Ident(c.to_string(), QuoteKind::None),
|
2020-04-12 19:37:12 -04:00
|
|
|
Self::List(..) => Value::Ident(
|
|
|
|
format!("{}{}", c, other.to_css_string(span)?),
|
|
|
|
QuoteKind::None,
|
|
|
|
),
|
|
|
|
_ => {
|
|
|
|
return Err((
|
|
|
|
format!(
|
|
|
|
"Undefined operation \"{} + {}\".",
|
|
|
|
c,
|
|
|
|
other.to_css_string(span)?
|
|
|
|
),
|
|
|
|
span,
|
|
|
|
)
|
|
|
|
.into())
|
|
|
|
}
|
2020-02-09 12:18:41 -05:00
|
|
|
},
|
2020-04-01 15:32:52 -04:00
|
|
|
Self::BinaryOp(left, op, right) => {
|
|
|
|
if op.precedence() >= precedence {
|
2020-04-12 19:37:12 -04:00
|
|
|
Self::BinaryOp(left, op, right)
|
|
|
|
.eval(span)?
|
|
|
|
.node
|
|
|
|
.add(other, span)?
|
2020-04-01 15:32:52 -04:00
|
|
|
} else {
|
|
|
|
Self::BinaryOp(
|
|
|
|
left,
|
|
|
|
op,
|
2020-04-12 19:37:12 -04:00
|
|
|
Box::new(
|
|
|
|
Self::BinaryOp(right, Op::Plus, Box::new(other))
|
|
|
|
.eval(span)?
|
|
|
|
.node,
|
|
|
|
),
|
2020-04-01 15:32:52 -04:00
|
|
|
)
|
2020-04-12 19:37:12 -04:00
|
|
|
.eval(span)?
|
|
|
|
.node
|
2020-04-01 15:32:52 -04:00
|
|
|
}
|
|
|
|
}
|
2020-04-12 19:37:12 -04:00
|
|
|
Self::UnaryOp(..) | Self::Paren(..) => self.eval(span)?.node.add(other, span)?,
|
2020-02-08 16:07:37 -05:00
|
|
|
Self::Ident(s1, quotes1) => match other {
|
2020-04-01 21:43:58 -04:00
|
|
|
Self::Ident(s2, _) => Value::Ident(format!("{}{}", s1, s2), quotes1.normalize()),
|
2020-04-12 19:37:12 -04:00
|
|
|
Self::Important | Self::True | Self::False | Self::Dimension(..) => Value::Ident(
|
|
|
|
format!("{}{}", s1, other.to_css_string(span)?),
|
|
|
|
quotes1.normalize(),
|
|
|
|
),
|
2020-03-23 12:12:08 -04:00
|
|
|
Self::Null => Value::Ident(s1, quotes1.normalize()),
|
|
|
|
Self::Color(c) => Value::Ident(format!("{}{}", s1, c), quotes1.normalize()),
|
2020-04-12 19:37:12 -04:00
|
|
|
Self::List(..) => {
|
|
|
|
Value::Ident(format!("{}{}", s1, other.to_css_string(span)?), quotes1)
|
|
|
|
}
|
2020-04-01 15:32:52 -04:00
|
|
|
Self::UnaryOp(..) | Self::BinaryOp(..) => todo!(),
|
2020-04-12 19:37:12 -04:00
|
|
|
Self::Paren(..) => Self::Ident(s1, quotes1).add(other.eval(span)?.node, span)?,
|
2020-04-03 21:38:34 -04:00
|
|
|
Self::Function(..) | Self::ArgList(..) | Self::Map(..) => todo!(),
|
2020-02-08 16:07:37 -05:00
|
|
|
},
|
2020-03-19 14:20:16 -04:00
|
|
|
Self::List(..) => match other {
|
2020-04-12 19:37:12 -04:00
|
|
|
Self::Ident(s, q) => {
|
|
|
|
Value::Ident(format!("{}{}", self.to_css_string(span)?, s), q.normalize())
|
|
|
|
}
|
|
|
|
Self::Paren(..) => (self.add(other.eval(span)?.node, span))?,
|
|
|
|
_ => Value::Ident(
|
|
|
|
format!(
|
|
|
|
"{}{}",
|
|
|
|
self.to_css_string(span)?,
|
|
|
|
other.to_css_string(span)?
|
|
|
|
),
|
|
|
|
QuoteKind::None,
|
|
|
|
),
|
2020-03-19 16:24:31 -04:00
|
|
|
},
|
2020-02-17 10:27:04 -05:00
|
|
|
})
|
2020-02-08 16:07:37 -05:00
|
|
|
}
|
|
|
|
|
2020-04-12 19:37:12 -04:00
|
|
|
pub fn sub(self, mut other: Self, span: Span) -> SassResult<Self> {
|
2020-04-05 17:34:30 -04:00
|
|
|
if let Self::Paren(..) = other {
|
2020-04-12 19:37:12 -04:00
|
|
|
other = other.eval(span)?.node
|
2020-04-05 17:34:30 -04:00
|
|
|
}
|
|
|
|
let precedence = Op::Mul.precedence();
|
2020-02-17 10:27:04 -05:00
|
|
|
Ok(match self {
|
2020-02-09 16:05:07 -05:00
|
|
|
Self::Null => todo!(),
|
2020-02-08 16:07:37 -05:00
|
|
|
Self::Dimension(num, unit) => match other {
|
2020-03-01 14:53:52 -05:00
|
|
|
Self::Dimension(num2, unit2) => {
|
|
|
|
if !unit.comparable(&unit2) {
|
2020-04-12 19:37:12 -04:00
|
|
|
return Err(
|
|
|
|
(format!("Incompatible units {} and {}.", unit2, unit), span).into(),
|
|
|
|
);
|
2020-03-01 14:53:52 -05:00
|
|
|
}
|
|
|
|
if unit == unit2 {
|
|
|
|
Value::Dimension(num - num2, unit)
|
2020-03-18 10:14:35 -04:00
|
|
|
} else if unit == Unit::None {
|
|
|
|
Value::Dimension(num - num2, unit2)
|
|
|
|
} else if unit2 == Unit::None {
|
|
|
|
Value::Dimension(num - num2, unit)
|
2020-03-01 14:53:52 -05:00
|
|
|
} else {
|
2020-03-18 10:14:35 -04:00
|
|
|
Value::Dimension(
|
|
|
|
num - num2
|
|
|
|
* UNIT_CONVERSION_TABLE[&unit.to_string()][&unit2.to_string()]
|
|
|
|
.clone(),
|
|
|
|
unit,
|
|
|
|
)
|
2020-03-01 14:53:52 -05:00
|
|
|
}
|
|
|
|
}
|
2020-04-12 19:37:12 -04:00
|
|
|
Self::List(..) => Value::Ident(
|
|
|
|
format!("{}{}-{}", num, unit, other.to_css_string(span)?),
|
|
|
|
QuoteKind::None,
|
|
|
|
),
|
|
|
|
Self::Ident(..) => Value::Ident(
|
|
|
|
format!("{}{}-{}", num, unit, other.to_css_string(span)?),
|
|
|
|
QuoteKind::None,
|
|
|
|
),
|
2020-02-08 16:07:37 -05:00
|
|
|
_ => todo!(),
|
|
|
|
},
|
2020-02-09 11:07:13 -05:00
|
|
|
Self::Color(c) => match other {
|
2020-03-23 12:12:08 -04:00
|
|
|
Self::Ident(s, q) => Value::Ident(
|
|
|
|
format!("{}-{}{}{}", c, q.normalize(), s, q.normalize()),
|
|
|
|
QuoteKind::None,
|
|
|
|
),
|
2020-02-09 11:07:13 -05:00
|
|
|
Self::Null => Value::Ident(format!("{}-", c), QuoteKind::None),
|
2020-02-29 20:14:51 -05:00
|
|
|
Self::Dimension(..) | Self::Color(..) => {
|
2020-04-12 19:37:12 -04:00
|
|
|
return Err((
|
|
|
|
format!(
|
|
|
|
"Undefined operation \"{} - {}\".",
|
|
|
|
c,
|
|
|
|
other.to_css_string(span)?
|
|
|
|
),
|
|
|
|
span,
|
|
|
|
)
|
|
|
|
.into())
|
2020-02-29 20:01:43 -05:00
|
|
|
}
|
2020-04-12 19:37:12 -04:00
|
|
|
_ => Value::Ident(
|
|
|
|
format!("{}-{}", c, other.to_css_string(span)?),
|
|
|
|
QuoteKind::None,
|
|
|
|
),
|
2020-02-09 11:07:13 -05:00
|
|
|
},
|
2020-04-05 17:34:30 -04:00
|
|
|
Self::BinaryOp(left, op, right) => {
|
|
|
|
if op.precedence() >= precedence {
|
2020-04-12 19:37:12 -04:00
|
|
|
Self::BinaryOp(left, op, right)
|
|
|
|
.eval(span)?
|
|
|
|
.node
|
|
|
|
.sub(other, span)?
|
2020-04-05 17:34:30 -04:00
|
|
|
} else {
|
|
|
|
Self::BinaryOp(
|
|
|
|
left,
|
|
|
|
op,
|
2020-04-12 19:37:12 -04:00
|
|
|
Box::new(
|
|
|
|
Self::BinaryOp(right, Op::Minus, Box::new(other))
|
|
|
|
.eval(span)?
|
|
|
|
.node,
|
|
|
|
),
|
2020-04-05 17:34:30 -04:00
|
|
|
)
|
2020-04-12 19:37:12 -04:00
|
|
|
.eval(span)?
|
|
|
|
.node
|
2020-04-05 17:34:30 -04:00
|
|
|
}
|
|
|
|
}
|
2020-04-12 19:37:12 -04:00
|
|
|
Self::Paren(..) => self.eval(span)?.node.sub(other, span)?,
|
2020-03-23 12:12:08 -04:00
|
|
|
Self::Ident(s1, q1) => match other {
|
2020-03-23 13:57:00 -04:00
|
|
|
Self::Ident(s2, q2) => Value::Ident(
|
|
|
|
format!(
|
|
|
|
"{}{}{}-{}{}{}",
|
|
|
|
q1.normalize(),
|
|
|
|
s1,
|
|
|
|
q1.normalize(),
|
|
|
|
q2.normalize(),
|
|
|
|
s2,
|
|
|
|
q2.normalize()
|
|
|
|
),
|
|
|
|
QuoteKind::None,
|
|
|
|
),
|
2020-02-09 12:18:41 -05:00
|
|
|
Self::Important
|
|
|
|
| Self::True
|
|
|
|
| Self::False
|
|
|
|
| Self::Dimension(..)
|
2020-03-23 13:57:00 -04:00
|
|
|
| Self::Color(..) => Value::Ident(
|
2020-04-12 19:37:12 -04:00
|
|
|
format!(
|
|
|
|
"{}{}{}-{}",
|
|
|
|
q1.normalize(),
|
|
|
|
s1,
|
|
|
|
q1.normalize(),
|
|
|
|
other.to_css_string(span)?
|
|
|
|
),
|
2020-03-23 13:57:00 -04:00
|
|
|
QuoteKind::None,
|
|
|
|
),
|
|
|
|
Self::Null => Value::Ident(
|
|
|
|
format!("{}{}{}-", q1.normalize(), s1, q1.normalize()),
|
|
|
|
QuoteKind::None,
|
|
|
|
),
|
|
|
|
Self::List(..) => Value::Ident(
|
2020-04-12 19:37:12 -04:00
|
|
|
format!(
|
|
|
|
"{}{}{}-{}",
|
|
|
|
q1.normalize(),
|
|
|
|
s1,
|
|
|
|
q1.normalize(),
|
|
|
|
other.to_css_string(span)?
|
|
|
|
),
|
2020-03-23 13:57:00 -04:00
|
|
|
QuoteKind::None,
|
|
|
|
),
|
2020-02-08 16:07:37 -05:00
|
|
|
_ => todo!(),
|
|
|
|
},
|
2020-03-19 14:20:16 -04:00
|
|
|
Self::List(..) => match other {
|
2020-03-23 13:57:00 -04:00
|
|
|
Self::Ident(s, q) => Value::Ident(
|
2020-04-12 19:37:12 -04:00
|
|
|
format!(
|
|
|
|
"{}-{}{}{}",
|
|
|
|
self.to_css_string(span)?,
|
|
|
|
q.normalize(),
|
|
|
|
s,
|
|
|
|
q.normalize()
|
|
|
|
),
|
|
|
|
QuoteKind::None,
|
|
|
|
),
|
|
|
|
_ => Value::Ident(
|
|
|
|
format!(
|
|
|
|
"{}-{}",
|
|
|
|
self.to_css_string(span)?,
|
|
|
|
other.to_css_string(span)?
|
|
|
|
),
|
2020-03-23 13:57:00 -04:00
|
|
|
QuoteKind::None,
|
|
|
|
),
|
2020-03-19 16:24:31 -04:00
|
|
|
},
|
2020-02-09 16:05:07 -05:00
|
|
|
_ => match other {
|
2020-03-23 13:57:00 -04:00
|
|
|
Self::Ident(s, q) => Value::Ident(
|
2020-04-12 19:37:12 -04:00
|
|
|
format!(
|
|
|
|
"{}-{}{}{}",
|
|
|
|
self.to_css_string(span)?,
|
|
|
|
q.normalize(),
|
|
|
|
s,
|
|
|
|
q.normalize()
|
|
|
|
),
|
|
|
|
QuoteKind::None,
|
|
|
|
),
|
|
|
|
Self::Null => {
|
|
|
|
Value::Ident(format!("{}-", self.to_css_string(span)?), QuoteKind::None)
|
|
|
|
}
|
|
|
|
_ => Value::Ident(
|
|
|
|
format!(
|
|
|
|
"{}-{}",
|
|
|
|
self.to_css_string(span)?,
|
|
|
|
other.to_css_string(span)?
|
|
|
|
),
|
2020-03-23 13:57:00 -04:00
|
|
|
QuoteKind::None,
|
|
|
|
),
|
2020-02-09 16:05:07 -05:00
|
|
|
},
|
2020-02-17 10:27:04 -05:00
|
|
|
})
|
2020-02-09 16:05:07 -05:00
|
|
|
}
|
|
|
|
|
2020-04-12 19:37:12 -04:00
|
|
|
pub fn mul(self, mut other: Self, span: Span) -> SassResult<Self> {
|
2020-04-05 17:34:30 -04:00
|
|
|
if let Self::Paren(..) = other {
|
2020-04-12 19:37:12 -04:00
|
|
|
other = other.eval(span)?.node
|
2020-04-05 17:34:30 -04:00
|
|
|
}
|
|
|
|
let precedence = Op::Mul.precedence();
|
2020-02-17 10:27:04 -05:00
|
|
|
Ok(match self {
|
2020-02-09 16:05:07 -05:00
|
|
|
Self::Null => todo!(),
|
|
|
|
Self::Dimension(num, unit) => match other {
|
2020-03-01 14:53:52 -05:00
|
|
|
Self::Dimension(num2, unit2) => {
|
2020-03-16 21:29:00 -04:00
|
|
|
if unit == Unit::None {
|
|
|
|
Value::Dimension(num * num2, unit2)
|
|
|
|
} else if unit2 == Unit::None {
|
2020-03-01 14:53:52 -05:00
|
|
|
Value::Dimension(num * num2, unit)
|
2020-03-17 10:06:24 -04:00
|
|
|
} else if let Unit::Mul(mut u) = unit {
|
|
|
|
u.push(unit2);
|
|
|
|
Value::Dimension(num * num2, Unit::Mul(u))
|
|
|
|
} else if let Unit::Mul(u2) = unit2 {
|
|
|
|
let mut u = vec![unit];
|
|
|
|
u.extend(u2);
|
|
|
|
Value::Dimension(num * num2, Unit::Mul(u))
|
2020-03-01 14:53:52 -05:00
|
|
|
} else {
|
2020-03-17 10:06:24 -04:00
|
|
|
Value::Dimension(num * num2, Unit::Mul(vec![unit, unit2]))
|
2020-03-01 14:53:52 -05:00
|
|
|
}
|
|
|
|
}
|
2020-02-29 20:01:43 -05:00
|
|
|
_ => {
|
2020-04-12 19:37:12 -04:00
|
|
|
return Err((
|
|
|
|
format!(
|
|
|
|
"Undefined operation \"{}{} * {}\".",
|
|
|
|
num,
|
|
|
|
unit,
|
|
|
|
other.to_css_string(span)?
|
|
|
|
),
|
|
|
|
span,
|
2020-02-29 20:01:43 -05:00
|
|
|
)
|
2020-04-12 19:37:12 -04:00
|
|
|
.into())
|
2020-02-29 20:01:43 -05:00
|
|
|
}
|
2020-02-09 16:05:07 -05:00
|
|
|
},
|
2020-04-05 17:34:30 -04:00
|
|
|
Self::BinaryOp(left, op, right) => {
|
|
|
|
if op.precedence() >= precedence {
|
2020-04-12 19:37:12 -04:00
|
|
|
Self::BinaryOp(left, op, right)
|
|
|
|
.eval(span)?
|
|
|
|
.node
|
|
|
|
.mul(other, span)?
|
2020-04-05 17:34:30 -04:00
|
|
|
} else {
|
|
|
|
Self::BinaryOp(
|
|
|
|
left,
|
|
|
|
op,
|
2020-04-12 19:37:12 -04:00
|
|
|
Box::new(
|
|
|
|
Self::BinaryOp(right, Op::Mul, Box::new(other))
|
|
|
|
.eval(span)?
|
|
|
|
.node,
|
|
|
|
),
|
2020-04-05 17:34:30 -04:00
|
|
|
)
|
2020-04-12 19:37:12 -04:00
|
|
|
.eval(span)?
|
|
|
|
.node
|
2020-04-05 17:34:30 -04:00
|
|
|
}
|
|
|
|
}
|
2020-04-12 19:37:12 -04:00
|
|
|
Self::UnaryOp(..) | Self::Paren(..) => self.eval(span)?.node.mul(other, span)?,
|
|
|
|
_ => {
|
|
|
|
return Err((
|
|
|
|
format!(
|
|
|
|
"Undefined operation \"{} * {}\".",
|
|
|
|
self.to_css_string(span)?,
|
|
|
|
other.to_css_string(span)?
|
|
|
|
),
|
|
|
|
span,
|
|
|
|
)
|
|
|
|
.into())
|
|
|
|
}
|
2020-02-17 10:27:04 -05:00
|
|
|
})
|
2020-02-09 16:05:07 -05:00
|
|
|
}
|
2020-03-17 10:06:24 -04:00
|
|
|
|
2020-04-12 19:37:12 -04:00
|
|
|
pub fn div(self, other: Self, span: Span) -> SassResult<Self> {
|
2020-04-01 15:32:52 -04:00
|
|
|
let precedence = Op::Div.precedence();
|
2020-02-17 10:27:04 -05:00
|
|
|
Ok(match self {
|
2020-02-09 16:05:07 -05:00
|
|
|
Self::Null => todo!(),
|
|
|
|
Self::Dimension(num, unit) => match other {
|
2020-02-09 18:28:24 -05:00
|
|
|
Self::Dimension(num2, unit2) => {
|
2020-03-31 02:31:14 -04:00
|
|
|
if !unit.comparable(&unit2) {
|
2020-04-12 19:37:12 -04:00
|
|
|
return Err(
|
|
|
|
(format!("Incompatible units {} and {}.", unit2, unit), span).into(),
|
|
|
|
);
|
2020-03-31 02:31:14 -04:00
|
|
|
}
|
2020-02-09 18:28:24 -05:00
|
|
|
if unit == unit2 {
|
|
|
|
Value::Dimension(num / num2, Unit::None)
|
2020-03-31 02:31:14 -04:00
|
|
|
} else if unit == Unit::None {
|
|
|
|
todo!("inverse units")
|
|
|
|
} else if unit2 == Unit::None {
|
|
|
|
Value::Dimension(num / num2, unit)
|
2020-02-09 18:28:24 -05:00
|
|
|
} else {
|
2020-03-31 02:31:14 -04:00
|
|
|
Value::Dimension(
|
|
|
|
num / (num2
|
|
|
|
* UNIT_CONVERSION_TABLE[&unit.to_string()][&unit2.to_string()]
|
|
|
|
.clone()),
|
|
|
|
Unit::None,
|
|
|
|
)
|
2020-02-09 18:28:24 -05:00
|
|
|
}
|
|
|
|
}
|
2020-03-23 13:57:00 -04:00
|
|
|
Self::Ident(s, q) => Value::Ident(
|
|
|
|
format!("{}{}/{}{}{}", num, unit, q.normalize(), s, q.normalize()),
|
|
|
|
QuoteKind::None,
|
|
|
|
),
|
2020-02-17 10:27:04 -05:00
|
|
|
Self::BinaryOp(..) | Self::Paren(..) => {
|
2020-04-12 19:37:12 -04:00
|
|
|
Self::Dimension(num, unit).div(other.eval(span)?.node, span)?
|
2020-02-17 10:27:04 -05:00
|
|
|
}
|
2020-02-09 16:05:07 -05:00
|
|
|
_ => todo!(),
|
|
|
|
},
|
|
|
|
Self::Color(c) => match other {
|
2020-03-23 13:57:00 -04:00
|
|
|
Self::Ident(s, q) => Value::Ident(
|
|
|
|
format!("{}/{}{}{}", c, q.normalize(), s, q.normalize()),
|
|
|
|
QuoteKind::None,
|
|
|
|
),
|
2020-02-09 16:05:07 -05:00
|
|
|
Self::Null => Value::Ident(format!("{}/", c), QuoteKind::None),
|
2020-02-29 20:14:51 -05:00
|
|
|
Self::Dimension(..) | Self::Color(..) => {
|
2020-04-12 19:37:12 -04:00
|
|
|
return Err((
|
|
|
|
format!(
|
|
|
|
"Undefined operation \"{} / {}\".",
|
|
|
|
c,
|
|
|
|
other.to_css_string(span)?
|
|
|
|
),
|
|
|
|
span,
|
|
|
|
)
|
|
|
|
.into())
|
2020-02-29 20:01:43 -05:00
|
|
|
}
|
2020-04-12 19:37:12 -04:00
|
|
|
_ => Value::Ident(
|
|
|
|
format!("{}/{}", c, other.to_css_string(span)?),
|
|
|
|
QuoteKind::None,
|
|
|
|
),
|
2020-02-09 16:05:07 -05:00
|
|
|
},
|
2020-04-01 15:32:52 -04:00
|
|
|
Self::BinaryOp(left, op, right) => {
|
|
|
|
if op.precedence() >= precedence {
|
2020-04-12 19:37:12 -04:00
|
|
|
Self::BinaryOp(left, op, right)
|
|
|
|
.eval(span)?
|
|
|
|
.node
|
|
|
|
.div(other, span)?
|
2020-04-01 15:32:52 -04:00
|
|
|
} else {
|
|
|
|
Self::BinaryOp(
|
|
|
|
left,
|
|
|
|
op,
|
2020-04-12 19:37:12 -04:00
|
|
|
Box::new(
|
|
|
|
Self::BinaryOp(right, Op::Div, Box::new(other))
|
|
|
|
.eval(span)?
|
|
|
|
.node,
|
|
|
|
),
|
2020-04-01 15:32:52 -04:00
|
|
|
)
|
2020-04-12 19:37:12 -04:00
|
|
|
.eval(span)?
|
|
|
|
.node
|
2020-04-01 15:32:52 -04:00
|
|
|
}
|
|
|
|
}
|
2020-04-12 19:37:12 -04:00
|
|
|
Self::Paren(..) => self.eval(span)?.node.div(other, span)?,
|
2020-03-23 12:12:08 -04:00
|
|
|
Self::Ident(s1, q1) => match other {
|
2020-03-23 13:57:00 -04:00
|
|
|
Self::Ident(s2, q2) => Value::Ident(
|
|
|
|
format!(
|
|
|
|
"{}{}{}/{}{}{}",
|
|
|
|
q1.normalize(),
|
|
|
|
s1,
|
|
|
|
q1.normalize(),
|
|
|
|
q2.normalize(),
|
|
|
|
s2,
|
|
|
|
q2.normalize()
|
|
|
|
),
|
|
|
|
QuoteKind::None,
|
|
|
|
),
|
2020-02-09 16:05:07 -05:00
|
|
|
Self::Important
|
|
|
|
| Self::True
|
|
|
|
| Self::False
|
|
|
|
| Self::Dimension(..)
|
2020-03-23 13:57:00 -04:00
|
|
|
| Self::Color(..) => Value::Ident(
|
2020-04-12 19:37:12 -04:00
|
|
|
format!(
|
|
|
|
"{}{}{}/{}",
|
|
|
|
q1.normalize(),
|
|
|
|
s1,
|
|
|
|
q1.normalize(),
|
|
|
|
other.to_css_string(span)?
|
|
|
|
),
|
2020-03-23 13:57:00 -04:00
|
|
|
QuoteKind::None,
|
|
|
|
),
|
|
|
|
Self::Null => Value::Ident(
|
|
|
|
format!("{}{}{}/", q1.normalize(), s1, q1.normalize()),
|
|
|
|
QuoteKind::None,
|
|
|
|
),
|
2020-02-09 16:05:07 -05:00
|
|
|
_ => todo!(),
|
|
|
|
},
|
|
|
|
_ => match other {
|
2020-03-23 13:57:00 -04:00
|
|
|
Self::Ident(s, q) => Value::Ident(
|
2020-04-12 19:37:12 -04:00
|
|
|
format!(
|
|
|
|
"{}/{}{}{}",
|
|
|
|
self.to_css_string(span)?,
|
|
|
|
q.normalize(),
|
|
|
|
s,
|
|
|
|
q.normalize()
|
|
|
|
),
|
|
|
|
QuoteKind::None,
|
|
|
|
),
|
|
|
|
Self::Null => {
|
|
|
|
Value::Ident(format!("{}/", self.to_css_string(span)?), QuoteKind::None)
|
|
|
|
}
|
|
|
|
_ => Value::Ident(
|
|
|
|
format!(
|
|
|
|
"{}/{}",
|
|
|
|
self.to_css_string(span)?,
|
|
|
|
other.to_css_string(span)?
|
|
|
|
),
|
2020-03-23 13:57:00 -04:00
|
|
|
QuoteKind::None,
|
|
|
|
),
|
2020-02-09 16:05:07 -05:00
|
|
|
},
|
2020-02-17 10:27:04 -05:00
|
|
|
})
|
2020-02-08 16:07:37 -05:00
|
|
|
}
|
2020-03-18 11:39:58 -04:00
|
|
|
|
2020-04-12 19:37:12 -04:00
|
|
|
pub fn rem(self, other: Self, span: Span) -> SassResult<Self> {
|
2020-03-18 11:39:58 -04:00
|
|
|
Ok(match self {
|
|
|
|
Value::Dimension(n, u) => match other {
|
|
|
|
Value::Dimension(n2, u2) => {
|
|
|
|
if !u.comparable(&u2) {
|
2020-04-12 19:37:12 -04:00
|
|
|
return Err((format!("Incompatible units {} and {}.", u2, u), span).into());
|
2020-03-18 11:39:58 -04:00
|
|
|
}
|
|
|
|
if u == u2 {
|
|
|
|
Value::Dimension(n % n2, u)
|
|
|
|
} else if u == Unit::None {
|
|
|
|
Value::Dimension(n % n2, u2)
|
|
|
|
} else if u2 == Unit::None {
|
|
|
|
Value::Dimension(n % n2, u)
|
|
|
|
} else {
|
|
|
|
Value::Dimension(n, u)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
2020-04-12 19:37:12 -04:00
|
|
|
return Err((
|
|
|
|
format!(
|
|
|
|
"Undefined operation \"{} % {}\".",
|
|
|
|
Value::Dimension(n, u).to_css_string(span)?,
|
|
|
|
other.to_css_string(span)?
|
|
|
|
),
|
|
|
|
span,
|
2020-03-18 11:39:58 -04:00
|
|
|
)
|
2020-04-12 19:37:12 -04:00
|
|
|
.into())
|
2020-03-18 11:39:58 -04:00
|
|
|
}
|
|
|
|
},
|
2020-04-12 19:37:12 -04:00
|
|
|
_ => {
|
|
|
|
return Err((
|
|
|
|
format!(
|
|
|
|
"Undefined operation \"{} % {}\".",
|
|
|
|
self.to_css_string(span)?,
|
|
|
|
other.to_css_string(span)?
|
|
|
|
),
|
|
|
|
span,
|
|
|
|
)
|
|
|
|
.into())
|
|
|
|
}
|
2020-03-18 11:39:58 -04:00
|
|
|
})
|
|
|
|
}
|
2020-03-21 12:14:02 -04:00
|
|
|
|
2020-04-12 19:37:12 -04:00
|
|
|
pub fn neg(self, span: Span) -> SassResult<Self> {
|
|
|
|
Ok(match self.eval(span)?.node {
|
2020-03-21 12:14:02 -04:00
|
|
|
Value::Dimension(n, u) => Value::Dimension(-n, u),
|
2020-04-12 19:37:12 -04:00
|
|
|
v => Value::Ident(format!("-{}", v.to_css_string(span)?), QuoteKind::None),
|
2020-03-21 12:14:02 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|