| 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 /** | 5 /** |
| 6 * An event generating parser of Dart programs. This parser expects | 6 * An event generating parser of Dart programs. This parser expects |
| 7 * all tokens in a linked list. | 7 * all tokens in a linked list. |
| 8 */ | 8 */ |
| 9 class Parser { | 9 class Parser { |
| 10 final Listener listener; | 10 final Listener listener; |
| (...skipping 1028 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1039 } | 1039 } |
| 1040 token = parseExpression(token.next); | 1040 token = parseExpression(token.next); |
| 1041 if (colon !== null) listener.handleNamedArgument(colon); | 1041 if (colon !== null) listener.handleNamedArgument(colon); |
| 1042 ++argumentCount; | 1042 ++argumentCount; |
| 1043 } while (optional(',', token)); | 1043 } while (optional(',', token)); |
| 1044 listener.endArguments(argumentCount, begin, token); | 1044 listener.endArguments(argumentCount, begin, token); |
| 1045 return expect(')', token); | 1045 return expect(')', token); |
| 1046 } | 1046 } |
| 1047 | 1047 |
| 1048 Token parseVariablesDeclaration(Token token) { | 1048 Token parseVariablesDeclaration(Token token) { |
| 1049 token = parseVariablesDeclarationNoSemicolon(token); |
| 1050 return expectSemicolon(token); |
| 1051 } |
| 1052 |
| 1053 Token parseVariablesDeclarationNoSemicolon(Token token) { |
| 1049 int count = 1; | 1054 int count = 1; |
| 1050 listener.beginVariablesDeclaration(token); | 1055 listener.beginVariablesDeclaration(token); |
| 1051 token = parseFinalVarOrType(token); | 1056 token = parseFinalVarOrType(token); |
| 1052 token = parseOptionallyInitializedIdentifier(token); | 1057 token = parseOptionallyInitializedIdentifier(token); |
| 1053 while (optional(',', token)) { | 1058 while (optional(',', token)) { |
| 1054 token = parseOptionallyInitializedIdentifier(token.next); | 1059 token = parseOptionallyInitializedIdentifier(token.next); |
| 1055 ++count; | 1060 ++count; |
| 1056 } | 1061 } |
| 1057 listener.endVariablesDeclaration(count, token); | 1062 listener.endVariablesDeclaration(count, token); |
| 1058 return expectSemicolon(token); | 1063 return token; |
| 1059 } | 1064 } |
| 1060 | 1065 |
| 1061 Token parseOptionallyInitializedIdentifier(Token token) { | 1066 Token parseOptionallyInitializedIdentifier(Token token) { |
| 1062 listener.beginInitializedIdentifier(token); | 1067 listener.beginInitializedIdentifier(token); |
| 1063 token = parseIdentifier(token); | 1068 token = parseIdentifier(token); |
| 1064 if (optional('=', token)) { | 1069 if (optional('=', token)) { |
| 1065 Token assignment = token; | 1070 Token assignment = token; |
| 1066 listener.beginInitializer(token); | 1071 listener.beginInitializer(token); |
| 1067 token = parseExpression(token.next); | 1072 token = parseExpression(token.next); |
| 1068 listener.endInitializer(assignment); | 1073 listener.endInitializer(assignment); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 1099 Token elseToken = null; | 1104 Token elseToken = null; |
| 1100 if (optional('else', token)) { | 1105 if (optional('else', token)) { |
| 1101 elseToken = token; | 1106 elseToken = token; |
| 1102 token = parseStatement(token.next); | 1107 token = parseStatement(token.next); |
| 1103 } | 1108 } |
| 1104 listener.endIfStatement(ifToken, elseToken); | 1109 listener.endIfStatement(ifToken, elseToken); |
| 1105 return token; | 1110 return token; |
| 1106 } | 1111 } |
| 1107 | 1112 |
| 1108 Token parseForStatement(Token token) { | 1113 Token parseForStatement(Token token) { |
| 1109 // TODO(ahe): Support for-in. | |
| 1110 Token forToken = token; | 1114 Token forToken = token; |
| 1111 listener.beginForStatement(forToken); | 1115 listener.beginForStatement(forToken); |
| 1112 token = expect('for', token); | 1116 token = expect('for', token); |
| 1113 token = expect('(', token); | 1117 token = expect('(', token); |
| 1114 token = parseVariablesDeclaration(token); // TODO(ahe): Support other forms. | 1118 token = parseVariablesDeclarationOrExpressionOpt(token); |
| 1115 token = parseExpressionStatement(token); | 1119 if (optional('in', token)) { |
| 1116 token = parseExpression(token); // TODO(ahe): Support expression list here. | 1120 return parseForInRest(forToken, token); |
| 1121 } else { |
| 1122 return parseForRest(forToken, token); |
| 1123 } |
| 1124 } |
| 1125 |
| 1126 Token parseVariablesDeclarationOrExpressionOpt(Token token) { |
| 1127 final String value = token.stringValue; |
| 1128 if (value === ';') { |
| 1129 listener.handleNoExpression(token); |
| 1130 return token; |
| 1131 } else if ((value === 'var') || (value === 'final')) { |
| 1132 return parseVariablesDeclarationNoSemicolon(token); |
| 1133 } |
| 1134 Token identifier = peekIdentifierAfterType(token); |
| 1135 if (identifier !== null) { |
| 1136 assert(identifier.kind === IDENTIFIER_TOKEN); |
| 1137 Token afterId = identifier.next; |
| 1138 int afterIdKind = afterId.kind; |
| 1139 if (afterIdKind === EQ_TOKEN || afterIdKind === SEMICOLON_TOKEN || |
| 1140 afterIdKind === COMMA_TOKEN || optional('in', afterId)) { |
| 1141 return parseVariablesDeclarationNoSemicolon(token); |
| 1142 } |
| 1143 } |
| 1144 return parseExpression(token); |
| 1145 } |
| 1146 |
| 1147 Token parseForRest(Token forToken, Token token) { |
| 1148 token = expectSemicolon(token); |
| 1149 if (optional(';', token)) { |
| 1150 token = parseEmptyStatement(token); |
| 1151 } else { |
| 1152 token = parseExpressionStatement(token); |
| 1153 } |
| 1154 int expressionCount = 0; |
| 1155 if (optional(')', token)) { |
| 1156 listener.endForStatement(expressionCount, forToken, token.next); |
| 1157 return token.next; |
| 1158 } |
| 1159 token = parseExpression(token); |
| 1160 ++expressionCount; |
| 1161 while (optional(',', token)) { |
| 1162 token = parseExpression(token.next); |
| 1163 ++expressionCount; |
| 1164 } |
| 1117 token = expect(')', token); | 1165 token = expect(')', token); |
| 1118 token = parseStatement(token); | 1166 token = parseStatement(token); |
| 1119 listener.endForStatement(forToken, token); | 1167 listener.endForStatement(expressionCount, forToken, token); |
| 1120 return token; | 1168 return token; |
| 1121 } | 1169 } |
| 1122 | 1170 |
| 1171 Token parseForInRest(Token forToken, Token token) { |
| 1172 assert(optional('in', token)); |
| 1173 Token inKeyword = token; |
| 1174 token = parseExpression(token.next); |
| 1175 token = expect(')', token); |
| 1176 token = parseStatement(token); |
| 1177 listener.endForInStatement(forToken, inKeyword, token); |
| 1178 return token; |
| 1179 } |
| 1180 |
| 1123 Token parseWhileStatement(Token token) { | 1181 Token parseWhileStatement(Token token) { |
| 1124 Token whileToken = token; | 1182 Token whileToken = token; |
| 1125 listener.beginWhileStatement(whileToken); | 1183 listener.beginWhileStatement(whileToken); |
| 1126 token = expect('while', token); | 1184 token = expect('while', token); |
| 1127 token = parseParenthesizedExpression(token); | 1185 token = parseParenthesizedExpression(token); |
| 1128 token = parseStatement(token); | 1186 token = parseStatement(token); |
| 1129 listener.endWhileStatement(whileToken, token); | 1187 listener.endWhileStatement(whileToken, token); |
| 1130 return token; | 1188 return token; |
| 1131 } | 1189 } |
| 1132 | 1190 |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1232 } | 1290 } |
| 1233 listener.handleContinueStatement(hasTarget, continueKeyword, token); | 1291 listener.handleContinueStatement(hasTarget, continueKeyword, token); |
| 1234 return expectSemicolon(token); | 1292 return expectSemicolon(token); |
| 1235 } | 1293 } |
| 1236 | 1294 |
| 1237 Token parseEmptyStatement(Token token) { | 1295 Token parseEmptyStatement(Token token) { |
| 1238 listener.handleEmptyStatement(token); | 1296 listener.handleEmptyStatement(token); |
| 1239 return expectSemicolon(token); | 1297 return expectSemicolon(token); |
| 1240 } | 1298 } |
| 1241 } | 1299 } |
| OLD | NEW |