remove unwrap in peek_ident

This commit is contained in:
ConnorSkees 2020-05-24 15:53:51 -04:00
parent e5cceb60ec
commit 8d4b4bedbe
7 changed files with 25 additions and 19 deletions

View File

@ -105,10 +105,10 @@ pub(crate) fn parse_for<I: Iterator<Item = Token>>(
devour_whitespace(toks); devour_whitespace(toks);
let mut from_toks = Vec::new(); let mut from_toks = Vec::new();
let mut through = 0; let mut through = 0;
while let Some(tok) = toks.peek() { while let Some(tok) = toks.peek().cloned() {
match tok.kind { match tok.kind {
't' | 'T' | '\\' => { 't' | 'T' | '\\' => {
let ident = peek_ident_no_interpolation(toks, false)?; let ident = peek_ident_no_interpolation(toks, false, tok.pos)?;
match ident.node.to_ascii_lowercase().as_str() { match ident.node.to_ascii_lowercase().as_str() {
"through" => { "through" => {
through = 1; through = 1;

View File

@ -49,12 +49,7 @@ impl If {
Some(t) => t.pos, Some(t) => t.pos,
None => return Err(("Expected expression.", span_before).into()), None => return Err(("Expected expression.", span_before).into()),
}; };
let init_cond = Value::from_vec( let init_cond = Value::from_vec(init_cond_toks, scope, super_selector, span_before)?;
init_cond_toks,
scope,
super_selector,
span_before,
)?;
devour_whitespace_or_comment(toks)?; devour_whitespace_or_comment(toks)?;
let mut init_toks = read_until_closing_curly_brace(toks)?; let mut init_toks = read_until_closing_curly_brace(toks)?;
if let Some(tok) = toks.next() { if let Some(tok) = toks.next() {
@ -69,10 +64,10 @@ impl If {
let mut else_ = Vec::new(); let mut else_ = Vec::new();
loop { loop {
match toks.peek() { match toks.peek().cloned() {
Some(Token { kind: '@', .. }) => { Some(Token { kind: '@', pos }) => {
toks.peek_forward(1); toks.peek_forward(1);
let mut ident = peek_ident_no_interpolation(toks, false)?; let mut ident = peek_ident_no_interpolation(toks, false, pos)?;
ident.node.make_ascii_lowercase(); ident.node.make_ascii_lowercase();
if ident.as_str() != "else" { if ident.as_str() != "else" {
toks.reset_view(); toks.reset_view();

View File

@ -287,7 +287,7 @@ pub(crate) fn eat_expr<I: Iterator<Item = Token>>(
continue; continue;
} }
let name = peek_ident_no_interpolation(toks, false)?; let name = peek_ident_no_interpolation(toks, false, tok.pos)?;
let whitespace = peek_whitespace(toks); let whitespace = peek_whitespace(toks);
if toks.peek().ok_or(("expected \":\".", name.span))?.kind == ':' { if toks.peek().ok_or(("expected \":\".", name.span))?.kind == ':' {

View File

@ -190,8 +190,8 @@ impl<'a> StyleSheetParser<'a> {
continue; continue;
} }
'$' => { '$' => {
self.lexer.next(); let span_before = self.lexer.next().unwrap().pos;
let name = peek_ident_no_interpolation(self.lexer, false)?; let name = peek_ident_no_interpolation(self.lexer, false, span_before)?;
let whitespace = peek_whitespace(self.lexer); let whitespace = peek_whitespace(self.lexer);
match self.lexer.peek() { match self.lexer.peek() {
@ -202,8 +202,12 @@ impl<'a> StyleSheetParser<'a> {
.for_each(drop); .for_each(drop);
devour_whitespace(self.lexer); devour_whitespace(self.lexer);
let VariableDecl { val, default, .. } = let VariableDecl { val, default, .. } = eat_variable_value(
eat_variable_value(self.lexer, &Scope::new(), &Selector::new(), pos)?; self.lexer,
&Scope::new(),
&Selector::new(),
pos,
)?;
if !(default && global_var_exists(&name.node)) { if !(default && global_var_exists(&name.node)) {
insert_global_var(&name.node, val)?; insert_global_var(&name.node, val)?;

View File

@ -160,9 +160,12 @@ pub(crate) fn peek_escape<I: Iterator<Item = Token>>(
pub(crate) fn peek_ident_no_interpolation<I: Iterator<Item = Token>>( pub(crate) fn peek_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>> {
// todo: panics on "$" let mut span = toks
let mut span = toks.peek().unwrap().pos(); .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.peek_forward(1); toks.peek_forward(1);

View File

@ -102,7 +102,7 @@ pub(crate) fn eat_variable_value<I: Iterator<Item = Token>>(
return Err(("Expected identifier.", pos).into()); return Err(("Expected identifier.", pos).into());
} }
// todo: it should not be possible to declare the same flag more than once // todo: it should not be possible to declare the same flag more than once
let mut ident = peek_ident_no_interpolation(toks, false)?; let mut ident = peek_ident_no_interpolation(toks, false, pos)?;
ident.node.make_ascii_lowercase(); ident.node.make_ascii_lowercase();
match ident.node.as_str() { match ident.node.as_str() {
"global" => { "global" => {

View File

@ -224,3 +224,7 @@ error!(
ident_colon_closing_brace, ident_colon_closing_brace,
"r:}", "Error: Expected expression." "r:}", "Error: Expected expression."
); );
error!(
dollar_sign_alone,
"$", "Error: Expected identifier."
);