Skip to content

Commit

Permalink
Use local whitespace functions where value is irrelevant
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesnw committed Dec 30, 2024
1 parent 38e05bf commit ba62f15
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 47 deletions.
13 changes: 9 additions & 4 deletions lib/src/parse/at_root_query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,27 @@ class AtRootQueryParser extends Parser {
AtRootQuery parse() {
return wrapSpanFormatException(() {
scanner.expectChar($lparen);
whitespace(allowNewlines: true);
_whitespace();
var include = scanIdentifier("with");
if (!include) expectIdentifier("without", name: '"with" or "without"');
whitespace(allowNewlines: true);
_whitespace();
scanner.expectChar($colon);
whitespace(allowNewlines: true);
_whitespace();

var atRules = <String>{};
do {
atRules.add(identifier().toLowerCase());
whitespace(allowNewlines: true);
_whitespace();
} while (lookingAtIdentifier());
scanner.expectChar($rparen);
scanner.expectDone();

return AtRootQuery(atRules, include: include);
});
}

/// The value of `allowNewlines` is not relevant for this class.
void _whitespace() {
whitespace(allowNewlines: true);
}
}
15 changes: 10 additions & 5 deletions lib/src/parse/css.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class CssParser extends ScssParser {
var start = scanner.state;
scanner.expectChar($at);
var name = interpolatedIdentifier();
whitespace(allowNewlines: true);
_whitespace();

return switch (name.asPlain) {
"at-root" ||
Expand Down Expand Up @@ -113,7 +113,7 @@ class CssParser extends ScssParser {
.text
};

whitespace(allowNewlines: true);
_whitespace();
var modifiers = tryImportModifiers();
expectStatementSeparator("@import rule");
return ImportRule(
Expand All @@ -126,7 +126,7 @@ class CssParser extends ScssParser {
// evaluation time.
var start = scanner.state;
scanner.expectChar($lparen);
whitespace(allowNewlines: true);
_whitespace();
var expression = expressionUntilComma();
scanner.expectChar($rparen);
return ParenthesizedExpression(expression, scanner.spanFrom(start));
Expand All @@ -151,7 +151,7 @@ class CssParser extends ScssParser {
var arguments = <Expression>[];
if (!scanner.scanChar($rparen)) {
do {
whitespace(allowNewlines: true);
_whitespace();
if (allowEmptySecondArg &&
arguments.length == 1 &&
scanner.peekChar() == $rparen) {
Expand All @@ -160,7 +160,7 @@ class CssParser extends ScssParser {
}

arguments.add(expressionUntilComma(singleEquals: true));
whitespace(allowNewlines: true);
_whitespace();
} while (scanner.scanChar($comma));
scanner.expectChar($rparen);
}
Expand All @@ -180,4 +180,9 @@ class CssParser extends ScssParser {
var expression = super.namespacedExpression(namespace, start);
error("Module namespaces aren't allowed in plain CSS.", expression.span);
}

/// The value of `allowNewlines` is not relevant for this class.
void _whitespace() {
whitespace(allowNewlines: true);
}
}
9 changes: 7 additions & 2 deletions lib/src/parse/keyframe_selector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class KeyframeSelectorParser extends Parser {
return wrapSpanFormatException(() {
var selectors = <String>[];
do {
whitespace(allowNewlines: false);
_whitespace();
if (lookingAtIdentifier()) {
if (scanIdentifier("from")) {
selectors.add("from");
Expand All @@ -26,7 +26,7 @@ class KeyframeSelectorParser extends Parser {
} else {
selectors.add(_percentage());
}
whitespace(allowNewlines: false);
_whitespace();
} while (scanner.scanChar($comma));
scanner.expectDone();

Expand Down Expand Up @@ -71,4 +71,9 @@ class KeyframeSelectorParser extends Parser {
buffer.writeCharCode($percent);
return buffer.toString();
}

/// The value of `allowNewlines` is not relevant for this class.
void _whitespace() {
whitespace(allowNewlines: true);
}
}
17 changes: 11 additions & 6 deletions lib/src/parse/media_query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ class MediaQueryParser extends Parser {
return wrapSpanFormatException(() {
var queries = <CssMediaQuery>[];
do {
whitespace(allowNewlines: true);
_whitespace();
queries.add(_mediaQuery());
whitespace(allowNewlines: true);
_whitespace();
} while (scanner.scanChar($comma));
scanner.expectDone();
return queries;
Expand All @@ -30,7 +30,7 @@ class MediaQueryParser extends Parser {
// This is somewhat duplicated in StylesheetParser._mediaQuery.
if (scanner.peekChar() == $lparen) {
var conditions = [_mediaInParens()];
whitespace(allowNewlines: true);
_whitespace();

var conjunction = true;
if (scanIdentifier("and")) {
Expand All @@ -57,7 +57,7 @@ class MediaQueryParser extends Parser {
}
}

whitespace(allowNewlines: true);
_whitespace();
if (!lookingAtIdentifier()) {
// For example, "@media screen {"
return CssMediaQuery.type(identifier1);
Expand All @@ -70,7 +70,7 @@ class MediaQueryParser extends Parser {
// For example, "@media screen and ..."
type = identifier1;
} else {
whitespace(allowNewlines: true);
_whitespace();
modifier = identifier1;
type = identifier2;
if (scanIdentifier("and")) {
Expand Down Expand Up @@ -102,7 +102,7 @@ class MediaQueryParser extends Parser {
var result = <String>[];
while (true) {
result.add(_mediaInParens());
whitespace(allowNewlines: true);
_whitespace();

if (!scanIdentifier(operator)) return result;
expectWhitespace();
Expand All @@ -117,4 +117,9 @@ class MediaQueryParser extends Parser {
scanner.expectChar($rparen);
return result;
}

/// The value of `allowNewlines` is not relevant for this class.
void _whitespace() {
whitespace(allowNewlines: true);
}
}
8 changes: 4 additions & 4 deletions lib/src/parse/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class Parser {
if (!scanner.scanChar($dollar)) return false;
if (!lookingAtIdentifier()) return false;
identifier();
whitespace(allowNewlines: false);
whitespace(allowNewlines: true);
return scanner.scanChar($colon);
}

Expand All @@ -76,7 +76,7 @@ class Parser {
/// Consumes whitespace, including any comments.
///
/// If [allowNewlines] is true, the indented syntax will consume newlines as
/// whitespace, in positions when a statement can not end.
/// whitespace in positions when a statement can't end.
@protected
void whitespace({required bool allowNewlines}) {
do {
Expand All @@ -87,7 +87,7 @@ class Parser {
/// Consumes whitespace, but not comments.
///
/// If [allowNewlines] is true, the indented syntax will consume newlines as
/// whitespace, in positions when a statement can not end.
/// whitespace in positions when a statement can't end.
@protected
void whitespaceWithoutComments({required bool allowNewlines}) {
while (!scanner.isDone && scanner.peekChar().isWhitespace) {
Expand Down Expand Up @@ -124,7 +124,7 @@ class Parser {
/// Like [whitespace], but throws an error if no whitespace is consumed.
///
/// If [allowNewlines] is true, the indented syntax will consume newlines as
/// whitespace, in positions when a statement can not end.
/// whitespace in positions when a statement can't end.
@protected
void expectWhitespace({bool allowNewlines = false}) {
if (scanner.isDone || !(scanner.peekChar().isWhitespace || scanComment())) {
Expand Down
30 changes: 20 additions & 10 deletions lib/src/parse/scss.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ScssParser extends StylesheetParser {
Interpolation styleRuleSelector() => almostAnyValue();

void expectStatementSeparator([String? name]) {
whitespaceWithoutComments(allowNewlines: true);
_whitespaceWithoutComments();
if (scanner.isDone) return;
if (scanner.peekChar() case $semicolon || $rbrace) return;
scanner.expectChar($semicolon);
Expand All @@ -38,7 +38,7 @@ class ScssParser extends StylesheetParser {

bool scanElse(int ifIndentation) {
var start = scanner.state;
whitespace(allowNewlines: true);
_whitespace();
var beforeAt = scanner.state;
if (scanner.scanChar($at)) {
if (scanIdentifier('else', caseSensitive: true)) return true;
Expand All @@ -62,7 +62,7 @@ class ScssParser extends StylesheetParser {

List<Statement> children(Statement child()) {
scanner.expectChar($lbrace);
whitespaceWithoutComments(allowNewlines: true);
_whitespaceWithoutComments();
var children = <Statement>[];
while (true) {
switch (scanner.peekChar()) {
Expand All @@ -73,17 +73,17 @@ class ScssParser extends StylesheetParser {
switch (scanner.peekChar(1)) {
case $slash:
children.add(_silentComment());
whitespaceWithoutComments(allowNewlines: true);
_whitespaceWithoutComments();
case $asterisk:
children.add(_loudComment());
whitespaceWithoutComments(allowNewlines: true);
_whitespaceWithoutComments();
default:
children.add(child());
}

case $semicolon:
scanner.readChar();
whitespaceWithoutComments(allowNewlines: true);
_whitespaceWithoutComments();

case $rbrace:
scanner.expectChar($rbrace);
Expand All @@ -97,7 +97,7 @@ class ScssParser extends StylesheetParser {

List<Statement> statements(Statement? statement()) {
var statements = <Statement>[];
whitespaceWithoutComments(allowNewlines: true);
_whitespaceWithoutComments();
while (!scanner.isDone) {
switch (scanner.peekChar()) {
case $dollar:
Expand All @@ -107,17 +107,17 @@ class ScssParser extends StylesheetParser {
switch (scanner.peekChar(1)) {
case $slash:
statements.add(_silentComment());
whitespaceWithoutComments(allowNewlines: true);
_whitespaceWithoutComments();
case $asterisk:
statements.add(_loudComment());
whitespaceWithoutComments(allowNewlines: true);
_whitespaceWithoutComments();
default:
if (statement() case var child?) statements.add(child);
}

case $semicolon:
scanner.readChar();
whitespaceWithoutComments(allowNewlines: true);
_whitespaceWithoutComments();

default:
if (statement() case var child?) statements.add(child);
Expand Down Expand Up @@ -183,4 +183,14 @@ class ScssParser extends StylesheetParser {
}
}
}

/// The value of `allowNewlines` is not relevant for this class.
void _whitespace() {
whitespace(allowNewlines: true);
}

/// The value of `allowNewlines` is not relevant for this class.
void _whitespaceWithoutComments() {
whitespaceWithoutComments(allowNewlines: true);
}
}
Loading

0 comments on commit ba62f15

Please sign in to comment.