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

Side by Side Diff: pkg/polymer_expression/lib/expression.dart

Issue 22950008: move fancy_syntax into Dart SVN (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: more readme edits Created 7 years, 4 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2013, 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 polymer_expression.expression;
6
7 import 'visitor.dart';
8
9 // Helper functions for building expression trees programmatically
10
11 EmptyExpression empty() => new EmptyExpression();
12 Literal literal(v) => new Literal(v);
13 MapLiteral mapLiteral(List<MapLiteralEntry> entries) => new MapLiteral(entries);
14 MapLiteralEntry mapLiteralEntry(Literal key, Expression value) =>
15 new MapLiteralEntry(key, value);
16 Identifier ident(String v) => new Identifier(v);
17 ParenthesizedExpression paren(Expression e) => new ParenthesizedExpression(e);
18 UnaryOperator unary(String op, Expression e) => new UnaryOperator(op, e);
19 BinaryOperator binary(Expression l, String op, Expression r) =>
20 new BinaryOperator(l, op, r);
21 Invoke invoke(Expression e, String m, [List<Expression> a]) =>
22 new Invoke(e, m, a);
23 InExpression inExpr(Expression l, Expression r) => new InExpression(l, r);
24
25
26 class AstFactory {
27 EmptyExpression empty() => new EmptyExpression();
28
29 Literal literal(v) => new Literal(v);
30
31 MapLiteral mapLiteral(List<MapLiteralEntry> entries) =>
32 new MapLiteral(entries);
33
34 MapLiteralEntry mapLiteralEntry(Literal key, Expression value) =>
35 new MapLiteralEntry(key, value);
36
37 Identifier identifier(String v) => new Identifier(v);
38
39 ParenthesizedExpression parenthesized(Expression e) =>
40 new ParenthesizedExpression(e);
41
42 UnaryOperator unary(String op, Expression e) => new UnaryOperator(op, e);
43
44 BinaryOperator binary(Expression l, String op, Expression r) =>
45 new BinaryOperator(l, op, r);
46
47 Invoke invoke(Expression e, String m, [List<Expression> a]) =>
48 new Invoke(e, m, a);
49
50 InExpression inExpr(Expression l, Expression r) => new InExpression(l, r);
51 }
52
53 /// Base class for all expressions
54 abstract class Expression {
55 accept(Visitor v);
56 }
57
58 class EmptyExpression extends Expression {
59 accept(Visitor v) => v.visitEmptyExpression(this);
60 bool operator ==(o) => o is EmptyExpression;
61 }
62
63 class Literal<T> extends Expression {
64 final T value;
65
66 Literal(this.value);
67
68 accept(Visitor v) => v.visitLiteral(this);
69
70 String toString() => (value is String) ? '"$value"' : '$value';
71
72 bool operator ==(o) => o is Literal<T> && o.value == value;
73
74 int get hashCode => value.hashCode;
75 }
76
77 class MapLiteral extends Expression {
78 final List<MapLiteralEntry> entries;
79
80 MapLiteral(this.entries);
81
82 accept(Visitor v) => v.visitMapLiteral(this);
83
84 String toString() => "{$entries}";
85
86 bool operator ==(o) => o is MapLiteral && _listEquals(o.entries, entries);
87 }
88
89 class MapLiteralEntry extends Expression {
90 final Literal key;
91 final Expression entryValue;
92
93 MapLiteralEntry(this.key, this.entryValue);
94
95 accept(Visitor v) => v.visitMapLiteralEntry(this);
96
97 String toString() => "$key: $entryValue";
98
99 bool operator ==(o) => o is MapLiteralEntry && o.key == key
100 && o.entryValue == entryValue;
101 }
102
103 class ParenthesizedExpression extends Expression {
104 final Expression child;
105
106 ParenthesizedExpression(this.child);
107
108 accept(Visitor v) => v.visitParenthesizedExpression(this);
109
110 String toString() => '($child)';
111
112 bool operator ==(o) => o is ParenthesizedExpression && o.child == child;
113
114 int get hashCode => child.hashCode;
115 }
116
117 class Identifier extends Expression {
118 final String value;
119
120 Identifier(this.value);
121
122 accept(Visitor v) => v.visitIdentifier(this);
123
124 String toString() => value;
125
126 bool operator ==(o) => o is Identifier && o.value == value;
127
128 int get hashCode => value.hashCode;
129 }
130
131 class UnaryOperator extends Expression {
132 final String operator;
133 final Expression child;
134
135 UnaryOperator(this.operator, this.child);
136
137 accept(Visitor v) => v.visitUnaryOperator(this);
138
139 String toString() => '$operator $child';
140
141 bool operator ==(o) => o is UnaryOperator && o.operator == operator
142 && o.child == child;
143 }
144
145 class BinaryOperator extends Expression {
146 final String operator;
147 final Expression left;
148 final Expression right;
149
150 BinaryOperator(this.left, this.operator, this.right);
151
152 accept(Visitor v) => v.visitBinaryOperator(this);
153
154 String toString() => '($left $operator $right)';
155
156 bool operator ==(o) => o is BinaryOperator && o.operator == operator
157 && o.left == left && o.right == right;
158 }
159
160 class InExpression extends Expression {
161 final Expression left;
162 final Expression right;
163
164 InExpression(this.left, this.right);
165
166 accept(Visitor v) => v.visitInExpression(this);
167
168 String toString() => '($left in $right)';
169
170 bool operator ==(o) => o is InExpression && o.left == left
171 && o.right == right;
172 }
173
174 /**
175 * Represents a function or method invocation. If [method] is null, then
176 * [receiver] is an expression that should evaluate to a function. If [method]
177 * is not null, then [receiver] is an expression that should evaluate to an
178 * object that has an appropriate method.
179 */
180 class Invoke extends Expression {
181 final Expression receiver;
182 final String method;
183 final List<Expression> arguments;
184
185 Invoke(this.receiver, this.method, [this.arguments]);
186
187 accept(Visitor v) => v.visitInvoke(this);
188
189 bool get isGetter => arguments == null;
190
191 String toString() => '$receiver.$method($arguments)';
192
193 bool operator ==(o) =>
194 o is Invoke
195 && o.receiver == receiver
196 && o.method == method
197 && _listEquals(o.arguments, arguments);
198 }
199
200 bool _listEquals(List a, List b) {
201 if (a == b) return true;
202 if (a == null || b == null) return false;
203 if (a.length != b.length) return false;
204 for (int i = 0; i < a.length; i++) {
205 if (a[i] != b[i]) return false;
206 }
207 return true;
208 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698