Chromium Code Reviews| Index: lib/src/backend/platform_selector/ast.dart |
| diff --git a/lib/src/backend/platform_selector/ast.dart b/lib/src/backend/platform_selector/ast.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..0deae11676520e797e98337a745400ec5da66d30 |
| --- /dev/null |
| +++ b/lib/src/backend/platform_selector/ast.dart |
| @@ -0,0 +1,124 @@ |
| +// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +library unittest.backend.platform_selector.ast; |
| + |
| +import 'package:source_span/source_span.dart'; |
| + |
| +/// The superclass of nodes in the platform selector abstract syntax tree. |
| +abstract class Node { |
| + /// The span indicating where this node came from. |
| + /// |
| + /// This is a [FileSpan] because the nodes are parsed from a single continuous |
| + /// string, but the string itself isn't actually a file. It might come from a |
| + /// statically-parsed annotation or from a parameter. |
| + /// |
| + /// This may be `null` for nodes without source information. |
| + FileSpan get span; |
| +} |
| + |
| +/// A single variable. |
| +class VariableNode implements Node { |
| + final FileSpan span; |
| + |
| + /// The variable name. |
| + final String name; |
| + |
| + VariableNode(this.name, [this.span]); |
| + |
| + String toString() => name; |
| +} |
| + |
| +/// A negation expression. |
| +class NotNode implements Node { |
| + final FileSpan span; |
| + |
| + /// The expression being negated. |
| + final Node child; |
| + |
| + NotNode(this.child, [this.span]); |
| + |
| + String toString() => child is VariableNode || child is NotNode |
| + ? "!$child" |
| + : "!($child)"; |
| +} |
| + |
| +/// An or expression. |
| +class OrNode implements Node { |
| + FileSpan get span => _expandSafe(branch1.span, branch2.span); |
| + |
| + /// The left-hand branch of the expression. |
| + final Node branch1; |
|
Bob Nystrom
2015/03/11 20:05:52
How about "left" and "right" for these?
nweiz
2015/03/12 19:48:57
Done.
|
| + |
| + /// The right-hand branch of the expression. |
| + final Node branch2; |
| + |
| + OrNode(this.branch1, this.branch2); |
| + |
| + String toString() { |
| + var string1 = branch1 is AndNode || branch1 is ConditionalNode |
| + ? "($branch1)" |
| + : branch1; |
| + var string2 = branch2 is AndNode || branch2 is ConditionalNode |
| + ? "($branch2)" |
| + : branch2; |
| + |
| + return "$string1 || $string2"; |
| + } |
| +} |
| + |
| +/// An and expression. |
| +class AndNode implements Node { |
| + FileSpan get span => _expandSafe(branch1.span, branch2.span); |
| + |
| + /// The left-hand branch of the expression. |
| + final Node branch1; |
| + |
| + /// The right-hand branch of the expression. |
| + final Node branch2; |
| + |
| + AndNode(this.branch1, this.branch2); |
| + |
| + String toString() { |
| + var string1 = branch1 is OrNode || branch1 is ConditionalNode |
| + ? "($branch1)" |
| + : branch1; |
| + var string2 = branch2 is OrNode || branch2 is ConditionalNode |
| + ? "($branch2)" |
| + : branch2; |
| + |
| + return "$string1 && $string2"; |
| + } |
| +} |
| + |
| +/// A ternary conditional expression. |
| +class ConditionalNode implements Node { |
| + FileSpan get span => _expandSafe(condition.span, branch2.span); |
| + |
| + /// The condition expression to check. |
| + final Node condition; |
| + |
| + /// The branch to run if the condition is true. |
| + final Node branch1; |
|
Bob Nystrom
2015/03/11 20:05:52
"thenBranch" and "elseBranch"?
nweiz
2015/03/12 19:48:57
Decided to go with "whenTrue" and "whenFalse" inst
|
| + |
| + /// The branch to run if the condition is false. |
| + final Node branch2; |
| + |
| + ConditionalNode(this.condition, this.branch1, this.branch2); |
| + |
| + String toString() { |
| + var conditionString = |
| + condition is ConditionalNode ? "($condition)" : condition; |
| + var string1 = branch1 is ConditionalNode ? "($branch1)" : branch1; |
| + return "$conditionString ? $string1 : $branch2"; |
| + } |
| +} |
| + |
| +/// Like [FileSpan.expand], except if [start] and [end] are `null` or from |
| +/// different files it returns `null` rather than throwing an error. |
| +FileSpan _expandSafe(FileSpan start, FileSpan end) { |
| + if (start == null || end == null) return null; |
| + if (start.file != end.file) return null; |
| + return start.expand(end); |
| +} |