Skip to content

Commit 098799f

Browse files
committed
Fix-up comment parsing
1 parent 4f0be36 commit 098799f

10 files changed

+31
-17
lines changed

src/interpolation.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,13 @@ namespace Sass {
7878
}
7979
}
8080

81-
if (auto str = contents.back()->isaItplString()) {
82-
text.write(
83-
str->text(),
84-
str->pstate());
85-
contents.pop_back();
81+
if (!contents.empty()) {
82+
if (auto str = contents.back()->isaItplString()) {
83+
text.write(
84+
str->text(),
85+
str->pstate());
86+
contents.pop_back();
87+
}
8688
}
8789

8890
}

src/parser.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ namespace Sass {
5959
if (scanner.peekChar() != $slash) return false;
6060
auto next = scanner.peekChar(1);
6161
if (next == $slash) {
62-
lastSilentComment = readSilentComment();
62+
//lastSilentComment = read
63+
scanSilentComment();
6364
return true;
6465
}
6566
else if (next == $asterisk) {
@@ -72,13 +73,12 @@ namespace Sass {
7273
}
7374

7475
// Consumes and ignores a silent (Sass-style) comment.
75-
SilentComment* Parser::readSilentComment()
76+
void Parser::scanSilentComment()
7677
{
7778
scanner.expect("//");
7879
while (!scanner.isDone() && !isNewline(scanner.peekChar())) {
7980
scanner.readChar();
8081
}
81-
return nullptr;
8282
}
8383

8484
// Consumes and ignores a loud (CSS-style) comment.

src/parser.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,11 @@ namespace Sass {
8787
// Returns whether the comment was consumed.
8888
bool scanComment();
8989

90-
// Consumes and ignores a silent (Sass-style) comment.
91-
virtual SilentComment* readSilentComment();
92-
9390
// Consumes and ignores a loud (CSS-style) comment.
9491
virtual void scanLoudComment();
9592

93+
virtual void scanSilentComment();
94+
9695
// Consumes a plain CSS identifier. If [unit] is `true`, this
9796
// doesn't parse a `-` followed by a digit. This ensures that
9897
// `1px-2px` parses as subtraction rather than the unit `px-2px`.

src/parser_css.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ namespace Sass {
5353
}
5454
// EO readSilentComment
5555

56+
// Consume a silent comment and throws error
57+
void CssParser::scanSilentComment()
58+
{
59+
Offset start(scanner.offset);
60+
lastSilentComment = ScssParser::readSilentComment();
61+
error("Silent comments aren't allowed in plain CSS.",
62+
scanner.relevantSpanFrom(start));
63+
}
64+
// EO readSilentComment
65+
5666
// Helper to declare all forbidden at-rules
5767
bool isForbiddenCssAtRule(const sass::string& name)
5868
{

src/parser_css.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ namespace Sass {
4141
// Consume a silent comment and throws error
4242
SilentComment* readSilentComment() override final;
4343

44+
// Consume a silent comment and throws error
45+
void scanSilentComment() override final;
46+
4447
// Parse allowed at-rule statement and parse children via [child_parser] parser function
4548
Statement* readAtRule(Statement* (StylesheetParser::* child_parser)(), bool root = false) override final;
4649

src/parser_sass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace Sass {
1616
Offset start(scanner.offset);
1717
InterpolationBuffer buffer(scanner);
1818
do {
19-
buffer.addInterpolation(readAlmostAnyValue());
19+
buffer.addInterpolation(readAlmostAnyValue(true));
2020
buffer.writeCharCode($lf);
2121
} while (buffer.trailingStringEndsWith(",") &&
2222
scanCharIf(isNewline));

src/parser_sass.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ namespace Sass {
109109
Statement* parseChild(Statement* (StylesheetParser::* statement)());
110110

111111
// Consumes an indented-style silent comment.
112-
SilentComment* readSilentComment() override final;
112+
SilentComment* readSilentComment();
113113

114114
// Consumes an indented-style loud context.
115115
// This overrides loud comment consumption so

src/parser_scss.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ namespace Sass {
7878
Statement* (StylesheetParser::* statement)()) override;
7979

8080
// Consumes a statement-level silent comment block.
81-
virtual SilentComment* readSilentComment() override;
81+
virtual SilentComment* readSilentComment();
8282

8383
// Consumes a statement-level loud comment block.
8484
virtual LoudComment* readLoudComment();

src/parser_stylesheet.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4576,7 +4576,7 @@ namespace Sass {
45764576
// the text is expected to be re-parsed.
45774577
// * This supports Sass-style single-line comments.
45784578
// * This does not compress adjacent whitespace characters.
4579-
Interpolation* StylesheetParser::readAlmostAnyValue()
4579+
Interpolation* StylesheetParser::readAlmostAnyValue(bool omitComments)
45804580
{
45814581
// const char* start = scanner.position;
45824582
InterpolationBuffer buffer(scanner);
@@ -4606,7 +4606,7 @@ namespace Sass {
46064606
case $slash:
46074607
commentStart = scanner.position;
46084608
if (scanComment()) {
4609-
buffer.write(scanner.substring(commentStart));
4609+
if (!omitComments) buffer.write(scanner.substring(commentStart));
46104610
}
46114611
else {
46124612
buffer.write(scanner.readChar());

src/parser_stylesheet.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ namespace Sass {
460460
// the text is expected to be re-parsed.
461461
// * This supports Sass-style single-line comments.
462462
// * This does not compress adjacent whitespace characters.
463-
Interpolation* readAlmostAnyValue();
463+
Interpolation* readAlmostAnyValue(bool omitComments = false);
464464

465465
// Consumes tokens until it reaches a top-level `";"`, `")"`, `"]"`, or `"}"` and
466466
// returns their contents as a string. If [allowEmpty] is `false` (the default), this

0 commit comments

Comments
 (0)