2020-01-25 11:00:29 -05:00
|
|
|
use std::collections::BTreeMap;
|
|
|
|
use std::iter::Peekable;
|
|
|
|
|
2020-01-26 09:13:39 -05:00
|
|
|
use crate::common::{Scope, Symbol};
|
2020-02-16 21:34:52 -05:00
|
|
|
use crate::error::SassResult;
|
2020-01-26 13:53:18 -05:00
|
|
|
use crate::utils::{devour_whitespace, devour_whitespace_or_comment};
|
2020-01-26 09:13:39 -05:00
|
|
|
use crate::value::Value;
|
2020-01-25 11:00:29 -05:00
|
|
|
use crate::{Token, TokenKind};
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
|
|
|
pub(crate) struct FuncArgs(pub Vec<FuncArg>);
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
|
|
|
pub(crate) struct FuncArg {
|
|
|
|
pub name: String,
|
2020-01-26 09:13:39 -05:00
|
|
|
pub default: Option<Value>,
|
2020-01-25 11:00:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FuncArgs {
|
|
|
|
pub const fn new() -> Self {
|
|
|
|
FuncArgs(Vec::new())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, std::default::Default)]
|
2020-01-26 09:13:39 -05:00
|
|
|
pub(crate) struct CallArgs(pub BTreeMap<String, Value>);
|
2020-01-25 11:00:29 -05:00
|
|
|
|
|
|
|
impl CallArgs {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
CallArgs(BTreeMap::new())
|
|
|
|
}
|
|
|
|
|
2020-01-26 09:13:39 -05:00
|
|
|
pub fn get(&self, val: &str) -> Option<&Value> {
|
2020-01-25 11:00:29 -05:00
|
|
|
self.0.get(val)
|
|
|
|
}
|
2020-02-14 11:52:31 -05:00
|
|
|
|
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.0.len()
|
|
|
|
}
|
2020-02-16 22:04:54 -05:00
|
|
|
|
|
|
|
pub fn remove(&mut self, s: &str) -> Option<Value> {
|
|
|
|
self.0.remove(s)
|
|
|
|
}
|
2020-01-25 11:00:29 -05:00
|
|
|
}
|
|
|
|
|
2020-01-26 09:13:39 -05:00
|
|
|
pub(crate) fn eat_func_args<I: Iterator<Item = Token>>(
|
|
|
|
toks: &mut Peekable<I>,
|
|
|
|
scope: &Scope,
|
2020-02-17 09:28:25 -05:00
|
|
|
) -> SassResult<FuncArgs> {
|
2020-01-25 11:00:29 -05:00
|
|
|
let mut args: Vec<FuncArg> = Vec::new();
|
|
|
|
|
|
|
|
devour_whitespace(toks);
|
|
|
|
while let Some(Token { kind, .. }) = toks.next() {
|
|
|
|
let name = match kind {
|
|
|
|
TokenKind::Variable(v) => v,
|
|
|
|
TokenKind::Symbol(Symbol::CloseParen) => break,
|
|
|
|
_ => todo!(),
|
|
|
|
};
|
|
|
|
let mut default: Vec<Token> = Vec::new();
|
|
|
|
devour_whitespace(toks);
|
|
|
|
let kind = match toks.next() {
|
|
|
|
Some(Token { kind, .. }) => kind,
|
|
|
|
_ => todo!("unexpected eof"),
|
|
|
|
};
|
|
|
|
match kind {
|
|
|
|
TokenKind::Symbol(Symbol::Colon) => {
|
|
|
|
devour_whitespace(toks);
|
|
|
|
while let Some(tok) = toks.peek() {
|
|
|
|
match &tok.kind {
|
|
|
|
TokenKind::Symbol(Symbol::Comma) => {
|
|
|
|
toks.next();
|
|
|
|
args.push(FuncArg {
|
|
|
|
name,
|
2020-02-17 09:28:25 -05:00
|
|
|
default: Some(Value::from_tokens(
|
|
|
|
&mut default.into_iter().peekable(),
|
|
|
|
scope,
|
|
|
|
)?),
|
2020-01-25 11:00:29 -05:00
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
TokenKind::Symbol(Symbol::CloseParen) => {
|
|
|
|
args.push(FuncArg {
|
|
|
|
name,
|
2020-02-17 09:28:25 -05:00
|
|
|
default: Some(Value::from_tokens(
|
|
|
|
&mut default.into_iter().peekable(),
|
|
|
|
scope,
|
|
|
|
)?),
|
2020-01-25 11:00:29 -05:00
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
let tok = toks.next().expect("we know this exists!");
|
|
|
|
default.push(tok)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TokenKind::Symbol(Symbol::Period) => todo!("handle varargs"),
|
|
|
|
TokenKind::Symbol(Symbol::CloseParen) => {
|
|
|
|
args.push(FuncArg {
|
|
|
|
name,
|
|
|
|
default: if default.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
2020-02-17 09:28:25 -05:00
|
|
|
Some(Value::from_tokens(
|
|
|
|
&mut default.into_iter().peekable(),
|
|
|
|
scope,
|
|
|
|
)?)
|
2020-01-25 11:00:29 -05:00
|
|
|
},
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
TokenKind::Symbol(Symbol::Comma) => args.push(FuncArg {
|
|
|
|
name,
|
|
|
|
default: None,
|
|
|
|
}),
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
devour_whitespace(toks);
|
|
|
|
}
|
|
|
|
devour_whitespace(toks);
|
|
|
|
if let Some(Token {
|
|
|
|
kind: TokenKind::Symbol(Symbol::OpenCurlyBrace),
|
|
|
|
..
|
|
|
|
}) = toks.next()
|
|
|
|
{
|
|
|
|
} else {
|
2020-01-25 12:46:51 -05:00
|
|
|
todo!("expected `{{` after args")
|
2020-01-25 11:00:29 -05:00
|
|
|
}
|
2020-02-17 09:28:25 -05:00
|
|
|
Ok(FuncArgs(args))
|
2020-01-25 11:00:29 -05:00
|
|
|
}
|
|
|
|
|
2020-01-26 09:13:39 -05:00
|
|
|
pub(crate) fn eat_call_args<I: Iterator<Item = Token>>(
|
|
|
|
toks: &mut Peekable<I>,
|
|
|
|
scope: &Scope,
|
2020-02-16 21:34:52 -05:00
|
|
|
) -> SassResult<CallArgs> {
|
2020-01-26 09:13:39 -05:00
|
|
|
let mut args: BTreeMap<String, Value> = BTreeMap::new();
|
2020-01-26 13:53:18 -05:00
|
|
|
devour_whitespace_or_comment(toks);
|
2020-02-16 21:34:52 -05:00
|
|
|
let mut name: String;
|
|
|
|
let mut val: Vec<Token> = Vec::new();
|
|
|
|
loop {
|
|
|
|
match toks.peek().unwrap().kind {
|
|
|
|
TokenKind::Variable(_) => {
|
|
|
|
let v = toks.next().unwrap();
|
2020-01-26 13:53:18 -05:00
|
|
|
devour_whitespace_or_comment(toks);
|
2020-02-28 01:01:31 -05:00
|
|
|
if toks.next().unwrap().is_symbol(Symbol::Colon) {
|
2020-02-16 21:34:52 -05:00
|
|
|
name = v.kind.to_string();
|
|
|
|
} else {
|
|
|
|
val.push(v);
|
|
|
|
name = args.len().to_string();
|
2020-01-26 09:13:39 -05:00
|
|
|
}
|
|
|
|
}
|
2020-02-16 21:34:52 -05:00
|
|
|
TokenKind::Symbol(Symbol::CloseParen) => {
|
|
|
|
toks.next();
|
|
|
|
return Ok(CallArgs(args));
|
2020-01-25 11:00:29 -05:00
|
|
|
}
|
2020-02-16 21:34:52 -05:00
|
|
|
_ => name = args.len().to_string(),
|
|
|
|
}
|
|
|
|
devour_whitespace_or_comment(toks);
|
|
|
|
|
|
|
|
while let Some(tok) = toks.next() {
|
|
|
|
match tok.kind {
|
|
|
|
TokenKind::Symbol(Symbol::CloseParen) => {
|
|
|
|
args.insert(
|
|
|
|
name,
|
2020-02-22 17:57:13 -05:00
|
|
|
Value::from_tokens(&mut val.into_iter().peekable(), scope)?,
|
2020-02-16 21:34:52 -05:00
|
|
|
);
|
|
|
|
return Ok(CallArgs(args));
|
|
|
|
}
|
|
|
|
TokenKind::Symbol(Symbol::Comma) => break,
|
|
|
|
TokenKind::Symbol(Symbol::OpenParen) => {
|
2020-02-09 14:27:54 -05:00
|
|
|
val.push(tok);
|
2020-02-16 21:34:52 -05:00
|
|
|
val.extend(read_until_close_paren(toks));
|
2020-02-09 14:27:54 -05:00
|
|
|
}
|
2020-02-16 21:34:52 -05:00
|
|
|
_ => val.push(tok),
|
2020-02-09 14:27:54 -05:00
|
|
|
}
|
2020-02-16 21:34:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
args.insert(
|
|
|
|
name,
|
|
|
|
Value::from_tokens(&mut val.clone().into_iter().peekable(), scope)?,
|
|
|
|
);
|
|
|
|
val.clear();
|
|
|
|
devour_whitespace_or_comment(toks);
|
|
|
|
|
|
|
|
if toks.peek().is_none() {
|
|
|
|
return Ok(CallArgs(args));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_until_close_paren<I: Iterator<Item = Token>>(toks: &mut Peekable<I>) -> Vec<Token> {
|
|
|
|
let mut v = Vec::new();
|
|
|
|
let mut scope = 0;
|
2020-02-22 17:57:13 -05:00
|
|
|
for tok in toks {
|
2020-02-16 21:34:52 -05:00
|
|
|
match tok.kind {
|
2020-01-25 11:00:29 -05:00
|
|
|
TokenKind::Symbol(Symbol::CloseParen) => {
|
2020-02-16 21:34:52 -05:00
|
|
|
if scope <= 1 {
|
|
|
|
v.push(tok);
|
|
|
|
return v;
|
|
|
|
} else {
|
|
|
|
scope -= 1;
|
2020-01-25 11:00:29 -05:00
|
|
|
}
|
|
|
|
}
|
2020-02-16 21:34:52 -05:00
|
|
|
TokenKind::Symbol(Symbol::OpenParen) => scope += 1,
|
|
|
|
_ => {}
|
2020-01-25 11:00:29 -05:00
|
|
|
}
|
2020-02-16 21:34:52 -05:00
|
|
|
v.push(tok)
|
2020-01-25 11:00:29 -05:00
|
|
|
}
|
2020-02-16 21:34:52 -05:00
|
|
|
v
|
2020-01-25 11:00:29 -05:00
|
|
|
}
|