2020-01-12 19:56:58 -05:00
|
|
|
use crate::common::{Scope, Symbol};
|
2020-01-25 09:54:38 -05:00
|
|
|
use crate::utils::{devour_whitespace_or_comment, eat_interpolation};
|
|
|
|
use crate::value::Value;
|
2020-01-12 19:56:58 -05:00
|
|
|
use crate::{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-18 20:24:28 -05:00
|
|
|
use std::vec::IntoIter;
|
2020-01-06 16:50:16 -05:00
|
|
|
|
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-01-06 16:50:16 -05:00
|
|
|
property: String,
|
|
|
|
value: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
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-01-18 20:24:28 -05:00
|
|
|
pub fn from_tokens(tokens: Vec<Token>, scope: &Scope) -> Result<Self, ()> {
|
2020-01-12 17:44:49 -05:00
|
|
|
Ok(StyleParser::new(tokens, scope)?.parse())
|
2020-01-06 16:50:16 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct StyleParser<'a> {
|
2020-01-18 20:24:28 -05:00
|
|
|
tokens: Peekable<IntoIter<Token>>,
|
2020-01-12 17:44:49 -05:00
|
|
|
scope: &'a Scope,
|
2020-01-06 16:50:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> StyleParser<'a> {
|
2020-01-18 20:24:28 -05:00
|
|
|
fn new(tokens: Vec<Token>, scope: &'a Scope) -> Result<Self, ()> {
|
2020-01-06 16:50:16 -05:00
|
|
|
if tokens.is_empty() {
|
|
|
|
return Err(());
|
|
|
|
}
|
2020-01-18 20:24:28 -05:00
|
|
|
let tokens = tokens.into_iter().peekable();
|
2020-01-12 17:44:49 -05:00
|
|
|
Ok(StyleParser { tokens, scope })
|
2020-01-06 16:50:16 -05:00
|
|
|
}
|
|
|
|
|
2020-01-12 10:54:46 -05:00
|
|
|
fn parse(&mut self) -> Style {
|
|
|
|
let mut property = String::new();
|
|
|
|
// read property until `:`
|
2020-01-08 20:39:05 -05:00
|
|
|
while let Some(Token { kind, .. }) = self.tokens.next() {
|
|
|
|
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(
|
|
|
|
&eat_interpolation(&mut self.tokens, self.scope)
|
|
|
|
.iter()
|
|
|
|
.map(|x| x.kind.to_string())
|
|
|
|
.collect::<String>(),
|
|
|
|
),
|
2020-01-06 16:50:16 -05:00
|
|
|
TokenKind::Symbol(Symbol::Colon) => break,
|
2020-01-12 10:54:46 -05:00
|
|
|
_ => property.push_str(&kind.to_string()),
|
|
|
|
};
|
2020-01-06 16:50:16 -05:00
|
|
|
}
|
|
|
|
|
2020-01-25 09:54:38 -05:00
|
|
|
devour_whitespace_or_comment(&mut self.tokens);
|
2020-01-12 10:54:46 -05:00
|
|
|
|
2020-01-06 16:50:16 -05:00
|
|
|
let mut value = String::new();
|
|
|
|
|
|
|
|
// read styles
|
|
|
|
while let Some(tok) = self.tokens.next() {
|
|
|
|
match &tok.kind {
|
|
|
|
TokenKind::Whitespace(_) => {
|
2020-01-08 20:39:05 -05:00
|
|
|
while let Some(Token { kind, .. }) = self.tokens.peek() {
|
|
|
|
match kind {
|
|
|
|
TokenKind::Whitespace(_) | TokenKind::MultilineComment(_) => {
|
2020-01-06 16:50:16 -05:00
|
|
|
self.tokens.next();
|
2020-01-08 20:39:05 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
TokenKind::Ident(ref s) => {
|
|
|
|
if s == &String::from("-") {
|
|
|
|
self.tokens.next();
|
|
|
|
value.push('-');
|
|
|
|
self.devour_whitespace_or_comment();
|
|
|
|
break;
|
|
|
|
}
|
2020-01-06 16:50:16 -05:00
|
|
|
}
|
2020-01-12 10:54:46 -05:00
|
|
|
TokenKind::Interpolation => {
|
|
|
|
self.tokens.next();
|
2020-01-14 17:39:19 -05:00
|
|
|
value.push_str(
|
|
|
|
&eat_interpolation(&mut self.tokens, self.scope)
|
|
|
|
.iter()
|
|
|
|
.map(|x| x.kind.to_string())
|
|
|
|
.collect::<String>(),
|
|
|
|
);
|
2020-01-12 10:54:46 -05:00
|
|
|
break;
|
|
|
|
}
|
2020-01-08 20:39:05 -05:00
|
|
|
_ => {}
|
2020-01-06 16:50:16 -05:00
|
|
|
}
|
|
|
|
value.push(' ');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-01-12 20:56:07 -05:00
|
|
|
TokenKind::Variable(ref v) => value.push_str(
|
|
|
|
&deref_variable(v, self.scope)
|
|
|
|
.iter()
|
|
|
|
.map(|x| x.kind.to_string())
|
|
|
|
.collect::<String>(),
|
|
|
|
),
|
2020-01-08 20:39:05 -05:00
|
|
|
TokenKind::MultilineComment(_) => continue,
|
2020-01-14 17:39:19 -05:00
|
|
|
TokenKind::Interpolation => value.push_str(
|
|
|
|
&eat_interpolation(&mut self.tokens, self.scope)
|
|
|
|
.iter()
|
|
|
|
.map(|x| x.kind.to_string())
|
|
|
|
.collect::<String>(),
|
|
|
|
),
|
2020-01-06 16:50:16 -05:00
|
|
|
_ => value.push_str(&tok.kind.to_string()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Style { property, value }
|
|
|
|
}
|
|
|
|
}
|