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

Side by Side Diff: lib/src/ast.dart

Issue 1712793002: Add the content of the package. (Closed) Base URL: git@github.com:dart-lang/boolean_selector@master
Patch Set: Created 4 years, 10 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
« no previous file with comments | « lib/src/all.dart ('k') | lib/src/evaluator.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 import 'package:source_span/source_span.dart';
6
7 import 'visitor.dart';
8
9 /// The superclass of nodes in the boolean 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 /// All the variables in this node, in the order they appear.
21 Iterable<String> get variables;
22
23 /// Calls the appropriate [Visitor] method on [this] and returns the result.
24 accept(Visitor visitor);
25 }
26
27 /// A single variable.
28 class VariableNode implements Node {
29 final FileSpan span;
30
31 /// The variable name.
32 final String name;
33
34 Iterable<String> get variables => [name];
35
36 VariableNode(this.name, [this.span]);
37
38 accept(Visitor visitor) => visitor.visitVariable(this);
39
40 String toString() => name;
41 }
42
43 /// A negation expression.
44 class NotNode implements Node {
45 final FileSpan span;
46
47 /// The expression being negated.
48 final Node child;
49
50 Iterable<String> get variables => child.variables;
51
52 NotNode(this.child, [this.span]);
53
54 accept(Visitor visitor) => visitor.visitNot(this);
55
56 String toString() => child is VariableNode || child is NotNode
57 ? "!$child"
58 : "!($child)";
59 }
60
61 /// An or expression.
62 class OrNode implements Node {
63 FileSpan get span => _expandSafe(left.span, right.span);
64
65 /// The left-hand branch of the expression.
66 final Node left;
67
68 /// The right-hand branch of the expression.
69 final Node right;
70
71 Iterable<String> get variables sync* {
72 yield* left.variables;
73 yield* right.variables;
74 }
75
76 OrNode(this.left, this.right);
77
78 accept(Visitor visitor) => visitor.visitOr(this);
79
80 String toString() {
81 var string1 = left is AndNode || left is ConditionalNode
82 ? "($left)"
83 : left;
84 var string2 = right is AndNode || right is ConditionalNode
85 ? "($right)"
86 : right;
87
88 return "$string1 || $string2";
89 }
90 }
91
92 /// An and expression.
93 class AndNode implements Node {
94 FileSpan get span => _expandSafe(left.span, right.span);
95
96 /// The left-hand branch of the expression.
97 final Node left;
98
99 /// The right-hand branch of the expression.
100 final Node right;
101
102 Iterable<String> get variables sync* {
103 yield* left.variables;
104 yield* right.variables;
105 }
106
107 AndNode(this.left, this.right);
108
109 accept(Visitor visitor) => visitor.visitAnd(this);
110
111 String toString() {
112 var string1 = left is OrNode || left is ConditionalNode
113 ? "($left)"
114 : left;
115 var string2 = right is OrNode || right is ConditionalNode
116 ? "($right)"
117 : right;
118
119 return "$string1 && $string2";
120 }
121 }
122
123 /// A ternary conditional expression.
124 class ConditionalNode implements Node {
125 FileSpan get span => _expandSafe(condition.span, whenFalse.span);
126
127 /// The condition expression to check.
128 final Node condition;
129
130 /// The branch to run if the condition is true.
131 final Node whenTrue;
132
133 /// The branch to run if the condition is false.
134 final Node whenFalse;
135
136 Iterable<String> get variables sync* {
137 yield* condition.variables;
138 yield* whenTrue.variables;
139 yield* whenFalse.variables;
140 }
141
142 ConditionalNode(this.condition, this.whenTrue, this.whenFalse);
143
144 accept(Visitor visitor) => visitor.visitConditional(this);
145
146 String toString() {
147 var conditionString =
148 condition is ConditionalNode ? "($condition)" : condition;
149 var trueString = whenTrue is ConditionalNode ? "($whenTrue)" : whenTrue;
150 return "$conditionString ? $trueString : $whenFalse";
151 }
152 }
153
154 /// Like [FileSpan.expand], except if [start] and [end] are `null` or from
155 /// different files it returns `null` rather than throwing an error.
156 FileSpan _expandSafe(FileSpan start, FileSpan end) {
157 if (start == null || end == null) return null;
158 if (start.file != end.file) return null;
159 return start.expand(end);
160 }
OLDNEW
« no previous file with comments | « lib/src/all.dart ('k') | lib/src/evaluator.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698