OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library polymer_expressions.parser; | 5 library polymer_expressions.parser; |
6 | 6 |
7 import 'tokenizer.dart'; | 7 import 'tokenizer.dart'; |
8 import 'expression.dart'; | 8 import 'expression.dart'; |
9 | 9 |
10 const _UNARY_OPERATORS = const ['+', '-', '!']; | 10 const _UNARY_OPERATORS = const ['+', '-', '!']; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 } else if (_token.value == '[') { | 57 } else if (_token.value == '[') { |
58 var indexExpr = _parseIndex(); | 58 var indexExpr = _parseIndex(); |
59 left = _astFactory.index(left, indexExpr); | 59 left = _astFactory.index(left, indexExpr); |
60 } else { | 60 } else { |
61 break; | 61 break; |
62 } | 62 } |
63 } else if (_token.kind == DOT_TOKEN) { | 63 } else if (_token.kind == DOT_TOKEN) { |
64 _advance(); | 64 _advance(); |
65 var right = _parseUnary(); | 65 var right = _parseUnary(); |
66 left = _makeInvokeOrGetter(left, right); | 66 left = _makeInvokeOrGetter(left, right); |
67 } else if (_token.kind == KEYWORD_TOKEN && _token.value == 'in') { | 67 } else if (_token.kind == KEYWORD_TOKEN) { |
68 left = _parseComprehension(left); | 68 if (_token.value == 'in') { |
| 69 left = _parseInExpression(left); |
| 70 } else if (_token.value == 'as') { |
| 71 left = _parseAsExpression(left); |
| 72 } else { |
| 73 break; |
| 74 } |
69 } else if (_token.kind == OPERATOR_TOKEN | 75 } else if (_token.kind == OPERATOR_TOKEN |
70 && _token.precedence >= precedence) { | 76 && _token.precedence >= precedence) { |
71 left = _token.value == '?' ? _parseTernary(left) : _parseBinary(left); | 77 left = _token.value == '?' ? _parseTernary(left) : _parseBinary(left); |
72 } else { | 78 } else { |
73 break; | 79 break; |
74 } | 80 } |
75 } | 81 } |
76 return left; | 82 return left; |
77 } | 83 } |
78 | 84 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 | 140 |
135 Expression _parsePrimary() { | 141 Expression _parsePrimary() { |
136 var kind = _token.kind; | 142 var kind = _token.kind; |
137 switch (kind) { | 143 switch (kind) { |
138 case KEYWORD_TOKEN: | 144 case KEYWORD_TOKEN: |
139 var keyword = _token.value; | 145 var keyword = _token.value; |
140 if (keyword == 'this') { | 146 if (keyword == 'this') { |
141 _advance(); | 147 _advance(); |
142 // TODO(justin): return keyword node | 148 // TODO(justin): return keyword node |
143 return _astFactory.identifier('this'); | 149 return _astFactory.identifier('this'); |
144 } else if (keyword == 'in') { | 150 } else if (KEYWORDS.contains(keyword)) { |
145 return null; | 151 throw new ParseException('invalid keyword: $keyword'); |
146 } | 152 } |
147 throw new ArgumentError('unrecognized keyword: $keyword'); | 153 throw new ArgumentError('unrecognized keyword: $keyword'); |
148 case IDENTIFIER_TOKEN: | 154 case IDENTIFIER_TOKEN: |
149 return _parseInvokeOrIdentifier(); | 155 return _parseInvokeOrIdentifier(); |
150 case STRING_TOKEN: | 156 case STRING_TOKEN: |
151 return _parseString(); | 157 return _parseString(); |
152 case INTEGER_TOKEN: | 158 case INTEGER_TOKEN: |
153 return _parseInteger(); | 159 return _parseInteger(); |
154 case DECIMAL_TOKEN: | 160 case DECIMAL_TOKEN: |
155 return _parseDecimal(); | 161 return _parseDecimal(); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 return new MapLiteral(entries); | 203 return new MapLiteral(entries); |
198 } | 204 } |
199 | 205 |
200 MapLiteralEntry _parseMapLiteralEntry() { | 206 MapLiteralEntry _parseMapLiteralEntry() { |
201 var key = _parseString(); | 207 var key = _parseString(); |
202 _advance(COLON_TOKEN, ':'); | 208 _advance(COLON_TOKEN, ':'); |
203 var value = _parseExpression(); | 209 var value = _parseExpression(); |
204 return _astFactory.mapLiteralEntry(key, value); | 210 return _astFactory.mapLiteralEntry(key, value); |
205 } | 211 } |
206 | 212 |
207 InExpression _parseComprehension(Expression left) { | 213 InExpression _parseInExpression(Expression left) { |
208 assert(_token.value == 'in'); | 214 assert(_token.value == 'in'); |
209 if (left is! Identifier) { | 215 if (left is! Identifier) { |
210 throw new ParseException( | 216 throw new ParseException( |
211 "in... statements must start with an identifier"); | 217 "in... statements must start with an identifier"); |
212 } | 218 } |
213 _advance(); | 219 _advance(); |
214 var right = _parseExpression(); | 220 var right = _parseExpression(); |
215 return _astFactory.inExpr(left, right); | 221 return _astFactory.inExpr(left, right); |
216 } | 222 } |
217 | 223 |
| 224 AsExpression _parseAsExpression(Expression left) { |
| 225 assert(_token.value == 'as'); |
| 226 _advance(); |
| 227 var right = _parseExpression(); |
| 228 if (right is! Identifier) { |
| 229 throw new ParseException( |
| 230 "as... statements must end with an identifier"); |
| 231 } |
| 232 return _astFactory.asExpr(left, right); |
| 233 } |
| 234 |
218 Expression _parseInvokeOrIdentifier() { | 235 Expression _parseInvokeOrIdentifier() { |
219 if (_token.value == 'true') { | 236 if (_token.value == 'true') { |
220 _advance(); | 237 _advance(); |
221 return _astFactory.literal(true); | 238 return _astFactory.literal(true); |
222 } | 239 } |
223 if (_token.value == 'false') { | 240 if (_token.value == 'false') { |
224 _advance(); | 241 _advance(); |
225 return _astFactory.literal(false); | 242 return _astFactory.literal(false); |
226 } | 243 } |
227 if (_token.value == 'null') { | 244 if (_token.value == 'null') { |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
292 return value; | 309 return value; |
293 } | 310 } |
294 | 311 |
295 Literal<double> _parseDecimal([String prefix = '']) { | 312 Literal<double> _parseDecimal([String prefix = '']) { |
296 var value = _astFactory.literal(double.parse('$prefix${_token.value}')); | 313 var value = _astFactory.literal(double.parse('$prefix${_token.value}')); |
297 _advance(); | 314 _advance(); |
298 return value; | 315 return value; |
299 } | 316 } |
300 | 317 |
301 } | 318 } |
OLD | NEW |