2020-02-01 21:59:23 -05:00
|
|
|
use crate::common::{Pos, Scope, Symbol};
|
2020-02-16 10:54:25 -05:00
|
|
|
use crate::error::SassResult;
|
2020-02-01 21:59:23 -05:00
|
|
|
use crate::selector::Selector;
|
|
|
|
use crate::utils::{devour_whitespace, parse_interpolation};
|
2020-01-25 09:54:38 -05:00
|
|
|
use crate::value::Value;
|
2020-02-01 21:59:23 -05:00
|
|
|
use crate::{Expr, Token, TokenKind};
|
2020-01-06 17:06:37 -05:00
|
|
|
use std::fmt::{self, Display};
|
2020-01-06 16:50:16 -05:00
|
|
|
use std::iter::Peekable;
|
|
|
|
|
2020-01-08 20:58:02 -05:00
|
|
|
/// A style: `color: red`
|
2020-01-06 16:50:16 -05:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
2020-01-20 13:15:47 -05:00
|
|
|
pub(crate) struct Style {
|
2020-02-01 19:33:56 -05:00
|
|
|
pub property: String,
|
|
|
|
pub value: Value,
|
2020-01-06 16:50:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for Style {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2020-01-12 10:54:46 -05:00
|
|
|
write!(f, "{}: {};", self.property, self.value)
|
2020-01-06 16:50:16 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Style {
|
2020-02-01 21:59:23 -05:00
|
|
|
pub fn parse_property<I: Iterator<Item = Token>>(
|
|
|
|
toks: &mut Peekable<I>,
|
|
|
|
scope: &Scope,
|
|
|
|
super_selector: &Selector,
|
|
|
|
super_property: String,
|
2020-02-17 07:18:54 -05:00
|
|
|
) -> SassResult<String> {
|
2020-02-01 21:59:23 -05:00
|
|
|
StyleParser::new(scope, super_selector).parse_property(toks, super_property)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn parse_value<I: Iterator<Item = Token>>(
|
|
|
|
toks: &mut Peekable<I>,
|
|
|
|
scope: &Scope,
|
|
|
|
super_selector: &Selector,
|
2020-02-16 10:54:25 -05:00
|
|
|
) -> SassResult<Value> {
|
2020-02-01 21:59:23 -05:00
|
|
|
StyleParser::new(scope, super_selector).parse_style_value(toks)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_tokens<I: Iterator<Item = Token>>(
|
|
|
|
toks: &mut Peekable<I>,
|
|
|
|
scope: &Scope,
|
|
|
|
super_selector: &Selector,
|
|
|
|
super_property: String,
|
2020-02-16 10:54:25 -05:00
|
|
|
) -> SassResult<Expr> {
|
2020-02-01 21:59:23 -05:00
|
|
|
StyleParser::new(scope, super_selector).eat_style_group(toks, super_property)
|
2020-01-06 16:50:16 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct StyleParser<'a> {
|
2020-01-12 17:44:49 -05:00
|
|
|
scope: &'a Scope,
|
2020-02-01 21:59:23 -05:00
|
|
|
super_selector: &'a Selector,
|
2020-01-06 16:50:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> StyleParser<'a> {
|
2020-02-09 19:07:44 -05:00
|
|
|
const fn new(scope: &'a Scope, super_selector: &'a Selector) -> Self {
|
2020-02-01 21:59:23 -05:00
|
|
|
StyleParser {
|
|
|
|
scope,
|
|
|
|
super_selector,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn parse_style_value<I: Iterator<Item = Token>>(
|
|
|
|
&self,
|
|
|
|
toks: &mut Peekable<I>,
|
2020-02-16 10:54:25 -05:00
|
|
|
) -> SassResult<Value> {
|
2020-02-01 21:59:23 -05:00
|
|
|
let mut style = Vec::new();
|
|
|
|
let mut n = 0;
|
|
|
|
devour_whitespace(toks);
|
|
|
|
while let Some(tok) = toks.peek() {
|
|
|
|
match tok.kind {
|
|
|
|
TokenKind::MultilineComment(_) => {
|
|
|
|
toks.next();
|
|
|
|
continue;
|
|
|
|
}
|
2020-02-22 16:41:10 -05:00
|
|
|
TokenKind::Interpolation => n += 1,
|
2020-02-01 21:59:23 -05:00
|
|
|
TokenKind::Symbol(Symbol::CloseCurlyBrace) => {
|
|
|
|
if n == 0 {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
// todo: toks.next() and push
|
|
|
|
n -= 1;
|
|
|
|
}
|
|
|
|
}
|
2020-02-22 16:41:10 -05:00
|
|
|
TokenKind::Symbol(Symbol::OpenCurlyBrace)
|
|
|
|
| TokenKind::Symbol(Symbol::SemiColon) => break,
|
2020-02-01 21:59:23 -05:00
|
|
|
TokenKind::Symbol(Symbol::BitAnd) => {
|
|
|
|
style.push(Token {
|
|
|
|
kind: TokenKind::Ident(self.super_selector.to_string()),
|
|
|
|
pos: Pos::new(),
|
|
|
|
});
|
|
|
|
toks.next();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
};
|
|
|
|
style.push(toks.next().unwrap());
|
2020-01-06 16:50:16 -05:00
|
|
|
}
|
2020-02-16 10:54:25 -05:00
|
|
|
Value::from_tokens(&mut style.into_iter().peekable(), self.scope)
|
2020-01-06 16:50:16 -05:00
|
|
|
}
|
|
|
|
|
2020-02-01 21:59:23 -05:00
|
|
|
pub(crate) fn eat_style_group<I: Iterator<Item = Token>>(
|
|
|
|
&self,
|
|
|
|
toks: &mut Peekable<I>,
|
|
|
|
super_property: String,
|
2020-02-16 10:54:25 -05:00
|
|
|
) -> SassResult<Expr> {
|
2020-02-01 21:59:23 -05:00
|
|
|
let mut styles = Vec::new();
|
|
|
|
devour_whitespace(toks);
|
|
|
|
while let Some(tok) = toks.peek() {
|
|
|
|
match tok.kind {
|
|
|
|
TokenKind::Symbol(Symbol::OpenCurlyBrace) => {
|
|
|
|
toks.next();
|
|
|
|
devour_whitespace(toks);
|
|
|
|
loop {
|
2020-02-17 07:18:54 -05:00
|
|
|
let property = self.parse_property(toks, super_property.clone())?;
|
2020-02-01 21:59:23 -05:00
|
|
|
if let Some(tok) = toks.peek() {
|
2020-02-22 16:41:10 -05:00
|
|
|
if tok.equals_symbol(Symbol::OpenCurlyBrace) {
|
|
|
|
match self.eat_style_group(toks, property)? {
|
|
|
|
Expr::Styles(s) => styles.extend(s),
|
2020-02-22 17:57:13 -05:00
|
|
|
Expr::Style(s) => styles.push(*s),
|
2020-02-22 16:41:10 -05:00
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
devour_whitespace(toks);
|
|
|
|
if let Some(tok) = toks.peek() {
|
|
|
|
if tok.equals_symbol(Symbol::CloseCurlyBrace) {
|
|
|
|
toks.next();
|
|
|
|
devour_whitespace(toks);
|
|
|
|
return Ok(Expr::Styles(styles));
|
|
|
|
} else {
|
|
|
|
continue;
|
2020-02-01 21:59:23 -05:00
|
|
|
}
|
|
|
|
}
|
2020-02-22 16:41:10 -05:00
|
|
|
continue;
|
2020-02-01 21:59:23 -05:00
|
|
|
}
|
|
|
|
}
|
2020-02-16 10:54:25 -05:00
|
|
|
let value = self.parse_style_value(toks)?;
|
2020-02-22 16:41:10 -05:00
|
|
|
match toks.peek().unwrap().kind {
|
|
|
|
TokenKind::Symbol(Symbol::CloseCurlyBrace) => {
|
|
|
|
styles.push(Style { property, value });
|
|
|
|
}
|
|
|
|
TokenKind::Symbol(Symbol::SemiColon) => {
|
|
|
|
toks.next();
|
|
|
|
devour_whitespace(toks);
|
|
|
|
styles.push(Style { property, value });
|
|
|
|
}
|
|
|
|
TokenKind::Symbol(Symbol::OpenCurlyBrace) => {
|
|
|
|
styles.push(Style {
|
|
|
|
property: property.clone(),
|
|
|
|
value,
|
|
|
|
});
|
|
|
|
match self.eat_style_group(toks, property)? {
|
2020-02-22 17:57:13 -05:00
|
|
|
Expr::Style(s) => styles.push(*s),
|
2020-02-22 16:41:10 -05:00
|
|
|
Expr::Styles(s) => styles.extend(s),
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
devour_whitespace(toks);
|
|
|
|
styles.push(Style { property, value });
|
|
|
|
}
|
|
|
|
}
|
2020-02-01 21:59:23 -05:00
|
|
|
if let Some(tok) = toks.peek() {
|
|
|
|
match tok.kind {
|
|
|
|
TokenKind::Symbol(Symbol::CloseCurlyBrace) => {
|
|
|
|
toks.next();
|
|
|
|
devour_whitespace(toks);
|
|
|
|
return Ok(Expr::Styles(styles));
|
|
|
|
}
|
|
|
|
_ => continue,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
2020-02-16 10:54:25 -05:00
|
|
|
let val = self.parse_style_value(toks)?;
|
2020-02-22 16:41:10 -05:00
|
|
|
match toks.peek().unwrap().kind {
|
|
|
|
TokenKind::Symbol(Symbol::CloseCurlyBrace) => {}
|
|
|
|
TokenKind::Symbol(Symbol::SemiColon) => {
|
|
|
|
toks.next();
|
|
|
|
devour_whitespace(toks);
|
|
|
|
}
|
|
|
|
TokenKind::Symbol(Symbol::OpenCurlyBrace) => {
|
|
|
|
let mut v = vec![Style {
|
|
|
|
property: super_property.clone(),
|
|
|
|
value: val,
|
|
|
|
}];
|
|
|
|
match self.eat_style_group(toks, super_property)? {
|
2020-02-22 17:57:13 -05:00
|
|
|
Expr::Style(s) => v.push(*s),
|
2020-02-22 16:41:10 -05:00
|
|
|
Expr::Styles(s) => v.extend(s),
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
return Ok(Expr::Styles(v));
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
2020-02-22 17:57:13 -05:00
|
|
|
return Ok(Expr::Style(Box::new(Style {
|
2020-02-01 21:59:23 -05:00
|
|
|
property: super_property,
|
|
|
|
value: val,
|
2020-02-22 17:57:13 -05:00
|
|
|
})));
|
2020-02-01 21:59:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(Expr::Styles(styles))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn parse_property<I: Iterator<Item = Token>>(
|
|
|
|
&self,
|
|
|
|
toks: &mut Peekable<I>,
|
|
|
|
mut super_property: String,
|
2020-02-17 07:18:54 -05:00
|
|
|
) -> SassResult<String> {
|
2020-01-12 10:54:46 -05:00
|
|
|
let mut property = String::new();
|
2020-02-01 21:59:23 -05:00
|
|
|
while let Some(Token { kind, .. }) = toks.next() {
|
2020-01-08 20:39:05 -05:00
|
|
|
match kind {
|
|
|
|
TokenKind::Whitespace(_) | TokenKind::MultilineComment(_) => continue,
|
2020-01-12 10:54:46 -05:00
|
|
|
TokenKind::Ident(ref s) => property.push_str(s),
|
2020-01-14 17:39:19 -05:00
|
|
|
TokenKind::Interpolation => property.push_str(
|
2020-02-17 07:18:54 -05:00
|
|
|
&parse_interpolation(toks, self.scope)?
|
2020-01-14 17:39:19 -05:00
|
|
|
.iter()
|
|
|
|
.map(|x| x.kind.to_string())
|
|
|
|
.collect::<String>(),
|
|
|
|
),
|
2020-01-06 16:50:16 -05:00
|
|
|
TokenKind::Symbol(Symbol::Colon) => break,
|
2020-02-01 21:59:23 -05:00
|
|
|
TokenKind::Symbol(Symbol::BitAnd) => {
|
|
|
|
property.push_str(&self.super_selector.to_string())
|
|
|
|
}
|
2020-01-12 10:54:46 -05:00
|
|
|
_ => property.push_str(&kind.to_string()),
|
|
|
|
};
|
2020-01-06 16:50:16 -05:00
|
|
|
}
|
2020-02-01 21:59:23 -05:00
|
|
|
devour_whitespace(toks);
|
2020-02-02 10:27:08 -05:00
|
|
|
if super_property.is_empty() {
|
2020-02-17 07:18:54 -05:00
|
|
|
Ok(property)
|
2020-02-02 10:27:08 -05:00
|
|
|
} else {
|
2020-02-01 21:59:23 -05:00
|
|
|
super_property.reserve(1 + property.len());
|
|
|
|
super_property.push('-');
|
|
|
|
super_property.push_str(&property);
|
2020-02-17 07:18:54 -05:00
|
|
|
Ok(super_property)
|
2020-02-01 21:59:23 -05:00
|
|
|
}
|
2020-01-06 16:50:16 -05:00
|
|
|
}
|
|
|
|
}
|