2020-03-30 15:43:15 -04:00
|
|
|
use std::cmp::Ordering;
|
2020-02-08 16:17:58 -05:00
|
|
|
use std::iter::Iterator;
|
2020-01-25 09:58:53 -05:00
|
|
|
|
2020-04-12 19:37:12 -04:00
|
|
|
use codemap::{Span, Spanned};
|
|
|
|
|
2020-01-25 09:58:53 -05:00
|
|
|
use crate::color::Color;
|
2020-03-23 19:56:24 -04:00
|
|
|
use crate::common::{Brackets, ListSeparator, Op, QuoteKind};
|
2020-02-17 10:27:04 -05:00
|
|
|
use crate::error::SassResult;
|
2020-03-30 10:42:13 -04:00
|
|
|
use crate::unit::{Unit, UNIT_CONVERSION_TABLE};
|
2020-04-19 15:50:22 -04:00
|
|
|
use crate::utils::hex_char_for;
|
2020-03-30 15:43:15 -04:00
|
|
|
|
2020-04-06 21:22:03 -04:00
|
|
|
use css_function::is_special_function;
|
2020-03-30 15:43:15 -04:00
|
|
|
pub(crate) use map::SassMap;
|
2020-02-08 18:43:18 -05:00
|
|
|
pub(crate) use number::Number;
|
2020-04-04 21:07:53 -04:00
|
|
|
pub(crate) use sass_function::SassFunction;
|
2020-01-25 09:58:53 -05:00
|
|
|
|
2020-04-06 19:26:47 -04:00
|
|
|
mod css_function;
|
2020-03-30 15:43:15 -04:00
|
|
|
mod map;
|
2020-02-08 18:43:18 -05:00
|
|
|
mod number;
|
2020-02-08 16:07:37 -05:00
|
|
|
mod ops;
|
2020-02-08 16:17:58 -05:00
|
|
|
mod parse;
|
2020-04-04 21:07:53 -04:00
|
|
|
mod sass_function;
|
2020-01-25 09:58:53 -05:00
|
|
|
|
2020-03-30 15:43:15 -04:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
2020-01-25 09:58:53 -05:00
|
|
|
pub(crate) enum Value {
|
|
|
|
Important,
|
|
|
|
True,
|
|
|
|
False,
|
|
|
|
Null,
|
2020-02-08 18:43:18 -05:00
|
|
|
Dimension(Number, Unit),
|
2020-03-23 19:56:24 -04:00
|
|
|
List(Vec<Value>, ListSeparator, Brackets),
|
2020-04-21 18:54:19 -04:00
|
|
|
Color(Box<Color>),
|
2020-03-21 12:14:02 -04:00
|
|
|
UnaryOp(Op, Box<Value>),
|
2020-01-25 10:54:25 -05:00
|
|
|
BinaryOp(Box<Value>, Op, Box<Value>),
|
2020-01-25 09:58:53 -05:00
|
|
|
Paren(Box<Value>),
|
|
|
|
Ident(String, QuoteKind),
|
2020-03-30 15:43:15 -04:00
|
|
|
Map(SassMap),
|
2020-04-12 19:37:12 -04:00
|
|
|
ArgList(Vec<Spanned<Value>>),
|
2020-04-03 21:38:34 -04:00
|
|
|
/// Returned by `get-function()`
|
2020-04-03 23:47:56 -04:00
|
|
|
Function(SassFunction),
|
2020-01-25 09:58:53 -05:00
|
|
|
}
|
|
|
|
|
2020-04-19 13:51:34 -04:00
|
|
|
fn visit_quoted_string(buf: &mut String, force_double_quote: bool, string: &str) -> SassResult<()> {
|
|
|
|
let mut has_single_quote = false;
|
|
|
|
let mut has_double_quote = false;
|
|
|
|
|
|
|
|
let mut buffer = String::new();
|
|
|
|
|
|
|
|
if force_double_quote {
|
|
|
|
buffer.push('"');
|
|
|
|
}
|
|
|
|
let mut iter = string.chars().peekable();
|
|
|
|
while let Some(c) = iter.next() {
|
|
|
|
match c {
|
|
|
|
'\'' => {
|
|
|
|
if force_double_quote {
|
|
|
|
buffer.push('\'');
|
|
|
|
} else if has_double_quote {
|
|
|
|
return visit_quoted_string(buf, true, string);
|
|
|
|
} else {
|
|
|
|
has_single_quote = true;
|
|
|
|
buffer.push('\'');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
'"' => {
|
|
|
|
if force_double_quote {
|
|
|
|
buffer.push('\\');
|
|
|
|
buffer.push('"');
|
|
|
|
} else if has_single_quote {
|
|
|
|
return visit_quoted_string(buf, true, string);
|
|
|
|
} else {
|
|
|
|
has_double_quote = true;
|
|
|
|
buffer.push('"');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
'\x00'..='\x08' | '\x0A'..='\x1F' => {
|
|
|
|
buffer.push('\\');
|
|
|
|
if c as u32 > 0xF {
|
|
|
|
buffer.push(hex_char_for(c as u32 >> 4))
|
|
|
|
}
|
|
|
|
buffer.push(hex_char_for(c as u32 & 0xF));
|
|
|
|
if iter.peek().is_none() {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
let next = iter.peek().unwrap();
|
|
|
|
|
|
|
|
if next.is_ascii_hexdigit() || next == &' ' || next == &'\t' {
|
|
|
|
buffer.push(' ');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
'\\' => {
|
|
|
|
buffer.push('\\');
|
|
|
|
buffer.push('\\');
|
|
|
|
}
|
|
|
|
_ => buffer.push(c),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if force_double_quote {
|
|
|
|
buffer.push('"');
|
|
|
|
} else {
|
|
|
|
let quote = if has_double_quote { '\'' } else { '"' };
|
|
|
|
buffer = format!("{}{}{}", quote, buffer, quote);
|
|
|
|
}
|
|
|
|
buf.push_str(&buffer);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-04-12 19:37:12 -04:00
|
|
|
impl Value {
|
2020-04-19 00:39:18 -04:00
|
|
|
pub fn is_null(&self, span: Span) -> SassResult<bool> {
|
2020-01-25 09:58:53 -05:00
|
|
|
match self {
|
2020-04-19 00:39:18 -04:00
|
|
|
&Value::Null => Ok(true),
|
|
|
|
Value::Ident(i, QuoteKind::None) if i.is_empty() => Ok(true),
|
|
|
|
Self::BinaryOp(..) | Self::Paren(..) | Self::UnaryOp(..) => {
|
|
|
|
self.clone().eval(span)?.is_null(span)
|
|
|
|
}
|
2020-04-21 18:22:26 -04:00
|
|
|
Self::List(v, ..) => Ok(v.iter().all(|f| f.is_null(span).unwrap())),
|
2020-04-19 00:39:18 -04:00
|
|
|
_ => Ok(false),
|
2020-04-12 19:37:12 -04:00
|
|
|
}
|
|
|
|
}
|
2020-04-18 20:11:49 -04:00
|
|
|
|
2020-04-12 19:37:12 -04:00
|
|
|
pub fn to_css_string(&self, span: Span) -> SassResult<String> {
|
|
|
|
Ok(match self {
|
2020-04-21 18:22:26 -04:00
|
|
|
Self::Important => "!important".to_string(),
|
2020-03-16 21:29:00 -04:00
|
|
|
Self::Dimension(num, unit) => match unit {
|
2020-03-18 20:11:14 -04:00
|
|
|
Unit::Mul(..) => {
|
2020-04-18 20:11:49 -04:00
|
|
|
return Err((format!("{}{} isn't a valid CSS value.", num, unit), span).into());
|
2020-03-16 21:29:00 -04:00
|
|
|
}
|
2020-04-12 19:37:12 -04:00
|
|
|
_ => format!("{}{}", num, unit),
|
2020-03-16 21:29:00 -04:00
|
|
|
},
|
2020-04-18 20:11:49 -04:00
|
|
|
Self::Map(..) => {
|
|
|
|
return Err((
|
2020-04-19 00:39:18 -04:00
|
|
|
format!("{} isn't a valid CSS value.", self.inspect(span)?),
|
2020-04-18 20:11:49 -04:00
|
|
|
span,
|
|
|
|
)
|
|
|
|
.into())
|
|
|
|
}
|
2020-04-12 19:37:12 -04:00
|
|
|
Self::Function(func) => format!("get-function(\"{}\")", func.name()),
|
2020-03-23 19:56:24 -04:00
|
|
|
Self::List(vals, sep, brackets) => match brackets {
|
2020-04-21 18:22:26 -04:00
|
|
|
Brackets::None => vals
|
|
|
|
.iter()
|
|
|
|
.filter(|x| !x.is_null(span).unwrap())
|
|
|
|
.map(|x| x.to_css_string(span))
|
|
|
|
.collect::<SassResult<Vec<String>>>()?
|
|
|
|
.join(sep.as_str()),
|
2020-04-12 19:37:12 -04:00
|
|
|
Brackets::Bracketed => format!(
|
2020-03-23 19:56:24 -04:00
|
|
|
"[{}]",
|
|
|
|
vals.iter()
|
2020-04-19 00:39:18 -04:00
|
|
|
.filter(|x| !x.is_null(span).unwrap())
|
2020-04-12 19:37:12 -04:00
|
|
|
.map(|x| x.to_css_string(span))
|
|
|
|
.collect::<SassResult<Vec<String>>>()?
|
2020-03-23 19:56:24 -04:00
|
|
|
.join(sep.as_str()),
|
|
|
|
),
|
|
|
|
},
|
2020-04-12 19:37:12 -04:00
|
|
|
Self::Color(c) => format!("{}", c),
|
|
|
|
Self::UnaryOp(..) | Self::BinaryOp(..) => {
|
2020-04-21 18:22:26 -04:00
|
|
|
self.clone().eval(span)?.to_css_string(span)?
|
2020-04-12 19:37:12 -04:00
|
|
|
}
|
2020-04-21 18:22:26 -04:00
|
|
|
Self::Paren(val) => val.to_css_string(span)?,
|
2020-04-19 13:51:34 -04:00
|
|
|
Self::Ident(string, QuoteKind::None) => {
|
|
|
|
let mut after_newline = false;
|
|
|
|
let mut buf = String::with_capacity(string.len());
|
|
|
|
for c in string.chars() {
|
|
|
|
match c {
|
2020-04-20 12:12:39 -04:00
|
|
|
'\n' => {
|
|
|
|
buf.push(' ');
|
|
|
|
after_newline = true;
|
|
|
|
}
|
2020-04-19 13:51:34 -04:00
|
|
|
' ' => {
|
|
|
|
if !after_newline {
|
|
|
|
buf.push(' ');
|
2020-02-24 19:49:24 -05:00
|
|
|
}
|
|
|
|
}
|
2020-04-19 13:51:34 -04:00
|
|
|
_ => {
|
|
|
|
buf.push(c);
|
|
|
|
after_newline = false;
|
|
|
|
}
|
2020-02-24 19:49:24 -05:00
|
|
|
}
|
|
|
|
}
|
2020-04-19 13:51:34 -04:00
|
|
|
buf
|
|
|
|
}
|
|
|
|
Self::Ident(string, QuoteKind::Quoted) => {
|
|
|
|
let mut buf = String::with_capacity(string.len());
|
|
|
|
visit_quoted_string(&mut buf, false, string)?;
|
|
|
|
buf
|
2020-02-24 19:49:24 -05:00
|
|
|
}
|
2020-04-12 19:37:12 -04:00
|
|
|
Self::True => "true".to_string(),
|
|
|
|
Self::False => "false".to_string(),
|
2020-04-18 13:44:11 -04:00
|
|
|
Self::Null => String::new(),
|
2020-04-21 18:22:26 -04:00
|
|
|
Self::ArgList(args) => args
|
|
|
|
.iter()
|
|
|
|
.filter(|x| !x.is_null(span).unwrap())
|
|
|
|
.map(|a| Ok(a.node.to_css_string(span)?))
|
|
|
|
.collect::<SassResult<Vec<String>>>()?
|
|
|
|
.join(", "),
|
2020-04-12 19:37:12 -04:00
|
|
|
})
|
2020-01-26 15:04:16 -05:00
|
|
|
}
|
|
|
|
|
2020-04-12 19:37:12 -04:00
|
|
|
pub fn is_true(&self, span: Span) -> SassResult<bool> {
|
2020-02-08 16:01:21 -05:00
|
|
|
match self {
|
2020-02-17 10:27:04 -05:00
|
|
|
Value::Null | Value::False => Ok(false),
|
2020-03-23 15:21:59 -04:00
|
|
|
Self::BinaryOp(..) | Self::Paren(..) | Self::UnaryOp(..) => {
|
2020-04-12 19:37:12 -04:00
|
|
|
self.clone().eval(span)?.is_true(span)
|
2020-03-23 15:21:59 -04:00
|
|
|
}
|
2020-02-17 10:27:04 -05:00
|
|
|
_ => Ok(true),
|
2020-02-08 16:01:21 -05:00
|
|
|
}
|
2020-01-25 09:58:53 -05:00
|
|
|
}
|
|
|
|
|
2020-02-02 21:11:22 -05:00
|
|
|
pub fn unquote(self) -> Self {
|
|
|
|
match self {
|
|
|
|
Self::Ident(s1, _) => Self::Ident(s1, QuoteKind::None),
|
2020-04-19 22:54:56 -04:00
|
|
|
Self::List(v, sep, bracket) => {
|
2020-04-21 18:22:26 -04:00
|
|
|
Self::List(v.into_iter().map(Value::unquote).collect(), sep, bracket)
|
2020-04-19 22:54:56 -04:00
|
|
|
}
|
2020-02-24 16:58:48 -05:00
|
|
|
v => v,
|
2020-02-02 21:11:22 -05:00
|
|
|
}
|
2020-01-25 09:58:53 -05:00
|
|
|
}
|
|
|
|
|
2020-04-21 18:22:26 -04:00
|
|
|
pub const fn span(self, span: Span) -> Spanned<Self> {
|
2020-04-12 19:37:12 -04:00
|
|
|
Spanned { node: self, span }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn kind(&self, span: Span) -> SassResult<&'static str> {
|
2020-02-03 07:56:21 -05:00
|
|
|
match self {
|
2020-03-23 15:13:19 -04:00
|
|
|
Self::Color(..) => Ok("color"),
|
|
|
|
Self::Ident(..) | Self::Important => Ok("string"),
|
|
|
|
Self::Dimension(..) => Ok("number"),
|
|
|
|
Self::List(..) => Ok("list"),
|
2020-04-03 21:38:34 -04:00
|
|
|
Self::Function(..) => Ok("function"),
|
2020-04-02 13:33:26 -04:00
|
|
|
Self::ArgList(..) => Ok("arglist"),
|
2020-03-23 15:13:19 -04:00
|
|
|
Self::True | Self::False => Ok("bool"),
|
|
|
|
Self::Null => Ok("null"),
|
2020-03-30 15:43:15 -04:00
|
|
|
Self::Map(..) => Ok("map"),
|
2020-04-12 19:37:12 -04:00
|
|
|
Self::BinaryOp(..) | Self::Paren(..) | Self::UnaryOp(..) => {
|
|
|
|
self.clone().eval(span)?.kind(span)
|
|
|
|
}
|
2020-02-03 07:56:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-06 21:22:03 -04:00
|
|
|
pub fn is_special_function(&self) -> bool {
|
|
|
|
match self {
|
|
|
|
Self::Ident(s, QuoteKind::None) => is_special_function(s),
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-08 15:53:49 -05:00
|
|
|
pub fn bool(b: bool) -> Self {
|
|
|
|
if b {
|
|
|
|
Value::True
|
|
|
|
} else {
|
|
|
|
Value::False
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-12 19:37:12 -04:00
|
|
|
pub fn inspect(&self, span: Span) -> SassResult<String> {
|
|
|
|
Ok(match self {
|
2020-04-06 00:27:09 -04:00
|
|
|
Value::List(v, _, brackets) if v.is_empty() => match brackets {
|
|
|
|
Brackets::None => "()".to_string(),
|
|
|
|
Brackets::Bracketed => "[]".to_string(),
|
|
|
|
},
|
2020-04-20 02:55:55 -04:00
|
|
|
Value::List(v, sep, brackets) if v.len() == 1 => match brackets {
|
|
|
|
Brackets::None => match sep {
|
2020-04-21 18:22:26 -04:00
|
|
|
ListSeparator::Space => v[0].inspect(span)?,
|
2020-04-20 02:55:55 -04:00
|
|
|
ListSeparator::Comma => format!("({},)", v[0].inspect(span)?),
|
|
|
|
},
|
|
|
|
Brackets::Bracketed => match sep {
|
|
|
|
ListSeparator::Space => format!("[{}]", v[0].inspect(span)?),
|
|
|
|
ListSeparator::Comma => format!("[{},]", v[0].inspect(span)?),
|
|
|
|
},
|
|
|
|
},
|
2020-04-20 03:07:02 -04:00
|
|
|
Self::List(vals, sep, brackets) => match brackets {
|
2020-04-21 18:22:26 -04:00
|
|
|
Brackets::None => vals
|
|
|
|
.iter()
|
|
|
|
.map(|x| x.inspect(span))
|
|
|
|
.collect::<SassResult<Vec<String>>>()?
|
|
|
|
.join(sep.as_str()),
|
2020-04-20 03:07:02 -04:00
|
|
|
Brackets::Bracketed => format!(
|
|
|
|
"[{}]",
|
|
|
|
vals.iter()
|
|
|
|
.map(|x| x.inspect(span))
|
|
|
|
.collect::<SassResult<Vec<String>>>()?
|
|
|
|
.join(sep.as_str()),
|
|
|
|
),
|
|
|
|
},
|
2020-04-06 00:27:09 -04:00
|
|
|
Value::Function(f) => format!("get-function(\"{}\")", f.name()),
|
2020-04-18 13:44:11 -04:00
|
|
|
Value::Null => "null".to_string(),
|
2020-04-18 20:11:49 -04:00
|
|
|
Value::Map(map) => format!(
|
|
|
|
"({})",
|
|
|
|
map.iter()
|
|
|
|
.map(|(k, v)| Ok(format!(
|
|
|
|
"{}: {}",
|
|
|
|
k.to_css_string(span)?,
|
|
|
|
v.to_css_string(span)?
|
|
|
|
)))
|
|
|
|
.collect::<SassResult<Vec<String>>>()?
|
|
|
|
.join(", ")
|
|
|
|
),
|
2020-04-20 03:07:02 -04:00
|
|
|
Value::Paren(v) => format!("({})", v.inspect(span)?),
|
2020-04-12 19:37:12 -04:00
|
|
|
v => v.to_css_string(span)?,
|
|
|
|
})
|
2020-04-06 00:27:09 -04:00
|
|
|
}
|
|
|
|
|
2020-04-12 19:37:12 -04:00
|
|
|
pub fn equals(self, other: Value, span: Span) -> SassResult<bool> {
|
|
|
|
Ok(match self.eval(span)?.node {
|
2020-03-18 17:23:38 -04:00
|
|
|
Self::Ident(s1, ..) => match other {
|
|
|
|
Self::Ident(s2, ..) => s1 == s2,
|
|
|
|
_ => false,
|
2020-03-18 20:11:14 -04:00
|
|
|
},
|
2020-04-03 14:34:59 -04:00
|
|
|
Self::Dimension(n, unit) => match other {
|
|
|
|
Self::Dimension(n2, unit2) => {
|
|
|
|
if !unit.comparable(&unit2) {
|
|
|
|
false
|
|
|
|
} else if unit == unit2 {
|
|
|
|
n == n2
|
|
|
|
} else if unit == Unit::None || unit2 == Unit::None {
|
|
|
|
false
|
|
|
|
} else {
|
|
|
|
n == (n2
|
|
|
|
* UNIT_CONVERSION_TABLE[&unit.to_string()][&unit2.to_string()].clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => false,
|
|
|
|
},
|
2020-04-12 19:37:12 -04:00
|
|
|
s => s == other.eval(span)?.node,
|
2020-03-18 17:23:38 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-04-12 19:37:12 -04:00
|
|
|
pub fn unary_op_plus(self, span: Span) -> SassResult<Self> {
|
|
|
|
Ok(match self.eval(span)?.node {
|
2020-03-21 12:14:02 -04:00
|
|
|
v @ Value::Dimension(..) => v,
|
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
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-04-12 19:37:12 -04:00
|
|
|
pub fn eval(self, span: Span) -> SassResult<Spanned<Self>> {
|
|
|
|
Ok(match self {
|
2020-02-03 07:56:21 -05:00
|
|
|
Self::BinaryOp(lhs, op, rhs) => match op {
|
2020-04-12 19:37:12 -04:00
|
|
|
Op::Plus => lhs.add(*rhs, span)?,
|
|
|
|
Op::Minus => lhs.sub(*rhs, span)?,
|
|
|
|
Op::Equal => Self::bool(lhs.equals(*rhs, span)?),
|
|
|
|
Op::NotEqual => Self::bool(!lhs.equals(*rhs, span)?),
|
|
|
|
Op::Mul => lhs.mul(*rhs, span)?,
|
|
|
|
Op::Div => lhs.div(*rhs, span)?,
|
|
|
|
Op::Rem => lhs.rem(*rhs, span)?,
|
|
|
|
Op::GreaterThan => return lhs.cmp(*rhs, op, span),
|
|
|
|
Op::GreaterThanEqual => return lhs.cmp(*rhs, op, span),
|
|
|
|
Op::LessThan => return lhs.cmp(*rhs, op, span),
|
|
|
|
Op::LessThanEqual => return lhs.cmp(*rhs, op, span),
|
2020-04-01 17:37:07 -04:00
|
|
|
Op::Not => unreachable!(),
|
2020-04-12 19:37:12 -04:00
|
|
|
Op::And => {
|
2020-04-21 18:22:26 -04:00
|
|
|
if lhs.is_true(span)? {
|
2020-04-12 19:37:12 -04:00
|
|
|
rhs.eval(span)?.node
|
|
|
|
} else {
|
|
|
|
lhs.eval(span)?.node
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Op::Or => {
|
|
|
|
if lhs.is_true(span)? {
|
|
|
|
lhs.eval(span)?.node
|
|
|
|
} else {
|
|
|
|
rhs.eval(span)?.node
|
|
|
|
}
|
|
|
|
}
|
2020-02-07 00:10:43 -05:00
|
|
|
},
|
2020-04-12 19:37:12 -04:00
|
|
|
Self::Paren(v) => v.eval(span)?.node,
|
2020-03-21 12:14:02 -04:00
|
|
|
Self::UnaryOp(op, val) => match op {
|
2020-04-12 19:37:12 -04:00
|
|
|
Op::Plus => val.unary_op_plus(span)?,
|
|
|
|
Op::Minus => val.neg(span)?,
|
|
|
|
Op::Not => Self::bool(!val.eval(span)?.is_true(span)?),
|
2020-03-21 12:14:02 -04:00
|
|
|
_ => unreachable!(),
|
|
|
|
},
|
2020-04-12 19:37:12 -04:00
|
|
|
_ => self,
|
2020-02-03 07:56:21 -05:00
|
|
|
}
|
2020-04-12 19:37:12 -04:00
|
|
|
.span(span))
|
2020-02-03 07:56:21 -05:00
|
|
|
}
|
2020-03-30 10:42:13 -04:00
|
|
|
|
2020-04-12 19:37:12 -04:00
|
|
|
pub fn cmp(self, mut other: Self, op: Op, span: Span) -> SassResult<Spanned<Value>> {
|
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.precedence();
|
|
|
|
let ordering = match self {
|
|
|
|
Self::Dimension(num, unit) => match &other {
|
2020-03-30 10:42:13 -04:00
|
|
|
Self::Dimension(num2, unit2) => {
|
2020-04-21 18:22:26 -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-30 10:42:13 -04:00
|
|
|
}
|
2020-04-21 18:22:26 -04:00
|
|
|
if &unit == unit2 || unit == Unit::None || unit2 == &Unit::None {
|
2020-03-30 10:42:13 -04:00
|
|
|
num.cmp(num2)
|
|
|
|
} else {
|
2020-03-30 15:43:15 -04:00
|
|
|
num.cmp(
|
|
|
|
&(num2.clone()
|
|
|
|
* UNIT_CONVERSION_TABLE[&unit.to_string()][&unit2.to_string()]
|
|
|
|
.clone()),
|
|
|
|
)
|
2020-03-30 10:42:13 -04:00
|
|
|
}
|
|
|
|
}
|
2020-04-05 17:34:30 -04:00
|
|
|
Self::BinaryOp(..) => todo!(),
|
2020-04-12 19:37:12 -04:00
|
|
|
v => {
|
|
|
|
return Err((
|
|
|
|
format!(
|
|
|
|
"Undefined operation \"{} {} {}\".",
|
|
|
|
v.to_css_string(span)?,
|
|
|
|
op,
|
|
|
|
other.to_css_string(span)?
|
|
|
|
),
|
|
|
|
span,
|
|
|
|
)
|
|
|
|
.into())
|
|
|
|
}
|
2020-04-05 17:34:30 -04:00
|
|
|
},
|
|
|
|
Self::BinaryOp(left, op2, right) => {
|
|
|
|
return if op2.precedence() >= precedence {
|
2020-04-12 19:37:12 -04:00
|
|
|
Self::BinaryOp(left, op2, right)
|
|
|
|
.eval(span)?
|
|
|
|
.node
|
|
|
|
.cmp(other, op, span)
|
2020-04-05 17:34:30 -04:00
|
|
|
} else {
|
|
|
|
Self::BinaryOp(
|
|
|
|
left,
|
|
|
|
op2,
|
2020-04-12 19:37:12 -04:00
|
|
|
Box::new(Self::BinaryOp(right, op, Box::new(other)).eval(span)?.node),
|
2020-03-30 15:43:15 -04:00
|
|
|
)
|
2020-04-12 19:37:12 -04:00
|
|
|
.eval(span)
|
2020-03-30 15:43:15 -04:00
|
|
|
}
|
2020-04-05 17:34:30 -04:00
|
|
|
}
|
2020-04-12 19:37:12 -04:00
|
|
|
Self::UnaryOp(..) | Self::Paren(..) => {
|
|
|
|
return self.eval(span)?.node.cmp(other, op, span)
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
return Err((
|
|
|
|
format!(
|
|
|
|
"Undefined operation \"{} {} {}\".",
|
|
|
|
self.to_css_string(span)?,
|
|
|
|
op,
|
|
|
|
other.to_css_string(span)?
|
|
|
|
),
|
|
|
|
span,
|
|
|
|
)
|
|
|
|
.into())
|
|
|
|
}
|
2020-04-05 17:34:30 -04:00
|
|
|
};
|
2020-04-12 19:37:12 -04:00
|
|
|
Ok(match op {
|
2020-04-05 17:34:30 -04:00
|
|
|
Op::GreaterThan => match ordering {
|
2020-04-12 19:37:12 -04:00
|
|
|
Ordering::Greater => Self::True,
|
|
|
|
Ordering::Less | Ordering::Equal => Self::False,
|
2020-04-05 17:34:30 -04:00
|
|
|
},
|
|
|
|
Op::GreaterThanEqual => match ordering {
|
2020-04-12 19:37:12 -04:00
|
|
|
Ordering::Greater | Ordering::Equal => Self::True,
|
|
|
|
Ordering::Less => Self::False,
|
2020-04-05 17:34:30 -04:00
|
|
|
},
|
|
|
|
Op::LessThan => match ordering {
|
2020-04-12 19:37:12 -04:00
|
|
|
Ordering::Less => Self::True,
|
|
|
|
Ordering::Greater | Ordering::Equal => Self::False,
|
2020-04-05 17:34:30 -04:00
|
|
|
},
|
|
|
|
Op::LessThanEqual => match ordering {
|
2020-04-12 19:37:12 -04:00
|
|
|
Ordering::Less | Ordering::Equal => Self::True,
|
|
|
|
Ordering::Greater => Self::False,
|
2020-04-05 17:34:30 -04:00
|
|
|
},
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
2020-04-12 19:37:12 -04:00
|
|
|
.span(span))
|
2020-03-30 10:42:13 -04:00
|
|
|
}
|
2020-01-25 09:58:53 -05:00
|
|
|
}
|