| OLD | NEW |
| (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 |
| 6 class A { |
| 7 int value; |
| 8 A(this.value); |
| 9 void set(int value) { this.value = value; } |
| 10 int get() => value; |
| 11 int operator[](int index) => value + index; |
| 12 void operator[]=(int index, int newValue) { value += -index + newValue; } |
| 13 void test(int expected) { |
| 14 Expect.equals(expected, value); |
| 15 } |
| 16 Function limp(int n) { |
| 17 if (n == 0) return set; |
| 18 return () => limp(n - 1); |
| 19 } |
| 20 A get self() => this; |
| 21 A operator+(A other) { |
| 22 this.value += other.value; |
| 23 return this; |
| 24 } |
| 25 } |
| 26 |
| 27 class Box { |
| 28 A value; |
| 29 Box(this.value); |
| 30 A operator[](int pos) => value; |
| 31 void operator[]=(int pos, A a) { value = a; } |
| 32 A get x() => value; |
| 33 void set x(A a) { value = a; } |
| 34 } |
| 35 |
| 36 // Subset of grammar being tested. |
| 37 // |
| 38 // expression: |
| 39 // assignableExpression assignmentOperator expression |
| 40 // | conditionalExpression cascadeSection* |
| 41 // ; |
| 42 // expressionWithoutCascade: |
| 43 // assignableExpression assignmentOperator expressionWithoutCascade |
| 44 // | conditionalExpression |
| 45 // ; |
| 46 // expressionList: |
| 47 // expression (',' expression)* |
| 48 // ; |
| 49 // assignableExpression: |
| 50 // primary (arguments* assignableSelector)+ |
| 51 // | super assignableSelector |
| 52 // | identifier |
| 53 // ; |
| 54 // conditionalExpression: |
| 55 // logicalOrExpression ('?' expressionWithoutCascade ':' expressionWithoutCa
scade)? |
| 56 // ; |
| 57 // primary: |
| 58 // thisExpression |
| 59 // | super assignableSelector |
| 60 // | functionExpression |
| 61 // | literal |
| 62 // | identifier |
| 63 // | newExpression |
| 64 // | constObjectExpression |
| 65 // | '(' expression ')' |
| 66 // ; |
| 67 // assignableSelector: |
| 68 // '[' expression ']' |
| 69 // | '.' identifier |
| 70 // ; |
| 71 // |
| 72 // In words: |
| 73 // An assignableExpression is either a variable or something ending in |
| 74 // [expression] or .identifier. |
| 75 |
| 76 main() { |
| 77 A a = new A(42); |
| 78 A original = a; |
| 79 A b = new A(87); |
| 80 fa() => a; |
| 81 Box box = new Box(a); |
| 82 // Different expressions on the left-hand side of '..'. |
| 83 // conditionalExpression >> postfixExpression > primary selector* |
| 84 Expect.equals(a, a..set(37)..get()); |
| 85 a.test(37); |
| 86 Expect.equals(a, fa()..set(42)..get()); |
| 87 a.test(42); |
| 88 Expect.equals(a, box.x..set(37)..get()); |
| 89 a.test(37); |
| 90 // '..' binds to 'b + a', i.e., to the 'b' object, not to 'a'. |
| 91 Expect.equals(b, b + a..test(124)..set(117)..get()); |
| 92 b.test(117); |
| 93 a.test(37); |
| 94 |
| 95 // expression :: conditionalExpression cascadeSection |
| 96 // and conditionalExpression ends in expressionWithoutCascade. |
| 97 // I.e., '..' binds to the entire condition expression, not to 'b'. |
| 98 (a.value == 37) ? a : b..set(42); |
| 99 a.test(42); |
| 100 |
| 101 // This binds .. to 'a', not 'c=a', and performs assignment after reading |
| 102 // c.get(). |
| 103 A c = new A(21); |
| 104 c = a..set(c.get()); // FAILING. |
| 105 Expect.equals(a, c); |
| 106 Expect.equals(original, a); |
| 107 a.test(21); // Fails as 42 if above is parsed as (c = a)..set(c.get()). |
| 108 |
| 109 // Should be parsed as (box..x = (c = a))..x.test(21). |
| 110 c = null; |
| 111 box..x = c = a..x.test(21); |
| 112 c.test(21); |
| 113 // Other variants |
| 114 c = null; |
| 115 box..x = c = (a..test(21))..x.test(21); |
| 116 c.test(21); |
| 117 |
| 118 c = null; |
| 119 box..x = (c = a..test(21))..x.test(21); |
| 120 c.test(21); |
| 121 |
| 122 // Should work the same: |
| 123 (a..set(42))..test(42); |
| 124 a..set(21)..test(21); |
| 125 |
| 126 c = null; |
| 127 Box originalBox = box; |
| 128 // Should parse as: |
| 129 // box = (box..x = (a.value == 21 ? b : c)..x.test(117)); |
| 130 box = box..x = a.value == 21 ? b : c..x.test(117); |
| 131 Expect.equals(originalBox, box); |
| 132 Expect.equals(box.value, b); |
| 133 |
| 134 // New cascades are allowed inside an expressionWithoutCascade if properly |
| 135 // delimited. |
| 136 box..x = (a..set(42)..test(42))..x.test(42); |
| 137 } |
| OLD | NEW |