MVP of mixin args
This commit is contained in:
parent
e1191d7108
commit
b05301f442
@ -487,3 +487,10 @@ impl Scope {
|
|||||||
self.mixins.extend(other.mixins);
|
self.mixins.extend(other.mixins);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub(crate) enum Printer {
|
||||||
|
Error(Pos, String),
|
||||||
|
Warn(Pos, String),
|
||||||
|
Debug(Pos, String),
|
||||||
|
}
|
||||||
|
134
src/main.rs
134
src/main.rs
@ -31,12 +31,12 @@ use std::io;
|
|||||||
use std::iter::{Iterator, Peekable};
|
use std::iter::{Iterator, Peekable};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
use crate::common::{AtRule, Keyword, Op, Pos, Scope, Symbol, Whitespace};
|
use crate::common::{AtRule, Keyword, Op, Pos, Printer, Scope, Symbol, Whitespace};
|
||||||
use crate::css::Css;
|
use crate::css::Css;
|
||||||
use crate::error::SassError;
|
use crate::error::SassError;
|
||||||
use crate::format::PrettyPrinter;
|
use crate::format::PrettyPrinter;
|
||||||
use crate::lexer::Lexer;
|
use crate::lexer::Lexer;
|
||||||
use crate::mixin::{FuncArgs, Mixin};
|
use crate::mixin::{CallArg, CallArgs, FuncArg, FuncArgs, Mixin};
|
||||||
use crate::selector::{Attribute, Selector};
|
use crate::selector::{Attribute, Selector};
|
||||||
use crate::style::Style;
|
use crate::style::Style;
|
||||||
use crate::units::Unit;
|
use crate::units::Unit;
|
||||||
@ -296,8 +296,6 @@ impl<'a> StyleSheetParser<'a> {
|
|||||||
Ok(StyleSheet { rules })
|
Ok(StyleSheet { rules })
|
||||||
}
|
}
|
||||||
|
|
||||||
fn eat_func_call(&mut self) {}
|
|
||||||
|
|
||||||
fn eat_rules(&mut self, super_selector: &Selector, scope: &mut Scope) -> Vec<Stmt> {
|
fn eat_rules(&mut self, super_selector: &Selector, scope: &mut Scope) -> Vec<Stmt> {
|
||||||
let mut stmts = Vec::new();
|
let mut stmts = Vec::new();
|
||||||
while let Ok(tok) = eat_expr(&mut self.lexer, scope, super_selector) {
|
while let Ok(tok) = eat_expr(&mut self.lexer, scope, super_selector) {
|
||||||
@ -337,7 +335,7 @@ impl<'a> StyleSheetParser<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn eat_include<I: std::iter::Iterator<Item = Token>>(
|
fn eat_include<I: Iterator<Item = Token>>(
|
||||||
toks: &mut Peekable<I>,
|
toks: &mut Peekable<I>,
|
||||||
scope: &Scope,
|
scope: &Scope,
|
||||||
super_selector: &Selector,
|
super_selector: &Selector,
|
||||||
@ -353,34 +351,129 @@ fn eat_include<I: std::iter::Iterator<Item = Token>>(
|
|||||||
|
|
||||||
devour_whitespace(toks);
|
devour_whitespace(toks);
|
||||||
|
|
||||||
match toks.next() {
|
let args = match toks.next() {
|
||||||
Some(Token {
|
Some(Token {
|
||||||
kind: TokenKind::Symbol(Symbol::SemiColon),
|
kind: TokenKind::Symbol(Symbol::SemiColon),
|
||||||
..
|
..
|
||||||
}) => {}
|
}) => CallArgs::new(),
|
||||||
Some(Token {
|
Some(Token {
|
||||||
kind: TokenKind::Symbol(Symbol::OpenParen),
|
kind: TokenKind::Symbol(Symbol::OpenParen),
|
||||||
..
|
..
|
||||||
}) => {}
|
}) => eat_call_args(toks),
|
||||||
Some(Token { pos, .. }) => return Err((pos, "expected `(` or `;`")),
|
Some(Token { pos, .. }) => return Err((pos, "expected `(` or `;`")),
|
||||||
None => return Err((pos, "unexpected EOF")),
|
None => return Err((pos, "unexpected EOF")),
|
||||||
|
};
|
||||||
|
|
||||||
|
devour_whitespace(toks);
|
||||||
|
|
||||||
|
if !args.is_empty() {
|
||||||
|
if let Some(tok) = toks.next() {
|
||||||
|
assert_eq!(tok.kind, TokenKind::Symbol(Symbol::SemiColon));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
devour_whitespace(toks);
|
||||||
|
|
||||||
let mut mixin = if let Some(m) = scope.mixins.get(&name) {
|
let mut mixin = if let Some(m) = scope.mixins.get(&name) {
|
||||||
m.clone()
|
m.clone()
|
||||||
} else {
|
} else {
|
||||||
return Err((pos, "expected identifier"));
|
return Err((pos, "expected identifier"));
|
||||||
};
|
};
|
||||||
let rules = mixin.eval(super_selector, &mut scope.clone());
|
let rules = mixin.call_with_args(&args).eval(super_selector, &mut scope.clone());
|
||||||
devour_whitespace(toks);
|
|
||||||
Ok(rules)
|
Ok(rules)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn eat_func_args() -> FuncArgs {
|
fn eat_func_args<I: Iterator<Item = Token>>(toks: &mut Peekable<I>) -> FuncArgs {
|
||||||
todo!()
|
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!(),
|
||||||
|
};
|
||||||
|
devour_whitespace(toks);
|
||||||
|
let kind = if let Some(Token { kind, .. }) = toks.next() {
|
||||||
|
kind
|
||||||
|
} else {
|
||||||
|
todo!()
|
||||||
|
};
|
||||||
|
match kind {
|
||||||
|
TokenKind::Symbol(Symbol::Colon) => {
|
||||||
|
todo!("handle default values")
|
||||||
|
// let mut val: Vec<Token> = Vec::new();
|
||||||
|
// while let Some(tok) = toks.next() {
|
||||||
|
// match &kind {
|
||||||
|
// _ => val.push(tok),
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
TokenKind::Symbol(Symbol::Period) => todo!("handle varargs"),
|
||||||
|
TokenKind::Symbol(Symbol::CloseParen) => {
|
||||||
|
args.push(FuncArg {
|
||||||
|
name,
|
||||||
|
default: None,
|
||||||
|
});
|
||||||
|
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 {
|
||||||
|
todo!("expected `{{` after mixin args")
|
||||||
|
}
|
||||||
|
FuncArgs(args)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_mixin<I: std::iter::Iterator<Item = Token>>(
|
fn eat_call_args<I: Iterator<Item = Token>>(toks: &mut Peekable<I>) -> CallArgs {
|
||||||
|
let mut args: Vec<CallArg> = Vec::new();
|
||||||
|
devour_whitespace(toks);
|
||||||
|
let mut name: Option<String> = None;
|
||||||
|
let mut val = Vec::new();
|
||||||
|
while let Some(Token { kind, pos }) = toks.next() {
|
||||||
|
match kind {
|
||||||
|
TokenKind::Variable(v) => name = Some(v),
|
||||||
|
TokenKind::Symbol(Symbol::Colon) => {
|
||||||
|
todo!("handle default values")
|
||||||
|
// let mut val: Vec<Token> = Vec::new();
|
||||||
|
// while let Some(Token { kind, .. }) = toks.next() {
|
||||||
|
// match &kind {
|
||||||
|
// _ => {}
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
TokenKind::Symbol(Symbol::CloseParen) => {
|
||||||
|
args.push(CallArg {
|
||||||
|
name: name.clone(),
|
||||||
|
val: val.clone(),
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
},
|
||||||
|
TokenKind::Symbol(Symbol::Comma) => {
|
||||||
|
args.push(CallArg {
|
||||||
|
name: name.clone(),
|
||||||
|
val: val.clone(),
|
||||||
|
});
|
||||||
|
if let Some(ref mut s) = name {
|
||||||
|
s.clear();
|
||||||
|
}
|
||||||
|
val.clear();
|
||||||
|
}
|
||||||
|
_ => val.push(Token { kind, pos }),
|
||||||
|
}
|
||||||
|
devour_whitespace(toks);
|
||||||
|
}
|
||||||
|
CallArgs(args)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_mixin<I: Iterator<Item = Token>>(
|
||||||
toks: &mut Peekable<I>,
|
toks: &mut Peekable<I>,
|
||||||
scope: Scope,
|
scope: Scope,
|
||||||
) -> Result<(String, Mixin), Printer> {
|
) -> Result<(String, Mixin), Printer> {
|
||||||
@ -403,7 +496,7 @@ fn parse_mixin<I: std::iter::Iterator<Item = Token>>(
|
|||||||
Some(Token {
|
Some(Token {
|
||||||
kind: TokenKind::Symbol(Symbol::OpenParen),
|
kind: TokenKind::Symbol(Symbol::OpenParen),
|
||||||
..
|
..
|
||||||
}) => eat_func_args(),
|
}) => eat_func_args(toks),
|
||||||
Some(Token {
|
Some(Token {
|
||||||
kind: TokenKind::Symbol(Symbol::OpenCurlyBrace),
|
kind: TokenKind::Symbol(Symbol::OpenCurlyBrace),
|
||||||
..
|
..
|
||||||
@ -430,14 +523,7 @@ fn parse_mixin<I: std::iter::Iterator<Item = Token>>(
|
|||||||
Ok((name, Mixin::new(scope, args, body)))
|
Ok((name, Mixin::new(scope, args, body)))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
fn eat_at_rule<I: Iterator<Item = Token>>(
|
||||||
enum Printer {
|
|
||||||
Error(Pos, String),
|
|
||||||
Warn(Pos, String),
|
|
||||||
Debug(Pos, String),
|
|
||||||
}
|
|
||||||
|
|
||||||
fn eat_at_rule<I: std::iter::Iterator<Item = Token>>(
|
|
||||||
rule: AtRule,
|
rule: AtRule,
|
||||||
pos: Pos,
|
pos: Pos,
|
||||||
toks: &mut Peekable<I>,
|
toks: &mut Peekable<I>,
|
||||||
@ -478,7 +564,7 @@ fn eat_at_rule<I: std::iter::Iterator<Item = Token>>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn eat_expr<I: std::iter::Iterator<Item = Token>>(
|
pub(crate) fn eat_expr<I: Iterator<Item = Token>>(
|
||||||
toks: &mut Peekable<I>,
|
toks: &mut Peekable<I>,
|
||||||
scope: &Scope,
|
scope: &Scope,
|
||||||
super_selector: &Selector,
|
super_selector: &Selector,
|
||||||
@ -569,7 +655,7 @@ pub(crate) fn eat_expr<I: std::iter::Iterator<Item = Token>>(
|
|||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
if let Some(tok) = toks.next() {
|
if let Some(tok) = toks.next() {
|
||||||
values.push(tok.clone())
|
values.push(tok)
|
||||||
} else {
|
} else {
|
||||||
unsafe { std::hint::unreachable_unchecked() }
|
unsafe { std::hint::unreachable_unchecked() }
|
||||||
}
|
}
|
||||||
|
45
src/mixin.rs
45
src/mixin.rs
@ -23,8 +23,21 @@ impl Mixin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn call_with_args(&mut self, args: &CallArgs) -> &mut Mixin {
|
||||||
|
for (idx, arg) in args.0.iter().enumerate() {
|
||||||
|
if arg.is_named() {
|
||||||
|
todo!("keyword args")
|
||||||
|
} else {
|
||||||
|
// dbg!(&self.args.0[idx].name.clone());
|
||||||
|
self.scope.vars.insert(self.args.0[idx].name.clone(), arg.val.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
pub fn eval(&mut self, super_selector: &Selector, scope: &mut Scope) -> Vec<Stmt> {
|
pub fn eval(&mut self, super_selector: &Selector, scope: &mut Scope) -> Vec<Stmt> {
|
||||||
let mut stmts = Vec::new();
|
let mut stmts = Vec::new();
|
||||||
|
// dbg!(&scope);
|
||||||
while let Ok(expr) = eat_expr(&mut self.body, scope, super_selector) {
|
while let Ok(expr) = eat_expr(&mut self.body, scope, super_selector) {
|
||||||
match expr {
|
match expr {
|
||||||
Expr::Style(s) => stmts.push(Stmt::Style(s)),
|
Expr::Style(s) => stmts.push(Stmt::Style(s)),
|
||||||
@ -56,7 +69,13 @@ impl Mixin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
||||||
pub struct FuncArgs(pub Vec<(Option<String>, Vec<Token>)>);
|
pub struct FuncArgs(pub Vec<FuncArg>);
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
||||||
|
pub struct FuncArg {
|
||||||
|
pub name: String,
|
||||||
|
pub default: Option<Vec<Token>>,
|
||||||
|
}
|
||||||
|
|
||||||
impl FuncArgs {
|
impl FuncArgs {
|
||||||
pub const fn new() -> Self {
|
pub const fn new() -> Self {
|
||||||
@ -65,4 +84,26 @@ impl FuncArgs {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct CallArgs(Vec<(Option<String>, Vec<Token>)>);
|
pub struct CallArgs(pub Vec<CallArg>);
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct CallArg {
|
||||||
|
pub name: Option<String>,
|
||||||
|
pub val: Vec<Token>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CallArg {
|
||||||
|
pub fn is_named(&self) -> bool {
|
||||||
|
self.name.is_some()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CallArgs {
|
||||||
|
pub const fn new() -> Self {
|
||||||
|
CallArgs(Vec::new())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_empty(&self) -> bool {
|
||||||
|
self.0.is_empty()
|
||||||
|
}
|
||||||
|
}
|
12
src/units.rs
12
src/units.rs
@ -126,7 +126,8 @@ impl TryFrom<&str> for Unit {
|
|||||||
b"kHz" => Ok(Unit::Khz),
|
b"kHz" => Ok(Unit::Khz),
|
||||||
b"dpi" => Ok(Unit::Dpi),
|
b"dpi" => Ok(Unit::Dpi),
|
||||||
b"dpcm" => Ok(Unit::Dpcm),
|
b"dpcm" => Ok(Unit::Dpcm),
|
||||||
b"dppx" | b"x" => Ok(Unit::Dppx),
|
b"dppx" => Ok(Unit::Dppx),
|
||||||
|
b"x" => Ok(Unit::X),
|
||||||
b"fr" => Ok(Unit::Fr),
|
b"fr" => Ok(Unit::Fr),
|
||||||
_ => Err("invalid unit"),
|
_ => Err("invalid unit"),
|
||||||
}
|
}
|
||||||
@ -168,7 +169,8 @@ impl Into<String> for Unit {
|
|||||||
Unit::Khz => "kHz",
|
Unit::Khz => "kHz",
|
||||||
Unit::Dpi => "dpi",
|
Unit::Dpi => "dpi",
|
||||||
Unit::Dpcm => "dpcm",
|
Unit::Dpcm => "dpcm",
|
||||||
Unit::Dppx | Unit::X => "dppx",
|
Unit::Dppx => "dppx",
|
||||||
|
Unit::X => "x",
|
||||||
Unit::Fr => "fr",
|
Unit::Fr => "fr",
|
||||||
Unit::None => "",
|
Unit::None => "",
|
||||||
}
|
}
|
||||||
@ -211,7 +213,8 @@ impl Into<&'static str> for Unit {
|
|||||||
Unit::Khz => "kHz",
|
Unit::Khz => "kHz",
|
||||||
Unit::Dpi => "dpi",
|
Unit::Dpi => "dpi",
|
||||||
Unit::Dpcm => "dpcm",
|
Unit::Dpcm => "dpcm",
|
||||||
Unit::Dppx | Unit::X => "dppx",
|
Unit::Dppx => "dppx",
|
||||||
|
Unit::X => "x",
|
||||||
Unit::Fr => "fr",
|
Unit::Fr => "fr",
|
||||||
Unit::None => "",
|
Unit::None => "",
|
||||||
}
|
}
|
||||||
@ -253,7 +256,8 @@ impl fmt::Display for Unit {
|
|||||||
Unit::Khz => write!(f, "kHz"),
|
Unit::Khz => write!(f, "kHz"),
|
||||||
Unit::Dpi => write!(f, "dpi"),
|
Unit::Dpi => write!(f, "dpi"),
|
||||||
Unit::Dpcm => write!(f, "dpcm"),
|
Unit::Dpcm => write!(f, "dpcm"),
|
||||||
Unit::Dppx | Unit::X => write!(f, "dppx"),
|
Unit::Dppx => write!(f, "dppx"),
|
||||||
|
Unit::X => write!(f, "x"),
|
||||||
Unit::Fr => write!(f, "fr"),
|
Unit::Fr => write!(f, "fr"),
|
||||||
Unit::None => write!(f, ""),
|
Unit::None => write!(f, ""),
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user