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

Side by Side 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 import '../environment.dart'; 5 import '../environment.dart';
6 6
7 /// A parsed Boolean expression AST. 7 /// A parsed Boolean expression AST.
8 abstract class Expression { 8 abstract class Expression {
9 /// Parses Boolean expressions in a .status file for Dart. 9 /// Parses Boolean expressions in a .status file for Dart.
10 /// 10 ///
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
84 _ComparisonExpression(this.left, this.right, this.negate); 84 _ComparisonExpression(this.left, this.right, this.negate);
85 85
86 void validate(Environment environment, List<String> errors) { 86 void validate(Environment environment, List<String> errors) {
87 environment.validate(left.name, right, errors); 87 environment.validate(left.name, right, errors);
88 } 88 }
89 89
90 bool evaluate(Environment environment) { 90 bool evaluate(Environment environment) {
91 return negate != (left.lookup(environment) == right); 91 return negate != (left.lookup(environment) == right);
92 } 92 }
93 93
94 String toString() => "(\$${left.name} ${negate ? '!=' : '=='} $right)"; 94 String toString() => "\$${left.name} ${negate ? '!=' : '=='} $right";
95 } 95 }
96 96
97 /// A reference to a variable defined in the environment. The expression 97 /// A reference to a variable defined in the environment. The expression
98 /// evaluates to true if the variable's stringified value is "true". 98 /// evaluates to true if the variable's stringified value is "true".
99 /// ``` 99 /// ```
100 /// $variable 100 /// $variable
101 /// ``` 101 /// ```
102 /// is equivalent to 102 /// is equivalent to
103 /// ``` 103 /// ```
104 /// $variable == true 104 /// $variable == true
(...skipping 13 matching lines...) Expand all
118 _VariableExpression(this.variable, {this.negate = false}); 118 _VariableExpression(this.variable, {this.negate = false});
119 119
120 void validate(Environment environment, List<String> errors) { 120 void validate(Environment environment, List<String> errors) {
121 // It must be a Boolean, so it should allow either Boolean value. 121 // It must be a Boolean, so it should allow either Boolean value.
122 environment.validate(variable.name, "true", errors); 122 environment.validate(variable.name, "true", errors);
123 } 123 }
124 124
125 bool evaluate(Environment environment) => 125 bool evaluate(Environment environment) =>
126 negate != (variable.lookup(environment) == "true"); 126 negate != (variable.lookup(environment) == "true");
127 127
128 String toString() => "(bool ${negate ? "! " : ""}\$${variable.name})"; 128 String toString() => "${negate ? "!" : ""}\$${variable.name}";
129 } 129 }
130 130
131 /// A logical `||` or `&&` expression. 131 /// A logical `||` or `&&` expression.
132 class _LogicExpression implements Expression { 132 class _LogicExpression implements Expression {
133 /// The operator, `||` or `&&`. 133 /// The operator, `||` or `&&`.
134 final String op; 134 final String op;
135 135
136 final Expression left; 136 final Expression left;
137 final Expression right; 137 final Expression right;
138 138
139 _LogicExpression(this.op, this.left, this.right); 139 _LogicExpression(this.op, this.left, this.right);
140 140
141 void validate(Environment environment, List<String> errors) { 141 void validate(Environment environment, List<String> errors) {
142 left.validate(environment, errors); 142 left.validate(environment, errors);
143 right.validate(environment, errors); 143 right.validate(environment, errors);
144 } 144 }
145 145
146 bool evaluate(Environment environment) => (op == _Token.and) 146 bool evaluate(Environment environment) => (op == _Token.and)
147 ? left.evaluate(environment) && right.evaluate(environment) 147 ? left.evaluate(environment) && right.evaluate(environment)
148 : left.evaluate(environment) || right.evaluate(environment); 148 : left.evaluate(environment) || right.evaluate(environment);
149 149
150 String toString() => "($left $op $right)"; 150 String toString() {
151 String parenthesize(Expression operand) {
152 var result = operand.toString();
153 if (op == "&&" && operand is _LogicExpression && operand.op == "||") {
154 result = "($result)";
155 }
156
157 return result;
158 }
159
160 return "${parenthesize(left)} $op ${parenthesize(right)}";
161 }
151 } 162 }
152 163
153 /// Parser for Boolean expressions in a .status file for Dart. 164 /// Parser for Boolean expressions in a .status file for Dart.
154 class _ExpressionParser { 165 class _ExpressionParser {
155 final _Scanner _scanner; 166 final _Scanner _scanner;
156 167
157 _ExpressionParser(String expression) : _scanner = new _Scanner(expression); 168 _ExpressionParser(String expression) : _scanner = new _Scanner(expression);
158 169
159 Expression parse() { 170 Expression parse() {
160 var expression = _parseOr(); 171 var expression = _parseOr();
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
283 return true; 294 return true;
284 } 295 }
285 296
286 /// Consumes the current token and returns it. 297 /// Consumes the current token and returns it.
287 String advance() { 298 String advance() {
288 var previous = current; 299 var previous = current;
289 current = tokenIterator.moveNext() ? tokenIterator.current : null; 300 current = tokenIterator.moveNext() ? tokenIterator.current : null;
290 return previous; 301 return previous;
291 } 302 }
292 } 303 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698