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 package com.google.dart.compiler.parser; | 5 package com.google.dart.compiler.parser; |
6 | 6 |
7 import com.google.common.annotations.VisibleForTesting; | 7 import com.google.common.annotations.VisibleForTesting; |
8 import com.google.common.io.CharStreams; | 8 import com.google.common.io.CharStreams; |
9 import com.google.dart.compiler.DartCompilationError; | 9 import com.google.dart.compiler.DartCompilationError; |
10 import com.google.dart.compiler.DartCompilerListener; | 10 import com.google.dart.compiler.DartCompilerListener; |
(...skipping 1005 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1016 DartFieldDefinition fieldDefinition = | 1016 DartFieldDefinition fieldDefinition = |
1017 new DartFieldDefinition(null, Lists.<DartField>create(field)); | 1017 new DartFieldDefinition(null, Lists.<DartField>create(field)); |
1018 fieldDefinition.setSourceInfo(field); | 1018 fieldDefinition.setSourceInfo(field); |
1019 return fieldDefinition; | 1019 return fieldDefinition; |
1020 } | 1020 } |
1021 return method; | 1021 return method; |
1022 } | 1022 } |
1023 | 1023 |
1024 /** | 1024 /** |
1025 * <pre> | 1025 * <pre> |
1026 * initializers | |
1027 * : ':' superCallOrFirstFieldInitializer (',' fieldInitializer)* | |
1028 * | THIS ('.' identifier) formalParameterList | |
1029 * ; | |
1030 * | |
1026 * fieldInitializer | 1031 * fieldInitializer |
1027 * : (THIS '.')? identifier '=' conditionalExpression | 1032 * : (THIS '.')? identifier '=' conditionalExpression |
1028 * | THIS ('.' identifier)? arguments | 1033 * ; |
1029 * ; | 1034 * |
1035 * superCallOrFirstFieldInitializer | |
1036 * : SUPER arguments | SUPER '.' identifier arguments | |
1037 * | fieldInitializer | |
1038 * ; | |
1039 * | |
1040 * fieldInitializer | |
1041 * : (THIS '.')? identifier '=' conditionalExpression | |
1042 * | THIS ('.' identifier)? arguments | |
1043 * ; | |
1030 * </pre> | 1044 * </pre> |
1031 * | |
1032 * @return true if initializer is a redirected constructor, false otherwise. | 1045 * @return true if initializer is a redirected constructor, false otherwise. |
1033 */ | 1046 */ |
1034 private boolean parseFieldInitializersOrRedirectedConstructor(List<DartInitial izer> inits) { | 1047 private boolean parseInitializers(List<DartInitializer> initializers) { |
1048 expect(Token.COLON); | |
1035 do { | 1049 do { |
1036 beginFieldInitializerOrRedirectedConstructor(); | 1050 beginInitializer(); |
1037 boolean hasThisPrefix = optional(Token.THIS); | 1051 if (match(Token.SUPER)) { |
1038 if (hasThisPrefix) { | 1052 beginSuperInitializer(); |
1039 if (match(Token.LPAREN)) { | 1053 expect(Token.SUPER); |
1040 return parseRedirectedConstructorInvocation(null, inits); | 1054 DartIdentifier constructor = null; |
1055 if (optional(Token.PERIOD)) { | |
1056 constructor = parseIdentifier(); | |
1041 } | 1057 } |
1042 expect(Token.PERIOD); | 1058 DartSuperConstructorInvocation superInvocation = |
1043 } | 1059 new DartSuperConstructorInvocation(constructor, parseArguments()); |
1044 DartIdentifier name = parseIdentifier(); | 1060 initializers.add(done(new DartInitializer(null, done(superInvocation)))) ; |
1045 if (hasThisPrefix && match(Token.LPAREN)) { | |
1046 return parseRedirectedConstructorInvocation(name, inits); | |
1047 } else { | 1061 } else { |
1048 expect(Token.ASSIGN); | 1062 boolean hasThisPrefix = optional(Token.THIS); |
1049 boolean save = setAllowFunctionExpression(false); | 1063 if (hasThisPrefix) { |
1050 DartExpression initExpr = parseExpression(); | 1064 if (match(Token.LPAREN)) { |
1051 setAllowFunctionExpression(save); | 1065 return parseRedirectedConstructorInvocation(null, initializers); |
mmendez
2011/11/08 15:24:06
Nit: this follows the existing pattern, but it wou
fabiomfv
2011/11/08 15:34:55
+1. we need proper error recovery.
| |
1052 inits.add(done(new DartInitializer(name, initExpr))); | 1066 } |
1067 expect(Token.PERIOD); | |
1068 } | |
1069 DartIdentifier name = parseIdentifier(); | |
1070 if (hasThisPrefix && match(Token.LPAREN)) { | |
1071 return parseRedirectedConstructorInvocation(name, initializers); | |
1072 } else { | |
1073 expect(Token.ASSIGN); | |
1074 boolean save = setAllowFunctionExpression(false); | |
1075 DartExpression initExpr = parseExpression(); | |
1076 setAllowFunctionExpression(save); | |
1077 initializers.add(done(new DartInitializer(name, initExpr))); | |
1078 } | |
1053 } | 1079 } |
1054 } while (optional(Token.COMMA)); | 1080 } while (optional(Token.COMMA)); |
1055 return false; | 1081 return false; |
1056 } | 1082 } |
1057 | 1083 |
1058 private boolean parseRedirectedConstructorInvocation(DartIdentifier name, | 1084 private boolean parseRedirectedConstructorInvocation(DartIdentifier name, |
1059 List<DartInitializer> ini ts) { | 1085 List<DartInitializer> ini tializers) { |
1060 if (inits.isEmpty()) { | 1086 if (initializers.isEmpty()) { |
1061 DartInvocation call = | 1087 DartRedirectConstructorInvocation redirConstructor = |
1062 doneWithoutConsuming(new DartRedirectConstructorInvocation(name, parseAr guments())); | 1088 new DartRedirectConstructorInvocation(name, parseArguments()); |
1063 inits.add(done(new DartInitializer(null, call))); | 1089 initializers.add(done(new DartInitializer(null, doneWithoutConsuming(redir Constructor)))); |
1064 return true; | 1090 return true; |
1065 } else { | 1091 } else { |
1066 reportUnexpectedToken(position(), Token.ASSIGN, Token.LPAREN); | 1092 reportUnexpectedToken(position(), Token.ASSIGN, Token.LPAREN); |
1067 } | 1093 } |
1068 return false; | 1094 return false; |
1069 } | 1095 } |
1070 | 1096 |
1071 /** | 1097 /** |
1072 * <pre> | 1098 * <pre> |
1073 * initializers : ':' superCallOrFirstFieldInitializer (',' fieldInitializer)* | |
1074 * | THIS ('.' identifier) formalParameterList ; | |
1075 * | |
1076 * fieldInitializer : (THIS '.')? identifier '=' conditionalExpression ; | |
1077 * | |
1078 * superCallOrFirstFieldInitializer : SUPER arguments | SUPER '.' identifier | |
1079 * arguments | fieldInitializer ; | |
1080 * <pre> | |
1081 * | |
1082 * @return true if initializer is a redirect constructor, false otherwise. | |
1083 */ | |
1084 private boolean parseInitializers(List<DartInitializer> initializers) { | |
1085 expect(Token.COLON); | |
1086 boolean callSuper = false; | |
1087 if (match(Token.SUPER)) { | |
1088 beginInitializer(); | |
1089 beginSuperInitializer(); | |
1090 expect(Token.SUPER); | |
1091 callSuper = true; | |
1092 DartIdentifier constructor = null; | |
1093 if (optional(Token.PERIOD)) { | |
1094 // Calling a super named constructor. | |
1095 constructor = parseIdentifier(); | |
1096 } | |
1097 DartSuperConstructorInvocation call = | |
1098 done(new DartSuperConstructorInvocation(constructor, parseArguments()) ); | |
1099 initializers.add(done(new DartInitializer(null, call))); | |
1100 } | |
1101 if (!callSuper || optional(Token.COMMA)) { | |
1102 return parseFieldInitializersOrRedirectedConstructor(initializers); | |
1103 } | |
1104 return false; | |
1105 } | |
1106 | |
1107 /** | |
1108 * <pre> | |
1109 * variableDeclaration | 1099 * variableDeclaration |
1110 * : constVarOrType identifierList | 1100 * : constVarOrType identifierList |
1111 * ; | 1101 * ; |
1112 * identifierList | 1102 * identifierList |
1113 * : identifier (',' identifier)* | 1103 * : identifier (',' identifier)* |
1114 * ; | 1104 * ; |
1115 * | 1105 * |
1116 * staticConstDeclarationList | 1106 * staticConstDeclarationList |
1117 * : staticConstDeclaration (',' staticConstDeclaration)* | 1107 * : staticConstDeclaration (',' staticConstDeclaration)* |
1118 * ; | 1108 * ; |
(...skipping 2358 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3477 } else { | 3467 } else { |
3478 ctx.error(dartError); | 3468 ctx.error(dartError); |
3479 errorHistory.add(dartError.hashCode()); | 3469 errorHistory.add(dartError.hashCode()); |
3480 } | 3470 } |
3481 } | 3471 } |
3482 | 3472 |
3483 private void reportError(DartNode node, ErrorCode errorCode, Object... argumen ts) { | 3473 private void reportError(DartNode node, ErrorCode errorCode, Object... argumen ts) { |
3484 reportError(new DartCompilationError(node, errorCode, arguments)); | 3474 reportError(new DartCompilationError(node, errorCode, arguments)); |
3485 } | 3475 } |
3486 } | 3476 } |
OLD | NEW |