| 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 12 matching lines...) Expand all Loading... |
| 23 _astFactory = (astFactory == null) ? new AstFactory() : astFactory; | 23 _astFactory = (astFactory == null) ? new AstFactory() : astFactory; |
| 24 | 24 |
| 25 Expression parse() { | 25 Expression parse() { |
| 26 _tokens = _tokenizer.tokenize(); | 26 _tokens = _tokenizer.tokenize(); |
| 27 _iterator = _tokens.iterator; | 27 _iterator = _tokens.iterator; |
| 28 _advance(); | 28 _advance(); |
| 29 return _parseExpression(); | 29 return _parseExpression(); |
| 30 } | 30 } |
| 31 | 31 |
| 32 _advance([int kind, String value]) { | 32 _advance([int kind, String value]) { |
| 33 if ((kind != null && _token.kind != kind) | 33 if ((kind != null && (_token == null || _token.kind != kind)) |
| 34 || (value != null && _token.value != value)) { | 34 || (value != null && (_token == null || _token.value != value))) { |
| 35 throw new ParseException("Expected $value: $_token"); | 35 throw new ParseException("Expected kind $kind ($value): $_token"); |
| 36 } | 36 } |
| 37 _iterator.moveNext(); | 37 _iterator.moveNext(); |
| 38 } | 38 } |
| 39 | 39 |
| 40 Expression _parseExpression() { | 40 Expression _parseExpression() { |
| 41 if (_token == null) return _astFactory.empty(); | 41 if (_token == null) return _astFactory.empty(); |
| 42 var expr = _parseUnary(); | 42 var expr = _parseUnary(); |
| 43 return (expr == null) ? null : _parsePrecedence(expr, 0); | 43 return (expr == null) ? null : _parsePrecedence(expr, 0); |
| 44 } | 44 } |
| 45 | 45 |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 return _parseString(); | 153 return _parseString(); |
| 154 case INTEGER_TOKEN: | 154 case INTEGER_TOKEN: |
| 155 return _parseInteger(); | 155 return _parseInteger(); |
| 156 case DECIMAL_TOKEN: | 156 case DECIMAL_TOKEN: |
| 157 return _parseDecimal(); | 157 return _parseDecimal(); |
| 158 case GROUPER_TOKEN: | 158 case GROUPER_TOKEN: |
| 159 if (_token.value == '(') { | 159 if (_token.value == '(') { |
| 160 return _parseParenthesized(); | 160 return _parseParenthesized(); |
| 161 } else if (_token.value == '{') { | 161 } else if (_token.value == '{') { |
| 162 return _parseMapLiteral(); | 162 return _parseMapLiteral(); |
| 163 } else if (_token.value == '[') { |
| 164 return _parseListLiteral(); |
| 163 } | 165 } |
| 164 return null; | 166 return null; |
| 165 case COLON_TOKEN: | 167 case COLON_TOKEN: |
| 166 // TODO(justinfagnani): We need better errors throughout the parser, and | 168 // TODO(justinfagnani): We need better errors throughout the parser, and |
| 167 // we should be throwing ParseErrors to be caught by the caller | 169 // we should be throwing ParseErrors to be caught by the caller |
| 168 throw new ArgumentError('unexpected token ":"'); | 170 throw new ArgumentError('unexpected token ":"'); |
| 169 default: | 171 default: |
| 170 return null; | 172 return null; |
| 171 } | 173 } |
| 172 } | 174 } |
| 173 | 175 |
| 176 ListLiteral _parseListLiteral() { |
| 177 var items = []; |
| 178 do { |
| 179 _advance(); |
| 180 if (_token.kind == GROUPER_TOKEN && _token.value == ']') { |
| 181 break; |
| 182 } |
| 183 items.add(_parseExpression()); |
| 184 } while(_token != null && _token.value == ','); |
| 185 _advance(GROUPER_TOKEN, ']'); |
| 186 return new ListLiteral(items); |
| 187 } |
| 188 |
| 174 MapLiteral _parseMapLiteral() { | 189 MapLiteral _parseMapLiteral() { |
| 175 var entries = []; | 190 var entries = []; |
| 176 do { | 191 do { |
| 177 _advance(); | 192 _advance(); |
| 178 if (_token.kind == GROUPER_TOKEN && _token.value == '}') { | 193 if (_token.kind == GROUPER_TOKEN && _token.value == '}') { |
| 179 break; | 194 break; |
| 180 } | 195 } |
| 181 entries.add(_parseMapLiteralEntry()); | 196 entries.add(_parseMapLiteralEntry()); |
| 182 } while(_token != null && _token.value == ','); | 197 } while(_token != null && _token.value == ','); |
| 183 _advance(GROUPER_TOKEN, '}'); | 198 _advance(GROUPER_TOKEN, '}'); |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 279 return value; | 294 return value; |
| 280 } | 295 } |
| 281 | 296 |
| 282 Literal<double> _parseDecimal([String prefix = '']) { | 297 Literal<double> _parseDecimal([String prefix = '']) { |
| 283 var value = _astFactory.literal(double.parse('$prefix${_token.value}')); | 298 var value = _astFactory.literal(double.parse('$prefix${_token.value}')); |
| 284 _advance(); | 299 _advance(); |
| 285 return value; | 300 return value; |
| 286 } | 301 } |
| 287 | 302 |
| 288 } | 303 } |
| OLD | NEW |