This commit is contained in:
ConnorSkees 2020-02-22 17:57:13 -05:00
parent 4ee9cc72a6
commit 53861ccb0d
8 changed files with 26 additions and 26 deletions

View File

@ -163,7 +163,7 @@ pub(crate) fn eat_call_args<I: Iterator<Item = Token>>(
TokenKind::Symbol(Symbol::CloseParen) => { TokenKind::Symbol(Symbol::CloseParen) => {
args.insert( args.insert(
name, name,
Value::from_tokens(&mut val.clone().into_iter().peekable(), scope)?, Value::from_tokens(&mut val.into_iter().peekable(), scope)?,
); );
return Ok(CallArgs(args)); return Ok(CallArgs(args));
} }
@ -192,7 +192,7 @@ pub(crate) fn eat_call_args<I: Iterator<Item = Token>>(
fn read_until_close_paren<I: Iterator<Item = Token>>(toks: &mut Peekable<I>) -> Vec<Token> { fn read_until_close_paren<I: Iterator<Item = Token>>(toks: &mut Peekable<I>) -> Vec<Token> {
let mut v = Vec::new(); let mut v = Vec::new();
let mut scope = 0; let mut scope = 0;
while let Some(tok) = toks.next() { for tok in toks {
match tok.kind { match tok.kind {
TokenKind::Symbol(Symbol::CloseParen) => { TokenKind::Symbol(Symbol::CloseParen) => {
if scope <= 1 { if scope <= 1 {

View File

@ -157,7 +157,7 @@ fn eat_unknown_atrule_body<I: Iterator<Item = Token>>(
while let Some(expr) = eat_expr(toks, scope, super_selector)? { while let Some(expr) = eat_expr(toks, scope, super_selector)? {
match expr { match expr {
Expr::AtRule(a) => stmts.push(Stmt::AtRule(a)), Expr::AtRule(a) => stmts.push(Stmt::AtRule(a)),
Expr::Style(s) => stmts.push(Stmt::Style(s)), Expr::Style(s) => stmts.push(Stmt::Style(*s)),
Expr::Styles(s) => stmts.extend(s.into_iter().map(Stmt::Style)), Expr::Styles(s) => stmts.extend(s.into_iter().map(Stmt::Style)),
Expr::Include(s) => stmts.extend(s), Expr::Include(s) => stmts.extend(s),
Expr::MixinDecl(..) | Expr::FunctionDecl(..) | Expr::Debug(..) | Expr::Warn(..) => { Expr::MixinDecl(..) | Expr::FunctionDecl(..) | Expr::Debug(..) | Expr::Warn(..) => {

View File

@ -12,34 +12,34 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
v @ Value::Dimension(..) => return Err(format!("$number: Expected {} to have no units.", v).into()), v @ Value::Dimension(..) => return Err(format!("$number: Expected {} to have no units.", v).into()),
v => return Err(format!("$number: {} is not a number.", v).into()), v => return Err(format!("$number: {} is not a number.", v).into()),
}; };
Ok(Value::Dimension(num, Unit::Percent)) Ok(Value::Dimension(num, Unit::Percent))
}); });
decl!(f "round", |args, _| { decl!(f "round", |args, _| {
max_args!(args, 1); max_args!(args, 1);
match arg!(args, 0, "number") { match arg!(args, 0, "number") {
Value::Dimension(n, u) => Ok(Value::Dimension(n.round(), u)), Value::Dimension(n, u) => Ok(Value::Dimension(n.round(), u)),
v => return Err(format!("$number: {} is not a number.", v).into()), v => Err(format!("$number: {} is not a number.", v).into()),
} }
}); });
decl!(f "ceil", |args, _| { decl!(f "ceil", |args, _| {
max_args!(args, 1); max_args!(args, 1);
match arg!(args, 0, "number") { match arg!(args, 0, "number") {
Value::Dimension(n, u) => Ok(Value::Dimension(n.ceil(), u)), Value::Dimension(n, u) => Ok(Value::Dimension(n.ceil(), u)),
v => return Err(format!("$number: {} is not a number.", v).into()), v => Err(format!("$number: {} is not a number.", v).into()),
} }
}); });
decl!(f "floor", |args, _| { decl!(f "floor", |args, _| {
max_args!(args, 1); max_args!(args, 1);
match arg!(args, 0, "number") { match arg!(args, 0, "number") {
Value::Dimension(n, u) => Ok(Value::Dimension(n.floor(), u)), Value::Dimension(n, u) => Ok(Value::Dimension(n.floor(), u)),
v => return Err(format!("$number: {} is not a number.", v).into()), v => Err(format!("$number: {} is not a number.", v).into()),
} }
}); });
decl!(f "abs", |args, _| { decl!(f "abs", |args, _| {
max_args!(args, 1); max_args!(args, 1);
match arg!(args, 0, "number") { match arg!(args, 0, "number") {
Value::Dimension(n, u) => Ok(Value::Dimension(n.abs(), u)), Value::Dimension(n, u) => Ok(Value::Dimension(n.abs(), u)),
v => return Err(format!("$number: {} is not a number.", v).into()), v => Err(format!("$number: {} is not a number.", v).into()),
} }
}); });
decl!(f "comparable", |args, _| { decl!(f "comparable", |args, _| {
@ -53,6 +53,6 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
v => return Err(format!("$number2: {} is not a number.", v).into()), v => return Err(format!("$number2: {} is not a number.", v).into()),
}; };
Ok(Value::bool(unit1.comparable(&unit2))) Ok(Value::bool(unit1.comparable(&unit2)))
}); });
} }

View File

@ -28,7 +28,7 @@ pub(crate) struct Color {
} }
impl Color { impl Color {
pub fn new_rgba( pub const fn new_rgba(
red: Number, red: Number,
green: Number, green: Number,
blue: Number, blue: Number,

View File

@ -228,7 +228,7 @@ impl RuleSet {
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
enum Expr { enum Expr {
/// A style: `color: red` /// A style: `color: red`
Style(Style), Style(Box<Style>),
/// Several styles /// Several styles
Styles(Vec<Style>), Styles(Vec<Style>),
/// A collection of styles, from a mixin or function /// A collection of styles, from a mixin or function
@ -486,7 +486,7 @@ impl<'a> StyleSheetParser<'a> {
let mut stmts = Vec::new(); let mut stmts = Vec::new();
while let Some(expr) = eat_expr(&mut self.lexer, scope, super_selector)? { while let Some(expr) = eat_expr(&mut self.lexer, scope, super_selector)? {
match expr { match expr {
Expr::Style(s) => stmts.push(Stmt::Style(s)), Expr::Style(s) => stmts.push(Stmt::Style(*s)),
Expr::AtRule(s) => stmts.push(Stmt::AtRule(s)), Expr::AtRule(s) => stmts.push(Stmt::AtRule(s)),
Expr::Styles(s) => stmts.extend(s.into_iter().map(Stmt::Style)), Expr::Styles(s) => stmts.extend(s.into_iter().map(Stmt::Style)),
Expr::MixinDecl(name, mixin) => { Expr::MixinDecl(name, mixin) => {
@ -556,7 +556,7 @@ pub(crate) fn eat_expr<I: Iterator<Item = Token>>(
let mut v = values.into_iter().peekable(); let mut v = values.into_iter().peekable();
let property = Style::parse_property(&mut v, scope, super_selector, String::new())?; let property = Style::parse_property(&mut v, scope, super_selector, String::new())?;
let value = Style::parse_value(&mut v, scope, super_selector)?; let value = Style::parse_value(&mut v, scope, super_selector)?;
return Ok(Some(Expr::Style(Style { property, value }))); return Ok(Some(Expr::Style(Box::new(Style { property, value }))));
} }
TokenKind::Symbol(Symbol::CloseCurlyBrace) => { TokenKind::Symbol(Symbol::CloseCurlyBrace) => {
if values.is_empty() { if values.is_empty() {
@ -571,7 +571,7 @@ pub(crate) fn eat_expr<I: Iterator<Item = Token>>(
let property = let property =
Style::parse_property(&mut v, scope, super_selector, String::new())?; Style::parse_property(&mut v, scope, super_selector, String::new())?;
let value = Style::parse_value(&mut v, scope, super_selector)?; let value = Style::parse_value(&mut v, scope, super_selector)?;
return Ok(Some(Expr::Style(Style { property, value }))); return Ok(Some(Expr::Style(Box::new(Style { property, value }))));
} }
} }
TokenKind::Symbol(Symbol::OpenCurlyBrace) => { TokenKind::Symbol(Symbol::OpenCurlyBrace) => {
@ -634,7 +634,7 @@ pub(crate) fn eat_expr<I: Iterator<Item = Token>>(
pos, pos,
}) = toks.next() }) = toks.next()
{ {
return match AtRule::from_tokens(rule, pos, toks, scope, &super_selector)? { return match AtRule::from_tokens(rule, pos, toks, scope, super_selector)? {
AtRule::Mixin(name, mixin) => Ok(Some(Expr::MixinDecl(name, mixin))), AtRule::Mixin(name, mixin) => Ok(Some(Expr::MixinDecl(name, mixin))),
AtRule::Function(name, func) => Ok(Some(Expr::FunctionDecl(name, func))), AtRule::Function(name, func) => Ok(Some(Expr::FunctionDecl(name, func))),
AtRule::Charset(_) => todo!("@charset as expr"), AtRule::Charset(_) => todo!("@charset as expr"),

View File

@ -95,7 +95,7 @@ impl Mixin {
while let Some(expr) = eat_expr(&mut self.body, &self.scope, super_selector)? { while let Some(expr) = eat_expr(&mut self.body, &self.scope, super_selector)? {
match expr { match expr {
Expr::AtRule(a) => stmts.push(Stmt::AtRule(a)), Expr::AtRule(a) => stmts.push(Stmt::AtRule(a)),
Expr::Style(s) => stmts.push(Stmt::Style(s)), Expr::Style(s) => stmts.push(Stmt::Style(*s)),
Expr::Styles(s) => stmts.extend(s.into_iter().map(Stmt::Style)), Expr::Styles(s) => stmts.extend(s.into_iter().map(Stmt::Style)),
Expr::Include(s) => stmts.extend(s), Expr::Include(s) => stmts.extend(s),
Expr::MixinDecl(..) | Expr::FunctionDecl(..) | Expr::Debug(..) | Expr::Warn(..) => { Expr::MixinDecl(..) | Expr::FunctionDecl(..) | Expr::Debug(..) | Expr::Warn(..) => {

View File

@ -13,7 +13,7 @@ use std::vec::IntoIter;
pub(crate) struct Selector(pub Vec<SelectorKind>); pub(crate) struct Selector(pub Vec<SelectorKind>);
impl Selector { impl Selector {
pub fn new() -> Selector { pub const fn new() -> Selector {
Selector(Vec::new()) Selector(Vec::new())
} }
} }

View File

@ -118,7 +118,7 @@ impl<'a> StyleParser<'a> {
if tok.equals_symbol(Symbol::OpenCurlyBrace) { if tok.equals_symbol(Symbol::OpenCurlyBrace) {
match self.eat_style_group(toks, property)? { match self.eat_style_group(toks, property)? {
Expr::Styles(s) => styles.extend(s), Expr::Styles(s) => styles.extend(s),
Expr::Style(s) => styles.push(s), Expr::Style(s) => styles.push(*s),
_ => unreachable!(), _ => unreachable!(),
} }
devour_whitespace(toks); devour_whitespace(toks);
@ -150,7 +150,7 @@ impl<'a> StyleParser<'a> {
value, value,
}); });
match self.eat_style_group(toks, property)? { match self.eat_style_group(toks, property)? {
Expr::Style(s) => styles.push(s), Expr::Style(s) => styles.push(*s),
Expr::Styles(s) => styles.extend(s), Expr::Styles(s) => styles.extend(s),
_ => unreachable!(), _ => unreachable!(),
} }
@ -186,7 +186,7 @@ impl<'a> StyleParser<'a> {
value: val, value: val,
}]; }];
match self.eat_style_group(toks, super_property)? { match self.eat_style_group(toks, super_property)? {
Expr::Style(s) => v.push(s), Expr::Style(s) => v.push(*s),
Expr::Styles(s) => v.extend(s), Expr::Styles(s) => v.extend(s),
_ => unreachable!(), _ => unreachable!(),
} }
@ -194,10 +194,10 @@ impl<'a> StyleParser<'a> {
} }
_ => {} _ => {}
} }
return Ok(Expr::Style(Style { return Ok(Expr::Style(Box::new(Style {
property: super_property, property: super_property,
value: val, value: val,
})); })));
} }
} }
} }