grass/src/value/mod.rs

377 lines
13 KiB
Rust
Raw Normal View History

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;
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-03-30 15:43:15 -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
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),
List(Vec<Value>, ListSeparator, Brackets),
2020-01-25 09:58:53 -05:00
Color(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>>),
/// Returned by `get-function()`
Function(SassFunction),
2020-01-25 09:58:53 -05:00
}
2020-04-12 19:37:12 -04:00
impl Value {
pub fn is_null(&self) -> bool {
2020-01-25 09:58:53 -05:00
match self {
2020-04-12 19:37:12 -04:00
&Value::Null => true,
Value::Ident(i, QuoteKind::None) if i.is_empty() => true,
_ => false,
}
}
pub fn to_css_string(&self, span: Span) -> SassResult<String> {
Ok(match self {
Self::Important => format!("!important"),
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-12 19:37:12 -04:00
return Err((
format!("Error: {}{} 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-12 19:37:12 -04:00
Self::Map(map) => format!(
2020-03-30 15:43:15 -04:00
"({})",
map.iter()
2020-04-12 19:37:12 -04:00
.map(|(k, v)| Ok(format!(
"{}: {}",
k.to_css_string(span)?,
v.to_css_string(span)?
)))
.collect::<SassResult<Vec<String>>>()?
2020-03-30 15:43:15 -04:00
.join(", ")
),
2020-04-12 19:37:12 -04:00
Self::Function(func) => format!("get-function(\"{}\")", func.name()),
Self::List(vals, sep, brackets) => match brackets {
2020-04-12 19:37:12 -04:00
Brackets::None => format!(
"{}",
vals.iter()
2020-03-31 00:27:00 -04:00
.filter(|x| !x.is_null())
2020-04-12 19:37:12 -04:00
.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!(
"[{}]",
vals.iter()
2020-03-31 00:27:00 -04:00
.filter(|x| !x.is_null())
2020-04-12 19:37:12 -04:00
.map(|x| x.to_css_string(span))
.collect::<SassResult<Vec<String>>>()?
.join(sep.as_str()),
),
},
2020-04-12 19:37:12 -04:00
Self::Color(c) => format!("{}", c),
Self::UnaryOp(..) | Self::BinaryOp(..) => {
format!("{}", self.clone().eval(span)?.to_css_string(span)?)
}
Self::Paren(val) => format!("{}", val.to_css_string(span)?),
Self::Ident(val, kind) => {
if kind == &QuoteKind::None {
2020-04-12 19:37:12 -04:00
return Ok(val.clone());
}
let has_single_quotes = val.contains(|x| x == '\'');
let has_double_quotes = val.contains(|x| x == '"');
if has_single_quotes && !has_double_quotes {
2020-04-12 19:37:12 -04:00
format!("\"{}\"", val)
} else if !has_single_quotes && has_double_quotes {
2020-04-12 19:37:12 -04:00
format!("'{}'", val)
2020-02-28 18:32:11 -05:00
} else if !has_single_quotes && !has_double_quotes {
2020-04-12 19:37:12 -04:00
format!("\"{}\"", val)
} else {
let quote_char = match kind {
QuoteKind::Double => '"',
QuoteKind::Single => '\'',
_ => unreachable!(),
};
2020-04-12 19:37:12 -04:00
let mut buf = String::with_capacity(val.len() + 2);
buf.push(quote_char);
for c in val.chars() {
match c {
'"' | '\'' if c == quote_char => {
2020-04-12 19:37:12 -04:00
buf.push('\\');
buf.push(quote_char);
}
2020-04-12 19:37:12 -04:00
v => buf.push(v),
}
}
2020-04-12 19:37:12 -04:00
buf.push(quote_char);
buf
}
}
2020-04-12 19:37:12 -04:00
Self::True => "true".to_string(),
Self::False => "false".to_string(),
Self::Null => "null".to_string(),
Self::ArgList(args) => format!(
2020-04-02 13:33:26 -04:00
"{}",
args.iter()
.filter(|x| !x.is_null())
2020-04-12 19:37:12 -04:00
.map(|a| Ok(a.node.to_css_string(span)?))
.collect::<SassResult<Vec<String>>>()?
2020-04-02 13:33:26 -04:00
.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),
Self::BinaryOp(..) | Self::Paren(..) | Self::UnaryOp(..) => {
2020-04-12 19:37:12 -04:00
self.clone().eval(span)?.is_true(span)
}
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
}
pub fn unquote(self) -> Self {
match self {
Self::Ident(s1, _) => Self::Ident(s1, QuoteKind::None),
v => v,
}
2020-01-25 09:58:53 -05:00
}
2020-04-12 19:37:12 -04:00
pub fn span(self, span: Span) -> Spanned<Self> {
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"),
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
}
}
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(),
},
Value::Function(f) => format!("get-function(\"{}\")", f.name()),
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 => {
if lhs.clone().is_true(span)? {
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-05 17:34:30 -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-05 17:34:30 -04:00
if &unit == unit2 {
2020-03-30 10:42:13 -04:00
num.cmp(num2)
2020-04-05 17:34:30 -04:00
} else if unit == Unit::None {
2020-03-30 10:42:13 -04:00
num.cmp(num2)
} else if unit2 == &Unit::None {
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
}