Skip to content

Commit 0d5277c

Browse files
delapuentexzyfer
authored andcommitted
Change the parser to recognize bracketed lists.
This is the idea: in the same way the old parser looks for parenthesis delimiters, let's add another case for brackets delimiters. In case an open bracket is found, the parser will look for a list. There are three possible results of parsing a list: First, the parsed expression is not a list at all. libsass parser tends to avoid wrapping if lists are only one item lists. But bracketed lists prevent unwrapping so the item becomes the solely item for a new bracketed list. Example: singleton-item --> [ singleton-item ] Second, the list is indeed a list with some explicit delimiter. This case is similar to the previous one. Brackets prevent unwrapping so this time is the list as a whole which become the unique item of the bracketed list. Example: (item1, item2, ...) --> [ (item1, item2, ...) ] [item1, item2, ...] --> [ [item1, item2, ...] ] Third, the list is a list with no delimiters. So its items actually belong to the bracketed list. The parse will fix the delimiter of the list for them to be brackets. Example: Suppose | ... | stands for a list with no delimiters. |item1, item2, ...| --> [item1, item2, ...]
1 parent c2e1e70 commit 0d5277c

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

src/parser.cpp

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -966,7 +966,7 @@ namespace Sass {
966966
else {
967967
value = &parse_list(DELAYED);
968968
if (List_Ptr list = SASS_MEMORY_CAST(List, value)) {
969-
if (list->length() == 0 && !peek< exactly <'{'> >()) {
969+
if (!list->is_bracketed() && list->length() == 0 && !peek< exactly <'{'> >()) {
970970
css_error("Invalid CSS", " after ", ": expected expression (e.g. 1px, bold), was ");
971971
}
972972
}
@@ -1000,8 +1000,13 @@ namespace Sass {
10001000
List_Obj map = SASS_MEMORY_NEW(List, pstate, 0, SASS_HASH);
10011001

10021002
// it's not a map so return the lexed value as a list value
1003-
if (!lex_css< exactly<':'> >())
1004-
{ return key; }
1003+
if (!lex_css< exactly<':'> >()) {
1004+
List_Obj list = SASS_MEMORY_CAST(List, key);
1005+
if (list && list->delimiter() == SASS_NO_DELIMITER) {
1006+
list->delimiter(SASS_PARENTHESIS);
1007+
}
1008+
return key;
1009+
}
10051010

10061011
Expression_Obj value = parse_space_list();
10071012

@@ -1051,6 +1056,7 @@ namespace Sass {
10511056
exactly<'}'>,
10521057
exactly<'{'>,
10531058
exactly<')'>,
1059+
exactly<']'>,
10541060
exactly<':'>,
10551061
end_of_file,
10561062
exactly<ellipsis>,
@@ -1085,6 +1091,7 @@ namespace Sass {
10851091
exactly<'}'>,
10861092
exactly<'{'>,
10871093
exactly<')'>,
1094+
exactly<']'>,
10881095
exactly<':'>,
10891096
end_of_file,
10901097
exactly<ellipsis>,
@@ -1111,6 +1118,7 @@ namespace Sass {
11111118
exactly<'}'>,
11121119
exactly<'{'>,
11131120
exactly<')'>,
1121+
exactly<']'>,
11141122
exactly<','>,
11151123
exactly<':'>,
11161124
end_of_file,
@@ -1129,6 +1137,7 @@ namespace Sass {
11291137
exactly<'}'>,
11301138
exactly<'{'>,
11311139
exactly<')'>,
1140+
exactly<']'>,
11321141
exactly<','>,
11331142
exactly<':'>,
11341143
end_of_file,
@@ -1328,6 +1337,21 @@ namespace Sass {
13281337
// expression can be evaluated
13291338
return &value;
13301339
}
1340+
else if (lex_css< exactly<'['> >()) {
1341+
// explicit bracketed
1342+
Expression_Obj value = parse_list();
1343+
// lex the expected closing square bracket
1344+
if (!lex_css< exactly<']'> >()) error("unclosed squared bracket", pstate);
1345+
// fix delimiter
1346+
List_Obj list = SASS_MEMORY_CAST(List, value);
1347+
if (!list || list->delimiter() != SASS_NO_DELIMITER) {
1348+
List_Ptr outer_list = SASS_MEMORY_NEW(List, pstate, 1, SASS_SPACE, false, SASS_BRACKETS);
1349+
outer_list->append(&value);
1350+
return outer_list;
1351+
}
1352+
list->delimiter(SASS_BRACKETS);
1353+
return value;
1354+
}
13311355
// string may be interpolated
13321356
// if (lex< quoted_string >()) {
13331357
// return &parse_string();

0 commit comments

Comments
 (0)