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

View File

@ -521,12 +521,12 @@ impl<'a> Parser<'a> {
'$' => { '$' => {
self.toks.next(); self.toks.next();
let val = match self.parse_identifier_no_interpolation(false) { 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)), Err(e) => return Some(Err(e)),
}; };
let span = val.span; let span = val.span;
IntermediateValue::Value(HigherIntermediateValue::Literal( 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, Ok(v) => v,
Err(e) => return Some(Err(e)), 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, &self,
name: Spanned<T>, name: &Spanned<Identifier>,
global_scope: &Scope, global_scope: &Scope,
) -> SassResult<Spanned<Value>> { ) -> SassResult<Spanned<Value>> {
let name = name.map_node(Into::into);
match self.vars.get(&name.node) { match self.vars.get(&name.node) {
Some(v) => Ok(v.clone()), 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>>( pub fn insert_var(&mut self, s: Identifier, v: Spanned<Value>) -> Option<Spanned<Value>> {
&mut self, self.vars.insert(s, v)
s: T,
v: Spanned<Value>,
) -> Option<Spanned<Value>> {
self.vars.insert(s.into(), v)
} }
pub fn var_exists_no_global(&self, name: &Identifier) -> bool { pub fn var_exists_no_global(&self, name: &Identifier) -> bool {