diff --git a/src/function.rs b/src/function.rs index d6a3a53..a72fa99 100644 --- a/src/function.rs +++ b/src/function.rs @@ -133,13 +133,44 @@ pub fn eat_call_args>(toks: &mut Peekable) -> CallA match kind { TokenKind::Variable(v) => name = Some(v), TokenKind::Symbol(Symbol::Colon) => { - todo!("handle default values") - // let mut val: Vec = Vec::new(); - // while let Some(Token { kind, .. }) = toks.next() { - // match &kind { - // _ => {} - // } - // } + devour_whitespace(toks); + while let Some(tok) = toks.peek() { + match &tok.kind { + TokenKind::Symbol(Symbol::Comma) => { + toks.next(); + args.insert( + name.clone().unwrap(), + CallArg { + name: name.clone(), + val: val.clone(), + }, + ); + if let Some(ref mut s) = name { + s.clear(); + } + val.clear(); + break; + } + TokenKind::Symbol(Symbol::CloseParen) => { + args.insert( + name.clone().unwrap(), + CallArg { + name: name.clone(), + val: val.clone(), + }, + ); + if let Some(ref mut s) = name { + s.clear(); + } + val.clear(); + break; + } + _ => { + let tok = toks.next().expect("we know this exists!"); + val.push(tok) + } + } + } } TokenKind::Symbol(Symbol::CloseParen) => { if name.is_some() { @@ -150,11 +181,11 @@ pub fn eat_call_args>(toks: &mut Peekable) -> CallA break; } TokenKind::Symbol(Symbol::Comma) => { - if name.is_some() { + if let Some(ref name) = name { args.insert( - name.clone().unwrap(), + name.clone(), CallArg { - name: name.clone(), + name: Some(name.clone()), val: val.clone(), }, ); diff --git a/src/main.rs b/src/main.rs index b4591cc..65f467c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1154,6 +1154,31 @@ mod test_mixins { "@mixin a($a: red) {\n color: $a;\n}\nd {\n @include a(blue);\n}\n", "d {\n color: blue;\n}\n" ); + test!( + mixin_keyword_arg, + "@mixin a($a) {\n color: $a;\n}\nd {\n @include a($a: blue);\n}\n", + "d {\n color: blue;\n}\n" + ); + test!( + mixin_keyword_arg_override_default, + "@mixin a($a: red) {\n color: $a;\n}\nd {\n @include a($a: blue);\n}\n", + "d {\n color: blue;\n}\n" + ); + test!( + mixin_keyword_applies_to_second_arg, + "@mixin a($a: red, $b) {\n color: $a $b;\n}\nd {\n @include a($b: blue);\n}\n", + "d {\n color: red blue;\n}\n" + ); + test!( + mixin_two_keywords, + "@mixin a($a, $b) {\n color: $a $b;\n}\nd {\n @include a($a: red, $b: blue);\n}\n", + "d {\n color: red blue;\n}\n" + ); + test!( + mixin_two_keywords_wrong_direction, + "@mixin a($a, $b) {\n color: $a $b;\n}\nd {\n @include a($b: blue, $a: red);\n}\n", + "d {\n color: red blue;\n}\n" + ); } #[cfg(test)]