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

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

Issue 997593003: Add a scanner and parser for platform selectors. (Closed) Base URL: git@github.com:dart-lang/unittest@master
Patch Set: A few more fixes from better future knowledge Created 5 years, 9 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
OLDNEW
(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(branch1.span, branch2.span);
50
51 /// The left-hand branch of the expression.
52 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.
53
54 /// The right-hand branch of the expression.
55 final Node branch2;
56
57 OrNode(this.branch1, this.branch2);
58
59 String toString() {
60 var string1 = branch1 is AndNode || branch1 is ConditionalNode
61 ? "($branch1)"
62 : branch1;
63 var string2 = branch2 is AndNode || branch2 is ConditionalNode
64 ? "($branch2)"
65 : branch2;
66
67 return "$string1 || $string2";
68 }
69 }
70
71 /// An and expression.
72 class AndNode implements Node {
73 FileSpan get span => _expandSafe(branch1.span, branch2.span);
74
75 /// The left-hand branch of the expression.
76 final Node branch1;
77
78 /// The right-hand branch of the expression.
79 final Node branch2;
80
81 AndNode(this.branch1, this.branch2);
82
83 String toString() {
84 var string1 = branch1 is OrNode || branch1 is ConditionalNode
85 ? "($branch1)"
86 : branch1;
87 var string2 = branch2 is OrNode || branch2 is ConditionalNode
88 ? "($branch2)"
89 : branch2;
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, branch2.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 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
104
105 /// The branch to run if the condition is false.
106 final Node branch2;
107
108 ConditionalNode(this.condition, this.branch1, this.branch2);
109
110 String toString() {
111 var conditionString =
112 condition is ConditionalNode ? "($condition)" : condition;
113 var string1 = branch1 is ConditionalNode ? "($branch1)" : branch1;
114 return "$conditionString ? $string1 : $branch2";
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 }
OLDNEW
« no previous file with comments | « no previous file | lib/src/backend/platform_selector/parser.dart » ('j') | lib/src/backend/platform_selector/parser.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698