reduce allocations for @if

This commit is contained in:
Connor Skees 2020-07-07 17:50:18 -04:00
parent 33f81f5bbe
commit aa209702d2

View File

@ -543,17 +543,30 @@ impl<'a> Parser<'a> {
Some(t) => t.pos, Some(t) => t.pos,
None => return Err(("Expected expression.", self.span_before).into()), None => return Err(("Expected expression.", self.span_before).into()),
}; };
if self.toks.peek().is_none() {
return Err(("expected \"}\".", span_before).into());
}
self.whitespace_or_comment(); self.whitespace_or_comment();
if self.parse_value_from_vec(init_cond_toks)?.is_true() { if self.parse_value_from_vec(init_cond_toks)?.is_true() {
found_true = true; found_true = true;
let mut init_toks = read_until_closing_curly_brace(self.toks)?; body = Parser {
if let Some(tok) = self.toks.next() { toks: self.toks,
init_toks.push(tok); map: self.map,
} else { path: self.path,
return Err(("expected \"}\".", span_before).into()); scopes: self.scopes,
global_scope: self.global_scope,
super_selectors: self.super_selectors,
span_before: self.span_before,
content: self.content,
flags: self.flags | ContextFlags::IN_CONTROL_FLOW,
at_root: self.at_root,
at_root_has_selector: self.at_root_has_selector,
extender: self.extender,
} }
body = init_toks; .parse_stmt()?;
} else { } else {
self.throw_away_until_closing_curly_brace(); self.throw_away_until_closing_curly_brace();
} }
@ -572,7 +585,7 @@ impl<'a> Parser<'a> {
} else { } else {
break; break;
} }
self.whitespace(); self.whitespace_or_comment();
if let Some(tok) = self.toks.peek().cloned() { if let Some(tok) = self.toks.peek().cloned() {
match tok.kind { match tok.kind {
'i' if matches!( 'i' if matches!(
@ -587,38 +600,8 @@ impl<'a> Parser<'a> {
self.toks.next(); self.toks.next();
if !found_true && self.parse_value_from_vec(cond)?.is_true() { if !found_true && self.parse_value_from_vec(cond)?.is_true() {
found_true = true; found_true = true;
body = read_until_closing_curly_brace(self.toks)?; body = Parser {
// todo: ensure there is a `{` toks: self.toks,
self.toks.next();
} else {
self.throw_away_until_closing_curly_brace();
}
self.whitespace();
}
'{' => {
self.toks.next();
if found_true {
self.throw_away_until_closing_curly_brace();
} else {
found_true = true;
body = read_until_closing_curly_brace(self.toks)?;
self.toks.next();
}
break;
}
_ => {
return Err(("expected \"{\".", tok.pos()).into());
}
}
} else {
break;
}
}
self.whitespace();
if found_true {
Parser {
toks: &mut body.into_iter().peekmore(),
map: self.map, map: self.map,
path: self.path, path: self.path,
scopes: self.scopes, scopes: self.scopes,
@ -631,10 +614,48 @@ impl<'a> Parser<'a> {
at_root_has_selector: self.at_root_has_selector, at_root_has_selector: self.at_root_has_selector,
extender: self.extender, extender: self.extender,
} }
.parse() .parse_stmt()?;
// todo: ensure there is a `{`
self.toks.next();
} else { } else {
Ok(Vec::new()) self.throw_away_until_closing_curly_brace();
} }
self.whitespace();
}
'{' => {
self.toks.next();
if found_true {
self.throw_away_until_closing_curly_brace();
break;
} else {
return Parser {
toks: self.toks,
map: self.map,
path: self.path,
scopes: self.scopes,
global_scope: self.global_scope,
super_selectors: self.super_selectors,
span_before: self.span_before,
content: self.content,
flags: self.flags | ContextFlags::IN_CONTROL_FLOW,
at_root: self.at_root,
at_root_has_selector: self.at_root_has_selector,
extender: self.extender,
}
.parse_stmt();
}
}
_ => {
return Err(("expected \"{\".", tok.pos()).into());
}
}
} else {
break;
}
}
self.whitespace();
Ok(body)
} }
fn parse_for(&mut self) -> SassResult<Vec<Stmt>> { fn parse_for(&mut self) -> SassResult<Vec<Stmt>> {