| 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.analyzer.ast_builder; | 5 library fasta.analyzer.ast_builder; |
| 6 | 6 |
| 7 import 'package:analyzer/analyzer.dart'; | 7 import 'package:analyzer/analyzer.dart'; |
| 8 import 'package:analyzer/dart/ast/ast_factory.dart' show AstFactory; | 8 import 'package:analyzer/dart/ast/ast_factory.dart' show AstFactory; |
| 9 import 'package:analyzer/dart/ast/standard_ast_factory.dart' as standard; | 9 import 'package:analyzer/dart/ast/standard_ast_factory.dart' as standard; |
| 10 import 'package:analyzer/dart/ast/token.dart' as analyzer show Token; | 10 import 'package:analyzer/dart/ast/token.dart' as analyzer show Token; |
| 11 import 'package:analyzer/dart/ast/token.dart' show Token, TokenType; | 11 import 'package:analyzer/dart/ast/token.dart' show Token, TokenType; |
| 12 import 'package:analyzer/dart/element/element.dart' show Element; | 12 import 'package:analyzer/dart/element/element.dart' show Element; |
| 13 import 'package:front_end/src/fasta/parser/parser.dart' | 13 import 'package:front_end/src/fasta/parser/parser.dart' |
| 14 show Assert, FormalParameterType, MemberKind, Parser; | 14 show Assert, FormalParameterType, MemberKind, Parser; |
| 15 import 'package:front_end/src/fasta/scanner/string_scanner.dart'; | 15 import 'package:front_end/src/fasta/scanner/string_scanner.dart'; |
| 16 import 'package:front_end/src/fasta/scanner/token.dart' show CommentToken; | 16 import 'package:front_end/src/fasta/scanner/token.dart' show CommentToken; |
| 17 import 'package:front_end/src/scanner/token.dart' as analyzer; | 17 import 'package:front_end/src/scanner/token.dart' as analyzer; |
| 18 | 18 |
| 19 import 'package:front_end/src/fasta/deprecated_problems.dart' | 19 import 'package:front_end/src/fasta/problems.dart' show unexpected, unhandled; |
| 20 show deprecated_internalProblem; | |
| 21 import 'package:front_end/src/fasta/messages.dart' | 20 import 'package:front_end/src/fasta/messages.dart' |
| 22 show Code, Message, codeExpectedExpression, codeExpectedFunctionBody; | 21 show Code, Message, codeExpectedExpression, codeExpectedFunctionBody; |
| 23 import 'package:front_end/src/fasta/kernel/kernel_builder.dart' | 22 import 'package:front_end/src/fasta/kernel/kernel_builder.dart' |
| 24 show Builder, KernelLibraryBuilder, ProcedureBuilder, Scope; | 23 show Builder, KernelLibraryBuilder, ProcedureBuilder, Scope; |
| 25 import 'package:front_end/src/fasta/parser/identifier_context.dart' | 24 import 'package:front_end/src/fasta/parser/identifier_context.dart' |
| 26 show IdentifierContext; | 25 show IdentifierContext; |
| 27 import 'package:front_end/src/fasta/quote.dart'; | 26 import 'package:front_end/src/fasta/quote.dart'; |
| 28 import 'package:front_end/src/fasta/source/scope_listener.dart' | 27 import 'package:front_end/src/fasta/source/scope_listener.dart' |
| 29 show JumpTargetKind, NullValue, ScopeListener; | 28 show JumpTargetKind, NullValue, ScopeListener; |
| 30 import 'analyzer.dart' show toKernel; | 29 import 'analyzer.dart' show toKernel; |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 List<InterpolationElement> elements = <InterpolationElement>[]; | 156 List<InterpolationElement> elements = <InterpolationElement>[]; |
| 158 elements.add(ast.interpolationString( | 157 elements.add(ast.interpolationString( |
| 159 first, unescapeFirstStringPart(first.lexeme, quote))); | 158 first, unescapeFirstStringPart(first.lexeme, quote))); |
| 160 for (int i = 1; i < parts.length - 1; i++) { | 159 for (int i = 1; i < parts.length - 1; i++) { |
| 161 var part = parts[i]; | 160 var part = parts[i]; |
| 162 if (part is Token) { | 161 if (part is Token) { |
| 163 elements.add(ast.interpolationString(part, part.lexeme)); | 162 elements.add(ast.interpolationString(part, part.lexeme)); |
| 164 } else if (part is Expression) { | 163 } else if (part is Expression) { |
| 165 elements.add(ast.interpolationExpression(null, part, null)); | 164 elements.add(ast.interpolationExpression(null, part, null)); |
| 166 } else { | 165 } else { |
| 167 deprecated_internalProblem( | 166 unhandled("${part.runtimeType}", "string interpolation", |
| 168 "Unexpected part in string interpolation: ${part.runtimeType}"); | 167 first.charOffset, uri); |
| 169 } | 168 } |
| 170 } | 169 } |
| 171 elements.add(ast.interpolationString( | 170 elements.add(ast.interpolationString( |
| 172 last, unescapeLastStringPart(last.lexeme, quote))); | 171 last, unescapeLastStringPart(last.lexeme, quote))); |
| 173 push(ast.stringInterpolation(elements)); | 172 push(ast.stringInterpolation(elements)); |
| 174 } | 173 } |
| 175 } | 174 } |
| 176 | 175 |
| 177 void handleScript(Token token) { | 176 void handleScript(Token token) { |
| 178 debugEvent("Script"); | 177 debugEvent("Script"); |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 } else if (body is ExpressionFunctionBody) { | 296 } else if (body is ExpressionFunctionBody) { |
| 298 bodyStatement = ast.returnStatement(null, body.expression, null); | 297 bodyStatement = ast.returnStatement(null, body.expression, null); |
| 299 } else { | 298 } else { |
| 300 bodyStatement = (body as BlockFunctionBody).block; | 299 bodyStatement = (body as BlockFunctionBody).block; |
| 301 } | 300 } |
| 302 var kernel = toKernel(bodyStatement, elementStore, library.library, scope); | 301 var kernel = toKernel(bodyStatement, elementStore, library.library, scope); |
| 303 if (member is ProcedureBuilder) { | 302 if (member is ProcedureBuilder) { |
| 304 ProcedureBuilder builder = member; | 303 ProcedureBuilder builder = member; |
| 305 builder.body = kernel; | 304 builder.body = kernel; |
| 306 } else { | 305 } else { |
| 307 deprecated_internalProblem( | 306 unexpected("procedure", "${member.runtimeType}", member.charOffset, uri); |
| 308 "Internal error: expected procedure, but got: $member"); | |
| 309 } | 307 } |
| 310 } | 308 } |
| 311 | 309 |
| 312 void beginCascade(Token token) { | 310 void beginCascade(Token token) { |
| 313 debugEvent("beginCascade"); | 311 debugEvent("beginCascade"); |
| 314 Expression expression = pop(); | 312 Expression expression = pop(); |
| 315 push(token); | 313 push(token); |
| 316 if (expression is CascadeExpression) { | 314 if (expression is CascadeExpression) { |
| 317 push(expression); | 315 push(expression); |
| 318 } else { | 316 } else { |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 362 } else { | 360 } else { |
| 363 push(ast.propertyAccess(receiver, token, identifierOrInvoke)); | 361 push(ast.propertyAccess(receiver, token, identifierOrInvoke)); |
| 364 } | 362 } |
| 365 } else if (identifierOrInvoke is MethodInvocation) { | 363 } else if (identifierOrInvoke is MethodInvocation) { |
| 366 assert(identifierOrInvoke.target == null); | 364 assert(identifierOrInvoke.target == null); |
| 367 identifierOrInvoke | 365 identifierOrInvoke |
| 368 ..target = receiver | 366 ..target = receiver |
| 369 ..operator = token; | 367 ..operator = token; |
| 370 push(identifierOrInvoke); | 368 push(identifierOrInvoke); |
| 371 } else { | 369 } else { |
| 372 deprecated_internalProblem( | 370 unhandled("${identifierOrInvoke.runtimeType}", "property access", |
| 373 "Unhandled property access: ${identifierOrInvoke.runtimeType}"); | 371 token.charOffset, uri); |
| 374 } | 372 } |
| 375 } | 373 } |
| 376 | 374 |
| 377 void handleLiteralInt(Token token) { | 375 void handleLiteralInt(Token token) { |
| 378 debugEvent("LiteralInt"); | 376 debugEvent("LiteralInt"); |
| 379 push(ast.integerLiteral(token, int.parse(token.lexeme))); | 377 push(ast.integerLiteral(token, int.parse(token.lexeme))); |
| 380 } | 378 } |
| 381 | 379 |
| 382 void handleExpressionFunctionBody(Token arrowToken, Token endToken) { | 380 void handleExpressionFunctionBody(Token arrowToken, Token endToken) { |
| 383 debugEvent("ExpressionFunctionBody"); | 381 debugEvent("ExpressionFunctionBody"); |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 512 VariableDeclaration variable; | 510 VariableDeclaration variable; |
| 513 // TODO(paulberry): This seems kludgy. It would be preferable if we | 511 // TODO(paulberry): This seems kludgy. It would be preferable if we |
| 514 // could respond to a "handleNoVariableInitializer" event by converting a | 512 // could respond to a "handleNoVariableInitializer" event by converting a |
| 515 // SimpleIdentifier into a VariableDeclaration, and then when this code was | 513 // SimpleIdentifier into a VariableDeclaration, and then when this code was |
| 516 // reached, node would always be a VariableDeclaration. | 514 // reached, node would always be a VariableDeclaration. |
| 517 if (node is VariableDeclaration) { | 515 if (node is VariableDeclaration) { |
| 518 variable = node; | 516 variable = node; |
| 519 } else if (node is SimpleIdentifier) { | 517 } else if (node is SimpleIdentifier) { |
| 520 variable = ast.variableDeclaration(node, null, null); | 518 variable = ast.variableDeclaration(node, null, null); |
| 521 } else { | 519 } else { |
| 522 deprecated_internalProblem("unhandled identifier: ${node.runtimeType}"); | 520 unhandled("${node.runtimeType}", "identifier", nameToken.charOffset, uri); |
| 523 } | 521 } |
| 524 push(variable); | 522 push(variable); |
| 525 scope.declare( | 523 scope.declare( |
| 526 variable.name.name, | 524 variable.name.name, |
| 527 variable.name.staticElement = | 525 variable.name.staticElement = |
| 528 new AnalyzerLocalVariableElemment(variable), | 526 new AnalyzerLocalVariableElemment(variable), |
| 529 nameToken.charOffset, | 527 nameToken.charOffset, |
| 530 uri); | 528 uri); |
| 531 } | 529 } |
| 532 | 530 |
| (...skipping 649 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1182 var declarations = <CompilationUnitMember>[]; | 1180 var declarations = <CompilationUnitMember>[]; |
| 1183 if (elements != null) { | 1181 if (elements != null) { |
| 1184 for (AstNode node in elements) { | 1182 for (AstNode node in elements) { |
| 1185 if (node is ScriptTag) { | 1183 if (node is ScriptTag) { |
| 1186 scriptTag = node; | 1184 scriptTag = node; |
| 1187 } else if (node is Directive) { | 1185 } else if (node is Directive) { |
| 1188 directives.add(node); | 1186 directives.add(node); |
| 1189 } else if (node is CompilationUnitMember) { | 1187 } else if (node is CompilationUnitMember) { |
| 1190 declarations.add(node); | 1188 declarations.add(node); |
| 1191 } else { | 1189 } else { |
| 1192 deprecated_internalProblem( | 1190 unhandled( |
| 1193 'Unrecognized compilation unit member: ${node.runtimeType}'); | 1191 "${node.runtimeType}", "compilation unit", node.offset, uri); |
| 1194 } | 1192 } |
| 1195 } | 1193 } |
| 1196 } | 1194 } |
| 1197 | 1195 |
| 1198 push(ast.compilationUnit( | 1196 push(ast.compilationUnit( |
| 1199 beginToken, scriptTag, directives, declarations, endToken)); | 1197 beginToken, scriptTag, directives, declarations, endToken)); |
| 1200 } | 1198 } |
| 1201 | 1199 |
| 1202 void endImport(Token importKeyword, Token deferredKeyword, Token asKeyword, | 1200 void endImport(Token importKeyword, Token deferredKeyword, Token asKeyword, |
| 1203 Token semicolon) { | 1201 Token semicolon) { |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1344 WithClause withClause; | 1342 WithClause withClause; |
| 1345 var supertype = pop(); | 1343 var supertype = pop(); |
| 1346 if (supertype == null) { | 1344 if (supertype == null) { |
| 1347 // No extends clause | 1345 // No extends clause |
| 1348 } else if (supertype is TypeName) { | 1346 } else if (supertype is TypeName) { |
| 1349 extendsClause = ast.extendsClause(extendsKeyword, supertype); | 1347 extendsClause = ast.extendsClause(extendsKeyword, supertype); |
| 1350 } else if (supertype is _MixinApplication) { | 1348 } else if (supertype is _MixinApplication) { |
| 1351 extendsClause = ast.extendsClause(extendsKeyword, supertype.supertype); | 1349 extendsClause = ast.extendsClause(extendsKeyword, supertype.supertype); |
| 1352 withClause = ast.withClause(supertype.withKeyword, supertype.mixinTypes); | 1350 withClause = ast.withClause(supertype.withKeyword, supertype.mixinTypes); |
| 1353 } else { | 1351 } else { |
| 1354 deprecated_internalProblem( | 1352 unhandled("${supertype.runtimeType}", "supertype", |
| 1355 'Unexpected kind of supertype ${supertype.runtimeType}'); | 1353 extendsKeyword.charOffset, uri); |
| 1356 } | 1354 } |
| 1357 TypeParameterList typeParameters = pop(); | 1355 TypeParameterList typeParameters = pop(); |
| 1358 SimpleIdentifier name = pop(); | 1356 SimpleIdentifier name = pop(); |
| 1359 assert(className == name.name); | 1357 assert(className == name.name); |
| 1360 className = null; | 1358 className = null; |
| 1361 _Modifiers modifiers = pop(); | 1359 _Modifiers modifiers = pop(); |
| 1362 Token abstractKeyword = modifiers?.abstractKeyword; | 1360 Token abstractKeyword = modifiers?.abstractKeyword; |
| 1363 List<Annotation> metadata = pop(); | 1361 List<Annotation> metadata = pop(); |
| 1364 Comment comment = pop(); | 1362 Comment comment = pop(); |
| 1365 push(ast.classDeclaration( | 1363 push(ast.classDeclaration( |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1508 Token separator; | 1506 Token separator; |
| 1509 ConstructorName redirectedConstructor; | 1507 ConstructorName redirectedConstructor; |
| 1510 Object bodyObject = pop(); | 1508 Object bodyObject = pop(); |
| 1511 if (bodyObject is FunctionBody) { | 1509 if (bodyObject is FunctionBody) { |
| 1512 body = bodyObject; | 1510 body = bodyObject; |
| 1513 } else if (bodyObject is _RedirectingFactoryBody) { | 1511 } else if (bodyObject is _RedirectingFactoryBody) { |
| 1514 separator = bodyObject.equalToken; | 1512 separator = bodyObject.equalToken; |
| 1515 redirectedConstructor = bodyObject.constructorName; | 1513 redirectedConstructor = bodyObject.constructorName; |
| 1516 body = ast.emptyFunctionBody(semicolon); | 1514 body = ast.emptyFunctionBody(semicolon); |
| 1517 } else { | 1515 } else { |
| 1518 deprecated_internalProblem( | 1516 unhandled("${bodyObject.runtimeType}", "bodyObject", |
| 1519 'Unexpected body object: ${bodyObject.runtimeType}'); | 1517 beginToken.charOffset, uri); |
| 1520 } | 1518 } |
| 1521 | 1519 |
| 1522 FormalParameterList parameters = pop(); | 1520 FormalParameterList parameters = pop(); |
| 1523 ConstructorName constructorName = pop(); | 1521 ConstructorName constructorName = pop(); |
| 1524 _Modifiers modifiers = pop(); | 1522 _Modifiers modifiers = pop(); |
| 1525 List<Annotation> metadata = pop(); | 1523 List<Annotation> metadata = pop(); |
| 1526 Comment comment = pop(); | 1524 Comment comment = pop(); |
| 1527 | 1525 |
| 1528 // Decompose the preliminary ConstructorName into the type name and | 1526 // Decompose the preliminary ConstructorName into the type name and |
| 1529 // the actual constructor name. | 1527 // the actual constructor name. |
| (...skipping 384 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1914 void addCompileTimeError(Message message, int charOffset) { | 1912 void addCompileTimeError(Message message, int charOffset) { |
| 1915 Code code = message.code; | 1913 Code code = message.code; |
| 1916 switch (code.analyzerCode) { | 1914 switch (code.analyzerCode) { |
| 1917 case "EXPECTED_TYPE_NAME": | 1915 case "EXPECTED_TYPE_NAME": |
| 1918 errorReporter?.reportErrorForOffset( | 1916 errorReporter?.reportErrorForOffset( |
| 1919 ParserErrorCode.EXPECTED_TYPE_NAME, charOffset, 1); | 1917 ParserErrorCode.EXPECTED_TYPE_NAME, charOffset, 1); |
| 1920 return; | 1918 return; |
| 1921 default: | 1919 default: |
| 1922 // fall through | 1920 // fall through |
| 1923 } | 1921 } |
| 1924 library.deprecated_addCompileTimeError(charOffset, message.message, | 1922 library.addCompileTimeError(message, charOffset, uri); |
| 1925 fileUri: uri); | |
| 1926 } | 1923 } |
| 1927 } | 1924 } |
| 1928 | 1925 |
| 1929 /// Data structure placed on the stack to represent a class body. | 1926 /// Data structure placed on the stack to represent a class body. |
| 1930 /// | 1927 /// |
| 1931 /// This is needed because analyzer has no separate AST representation of a | 1928 /// This is needed because analyzer has no separate AST representation of a |
| 1932 /// class body; it simply stores all of the relevant data in the | 1929 /// class body; it simply stores all of the relevant data in the |
| 1933 /// [ClassDeclaration] object. | 1930 /// [ClassDeclaration] object. |
| 1934 class _ClassBody { | 1931 class _ClassBody { |
| 1935 final Token beginToken; | 1932 final Token beginToken; |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2019 externalKeyword = token; | 2016 externalKeyword = token; |
| 2020 } else if (identical('final', s)) { | 2017 } else if (identical('final', s)) { |
| 2021 finalConstOrVarKeyword = token; | 2018 finalConstOrVarKeyword = token; |
| 2022 } else if (identical('static', s)) { | 2019 } else if (identical('static', s)) { |
| 2023 staticKeyword = token; | 2020 staticKeyword = token; |
| 2024 } else if (identical('var', s)) { | 2021 } else if (identical('var', s)) { |
| 2025 finalConstOrVarKeyword = token; | 2022 finalConstOrVarKeyword = token; |
| 2026 } else if (identical('covariant', s)) { | 2023 } else if (identical('covariant', s)) { |
| 2027 covariantKeyword = token; | 2024 covariantKeyword = token; |
| 2028 } else { | 2025 } else { |
| 2029 deprecated_internalProblem('Unhandled modifier: $s'); | 2026 unhandled("$s", "modifier", token.charOffset, null); |
| 2030 } | 2027 } |
| 2031 } | 2028 } |
| 2032 } | 2029 } |
| 2033 } | 2030 } |
| OLD | NEW |