reduce nesting in ident parsing

This commit is contained in:
ConnorSkees 2020-04-30 16:02:40 -04:00
parent fda95683ce
commit a6e03e4ae1
2 changed files with 66 additions and 68 deletions

View File

@ -4,7 +4,7 @@
use bimap::BiMap; use bimap::BiMap;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
pub(crate) static NAMED_COLORS: Lazy<BiMap<&str, [u8; 4]>> = Lazy::new(|| { pub(crate) static NAMED_COLORS: Lazy<BiMap<&'static str, [u8; 4]>> = Lazy::new(|| {
let mut m = BiMap::with_capacity(150); let mut m = BiMap::with_capacity(150);
m.insert("aliceblue", [0xF0, 0xF8, 0xFF, 0xFF]); m.insert("aliceblue", [0xF0, 0xF8, 0xFF, 0xFF]);
m.insert("antiquewhite", [0xFA, 0xEB, 0xD7, 0xFF]); m.insert("antiquewhite", [0xFA, 0xEB, 0xD7, 0xFF]);

View File

@ -502,6 +502,7 @@ impl Value {
super_selector: &Selector, super_selector: &Selector,
) -> SassResult<IntermediateValue> { ) -> SassResult<IntermediateValue> {
let Spanned { node: mut s, span } = eat_ident(toks, scope, super_selector)?; let Spanned { node: mut s, span } = eat_ident(toks, scope, super_selector)?;
if s == "progid" && toks.peek().is_some() && toks.peek().unwrap().kind == ':' { if s == "progid" && toks.peek().is_some() && toks.peek().unwrap().kind == ':' {
toks.next(); toks.next();
s.push(':'); s.push(':');
@ -511,8 +512,8 @@ impl Value {
span, span,
})); }));
} }
match toks.peek() {
Some(Token { kind: '(', .. }) => { if let Some(Token { kind: '(', .. }) = toks.peek() {
toks.next(); toks.next();
let func = match scope.get_fn(Spanned { let func = match scope.get_fn(Spanned {
node: s.clone(), node: s.clone(),
@ -527,7 +528,7 @@ impl Value {
})) }))
} }
None => { None => {
match s.as_str() { match s.to_ascii_lowercase().as_str() {
"calc" | "element" | "expression" => { "calc" | "element" | "expression" => {
s.push_str(&eat_calc_args(toks, scope, super_selector)?) s.push_str(&eat_calc_args(toks, scope, super_selector)?)
} }
@ -536,8 +537,7 @@ impl Value {
"url" => match try_eat_url(toks, scope, super_selector)? { "url" => match try_eat_url(toks, scope, super_selector)? {
Some(val) => s = val, Some(val) => s = val,
None => s.push_str( None => s.push_str(
&eat_call_args(toks)? &eat_call_args(toks)?.to_css_string(scope, super_selector)?,
.to_css_string(scope, super_selector)?,
), ),
}, },
_ => s.push_str( _ => s.push_str(
@ -551,18 +551,19 @@ impl Value {
} }
}, },
}; };
Ok(IntermediateValue::Value( return Ok(IntermediateValue::Value(
func.eval(eat_call_args(toks)?, scope, super_selector)? func.eval(eat_call_args(toks)?, scope, super_selector)?
.span(span), .span(span),
)) ));
} }
_ => {
if let Some(c) = NAMED_COLORS.get_by_left(&s.as_str()) { if let Some(c) = NAMED_COLORS.get_by_left(&s.as_str()) {
Ok(IntermediateValue::Value(Spanned { return Ok(IntermediateValue::Value(Spanned {
node: Value::Color(Box::new(Color::new(c[0], c[1], c[2], c[3], s))), node: Value::Color(Box::new(Color::new(c[0], c[1], c[2], c[3], s))),
span, span,
})) }));
} else { }
Ok(match s.to_ascii_lowercase().as_str() { Ok(match s.to_ascii_lowercase().as_str() {
"true" => IntermediateValue::Value(Value::True.span(span)), "true" => IntermediateValue::Value(Value::True.span(span)),
"false" => IntermediateValue::Value(Value::False.span(span)), "false" => IntermediateValue::Value(Value::False.span(span)),
@ -582,9 +583,6 @@ impl Value {
}), }),
}) })
} }
}
}
}
fn parse_intermediate_value<I: Iterator<Item = Token>>( fn parse_intermediate_value<I: Iterator<Item = Token>>(
toks: &mut PeekMoreIterator<I>, toks: &mut PeekMoreIterator<I>,