| 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 #import('parser_helper.dart'); |
| 6 #import("../../../lib/compiler/implementation/scanner/scannerlib.dart"); |
| 7 #import("../../../lib/compiler/implementation/tree/tree.dart"); |
| 8 |
| 9 |
| 10 void testNode(Node node, String expected, String text, [bool hard = true]) { |
| 11 var debug = 'text=$text,expected=$expected,node:${node}'; |
| 12 Expect.isNotNull(node, debug); |
| 13 |
| 14 Token beginToken = node.getBeginToken(); |
| 15 Expect.isNotNull(beginToken, debug); |
| 16 Token endToken = node.getEndToken(); |
| 17 Expect.isNotNull(endToken, debug); |
| 18 |
| 19 int begin = beginToken.charOffset; |
| 20 int end = endToken.charOffset + endToken.slowCharCount; |
| 21 Expect.isTrue(begin <= end, debug); |
| 22 |
| 23 if (hard) { |
| 24 Expect.stringEquals(expected, text.substring(begin, end), debug); |
| 25 } |
| 26 } |
| 27 |
| 28 Node testExpression(String text, [String alternate]) { |
| 29 var node = parseStatement('$text;').expression; |
| 30 testNode(node, alternate === null ? text : alternate, text); |
| 31 return node; |
| 32 } |
| 33 |
| 34 void testUnaryExpression() { |
| 35 testExpression('x++'); |
| 36 testExpression('++x'); |
| 37 } |
| 38 |
| 39 void testAssignment() { |
| 40 Expression node; |
| 41 SendSet sendSet; |
| 42 String text; |
| 43 |
| 44 text = "a = b"; |
| 45 node = testExpression(text); |
| 46 |
| 47 text = "a = b = c"; |
| 48 node = testExpression(text); |
| 49 // Should parse as: a = (b = c). |
| 50 Expect.isNotNull(sendSet = node.asSendSet()); |
| 51 testNode(sendSet.arguments.head.asSendSet(), 'b = c', text); |
| 52 |
| 53 text = "a = b = c = d"; |
| 54 node = testExpression(text); |
| 55 // Should parse as: a = (b = (c = d)). |
| 56 Expect.isNotNull(sendSet = node.asSendSet()); |
| 57 testNode(sendSet = sendSet.arguments.head.asSendSet(), 'b = c = d', text); |
| 58 testNode(sendSet = sendSet.arguments.head.asSendSet(), 'c = d', text); |
| 59 |
| 60 text = "a.b = c"; |
| 61 node = testExpression(text); |
| 62 // Should parse as: receiver = a, selector = b, arguments = c. |
| 63 Expect.isNotNull(sendSet = node.asSendSet()); |
| 64 testNode(sendSet.receiver, "a", "a.b = c"); |
| 65 testNode(sendSet.selector, "b", "a.b = c"); |
| 66 testNode(sendSet.arguments.head, "c", "a.b = c"); |
| 67 |
| 68 text = "a.b = c.d"; |
| 69 node = testExpression(text); |
| 70 // Should parse as: a.b = (c.d). |
| 71 Expect.isNotNull(sendSet = node.asSendSet()); |
| 72 Expect.stringEquals("a", sendSet.receiver.toString()); |
| 73 Expect.stringEquals("b", sendSet.selector.toString()); |
| 74 Expect.stringEquals("c.d", sendSet.arguments.head.toString()); |
| 75 |
| 76 text = "a.b = c.d = e.f"; |
| 77 node = testExpression(text); |
| 78 // Should parse as: a.b = (c.d = (e.f)). |
| 79 Expect.isNotNull(sendSet = node.asSendSet()); |
| 80 testNode(sendSet.receiver, "a", text); |
| 81 testNode(sendSet.selector, "b", text); |
| 82 Expect.isNotNull(sendSet = sendSet.arguments.head.asSendSet()); |
| 83 testNode(sendSet.receiver, "c", text); |
| 84 testNode(sendSet.selector, "d", text); |
| 85 testNode(sendSet.arguments.head, "e.f", text); |
| 86 } |
| 87 |
| 88 void testIndex() { |
| 89 Expression node; |
| 90 Send send; |
| 91 SendSet sendSet; |
| 92 String text; |
| 93 |
| 94 text = "a[b]"; |
| 95 node = testExpression(text); |
| 96 // Should parse as: (a)[b]. |
| 97 Expect.isNotNull(send = node.asSend()); |
| 98 testNode(send.receiver, "a", text); |
| 99 // TODO(johnniwinther): [selector] is the synthetic [] Operator which doesn't |
| 100 // return the right begin/end tokens. In the next line we should have expected |
| 101 // "[b]" instead of "[b". |
| 102 testNode(send.selector, "[b", text); |
| 103 testNode(send.arguments.head, "b", text); |
| 104 |
| 105 text = "a[b] = c"; |
| 106 node = testExpression(text); |
| 107 // Should parse as: (a)[b] = c. |
| 108 Expect.isNotNull(sendSet = node.asSendSet()); |
| 109 testNode(sendSet.receiver, "a", text); |
| 110 testNode(sendSet.selector, "[]", text, false); // Operator token is synthetic. |
| 111 testNode(sendSet.assignmentOperator, "=", text); |
| 112 testNode(sendSet.arguments.head, "b", text); |
| 113 testNode(sendSet.arguments.tail.head, "c", text); |
| 114 |
| 115 text = "a.b[c]"; |
| 116 node = testExpression(text); |
| 117 // Should parse as: (a.b)[c]. |
| 118 Expect.isNotNull(send = node.asSend()); |
| 119 testNode(send.receiver, "a.b", text); |
| 120 testNode(send.selector, "[]", text, false); // Operator token is synthetic. |
| 121 testNode(send.arguments.head, "c", text); |
| 122 |
| 123 text = "a.b[c] = d"; |
| 124 node = testExpression(text); |
| 125 // Should parse as: (a.b)[] = (c, d). |
| 126 Expect.isNotNull(sendSet = node.asSendSet()); |
| 127 Expect.isNotNull(send = sendSet.receiver.asSend()); |
| 128 testNode(send, "a.b", text); |
| 129 testNode(sendSet.selector, "[]", text, false); // Operator token is synthetic. |
| 130 testNode(sendSet.assignmentOperator, "=", text); |
| 131 testNode(sendSet.arguments.head, "c", text); |
| 132 testNode(sendSet.arguments.tail.head, "d", text); |
| 133 } |
| 134 |
| 135 void testPostfix() { |
| 136 Expression node; |
| 137 SendSet sendSet; |
| 138 String text; |
| 139 |
| 140 text = "a.b++"; |
| 141 node = testExpression(text); |
| 142 // Should parse as: (a.b)++. |
| 143 Expect.isNotNull(sendSet = node.asSendSet()); |
| 144 testNode(sendSet.receiver, "a", text); |
| 145 testNode(sendSet.selector, "b", text); |
| 146 testNode(sendSet.assignmentOperator, "++", text); |
| 147 Expect.isTrue(sendSet.arguments.isEmpty()); |
| 148 |
| 149 text = "++a[b]"; |
| 150 // TODO(johnniwinther): SendSet generates the wrong end token in the following |
| 151 // line. We should have [:testExpression(text):] instead of |
| 152 // [:testExpression(text, "++a"):]. |
| 153 node = testExpression(text, "++a"); |
| 154 Expect.isNotNull(sendSet = node.asSendSet()); |
| 155 testNode(sendSet.receiver, "a", text); |
| 156 testNode(sendSet.selector, "[]", text, false); // Operator token is synthetic. |
| 157 testNode(sendSet.assignmentOperator, "++", text); |
| 158 testNode(sendSet.arguments.head, "b", text); |
| 159 |
| 160 text = "a[b]++"; |
| 161 node = testExpression(text); |
| 162 Expect.isNotNull(sendSet = node.asSendSet()); |
| 163 testNode(sendSet.receiver, "a", text); |
| 164 testNode(sendSet.selector, "[]", text, false); // Operator token is synthetic. |
| 165 testNode(sendSet.assignmentOperator, "++", text); |
| 166 testNode(sendSet.arguments.head, "b", text); |
| 167 } |
| 168 |
| 169 void main() { |
| 170 testUnaryExpression(); |
| 171 testAssignment(); |
| 172 testIndex(); |
| 173 testPostfix(); |
| 174 } |
| OLD | NEW |