put more things in Arc

This commit is contained in:
connorskees 2023-01-07 08:02:57 +00:00
parent 02d36f872a
commit 4f6bff8716
8 changed files with 54 additions and 52 deletions

View File

@ -26,14 +26,14 @@ pub(crate) struct ListExpr {
pub(crate) struct FunctionCallExpr { pub(crate) struct FunctionCallExpr {
pub namespace: Option<Spanned<Identifier>>, pub namespace: Option<Spanned<Identifier>>,
pub name: Identifier, pub name: Identifier,
pub arguments: Box<ArgumentInvocation>, pub arguments: Arc<ArgumentInvocation>,
pub span: Span, pub span: Span,
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub(crate) struct InterpolatedFunction { pub(crate) struct InterpolatedFunction {
pub name: Interpolation, pub name: Interpolation,
pub arguments: Box<ArgumentInvocation>, pub arguments: ArgumentInvocation,
pub span: Span, pub span: Span,
} }
@ -61,7 +61,7 @@ pub(crate) enum AstExpr {
Color(Arc<Color>), Color(Arc<Color>),
FunctionCall(FunctionCallExpr), FunctionCall(FunctionCallExpr),
If(Arc<Ternary>), If(Arc<Ternary>),
InterpolatedFunction(InterpolatedFunction), InterpolatedFunction(Arc<InterpolatedFunction>),
List(ListExpr), List(ListExpr),
Map(AstSassMap), Map(AstSassMap),
Null, Null,

View File

@ -2360,7 +2360,8 @@ impl<'a> Visitor<'a> {
let old_in_function = self.flags.in_function(); let old_in_function = self.flags.in_function();
self.flags.set(ContextFlags::IN_FUNCTION, true); self.flags.set(ContextFlags::IN_FUNCTION, true);
let value = self.run_function_callable(func, *func_call.arguments, func_call.span)?; let value =
self.run_function_callable(func, (*func_call.arguments).clone(), func_call.span)?;
self.flags.set(ContextFlags::IN_FUNCTION, old_in_function); self.flags.set(ContextFlags::IN_FUNCTION, old_in_function);
Ok(value) Ok(value)
@ -2381,7 +2382,7 @@ impl<'a> Visitor<'a> {
let mut buffer = format!("{}(", fn_name); let mut buffer = format!("{}(", fn_name);
let mut first = true; let mut first = true;
for arg in args.positional { for arg in args.positional.clone() {
if first { if first {
first = false; first = false;
} else { } else {
@ -2391,7 +2392,7 @@ impl<'a> Visitor<'a> {
buffer.push_str(&evaluated); buffer.push_str(&evaluated);
} }
if let Some(rest_arg) = args.rest { if let Some(rest_arg) = args.rest.clone() {
let rest = self.visit_expr(rest_arg)?; let rest = self.visit_expr(rest_arg)?;
if !first { if !first {
buffer.push_str(", "); buffer.push_str(", ");
@ -2421,15 +2422,13 @@ impl<'a> Visitor<'a> {
}), }),
AstExpr::List(list) => self.visit_list_expr(list)?, AstExpr::List(list) => self.visit_list_expr(list)?,
AstExpr::String(StringExpr(text, quote), ..) => self.visit_string(text, quote)?, AstExpr::String(StringExpr(text, quote), ..) => self.visit_string(text, quote)?,
AstExpr::BinaryOp(binop) => { AstExpr::BinaryOp(binop) => self.visit_bin_op(
self.visit_bin_op( binop.lhs.clone(),
binop.lhs.clone(), binop.op,
binop.op, binop.rhs.clone(),
binop.rhs.clone(), binop.allows_slash,
binop.allows_slash, binop.span,
binop.span, )?,
)?
}
AstExpr::True => Value::True, AstExpr::True => Value::True,
AstExpr::False => Value::False, AstExpr::False => Value::False,
AstExpr::Calculation { name, args } => { AstExpr::Calculation { name, args } => {
@ -2437,7 +2436,9 @@ impl<'a> Visitor<'a> {
} }
AstExpr::FunctionCall(func_call) => self.visit_function_call_expr(func_call)?, AstExpr::FunctionCall(func_call) => self.visit_function_call_expr(func_call)?,
AstExpr::If(if_expr) => self.visit_ternary((*if_expr).clone())?, AstExpr::If(if_expr) => self.visit_ternary((*if_expr).clone())?,
AstExpr::InterpolatedFunction(func) => self.visit_interpolated_func_expr(func)?, AstExpr::InterpolatedFunction(func) => {
self.visit_interpolated_func_expr((*func).clone())?
}
AstExpr::Map(map) => self.visit_map(map)?, AstExpr::Map(map) => self.visit_map(map)?,
AstExpr::Null => Value::Null, AstExpr::Null => Value::Null,
AstExpr::Paren(expr) => self.visit_expr((*expr).clone())?, AstExpr::Paren(expr) => self.visit_expr((*expr).clone())?,
@ -2477,17 +2478,15 @@ impl<'a> Visitor<'a> {
debug_assert!(string_expr.1 == QuoteKind::None); debug_assert!(string_expr.1 == QuoteKind::None);
CalculationArg::Interpolation(self.perform_interpolation(string_expr.0, false)?) CalculationArg::Interpolation(self.perform_interpolation(string_expr.0, false)?)
} }
AstExpr::BinaryOp(binop) => { AstExpr::BinaryOp(binop) => SassCalculation::operate_internal(
SassCalculation::operate_internal( binop.op,
binop.op, self.visit_calculation_value(binop.lhs.clone(), in_min_or_max, span)?,
self.visit_calculation_value(binop.lhs.clone(), in_min_or_max, span)?, self.visit_calculation_value(binop.rhs.clone(), in_min_or_max, span)?,
self.visit_calculation_value(binop.rhs.clone(), in_min_or_max, span)?, in_min_or_max,
in_min_or_max, !self.flags.in_supports_declaration(),
!self.flags.in_supports_declaration(), self.options,
self.options, span,
span, )?,
)?
}
AstExpr::Number { .. } AstExpr::Number { .. }
| AstExpr::Calculation { .. } | AstExpr::Calculation { .. }
| AstExpr::Variable { .. } | AstExpr::Variable { .. }

View File

@ -1,4 +1,4 @@
use std::{collections::BTreeMap, path::Path}; use std::{collections::BTreeMap, path::Path, sync::Arc};
use codemap::{CodeMap, Span, Spanned}; use codemap::{CodeMap, Span, Spanned};
@ -203,17 +203,17 @@ impl<'a> CssParser<'a> {
return Err(("This function isn't allowed in plain CSS.", span).into()); return Err(("This function isn't allowed in plain CSS.", span).into());
} }
Ok(AstExpr::InterpolatedFunction(InterpolatedFunction { Ok(AstExpr::InterpolatedFunction(Arc::new(InterpolatedFunction {
name: identifier, name: identifier,
arguments: Box::new(ArgumentInvocation { arguments: ArgumentInvocation {
positional: arguments, positional: arguments,
named: BTreeMap::new(), named: BTreeMap::new(),
rest: None, rest: None,
keyword_rest: None, keyword_rest: None,
span: self.toks.span_from(before_args), span: self.toks.span_from(before_args),
}), },
span, span,
}) }))
.span(span)) .span(span))
} }
} }

View File

@ -3,7 +3,8 @@ use std::{
collections::{BTreeMap, HashSet}, collections::{BTreeMap, HashSet},
ffi::OsString, ffi::OsString,
mem, mem,
path::{Path, PathBuf}, sync::Arc, path::{Path, PathBuf},
sync::Arc,
}; };
use codemap::{CodeMap, Span, Spanned}; use codemap::{CodeMap, Span, Spanned};
@ -840,11 +841,11 @@ pub(crate) trait StylesheetParser<'a>: BaseParser<'a> + Sized {
StringExpr(contents, QuoteKind::None), StringExpr(contents, QuoteKind::None),
self.toks_mut().span_from(start), self.toks_mut().span_from(start),
), ),
None => AstExpr::InterpolatedFunction(InterpolatedFunction { None => AstExpr::InterpolatedFunction(Arc::new(InterpolatedFunction {
name: Interpolation::new_plain("url".to_owned()), name: Interpolation::new_plain("url".to_owned()),
arguments: Box::new(self.parse_argument_invocation(false, false)?), arguments: self.parse_argument_invocation(false, false)?,
span: self.toks_mut().span_from(start), span: self.toks_mut().span_from(start),
}), })),
}) })
} }

View File

@ -1228,17 +1228,17 @@ impl<'a, 'c, P: StylesheetParser<'a>> ValueParser<'a, 'c, P> {
Ok(AstExpr::FunctionCall(FunctionCallExpr { Ok(AstExpr::FunctionCall(FunctionCallExpr {
namespace: None, namespace: None,
name: Identifier::from(plain), name: Identifier::from(plain),
arguments: Box::new(arguments), arguments: Arc::new(arguments),
span: parser.toks_mut().span_from(start), span: parser.toks_mut().span_from(start),
}) })
.span(parser.toks_mut().span_from(start))) .span(parser.toks_mut().span_from(start)))
} else { } else {
let arguments = parser.parse_argument_invocation(false, false)?; let arguments = parser.parse_argument_invocation(false, false)?;
Ok(AstExpr::InterpolatedFunction(InterpolatedFunction { Ok(AstExpr::InterpolatedFunction(Arc::new(InterpolatedFunction {
name: identifier, name: identifier,
arguments: Box::new(arguments), arguments,
span: parser.toks_mut().span_from(start), span: parser.toks_mut().span_from(start),
}) }))
.span(parser.toks_mut().span_from(start))) .span(parser.toks_mut().span_from(start)))
} }
} }
@ -1286,7 +1286,7 @@ impl<'a, 'c, P: StylesheetParser<'a>> ValueParser<'a, 'c, P> {
Ok(AstExpr::FunctionCall(FunctionCallExpr { Ok(AstExpr::FunctionCall(FunctionCallExpr {
namespace: Some(namespace), namespace: Some(namespace),
name: Identifier::from(name), name: Identifier::from(name),
arguments: Box::new(args), arguments: Arc::new(args),
span, span,
}) })
.span(span)) .span(span))
@ -1575,7 +1575,7 @@ impl<'a, 'c, P: StylesheetParser<'a>> ValueParser<'a, 'c, P> {
Ok(AstExpr::FunctionCall(FunctionCallExpr { Ok(AstExpr::FunctionCall(FunctionCallExpr {
namespace: None, namespace: None,
name: Identifier::from(ident), name: Identifier::from(ident),
arguments: Box::new(parser.parse_argument_invocation(false, false)?), arguments: Arc::new(parser.parse_argument_invocation(false, false)?),
span: parser.toks_mut().span_from(start), span: parser.toks_mut().span_from(start),
}) })
.span(parser.toks_mut().span_from(start))) .span(parser.toks_mut().span_from(start)))

View File

@ -1,4 +1,4 @@
use std::fmt; use std::{fmt, sync::Arc};
use crate::interner::InternedString; use crate::interner::InternedString;
@ -100,7 +100,7 @@ pub(crate) enum Unit {
/// Unspecified unit /// Unspecified unit
None, None,
Complex(Box<ComplexUnit>), Complex(Arc<ComplexUnit>),
} }
#[derive(Clone, Debug, PartialEq, Eq, Hash)] #[derive(Clone, Debug, PartialEq, Eq, Hash)]
@ -141,13 +141,13 @@ impl Unit {
} else if denom.is_empty() && numer.len() == 1 { } else if denom.is_empty() && numer.len() == 1 {
numer.pop().unwrap() numer.pop().unwrap()
} else { } else {
Unit::Complex(Box::new(ComplexUnit { numer, denom })) Unit::Complex(Arc::new(ComplexUnit { numer, denom }))
} }
} }
pub fn numer_and_denom(self) -> (Vec<Unit>, Vec<Unit>) { pub fn numer_and_denom(self) -> (Vec<Unit>, Vec<Unit>) {
match self { match self {
Self::Complex(complex) => (complex.numer, complex.denom), Self::Complex(complex) => (complex.numer.clone(), complex.denom.clone()),
Self::None => (Vec::new(), Vec::new()), Self::None => (Vec::new(), Vec::new()),
v => (vec![v], Vec::new()), v => (vec![v], Vec::new()),
} }

View File

@ -129,7 +129,7 @@ impl Value {
span: Span, span: Span,
) -> SassResult<Self> { ) -> SassResult<Self> {
let mut number = self.assert_number(span)?; let mut number = self.assert_number(span)?;
number.as_slash = Some(Box::new((numerator, denom))); number.as_slash = Some(Arc::new((numerator, denom)));
Ok(Value::Dimension(number)) Ok(Value::Dimension(number))
} }
@ -194,7 +194,7 @@ impl Value {
Value::Null => true, Value::Null => true,
Value::String(i, QuoteKind::None) if i.is_empty() => true, Value::String(i, QuoteKind::None) if i.is_empty() => true,
Value::List(_, _, Brackets::Bracketed) => false, Value::List(_, _, Brackets::Bracketed) => false,
Value::List(v, ..) => v.iter().map(Value::is_blank).all(|f| f), Value::List(v, ..) => v.iter().all(Value::is_blank),
Value::ArgList(v, ..) => v.is_blank(), Value::ArgList(v, ..) => v.is_blank(),
_ => false, _ => false,
} }
@ -258,7 +258,7 @@ impl Value {
} }
} }
pub fn as_slash(&self) -> Option<Box<(SassNumber, SassNumber)>> { pub fn as_slash(&self) -> Option<Arc<(SassNumber, SassNumber)>> {
match self { match self {
Value::Dimension(SassNumber { as_slash, .. }) => as_slash.clone(), Value::Dimension(SassNumber { as_slash, .. }) => as_slash.clone(),
_ => None, _ => None,

View File

@ -1,4 +1,7 @@
use std::ops::{Add, Div, Mul, Sub}; use std::{
ops::{Add, Div, Mul, Sub},
sync::Arc,
};
use codemap::Span; use codemap::Span;
@ -11,12 +14,11 @@ use crate::{
use super::Number; use super::Number;
// todo: is as_slash included in eq
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub(crate) struct SassNumber { pub(crate) struct SassNumber {
pub num: Number, pub num: Number,
pub unit: Unit, pub unit: Unit,
pub as_slash: Option<Box<(Self, Self)>>, pub as_slash: Option<Arc<(Self, Self)>>,
} }
pub(crate) fn conversion_factor(from: &Unit, to: &Unit) -> Option<f64> { pub(crate) fn conversion_factor(from: &Unit, to: &Unit) -> Option<f64> {