grass/src/value/parse.rs

312 lines
13 KiB
Rust
Raw Normal View History

2020-02-08 16:17:58 -05:00
use std::convert::TryFrom;
use std::iter::{Iterator, Peekable};
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;
use crate::common::{Keyword, ListSeparator, Op, QuoteKind, Symbol};
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_or_comment, flatten_ident, parse_interpolation, parse_quoted_string,
};
2020-02-08 16:17:58 -05:00
use crate::value::Value;
use crate::{Token, TokenKind};
2020-02-08 18:43:18 -05:00
use super::number::Number;
2020-02-29 16:13:57 -05:00
fn parse_hex(s: &str) -> Value {
2020-02-08 16:44:42 -05:00
match s.len() {
3 => {
2020-03-18 20:11:14 -04:00
let v = match u16::from_str_radix(s, 16) {
Ok(a) => a,
Err(_) => return 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-02-08 16:44:42 -05:00
Value::Color(Color::new(red, green, blue, 1, format!("#{}", s)))
}
4 => {
2020-03-18 20:11:14 -04:00
let v = match u16::from_str_radix(s, 16) {
Ok(a) => a,
Err(_) => return 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-02-08 16:44:42 -05:00
Value::Color(Color::new(red, green, blue, alpha, format!("#{}", s)))
}
6 => {
2020-03-18 20:11:14 -04:00
let v = match u32::from_str_radix(s, 16) {
Ok(a) => a,
Err(_) => return 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-02-08 16:44:42 -05:00
Value::Color(Color::new(red, green, blue, 1, format!("#{}", s)))
}
8 => {
2020-03-18 20:11:14 -04:00
let v = match u32::from_str_radix(s, 16) {
Ok(a) => a,
Err(_) => return 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-02-08 16:44:42 -05:00
Value::Color(Color::new(red, green, blue, alpha, format!("#{}", s)))
}
_ => Value::Ident(format!("#{}", s), QuoteKind::None),
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-01 14:53:52 -05:00
devour_whitespace_or_comment(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 {
TokenKind::Symbol(Symbol::SemiColon) | TokenKind::Symbol(Symbol::CloseParen) => {
2020-02-16 10:54:25 -05:00
Ok(left)
2020-02-08 16:17:58 -05:00
}
TokenKind::Symbol(Symbol::Comma) => {
toks.next();
devour_whitespace_or_comment(toks);
2020-03-01 12:03:14 -05:00
let right = match Self::from_tokens(toks, scope, super_selector) {
2020-02-16 10:54:25 -05:00
Ok(x) => x,
Err(_) => return Ok(left),
2020-02-08 16:17:58 -05:00
};
if let Value::List(v, ListSeparator::Comma) = right {
let mut v2 = vec![left];
v2.extend(v);
Ok(Value::List(v2, ListSeparator::Comma))
} else {
Ok(Value::List(vec![left, right], ListSeparator::Comma))
}
2020-02-08 16:17:58 -05:00
}
TokenKind::Symbol(Symbol::Plus)
| TokenKind::Symbol(Symbol::Minus)
| TokenKind::Op(_)
| TokenKind::Symbol(Symbol::Mul)
| TokenKind::Symbol(Symbol::Div)
| TokenKind::Symbol(Symbol::Percent) => {
let op = match next.kind {
TokenKind::Symbol(Symbol::Plus) => Op::Plus,
TokenKind::Symbol(Symbol::Minus) => Op::Minus,
TokenKind::Symbol(Symbol::Mul) => Op::Mul,
TokenKind::Symbol(Symbol::Div) => Op::Div,
TokenKind::Symbol(Symbol::Percent) => Op::Rem,
TokenKind::Op(op) => op,
_ => unsafe { std::hint::unreachable_unchecked() },
};
toks.next();
devour_whitespace_or_comment(toks);
2020-03-01 12:03:14 -05:00
let right = match Self::from_tokens(toks, scope, super_selector) {
2020-02-16 10:54:25 -05:00
Ok(x) => x,
Err(_) => return Ok(left),
2020-02-08 16:17:58 -05:00
};
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-02-08 16:17:58 -05:00
devour_whitespace_or_comment(toks);
2020-03-01 12:03:14 -05:00
let right = match Self::from_tokens(toks, scope, super_selector) {
2020-02-16 10:54:25 -05:00
Ok(x) => x,
Err(_) => return Ok(left),
2020-02-08 16:17:58 -05:00
};
2020-03-19 21:40:36 -04:00
if let Value::List(v, ListSeparator::Space) = right {
let mut v2 = vec![left];
v2.extend(v);
Ok(Value::List(v2, ListSeparator::Space))
} else {
Ok(Value::List(vec![left, right], ListSeparator::Space))
}
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-02-08 16:17:58 -05:00
let kind = if let Some(tok) = toks.next() {
tok.kind
} else {
2020-02-29 15:28:48 -05:00
panic!("Unexpected EOF");
2020-02-08 16:17:58 -05:00
};
match kind {
TokenKind::Number(val) => {
let unit = if let Some(tok) = toks.peek() {
match tok.kind.clone() {
TokenKind::Ident(i) => {
toks.next();
Unit::from(&i)
}
TokenKind::Symbol(Symbol::Percent) => {
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-02-16 10:54:25 -05:00
Ok(Value::Dimension(Number::new(n), unit))
2020-02-08 16:17:58 -05:00
}
TokenKind::Symbol(Symbol::OpenParen) => {
devour_whitespace_or_comment(toks);
2020-03-20 20:01:57 -04:00
if toks.peek().unwrap().is_symbol(Symbol::CloseParen) {
toks.next();
return Ok(Value::List(Vec::new(), ListSeparator::Space));
}
2020-03-01 12:03:14 -05:00
let val = Self::from_tokens(toks, scope, super_selector)?;
let next = toks.next();
if next.is_none() || !next.unwrap().is_symbol(Symbol::CloseParen) {
return Err("expected \")\".".into());
}
2020-02-16 10:54:25 -05:00
Ok(Value::Paren(Box::new(val)))
2020-02-08 16:17:58 -05:00
}
TokenKind::Symbol(Symbol::BitAnd) => {
2020-03-01 12:03:14 -05:00
Ok(Value::Ident(super_selector.to_string(), QuoteKind::None))
}
TokenKind::Symbol(Symbol::Hash) => {
Ok(parse_hex(&flatten_ident(toks, scope, super_selector)?))
2020-02-08 16:17:58 -05:00
}
TokenKind::Ident(mut s) => {
2020-03-01 12:03:14 -05:00
s.push_str(&flatten_ident(toks, scope, super_selector)?);
2020-02-08 16:17:58 -05:00
match toks.peek() {
Some(Token {
kind: TokenKind::Symbol(Symbol::OpenParen),
..
}) => {
toks.next();
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,
)
}
None => {
s.push('(');
let mut unclosed_parens = 0;
while let Some(t) = toks.next() {
match &t.kind {
TokenKind::Symbol(Symbol::OpenParen) => {
unclosed_parens += 1;
}
TokenKind::Interpolation => s.push_str(
2020-03-01 12:03:14 -05:00
&parse_interpolation(toks, scope, super_selector)?
.to_string(),
),
TokenKind::Variable(v) => {
s.push_str(&scope.get_var(v)?.to_string())
}
TokenKind::Symbol(Symbol::CloseParen) => {
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-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)?)?
.call(super_selector)?)
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-02-16 10:54:25 -05:00
Ok(Value::Ident(s, QuoteKind::None))
2020-02-08 16:17:58 -05:00
}
}
}
}
2020-02-24 09:37:32 -05:00
q @ TokenKind::Symbol(Symbol::DoubleQuote)
2020-03-01 12:03:14 -05:00
| q @ TokenKind::Symbol(Symbol::SingleQuote) => {
parse_quoted_string(toks, scope, &q, super_selector)
}
2020-03-18 20:11:14 -04:00
TokenKind::Variable(ref v) => Ok(scope.get_var(v)?),
2020-02-08 16:17:58 -05:00
TokenKind::Interpolation => {
2020-03-01 12:03:14 -05:00
let mut s = parse_interpolation(toks, scope, super_selector)?.to_string();
2020-02-08 16:17:58 -05:00
while let Some(tok) = toks.peek() {
match tok.kind.clone() {
TokenKind::Interpolation => {
toks.next();
s.push_str(
2020-03-01 12:03:14 -05:00
&parse_interpolation(toks, scope, super_selector)?.to_string(),
2020-02-08 16:17:58 -05:00
)
}
TokenKind::Ident(ref i) => {
toks.next();
s.push_str(i)
}
_ => break,
}
}
2020-02-16 10:54:25 -05:00
Ok(Value::Ident(s, QuoteKind::None))
2020-02-08 16:17:58 -05:00
}
2020-02-16 10:54:25 -05:00
TokenKind::Keyword(Keyword::Important) => Ok(Value::Important),
TokenKind::Keyword(Keyword::True) => Ok(Value::True),
TokenKind::Keyword(Keyword::False) => Ok(Value::False),
TokenKind::Keyword(Keyword::Null) => Ok(Value::Null),
TokenKind::Keyword(Keyword::From(s)) => Ok(Value::Ident(s, QuoteKind::None)),
TokenKind::Keyword(Keyword::Through(s)) => Ok(Value::Ident(s, QuoteKind::None)),
TokenKind::Keyword(Keyword::To(s)) => Ok(Value::Ident(s, QuoteKind::None)),
2020-02-23 07:52:14 -05:00
TokenKind::Unknown(c) => Ok(Value::Ident(c.to_string(), QuoteKind::None)),
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
}
}
}