2020-02-08 16:17:58 -05:00
|
|
|
use std::convert::TryFrom;
|
|
|
|
use std::iter::{Iterator, Peekable};
|
|
|
|
|
2020-02-08 20:07:20 -05:00
|
|
|
use num_bigint::BigInt;
|
2020-02-08 20:20:03 -05:00
|
|
|
use num_rational::BigRational;
|
2020-02-08 20:07:20 -05:00
|
|
|
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;
|
2020-03-17 20:13:53 -04:00
|
|
|
use crate::scope::Scope;
|
2020-03-01 12:03:14 -05:00
|
|
|
use crate::selector::Selector;
|
2020-03-19 16:24:31 -04:00
|
|
|
use crate::unit::Unit;
|
2020-03-01 12:03:14 -05:00
|
|
|
use crate::utils::{
|
2020-03-30 02:21:41 -04:00
|
|
|
devour_whitespace, eat_comment, eat_ident, eat_ident_no_interpolation, eat_number,
|
|
|
|
parse_interpolation, parse_quoted_string, read_until_newline,
|
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);
|
|
|
|
if toks.peek().unwrap().kind.is_ascii_digit() {
|
|
|
|
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) {
|
2020-02-23 07:41:00 -05:00
|
|
|
Ok(a) => a,
|
2020-03-29 13:28:17 -04:00
|
|
|
Err(_) => return Ok(Value::Ident(format!("#{}", s), QuoteKind::None)),
|
2020-02-23 07:41:00 -05:00
|
|
|
};
|
2020-02-09 13:31:58 -05:00
|
|
|
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) {
|
2020-02-23 07:41:00 -05:00
|
|
|
Ok(a) => a,
|
2020-03-29 13:28:17 -04:00
|
|
|
Err(_) => return Ok(Value::Ident(format!("#{}", s), QuoteKind::None)),
|
2020-02-23 07:41:00 -05:00
|
|
|
};
|
2020-02-09 13:31:58 -05:00
|
|
|
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) {
|
2020-02-23 07:41:00 -05:00
|
|
|
Ok(a) => a,
|
2020-03-29 13:28:17 -04:00
|
|
|
Err(_) => return Ok(Value::Ident(format!("#{}", s), QuoteKind::None)),
|
2020-02-23 07:41:00 -05:00
|
|
|
};
|
2020-02-09 13:31:58 -05:00
|
|
|
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) {
|
2020-02-23 07:41:00 -05:00
|
|
|
Ok(a) => a,
|
2020-03-29 13:28:17 -04:00
|
|
|
Err(_) => return Ok(Value::Ident(format!("#{}", s), QuoteKind::None)),
|
2020-02-23 07:41:00 -05:00
|
|
|
};
|
2020-02-09 13:31:58 -05:00
|
|
|
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-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-03-01 12:03:14 -05:00
|
|
|
let left = Self::_from_tokens(toks, scope, super_selector)?;
|
2020-03-29 13:28:17 -04:00
|
|
|
devour_whitespace(toks);
|
2020-02-08 16:17:58 -05:00
|
|
|
let next = match toks.peek() {
|
|
|
|
Some(x) => x,
|
2020-02-16 10:54:25 -05:00
|
|
|
None => return Ok(left),
|
2020-02-08 16:17:58 -05:00
|
|
|
};
|
|
|
|
match next.kind {
|
2020-03-30 15:43:15 -04:00
|
|
|
';' | ')' | ']' | ':' => Ok(left),
|
2020-03-29 13:28:17 -04:00
|
|
|
',' => {
|
2020-02-08 16:17:58 -05:00
|
|
|
toks.next();
|
2020-03-29 13:28:17 -04:00
|
|
|
devour_whitespace(toks);
|
2020-03-24 00:17:18 -04:00
|
|
|
if toks.peek() == None {
|
|
|
|
return Ok(Value::List(
|
|
|
|
vec![left],
|
|
|
|
ListSeparator::Comma,
|
|
|
|
Brackets::None,
|
|
|
|
));
|
|
|
|
} else if let Some(tok) = toks.peek() {
|
2020-03-29 13:28:17 -04:00
|
|
|
if tok.kind == ')' {
|
2020-03-24 00:17:18 -04:00
|
|
|
return Ok(Value::List(
|
|
|
|
vec![left],
|
|
|
|
ListSeparator::Comma,
|
|
|
|
Brackets::None,
|
|
|
|
));
|
2020-03-29 13:28:17 -04:00
|
|
|
} else if tok.kind == ']' {
|
2020-03-24 00:17:18 -04:00
|
|
|
return Ok(Value::List(
|
|
|
|
vec![left],
|
|
|
|
ListSeparator::Comma,
|
|
|
|
Brackets::Bracketed,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
2020-03-20 23:34:23 -04:00
|
|
|
let right = Self::from_tokens(toks, scope, super_selector)?;
|
2020-03-23 19:56:24 -04:00
|
|
|
if let Value::List(v, ListSeparator::Comma, Brackets::None) = right {
|
2020-03-20 10:59:16 -04:00
|
|
|
let mut v2 = vec![left];
|
|
|
|
v2.extend(v);
|
2020-03-23 19:56:24 -04:00
|
|
|
Ok(Value::List(v2, ListSeparator::Comma, Brackets::None))
|
2020-03-20 10:59:16 -04:00
|
|
|
} else {
|
2020-03-23 19:56:24 -04:00
|
|
|
Ok(Value::List(
|
|
|
|
vec![left, right],
|
|
|
|
ListSeparator::Comma,
|
|
|
|
Brackets::None,
|
|
|
|
))
|
2020-03-20 10:59:16 -04:00
|
|
|
}
|
2020-02-08 16:17:58 -05:00
|
|
|
}
|
2020-03-29 22:17:56 -04:00
|
|
|
'+' | '*' | '%' => {
|
2020-02-08 16:17:58 -05:00
|
|
|
let op = match next.kind {
|
2020-03-29 13:28:17 -04:00
|
|
|
'+' => Op::Plus,
|
|
|
|
'*' => Op::Mul,
|
|
|
|
'%' => Op::Rem,
|
2020-02-08 16:17:58 -05:00
|
|
|
_ => unsafe { std::hint::unreachable_unchecked() },
|
|
|
|
};
|
|
|
|
toks.next();
|
2020-03-29 13:28:17 -04:00
|
|
|
devour_whitespace(toks);
|
2020-03-20 23:34:23 -04:00
|
|
|
let right = Self::from_tokens(toks, scope, super_selector)?;
|
2020-02-16 10:54:25 -05:00
|
|
|
Ok(Value::BinaryOp(Box::new(left), op, Box::new(right)))
|
2020-02-08 16:17:58 -05:00
|
|
|
}
|
2020-03-29 13:28:17 -04:00
|
|
|
'=' => {
|
|
|
|
toks.next();
|
|
|
|
if toks.peek().unwrap().kind == '=' {
|
|
|
|
toks.next();
|
|
|
|
devour_whitespace(toks);
|
|
|
|
let right = Self::from_tokens(toks, scope, super_selector)?;
|
|
|
|
Ok(Value::BinaryOp(Box::new(left), Op::Equal, Box::new(right)))
|
|
|
|
} else {
|
2020-03-30 17:06:23 -04:00
|
|
|
Err("expected \"=\".".into())
|
2020-03-29 13:28:17 -04:00
|
|
|
}
|
|
|
|
}
|
2020-03-30 10:42:13 -04:00
|
|
|
q @ '>' | q @ '<' => {
|
|
|
|
toks.next();
|
|
|
|
let op = if toks.peek().unwrap().kind == '=' {
|
|
|
|
toks.next();
|
|
|
|
match q {
|
|
|
|
'>' => Op::GreaterThanEqual,
|
|
|
|
'<' => Op::LessThanEqual,
|
2020-03-30 15:43:15 -04:00
|
|
|
_ => unreachable!(),
|
2020-03-30 10:42:13 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
match q {
|
|
|
|
'>' => Op::GreaterThan,
|
|
|
|
'<' => Op::LessThan,
|
2020-03-30 15:43:15 -04:00
|
|
|
_ => unreachable!(),
|
2020-03-30 10:42:13 -04:00
|
|
|
}
|
|
|
|
};
|
2020-03-30 15:43:15 -04:00
|
|
|
devour_whitespace(toks);
|
2020-03-30 10:42:13 -04:00
|
|
|
let right = Self::from_tokens(toks, scope, super_selector)?;
|
|
|
|
Ok(Value::BinaryOp(Box::new(left), op, Box::new(right)))
|
|
|
|
}
|
2020-03-29 13:28:17 -04:00
|
|
|
'!' => {
|
|
|
|
toks.next();
|
|
|
|
if toks.peek().unwrap().kind == '=' {
|
|
|
|
toks.next();
|
|
|
|
devour_whitespace(toks);
|
|
|
|
let right = Self::from_tokens(toks, scope, super_selector)?;
|
|
|
|
Ok(Value::BinaryOp(
|
|
|
|
Box::new(left),
|
|
|
|
Op::NotEqual,
|
|
|
|
Box::new(right),
|
|
|
|
))
|
|
|
|
} else if eat_ident(toks, scope, super_selector)?
|
|
|
|
.to_ascii_lowercase()
|
|
|
|
.as_str()
|
|
|
|
== "important"
|
|
|
|
{
|
|
|
|
Ok(Value::List(
|
|
|
|
vec![left, Value::Important],
|
|
|
|
ListSeparator::Space,
|
|
|
|
Brackets::None,
|
|
|
|
))
|
|
|
|
} else {
|
2020-03-30 17:06:23 -04:00
|
|
|
Err("Expected \"important\".".into())
|
2020-03-29 13:28:17 -04:00
|
|
|
}
|
|
|
|
}
|
2020-03-29 22:17:56 -04:00
|
|
|
'-' => {
|
|
|
|
toks.next();
|
|
|
|
if devour_whitespace(toks) {
|
|
|
|
let right = Self::from_tokens(toks, scope, super_selector)?;
|
|
|
|
Ok(Value::BinaryOp(Box::new(left), Op::Minus, Box::new(right)))
|
|
|
|
} else {
|
|
|
|
let right = Self::from_tokens(toks, scope, super_selector)?;
|
|
|
|
if let Value::List(mut v, ListSeparator::Space, ..) = right {
|
|
|
|
let mut v2 = vec![left];
|
|
|
|
let val = v.remove(0);
|
|
|
|
v2.push((-val)?);
|
|
|
|
v2.extend(v);
|
|
|
|
Ok(Value::List(v2, ListSeparator::Space, Brackets::None))
|
|
|
|
} else {
|
|
|
|
Ok(Value::List(
|
|
|
|
vec![left, (-right)?],
|
|
|
|
ListSeparator::Space,
|
|
|
|
Brackets::None,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-29 13:28:17 -04:00
|
|
|
'/' => {
|
|
|
|
toks.next();
|
|
|
|
match toks.peek().unwrap().kind {
|
|
|
|
v @ '*' | v @ '/' => {
|
|
|
|
toks.next();
|
|
|
|
if v == '*' {
|
|
|
|
eat_comment(toks, &Scope::new(), &Selector::new())?;
|
|
|
|
} else {
|
|
|
|
read_until_newline(toks);
|
|
|
|
}
|
|
|
|
devour_whitespace(toks);
|
|
|
|
if toks.peek().is_none() {
|
|
|
|
return Ok(left);
|
|
|
|
}
|
|
|
|
let right = Self::from_tokens(toks, scope, super_selector)?;
|
|
|
|
if let Value::List(v, ListSeparator::Space, ..) = right {
|
|
|
|
let mut v2 = vec![left];
|
|
|
|
v2.extend(v);
|
|
|
|
Ok(Value::List(v2, ListSeparator::Space, Brackets::None))
|
|
|
|
} else {
|
|
|
|
Ok(Value::List(
|
|
|
|
vec![left, right],
|
|
|
|
ListSeparator::Space,
|
|
|
|
Brackets::None,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
devour_whitespace(toks);
|
|
|
|
let right = Self::from_tokens(toks, scope, super_selector)?;
|
|
|
|
Ok(Value::BinaryOp(Box::new(left), Op::Div, Box::new(right)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-24 19:05:50 -05:00
|
|
|
_ => {
|
2020-03-29 13:28:17 -04:00
|
|
|
devour_whitespace(toks);
|
2020-03-20 23:34:23 -04:00
|
|
|
let right = Self::from_tokens(toks, scope, super_selector)?;
|
2020-03-24 00:17:18 -04:00
|
|
|
if let Value::List(v, ListSeparator::Space, ..) = right {
|
2020-03-19 21:40:36 -04:00
|
|
|
let mut v2 = vec![left];
|
|
|
|
v2.extend(v);
|
2020-03-23 19:56:24 -04:00
|
|
|
Ok(Value::List(v2, ListSeparator::Space, Brackets::None))
|
2020-03-19 21:40:36 -04:00
|
|
|
} else {
|
2020-03-23 19:56:24 -04:00
|
|
|
Ok(Value::List(
|
|
|
|
vec![left, right],
|
|
|
|
ListSeparator::Space,
|
|
|
|
Brackets::None,
|
|
|
|
))
|
2020-03-19 21:40:36 -04:00
|
|
|
}
|
2020-02-08 16:17:58 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-03-29 13:28:17 -04:00
|
|
|
let kind = if let Some(tok) = toks.peek() {
|
2020-02-08 16:17:58 -05:00
|
|
|
tok.kind
|
|
|
|
} else {
|
2020-02-29 15:28:48 -05:00
|
|
|
panic!("Unexpected EOF");
|
2020-02-08 16:17:58 -05:00
|
|
|
};
|
|
|
|
match kind {
|
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>() {
|
2020-02-08 20:07:20 -05:00
|
|
|
// 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-08 20:07:20 -05:00
|
|
|
}
|
2020-02-09 19:07:44 -05:00
|
|
|
num.push(c);
|
|
|
|
}
|
|
|
|
for c in chars {
|
|
|
|
num_dec += 1;
|
|
|
|
num.push(c);
|
2020-02-08 20:07:20 -05:00
|
|
|
}
|
2020-02-09 19:07:44 -05:00
|
|
|
BigRational::new(num.parse().unwrap(), pow(BigInt::from(10), num_dec))
|
2020-02-08 20:07:20 -05:00
|
|
|
};
|
2020-02-16 10:54:25 -05:00
|
|
|
Ok(Value::Dimension(Number::new(n), unit))
|
2020-02-08 16:17:58 -05:00
|
|
|
}
|
2020-03-29 13:28:17 -04:00
|
|
|
'(' => {
|
|
|
|
toks.next();
|
|
|
|
devour_whitespace(toks);
|
2020-03-30 15:43:15 -04:00
|
|
|
if toks.peek().ok_or("expected \")\".")?.kind == ')' {
|
2020-03-20 20:01:57 -04:00
|
|
|
toks.next();
|
2020-03-30 15:43:15 -04:00
|
|
|
devour_whitespace(toks);
|
2020-03-23 19:56:24 -04:00
|
|
|
return Ok(Value::List(
|
|
|
|
Vec::new(),
|
|
|
|
ListSeparator::Space,
|
|
|
|
Brackets::None,
|
|
|
|
));
|
2020-03-20 20:01:57 -04:00
|
|
|
}
|
2020-03-30 15:43:15 -04:00
|
|
|
let mut map = SassMap::new();
|
|
|
|
let mut key = Self::from_tokens(toks, scope, super_selector)?;
|
|
|
|
match toks.next().ok_or("expected \")\".")?.kind {
|
|
|
|
')' => return Ok(Value::Paren(Box::new(key))),
|
|
|
|
':' => {}
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
loop {
|
|
|
|
devour_whitespace(toks);
|
|
|
|
match Self::from_tokens(toks, scope, super_selector)? {
|
|
|
|
Value::List(mut v, ListSeparator::Comma, Brackets::None) => {
|
|
|
|
devour_whitespace(toks);
|
|
|
|
match v.len() {
|
|
|
|
1 => {
|
|
|
|
map.insert(key, v.pop().unwrap());
|
|
|
|
if toks.peek().is_some() && toks.peek().unwrap().kind == ')' {
|
|
|
|
toks.next();
|
|
|
|
} else {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
2 => {
|
|
|
|
let next_key = v.pop().unwrap();
|
|
|
|
map.insert(key, v.pop().unwrap());
|
|
|
|
key = next_key;
|
|
|
|
if toks.next().ok_or("expected \")\".")?.kind == ':' {
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => todo!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
v => {
|
|
|
|
map.insert(key, v);
|
|
|
|
if toks.peek().is_some() && toks.peek().unwrap().kind == ')' {
|
|
|
|
toks.next();
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-20 23:21:40 -04:00
|
|
|
}
|
2020-03-30 15:43:15 -04:00
|
|
|
Ok(Value::Map(map))
|
2020-02-08 16:17:58 -05:00
|
|
|
}
|
2020-03-29 13:28:17 -04:00
|
|
|
'&' => {
|
|
|
|
toks.next();
|
2020-03-01 12:03:14 -05:00
|
|
|
Ok(Value::Ident(super_selector.to_string(), QuoteKind::None))
|
|
|
|
}
|
2020-03-29 13:28:17 -04:00
|
|
|
'#' => {
|
|
|
|
if let Ok(s) = eat_ident(toks, scope, super_selector) {
|
|
|
|
Ok(Value::Ident(s, QuoteKind::None))
|
|
|
|
} else {
|
|
|
|
Ok(parse_hex(toks, scope, super_selector)?)
|
|
|
|
}
|
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()) =>
|
|
|
|
{
|
2020-03-29 13:28:17 -04:00
|
|
|
let mut s = eat_ident(toks, scope, super_selector)?;
|
2020-02-08 16:17:58 -05:00
|
|
|
match toks.peek() {
|
2020-03-29 13:28:17 -04:00
|
|
|
Some(Token { kind: '(', .. }) => {
|
2020-02-08 16:17:58 -05:00
|
|
|
toks.next();
|
2020-02-08 17:26:01 -05:00
|
|
|
let func = match scope.get_fn(&s) {
|
|
|
|
Ok(f) => f,
|
|
|
|
Err(_) => match GLOBAL_FUNCTIONS.get(&s) {
|
2020-03-01 12:03:14 -05:00
|
|
|
Some(f) => {
|
|
|
|
return f(
|
|
|
|
&mut eat_call_args(toks, scope, super_selector)?,
|
|
|
|
scope,
|
|
|
|
)
|
|
|
|
}
|
2020-02-09 20:26:14 -05:00
|
|
|
None => {
|
|
|
|
s.push('(');
|
|
|
|
let mut unclosed_parens = 0;
|
|
|
|
while let Some(t) = toks.next() {
|
|
|
|
match &t.kind {
|
2020-03-29 13:28:17 -04:00
|
|
|
'(' => {
|
2020-02-09 20:26:14 -05:00
|
|
|
unclosed_parens += 1;
|
|
|
|
}
|
2020-03-29 13:28:17 -04:00
|
|
|
'#' if toks.next().unwrap().kind == '{' => s.push_str(
|
2020-03-01 12:03:14 -05:00
|
|
|
&parse_interpolation(toks, scope, super_selector)?
|
|
|
|
.to_string(),
|
2020-02-09 20:26:14 -05:00
|
|
|
),
|
2020-03-29 13:28:17 -04:00
|
|
|
'$' => s.push_str(
|
|
|
|
&scope
|
|
|
|
.get_var(&eat_ident(
|
|
|
|
toks,
|
|
|
|
scope,
|
|
|
|
super_selector,
|
|
|
|
)?)?
|
|
|
|
.to_string(),
|
|
|
|
),
|
|
|
|
')' => {
|
2020-02-09 20:26:14 -05:00
|
|
|
if unclosed_parens <= 1 {
|
|
|
|
s.push(')');
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
unclosed_parens -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
s.push_str(&t.kind.to_string());
|
|
|
|
}
|
2020-02-16 10:54:25 -05:00
|
|
|
return Ok(Value::Ident(s, QuoteKind::None));
|
2020-02-09 20:26:14 -05:00
|
|
|
}
|
2020-02-08 16:17:58 -05:00
|
|
|
},
|
|
|
|
};
|
2020-02-17 09:28:25 -05:00
|
|
|
Ok(func
|
|
|
|
.clone()
|
2020-03-01 12:03:14 -05:00
|
|
|
.args(&mut eat_call_args(toks, scope, super_selector)?)?
|
2020-03-24 22:13:38 -04:00
|
|
|
.call(super_selector, func.body())?)
|
2020-02-08 16:17:58 -05:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
if let Ok(c) = crate::color::ColorName::try_from(s.as_ref()) {
|
2020-02-16 10:54:25 -05:00
|
|
|
Ok(Value::Color(c.into_color(s)))
|
2020-02-08 16:17:58 -05:00
|
|
|
} else {
|
2020-03-29 13:28:17 -04:00
|
|
|
match s.to_ascii_lowercase().as_str() {
|
|
|
|
"true" => Ok(Value::True),
|
|
|
|
"false" => Ok(Value::False),
|
|
|
|
"null" => Ok(Value::Null),
|
|
|
|
_ => Ok(Value::Ident(s, QuoteKind::None)),
|
|
|
|
}
|
2020-02-08 16:17:58 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-29 13:28:17 -04:00
|
|
|
q @ '"' | q @ '\'' => {
|
|
|
|
toks.next();
|
|
|
|
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-03-23 23:19:13 -04:00
|
|
|
if let Some(tok) = toks.peek() {
|
2020-03-29 13:28:17 -04:00
|
|
|
if tok.kind == ']' {
|
2020-03-23 23:19:13 -04:00
|
|
|
toks.next();
|
2020-03-23 23:52:15 -04:00
|
|
|
return Ok(Value::List(
|
|
|
|
Vec::new(),
|
|
|
|
ListSeparator::Space,
|
|
|
|
Brackets::Bracketed,
|
|
|
|
));
|
2020-03-23 23:19:13 -04:00
|
|
|
}
|
|
|
|
}
|
2020-03-23 19:56:24 -04:00
|
|
|
let inner = Self::from_tokens(toks, scope, super_selector)?;
|
2020-03-29 13:28:17 -04:00
|
|
|
devour_whitespace(toks);
|
2020-03-23 19:56:24 -04:00
|
|
|
toks.next();
|
|
|
|
Ok(match inner {
|
|
|
|
Value::List(v, sep, ..) => Value::List(v, sep, Brackets::Bracketed),
|
|
|
|
v => Value::List(vec![v], ListSeparator::Space, Brackets::Bracketed),
|
|
|
|
})
|
|
|
|
}
|
2020-03-29 13:28:17 -04:00
|
|
|
'$' => {
|
|
|
|
toks.next();
|
2020-03-30 01:48:37 -04:00
|
|
|
Ok(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();
|
|
|
|
devour_whitespace(toks);
|
2020-03-21 12:14:02 -04:00
|
|
|
let v = Self::_from_tokens(toks, scope, super_selector)?;
|
|
|
|
Ok(Value::UnaryOp(Op::Plus, Box::new(v)))
|
|
|
|
}
|
2020-03-29 13:28:17 -04:00
|
|
|
'-' => {
|
|
|
|
toks.next();
|
|
|
|
devour_whitespace(toks);
|
2020-03-21 12:14:02 -04:00
|
|
|
let v = Self::_from_tokens(toks, scope, super_selector)?;
|
|
|
|
Ok(Value::UnaryOp(Op::Minus, Box::new(v)))
|
|
|
|
}
|
2020-03-29 13:28:17 -04:00
|
|
|
'!' => {
|
|
|
|
toks.next();
|
|
|
|
let v = eat_ident(toks, scope, super_selector)?;
|
|
|
|
if v.to_ascii_lowercase().as_str() == "important" {
|
|
|
|
Ok(Value::Important)
|
|
|
|
} else {
|
|
|
|
Err("Expected \"important\".".into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
'/' => {
|
|
|
|
toks.next();
|
|
|
|
if '*' == toks.peek().unwrap().kind {
|
|
|
|
toks.next();
|
|
|
|
eat_comment(toks, &Scope::new(), &Selector::new())?;
|
|
|
|
Self::_from_tokens(toks, scope, super_selector)
|
|
|
|
} else if '/' == toks.peek().unwrap().kind {
|
|
|
|
read_until_newline(toks);
|
|
|
|
devour_whitespace(toks);
|
|
|
|
Self::_from_tokens(toks, scope, super_selector)
|
|
|
|
} else {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|