OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 'package:source_span/source_span.dart'; | 5 import 'package:source_span/source_span.dart'; |
6 | 6 |
7 import 'visitor.dart'; | 7 import 'visitor.dart'; |
8 | 8 |
9 /// The superclass of nodes in the boolean selector abstract syntax tree. | 9 /// The superclass of nodes in the boolean selector abstract syntax tree. |
10 abstract class Node { | 10 abstract class Node { |
(...skipping 20 matching lines...) Expand all Loading... |
31 /// The variable name. | 31 /// The variable name. |
32 final String name; | 32 final String name; |
33 | 33 |
34 Iterable<String> get variables => [name]; | 34 Iterable<String> get variables => [name]; |
35 | 35 |
36 VariableNode(this.name, [this.span]); | 36 VariableNode(this.name, [this.span]); |
37 | 37 |
38 accept(Visitor visitor) => visitor.visitVariable(this); | 38 accept(Visitor visitor) => visitor.visitVariable(this); |
39 | 39 |
40 String toString() => name; | 40 String toString() => name; |
| 41 |
| 42 bool operator==(other) => other is VariableNode && name == other.name; |
| 43 |
| 44 int get hashCode => name.hashCode; |
41 } | 45 } |
42 | 46 |
43 /// A negation expression. | 47 /// A negation expression. |
44 class NotNode implements Node { | 48 class NotNode implements Node { |
45 final FileSpan span; | 49 final FileSpan span; |
46 | 50 |
47 /// The expression being negated. | 51 /// The expression being negated. |
48 final Node child; | 52 final Node child; |
49 | 53 |
50 Iterable<String> get variables => child.variables; | 54 Iterable<String> get variables => child.variables; |
51 | 55 |
52 NotNode(this.child, [this.span]); | 56 NotNode(this.child, [this.span]); |
53 | 57 |
54 accept(Visitor visitor) => visitor.visitNot(this); | 58 accept(Visitor visitor) => visitor.visitNot(this); |
55 | 59 |
56 String toString() => child is VariableNode || child is NotNode | 60 String toString() => child is VariableNode || child is NotNode |
57 ? "!$child" | 61 ? "!$child" |
58 : "!($child)"; | 62 : "!($child)"; |
| 63 |
| 64 bool operator==(other) => other is NotNode && child == other.child; |
| 65 |
| 66 int get hashCode => ~child.hashCode; |
59 } | 67 } |
60 | 68 |
61 /// An or expression. | 69 /// An or expression. |
62 class OrNode implements Node { | 70 class OrNode implements Node { |
63 FileSpan get span => _expandSafe(left.span, right.span); | 71 FileSpan get span => _expandSafe(left.span, right.span); |
64 | 72 |
65 /// The left-hand branch of the expression. | 73 /// The left-hand branch of the expression. |
66 final Node left; | 74 final Node left; |
67 | 75 |
68 /// The right-hand branch of the expression. | 76 /// The right-hand branch of the expression. |
(...skipping 11 matching lines...) Expand all Loading... |
80 String toString() { | 88 String toString() { |
81 var string1 = left is AndNode || left is ConditionalNode | 89 var string1 = left is AndNode || left is ConditionalNode |
82 ? "($left)" | 90 ? "($left)" |
83 : left; | 91 : left; |
84 var string2 = right is AndNode || right is ConditionalNode | 92 var string2 = right is AndNode || right is ConditionalNode |
85 ? "($right)" | 93 ? "($right)" |
86 : right; | 94 : right; |
87 | 95 |
88 return "$string1 || $string2"; | 96 return "$string1 || $string2"; |
89 } | 97 } |
| 98 |
| 99 bool operator==(other) => |
| 100 other is OrNode && left == other.left && right == other.right; |
| 101 |
| 102 int get hashCode => left.hashCode ^ right.hashCode; |
90 } | 103 } |
91 | 104 |
92 /// An and expression. | 105 /// An and expression. |
93 class AndNode implements Node { | 106 class AndNode implements Node { |
94 FileSpan get span => _expandSafe(left.span, right.span); | 107 FileSpan get span => _expandSafe(left.span, right.span); |
95 | 108 |
96 /// The left-hand branch of the expression. | 109 /// The left-hand branch of the expression. |
97 final Node left; | 110 final Node left; |
98 | 111 |
99 /// The right-hand branch of the expression. | 112 /// The right-hand branch of the expression. |
(...skipping 11 matching lines...) Expand all Loading... |
111 String toString() { | 124 String toString() { |
112 var string1 = left is OrNode || left is ConditionalNode | 125 var string1 = left is OrNode || left is ConditionalNode |
113 ? "($left)" | 126 ? "($left)" |
114 : left; | 127 : left; |
115 var string2 = right is OrNode || right is ConditionalNode | 128 var string2 = right is OrNode || right is ConditionalNode |
116 ? "($right)" | 129 ? "($right)" |
117 : right; | 130 : right; |
118 | 131 |
119 return "$string1 && $string2"; | 132 return "$string1 && $string2"; |
120 } | 133 } |
| 134 |
| 135 bool operator==(other) => |
| 136 other is AndNode && left == other.left && right == other.right; |
| 137 |
| 138 int get hashCode => left.hashCode ^ right.hashCode; |
121 } | 139 } |
122 | 140 |
123 /// A ternary conditional expression. | 141 /// A ternary conditional expression. |
124 class ConditionalNode implements Node { | 142 class ConditionalNode implements Node { |
125 FileSpan get span => _expandSafe(condition.span, whenFalse.span); | 143 FileSpan get span => _expandSafe(condition.span, whenFalse.span); |
126 | 144 |
127 /// The condition expression to check. | 145 /// The condition expression to check. |
128 final Node condition; | 146 final Node condition; |
129 | 147 |
130 /// The branch to run if the condition is true. | 148 /// The branch to run if the condition is true. |
(...skipping 11 matching lines...) Expand all Loading... |
142 ConditionalNode(this.condition, this.whenTrue, this.whenFalse); | 160 ConditionalNode(this.condition, this.whenTrue, this.whenFalse); |
143 | 161 |
144 accept(Visitor visitor) => visitor.visitConditional(this); | 162 accept(Visitor visitor) => visitor.visitConditional(this); |
145 | 163 |
146 String toString() { | 164 String toString() { |
147 var conditionString = | 165 var conditionString = |
148 condition is ConditionalNode ? "($condition)" : condition; | 166 condition is ConditionalNode ? "($condition)" : condition; |
149 var trueString = whenTrue is ConditionalNode ? "($whenTrue)" : whenTrue; | 167 var trueString = whenTrue is ConditionalNode ? "($whenTrue)" : whenTrue; |
150 return "$conditionString ? $trueString : $whenFalse"; | 168 return "$conditionString ? $trueString : $whenFalse"; |
151 } | 169 } |
| 170 |
| 171 bool operator==(other) => |
| 172 other is ConditionalNode && |
| 173 condition == other.condition && |
| 174 whenTrue == other.whenTrue && |
| 175 whenFalse == other.whenFalse; |
| 176 |
| 177 int get hashCode => |
| 178 condition.hashCode ^ whenTrue.hashCode ^ whenFalse.hashCode; |
152 } | 179 } |
153 | 180 |
154 /// Like [FileSpan.expand], except if [start] and [end] are `null` or from | 181 /// Like [FileSpan.expand], except if [start] and [end] are `null` or from |
155 /// different files it returns `null` rather than throwing an error. | 182 /// different files it returns `null` rather than throwing an error. |
156 FileSpan _expandSafe(FileSpan start, FileSpan end) { | 183 FileSpan _expandSafe(FileSpan start, FileSpan end) { |
157 if (start == null || end == null) return null; | 184 if (start == null || end == null) return null; |
158 if (start.file != end.file) return null; | 185 if (start.file != end.file) return null; |
159 return start.expand(end); | 186 return start.expand(end); |
160 } | 187 } |
OLD | NEW |