Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(815)

Unified Diff: pkg/status_file/lib/src/expression.dart

Issue 2988383002: A minimal status file formatter and canonicalizer. (Closed)
Patch Set: Created 3 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: pkg/status_file/lib/src/expression.dart
diff --git a/pkg/status_file/lib/src/expression.dart b/pkg/status_file/lib/src/expression.dart
index 876fb150b67497d60e00e6d8449a7c6da129b758..90559de9ceb64a8ff9bbae0ddf53528f4c03b151 100644
--- a/pkg/status_file/lib/src/expression.dart
+++ b/pkg/status_file/lib/src/expression.dart
@@ -91,7 +91,7 @@ class _ComparisonExpression implements Expression {
return negate != (left.lookup(environment) == right);
}
- String toString() => "(\$${left.name} ${negate ? '!=' : '=='} $right)";
+ String toString() => "\$${left.name} ${negate ? '!=' : '=='} $right";
}
/// A reference to a variable defined in the environment. The expression
@@ -125,7 +125,7 @@ class _VariableExpression implements Expression {
bool evaluate(Environment environment) =>
negate != (variable.lookup(environment) == "true");
- String toString() => "(bool ${negate ? "! " : ""}\$${variable.name})";
+ String toString() => "${negate ? "!" : ""}\$${variable.name}";
}
/// A logical `||` or `&&` expression.
@@ -147,7 +147,18 @@ class _LogicExpression implements Expression {
? left.evaluate(environment) && right.evaluate(environment)
: left.evaluate(environment) || right.evaluate(environment);
- String toString() => "($left $op $right)";
+ String toString() {
+ String parenthesize(Expression operand) {
+ var result = operand.toString();
+ if (op == "&&" && operand is _LogicExpression && operand.op == "||") {
+ result = "($result)";
+ }
+
+ return result;
+ }
+
+ return "${parenthesize(left)} $op ${parenthesize(right)}";
+ }
}
/// Parser for Boolean expressions in a .status file for Dart.

Powered by Google App Engine
This is Rietveld 408576698