avoid unnecessary cloning of identifiers

This commit is contained in:
Connor Skees 2020-07-07 17:22:18 -04:00
parent 817c808826
commit 9b4815e75f
3 changed files with 22 additions and 22 deletions

View File

@ -10,6 +10,7 @@ use crate::{
media::MediaRule,
AtRuleKind, Content, SupportsRule, UnknownAtRule,
},
common::Identifier,
error::SassResult,
scope::Scope,
selector::{
@ -514,10 +515,12 @@ impl<'a> Parser<'a> {
let mut scope = 0;
while let Some(tok) = self.toks.next() {
match tok.kind {
'}' => if scope == 0 {
break
} else {
scope -= 1;
'}' => {
if scope == 0 {
break;
} else {
scope -= 1;
}
}
'{' => scope += 1,
_ => continue,
@ -640,8 +643,10 @@ impl<'a> Parser<'a> {
.toks
.next()
.ok_or(("expected \"$\".", self.span_before))?;
let var = match next.kind {
'$' => self.parse_identifier_no_interpolation(false)?,
let var: Spanned<Identifier> = match next.kind {
'$' => self
.parse_identifier_no_interpolation(false)?
.map_node(|i| i.into()),
_ => return Err(("expected \"$\".", self.span_before).into()),
};
self.whitespace();
@ -872,7 +877,7 @@ impl<'a> Parser<'a> {
fn parse_each(&mut self) -> SassResult<Vec<Stmt>> {
self.whitespace();
let mut vars = Vec::new();
let mut vars: Vec<Spanned<Identifier>> = Vec::new();
loop {
let next = self
@ -881,7 +886,7 @@ impl<'a> Parser<'a> {
.ok_or(("expected \"$\".", self.span_before))?;
match next.kind {
'$' => vars.push(self.parse_identifier()?),
'$' => vars.push(self.parse_identifier()?.map_node(|i| i.into())),
_ => return Err(("expected \"$\".", next.pos()).into()),
}
self.whitespace();
@ -919,7 +924,7 @@ impl<'a> Parser<'a> {
for row in iter {
if vars.len() == 1 {
self.scopes.last_mut().insert_var(
&vars[0].node,
vars[0].node.clone(),
Spanned {
node: row,
span: vars[0].span,
@ -932,7 +937,7 @@ impl<'a> Parser<'a> {
.chain(std::iter::once(Value::Null).cycle()),
) {
self.scopes.last_mut().insert_var(
&var.node,
var.node.clone(),
Spanned {
node: val,
span: var.span,

View File

@ -521,12 +521,12 @@ impl<'a> Parser<'a> {
'$' => {
self.toks.next();
let val = match self.parse_identifier_no_interpolation(false) {
Ok(v) => v,
Ok(v) => v.map_node(|i| i.into()),
Err(e) => return Some(Err(e)),
};
let span = val.span;
IntermediateValue::Value(HigherIntermediateValue::Literal(
match self.scopes.last().get_var(val, self.global_scope) {
match self.scopes.last().get_var(&val, self.global_scope) {
Ok(v) => v,
Err(e) => return Some(Err(e)),
}

View File

@ -35,24 +35,19 @@ impl Scope {
}
}
pub fn get_var<T: Into<Identifier>>(
pub fn get_var(
&self,
name: Spanned<T>,
name: &Spanned<Identifier>,
global_scope: &Scope,
) -> SassResult<Spanned<Value>> {
let name = name.map_node(Into::into);
match self.vars.get(&name.node) {
Some(v) => Ok(v.clone()),
None => global_scope.get_var_no_global(&name),
None => global_scope.get_var_no_global(name),
}
}
pub fn insert_var<T: Into<Identifier>>(
&mut self,
s: T,
v: Spanned<Value>,
) -> Option<Spanned<Value>> {
self.vars.insert(s.into(), v)
pub fn insert_var(&mut self, s: Identifier, v: Spanned<Value>) -> Option<Spanned<Value>> {
self.vars.insert(s, v)
}
pub fn var_exists_no_global(&self, name: &Identifier) -> bool {