From efc5f91348a7de21dd3f115fcaabcc7b5e4a102e Mon Sep 17 00:00:00 2001 From: Connor Skees Date: Fri, 3 Jul 2020 23:49:31 -0400 Subject: [PATCH] more robustly parse empty bracketed lists --- CHANGELOG.md | 4 ++++ src/parse/value/parse.rs | 23 +++++++++++------------ tests/list.rs | 10 ++++++++++ 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f55355c..8209b50 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# TBD + + - more robustly parse empty bracketed lists + # 0.9.2 - implement builtin functions `min` and `max` diff --git a/src/parse/value/parse.rs b/src/parse/value/parse.rs index bb3c012..c8a8433 100644 --- a/src/parse/value/parse.rs +++ b/src/parse/value/parse.rs @@ -112,17 +112,6 @@ impl<'a> Parser<'a> { } IntermediateValue::Bracketed(t) => { last_was_whitespace = false; - if t.is_empty() { - space_separated.push( - HigherIntermediateValue::Literal(Value::List( - Vec::new(), - ListSeparator::Space, - Brackets::Bracketed, - )) - .span(val.span), - ); - continue; - } space_separated.push( HigherIntermediateValue::Literal( match iter.parser.parse_value_from_vec(t)?.node { @@ -509,6 +498,7 @@ impl<'a> Parser<'a> { } '[' => { let mut span = self.toks.next().unwrap().pos(); + self.whitespace_or_comment(); let mut inner = match read_until_closing_square_brace(self.toks) { Ok(v) => v, Err(e) => return Some(Err(e)), @@ -519,7 +509,16 @@ impl<'a> Parser<'a> { } span = span.merge(last_tok.pos()); } - IntermediateValue::Bracketed(inner).span(span) + if inner.is_empty() { + IntermediateValue::Value(HigherIntermediateValue::Literal(Value::List( + Vec::new(), + ListSeparator::Space, + Brackets::Bracketed, + ))) + } else { + IntermediateValue::Bracketed(inner) + } + .span(span) } '$' => { self.toks.next(); diff --git a/tests/list.rs b/tests/list.rs index a396edd..5b0b1dd 100644 --- a/tests/list.rs +++ b/tests/list.rs @@ -314,6 +314,16 @@ test!( "a {\n color: 3;\n}\n" ); test!(empty_bracketed_list, "a {\n empty: [];\n}\n"); +test!( + empty_bracketed_list_equality, + "a {\n color: []==[];\n}\n", + "a {\n color: true;\n}\n" +); +test!( + empty_bracketed_list_whitespace, + "a {\n color: [ /**/ ];\n}\n", + "a {\n color: [];\n}\n" +); error!( invalid_item_in_space_separated_list, "a {\n color: red color * #abc;\n}\n", "Error: Undefined operation \"color * #abc\"."