2020-01-25 09:58:53 -05:00
|
|
|
#![allow(dead_code, unused_variables)]
|
|
|
|
use std::fmt::{self, Display};
|
|
|
|
use std::iter::{Iterator, Peekable};
|
2020-01-25 20:56:44 -05:00
|
|
|
use std::ops::Add;
|
2020-01-25 09:58:53 -05:00
|
|
|
|
2020-01-25 20:56:44 -05:00
|
|
|
use crate::args::eat_call_args;
|
|
|
|
use crate::builtin::GLOBAL_FUNCTIONS;
|
2020-01-25 09:58:53 -05:00
|
|
|
use crate::color::Color;
|
|
|
|
use crate::common::{Keyword, Op, Scope, Symbol};
|
|
|
|
use crate::units::Unit;
|
2020-01-26 09:28:44 -05:00
|
|
|
use crate::utils::{devour_whitespace_or_comment, eat_interpolation};
|
2020-01-25 09:58:53 -05:00
|
|
|
use crate::{Token, TokenKind};
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
pub(crate) struct Dimension {
|
|
|
|
val: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Add for Dimension {
|
|
|
|
type Output = Self;
|
|
|
|
|
|
|
|
fn add(self, other: Self) -> Self {
|
|
|
|
Dimension {
|
|
|
|
val: self.val + other.val,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for Dimension {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
write!(f, "{}", self.val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
pub(crate) enum ListSeparator {
|
|
|
|
Space,
|
|
|
|
Comma,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ListSeparator {
|
|
|
|
pub fn as_str(self) -> &'static str {
|
|
|
|
match self {
|
|
|
|
Self::Space => " ",
|
|
|
|
Self::Comma => ", ",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn name(self) -> &'static str {
|
|
|
|
match self {
|
|
|
|
Self::Space => "space",
|
|
|
|
Self::Comma => "comma",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for ListSeparator {
|
|
|
|
#[inline]
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
Self::Space => write!(f, " "),
|
|
|
|
Self::Comma => write!(f, ", "),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
|
|
|
pub(crate) enum QuoteKind {
|
|
|
|
Single,
|
|
|
|
Double,
|
|
|
|
None,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl QuoteKind {
|
|
|
|
pub fn as_str(self) -> &'static str {
|
|
|
|
match self {
|
|
|
|
Self::Single => "'",
|
|
|
|
Self::Double => "\"",
|
|
|
|
Self::None => "",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
|
|
|
pub(crate) enum Value {
|
|
|
|
Important,
|
|
|
|
True,
|
|
|
|
False,
|
|
|
|
Null,
|
|
|
|
Dimension(Dimension, Unit),
|
|
|
|
List(Vec<Value>, ListSeparator),
|
|
|
|
Color(Color),
|
2020-01-25 10:54:25 -05:00
|
|
|
BinaryOp(Box<Value>, Op, Box<Value>),
|
2020-01-25 09:58:53 -05:00
|
|
|
Paren(Box<Value>),
|
|
|
|
Ident(String, QuoteKind),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Add for Value {
|
|
|
|
type Output = Self;
|
|
|
|
|
|
|
|
fn add(self, other: Self) -> Self {
|
|
|
|
match self {
|
|
|
|
Self::Important => todo!(),
|
|
|
|
Self::True => todo!(),
|
|
|
|
Self::False => todo!(),
|
|
|
|
Self::Null => todo!(),
|
|
|
|
Self::Dimension(num, unit) => match other {
|
|
|
|
Self::Dimension(num2, unit2) => Value::Dimension(num + num2, unit),
|
|
|
|
_ => todo!(),
|
|
|
|
},
|
|
|
|
Self::List(..) => todo!(),
|
|
|
|
Self::Color(..) => todo!(),
|
2020-01-25 10:54:25 -05:00
|
|
|
Self::BinaryOp(..) => todo!(),
|
2020-01-25 09:58:53 -05:00
|
|
|
Self::Paren(..) => todo!(),
|
|
|
|
Self::Ident(..) => todo!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for Value {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
Self::Important => write!(f, "!important"),
|
|
|
|
Self::Dimension(num, unit) => write!(f, "{}{}", num, unit),
|
|
|
|
Self::List(vals, sep) => write!(
|
|
|
|
f,
|
|
|
|
"{}",
|
|
|
|
vals.iter()
|
|
|
|
.map(|x| x.to_string())
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join(sep.as_str())
|
|
|
|
),
|
|
|
|
Self::Color(c) => write!(f, "{}", c),
|
2020-01-25 10:54:25 -05:00
|
|
|
Self::BinaryOp(lhs, op, rhs) => write!(f, "{}{}{}", lhs, op, rhs),
|
2020-01-25 10:11:46 -05:00
|
|
|
Self::Paren(val) => write!(f, "{}", val),
|
2020-01-25 09:58:53 -05:00
|
|
|
Self::Ident(val, kind) => write!(f, "{}{}{}", kind.as_str(), val, kind.as_str()),
|
|
|
|
Self::True => write!(f, "true"),
|
|
|
|
Self::False => write!(f, "false"),
|
|
|
|
Self::Null => write!(f, "null"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Value {
|
|
|
|
pub fn is_true(&self) -> bool {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn unquote(&mut self) -> &mut Self {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_tokens<I: Iterator<Item = Token>>(
|
|
|
|
toks: &mut Peekable<I>,
|
|
|
|
scope: &Scope,
|
|
|
|
) -> Option<Self> {
|
|
|
|
let left = Self::_from_tokens(toks, scope)?;
|
|
|
|
let whitespace = devour_whitespace_or_comment(toks);
|
|
|
|
let next = match toks.peek() {
|
|
|
|
Some(x) => x,
|
|
|
|
None => return Some(left),
|
|
|
|
};
|
|
|
|
match next.kind {
|
2020-01-25 20:56:44 -05:00
|
|
|
TokenKind::Symbol(Symbol::SemiColon) => Some(left),
|
2020-01-25 09:58:53 -05:00
|
|
|
TokenKind::Symbol(Symbol::Comma) => {
|
|
|
|
toks.next();
|
|
|
|
devour_whitespace_or_comment(toks);
|
|
|
|
let right = match Self::from_tokens(toks, scope) {
|
|
|
|
Some(x) => x,
|
|
|
|
None => return Some(left),
|
|
|
|
};
|
|
|
|
Some(Value::List(vec![left, right], ListSeparator::Comma))
|
|
|
|
}
|
2020-01-25 10:11:46 -05:00
|
|
|
TokenKind::Symbol(Symbol::CloseParen) => Some(left),
|
2020-01-25 10:54:25 -05:00
|
|
|
TokenKind::Symbol(Symbol::Plus)
|
|
|
|
| TokenKind::Symbol(Symbol::Minus)
|
|
|
|
| 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,
|
2020-01-25 20:56:44 -05:00
|
|
|
_ => unsafe { std::hint::unreachable_unchecked() },
|
2020-01-25 10:54:25 -05:00
|
|
|
};
|
2020-01-25 09:58:53 -05:00
|
|
|
toks.next();
|
|
|
|
devour_whitespace_or_comment(toks);
|
|
|
|
let right = match Self::from_tokens(toks, scope) {
|
|
|
|
Some(x) => x,
|
|
|
|
None => return Some(left),
|
|
|
|
};
|
2020-01-25 10:54:25 -05:00
|
|
|
Some(Value::BinaryOp(Box::new(left), op, Box::new(right)))
|
2020-01-25 09:58:53 -05:00
|
|
|
}
|
|
|
|
_ if whitespace => {
|
|
|
|
devour_whitespace_or_comment(toks);
|
|
|
|
let right = match Self::from_tokens(toks, scope) {
|
|
|
|
Some(x) => x,
|
|
|
|
None => return Some(left),
|
|
|
|
};
|
|
|
|
Some(Value::List(vec![left, right], ListSeparator::Space))
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
dbg!(&next.kind);
|
|
|
|
todo!("unimplemented token in value")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn _from_tokens<I: Iterator<Item = Token>>(
|
|
|
|
toks: &mut Peekable<I>,
|
|
|
|
scope: &Scope,
|
|
|
|
) -> Option<Self> {
|
|
|
|
let kind = if let Some(tok) = toks.next() {
|
|
|
|
tok.kind
|
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
};
|
|
|
|
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
|
|
|
|
};
|
|
|
|
Some(Value::Dimension(
|
|
|
|
Dimension {
|
|
|
|
val: val.parse().unwrap(),
|
|
|
|
},
|
|
|
|
unit,
|
|
|
|
))
|
|
|
|
}
|
2020-01-25 10:11:46 -05:00
|
|
|
TokenKind::Symbol(Symbol::OpenParen) => {
|
|
|
|
devour_whitespace_or_comment(toks);
|
|
|
|
let val = Self::from_tokens(toks, scope)?;
|
|
|
|
assert_eq!(
|
|
|
|
toks.next().unwrap().kind,
|
|
|
|
TokenKind::Symbol(Symbol::CloseParen)
|
|
|
|
);
|
|
|
|
Some(Value::Paren(Box::new(val)))
|
|
|
|
}
|
2020-01-25 09:58:53 -05:00
|
|
|
TokenKind::Ident(mut s) => {
|
|
|
|
while let Some(tok) = toks.peek() {
|
|
|
|
match tok.kind.clone() {
|
|
|
|
TokenKind::Interpolation => {
|
|
|
|
toks.next();
|
|
|
|
s.push_str(
|
|
|
|
&eat_interpolation(toks, scope)
|
|
|
|
.iter()
|
|
|
|
.map(|x| x.kind.to_string())
|
|
|
|
.collect::<String>(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
TokenKind::Ident(ref i) => {
|
|
|
|
toks.next();
|
|
|
|
s.push_str(i)
|
|
|
|
}
|
|
|
|
_ => break,
|
|
|
|
}
|
|
|
|
}
|
2020-01-25 20:56:44 -05:00
|
|
|
match toks.peek() {
|
|
|
|
Some(Token {
|
|
|
|
kind: TokenKind::Symbol(Symbol::OpenParen),
|
|
|
|
..
|
|
|
|
}) => {
|
|
|
|
toks.next();
|
2020-01-26 09:28:44 -05:00
|
|
|
let args = eat_call_args(toks, scope);
|
2020-01-25 20:56:44 -05:00
|
|
|
let func = match scope.functions.get(&s) {
|
|
|
|
Some(f) => f,
|
|
|
|
None => match GLOBAL_FUNCTIONS.get(&s) {
|
2020-01-25 23:33:45 -05:00
|
|
|
Some(f) => return Some(f(&args)),
|
2020-01-25 20:56:44 -05:00
|
|
|
None => todo!("called undefined function"),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
Some(func.clone().args(&args).call())
|
|
|
|
}
|
|
|
|
_ => Some(Value::Ident(s, QuoteKind::None)),
|
|
|
|
}
|
2020-01-25 09:58:53 -05:00
|
|
|
}
|
|
|
|
TokenKind::Symbol(Symbol::DoubleQuote) => {
|
|
|
|
let mut s = String::new();
|
|
|
|
while let Some(tok) = toks.next() {
|
2020-01-25 13:25:38 -05:00
|
|
|
if tok.kind == TokenKind::Symbol(Symbol::DoubleQuote) {
|
|
|
|
break;
|
2020-01-25 09:58:53 -05:00
|
|
|
}
|
|
|
|
s.push_str(&tok.kind.to_string());
|
|
|
|
}
|
|
|
|
Some(Value::Ident(s, QuoteKind::Double))
|
|
|
|
}
|
|
|
|
TokenKind::Symbol(Symbol::SingleQuote) => {
|
|
|
|
let mut s = String::new();
|
|
|
|
while let Some(tok) = toks.next() {
|
2020-01-25 13:25:38 -05:00
|
|
|
if tok.kind == TokenKind::Symbol(Symbol::SingleQuote) {
|
|
|
|
break;
|
2020-01-25 09:58:53 -05:00
|
|
|
}
|
|
|
|
s.push_str(&tok.kind.to_string());
|
|
|
|
}
|
|
|
|
Some(Value::Ident(s, QuoteKind::Single))
|
|
|
|
}
|
2020-01-26 09:28:44 -05:00
|
|
|
TokenKind::Variable(ref v) => Some(scope.vars.get(v).unwrap().clone()),
|
2020-01-25 09:58:53 -05:00
|
|
|
TokenKind::Interpolation => {
|
|
|
|
let mut s = eat_interpolation(toks, scope)
|
|
|
|
.iter()
|
|
|
|
.map(|x| x.kind.to_string())
|
|
|
|
.collect::<String>();
|
|
|
|
while let Some(tok) = toks.peek() {
|
|
|
|
match tok.kind.clone() {
|
|
|
|
TokenKind::Interpolation => {
|
|
|
|
toks.next();
|
|
|
|
s.push_str(
|
|
|
|
&eat_interpolation(toks, scope)
|
|
|
|
.iter()
|
|
|
|
.map(|x| x.kind.to_string())
|
|
|
|
.collect::<String>(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
TokenKind::Ident(ref i) => {
|
|
|
|
toks.next();
|
|
|
|
s.push_str(i)
|
|
|
|
}
|
|
|
|
_ => break,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(Value::Ident(s, QuoteKind::None))
|
|
|
|
}
|
|
|
|
TokenKind::Keyword(Keyword::Important) => Some(Value::Important),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|