grass/src/value/parse.rs

623 lines
23 KiB
Rust
Raw Normal View History

2020-02-08 16:17:58 -05:00
use std::convert::TryFrom;
use std::iter::{Iterator, Peekable};
2020-04-01 15:32:52 -04:00
use std::mem;
2020-02-08 16:17:58 -05:00
use num_bigint::BigInt;
2020-02-08 20:20:03 -05:00
use num_rational::BigRational;
use num_traits::pow;
2020-02-08 16:17:58 -05:00
use crate::args::eat_call_args;
use crate::builtin::GLOBAL_FUNCTIONS;
use crate::color::Color;
2020-03-29 13:28:17 -04:00
use crate::common::{Brackets, ListSeparator, Op, QuoteKind};
2020-02-16 10:54:25 -05:00
use crate::error::SassResult;
use crate::scope::Scope;
2020-03-01 12:03:14 -05:00
use crate::selector::Selector;
use crate::unit::Unit;
2020-03-01 12:03:14 -05:00
use crate::utils::{
devour_whitespace, eat_comment, eat_ident, eat_ident_no_interpolation, eat_number,
2020-04-01 15:32:52 -04:00
parse_interpolation, parse_quoted_string, read_until_char, read_until_closing_paren,
read_until_closing_square_brace, read_until_newline, IsWhitespace,
2020-03-01 12:03:14 -05:00
};
2020-02-08 16:17:58 -05:00
use crate::value::Value;
2020-03-29 13:28:17 -04:00
use crate::Token;
2020-02-08 16:17:58 -05:00
2020-03-30 15:43:15 -04:00
use super::map::SassMap;
2020-02-08 18:43:18 -05:00
use super::number::Number;
2020-03-29 13:28:17 -04:00
fn parse_hex<I: Iterator<Item = Token>>(
toks: &mut Peekable<I>,
scope: &Scope,
super_selector: &Selector,
) -> SassResult<Value> {
let mut s = String::with_capacity(8);
2020-04-02 02:08:14 -04:00
if toks
.peek()
.ok_or("Expected identifier.")?
.kind
.is_ascii_digit()
{
2020-03-29 13:28:17 -04:00
while let Some(c) = toks.peek() {
if !c.kind.is_ascii_hexdigit() || s.len() == 8 {
break;
}
s.push(toks.next().unwrap().kind);
}
} else {
let i = eat_ident(toks, scope, super_selector)?;
if i.chars().all(|c| c.is_ascii_hexdigit()) {
s = i;
} else {
return Ok(Value::Ident(format!("#{}", i), QuoteKind::None));
}
}
2020-02-08 16:44:42 -05:00
match s.len() {
3 => {
2020-03-29 13:28:17 -04:00
let v = match u16::from_str_radix(&s, 16) {
Ok(a) => a,
2020-03-29 13:28:17 -04:00
Err(_) => return Ok(Value::Ident(format!("#{}", s), QuoteKind::None)),
};
let red = (((v & 0xf00) >> 8) * 0x11) as u8;
let green = (((v & 0x0f0) >> 4) * 0x11) as u8;
let blue = ((v & 0x00f) * 0x11) as u8;
2020-03-29 13:28:17 -04:00
Ok(Value::Color(Color::new(
red,
green,
blue,
1,
format!("#{}", s),
)))
2020-02-08 16:44:42 -05:00
}
4 => {
2020-03-29 13:28:17 -04:00
let v = match u16::from_str_radix(&s, 16) {
Ok(a) => a,
2020-03-29 13:28:17 -04:00
Err(_) => return Ok(Value::Ident(format!("#{}", s), QuoteKind::None)),
};
let red = (((v & 0xf000) >> 12) * 0x11) as u8;
let green = (((v & 0x0f00) >> 8) * 0x11) as u8;
let blue = (((v & 0x00f0) >> 4) * 0x11) as u8;
let alpha = ((v & 0x000f) * 0x11) as u8;
2020-03-29 13:28:17 -04:00
Ok(Value::Color(Color::new(
red,
green,
blue,
alpha,
format!("#{}", s),
)))
2020-02-08 16:44:42 -05:00
}
6 => {
2020-03-29 13:28:17 -04:00
let v = match u32::from_str_radix(&s, 16) {
Ok(a) => a,
2020-03-29 13:28:17 -04:00
Err(_) => return Ok(Value::Ident(format!("#{}", s), QuoteKind::None)),
};
let red = ((v & 0x00ff_0000) >> 16) as u8;
let green = ((v & 0x0000_ff00) >> 8) as u8;
let blue = (v & 0x0000_00ff) as u8;
2020-03-29 13:28:17 -04:00
Ok(Value::Color(Color::new(
red,
green,
blue,
1,
format!("#{}", s),
)))
2020-02-08 16:44:42 -05:00
}
8 => {
2020-03-29 13:28:17 -04:00
let v = match u32::from_str_radix(&s, 16) {
Ok(a) => a,
2020-03-29 13:28:17 -04:00
Err(_) => return Ok(Value::Ident(format!("#{}", s), QuoteKind::None)),
};
let red = ((v & 0xff00_0000) >> 24) as u8;
let green = ((v & 0x00ff_0000) >> 16) as u8;
let blue = ((v & 0x0000_ff00) >> 8) as u8;
let alpha = (v & 0x0000_00ff) as u8;
2020-03-29 13:28:17 -04:00
Ok(Value::Color(Color::new(
red,
green,
blue,
alpha,
format!("#{}", s),
)))
2020-02-08 16:44:42 -05:00
}
2020-03-29 13:28:17 -04:00
_ => Err("Expected hex digit.".into()),
2020-02-08 16:44:42 -05:00
}
}
2020-04-01 15:32:52 -04:00
#[derive(Clone, Debug, Eq, PartialEq)]
enum IntermediateValue {
Value(Value),
Op(Op),
Bracketed(Vec<Token>),
Paren(Vec<Token>),
Comma,
Whitespace,
}
impl IsWhitespace for IntermediateValue {
fn is_whitespace(&self) -> bool {
if self == &IntermediateValue::Whitespace {
return true;
}
false
}
}
fn eat_op<I: Iterator<Item = IntermediateValue>>(
iter: &mut Peekable<I>,
scope: &Scope,
super_selector: &Selector,
op: Op,
space_separated: &mut Vec<Value>,
) -> SassResult<()> {
match op {
2020-04-01 17:37:07 -04:00
Op::Not => {
devour_whitespace(iter);
let right = single_value(iter, scope, super_selector)?;
space_separated.push(Value::UnaryOp(op, Box::new(right)));
}
2020-04-01 15:32:52 -04:00
Op::Plus => {
if let Some(left) = space_separated.pop() {
devour_whitespace(iter);
let right = single_value(iter, scope, super_selector)?;
space_separated.push(Value::BinaryOp(Box::new(left), op, Box::new(right)));
} else {
devour_whitespace(iter);
let right = single_value(iter, scope, super_selector)?;
space_separated.push(Value::UnaryOp(op, Box::new(right)));
}
}
Op::Minus => {
if devour_whitespace(iter) {
let right = single_value(iter, scope, super_selector)?;
if let Some(left) = space_separated.pop() {
space_separated.push(Value::BinaryOp(Box::new(left), op, Box::new(right)));
} else {
space_separated.push(Value::UnaryOp(op, Box::new(right)));
}
} else {
let right = single_value(iter, scope, super_selector)?;
space_separated.push(Value::UnaryOp(op, Box::new(right)));
}
}
2020-04-01 17:37:07 -04:00
Op::And | Op::Or => {
devour_whitespace(iter);
if iter.peek().is_none() {
space_separated.push(Value::Ident(op.to_string(), QuoteKind::None));
} else if let Some(left) = space_separated.pop() {
devour_whitespace(iter);
let right = single_value(iter, scope, super_selector)?;
space_separated.push(Value::BinaryOp(Box::new(left), op, Box::new(right)));
} else {
return Err("Expected expression.".into());
}
}
2020-04-01 15:32:52 -04:00
_ => {
if let Some(left) = space_separated.pop() {
devour_whitespace(iter);
let right = single_value(iter, scope, super_selector)?;
space_separated.push(Value::BinaryOp(Box::new(left), op, Box::new(right)));
} else {
return Err("Expected expression.".into());
}
}
}
Ok(())
}
fn single_value<I: Iterator<Item = IntermediateValue>>(
iter: &mut Peekable<I>,
scope: &Scope,
super_selector: &Selector,
) -> SassResult<Value> {
2020-04-01 17:37:07 -04:00
Ok(match iter.next().ok_or("Expected expression.")? {
2020-04-01 15:32:52 -04:00
IntermediateValue::Value(v) => v,
IntermediateValue::Op(op) => match op {
Op::Minus => {
devour_whitespace(iter);
(-single_value(iter, scope, super_selector)?)?
}
2020-04-01 17:37:07 -04:00
Op::Not => {
devour_whitespace(iter);
Value::UnaryOp(op, Box::new(single_value(iter, scope, super_selector)?))
}
2020-04-01 15:32:52 -04:00
_ => todo!(),
},
IntermediateValue::Whitespace => unreachable!(),
IntermediateValue::Comma => return Err("Expected expression.".into()),
IntermediateValue::Bracketed(t) => {
match Value::from_tokens(&mut t.into_iter().peekable(), scope, super_selector)? {
Value::List(v, sep, Brackets::None) => Value::List(v, sep, Brackets::Bracketed),
v => Value::List(vec![v], ListSeparator::Space, Brackets::Bracketed),
}
}
IntermediateValue::Paren(t) => {
let inner = Value::from_tokens(&mut t.into_iter().peekable(), scope, super_selector)?;
Value::Paren(Box::new(inner))
}
})
}
2020-02-08 16:17:58 -05:00
impl Value {
pub fn from_tokens<I: Iterator<Item = Token>>(
toks: &mut Peekable<I>,
scope: &Scope,
2020-03-01 12:03:14 -05:00
super_selector: &Selector,
2020-02-16 10:54:25 -05:00
) -> SassResult<Self> {
2020-04-01 15:32:52 -04:00
let mut intermediate_values = Vec::new();
while toks.peek().is_some() {
intermediate_values.push(Self::parse_intermediate_value(toks, scope, super_selector)?);
}
let mut space_separated = Vec::new();
let mut comma_separated = Vec::new();
let mut iter = intermediate_values.into_iter().peekable();
while let Some(val) = iter.next() {
match val {
IntermediateValue::Value(v) => space_separated.push(v),
IntermediateValue::Op(op) => {
eat_op(&mut iter, scope, super_selector, op, &mut space_separated)?;
}
IntermediateValue::Whitespace => continue,
IntermediateValue::Comma => {
if space_separated.len() == 1 {
comma_separated.push(space_separated.pop().unwrap());
} else {
comma_separated.push(Value::List(
mem::take(&mut space_separated),
ListSeparator::Space,
2020-03-24 00:17:18 -04:00
Brackets::None,
));
}
}
2020-04-01 15:32:52 -04:00
IntermediateValue::Bracketed(t) => space_separated.push(match Value::from_tokens(
&mut t.into_iter().peekable(),
scope,
super_selector,
)? {
Value::List(v, sep, Brackets::None) => Value::List(v, sep, Brackets::Bracketed),
v => Value::List(vec![v], ListSeparator::Space, Brackets::Bracketed),
}),
IntermediateValue::Paren(t) => {
if t.is_empty() {
space_separated.push(Value::List(
Vec::new(),
2020-03-29 22:17:56 -04:00
ListSeparator::Space,
Brackets::None,
2020-04-01 15:32:52 -04:00
));
continue;
2020-03-29 22:17:56 -04:00
}
2020-04-01 15:32:52 -04:00
let paren_toks = &mut t.into_iter().peekable();
let mut map = SassMap::new();
let key = Value::from_tokens(
&mut read_until_char(paren_toks, ':').into_iter().peekable(),
scope,
super_selector,
)?;
if paren_toks.peek().is_none() {
space_separated.push(Value::Paren(Box::new(key)));
continue;
2020-03-29 13:28:17 -04:00
}
2020-04-01 15:32:52 -04:00
let val = Self::from_tokens(
&mut read_until_char(paren_toks, ',').into_iter().peekable(),
scope,
super_selector,
)?;
map.insert(key, val);
if paren_toks.peek().is_none() {
space_separated.push(Value::Map(map));
continue;
2020-03-29 13:28:17 -04:00
}
2020-04-01 15:32:52 -04:00
loop {
let key = Value::from_tokens(
&mut read_until_char(paren_toks, ':').into_iter().peekable(),
scope,
super_selector,
)?;
devour_whitespace(paren_toks);
let val = Self::from_tokens(
&mut read_until_char(paren_toks, ',').into_iter().peekable(),
scope,
super_selector,
)?;
devour_whitespace(paren_toks);
map.insert(key, val);
if paren_toks.peek().is_none() {
break;
}
}
space_separated.push(Value::Map(map))
2020-03-19 21:40:36 -04:00
}
2020-02-08 16:17:58 -05:00
}
}
2020-04-01 15:32:52 -04:00
2020-04-01 18:09:58 -04:00
Ok(if !comma_separated.is_empty() {
2020-04-01 15:32:52 -04:00
if space_separated.len() == 1 {
comma_separated.push(space_separated.pop().unwrap());
} else if !space_separated.is_empty() {
comma_separated.push(Value::List(
space_separated,
ListSeparator::Space,
Brackets::None,
));
}
Value::List(comma_separated, ListSeparator::Comma, Brackets::None)
} else if space_separated.len() == 1 {
space_separated.pop().unwrap()
} else {
Value::List(space_separated, ListSeparator::Space, Brackets::None)
})
2020-02-08 16:17:58 -05:00
}
fn ident<I: Iterator<Item = Token>>(
toks: &mut Peekable<I>,
scope: &Scope,
super_selector: &Selector,
) -> SassResult<IntermediateValue> {
let mut s = eat_ident(toks, scope, super_selector)?;
match toks.peek() {
Some(Token { kind: '(', .. }) => {
toks.next();
let func = match scope.get_fn(&s) {
Ok(f) => f,
Err(_) => match GLOBAL_FUNCTIONS.get(&s) {
Some(f) => {
return Ok(IntermediateValue::Value(f(
&mut eat_call_args(toks, scope, super_selector)?,
scope,
)?))
}
None => {
s.push('(');
let mut unclosed_parens = 0;
while let Some(t) = toks.next() {
match &t.kind {
'(' => {
unclosed_parens += 1;
}
'#' if toks.next().unwrap().kind == '{' => s.push_str(
&parse_interpolation(toks, scope, super_selector)?
.to_string(),
),
'$' => s.push_str(
&scope
.get_var(&eat_ident(toks, scope, super_selector)?)?
.to_string(),
),
')' => {
if unclosed_parens <= 1 {
s.push(')');
break;
} else {
unclosed_parens -= 1;
}
}
_ => {}
}
s.push_str(&t.kind.to_string());
}
return Ok(IntermediateValue::Value(Value::Ident(s, QuoteKind::None)));
}
},
};
Ok(IntermediateValue::Value(
func.clone()
.args(&mut eat_call_args(toks, scope, super_selector)?)?
.call(super_selector, func.body())?,
))
}
_ => {
if let Ok(c) = crate::color::ColorName::try_from(s.as_ref()) {
Ok(IntermediateValue::Value(Value::Color(c.into_color(s))))
} else {
match s.to_ascii_lowercase().as_str() {
"true" => Ok(IntermediateValue::Value(Value::True)),
"false" => Ok(IntermediateValue::Value(Value::False)),
"null" => Ok(IntermediateValue::Value(Value::Null)),
"not" => Ok(IntermediateValue::Op(Op::Not)),
"and" => Ok(IntermediateValue::Op(Op::And)),
"or" => Ok(IntermediateValue::Op(Op::Or)),
_ => Ok(IntermediateValue::Value(Value::Ident(s, QuoteKind::None))),
}
}
}
}
}
2020-04-01 15:32:52 -04:00
fn parse_intermediate_value<I: Iterator<Item = Token>>(
2020-02-08 16:17:58 -05:00
toks: &mut Peekable<I>,
scope: &Scope,
2020-03-01 12:03:14 -05:00
super_selector: &Selector,
2020-04-01 15:32:52 -04:00
) -> SassResult<IntermediateValue> {
if devour_whitespace(toks) {
return Ok(IntermediateValue::Whitespace);
}
let kind = match toks.peek() {
Some(v) => v.kind,
None => panic!("unexpected eof"),
2020-02-08 16:17:58 -05:00
};
match kind {
2020-04-01 15:32:52 -04:00
',' => {
toks.next();
Ok(IntermediateValue::Comma)
}
2020-03-29 13:28:17 -04:00
'0'..='9' | '.' => {
let val = eat_number(toks)?;
2020-02-08 16:17:58 -05:00
let unit = if let Some(tok) = toks.peek() {
2020-03-29 13:28:17 -04:00
match tok.kind {
'a'..='z' | 'A'..='Z' | '_' => {
Unit::from(&eat_ident(toks, scope, super_selector)?)
2020-02-08 16:17:58 -05:00
}
2020-03-29 13:28:17 -04:00
'%' => {
2020-02-08 16:17:58 -05:00
toks.next();
Unit::Percent
}
_ => Unit::None,
}
} else {
Unit::None
};
2020-02-09 19:07:44 -05:00
let n = if let Ok(v) = val.parse::<BigRational>() {
// the number is an integer!
2020-02-09 19:07:44 -05:00
v
// the number is floating point
} else {
let mut num = String::new();
let mut chars = val.chars();
let mut num_dec = 0;
while let Some(c) = chars.next() {
if c == '.' {
break;
}
2020-02-09 19:07:44 -05:00
num.push(c);
}
for c in chars {
num_dec += 1;
num.push(c);
}
2020-02-09 19:07:44 -05:00
BigRational::new(num.parse().unwrap(), pow(BigInt::from(10), num_dec))
};
2020-04-01 15:32:52 -04:00
Ok(IntermediateValue::Value(Value::Dimension(
Number::new(n),
unit,
)))
2020-02-08 16:17:58 -05:00
}
2020-03-29 13:28:17 -04:00
'(' => {
toks.next();
2020-04-01 15:32:52 -04:00
let mut inner = read_until_closing_paren(toks);
// todo: the above shouldn't eat the closing paren
2020-04-01 18:09:58 -04:00
if !inner.is_empty() && inner.pop().unwrap().kind != ')' {
2020-04-01 15:32:52 -04:00
return Err("expected \")\".".into());
2020-03-20 20:01:57 -04:00
}
2020-04-01 15:32:52 -04:00
Ok(IntermediateValue::Paren(inner))
2020-02-08 16:17:58 -05:00
}
2020-03-29 13:28:17 -04:00
'&' => {
toks.next();
2020-04-01 15:32:52 -04:00
Ok(IntermediateValue::Value(Value::Ident(
super_selector.to_string(),
QuoteKind::None,
)))
2020-03-01 12:03:14 -05:00
}
2020-03-29 13:28:17 -04:00
'#' => {
if let Ok(s) = eat_ident(toks, scope, super_selector) {
2020-04-01 15:32:52 -04:00
Ok(IntermediateValue::Value(Value::Ident(s, QuoteKind::None)))
2020-03-29 13:28:17 -04:00
} else {
2020-04-01 15:32:52 -04:00
Ok(IntermediateValue::Value(parse_hex(
toks,
scope,
super_selector,
)?))
2020-03-29 13:28:17 -04:00
}
2020-02-08 16:17:58 -05:00
}
2020-03-30 01:24:50 -04:00
_ if kind.is_ascii_alphabetic()
|| kind == '_'
|| kind == '\\'
|| (!kind.is_ascii() && !kind.is_control()) =>
{
Self::ident(toks, scope, super_selector)
2020-02-08 16:17:58 -05:00
}
2020-03-29 13:28:17 -04:00
q @ '"' | q @ '\'' => {
toks.next();
2020-04-01 15:32:52 -04:00
Ok(IntermediateValue::Value(parse_quoted_string(
toks,
scope,
q,
super_selector,
)?))
2020-03-01 12:03:14 -05:00
}
2020-03-29 13:28:17 -04:00
'[' => {
toks.next();
2020-04-01 15:32:52 -04:00
let mut inner = read_until_closing_square_brace(toks);
inner.pop();
Ok(IntermediateValue::Bracketed(inner))
}
2020-03-29 13:28:17 -04:00
'$' => {
toks.next();
2020-04-01 15:32:52 -04:00
Ok(IntermediateValue::Value(
scope.get_var(&eat_ident_no_interpolation(toks)?)?,
))
2020-03-22 13:45:41 -04:00
}
2020-03-29 13:28:17 -04:00
'@' => Err("expected \";\".".into()),
'+' => {
toks.next();
2020-04-01 15:32:52 -04:00
Ok(IntermediateValue::Op(Op::Plus))
2020-03-21 12:14:02 -04:00
}
2020-03-29 13:28:17 -04:00
'-' => {
toks.next();
2020-04-01 15:32:52 -04:00
Ok(IntermediateValue::Op(Op::Minus))
}
'*' => {
toks.next();
Ok(IntermediateValue::Op(Op::Mul))
}
'%' => {
toks.next();
Ok(IntermediateValue::Op(Op::Rem))
}
q @ '>' | q @ '<' => {
toks.next();
Ok(IntermediateValue::Op(if toks.peek().unwrap().kind == '=' {
toks.next();
match q {
'>' => Op::GreaterThanEqual,
'<' => Op::LessThanEqual,
_ => unreachable!(),
}
} else {
match q {
'>' => Op::GreaterThan,
'<' => Op::LessThan,
_ => unreachable!(),
}
}))
}
'=' => {
toks.next();
if toks.next().unwrap().kind == '=' {
Ok(IntermediateValue::Op(Op::Equal))
} else {
Err("expected \"=\".".into())
}
2020-03-21 12:14:02 -04:00
}
2020-03-29 13:28:17 -04:00
'!' => {
toks.next();
2020-04-01 15:32:52 -04:00
if toks.peek().is_some() && toks.peek().unwrap().kind == '=' {
toks.next();
return Ok(IntermediateValue::Op(Op::NotEqual));
}
devour_whitespace(toks);
2020-03-29 13:28:17 -04:00
let v = eat_ident(toks, scope, super_selector)?;
if v.to_ascii_lowercase().as_str() == "important" {
2020-04-01 15:32:52 -04:00
Ok(IntermediateValue::Value(Value::Important))
2020-03-29 13:28:17 -04:00
} else {
Err("Expected \"important\".".into())
}
}
'/' => {
toks.next();
if '*' == toks.peek().unwrap().kind {
toks.next();
eat_comment(toks, &Scope::new(), &Selector::new())?;
2020-04-01 15:32:52 -04:00
Ok(IntermediateValue::Whitespace)
2020-03-29 13:28:17 -04:00
} else if '/' == toks.peek().unwrap().kind {
read_until_newline(toks);
devour_whitespace(toks);
2020-04-01 15:32:52 -04:00
Ok(IntermediateValue::Whitespace)
2020-03-29 13:28:17 -04:00
} else {
2020-04-01 15:32:52 -04:00
Ok(IntermediateValue::Op(Op::Div))
2020-03-29 13:28:17 -04:00
}
}
':' | '?' | ')' => Err("expected \";\".".into()),
2020-03-29 13:28:17 -04:00
v if v.is_control() => Err("Expected expression.".into()),
2020-03-01 12:03:14 -05:00
v => {
dbg!(v);
panic!("Unexpected token in value parsing")
}
2020-02-08 16:17:58 -05:00
}
}
}