Support @while

This commit is contained in:
Natalie Weizenbaum 2016-09-07 09:34:55 -07:00 committed by Natalie Weizenbaum
parent 80f0afb484
commit e873242ac4
5 changed files with 52 additions and 0 deletions

View File

@ -44,6 +44,7 @@ export 'sass/statement/stylesheet.dart';
export 'sass/statement/supports_rule.dart';
export 'sass/statement/variable_declaration.dart';
export 'sass/statement/warn_rule.dart';
export 'sass/statement/while_rule.dart';
export 'sass/supports_condition.dart';
export 'sass/supports_condition/declaration.dart';
export 'sass/supports_condition/interpolation.dart';

View File

@ -0,0 +1,25 @@
// Copyright 2016 Google Inc. Use of this source code is governed by an
// MIT-style license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.
import 'package:source_span/source_span.dart';
import '../../../visitor/interface/statement.dart';
import '../expression.dart';
import '../statement.dart';
class WhileRule implements Statement {
final Expression condition;
final List<Statement> children;
final FileSpan span;
WhileRule(this.condition, Iterable<Statement> children, this.span)
: children = new List.unmodifiable(children);
/*=T*/ accept/*<T>*/(StatementVisitor/*<T>*/ visitor) =>
visitor.visitWhileRule(this);
String toString() => "@while $condition {${children.join(" ")}}";
}

View File

@ -351,6 +351,8 @@ class Parser {
return _supportsRule(start);
case "warn":
return _warnRule(start);
case "while":
return _whileRule(start, child);
default:
return _unknownAtRule(start, name);
}
@ -375,6 +377,8 @@ class Parser {
return _includeRule(start);
case "warn":
return _warnRule(start);
case "while":
return _whileRule(start, _declarationChild);
default:
return _disallowedAtRule(start);
}
@ -395,6 +399,8 @@ class Parser {
return _returnRule(start);
case "warn":
return _warnRule(start);
case "while":
return _whileRule(start, _functionAtRule);
default:
return _disallowedAtRule(start);
}
@ -580,6 +586,15 @@ class Parser {
WarnRule _warnRule(LineScannerState start) =>
new WarnRule(_expression(), _scanner.spanFrom(start));
WhileRule _whileRule(LineScannerState start, Statement child()) {
var wasInControlDirective = _inControlDirective;
_inControlDirective = true;
var expression = _expression();
var children = _children(child);
_inControlDirective = wasInControlDirective;
return new WhileRule(expression, children, _scanner.spanFrom(start));
}
AtRule _unknownAtRule(LineScannerState start, String name) {
Interpolation value;
var next = _scanner.peekChar();

View File

@ -27,4 +27,5 @@ abstract class StatementVisitor<T> {
T visitSupportsRule(SupportsRule node);
T visitVariableDeclaration(VariableDeclaration node);
T visitWarnRule(WarnRule node);
T visitWhileRule(WhileRule node);
}

View File

@ -532,6 +532,16 @@ class PerformVisitor implements StatementVisitor, ExpressionVisitor<Value> {
}
}
void visitWhileRule(WhileRule node) {
_environment.scope(() {
while (node.condition.accept(this).isTruthy) {
for (var child in node.children) {
child.accept(this);
}
}
}, semiGlobal: true);
}
// ## Expressions
Value visitVariableExpression(VariableExpression node) {