|
| 1 | +// Copyright 2025 Google Inc. Use of this source code is governed by an |
| 2 | +// MIT-style license that can be found in the LICENSE file or at |
| 3 | +// https://opensource.org/licenses/MIT. |
| 4 | + |
| 5 | +import 'package:charcode/charcode.dart'; |
| 6 | +import 'package:meta/meta.dart'; |
| 7 | +import 'package:source_span/source_span.dart'; |
| 8 | + |
| 9 | +import '../../../ast/node.dart'; |
| 10 | +import '../../../ast/sass.dart'; |
| 11 | +import '../../../interpolation_buffer.dart'; |
| 12 | +import '../../../util/lazy_file_span.dart'; |
| 13 | +import '../../../visitor/interface/expression.dart'; |
| 14 | +import '../../../visitor/interface/if_condition_expression.dart'; |
| 15 | + |
| 16 | +/// A CSS `if()` expression. |
| 17 | +/// |
| 18 | +/// In addition to supporting the plain-CSS syntax, this supports a `sass()` |
| 19 | +/// condition that evaluates SassScript expressions. |
| 20 | +/// |
| 21 | +/// {@category AST} |
| 22 | +final class IfExpression extends Expression { |
| 23 | + /// The conditional branches that make up the `if()`. |
| 24 | + /// |
| 25 | + /// A `null` expression indicates an `else` branch that is always evaluated. |
| 26 | + final List<(IfConditionExpression?, Expression)> branches; |
| 27 | + |
| 28 | + final FileSpan span; |
| 29 | + |
| 30 | + IfExpression( |
| 31 | + Iterable<(IfConditionExpression?, Expression)> branches, this.span) |
| 32 | + : branches = List.unmodifiable(branches) { |
| 33 | + if (this.branches.isEmpty) { |
| 34 | + throw ArgumentError.value(this.branches, "branches", "may not be empty"); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + T accept<T>(ExpressionVisitor<T> visitor) => visitor.visitIfExpression(this); |
| 39 | + |
| 40 | + String toString() { |
| 41 | + var buffer = StringBuffer("if("); |
| 42 | + var first = true; |
| 43 | + for (var (condition, expression) in branches) { |
| 44 | + if (first) { |
| 45 | + first = false; |
| 46 | + } else { |
| 47 | + buffer.write("; "); |
| 48 | + } |
| 49 | + |
| 50 | + buffer.write(condition ?? "else"); |
| 51 | + buffer.write(": "); |
| 52 | + buffer.write(expression); |
| 53 | + } |
| 54 | + buffer.writeCharCode($rparen); |
| 55 | + return buffer.toString(); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +/// The parent class of conditions in an [IfExpression]. |
| 60 | +/// |
| 61 | +/// {@category AST} |
| 62 | +sealed class IfConditionExpression implements SassNode { |
| 63 | + /// Returns whether this is an arbitrary substitution expression which may be |
| 64 | + /// replaced with multiple tokens at evaluation or render time. |
| 65 | + /// |
| 66 | + /// @nodoc |
| 67 | + @internal |
| 68 | + bool get isArbitrarySubstitution => false; |
| 69 | + |
| 70 | + /// Converts this expression into an interpolation that produces the same |
| 71 | + /// value. |
| 72 | + /// |
| 73 | + /// Throws a [SourceSpanFormatException] if this contains an |
| 74 | + /// [IfConditionSass]. [arbitrarySubstitution]'s span is used for this error. |
| 75 | + /// |
| 76 | + /// @nodoc |
| 77 | + @internal |
| 78 | + Interpolation toInterpolation(AstNode arbitrarySubstitution); |
| 79 | + |
| 80 | + /// Calls the appropriate visit method on [visitor]. |
| 81 | + T accept<T>(IfConditionExpressionVisitor<T> visitor); |
| 82 | +} |
| 83 | + |
| 84 | +/// A parenthesized condition. |
| 85 | +/// |
| 86 | +/// {@category AST} |
| 87 | +final class IfConditionParenthesized extends IfConditionExpression { |
| 88 | + /// The parenthesized expression. |
| 89 | + final IfConditionExpression expression; |
| 90 | + |
| 91 | + final FileSpan span; |
| 92 | + |
| 93 | + IfConditionParenthesized(this.expression, this.span); |
| 94 | + |
| 95 | + /// @nodoc |
| 96 | + @internal |
| 97 | + Interpolation toInterpolation(AstNode arbitrarySubstitution) => |
| 98 | + (InterpolationBuffer() |
| 99 | + ..writeCharCode($lparen) |
| 100 | + ..addInterpolation( |
| 101 | + expression.toInterpolation(arbitrarySubstitution)) |
| 102 | + ..writeCharCode($rparen)) |
| 103 | + .interpolation(span); |
| 104 | + |
| 105 | + T accept<T>(IfConditionExpressionVisitor<T> visitor) => |
| 106 | + visitor.visitIfConditionParenthesized(this); |
| 107 | + |
| 108 | + String toString() => "($expression)"; |
| 109 | +} |
| 110 | + |
| 111 | +/// A negated condition. |
| 112 | +/// |
| 113 | +/// {@category AST} |
| 114 | +final class IfConditionNegation extends IfConditionExpression { |
| 115 | + /// The expression negated by this. |
| 116 | + final IfConditionExpression expression; |
| 117 | + |
| 118 | + final FileSpan span; |
| 119 | + |
| 120 | + IfConditionNegation(this.expression, this.span); |
| 121 | + |
| 122 | + /// @nodoc |
| 123 | + @internal |
| 124 | + Interpolation toInterpolation(AstNode arbitrarySubstitution) => |
| 125 | + (InterpolationBuffer() |
| 126 | + ..write('not ') |
| 127 | + ..addInterpolation( |
| 128 | + expression.toInterpolation(arbitrarySubstitution))) |
| 129 | + .interpolation(span); |
| 130 | + |
| 131 | + T accept<T>(IfConditionExpressionVisitor<T> visitor) => |
| 132 | + visitor.visitIfConditionNegation(this); |
| 133 | + |
| 134 | + String toString() => "not $expression"; |
| 135 | +} |
| 136 | + |
| 137 | +/// A sequence of `and`s or `or`s. |
| 138 | +/// |
| 139 | +/// {@category AST} |
| 140 | +final class IfConditionOperation extends IfConditionExpression { |
| 141 | + /// The expressions conjoined or disjoined by this operation. |
| 142 | + final List<IfConditionExpression> expressions; |
| 143 | + |
| 144 | + final BooleanOperator op; |
| 145 | + |
| 146 | + FileSpan get span => expressions.first.span.expand(expressions.last.span); |
| 147 | + |
| 148 | + IfConditionOperation(Iterable<IfConditionExpression> expressions, this.op) |
| 149 | + : expressions = List.unmodifiable(expressions) { |
| 150 | + if (this.expressions.length < 2) { |
| 151 | + throw ArgumentError.value( |
| 152 | + this.expressions, "expressions", "must have length >= 2"); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + /// @nodoc |
| 157 | + @internal |
| 158 | + Interpolation toInterpolation(AstNode arbitrarySubstitution) { |
| 159 | + var buffer = InterpolationBuffer(); |
| 160 | + var first = true; |
| 161 | + for (var expression in expressions) { |
| 162 | + if (first) { |
| 163 | + first = false; |
| 164 | + } else { |
| 165 | + buffer.write(' $op '); |
| 166 | + } |
| 167 | + buffer |
| 168 | + .addInterpolation(expression.toInterpolation(arbitrarySubstitution)); |
| 169 | + } |
| 170 | + return buffer.interpolation(LazyFileSpan(() => span)); |
| 171 | + } |
| 172 | + |
| 173 | + T accept<T>(IfConditionExpressionVisitor<T> visitor) => |
| 174 | + visitor.visitIfConditionOperation(this); |
| 175 | + |
| 176 | + String toString() => expressions.join(" $op "); |
| 177 | +} |
| 178 | + |
| 179 | +/// A plain-CSS function-style condition. |
| 180 | +/// |
| 181 | +/// {@category AST} |
| 182 | +final class IfConditionFunction extends IfConditionExpression { |
| 183 | + /// The name of the function being called. |
| 184 | + final Interpolation name; |
| 185 | + |
| 186 | + /// The arguments passed to the function call. |
| 187 | + final Interpolation arguments; |
| 188 | + |
| 189 | + final FileSpan span; |
| 190 | + |
| 191 | + /// @nodoc |
| 192 | + @internal |
| 193 | + bool get isArbitrarySubstitution => switch (name.asPlain?.toLowerCase()) { |
| 194 | + "if" || "var" || "attr" => true, |
| 195 | + var str? when str.startsWith("--") => true, |
| 196 | + _ => false, |
| 197 | + }; |
| 198 | + |
| 199 | + IfConditionFunction(this.name, this.arguments, this.span); |
| 200 | + |
| 201 | + /// @nodoc |
| 202 | + @internal |
| 203 | + Interpolation toInterpolation(AstNode _) => (InterpolationBuffer() |
| 204 | + ..addInterpolation(name) |
| 205 | + ..writeCharCode($lparen) |
| 206 | + ..addInterpolation(arguments) |
| 207 | + ..writeCharCode($rparen)) |
| 208 | + .interpolation(span); |
| 209 | + |
| 210 | + T accept<T>(IfConditionExpressionVisitor<T> visitor) => |
| 211 | + visitor.visitIfConditionFunction(this); |
| 212 | + |
| 213 | + String toString() => "$name($arguments)"; |
| 214 | +} |
| 215 | + |
| 216 | +/// A Sass condition that will evaluate to true or false at compile time. |
| 217 | +/// |
| 218 | +/// {@category AST} |
| 219 | +final class IfConditionSass extends IfConditionExpression { |
| 220 | + /// The expression that determines whether this condition matches. |
| 221 | + final Expression expression; |
| 222 | + |
| 223 | + final FileSpan span; |
| 224 | + |
| 225 | + IfConditionSass(this.expression, this.span); |
| 226 | + |
| 227 | + /// @nodoc |
| 228 | + @internal |
| 229 | + Interpolation toInterpolation(AstNode arbitrarySubstitution) => |
| 230 | + throw MultiSourceSpanFormatException( |
| 231 | + 'if() conditions with arbitrary substitutions may not contain sass() ' |
| 232 | + 'expressions.', |
| 233 | + arbitrarySubstitution.span, |
| 234 | + "arbitrary substitution", |
| 235 | + {span: "sass() expression"}); |
| 236 | + |
| 237 | + T accept<T>(IfConditionExpressionVisitor<T> visitor) => |
| 238 | + visitor.visitIfConditionSass(this); |
| 239 | + |
| 240 | + String toString() => "sass($expression)"; |
| 241 | +} |
| 242 | + |
| 243 | +/// A chunk of raw text, possibly with interpolations. |
| 244 | +/// |
| 245 | +/// This is used to represent explicit interpolation, as well as whole |
| 246 | +/// expressions where arbitrary substitutions are used in place of operators. |
| 247 | +/// |
| 248 | +/// {@category AST} |
| 249 | +final class IfConditionRaw extends IfConditionExpression { |
| 250 | + /// The text that encompasses this condition. |
| 251 | + final Interpolation text; |
| 252 | + |
| 253 | + FileSpan get span => text.span; |
| 254 | + |
| 255 | + /// @nodoc |
| 256 | + @internal |
| 257 | + bool get isArbitrarySubstitution => true; |
| 258 | + |
| 259 | + IfConditionRaw(this.text); |
| 260 | + |
| 261 | + /// @nodoc |
| 262 | + @internal |
| 263 | + Interpolation toInterpolation(AstNode _) => text; |
| 264 | + |
| 265 | + T accept<T>(IfConditionExpressionVisitor<T> visitor) => |
| 266 | + visitor.visitIfConditionRaw(this); |
| 267 | + |
| 268 | + String toString() => text.toString(); |
| 269 | +} |
0 commit comments