OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 library unittest.backend.platform_selector.ast; |
| 6 |
| 7 import 'package:source_span/source_span.dart'; |
| 8 |
| 9 /// The superclass of nodes in the platform selector abstract syntax tree. |
| 10 abstract class Node { |
| 11 /// The span indicating where this node came from. |
| 12 /// |
| 13 /// This is a [FileSpan] because the nodes are parsed from a single continuous |
| 14 /// string, but the string itself isn't actually a file. It might come from a |
| 15 /// statically-parsed annotation or from a parameter. |
| 16 /// |
| 17 /// This may be `null` for nodes without source information. |
| 18 FileSpan get span; |
| 19 } |
| 20 |
| 21 /// A single variable. |
| 22 class VariableNode implements Node { |
| 23 final FileSpan span; |
| 24 |
| 25 /// The variable name. |
| 26 final String name; |
| 27 |
| 28 VariableNode(this.name, [this.span]); |
| 29 |
| 30 String toString() => name; |
| 31 } |
| 32 |
| 33 /// A negation expression. |
| 34 class NotNode implements Node { |
| 35 final FileSpan span; |
| 36 |
| 37 /// The expression being negated. |
| 38 final Node child; |
| 39 |
| 40 NotNode(this.child, [this.span]); |
| 41 |
| 42 String toString() => child is VariableNode || child is NotNode |
| 43 ? "!$child" |
| 44 : "!($child)"; |
| 45 } |
| 46 |
| 47 /// An or expression. |
| 48 class OrNode implements Node { |
| 49 FileSpan get span => _expandSafe(left.span, right.span); |
| 50 |
| 51 /// The left-hand branch of the expression. |
| 52 final Node left; |
| 53 |
| 54 /// The right-hand branch of the expression. |
| 55 final Node right; |
| 56 |
| 57 OrNode(this.left, this.right); |
| 58 |
| 59 String toString() { |
| 60 var string1 = left is AndNode || left is ConditionalNode |
| 61 ? "($left)" |
| 62 : left; |
| 63 var string2 = right is AndNode || right is ConditionalNode |
| 64 ? "($right)" |
| 65 : right; |
| 66 |
| 67 return "$string1 || $string2"; |
| 68 } |
| 69 } |
| 70 |
| 71 /// An and expression. |
| 72 class AndNode implements Node { |
| 73 FileSpan get span => _expandSafe(left.span, right.span); |
| 74 |
| 75 /// The left-hand branch of the expression. |
| 76 final Node left; |
| 77 |
| 78 /// The right-hand branch of the expression. |
| 79 final Node right; |
| 80 |
| 81 AndNode(this.left, this.right); |
| 82 |
| 83 String toString() { |
| 84 var string1 = left is OrNode || left is ConditionalNode |
| 85 ? "($left)" |
| 86 : left; |
| 87 var string2 = right is OrNode || right is ConditionalNode |
| 88 ? "($right)" |
| 89 : right; |
| 90 |
| 91 return "$string1 && $string2"; |
| 92 } |
| 93 } |
| 94 |
| 95 /// A ternary conditional expression. |
| 96 class ConditionalNode implements Node { |
| 97 FileSpan get span => _expandSafe(condition.span, whenFalse.span); |
| 98 |
| 99 /// The condition expression to check. |
| 100 final Node condition; |
| 101 |
| 102 /// The branch to run if the condition is true. |
| 103 final Node whenTrue; |
| 104 |
| 105 /// The branch to run if the condition is false. |
| 106 final Node whenFalse; |
| 107 |
| 108 ConditionalNode(this.condition, this.whenTrue, this.whenFalse); |
| 109 |
| 110 String toString() { |
| 111 var conditionString = |
| 112 condition is ConditionalNode ? "($condition)" : condition; |
| 113 var trueString = whenTrue is ConditionalNode ? "($whenTrue)" : whenTrue; |
| 114 return "$conditionString ? $trueString : $whenFalse"; |
| 115 } |
| 116 } |
| 117 |
| 118 /// Like [FileSpan.expand], except if [start] and [end] are `null` or from |
| 119 /// different files it returns `null` rather than throwing an error. |
| 120 FileSpan _expandSafe(FileSpan start, FileSpan end) { |
| 121 if (start == null || end == null) return null; |
| 122 if (start.file != end.file) return null; |
| 123 return start.expand(end); |
| 124 } |
OLD | NEW |