From 3ab2aa961ad5b9bd83165a585cf28663c437d34c Mon Sep 17 00:00:00 2001 From: Connor Skees Date: Sat, 24 Jul 2021 20:10:10 -0400 Subject: [PATCH] don't clone in order to construct lexer --- src/atrule/mixin.rs | 2 +- src/builtin/functions/color/hsl.rs | 28 +++++++++++------------ src/builtin/functions/color/hwb.rs | 6 ++--- src/builtin/functions/color/opacity.rs | 12 +++++----- src/builtin/functions/color/other.rs | 8 +++---- src/builtin/functions/color/rgb.rs | 14 ++++++------ src/builtin/functions/list.rs | 18 +++++++-------- src/builtin/functions/map.rs | 14 ++++++------ src/builtin/functions/math.rs | 20 ++++++++--------- src/builtin/functions/meta.rs | 31 ++++++++++++-------------- src/builtin/functions/mod.rs | 7 ++---- src/builtin/functions/selector.rs | 16 ++++++------- src/builtin/functions/string.rs | 18 +++++++-------- src/builtin/modules/math.rs | 20 ++++++++--------- src/builtin/modules/meta.rs | 6 ++--- src/builtin/modules/mod.rs | 2 +- src/lexer.rs | 24 +++++++++++++------- src/parse/args.rs | 16 ++++++++----- src/parse/control_flow.rs | 20 ++++++++--------- src/parse/function.rs | 4 ++-- src/parse/ident.rs | 2 +- src/parse/import.rs | 2 +- src/parse/keyframes.rs | 10 ++++----- src/parse/media.rs | 2 +- src/parse/mixin.rs | 10 ++++----- src/parse/mod.rs | 10 ++++----- src/parse/module.rs | 2 +- src/parse/style.rs | 4 ++-- src/parse/throw_away.rs | 2 +- src/parse/value/css_function.rs | 2 +- src/parse/value/eval.rs | 10 ++++----- src/parse/value/parse.rs | 18 +++++++-------- src/parse/variable.rs | 2 +- src/selector/attribute.rs | 6 ++--- src/selector/parse.rs | 8 +++---- src/value/mod.rs | 2 +- src/value/sass_function.rs | 2 +- 37 files changed, 193 insertions(+), 187 deletions(-) diff --git a/src/atrule/mixin.rs b/src/atrule/mixin.rs index b89d2f7..a5f010e 100644 --- a/src/atrule/mixin.rs +++ b/src/atrule/mixin.rs @@ -7,7 +7,7 @@ use crate::{ Token, }; -pub(crate) type BuiltinMixin = fn(CallArgs, &mut Parser<'_>) -> SassResult>; +pub(crate) type BuiltinMixin = fn(CallArgs, &mut Parser) -> SassResult>; #[derive(Clone)] pub(crate) enum Mixin { diff --git a/src/builtin/functions/color/hsl.rs b/src/builtin/functions/color/hsl.rs index f26d1ec..89c7693 100644 --- a/src/builtin/functions/color/hsl.rs +++ b/src/builtin/functions/color/hsl.rs @@ -13,7 +13,7 @@ use crate::{ value::{Number, Value}, }; -fn inner_hsl(name: &'static str, mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +fn inner_hsl(name: &'static str, mut args: CallArgs, parser: &mut Parser) -> SassResult { if args.is_empty() { return Err(("Missing argument $channels.", args.span()).into()); } @@ -213,15 +213,15 @@ fn inner_hsl(name: &'static str, mut args: CallArgs, parser: &mut Parser<'_>) -> } } -pub(crate) fn hsl(args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn hsl(args: CallArgs, parser: &mut Parser) -> SassResult { inner_hsl("hsl", args, parser) } -pub(crate) fn hsla(args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn hsla(args: CallArgs, parser: &mut Parser) -> SassResult { inner_hsl("hsla", args, parser) } -pub(crate) fn hue(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn hue(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(1)?; match args.get_err(0, "color")? { Value::Color(c) => Ok(Value::Dimension(Some(c.hue()), Unit::Deg, true)), @@ -233,7 +233,7 @@ pub(crate) fn hue(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult) -> SassResult { +pub(crate) fn saturation(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(1)?; match args.get_err(0, "color")? { Value::Color(c) => Ok(Value::Dimension(Some(c.saturation()), Unit::Percent, true)), @@ -245,7 +245,7 @@ pub(crate) fn saturation(mut args: CallArgs, parser: &mut Parser<'_>) -> SassRes } } -pub(crate) fn lightness(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn lightness(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(1)?; match args.get_err(0, "color")? { Value::Color(c) => Ok(Value::Dimension(Some(c.lightness()), Unit::Percent, true)), @@ -257,7 +257,7 @@ pub(crate) fn lightness(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResu } } -pub(crate) fn adjust_hue(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn adjust_hue(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(2)?; let color = match args.get_err(0, "color")? { Value::Color(c) => c, @@ -286,7 +286,7 @@ pub(crate) fn adjust_hue(mut args: CallArgs, parser: &mut Parser<'_>) -> SassRes Ok(Value::Color(Box::new(color.adjust_hue(degrees)))) } -fn lighten(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +fn lighten(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(2)?; let color = match args.get_err(0, "color")? { Value::Color(c) => c, @@ -315,7 +315,7 @@ fn lighten(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { Ok(Value::Color(Box::new(color.lighten(amount)))) } -fn darken(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +fn darken(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(2)?; let color = match args.get_err(0, "color")? { Value::Color(c) => c, @@ -344,7 +344,7 @@ fn darken(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { Ok(Value::Color(Box::new(color.darken(amount)))) } -fn saturate(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +fn saturate(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(2)?; if args.len() == 1 { return Ok(Value::String( @@ -389,7 +389,7 @@ fn saturate(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { Ok(Value::Color(Box::new(color.saturate(amount)))) } -fn desaturate(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +fn desaturate(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(2)?; let color = match args.get_err(0, "color")? { Value::Color(c) => c, @@ -418,7 +418,7 @@ fn desaturate(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult Ok(Value::Color(Box::new(color.desaturate(amount)))) } -pub(crate) fn grayscale(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn grayscale(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(1)?; let color = match args.get_err(0, "color")? { Value::Color(c) => c, @@ -439,7 +439,7 @@ pub(crate) fn grayscale(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResu Ok(Value::Color(Box::new(color.desaturate(Number::one())))) } -pub(crate) fn complement(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn complement(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(1)?; let color = match args.get_err(0, "color")? { Value::Color(c) => c, @@ -454,7 +454,7 @@ pub(crate) fn complement(mut args: CallArgs, parser: &mut Parser<'_>) -> SassRes Ok(Value::Color(Box::new(color.complement()))) } -pub(crate) fn invert(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn invert(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(2)?; let weight = match args.get(1, "weight") { Some(Err(e)) => return Err(e), diff --git a/src/builtin/functions/color/hwb.rs b/src/builtin/functions/color/hwb.rs index 0ebcc4b..a1c09e5 100644 --- a/src/builtin/functions/color/hwb.rs +++ b/src/builtin/functions/color/hwb.rs @@ -9,7 +9,7 @@ use crate::{ value::{Number, Value}, }; -pub(crate) fn blackness(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn blackness(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(1)?; let color = match args.get_err(0, "color")? { @@ -29,7 +29,7 @@ pub(crate) fn blackness(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResu Ok(Value::Dimension(Some(blackness * 100), Unit::Percent, true)) } -pub(crate) fn whiteness(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn whiteness(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(1)?; let color = match args.get_err(0, "color")? { @@ -48,7 +48,7 @@ pub(crate) fn whiteness(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResu Ok(Value::Dimension(Some(whiteness * 100), Unit::Percent, true)) } -pub(crate) fn hwb(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn hwb(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(4)?; if args.is_empty() { diff --git a/src/builtin/functions/color/opacity.rs b/src/builtin/functions/color/opacity.rs index 2686a23..62e2dca 100644 --- a/src/builtin/functions/color/opacity.rs +++ b/src/builtin/functions/color/opacity.rs @@ -35,7 +35,7 @@ mod test { } } -pub(crate) fn alpha(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn alpha(mut args: CallArgs, parser: &mut Parser) -> SassResult { if args.len() <= 1 { match args.get_err(0, "color")? { Value::Color(c) => Ok(Value::Dimension(Some(c.alpha()), Unit::None, true)), @@ -69,7 +69,7 @@ pub(crate) fn alpha(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult) -> SassResult { +pub(crate) fn opacity(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(1)?; match args.get_err(0, "color")? { Value::Color(c) => Ok(Value::Dimension(Some(c.alpha()), Unit::None, true)), @@ -87,7 +87,7 @@ pub(crate) fn opacity(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult } // todo: unify `opacify` and `fade_in` -fn opacify(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +fn opacify(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(2)?; let color = match args.get_err(0, "color")? { Value::Color(c) => c, @@ -113,7 +113,7 @@ fn opacify(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { Ok(Value::Color(Box::new(color.fade_in(amount)))) } -fn fade_in(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +fn fade_in(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(2)?; let color = match args.get_err(0, "color")? { Value::Color(c) => c, @@ -140,7 +140,7 @@ fn fade_in(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { } // todo: unify with `fade_out` -fn transparentize(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +fn transparentize(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(2)?; let color = match args.get_err(0, "color")? { Value::Color(c) => c, @@ -166,7 +166,7 @@ fn transparentize(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult) -> SassResult { +fn fade_out(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(2)?; let color = match args.get_err(0, "color")? { Value::Color(c) => c, diff --git a/src/builtin/functions/color/other.rs b/src/builtin/functions/color/other.rs index 5b0b66e..7d16b72 100644 --- a/src/builtin/functions/color/other.rs +++ b/src/builtin/functions/color/other.rs @@ -48,7 +48,7 @@ macro_rules! opt_hsl { }; } -pub(crate) fn change_color(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn change_color(mut args: CallArgs, parser: &mut Parser) -> SassResult { if args.positional_arg(1).is_some() { return Err(( "Only one positional argument is allowed. All other arguments must be passed by name.", @@ -116,7 +116,7 @@ pub(crate) fn change_color(mut args: CallArgs, parser: &mut Parser<'_>) -> SassR })) } -pub(crate) fn adjust_color(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn adjust_color(mut args: CallArgs, parser: &mut Parser) -> SassResult { let color = match args.get_err(0, "color")? { Value::Color(c) => c, v => { @@ -179,7 +179,7 @@ pub(crate) fn adjust_color(mut args: CallArgs, parser: &mut Parser<'_>) -> SassR #[allow(clippy::cognitive_complexity)] // todo: refactor into rgb and hsl? -pub(crate) fn scale_color(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn scale_color(mut args: CallArgs, parser: &mut Parser) -> SassResult { pub(crate) fn scale(val: Number, by: Number, max: Number) -> Number { if by.is_zero() { return val; @@ -293,7 +293,7 @@ pub(crate) fn scale_color(mut args: CallArgs, parser: &mut Parser<'_>) -> SassRe })) } -pub(crate) fn ie_hex_str(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn ie_hex_str(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(1)?; let color = match args.get_err(0, "color")? { Value::Color(c) => c, diff --git a/src/builtin/functions/color/rgb.rs b/src/builtin/functions/color/rgb.rs index 9bf36ab..fec1711 100644 --- a/src/builtin/functions/color/rgb.rs +++ b/src/builtin/functions/color/rgb.rs @@ -15,7 +15,7 @@ use crate::{ /// name: Either `rgb` or `rgba` depending on the caller // todo: refactor into smaller functions #[allow(clippy::cognitive_complexity)] -fn inner_rgb(name: &'static str, mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +fn inner_rgb(name: &'static str, mut args: CallArgs, parser: &mut Parser) -> SassResult { if args.is_empty() { return Err(("Missing argument $channels.", args.span()).into()); } @@ -350,15 +350,15 @@ fn inner_rgb(name: &'static str, mut args: CallArgs, parser: &mut Parser<'_>) -> } } -pub(crate) fn rgb(args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn rgb(args: CallArgs, parser: &mut Parser) -> SassResult { inner_rgb("rgb", args, parser) } -pub(crate) fn rgba(args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn rgba(args: CallArgs, parser: &mut Parser) -> SassResult { inner_rgb("rgba", args, parser) } -pub(crate) fn red(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn red(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(1)?; match args.get_err(0, "color")? { Value::Color(c) => Ok(Value::Dimension(Some(c.red()), Unit::None, true)), @@ -370,7 +370,7 @@ pub(crate) fn red(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult) -> SassResult { +pub(crate) fn green(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(1)?; match args.get_err(0, "color")? { Value::Color(c) => Ok(Value::Dimension(Some(c.green()), Unit::None, true)), @@ -382,7 +382,7 @@ pub(crate) fn green(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult) -> SassResult { +pub(crate) fn blue(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(1)?; match args.get_err(0, "color")? { Value::Color(c) => Ok(Value::Dimension(Some(c.blue()), Unit::None, true)), @@ -394,7 +394,7 @@ pub(crate) fn blue(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult) -> SassResult { +pub(crate) fn mix(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(3)?; let color1 = match args.get_err(0, "color1")? { Value::Color(c) => c, diff --git a/src/builtin/functions/list.rs b/src/builtin/functions/list.rs index 5613045..470a90b 100644 --- a/src/builtin/functions/list.rs +++ b/src/builtin/functions/list.rs @@ -11,7 +11,7 @@ use crate::{ value::{Number, Value}, }; -pub(crate) fn length(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn length(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(1)?; Ok(Value::Dimension( Some(Number::from(args.get_err(0, "list")?.as_list().len())), @@ -20,7 +20,7 @@ pub(crate) fn length(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult< )) } -pub(crate) fn nth(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn nth(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(2)?; let mut list = args.get_err(0, "list")?.as_list(); let (n, unit) = match args.get_err(1, "n")? { @@ -65,7 +65,7 @@ pub(crate) fn nth(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult) -> SassResult { +pub(crate) fn list_separator(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(1)?; Ok(Value::String( match args.get_err(0, "list")? { @@ -78,7 +78,7 @@ pub(crate) fn list_separator(mut args: CallArgs, parser: &mut Parser<'_>) -> Sas )) } -pub(crate) fn set_nth(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn set_nth(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(3)?; let (mut list, sep, brackets) = match args.get_err(0, "list")? { Value::List(v, sep, b) => (v, sep, b), @@ -136,7 +136,7 @@ pub(crate) fn set_nth(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult Ok(Value::List(list, sep, brackets)) } -pub(crate) fn append(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn append(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(3)?; let (mut list, sep, brackets) = match args.get_err(0, "list")? { Value::List(v, sep, b) => (v, sep, b), @@ -174,7 +174,7 @@ pub(crate) fn append(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult< Ok(Value::List(list, sep, brackets)) } -pub(crate) fn join(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn join(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(4)?; let (mut list1, sep1, brackets) = match args.get_err(0, "list1")? { Value::List(v, sep, brackets) => (v, sep, brackets), @@ -241,7 +241,7 @@ pub(crate) fn join(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult) -> SassResult { +pub(crate) fn is_bracketed(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(1)?; Ok(Value::bool(match args.get_err(0, "list")? { Value::List(.., brackets) => match brackets { @@ -252,7 +252,7 @@ pub(crate) fn is_bracketed(mut args: CallArgs, parser: &mut Parser<'_>) -> SassR })) } -pub(crate) fn index(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn index(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(2)?; let list = args.get_err(0, "list")?.as_list(); let value = args.get_err(1, "value")?; @@ -263,7 +263,7 @@ pub(crate) fn index(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult) -> SassResult { +pub(crate) fn zip(args: CallArgs, parser: &mut Parser) -> SassResult { let lists = args .get_variadic()? .into_iter() diff --git a/src/builtin/functions/map.rs b/src/builtin/functions/map.rs index e547c38..db75afd 100644 --- a/src/builtin/functions/map.rs +++ b/src/builtin/functions/map.rs @@ -8,7 +8,7 @@ use crate::{ value::{SassMap, Value}, }; -pub(crate) fn map_get(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn map_get(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(2)?; let key = args.get_err(1, "key")?; let map = match args.get_err(0, "map")? { @@ -26,7 +26,7 @@ pub(crate) fn map_get(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult Ok(map.get(&key).unwrap_or(Value::Null)) } -pub(crate) fn map_has_key(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn map_has_key(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(2)?; let key = args.get_err(1, "key")?; let map = match args.get_err(0, "map")? { @@ -44,7 +44,7 @@ pub(crate) fn map_has_key(mut args: CallArgs, parser: &mut Parser<'_>) -> SassRe Ok(Value::bool(map.get(&key).is_some())) } -pub(crate) fn map_keys(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn map_keys(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(1)?; let map = match args.get_err(0, "map")? { Value::Map(m) => m, @@ -65,7 +65,7 @@ pub(crate) fn map_keys(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResul )) } -pub(crate) fn map_values(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn map_values(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(1)?; let map = match args.get_err(0, "map")? { Value::Map(m) => m, @@ -86,7 +86,7 @@ pub(crate) fn map_values(mut args: CallArgs, parser: &mut Parser<'_>) -> SassRes )) } -pub(crate) fn map_merge(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn map_merge(mut args: CallArgs, parser: &mut Parser) -> SassResult { if args.len() == 1 { return Err(("Expected $args to contain a key.", args.span()).into()); } @@ -163,7 +163,7 @@ pub(crate) fn map_merge(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResu Ok(Value::Map(map1)) } -pub(crate) fn map_remove(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn map_remove(mut args: CallArgs, parser: &mut Parser) -> SassResult { let mut map = match args.get_err(0, "map")? { Value::Map(m) => m, Value::List(v, ..) if v.is_empty() => SassMap::new(), @@ -183,7 +183,7 @@ pub(crate) fn map_remove(mut args: CallArgs, parser: &mut Parser<'_>) -> SassRes Ok(Value::Map(map)) } -pub(crate) fn map_set(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn map_set(mut args: CallArgs, parser: &mut Parser) -> SassResult { let key_position = args.len().saturating_sub(2); let value_position = args.len().saturating_sub(1); diff --git a/src/builtin/functions/math.rs b/src/builtin/functions/math.rs index 3deb68e..50a6cc1 100644 --- a/src/builtin/functions/math.rs +++ b/src/builtin/functions/math.rs @@ -14,7 +14,7 @@ use crate::{ value::{Number, Value}, }; -pub(crate) fn percentage(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn percentage(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(1)?; let num = match args.get_err(0, "number")? { Value::Dimension(Some(n), Unit::None, _) => Some(n * Number::from(100)), @@ -40,7 +40,7 @@ pub(crate) fn percentage(mut args: CallArgs, parser: &mut Parser<'_>) -> SassRes Ok(Value::Dimension(num, Unit::Percent, true)) } -pub(crate) fn round(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn round(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(1)?; match args.get_err(0, "number")? { Value::Dimension(Some(n), u, _) => Ok(Value::Dimension(Some(n.round()), u, true)), @@ -53,7 +53,7 @@ pub(crate) fn round(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult) -> SassResult { +pub(crate) fn ceil(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(1)?; match args.get_err(0, "number")? { Value::Dimension(Some(n), u, _) => Ok(Value::Dimension(Some(n.ceil()), u, true)), @@ -66,7 +66,7 @@ pub(crate) fn ceil(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult) -> SassResult { +pub(crate) fn floor(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(1)?; match args.get_err(0, "number")? { Value::Dimension(Some(n), u, _) => Ok(Value::Dimension(Some(n.floor()), u, true)), @@ -79,7 +79,7 @@ pub(crate) fn floor(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult) -> SassResult { +pub(crate) fn abs(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(1)?; match args.get_err(0, "number")? { Value::Dimension(Some(n), u, _) => Ok(Value::Dimension(Some(n.abs()), u, true)), @@ -92,7 +92,7 @@ pub(crate) fn abs(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult) -> SassResult { +pub(crate) fn comparable(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(2)?; let unit1 = match args.get_err(0, "number1")? { Value::Dimension(_, u, _) => u, @@ -120,7 +120,7 @@ pub(crate) fn comparable(mut args: CallArgs, parser: &mut Parser<'_>) -> SassRes // TODO: write tests for this #[cfg(feature = "random")] -pub(crate) fn random(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn random(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(1)?; let limit = match args.default_arg(0, "limit", Value::Null)? { Value::Dimension(Some(n), ..) => n, @@ -179,7 +179,7 @@ pub(crate) fn random(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult< )) } -pub(crate) fn min(args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn min(args: CallArgs, parser: &mut Parser) -> SassResult { args.min_args(1)?; let span = args.span(); let mut nums = args @@ -225,7 +225,7 @@ pub(crate) fn min(args: CallArgs, parser: &mut Parser<'_>) -> SassResult Ok(Value::Dimension(Some(min.0), min.1, true)) } -pub(crate) fn max(args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn max(args: CallArgs, parser: &mut Parser) -> SassResult { args.min_args(1)?; let span = args.span(); let mut nums = args @@ -271,7 +271,7 @@ pub(crate) fn max(args: CallArgs, parser: &mut Parser<'_>) -> SassResult Ok(Value::Dimension(Some(max.0), max.1, true)) } -pub(crate) fn divide(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn divide(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(2)?; let number1 = args.get_err(0, "number1")?; diff --git a/src/builtin/functions/meta.rs b/src/builtin/functions/meta.rs index 002c9e9..ed0aa6b 100644 --- a/src/builtin/functions/meta.rs +++ b/src/builtin/functions/meta.rs @@ -11,7 +11,7 @@ use crate::{ value::{SassFunction, Value}, }; -fn if_(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +fn if_(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(3)?; if args.get_err(0, "condition")?.is_true() { Ok(args.get_err(1, "if-true")?) @@ -20,7 +20,7 @@ fn if_(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { } } -pub(crate) fn feature_exists(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn feature_exists(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(1)?; match args.get_err(0, "feature")? { #[allow(clippy::match_same_arms)] @@ -50,7 +50,7 @@ pub(crate) fn feature_exists(mut args: CallArgs, parser: &mut Parser<'_>) -> Sas } } -pub(crate) fn unit(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn unit(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(1)?; let unit = match args.get_err(0, "number")? { Value::Dimension(_, u, _) => u.to_string(), @@ -65,13 +65,13 @@ pub(crate) fn unit(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult) -> SassResult { +pub(crate) fn type_of(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(1)?; let value = args.get_err(0, "value")?; Ok(Value::String(value.kind().to_owned(), QuoteKind::None)) } -pub(crate) fn unitless(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn unitless(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(1)?; Ok(match args.get_err(0, "number")? { Value::Dimension(_, Unit::None, _) => Value::True, @@ -86,7 +86,7 @@ pub(crate) fn unitless(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResul }) } -pub(crate) fn inspect(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn inspect(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(1)?; Ok(Value::String( args.get_err(0, "value")?.inspect(args.span())?.into_owned(), @@ -94,7 +94,7 @@ pub(crate) fn inspect(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult )) } -pub(crate) fn variable_exists(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn variable_exists(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(1)?; match args.get_err(0, "name")? { Value::String(s, _) => Ok(Value::bool( @@ -108,10 +108,7 @@ pub(crate) fn variable_exists(mut args: CallArgs, parser: &mut Parser<'_>) -> Sa } } -pub(crate) fn global_variable_exists( - mut args: CallArgs, - parser: &mut Parser<'_>, -) -> SassResult { +pub(crate) fn global_variable_exists(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(2)?; let name: Identifier = match args.get_err(0, "name")? { @@ -147,7 +144,7 @@ pub(crate) fn global_variable_exists( })) } -pub(crate) fn mixin_exists(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn mixin_exists(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(2)?; let name: Identifier = match args.get_err(0, "name")? { Value::String(s, _) => s.into(), @@ -182,7 +179,7 @@ pub(crate) fn mixin_exists(mut args: CallArgs, parser: &mut Parser<'_>) -> SassR })) } -pub(crate) fn function_exists(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn function_exists(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(2)?; let name: Identifier = match args.get_err(0, "name")? { @@ -218,7 +215,7 @@ pub(crate) fn function_exists(mut args: CallArgs, parser: &mut Parser<'_>) -> Sa })) } -pub(crate) fn get_function(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn get_function(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(3)?; let name: Identifier = match args.get_err(0, "name")? { Value::String(s, _) => s.into(), @@ -272,7 +269,7 @@ pub(crate) fn get_function(mut args: CallArgs, parser: &mut Parser<'_>) -> SassR Ok(Value::FunctionRef(func)) } -pub(crate) fn call(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn call(mut args: CallArgs, parser: &mut Parser) -> SassResult { let func = match args.get_err(0, "function")? { Value::FunctionRef(f) => f, v => { @@ -290,7 +287,7 @@ pub(crate) fn call(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult) -> SassResult { +pub(crate) fn content_exists(args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(0)?; if !parser.flags.in_mixin() { return Err(( @@ -305,7 +302,7 @@ pub(crate) fn content_exists(args: CallArgs, parser: &mut Parser<'_>) -> SassRes } #[allow(unused_variables, clippy::needless_pass_by_value)] -pub(crate) fn keywords(args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn keywords(args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(1)?; Err(( diff --git a/src/builtin/functions/mod.rs b/src/builtin/functions/mod.rs index 1c28974..f5a3968 100644 --- a/src/builtin/functions/mod.rs +++ b/src/builtin/functions/mod.rs @@ -27,13 +27,10 @@ static FUNCTION_COUNT: AtomicUsize = AtomicUsize::new(0); // TODO: impl Fn #[derive(Clone)] -pub(crate) struct Builtin( - pub fn(CallArgs, &mut Parser<'_>) -> SassResult, - usize, -); +pub(crate) struct Builtin(pub fn(CallArgs, &mut Parser) -> SassResult, usize); impl Builtin { - pub fn new(body: fn(CallArgs, &mut Parser<'_>) -> SassResult) -> Builtin { + pub fn new(body: fn(CallArgs, &mut Parser) -> SassResult) -> Builtin { let count = FUNCTION_COUNT.fetch_add(1, Ordering::Relaxed); Self(body, count) } diff --git a/src/builtin/functions/selector.rs b/src/builtin/functions/selector.rs index ecccfce..91e0243 100644 --- a/src/builtin/functions/selector.rs +++ b/src/builtin/functions/selector.rs @@ -9,7 +9,7 @@ use crate::{ value::Value, }; -pub(crate) fn is_superselector(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn is_superselector(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(2)?; let parent_selector = args .get_err(0, "super")? @@ -21,7 +21,7 @@ pub(crate) fn is_superselector(mut args: CallArgs, parser: &mut Parser<'_>) -> S )) } -pub(crate) fn simple_selectors(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn simple_selectors(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(1)?; // todo: Value::to_compound_selector let selector = args @@ -51,7 +51,7 @@ pub(crate) fn simple_selectors(mut args: CallArgs, parser: &mut Parser<'_>) -> S )) } -pub(crate) fn selector_parse(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn selector_parse(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(1)?; Ok(args .get_err(0, "selector")? @@ -60,7 +60,7 @@ pub(crate) fn selector_parse(mut args: CallArgs, parser: &mut Parser<'_>) -> Sas .into_value()) } -pub(crate) fn selector_nest(args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn selector_nest(args: CallArgs, parser: &mut Parser) -> SassResult { let span = args.span(); let selectors = args.get_variadic()?; if selectors.is_empty() { @@ -81,7 +81,7 @@ pub(crate) fn selector_nest(args: CallArgs, parser: &mut Parser<'_>) -> SassResu .into_value()) } -pub(crate) fn selector_append(args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn selector_append(args: CallArgs, parser: &mut Parser) -> SassResult { let span = args.span(); let selectors = args.get_variadic()?; if selectors.is_empty() { @@ -129,7 +129,7 @@ pub(crate) fn selector_append(args: CallArgs, parser: &mut Parser<'_>) -> SassRe .into_value()) } -pub(crate) fn selector_extend(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn selector_extend(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(3)?; let selector = args .get_err(0, "selector")? @@ -144,7 +144,7 @@ pub(crate) fn selector_extend(mut args: CallArgs, parser: &mut Parser<'_>) -> Sa Ok(Extender::extend(selector.0, source.0, target.0, args.span())?.to_sass_list()) } -pub(crate) fn selector_replace(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn selector_replace(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(3)?; let selector = args .get_err(0, "selector")? @@ -158,7 +158,7 @@ pub(crate) fn selector_replace(mut args: CallArgs, parser: &mut Parser<'_>) -> S Ok(Extender::replace(selector.0, source.0, target.0, args.span())?.to_sass_list()) } -pub(crate) fn selector_unify(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn selector_unify(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(2)?; let selector1 = args .get_err(0, "selector1")? diff --git a/src/builtin/functions/string.rs b/src/builtin/functions/string.rs index 4a2d150..72ea2d0 100644 --- a/src/builtin/functions/string.rs +++ b/src/builtin/functions/string.rs @@ -15,7 +15,7 @@ use crate::{ value::{Number, Value}, }; -pub(crate) fn to_upper_case(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn to_upper_case(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(1)?; match args.get_err(0, "string")? { Value::String(mut i, q) => { @@ -30,7 +30,7 @@ pub(crate) fn to_upper_case(mut args: CallArgs, parser: &mut Parser<'_>) -> Sass } } -pub(crate) fn to_lower_case(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn to_lower_case(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(1)?; match args.get_err(0, "string")? { Value::String(mut i, q) => { @@ -45,7 +45,7 @@ pub(crate) fn to_lower_case(mut args: CallArgs, parser: &mut Parser<'_>) -> Sass } } -pub(crate) fn str_length(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn str_length(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(1)?; match args.get_err(0, "string")? { Value::String(i, _) => Ok(Value::Dimension( @@ -61,7 +61,7 @@ pub(crate) fn str_length(mut args: CallArgs, parser: &mut Parser<'_>) -> SassRes } } -pub(crate) fn quote(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn quote(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(1)?; match args.get_err(0, "string")? { Value::String(i, _) => Ok(Value::String(i, QuoteKind::Quoted)), @@ -73,7 +73,7 @@ pub(crate) fn quote(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult) -> SassResult { +pub(crate) fn unquote(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(1)?; match args.get_err(0, "string")? { i @ Value::String(..) => Ok(i.unquote()), @@ -85,7 +85,7 @@ pub(crate) fn unquote(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult } } -pub(crate) fn str_slice(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn str_slice(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(3)?; let (string, quotes) = match args.get_err(0, "string")? { Value::String(s, q) => (s, q), @@ -184,7 +184,7 @@ pub(crate) fn str_slice(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResu } } -pub(crate) fn str_index(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn str_index(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(2)?; let s1 = match args.get_err(0, "string")? { Value::String(i, _) => i, @@ -214,7 +214,7 @@ pub(crate) fn str_index(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResu }) } -pub(crate) fn str_insert(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn str_insert(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(3)?; let (s1, quotes) = match args.get_err(0, "string")? { Value::String(i, q) => (i, q), @@ -314,7 +314,7 @@ pub(crate) fn str_insert(mut args: CallArgs, parser: &mut Parser<'_>) -> SassRes #[cfg(feature = "random")] #[allow(clippy::needless_pass_by_value)] -pub(crate) fn unique_id(args: CallArgs, _: &mut Parser<'_>) -> SassResult { +pub(crate) fn unique_id(args: CallArgs, _: &mut Parser) -> SassResult { args.max_args(0)?; let mut rng = thread_rng(); let string = std::iter::repeat(()) diff --git a/src/builtin/modules/math.rs b/src/builtin/modules/math.rs index aa886e3..659708b 100644 --- a/src/builtin/modules/math.rs +++ b/src/builtin/modules/math.rs @@ -19,7 +19,7 @@ use crate::{ #[cfg(feature = "random")] use crate::builtin::math::random; -fn clamp(mut args: CallArgs, _: &mut Parser<'_>) -> SassResult { +fn clamp(mut args: CallArgs, _: &mut Parser) -> SassResult { args.max_args(3)?; let span = args.span(); @@ -101,7 +101,7 @@ fn clamp(mut args: CallArgs, _: &mut Parser<'_>) -> SassResult { Ok(number) } -fn hypot(args: CallArgs, _: &mut Parser<'_>) -> SassResult { +fn hypot(args: CallArgs, _: &mut Parser) -> SassResult { args.min_args(1)?; let span = args.span(); @@ -172,7 +172,7 @@ fn hypot(args: CallArgs, _: &mut Parser<'_>) -> SassResult { Ok(Value::Dimension(sum.sqrt(), first.1, true)) } -fn log(mut args: CallArgs, _: &mut Parser<'_>) -> SassResult { +fn log(mut args: CallArgs, _: &mut Parser) -> SassResult { args.max_args(2)?; let number = match args.get_err(0, "number")? { @@ -239,7 +239,7 @@ fn log(mut args: CallArgs, _: &mut Parser<'_>) -> SassResult { )) } -fn pow(mut args: CallArgs, _: &mut Parser<'_>) -> SassResult { +fn pow(mut args: CallArgs, _: &mut Parser) -> SassResult { args.max_args(2)?; let base = match args.get_err(0, "base")? { @@ -289,7 +289,7 @@ fn pow(mut args: CallArgs, _: &mut Parser<'_>) -> SassResult { Ok(Value::Dimension(base.pow(exponent), Unit::None, true)) } -fn sqrt(mut args: CallArgs, _: &mut Parser<'_>) -> SassResult { +fn sqrt(mut args: CallArgs, _: &mut Parser) -> SassResult { args.max_args(1)?; let number = args.get_err(0, "number")?; @@ -318,7 +318,7 @@ fn sqrt(mut args: CallArgs, _: &mut Parser<'_>) -> SassResult { macro_rules! trig_fn { ($name:ident, $name_deg:ident) => { - fn $name(mut args: CallArgs, _: &mut Parser<'_>) -> SassResult { + fn $name(mut args: CallArgs, _: &mut Parser) -> SassResult { args.max_args(1)?; let number = args.get_err(0, "number")?; @@ -357,7 +357,7 @@ trig_fn!(cos, cos_deg); trig_fn!(sin, sin_deg); trig_fn!(tan, tan_deg); -fn acos(mut args: CallArgs, _: &mut Parser<'_>) -> SassResult { +fn acos(mut args: CallArgs, _: &mut Parser) -> SassResult { args.max_args(1)?; let number = args.get_err(0, "number")?; @@ -394,7 +394,7 @@ fn acos(mut args: CallArgs, _: &mut Parser<'_>) -> SassResult { }) } -fn asin(mut args: CallArgs, _: &mut Parser<'_>) -> SassResult { +fn asin(mut args: CallArgs, _: &mut Parser) -> SassResult { args.max_args(1)?; let number = args.get_err(0, "number")?; @@ -429,7 +429,7 @@ fn asin(mut args: CallArgs, _: &mut Parser<'_>) -> SassResult { }) } -fn atan(mut args: CallArgs, _: &mut Parser<'_>) -> SassResult { +fn atan(mut args: CallArgs, _: &mut Parser) -> SassResult { args.max_args(1)?; let number = args.get_err(0, "number")?; @@ -462,7 +462,7 @@ fn atan(mut args: CallArgs, _: &mut Parser<'_>) -> SassResult { }) } -fn atan2(mut args: CallArgs, _: &mut Parser<'_>) -> SassResult { +fn atan2(mut args: CallArgs, _: &mut Parser) -> SassResult { args.max_args(2)?; let (y_num, y_unit) = match args.get_err(0, "y")? { Value::Dimension(n, u, ..) => (n, u), diff --git a/src/builtin/modules/meta.rs b/src/builtin/modules/meta.rs index d6cc9ee..f103781 100644 --- a/src/builtin/modules/meta.rs +++ b/src/builtin/modules/meta.rs @@ -14,7 +14,7 @@ use crate::{ value::Value, }; -fn load_css(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult> { +fn load_css(mut args: CallArgs, parser: &mut Parser) -> SassResult> { args.max_args(2)?; let span = args.span(); @@ -69,7 +69,7 @@ fn load_css(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult } } -fn module_functions(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +fn module_functions(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(1)?; let module = match args.get_err(0, "module")? { @@ -88,7 +88,7 @@ fn module_functions(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult) -> SassResult { +fn module_variables(mut args: CallArgs, parser: &mut Parser) -> SassResult { args.max_args(1)?; let module = match args.get_err(0, "module")? { diff --git a/src/builtin/modules/mod.rs b/src/builtin/modules/mod.rs index 62bc9c1..dd9757a 100644 --- a/src/builtin/modules/mod.rs +++ b/src/builtin/modules/mod.rs @@ -198,7 +198,7 @@ impl Module { pub fn insert_builtin( &mut self, name: &'static str, - function: fn(CallArgs, &mut Parser<'_>) -> SassResult, + function: fn(CallArgs, &mut Parser) -> SassResult, ) { let ident = name.into(); self.scope diff --git a/src/lexer.rs b/src/lexer.rs index 4b4fc2e..c088f73 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -1,4 +1,4 @@ -use std::{iter::Peekable, str::Chars, sync::Arc}; +use std::{borrow::Cow, iter::Peekable, str::Chars, sync::Arc}; use codemap::File; @@ -7,13 +7,13 @@ use crate::Token; const FORM_FEED: char = '\x0C'; #[derive(Debug, Clone)] -pub(crate) struct Lexer { - buf: Vec, +pub(crate) struct Lexer<'a> { + buf: Cow<'a, [Token]>, cursor: usize, amt_peeked: usize, } -impl Lexer { +impl<'a> Lexer<'a> { fn peek_cursor(&self) -> usize { self.cursor + self.amt_peeked } @@ -64,7 +64,7 @@ impl Lexer { } } -impl Iterator for Lexer { +impl<'a> Iterator for Lexer<'a> { type Item = Token; fn next(&mut self) -> Option { @@ -106,7 +106,7 @@ impl<'a> Iterator for TokenLexer<'a> { } } -impl Lexer { +impl<'a> Lexer<'a> { pub fn new_from_file(file: &Arc) -> Self { let buf = TokenLexer { file: Arc::clone(file), @@ -118,9 +118,17 @@ impl Lexer { Self::new(buf) } - pub fn new(buf: Vec) -> Lexer { + pub fn new(buf: Vec) -> Self { Lexer { - buf, + buf: Cow::Owned(buf), + cursor: 0, + amt_peeked: 0, + } + } + + pub fn new_ref(buf: &'a [Token]) -> Lexer<'a> { + Lexer { + buf: Cow::Borrowed(buf), cursor: 0, amt_peeked: 0, } diff --git a/src/parse/args.rs b/src/parse/args.rs index 09c5c94..c939da2 100644 --- a/src/parse/args.rs +++ b/src/parse/args.rs @@ -14,7 +14,7 @@ use crate::{ use super::Parser; -impl<'a> Parser<'a> { +impl<'a, 'b> Parser<'a, 'b> { pub(super) fn parse_func_args(&mut self) -> SassResult { let mut args: Vec = Vec::new(); let mut close_paren_span: Span = match self.toks.peek() { @@ -350,8 +350,12 @@ impl<'a> Parser<'a> { } } -impl<'a> Parser<'a> { - pub(super) fn eval_args(&mut self, fn_args: FuncArgs, mut args: CallArgs) -> SassResult { +impl<'a, 'b> Parser<'a, 'b> { + pub(super) fn eval_args( + &mut self, + fn_args: &FuncArgs, + mut args: CallArgs, + ) -> SassResult { let mut scope = Scope::new(); if fn_args.0.is_empty() { args.max_args(0)?; @@ -363,7 +367,7 @@ impl<'a> Parser<'a> { } self.scopes.enter_new_scope(); - for (idx, mut arg) in fn_args.0.into_iter().enumerate() { + for (idx, arg) in fn_args.0.iter().enumerate() { if arg.is_variadic { let arg_list = Value::ArgList(args.get_variadic()?); scope.insert_var(arg.name, arg_list); @@ -372,8 +376,8 @@ impl<'a> Parser<'a> { let val = match args.get(idx, arg.name) { Some(v) => v, - None => match arg.default.as_mut() { - Some(v) => self.parse_value_from_vec(mem::take(v), true), + None => match arg.default.as_ref() { + Some(v) => self.parse_value_from_vec(v, true), None => { return Err( (format!("Missing argument ${}.", &arg.name), args.span()).into() diff --git a/src/parse/control_flow.rs b/src/parse/control_flow.rs index ea22886..03abd01 100644 --- a/src/parse/control_flow.rs +++ b/src/parse/control_flow.rs @@ -12,7 +12,7 @@ use crate::{ Token, }; -impl<'a> Parser<'a> { +impl<'a, 'b> Parser<'a, 'b> { pub(super) fn parse_if(&mut self) -> SassResult> { self.whitespace_or_comment(); @@ -271,7 +271,7 @@ impl<'a> Parser<'a> { ); if self.flags.in_function() { let these_stmts = Parser { - toks: &mut Lexer::new(body.clone()), + toks: &mut Lexer::new_ref(&body), map: self.map, path: self.path, scopes: self.scopes, @@ -295,7 +295,7 @@ impl<'a> Parser<'a> { } else { stmts.append( &mut Parser { - toks: &mut Lexer::new(body.clone()), + toks: &mut Lexer::new_ref(&body), map: self.map, path: self.path, scopes: self.scopes, @@ -343,12 +343,12 @@ impl<'a> Parser<'a> { }); let mut stmts = Vec::new(); - let mut val = self.parse_value_from_vec(cond.clone(), true)?; + let mut val = self.parse_value_from_vec(&cond, true)?; self.scopes.enter_new_scope(); while val.node.is_true() { if self.flags.in_function() { let these_stmts = Parser { - toks: &mut Lexer::new(body.clone()), + toks: &mut Lexer::new_ref(&body), map: self.map, path: self.path, scopes: self.scopes, @@ -372,7 +372,7 @@ impl<'a> Parser<'a> { } else { stmts.append( &mut Parser { - toks: &mut Lexer::new(body.clone()), + toks: &mut Lexer::new_ref(&body), map: self.map, path: self.path, scopes: self.scopes, @@ -392,7 +392,7 @@ impl<'a> Parser<'a> { .parse_stmt()?, ); } - val = self.parse_value_from_vec(cond.clone(), true)?; + val = self.parse_value_from_vec(&cond, true)?; } self.scopes.exit_scope(); @@ -429,7 +429,7 @@ impl<'a> Parser<'a> { self.whitespace_or_comment(); let iter_val_toks = read_until_open_curly_brace(self.toks)?; let iter = self - .parse_value_from_vec(iter_val_toks, true)? + .parse_value_from_vec(&iter_val_toks, true)? .node .as_list(); self.toks.next(); @@ -460,7 +460,7 @@ impl<'a> Parser<'a> { if self.flags.in_function() { let these_stmts = Parser { - toks: &mut Lexer::new(body.clone()), + toks: &mut Lexer::new_ref(&body), map: self.map, path: self.path, scopes: self.scopes, @@ -484,7 +484,7 @@ impl<'a> Parser<'a> { } else { stmts.append( &mut Parser { - toks: &mut Lexer::new(body.clone()), + toks: &mut Lexer::new_ref(&body), map: self.map, path: self.path, scopes: self.scopes, diff --git a/src/parse/function.rs b/src/parse/function.rs index 9ad00a9..042cf7d 100644 --- a/src/parse/function.rs +++ b/src/parse/function.rs @@ -17,7 +17,7 @@ use super::{common::ContextFlags, Parser, Stmt}; const RESERVED_IDENTIFIERS: [&str; 7] = ["calc", "element", "expression", "url", "and", "or", "not"]; -impl<'a> Parser<'a> { +impl<'a, 'b> Parser<'a, 'b> { pub(super) fn parse_function(&mut self) -> SassResult<()> { self.whitespace_or_comment(); let Spanned { node: name, span } = self.parse_identifier()?; @@ -86,7 +86,7 @@ impl<'a> Parser<'a> { .. } = function; - let scope = self.eval_args(fn_args, args)?; + let scope = self.eval_args(&fn_args, args)?; let mut new_scope = Scopes::new(); let mut entered_scope = false; diff --git a/src/parse/ident.rs b/src/parse/ident.rs index 0be4155..e72f79e 100644 --- a/src/parse/ident.rs +++ b/src/parse/ident.rs @@ -12,7 +12,7 @@ use crate::{ use super::Parser; -impl<'a> Parser<'a> { +impl<'a, 'b> Parser<'a, 'b> { fn ident_body_no_interpolation(&mut self, unit: bool) -> SassResult> { let mut text = String::new(); while let Some(tok) = self.toks.peek() { diff --git a/src/parse/import.rs b/src/parse/import.rs index e0c08b6..00616f4 100644 --- a/src/parse/import.rs +++ b/src/parse/import.rs @@ -26,7 +26,7 @@ fn is_plain_css_import(url: &str) -> bool { || lower.starts_with("//") } -impl<'a> Parser<'a> { +impl<'a, 'b> Parser<'a, 'b> { /// Searches the current directory of the file then searches in `load_paths` directories /// if the import has not yet been found. /// diff --git a/src/parse/keyframes.rs b/src/parse/keyframes.rs index b2d6457..57b0110 100644 --- a/src/parse/keyframes.rs +++ b/src/parse/keyframes.rs @@ -20,12 +20,12 @@ impl fmt::Display for KeyframesSelector { } } -struct KeyframesSelectorParser<'a, 'b> { - parser: &'a mut Parser<'b>, +struct KeyframesSelectorParser<'a, 'b, 'c> { + parser: &'a mut Parser<'b, 'c>, } -impl<'a, 'b> KeyframesSelectorParser<'a, 'b> { - pub fn new(parser: &'a mut Parser<'b>) -> Self { +impl<'a, 'b, 'c> KeyframesSelectorParser<'a, 'b, 'c> { + pub fn new(parser: &'a mut Parser<'b, 'c>) -> Self { Self { parser } } @@ -81,7 +81,7 @@ impl<'a, 'b> KeyframesSelectorParser<'a, 'b> { } } -impl<'a> Parser<'a> { +impl<'a, 'b> Parser<'a, 'b> { fn parse_keyframes_name(&mut self) -> SassResult { let mut name = String::new(); self.whitespace_or_comment(); diff --git a/src/parse/media.rs b/src/parse/media.rs index ddb69fd..70c0bb4 100644 --- a/src/parse/media.rs +++ b/src/parse/media.rs @@ -6,7 +6,7 @@ use crate::{ use super::Parser; -impl<'a> Parser<'a> { +impl<'a, 'b> Parser<'a, 'b> { /// Peeks to see if the `ident` is at the current position. If it is, /// consume the identifier pub fn scan_identifier(&mut self, ident: &'static str, case_insensitive: bool) -> bool { diff --git a/src/parse/mixin.rs b/src/parse/mixin.rs index f97ad68..373780d 100644 --- a/src/parse/mixin.rs +++ b/src/parse/mixin.rs @@ -14,7 +14,7 @@ use crate::{ use super::{common::ContextFlags, Parser, Stmt}; -impl<'a> Parser<'a> { +impl<'a, 'b> Parser<'a, 'b> { pub(super) fn parse_mixin(&mut self) -> SassResult<()> { self.whitespace(); let Spanned { node: name, span } = self.parse_identifier_no_interpolation(false)?; @@ -142,7 +142,7 @@ impl<'a> Parser<'a> { } }; - let scope = self.eval_args(fn_args, args)?; + let scope = self.eval_args(&fn_args, args)?; let scope_len = self.scopes.len(); @@ -218,16 +218,16 @@ impl<'a> Parser<'a> { if let Some(ref content_args) = content.content_args { call_args.max_args(content_args.len())?; - let scope = self.eval_args(content_args.clone(), call_args)?; + let scope = self.eval_args(&content_args, call_args)?; scope_at_decl.enter_scope(scope); entered_scope = true; } else { call_args.max_args(0)?; } - let stmts = if let Some(body) = content.content.clone() { + let stmts = if let Some(body) = &content.content { Parser { - toks: &mut Lexer::new(body), + toks: &mut Lexer::new_ref(body), map: self.map, path: self.path, scopes: &mut scope_at_decl, diff --git a/src/parse/mod.rs b/src/parse/mod.rs index f6d69d8..5275f8f 100644 --- a/src/parse/mod.rs +++ b/src/parse/mod.rs @@ -64,8 +64,8 @@ pub(crate) enum Stmt { } // todo: merge at_root and at_root_has_selector into an enum -pub(crate) struct Parser<'a> { - pub toks: &'a mut Lexer, +pub(crate) struct Parser<'a, 'b> { + pub toks: &'a mut Lexer<'b>, pub map: &'a mut CodeMap, pub path: &'a Path, pub global_scope: &'a mut Scope, @@ -89,7 +89,7 @@ pub(crate) struct Parser<'a> { pub module_config: &'a mut ModuleConfig, } -impl<'a> Parser<'a> { +impl<'a, 'b> Parser<'a, 'b> { pub fn parse(&mut self) -> SassResult> { let mut stmts = Vec::new(); @@ -610,7 +610,7 @@ impl<'a> Parser<'a> { } } -impl<'a> Parser<'a> { +impl<'a, 'b> Parser<'a, 'b> { fn parse_unknown_at_rule(&mut self, name: String) -> SassResult { if self.flags.in_function() { return Err(("This at-rule is not allowed here.", self.span_before).into()); @@ -947,7 +947,7 @@ impl<'a> Parser<'a> { } } -impl<'a> Parser<'a> { +impl<'a, 'b> Parser<'a, 'b> { fn debug(&self, message: &Spanned>) { if self.options.quiet { return; diff --git a/src/parse/module.rs b/src/parse/module.rs index 67fe53a..a404cca 100644 --- a/src/parse/module.rs +++ b/src/parse/module.rs @@ -16,7 +16,7 @@ use crate::{ Token, }; -impl<'a> Parser<'a> { +impl<'a, 'b> Parser<'a, 'b> { fn parse_module_alias(&mut self) -> SassResult> { if !matches!( self.toks.peek(), diff --git a/src/parse/style.rs b/src/parse/style.rs index b0a19ab..451831a 100644 --- a/src/parse/style.rs +++ b/src/parse/style.rs @@ -13,7 +13,7 @@ use super::common::SelectorOrStyle; use super::Parser; -impl<'a> Parser<'a> { +impl<'a, 'b> Parser<'a, 'b> { fn parse_style_value_when_no_space_after_semicolon(&mut self) -> Option> { let mut toks = Vec::new(); while let Some(tok) = self.toks.peek() { @@ -128,7 +128,7 @@ impl<'a> Parser<'a> { self.parse_style_value_when_no_space_after_semicolon() { let len = toks.len(); - if let Ok(val) = self.parse_value_from_vec(toks, false) { + if let Ok(val) = self.parse_value_from_vec(&toks, false) { self.toks.take(len).for_each(drop); return Ok(SelectorOrStyle::Style( InternedString::get_or_intern(property), diff --git a/src/parse/throw_away.rs b/src/parse/throw_away.rs index 316cf36..9952aaa 100644 --- a/src/parse/throw_away.rs +++ b/src/parse/throw_away.rs @@ -4,7 +4,7 @@ use crate::{error::SassResult, Token}; use super::Parser; -impl<'a> Parser<'a> { +impl<'a, 'b> Parser<'a, 'b> { pub(super) fn throw_away_until_newline(&mut self) { for tok in &mut self.toks { if tok.kind == '\n' { diff --git a/src/parse/value/css_function.rs b/src/parse/value/css_function.rs index 30c4455..1a38eca 100644 --- a/src/parse/value/css_function.rs +++ b/src/parse/value/css_function.rs @@ -4,7 +4,7 @@ use crate::{error::SassResult, parse::common::Comment, utils::IsWhitespace, valu use super::super::Parser; -impl<'a> Parser<'a> { +impl<'a, 'b> Parser<'a, 'b> { pub(super) fn parse_calc_args(&mut self, buf: &mut String) -> SassResult<()> { buf.reserve(2); buf.push('('); diff --git a/src/parse/value/eval.rs b/src/parse/value/eval.rs index 2da02ae..b33104f 100644 --- a/src/parse/value/eval.rs +++ b/src/parse/value/eval.rs @@ -30,19 +30,19 @@ impl HigherIntermediateValue { } } -impl<'a> Parser<'a> { +impl<'a, 'b> Parser<'a, 'b> { fn call_function(&mut self, function: SassFunction, args: CallArgs) -> SassResult { function.call(args, self) } } -pub(crate) struct ValueVisitor<'a, 'b: 'a> { - parser: &'a mut Parser<'b>, +pub(crate) struct ValueVisitor<'a, 'b: 'a, 'c> { + parser: &'a mut Parser<'b, 'c>, span: Span, } -impl<'a, 'b: 'a> ValueVisitor<'a, 'b> { - pub fn new(parser: &'a mut Parser<'b>, span: Span) -> Self { +impl<'a, 'b: 'a, 'c> ValueVisitor<'a, 'b, 'c> { + pub fn new(parser: &'a mut Parser<'b, 'c>, span: Span) -> Self { Self { parser, span } } diff --git a/src/parse/value/parse.rs b/src/parse/value/parse.rs index d60bdc3..536a34a 100644 --- a/src/parse/value/parse.rs +++ b/src/parse/value/parse.rs @@ -46,9 +46,9 @@ impl IsWhitespace for IntermediateValue { } /// We parse a value until the predicate returns true -type Predicate<'a> = &'a dyn Fn(&mut Parser<'_>) -> bool; +type Predicate<'a> = &'a dyn Fn(&mut Parser<'_, '_>) -> bool; -impl<'a> Parser<'a> { +impl<'a, 'b> Parser<'a, 'b> { /// Parse a value from a stream of tokens /// /// This function will cease parsing if the predicate returns true. @@ -171,11 +171,11 @@ impl<'a> Parser<'a> { pub(crate) fn parse_value_from_vec( &mut self, - toks: Vec, + toks: &[Token], in_paren: bool, ) -> SassResult> { Parser { - toks: &mut Lexer::new(toks), + toks: &mut Lexer::new_ref(toks), map: self.map, path: self.path, scopes: self.scopes, @@ -1063,13 +1063,13 @@ impl<'a> Parser<'a> { } } -struct IntermediateValueIterator<'a, 'b: 'a> { - parser: &'a mut Parser<'b>, +struct IntermediateValueIterator<'a, 'b: 'a, 'c> { + parser: &'a mut Parser<'b, 'c>, peek: Option>>, predicate: Predicate<'a>, } -impl<'a, 'b: 'a> Iterator for IntermediateValueIterator<'a, 'b> { +impl<'a, 'b: 'a, 'c> Iterator for IntermediateValueIterator<'a, 'b, 'c> { type Item = SassResult>; fn next(&mut self) -> Option { if self.peek.is_some() { @@ -1080,8 +1080,8 @@ impl<'a, 'b: 'a> Iterator for IntermediateValueIterator<'a, 'b> { } } -impl<'a, 'b: 'a> IntermediateValueIterator<'a, 'b> { - pub fn new(parser: &'a mut Parser<'b>, predicate: Predicate<'a>) -> Self { +impl<'a, 'b: 'a, 'c> IntermediateValueIterator<'a, 'b, 'c> { + pub fn new(parser: &'a mut Parser<'b, 'c>, predicate: Predicate<'a>) -> Self { Self { parser, peek: None, diff --git a/src/parse/variable.rs b/src/parse/variable.rs index 8c592b1..9aa1021 100644 --- a/src/parse/variable.rs +++ b/src/parse/variable.rs @@ -21,7 +21,7 @@ impl VariableValue { } } -impl<'a> Parser<'a> { +impl<'a, 'b> Parser<'a, 'b> { pub(super) fn parse_variable_declaration(&mut self) -> SassResult<()> { let next = self.toks.next(); assert!(matches!(next, Some(Token { kind: '$', .. }))); diff --git a/src/selector/attribute.rs b/src/selector/attribute.rs index 294e986..604e514 100644 --- a/src/selector/attribute.rs +++ b/src/selector/attribute.rs @@ -40,7 +40,7 @@ impl Hash for Attribute { } } -fn attribute_name(parser: &mut Parser<'_>, start: Span) -> SassResult { +fn attribute_name(parser: &mut Parser, start: Span) -> SassResult { let next = parser.toks.peek().ok_or(("Expected identifier.", start))?; if next.kind == '*' { parser.toks.next(); @@ -85,7 +85,7 @@ fn attribute_name(parser: &mut Parser<'_>, start: Span) -> SassResult) -> SassResult { +fn attribute_operator(parser: &mut Parser) -> SassResult { let op = match parser.toks.next() { Some(Token { kind: '=', .. }) => return Ok(AttributeOp::Equals), Some(Token { kind: '~', .. }) => AttributeOp::Include, @@ -101,7 +101,7 @@ fn attribute_operator(parser: &mut Parser<'_>) -> SassResult { Ok(op) } impl Attribute { - pub fn from_tokens(parser: &mut Parser<'_>) -> SassResult { + pub fn from_tokens(parser: &mut Parser) -> SassResult { let start = parser.span_before; parser.whitespace(); let attr = attribute_name(parser, start)?; diff --git a/src/selector/parse.rs b/src/selector/parse.rs index ee524c4..5246f06 100644 --- a/src/selector/parse.rs +++ b/src/selector/parse.rs @@ -44,21 +44,21 @@ const SELECTOR_PSEUDO_CLASSES: [&str; 8] = [ /// Pseudo-element selectors that take unadorned selectors as arguments. const SELECTOR_PSEUDO_ELEMENTS: [&str; 1] = ["slotted"]; -pub(crate) struct SelectorParser<'a, 'b> { +pub(crate) struct SelectorParser<'a, 'b, 'c> { /// Whether this parser allows the parent selector `&`. allows_parent: bool, /// Whether this parser allows placeholder selectors beginning with `%`. allows_placeholder: bool, - parser: &'a mut Parser<'b>, + parser: &'a mut Parser<'b, 'c>, span: Span, } -impl<'a, 'b> SelectorParser<'a, 'b> { +impl<'a, 'b, 'c> SelectorParser<'a, 'b, 'c> { pub fn new( - parser: &'a mut Parser<'b>, + parser: &'a mut Parser<'b, 'c>, allows_parent: bool, allows_placeholder: bool, span: Span, diff --git a/src/value/mod.rs b/src/value/mod.rs index 15e6ebf..370ef3e 100644 --- a/src/value/mod.rs +++ b/src/value/mod.rs @@ -511,7 +511,7 @@ impl Value { /// `name` is the argument name. It's used for error reporting. pub fn to_selector( self, - parser: &mut Parser<'_>, + parser: &mut Parser, name: &str, allows_parent: bool, ) -> SassResult { diff --git a/src/value/sass_function.rs b/src/value/sass_function.rs index 9535933..ba65030 100644 --- a/src/value/sass_function.rs +++ b/src/value/sass_function.rs @@ -48,7 +48,7 @@ impl SassFunction { } } - pub fn call(self, args: CallArgs, parser: &mut Parser<'_>) -> SassResult { + pub fn call(self, args: CallArgs, parser: &mut Parser) -> SassResult { match self { Self::Builtin(f, ..) => f.0(args, parser), Self::UserDefined(f, ..) => parser.eval_function(*f, args),