reduce nesting in ident parsing
This commit is contained in:
parent
fda95683ce
commit
a6e03e4ae1
@ -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]);
|
||||||
|
@ -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,79 +512,76 @@ 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(),
|
||||||
span,
|
span,
|
||||||
}) {
|
}) {
|
||||||
Ok(f) => f,
|
Ok(f) => f,
|
||||||
Err(_) => match GLOBAL_FUNCTIONS.get(s.replace('_', "-").as_str()) {
|
Err(_) => match GLOBAL_FUNCTIONS.get(s.replace('_', "-").as_str()) {
|
||||||
Some(f) => {
|
Some(f) => {
|
||||||
return Ok(IntermediateValue::Value(Spanned {
|
return Ok(IntermediateValue::Value(Spanned {
|
||||||
node: f.0(eat_call_args(toks)?, scope, super_selector)?,
|
node: f.0(eat_call_args(toks)?, scope, super_selector)?,
|
||||||
span,
|
span,
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
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)?)
|
||||||
}
|
}
|
||||||
// "min" => {}
|
// "min" => {}
|
||||||
// "max" => {}
|
// "max" => {}
|
||||||
"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)?
|
|
||||||
.to_css_string(scope, super_selector)?,
|
|
||||||
),
|
|
||||||
},
|
|
||||||
_ => s.push_str(
|
|
||||||
&eat_call_args(toks)?.to_css_string(scope, super_selector)?,
|
&eat_call_args(toks)?.to_css_string(scope, super_selector)?,
|
||||||
),
|
),
|
||||||
}
|
},
|
||||||
return Ok(IntermediateValue::Value(Spanned {
|
_ => s.push_str(
|
||||||
node: Value::Ident(s, QuoteKind::None),
|
&eat_call_args(toks)?.to_css_string(scope, super_selector)?,
|
||||||
span,
|
),
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
},
|
return Ok(IntermediateValue::Value(Spanned {
|
||||||
};
|
|
||||||
Ok(IntermediateValue::Value(
|
|
||||||
func.eval(eat_call_args(toks)?, scope, super_selector)?
|
|
||||||
.span(span),
|
|
||||||
))
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
if let Some(c) = NAMED_COLORS.get_by_left(&s.as_str()) {
|
|
||||||
Ok(IntermediateValue::Value(Spanned {
|
|
||||||
node: Value::Color(Box::new(Color::new(c[0], c[1], c[2], c[3], s))),
|
|
||||||
span,
|
|
||||||
}))
|
|
||||||
} else {
|
|
||||||
Ok(match s.to_ascii_lowercase().as_str() {
|
|
||||||
"true" => IntermediateValue::Value(Value::True.span(span)),
|
|
||||||
"false" => IntermediateValue::Value(Value::False.span(span)),
|
|
||||||
"null" => IntermediateValue::Value(Value::Null.span(span)),
|
|
||||||
"not" => IntermediateValue::Op(Spanned {
|
|
||||||
node: Op::Not,
|
|
||||||
span,
|
|
||||||
}),
|
|
||||||
"and" => IntermediateValue::Op(Spanned {
|
|
||||||
node: Op::And,
|
|
||||||
span,
|
|
||||||
}),
|
|
||||||
"or" => IntermediateValue::Op(Spanned { node: Op::Or, span }),
|
|
||||||
_ => IntermediateValue::Value(Spanned {
|
|
||||||
node: Value::Ident(s, QuoteKind::None),
|
node: Value::Ident(s, QuoteKind::None),
|
||||||
span,
|
span,
|
||||||
}),
|
}));
|
||||||
})
|
}
|
||||||
}
|
},
|
||||||
}
|
};
|
||||||
|
return Ok(IntermediateValue::Value(
|
||||||
|
func.eval(eat_call_args(toks)?, scope, super_selector)?
|
||||||
|
.span(span),
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let Some(c) = NAMED_COLORS.get_by_left(&s.as_str()) {
|
||||||
|
return Ok(IntermediateValue::Value(Spanned {
|
||||||
|
node: Value::Color(Box::new(Color::new(c[0], c[1], c[2], c[3], s))),
|
||||||
|
span,
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(match s.to_ascii_lowercase().as_str() {
|
||||||
|
"true" => IntermediateValue::Value(Value::True.span(span)),
|
||||||
|
"false" => IntermediateValue::Value(Value::False.span(span)),
|
||||||
|
"null" => IntermediateValue::Value(Value::Null.span(span)),
|
||||||
|
"not" => IntermediateValue::Op(Spanned {
|
||||||
|
node: Op::Not,
|
||||||
|
span,
|
||||||
|
}),
|
||||||
|
"and" => IntermediateValue::Op(Spanned {
|
||||||
|
node: Op::And,
|
||||||
|
span,
|
||||||
|
}),
|
||||||
|
"or" => IntermediateValue::Op(Spanned { node: Op::Or, span }),
|
||||||
|
_ => IntermediateValue::Value(Spanned {
|
||||||
|
node: Value::Ident(s, QuoteKind::None),
|
||||||
|
span,
|
||||||
|
}),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_intermediate_value<I: Iterator<Item = Token>>(
|
fn parse_intermediate_value<I: Iterator<Item = Token>>(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user