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 { match toks.peek().unwrap().kind {
'$' => { '$' => {
let Token { pos, .. } = toks.next().unwrap(); 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)?; let whitespace = devour_whitespace_or_comment(toks)?;
if toks.peek().unwrap().kind == ':' { if toks.peek().unwrap().kind == ':' {
toks.next(); toks.next();

View File

@ -290,10 +290,11 @@ impl Selector {
let mut iter = sel_toks.into_iter().peekmore(); let mut iter = sel_toks.into_iter().peekmore();
while let Some(tok) = iter.peek() { while let Some(tok) = iter.peek() {
inner.push(match tok.kind { let Token { kind, pos } = *tok;
_ if is_selector_name_char(tok.kind) => { inner.push(match kind {
_ if is_selector_name_char(kind) => {
inner.push(SelectorKind::Element( inner.push(SelectorKind::Element(
eat_ident_no_interpolation(&mut iter, false)?.node, eat_ident_no_interpolation(&mut iter, false, pos)?.node,
)); ));
continue; continue;
} }
@ -304,14 +305,14 @@ impl Selector {
'.' => { '.' => {
iter.next(); iter.next();
inner.push(SelectorKind::Class( inner.push(SelectorKind::Class(
eat_ident_no_interpolation(&mut iter, false)?.node, eat_ident_no_interpolation(&mut iter, false, pos)?.node,
)); ));
continue; continue;
} }
'#' => { '#' => {
iter.next(); iter.next();
inner.push(SelectorKind::Id( inner.push(SelectorKind::Id(
eat_ident_no_interpolation(&mut iter, false)?.node, eat_ident_no_interpolation(&mut iter, false, pos)?.node,
)); ));
continue; continue;
} }
@ -319,7 +320,7 @@ impl Selector {
iter.next(); iter.next();
is_invisible = true; is_invisible = true;
inner.push(SelectorKind::Placeholder( inner.push(SelectorKind::Placeholder(
eat_ident_no_interpolation(&mut iter, false)?.node, eat_ident_no_interpolation(&mut iter, false, pos)?.node,
)); ));
continue; continue;
} }
@ -405,8 +406,13 @@ impl Selector {
} else { } else {
false false
}; };
if is_selector_name_char(toks.peek().unwrap().kind) { let t = if let Some(tok) = toks.peek() {
let name = eat_ident_no_interpolation(toks, false)?.node; *tok
} else {
todo!()
};
if is_selector_name_char(t.kind) {
let name = eat_ident_no_interpolation(toks, false, t.pos)?.node;
Ok( Ok(
if toks.peek().is_some() && toks.peek().unwrap().kind == '(' { if toks.peek().is_some() && toks.peek().unwrap().kind == '(' {
toks.next(); toks.next();
@ -425,7 +431,7 @@ impl Selector {
}, },
) )
} else { } 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>>( pub(crate) fn eat_ident_no_interpolation<I: Iterator<Item = Token>>(
toks: &mut PeekMoreIterator<I>, toks: &mut PeekMoreIterator<I>,
unit: bool, unit: bool,
span_before: Span,
) -> SassResult<Spanned<String>> { ) -> 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(); let mut text = String::new();
if toks.peek().unwrap().kind == '-' { if toks.peek().unwrap().kind == '-' {
toks.next(); toks.next();

View File

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

View File

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

View File

@ -541,3 +541,11 @@ test!(
"|f {\n color: &;\n}\n", "|f {\n color: &;\n}\n",
"|f {\n color: |f;\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."
);