From f52d784756fed85857f8fe4e17a4b29f86c2f19f Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Sun, 24 May 2020 10:16:47 -0400 Subject: [PATCH] remove panic from unclosed call args --- src/args.rs | 4 ++-- src/atrule/mixin.rs | 2 +- src/value/parse.rs | 11 ++++++----- tests/args.rs | 7 +++++++ tests/if.rs | 1 + 5 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/args.rs b/src/args.rs index 6f25a6f..c9f5ffb 100644 --- a/src/args.rs +++ b/src/args.rs @@ -324,13 +324,13 @@ pub(crate) fn eat_func_args>( pub(crate) fn eat_call_args>( toks: &mut PeekMoreIterator, + span_before: Span, ) -> SassResult { let mut args: HashMap> = HashMap::new(); devour_whitespace_or_comment(toks)?; let mut name = String::new(); let mut val: Vec = Vec::new(); - // todo: panics on a { color:rgb(; } - let mut span = toks.peek().unwrap().pos(); + let mut span = toks.peek().ok_or(("expected \")\".", span_before))?.pos(); loop { match toks.peek().unwrap().kind { '$' => { diff --git a/src/atrule/mixin.rs b/src/atrule/mixin.rs index d729690..485a33e 100644 --- a/src/atrule/mixin.rs +++ b/src/atrule/mixin.rs @@ -200,7 +200,7 @@ pub(crate) fn eat_include>( match tok.kind { ';' => CallArgs::new(name.span), '(' => { - let tmp = eat_call_args(toks)?; + let tmp = eat_call_args(toks, tok.pos)?; devour_whitespace_or_comment(toks)?; if let Some(tok) = toks.peek() { match tok.kind { diff --git a/src/value/parse.rs b/src/value/parse.rs index 6b34b4f..a4f9216 100644 --- a/src/value/parse.rs +++ b/src/value/parse.rs @@ -572,7 +572,8 @@ impl Value { }); } - if let Some(Token { kind: '(', .. }) = toks.peek() { + if let Some(Token { kind: '(', pos }) = toks.peek() { + let pos = *pos; let as_ident = Identifier::from(&s); toks.next(); let func = match scope.get_fn(Spanned { @@ -583,7 +584,7 @@ impl Value { Err(_) => match GLOBAL_FUNCTIONS.get(as_ident.into_inner().as_str()) { Some(f) => { return Ok(IntermediateValue::Value(f.0( - eat_call_args(toks)?, + eat_call_args(toks, pos)?, scope, super_selector, )?) @@ -600,11 +601,11 @@ impl Value { "url" => match try_eat_url(toks, scope, super_selector)? { Some(val) => s = val, None => s.push_str( - &eat_call_args(toks)?.to_css_string(scope, super_selector)?, + &eat_call_args(toks, pos)?.to_css_string(scope, super_selector)?, ), }, _ => s.push_str( - &eat_call_args(toks)?.to_css_string(scope, super_selector)?, + &eat_call_args(toks, pos)?.to_css_string(scope, super_selector)?, ), } return Ok( @@ -614,7 +615,7 @@ impl Value { }, }; return Ok(IntermediateValue::Value(func.eval( - eat_call_args(toks)?, + eat_call_args(toks, pos)?, scope, super_selector, )?) diff --git a/tests/args.rs b/tests/args.rs index d72e02e..8c8fe90 100644 --- a/tests/args.rs +++ b/tests/args.rs @@ -45,3 +45,10 @@ test!( "@function foo($a, $b: $a) {\n @return $b;\n}\n\na {\n color: foo(2);\n}\n", "a {\n color: 2;\n}\n" ); +// todo: this should have a space after : +// and should be "expected \")\"." +error!( + nothing_after_open, + "a { color:rgb(; }", + "Error: expected \"{\"." +); \ No newline at end of file diff --git a/tests/if.rs b/tests/if.rs index 9aa3ecc..ca058b1 100644 --- a/tests/if.rs +++ b/tests/if.rs @@ -128,3 +128,4 @@ error!( ); error!(unclosed_dbl_quote, "@if true \" {}", "Error: Expected \"."); error!(unclosed_sgl_quote, "@if true ' {}", "Error: Expected '."); +error!(unclosed_call_args, "@if a({}", "Error: expected \")\".");