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

Side by Side Diff: dart/language/grammar/example.dart

Issue 10917035: Remove language directory. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 8 years, 3 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
« no previous file with comments | « dart/language/grammar/doit.sh ('k') | dart/language/grammar/examples.xsl » ('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) 2011, 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 class Object {
6 var x;
7 int foo() {
8 return 42;
9 }
10 bar(int x, int y, z) { }
11 }
12
13 class Baz extends Kuk implements A, B, C {
14 static final y = 87, z = 42;
15 static final Foo moms = 42, kuks = 42;
16 final Kuk hest;
17 static var foo;
18 var fisk = 2;
19 final fiskHest = const Foo();
20
21 /* Try a few
22 * syntactic constructs. */
23 void baz() {
24 if (42) if (42) 42; else throw 42;
25 switch (42) { case 42: return 42; default: break; }
26 try { } catch (var e) { }
27 int kongy(x,y) { return 42; } // This is a comment.
28 for (var i in e) {}
29 for (var i in e.baz) {}
30 for (var i in e[0]) {}
31 for (final i in e) {}
32 for (final Foo<int> i in e) {}
33 for (Foo<int> i in e) {}
34 for (i in e) {}
35 }
36
37 int hest(a) {
38 for (var i = 0; i < a.length; i++) {
39 a.b.c.f().g[i] += foo(i);
40 int kuk = 42;
41 (kuk);
42 id(x) { return x; }
43 int id(x) { return x; }
44 var f = hest() { };
45 var f = int horse() { };
46 assert(x == 12);
47 }
48 }
49
50 Baz(x, y, z) : super(x, y, z) {}
51 }
52
53 interface Foo extends D, E {
54 bar();
55 }
56
57
58 // Test various basic forms of formal parameters.
59 interface MethodSignatureSyntax {
60 a();
61 b(x);
62 c(int x);
63 d(var x);
64 e(final x);
65
66 f(x, y);
67 g(var x, y);
68 h(final x, y);
69 j(var x, var y);
70 k(final x, final y);
71
72 l(int x, y);
73 m(int x, int y);
74 }
75
76
77 // Test more details on the formal parameter syntax.
78 class FormalParameterSyntax {
79 a([x = 42]) { }
80 b([int x = 42]) { }
81 c(x, [y = 42]) { }
82 d(x, [int y = 42]) { }
83 }
84
85
86 // Test various forms of function type syntax.
87 class FunctionTypeSyntax {
88 Function a;
89 static Function b;
90
91 Function c() { }
92 static Function d() { }
93
94 e(Function f) { }
95 static f(Function f) { }
96
97 // Dart allows C++ style function types in formal
98 // parameter lists.
99 g(f()) { }
100 h(void f()) { }
101 j(f(x)) { }
102 k(f(x, y)) { }
103 l(int f(int x, int y)) { }
104 m(int x, int f(x), int y) { }
105 }
106
107
108 // Test super calls.
109 class SuperCallSyntax {
110 method() {
111 super.foo();
112 super.foo(1);
113 super.foo(1, 2);
114
115 super.foo().x;
116 super.foo()[42];
117 super.foo().x++;
118
119 super.foo()();
120 super.foo(1, 2)(3, 4);
121
122 var v1 = super.foo();
123 var v2 = super.foo(1);
124 var v3 = super.foo(1, 2);
125
126 var v4 = super.foo().x;
127 var v5 = super.foo()[42];
128 var v6 = super.foo().x++;
129
130 var v7 = super.foo()();
131 var v8 = super.foo(1, 2)(3, 4);
132 }
133
134 get field() {
135 super.field;
136 super.field = 42;
137 super.field += 87;
138
139 super['baz'];
140 super['baz'] = 42;
141 super['baz'] += 87;
142 }
143 }
144
145
146 // Test generic types.
147 class Box<T> {
148 T t;
149 getT() { return t; }
150 setT(T t) { this.t = t; }
151 }
152
153 class UseBox {
154 Box<Box<Box<prefix.Fisk>>> boxIt(Box<Box<prefix.Fisk>> box) {
155 return new Box<Box<Box<prefix.Fisk>>>(box);
156 }
157 }
158
159 // Test shift operators.
160 class Shifting {
161 operator >>>(other) {
162 Box<Box<Box<prefix.Fisk>>> foo = null;
163 return other >>> 1;
164 }
165
166 operator >>(other) {
167 Box<Box<prefix.Fisk>> foo = null;
168 return other >> 1;
169 }
170 }
171
172 typedef void VoidCallback1(Event event);
173 typedef void VoidCallback2(Event event, int x);
174 typedef void VoidCallback3(Event event, int x, y);
175 typedef void VoidCallback4(Event event, int x, var y);
176
177 typedef Callback1(Event event);
178 typedef Callback2(Event event, int x);
179 typedef Callback3(Event event, int x, y);
180 typedef Callback4(Event event, int x, var y);
181
182 typedef int IntCallback1(Event event);
183 typedef int IntCallback2(Event event, int x);
184 typedef int IntCallback3(Event event, int x, y);
185 typedef int IntCallback4(Event event, int x, var y);
186
187 typedef Box<int> BoxCallback1(Event event);
188 typedef Box<int> BoxCallback2(Event event, int x);
189 typedef Box<int> BoxCallback3(Event event, int x, y);
190 typedef Box<int> BoxCallback4(Event event, int x, var y);
191
192 typedef Box<Box<int>> BoxBoxCallback1(Event event);
193 typedef Box<Box<int>> BoxBoxCallback2(Event event, int x);
194 typedef Box<Box<int>> BoxBoxCallback3(Event event, int x, y);
195 typedef Box<Box<int>> BoxBoxCallback4(Event event, int x, var y);
196
197 typedef void VoidCallbak1(Event event);
198 typedef void VoidCallbak2(Event event, int x);
199 typedef void VoidCallbak3(Event event, int x, y);
200 typedef void VoidCallbak4(Event event, int x, var y);
201
202 typedef void VoidCallbuk1<E>(E event);
203 typedef void VoidCallbuk2<E, I>(E event, I x);
204 typedef void VoidCallbuk3<E extends Event, I extends int>(E event, I x, y);
205 typedef void VoidCallbuk4<E extends Event<E>, I extends int>(E event, I x,
206 var y);
207
208 typedef Callbuk1<E>(E event);
209 typedef Callbuk2<E, I>(E event, I x);
210 typedef Callbuk3<E extends Event, I>(E event, I x, y);
211 typedef Callbuk4<E, I extends int>(E event, I x, var y);
212
213 typedef int IntCallbuk1<E>(E event);
214 typedef I IntCallbuk2<E extends Event, I extends int>(E event, I x);
215 typedef I IntCallbuk3<E, I>(E event, I x, y);
216 typedef I IntCallbuk4<E, I>(E event, I x, var y);
217
218 typedef Box<int> BoxCallbuk1<E>(E event);
219 typedef Box<I> BoxCallbuk2<E, I>(E event, I x);
220 typedef Box<I> BoxCallbuk3<E, I>(E event, I x, y);
221 typedef Box<I> BoxCallbuk4<E, I>(E event, I x, var y);
222
223 typedef Box<int> BoxBoxCallbuk1<E>(E event);
224 typedef Box<I> BoxBoxCallbuk2<E, I>(E event, I x);
225 typedef Box<I> BoxBoxCallbuk3<E, I>(E event, I x, y);
226 typedef Box<I> BoxBoxCallbuk4<E, I>(E event, I x, var y);
227
228 class NumberSyntax {
229 f() {
230 1; 12; 123;
231 1.0; 12.0; 123.0;
232 .1; .12; .123;
233 1.0; 12.12; 123.123;
234
235 1e1; 12e12; 123e123;
236 1e+1; 12e+12; 123e+123;
237 1e-1; 12e-12; 123e-123;
238
239 1.0e1; 12.0e12; 123.0e123;
240 1.0e+1; 12.0e+12; 123.0e+123;
241 1.0e-1; 12.0e-12; 123.0e-123;
242
243 .1e1; .12e12; .123e123;
244 .1e+1; .12e+12; .123e+123;
245 .1e-1; .12e-12; .123e-123;
246
247 1.0e1; 12.12e12; 123.123e123;
248 1.0e+1; 12.12e+12; 123.123e+123;
249 1.0e-1; 12.12e-12; 123.123e-123;
250
251 1.1234e+444;
252
253 o.d();
254 }
255 }
256
257 class ArrayLiteralSyntax {
258 void f() {
259 var a0 = [];
260 var a1 = [12];
261 var a2 = [null];
262 var a3 = [f(),o2];
263 var a4 = [(){return 42;},o2];
264 var a5 = [()=>42,o2];
265 var a6 = const <int> [ 12, 18 ];
266 var a7 = <int> [ 12, 18 ];
267 }
268 }
269
270 class MapLiteralSyntax {
271 void f() {
272 var o0 = {};
273 var o1 = {"a":12};
274 var o2 = {"a":null,};
275 var o3 = {"a":f(),"b":o2};
276 var o4 = {"a":(){return 42;},"b":o2,};
277 var o4 = {"a":()=>42,"b":o2,};
278 var o5 = {"if": 12};
279 var o6 = {"foo bar":null, "while":17};
280 var o7 = const <String,int> { "a": 12, "b": 18 };
281 var o8 = <String,int> { "a": 12, "b": 18 };
282 }
283 }
284
285 class CompileTimeConstructorSyntax {
286 const CompileTimeConstructorSyntax();
287 const CompileTimeConstructorSyntax() : super(1, 2, 3);
288 }
289
290 class AbstractMethodSyntax {
291 abstract f0();
292 abstract void f1();
293 abstract int f2(x, y);
294 abstract f3(int x, var y);
295
296 abstract get x();
297 abstract int get x();
298 abstract set y(value);
299 abstract void set y(value);
300
301 abstract operator +(x);
302 abstract int operator -(x);
303 }
304
305 class AssignableSyntax {
306 test(a) {
307 a[0] ? "a" : "b";
308 return a[0] ? "a" : "b";
309 }
310 }
311
312 class SetGetSyntax {
313 get x() { }
314 set x(v) { }
315 int get y() { }
316 void set y(v) { }
317
318 static get x() { }
319 static set x(v) { }
320 static int get y() { }
321 static void set y(v) { }
322 }
323
324 class SwitchSyntax {
325 void foo() {
326 switch (42) {
327 case 42:
328 var x = 0;
329 break;
330 case 87:
331 throw 42;
332 var x = 0; // Dead code is allowed by the grammar
333 L: default:
334 var x = 0;
335 return;
336 var y = 0; // Dead code is allowed by the grammar.
337 }
338 }
339 }
340
341 class ConstructorSyntax {
342 ConstructorSyntax(x, y) : super(), this.x = x, this.y = y {}
343 ConstructorSyntax.a(x, y) : x = x, super(), y = x {}
344 ConstructorSyntax.b(x, y) : this.x = y, this.y = x, super() {}
345 }
346
347 class FieldParameterSyntax {
348 FieldParameterSyntax(this.x){}
349 FieldParameterSyntax.a(int this.x){}
350 FieldParameterSyntax.b(var this.x, int y){}
351 FieldParameterSyntax.b(int x, final this.y){}
352 }
353
354 class WithNamedArguments {
355 void m1([int foo([int i])]) {}
356 void m2([int foo([int i]), int bar([int i])]) {}
357 void m3([int foo([int i, int i]), int bar([int i, int i])]) {}
358
359 void test() {
360 foo(x, n1:x);
361 foo(x, y, n1:x, n2:x);
362 foo(x, y, z, n1:x, n2:x, n3:x);
363 foo(n1:x);
364 foo(n1:x, n2:x);
365 foo(n1:x, n2:x, n3:x);
366 }
367 }
368
369 // Top level functions.
370 topLevelUntypedFunction() {}
371 void topLevelTypedFunction(int a) {}
372
373 // Top level variables.
374 final topLevelFinalUntypedVariable = 1;
375 final topLevelListFinalUntypedVariable = 1, b = 2, c = 3;
376 final int topLevelFinalTypedVariable = 1;
377 final int topLevelListFinalTypedVariable = 1, b = 2, c = 3;
378 int topLevelTypedVariable;
379 var topLevelUnTypedVariable;
380 int topLevelListTypedVariable, a, b;
381 var topLevelListUnTypedVariable, a, b;
382 var topLevelInitializedVariable = 2;
383 final topLevelInitializedVariable2 = const Foo();
384
385 // Top level setters
386 get topLevelGetter() {}
387 set topLevelSetter(a) {}
388 Foo<int> get topLevelGetter3() {}
389 void set topLevelSetter3(Foo<int> a) {}
390
391
392 class Operators {
393
394 operator ~() { }
395 operator negate() { }
396
397 operator *(x) { }
398 operator /(x) { }
399 operator %(x) { }
400 operator ~/(x) { }
401
402 operator +(x) { }
403 operator -(x) { }
404
405 operator <<(x) { }
406 operator >>(x) { }
407 operator >>>(x) { }
408
409 operator ==(x) { }
410 operator <=(x) { }
411 operator <(x) { }
412 operator >=(x) { }
413 operator >(x) { }
414
415 operator &(x) { }
416 operator ^(x) { }
417 operator |(x) { }
418
419 foo() {
420 ~super;
421 -super;
422
423 super * 42;
424 super / 42;
425 super % 42;
426 super ~/ 42;
427
428 super + 42;
429 super - 42;
430
431 super << 42;
432 super >> 42;
433 super >>> 42;
434
435 super == 42;
436 super != 42; // Expected to map to !(super == 42).
437 super <= 42;
438 super < 42;
439 super >= 42;
440 super > 42;
441
442 super & 42;
443 super ^ 42;
444 super | 42;
445
446 // BUG(4994724): Do we need to allow calling these?
447 !super;
448 super === 42;
449 super !== 42;
450 }
451
452 }
453
454
455 class Redirection {
456
457 const Redirection() : this.foo();
458 const Redirection.bar() : this.foo();
459
460 Redirection() : this.foo();
461 Redirection.baz() : this.foo();
462
463 }
464
465
466 class FunctionBody {
467
468 // Even constructors can use the => syntax instead of a
469 // body. Syntactically okay, but doesn't make much sense since
470 // constructors aren't allowed to return anything.
471 FunctionBody() : this.x = 99 => 42;
472
473 foo() => 99;
474 get x() => x;
475 set y(x) => x + y; // Setters should be void -- not enforced by syntax.
476 operator +(x) => x + 42;
477
478 int foo() => 99;
479 int get x() => x;
480 void set y(x) => x + y;
481 int operator +(x) => x + 42;
482
483 bar() {
484 baz() => 87;
485 int biz() => 87;
486
487 var f = () => 42;
488 var g = int _() => 87;
489 var h = fugl() => 99;
490 }
491
492 }
493
494
495 class NastyConstructor {
496
497 // NOTE: These examples aren't pretty but they illustrate what's legal.
498 A() : x = foo() => 42;
499 A() : x = foo() { }
500 A() : x = (foo() { }) { }
501 A() : x = (foo() => 42) { }
502
503 A() : x = ((foo()) { }) { }
504 A() : x = ((foo()) => 42) { }
505 A() : x = ((foo()) { }) => 87;
506 A() : x = ((foo()) => 42) => 87;
507
508 A() : x = bar((foo()) { }) { }
509 A() : x = bar((foo()) => 42) { }
510 A() : x = bar((foo()) { }) => 87;
511 A() : x = bar((foo()) => 42) => 87;
512
513 A() : x = [(foo()) { }] { }
514 A() : x = [(foo()) => 42] { }
515 A() : x = [(foo()) { }] => 87;
516 A() : x = [(foo()) => 42] => 87;
517
518 A() : x = {'x':(foo()) { }} { }
519 A() : x = {'x':(foo()) => 42} { }
520 A() : x = {'x':(foo()) { }} => 87;
521 A() : x = {'x':(foo()) => 42} => 87;
522
523 factory lib.Class<T, X>.name() { return null; }
524 factory Class<T, X>.name() { return null; }
525 factory lib.Class<T, X>() { return null; }
526 factory Class<T, X>() { return null; }
527 }
OLDNEW
« no previous file with comments | « dart/language/grammar/doit.sh ('k') | dart/language/grammar/examples.xsl » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698