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