remove unwrap and todo! from attribute parsing

This commit is contained in:
ConnorSkees 2020-04-02 21:23:23 -04:00
parent 6923869b7e
commit 6e8c226834

View File

@ -23,61 +23,51 @@ impl Attribute {
super_selector: &Selector, super_selector: &Selector,
) -> SassResult<SelectorKind> { ) -> SassResult<SelectorKind> {
devour_whitespace(toks); devour_whitespace(toks);
let attr = if let Some(t) = toks.next() { let attr = match toks.next().ok_or("Expected identifier.")?.kind {
match t.kind { v @ 'a'..='z' | v @ 'A'..='Z' | v @ '-' | v @ '_' => {
v @ 'a'..='z' | v @ 'A'..='Z' | v @ '-' | v @ '_' => { format!("{}{}", v, eat_ident(toks, scope, super_selector)?)
format!("{}{}", v, eat_ident(toks, scope, super_selector)?)
}
'#' if toks.next().unwrap().kind == '{' => {
parse_interpolation(toks, scope, super_selector)?.to_string()
}
q @ '"' | q @ '\'' => {
parse_quoted_string(toks, scope, q, super_selector)?.to_string()
}
_ => return Err("Expected identifier.".into()),
} }
} else { '#' if toks.next().ok_or("Expected expression.")?.kind == '{' => {
todo!() parse_interpolation(toks, scope, super_selector)?.to_string()
}
q @ '"' | q @ '\'' => parse_quoted_string(toks, scope, q, super_selector)?.to_string(),
_ => return Err("Expected identifier.".into()),
}; };
devour_whitespace(toks); devour_whitespace(toks);
let kind = if let Some(t) = toks.next() { let kind = match toks.next().ok_or("expected \"{\".")?.kind {
match t.kind { v @ 'a'..='z' | v @ 'A'..='Z' => {
v @ 'a'..='z' | v @ 'A'..='Z' => { match toks.next().ok_or("expected \"]\".")?.kind {
match toks.next().unwrap().kind { ']' => {}
']' => {} _ => return Err("expected \"]\".".into()),
_ => return Err("expected \"]\".".into()),
}
return Ok(SelectorKind::Attribute(Attribute {
kind: AttributeKind::Any,
attr,
value: String::new(),
modifier: Some(v),
}));
} }
']' => { return Ok(SelectorKind::Attribute(Attribute {
return Ok(SelectorKind::Attribute(Attribute { kind: AttributeKind::Any,
kind: AttributeKind::Any, attr,
attr, value: String::new(),
value: String::new(), modifier: Some(v),
modifier: None, }));
}));
}
'=' => AttributeKind::Equals,
'~' => AttributeKind::Include,
'|' => AttributeKind::Dash,
'^' => AttributeKind::Prefix,
'$' => AttributeKind::Suffix,
'*' => AttributeKind::Contains,
_ => return Err("Expected \"]\".".into()),
} }
} else { ']' => {
todo!() return Ok(SelectorKind::Attribute(Attribute {
kind: AttributeKind::Any,
attr,
value: String::new(),
modifier: None,
}));
}
'=' => AttributeKind::Equals,
'~' => AttributeKind::Include,
'|' => AttributeKind::Dash,
'^' => AttributeKind::Prefix,
'$' => AttributeKind::Suffix,
'*' => AttributeKind::Contains,
_ => return Err("Expected \"]\".".into()),
}; };
if kind != AttributeKind::Equals { if kind != AttributeKind::Equals {
match toks.next().unwrap().kind { match toks.next().ok_or("expected \"=\".")?.kind {
'=' => {} '=' => {}
_ => return Err("expected \"=\".".into()), _ => return Err("expected \"=\".".into()),
} }
@ -85,43 +75,33 @@ impl Attribute {
devour_whitespace(toks); devour_whitespace(toks);
let value = if let Some(t) = toks.next() { let value = match toks.next().ok_or("Expected identifier.")?.kind {
match t.kind { v @ 'a'..='z' | v @ 'A'..='Z' | v @ '-' | v @ '_' => {
v @ 'a'..='z' | v @ 'A'..='Z' | v @ '-' | v @ '_' => { format!("{}{}", v, eat_ident(toks, scope, super_selector)?)
format!("{}{}", v, eat_ident(toks, scope, super_selector)?)
}
q @ '"' | q @ '\'' => {
parse_quoted_string(toks, scope, q, super_selector)?.to_string()
}
_ => return Err("Expected identifier.".into()),
} }
} else { q @ '"' | q @ '\'' => parse_quoted_string(toks, scope, q, super_selector)?.to_string(),
todo!() _ => return Err("Expected identifier.".into()),
}; };
devour_whitespace(toks); devour_whitespace(toks);
let modifier = if let Some(t) = toks.next() { let modifier = match toks.next().ok_or("expected \"]\".")?.kind {
match t.kind { ']' => {
']' => { return Ok(SelectorKind::Attribute(Attribute {
return Ok(SelectorKind::Attribute(Attribute { kind,
kind, attr,
attr, value,
value, modifier: None,
modifier: None, }))
}))
}
v @ 'a'..='z' | v @ 'A'..='Z' => {
match toks.next().unwrap().kind {
']' => {}
_ => return Err("expected \"]\".".into()),
}
Some(v)
}
_ => return Err("Expected \"]\".".into()),
} }
} else { v @ 'a'..='z' | v @ 'A'..='Z' => {
return Err("expected \"]\".".into()); match toks.next().ok_or("expected \"]\".")?.kind {
']' => {}
_ => return Err("expected \"]\".".into()),
}
Some(v)
}
_ => return Err("Expected \"]\".".into()),
}; };
Ok(SelectorKind::Attribute(Attribute { Ok(SelectorKind::Attribute(Attribute {