remove panics from missing identifiers

This commit is contained in:
ConnorSkees 2020-05-24 08:56:53 -04:00
parent 2aaaf59e4f
commit 042dbfa914
6 changed files with 37 additions and 14 deletions

View File

@ -335,7 +335,7 @@ pub(crate) fn eat_call_args<I: Iterator<Item = Token>>(
match toks.peek().unwrap().kind {
'$' => {
let Token { pos, .. } = toks.next().unwrap();
let v = eat_ident_no_interpolation(toks, false)?;
let v = eat_ident_no_interpolation(toks, false, pos)?;
let whitespace = devour_whitespace_or_comment(toks)?;
if toks.peek().unwrap().kind == ':' {
toks.next();

View File

@ -290,10 +290,11 @@ impl Selector {
let mut iter = sel_toks.into_iter().peekmore();
while let Some(tok) = iter.peek() {
inner.push(match tok.kind {
_ if is_selector_name_char(tok.kind) => {
let Token { kind, pos } = *tok;
inner.push(match kind {
_ if is_selector_name_char(kind) => {
inner.push(SelectorKind::Element(
eat_ident_no_interpolation(&mut iter, false)?.node,
eat_ident_no_interpolation(&mut iter, false, pos)?.node,
));
continue;
}
@ -304,14 +305,14 @@ impl Selector {
'.' => {
iter.next();
inner.push(SelectorKind::Class(
eat_ident_no_interpolation(&mut iter, false)?.node,
eat_ident_no_interpolation(&mut iter, false, pos)?.node,
));
continue;
}
'#' => {
iter.next();
inner.push(SelectorKind::Id(
eat_ident_no_interpolation(&mut iter, false)?.node,
eat_ident_no_interpolation(&mut iter, false, pos)?.node,
));
continue;
}
@ -319,7 +320,7 @@ impl Selector {
iter.next();
is_invisible = true;
inner.push(SelectorKind::Placeholder(
eat_ident_no_interpolation(&mut iter, false)?.node,
eat_ident_no_interpolation(&mut iter, false, pos)?.node,
));
continue;
}
@ -405,8 +406,13 @@ impl Selector {
} else {
false
};
if is_selector_name_char(toks.peek().unwrap().kind) {
let name = eat_ident_no_interpolation(toks, false)?.node;
let t = if let Some(tok) = toks.peek() {
*tok
} else {
todo!()
};
if is_selector_name_char(t.kind) {
let name = eat_ident_no_interpolation(toks, false, t.pos)?.node;
Ok(
if toks.peek().is_some() && toks.peek().unwrap().kind == '(' {
toks.next();
@ -425,7 +431,7 @@ impl Selector {
},
)
} else {
todo!()
return Err(("Expected identifier.", t.pos).into());
}
}

View File

@ -232,8 +232,12 @@ pub(crate) fn eat_ident<I: Iterator<Item = Token>>(
pub(crate) fn eat_ident_no_interpolation<I: Iterator<Item = Token>>(
toks: &mut PeekMoreIterator<I>,
unit: bool,
span_before: Span,
) -> SassResult<Spanned<String>> {
let mut span = toks.peek().unwrap().pos();
let mut span = toks
.peek()
.ok_or(("Expected identifier.", span_before))?
.pos();
let mut text = String::new();
if toks.peek().unwrap().kind == '-' {
toks.next();

View File

@ -679,9 +679,10 @@ impl Value {
Err(e) => return Some(Err(e)),
};
let unit = if let Some(tok) = toks.peek() {
match tok.kind {
let Token { kind, pos } = *tok;
match kind {
'a'..='z' | 'A'..='Z' | '_' | '\\' => {
let u = match eat_ident_no_interpolation(toks, true) {
let u = match eat_ident_no_interpolation(toks, true, pos) {
Ok(v) => v,
Err(e) => return Some(Err(e)),
};
@ -804,7 +805,7 @@ impl Value {
}
'$' => {
toks.next();
let val = match eat_ident_no_interpolation(toks, false) {
let val = match eat_ident_no_interpolation(toks, false, span) {
Ok(v) => v,
Err(e) => return Some(Err(e)),
};

View File

@ -109,6 +109,10 @@ test!(
"a {\n color: red;\n}\n"
);
error!(nothing_after_if, "@if", "Error: Expected expression.");
error!(
nothing_after_dollar,
"@if ${}", "Error: Expected identifier."
);
error!(no_condition, "@if{}", "Error: Expected expression.");
error!(
nothing_after_open_curly,

View File

@ -541,3 +541,11 @@ test!(
"|f {\n color: &;\n}\n",
"|f {\n color: |f;\n}\n"
);
error!(nothing_after_period, ". {}", "Error: Expected identifier.");
error!(nothing_after_hash, "# {}", "Error: Expected identifier.");
error!(nothing_after_percent, "% {}", "Error: Expected identifier.");
error!(nothing_after_colon, ": {}", "Error: Expected identifier.");
error!(
non_ident_after_colon,
":#ab {}", "Error: Expected identifier."
);