OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 fasta.body_builder; | 5 library fasta.body_builder; |
6 | 6 |
7 import '../parser/parser.dart' show FormalParameterType, optional; | 7 import '../parser/parser.dart' show FormalParameterType, optional; |
8 | 8 |
9 import '../parser/error_kind.dart' show ErrorKind; | 9 import '../parser/error_kind.dart' show ErrorKind; |
10 | 10 |
11 import '../parser/identifier_context.dart' show IdentifierContext; | 11 import '../parser/identifier_context.dart' show IdentifierContext; |
12 | 12 |
13 import 'package:kernel/ast.dart'; | 13 import 'package:kernel/ast.dart'; |
14 | 14 |
15 import 'package:kernel/clone.dart' show CloneVisitor; | 15 import 'package:kernel/clone.dart' show CloneVisitor; |
16 | 16 |
17 import 'package:kernel/transformations/flags.dart' show TransformerFlag; | 17 import 'package:kernel/transformations/flags.dart' show TransformerFlag; |
18 | 18 |
19 import 'package:kernel/class_hierarchy.dart' show ClassHierarchy; | 19 import 'package:kernel/class_hierarchy.dart' show ClassHierarchy; |
20 | 20 |
21 import 'package:kernel/core_types.dart' show CoreTypes; | 21 import 'package:kernel/core_types.dart' show CoreTypes; |
22 | 22 |
23 import '../parser/dart_vm_native.dart' show skipNativeClause; | 23 import '../parser/dart_vm_native.dart' show skipNativeClause; |
24 | 24 |
25 import '../scanner/token.dart' | 25 import '../scanner/token.dart' |
26 show BeginGroupToken, Token, isBinaryOperator, isMinusOperator; | 26 show BeginGroupToken, Token, isBinaryOperator, isMinusOperator; |
27 | 27 |
28 import '../errors.dart' show internalError, printUnexpected; | 28 import '../errors.dart' show formatUnexpected, internalError; |
29 | 29 |
30 import '../source/scope_listener.dart' | 30 import '../source/scope_listener.dart' |
31 show JumpTargetKind, NullValue, ScopeListener; | 31 show JumpTargetKind, NullValue, ScopeListener; |
32 | 32 |
33 import '../builder/scope.dart' show AccessErrorBuilder, AmbiguousBuilder, Scope; | 33 import '../builder/scope.dart' show AccessErrorBuilder, AmbiguousBuilder, Scope; |
34 | 34 |
35 import '../source/outline_builder.dart' show asyncMarkerFromTokens; | 35 import '../source/outline_builder.dart' show asyncMarkerFromTokens; |
36 | 36 |
37 import 'builder_accessors.dart'; | 37 import 'builder_accessors.dart'; |
38 | 38 |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 | 105 |
106 final bool isDartLibrary; | 106 final bool isDartLibrary; |
107 | 107 |
108 @override | 108 @override |
109 final Uri uri; | 109 final Uri uri; |
110 | 110 |
111 Scope formalParameterScope; | 111 Scope formalParameterScope; |
112 | 112 |
113 bool isFirstIdentifier = false; | 113 bool isFirstIdentifier = false; |
114 | 114 |
115 bool hasParserError = false; | |
116 | |
117 bool inInitializer = false; | 115 bool inInitializer = false; |
118 | 116 |
119 bool inCatchClause = false; | 117 bool inCatchClause = false; |
120 | 118 |
121 int functionNestingLevel = 0; | 119 int functionNestingLevel = 0; |
122 | 120 |
123 Statement compileTimeErrorInTry; | 121 Statement compileTimeErrorInTry; |
124 | 122 |
125 Statement compileTimeErrorInLoopOrSwitch; | 123 Statement compileTimeErrorInLoopOrSwitch; |
126 | 124 |
127 Scope switchScope; | 125 Scope switchScope; |
128 | 126 |
129 CloneVisitor cloner; | 127 CloneVisitor cloner; |
130 | 128 |
131 BodyBuilder( | 129 BodyBuilder( |
132 KernelLibraryBuilder library, | 130 KernelLibraryBuilder library, |
133 this.member, | 131 this.member, |
134 Scope scope, | 132 Scope scope, |
135 this.formalParameterScope, | 133 this.formalParameterScope, |
136 this.hierarchy, | 134 this.hierarchy, |
137 this.coreTypes, | 135 this.coreTypes, |
138 this.classBuilder, | 136 this.classBuilder, |
139 this.isInstanceMember, | 137 this.isInstanceMember, |
140 this.uri) | 138 this.uri) |
141 : enclosingScope = scope, | 139 : enclosingScope = scope, |
142 library = library, | 140 library = library, |
143 isDartLibrary = library.uri.scheme == "dart", | 141 isDartLibrary = library.uri.scheme == "dart", |
144 super(scope); | 142 super(scope); |
145 | 143 |
| 144 bool get hasParserError => recoverableErrors.isNotEmpty; |
| 145 |
146 bool get inConstructor { | 146 bool get inConstructor { |
147 return functionNestingLevel == 0 && member is KernelConstructorBuilder; | 147 return functionNestingLevel == 0 && member is KernelConstructorBuilder; |
148 } | 148 } |
149 | 149 |
150 bool get isInstanceContext { | 150 bool get isInstanceContext { |
151 return isInstanceMember || member is KernelConstructorBuilder; | 151 return isInstanceMember || member is KernelConstructorBuilder; |
152 } | 152 } |
153 | 153 |
154 @override | 154 @override |
155 void push(Object node) { | 155 void push(Object node) { |
(...skipping 2041 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2197 | 2197 |
2198 @override | 2198 @override |
2199 void handleModifiers(int count) { | 2199 void handleModifiers(int count) { |
2200 debugEvent("Modifiers"); | 2200 debugEvent("Modifiers"); |
2201 // TODO(ahe): Copied from outline_builder.dart. | 2201 // TODO(ahe): Copied from outline_builder.dart. |
2202 push(popList(count) ?? NullValue.Modifiers); | 2202 push(popList(count) ?? NullValue.Modifiers); |
2203 } | 2203 } |
2204 | 2204 |
2205 @override | 2205 @override |
2206 void handleRecoverableError(Token token, ErrorKind kind, Map arguments) { | 2206 void handleRecoverableError(Token token, ErrorKind kind, Map arguments) { |
| 2207 bool silent = hasParserError; |
2207 super.handleRecoverableError(token, kind, arguments); | 2208 super.handleRecoverableError(token, kind, arguments); |
2208 if (!hasParserError) { | 2209 addCompileTimeError(recoverableErrors.last.beginOffset, |
2209 printUnexpected(uri, recoverableErrors.last.beginOffset, | 2210 '${recoverableErrors.last.kind} $arguments', |
2210 '${recoverableErrors.last.kind}'); | 2211 silent: silent); |
2211 } | |
2212 hasParserError = true; | |
2213 } | 2212 } |
2214 | 2213 |
2215 @override | 2214 @override |
2216 Token handleUnrecoverableError(Token token, ErrorKind kind, Map arguments) { | 2215 Token handleUnrecoverableError(Token token, ErrorKind kind, Map arguments) { |
2217 if (isDartLibrary && kind == ErrorKind.ExpectedFunctionBody) { | 2216 if (isDartLibrary && kind == ErrorKind.ExpectedFunctionBody) { |
2218 Token recover = skipNativeClause(token); | 2217 Token recover = skipNativeClause(token); |
2219 if (recover != null) return recover; | 2218 if (recover != null) return recover; |
2220 } else if (kind == ErrorKind.UnexpectedToken) { | 2219 } else if (kind == ErrorKind.UnexpectedToken) { |
2221 String expected = arguments["expected"]; | 2220 String expected = arguments["expected"]; |
2222 const List<String> trailing = const <String>[")", "}", ";", ","]; | 2221 const List<String> trailing = const <String>[")", "}", ";", ","]; |
2223 if (trailing.contains(token.stringValue) && trailing.contains(expected)) { | 2222 if (trailing.contains(token.stringValue) && trailing.contains(expected)) { |
2224 arguments.putIfAbsent("actual", () => token.value); | 2223 arguments.putIfAbsent("actual", () => token.value); |
2225 handleRecoverableError(token, ErrorKind.ExpectedButGot, arguments); | 2224 handleRecoverableError(token, ErrorKind.ExpectedButGot, arguments); |
2226 return newSyntheticToken(token); | 2225 return newSyntheticToken(token); |
2227 } | 2226 } |
2228 } | 2227 } |
2229 return super.handleUnrecoverableError(token, kind, arguments); | 2228 return super.handleUnrecoverableError(token, kind, arguments); |
2230 } | 2229 } |
2231 | 2230 |
2232 @override | 2231 @override |
2233 Expression buildCompileTimeError(error, [int charOffset = -1]) { | 2232 Expression buildCompileTimeError(error, [int charOffset = -1]) { |
2234 String message = printUnexpected(uri, charOffset, error); | 2233 addCompileTimeError(charOffset, error); |
| 2234 String message = formatUnexpected(uri, charOffset, error); |
2235 Builder constructor = library.loader.getCompileTimeError(); | 2235 Builder constructor = library.loader.getCompileTimeError(); |
2236 return new Throw(buildStaticInvocation(constructor.target, | 2236 return new Throw(buildStaticInvocation(constructor.target, |
2237 new Arguments(<Expression>[new StringLiteral(message)]), | 2237 new Arguments(<Expression>[new StringLiteral(message)]), |
2238 isConst: false)); // TODO(ahe): Make this const. | 2238 isConst: false)); // TODO(ahe): Make this const. |
2239 } | 2239 } |
2240 | 2240 |
2241 Statement buildCompileTimeErrorStatement(error, [int charOffset = -1]) { | 2241 Statement buildCompileTimeErrorStatement(error, [int charOffset = -1]) { |
2242 return new ExpressionStatement(buildCompileTimeError(error, charOffset)); | 2242 return new ExpressionStatement(buildCompileTimeError(error, charOffset)); |
2243 } | 2243 } |
2244 | 2244 |
(...skipping 18 matching lines...) Expand all Loading... |
2263 void handleOperator(Token token) { | 2263 void handleOperator(Token token) { |
2264 debugEvent("Operator"); | 2264 debugEvent("Operator"); |
2265 push(new Operator(token.stringValue)..fileOffset = token.charOffset); | 2265 push(new Operator(token.stringValue)..fileOffset = token.charOffset); |
2266 } | 2266 } |
2267 | 2267 |
2268 @override | 2268 @override |
2269 void handleSymbolVoid(Token token) { | 2269 void handleSymbolVoid(Token token) { |
2270 logEvent("SymbolVoid"); | 2270 logEvent("SymbolVoid"); |
2271 } | 2271 } |
2272 | 2272 |
2273 dynamic addCompileTimeError(int charOffset, String message) { | 2273 dynamic addCompileTimeError(int charOffset, String message, |
2274 return library.addCompileTimeError(charOffset, message, uri); | 2274 {bool silent: false}) { |
| 2275 return library.addCompileTimeError(charOffset, message, fileUri: uri); |
2275 } | 2276 } |
2276 | 2277 |
2277 @override | 2278 @override |
2278 void handleInvalidFunctionBody(Token token) { | 2279 void handleInvalidFunctionBody(Token token) { |
2279 if (member.isNative) { | 2280 if (member.isNative) { |
2280 push(NullValue.FunctionBody); | 2281 push(NullValue.FunctionBody); |
2281 } else { | 2282 } else { |
2282 push(new Block(<Statement>[new InvalidStatement()])); | 2283 push(new Block(<Statement>[new InvalidStatement()])); |
2283 } | 2284 } |
2284 } | 2285 } |
(...skipping 438 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2723 } else if (node is PrefixBuilder) { | 2724 } else if (node is PrefixBuilder) { |
2724 return node.name; | 2725 return node.name; |
2725 } else if (node is ThisAccessor) { | 2726 } else if (node is ThisAccessor) { |
2726 return node.isSuper ? "super" : "this"; | 2727 return node.isSuper ? "super" : "this"; |
2727 } else if (node is BuilderAccessor) { | 2728 } else if (node is BuilderAccessor) { |
2728 return node.plainNameForRead; | 2729 return node.plainNameForRead; |
2729 } else { | 2730 } else { |
2730 return internalError("Unhandled: ${node.runtimeType}"); | 2731 return internalError("Unhandled: ${node.runtimeType}"); |
2731 } | 2732 } |
2732 } | 2733 } |
OLD | NEW |