| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 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 | 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 // TODO(jimhug): Error recovery needs major work! | 5 // TODO(jimhug): Error recovery needs major work! |
| 6 /** | 6 /** |
| 7 * A simple recursive descent parser for the dart language. | 7 * A simple recursive descent parser for the dart language. |
| 8 * | 8 * |
| 9 * This parser is designed to be more permissive than the official | 9 * This parser is designed to be more permissive than the official |
| 10 * Dart grammar. It is expected that many grammar errors would be | 10 * Dart grammar. It is expected that many grammar errors would be |
| (...skipping 1097 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1108 case TokenKind.INTEGER: | 1108 case TokenKind.INTEGER: |
| 1109 var t = _next(); | 1109 var t = _next(); |
| 1110 return _makeLiteral(Value.fromInt(Math.parseInt(t.text), t.span)); | 1110 return _makeLiteral(Value.fromInt(Math.parseInt(t.text), t.span)); |
| 1111 | 1111 |
| 1112 case TokenKind.DOUBLE: | 1112 case TokenKind.DOUBLE: |
| 1113 var t = _next(); | 1113 var t = _next(); |
| 1114 return _makeLiteral( | 1114 return _makeLiteral( |
| 1115 Value.fromDouble(Math.parseDouble(t.text), t.span)); | 1115 Value.fromDouble(Math.parseDouble(t.text), t.span)); |
| 1116 | 1116 |
| 1117 case TokenKind.STRING: | 1117 case TokenKind.STRING: |
| 1118 var t = _next(); | |
| 1119 return _makeLiteral(Value.fromString(t.value, t.span)); | |
| 1120 | |
| 1121 case TokenKind.STRING_PART: | 1118 case TokenKind.STRING_PART: |
| 1122 return stringInterpolation(); | 1119 return adjacentStrings(); |
| 1123 | 1120 |
| 1124 case TokenKind.LT: | 1121 case TokenKind.LT: |
| 1125 return finishTypedLiteral(start, false); | 1122 return finishTypedLiteral(start, false); |
| 1126 | 1123 |
| 1127 case TokenKind.VOID: | 1124 case TokenKind.VOID: |
| 1128 case TokenKind.VAR: | 1125 case TokenKind.VAR: |
| 1129 case TokenKind.FINAL: | 1126 case TokenKind.FINAL: |
| 1130 return declaredIdentifier(false); | 1127 return declaredIdentifier(false); |
| 1131 | 1128 |
| 1132 default: | 1129 default: |
| 1133 if (!_peekIdentifier()) { | 1130 if (!_peekIdentifier()) { |
| 1134 // TODO(jimhug): Better error message. | 1131 // TODO(jimhug): Better error message. |
| 1135 _errorExpected('expression'); | 1132 _errorExpected('expression'); |
| 1136 } | 1133 } |
| 1137 return new VarExpression(identifier(), _makeSpan(start)); | 1134 return new VarExpression(identifier(), _makeSpan(start)); |
| 1138 } | 1135 } |
| 1139 } | 1136 } |
| 1140 | 1137 |
| 1138 adjacentStrings() { |
| 1139 int start = _peekToken.start; |
| 1140 List<Expression> strings = []; |
| 1141 while (_peek() == TokenKind.STRING || _peek() == TokenKind.STRING_PART) { |
| 1142 Expression part = null; |
| 1143 if (_peek() == TokenKind.STRING) { |
| 1144 var t = _next(); |
| 1145 part = _makeLiteral(Value.fromString(t.value, t.span)); |
| 1146 } else { |
| 1147 part = stringInterpolation(); |
| 1148 } |
| 1149 strings.add(part); |
| 1150 } |
| 1151 if (strings.length == 1) { |
| 1152 return strings[0]; |
| 1153 } else { |
| 1154 assert(!strings.isEmpty()); |
| 1155 return new StringConcatExpression(strings, _makeSpan(start)); |
| 1156 } |
| 1157 } |
| 1158 |
| 1141 stringInterpolation() { | 1159 stringInterpolation() { |
| 1142 int start = _peekToken.start; | 1160 int start = _peekToken.start; |
| 1143 var pieces = new List<Expression>(); | 1161 var pieces = new List<Expression>(); |
| 1144 var startQuote = null, endQuote = null; | 1162 var startQuote = null, endQuote = null; |
| 1145 while(_peekKind(TokenKind.STRING_PART)) { | 1163 while(_peekKind(TokenKind.STRING_PART)) { |
| 1146 var token = _next(); | 1164 var token = _next(); |
| 1147 pieces.add(_makeLiteral(Value.fromString(token.value, token.span))); | 1165 pieces.add(_makeLiteral(Value.fromString(token.value, token.span))); |
| 1148 if (_maybeEat(TokenKind.LBRACE)) { | 1166 if (_maybeEat(TokenKind.LBRACE)) { |
| 1149 pieces.add(expression()); | 1167 pieces.add(expression()); |
| 1150 _eat(TokenKind.RBRACE); | 1168 _eat(TokenKind.RBRACE); |
| (...skipping 570 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1721 int _pos = 0; | 1739 int _pos = 0; |
| 1722 next() { | 1740 next() { |
| 1723 var token = tokens[_pos]; | 1741 var token = tokens[_pos]; |
| 1724 ++_pos; | 1742 ++_pos; |
| 1725 if (_pos == tokens.length) { | 1743 if (_pos == tokens.length) { |
| 1726 parser.tokenizer = previousTokenizer; | 1744 parser.tokenizer = previousTokenizer; |
| 1727 } | 1745 } |
| 1728 return token; | 1746 return token; |
| 1729 } | 1747 } |
| 1730 } | 1748 } |
| OLD | NEW |