Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(177)

Side by Side Diff: pkg/analyzer/test/generated/parser_test.dart

Issue 1413773003: Improve recovery for local variable declarations. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « pkg/analyzer/lib/src/generated/parser.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, 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 engine.parser_test; 5 library engine.parser_test;
6 6
7 import 'package:analyzer/src/generated/ast.dart'; 7 import 'package:analyzer/src/generated/ast.dart';
8 import 'package:analyzer/src/generated/element.dart'; 8 import 'package:analyzer/src/generated/element.dart';
9 import 'package:analyzer/src/generated/engine.dart'; 9 import 'package:analyzer/src/generated/engine.dart';
10 import 'package:analyzer/src/generated/error.dart'; 10 import 'package:analyzer/src/generated/error.dart';
(...skipping 3203 matching lines...) Expand 10 before | Expand all | Expand 10 after
3214 } 3214 }
3215 3215
3216 void test_conditionalExpression_missingThen() { 3216 void test_conditionalExpression_missingThen() {
3217 ConditionalExpression expression = parse4("parseConditionalExpression", 3217 ConditionalExpression expression = parse4("parseConditionalExpression",
3218 "x ? : z", [ParserErrorCode.MISSING_IDENTIFIER]); 3218 "x ? : z", [ParserErrorCode.MISSING_IDENTIFIER]);
3219 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier, 3219 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
3220 SimpleIdentifier, expression.thenExpression); 3220 SimpleIdentifier, expression.thenExpression);
3221 expect(expression.thenExpression.isSynthetic, isTrue); 3221 expect(expression.thenExpression.isSynthetic, isTrue);
3222 } 3222 }
3223 3223
3224 void test_declarationBeforeDirective() {
3225 CompilationUnit unit = ParserTestCase.parseCompilationUnit(
3226 "class foo { } import 'bar.dart';",
3227 [ParserErrorCode.DIRECTIVE_AFTER_DECLARATION]);
3228 expect(unit.directives, hasLength(1));
3229 expect(unit.declarations, hasLength(1));
3230 ClassDeclaration classDecl = unit.childEntities.first;
3231 expect(classDecl, isNotNull);
3232 expect(classDecl.name.name, 'foo');
3233 }
3234
3224 void test_equalityExpression_missing_LHS() { 3235 void test_equalityExpression_missing_LHS() {
3225 BinaryExpression expression = 3236 BinaryExpression expression =
3226 parseExpression("== y", [ParserErrorCode.MISSING_IDENTIFIER]); 3237 parseExpression("== y", [ParserErrorCode.MISSING_IDENTIFIER]);
3227 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier, 3238 EngineTestCase.assertInstanceOf((obj) => obj is SimpleIdentifier,
3228 SimpleIdentifier, expression.leftOperand); 3239 SimpleIdentifier, expression.leftOperand);
3229 expect(expression.leftOperand.isSynthetic, isTrue); 3240 expect(expression.leftOperand.isSynthetic, isTrue);
3230 } 3241 }
3231 3242
3232 void test_equalityExpression_missing_LHS_RHS() { 3243 void test_equalityExpression_missing_LHS_RHS() {
3233 BinaryExpression expression = parseExpression("==", [ 3244 BinaryExpression expression = parseExpression("==", [
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
3331 NodeList<VariableDeclaration> vars = 3342 NodeList<VariableDeclaration> vars =
3332 (fieldDecl as FieldDeclaration).fields.variables; 3343 (fieldDecl as FieldDeclaration).fields.variables;
3333 expect(vars, hasLength(1)); 3344 expect(vars, hasLength(1));
3334 expect(vars[0].name.name, "v"); 3345 expect(vars[0].name.name, "v");
3335 } 3346 }
3336 3347
3337 void test_functionExpression_named() { 3348 void test_functionExpression_named() {
3338 parseExpression("m(f() => 0);", [ParserErrorCode.EXPECTED_TOKEN]); 3349 parseExpression("m(f() => 0);", [ParserErrorCode.EXPECTED_TOKEN]);
3339 } 3350 }
3340 3351
3341 void test_declarationBeforeDirective() {
3342 CompilationUnit unit = ParserTestCase.parseCompilationUnit(
3343 "class foo { } import 'bar.dart';",
3344 [ParserErrorCode.DIRECTIVE_AFTER_DECLARATION]);
3345 expect(unit.directives, hasLength(1));
3346 expect(unit.declarations, hasLength(1));
3347 ClassDeclaration classDecl = unit.childEntities.first;
3348 expect(classDecl, isNotNull);
3349 expect(classDecl.name.name, 'foo');
3350 }
3351
3352 void test_importDirectivePartial_as() { 3352 void test_importDirectivePartial_as() {
3353 CompilationUnit unit = ParserTestCase.parseCompilationUnit( 3353 CompilationUnit unit = ParserTestCase.parseCompilationUnit(
3354 "import 'b.dart' d as b;", [ParserErrorCode.UNEXPECTED_TOKEN]); 3354 "import 'b.dart' d as b;", [ParserErrorCode.UNEXPECTED_TOKEN]);
3355 ImportDirective importDirective = unit.childEntities.first; 3355 ImportDirective importDirective = unit.childEntities.first;
3356 expect(importDirective.asKeyword, isNotNull); 3356 expect(importDirective.asKeyword, isNotNull);
3357 expect(unit.directives, hasLength(1)); 3357 expect(unit.directives, hasLength(1));
3358 expect(unit.declarations, hasLength(0)); 3358 expect(unit.declarations, hasLength(0));
3359 } 3359 }
3360 3360
3361 void test_importDirectivePartial_hide() { 3361 void test_importDirectivePartial_hide() {
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
3542 (obj) => obj is FieldDeclaration, FieldDeclaration, classMember); 3542 (obj) => obj is FieldDeclaration, FieldDeclaration, classMember);
3543 VariableDeclarationList fieldList = 3543 VariableDeclarationList fieldList =
3544 (classMember as FieldDeclaration).fields; 3544 (classMember as FieldDeclaration).fields;
3545 expect((fieldList.keyword as KeywordToken).keyword, Keyword.VAR); 3545 expect((fieldList.keyword as KeywordToken).keyword, Keyword.VAR);
3546 NodeList<VariableDeclaration> fields = fieldList.variables; 3546 NodeList<VariableDeclaration> fields = fieldList.variables;
3547 expect(fields, hasLength(1)); 3547 expect(fields, hasLength(1));
3548 VariableDeclaration field = fields[0]; 3548 VariableDeclaration field = fields[0];
3549 expect(field.name.isSynthetic, isTrue); 3549 expect(field.name.isSynthetic, isTrue);
3550 } 3550 }
3551 3551
3552 void test_incompleteLocalVariable_atTheEndOfBlock() {
3553 Statement statement = ParserTestCase.parseStatement(
3554 'String v }', [ParserErrorCode.EXPECTED_TOKEN]);
3555 expect(statement, new isInstanceOf<VariableDeclarationStatement>());
3556 expect(statement.toSource(), 'String v;');
3557 }
3558
3559 void test_incompleteLocalVariable_beforeIdentifier() {
3560 Statement statement = ParserTestCase.parseStatement(
3561 'String v String v2;', [ParserErrorCode.EXPECTED_TOKEN]);
3562 expect(statement, new isInstanceOf<VariableDeclarationStatement>());
3563 expect(statement.toSource(), 'String v;');
3564 }
3565
3566 void test_incompleteLocalVariable_beforeKeyword() {
3567 Statement statement = ParserTestCase.parseStatement(
3568 'String v if (true) {}', [ParserErrorCode.EXPECTED_TOKEN]);
3569 expect(statement, new isInstanceOf<VariableDeclarationStatement>());
3570 expect(statement.toSource(), 'String v;');
3571 }
3572
3573 void test_incompleteLocalVariable_beforeNextBlock() {
3574 Statement statement = ParserTestCase.parseStatement(
3575 'String v {}', [ParserErrorCode.EXPECTED_TOKEN]);
3576 expect(statement, new isInstanceOf<VariableDeclarationStatement>());
3577 expect(statement.toSource(), 'String v;');
3578 }
3579
3580 void test_incompleteLocalVariable_parameterizedType() {
3581 Statement statement = ParserTestCase.parseStatement(
3582 'List<String> v {}', [ParserErrorCode.EXPECTED_TOKEN]);
3583 expect(statement, new isInstanceOf<VariableDeclarationStatement>());
3584 expect(statement.toSource(), 'List<String> v;');
3585 }
3586
3552 void test_invalidFunctionBodyModifier() { 3587 void test_invalidFunctionBodyModifier() {
3553 ParserTestCase.parseCompilationUnit( 3588 ParserTestCase.parseCompilationUnit(
3554 "f() sync {}", [ParserErrorCode.MISSING_STAR_AFTER_SYNC]); 3589 "f() sync {}", [ParserErrorCode.MISSING_STAR_AFTER_SYNC]);
3555 } 3590 }
3556 3591
3557 void test_isExpression_noType() { 3592 void test_isExpression_noType() {
3558 CompilationUnit unit = ParserTestCase.parseCompilationUnit( 3593 CompilationUnit unit = ParserTestCase.parseCompilationUnit(
3559 "class Bar<T extends Foo> {m(x){if (x is ) return;if (x is !)}}", [ 3594 "class Bar<T extends Foo> {m(x){if (x is ) return;if (x is !)}}", [
3560 ParserErrorCode.EXPECTED_TYPE_NAME, 3595 ParserErrorCode.EXPECTED_TYPE_NAME,
3561 ParserErrorCode.EXPECTED_TYPE_NAME, 3596 ParserErrorCode.EXPECTED_TYPE_NAME,
(...skipping 5854 matching lines...) Expand 10 before | Expand all | Expand 10 after
9416 expect(identifier.name, lexeme); 9451 expect(identifier.name, lexeme);
9417 } 9452 }
9418 9453
9419 void test_parseSimpleIdentifier_normalIdentifier() { 9454 void test_parseSimpleIdentifier_normalIdentifier() {
9420 String lexeme = "foo"; 9455 String lexeme = "foo";
9421 SimpleIdentifier identifier = parse4("parseSimpleIdentifier", lexeme); 9456 SimpleIdentifier identifier = parse4("parseSimpleIdentifier", lexeme);
9422 expect(identifier.token, isNotNull); 9457 expect(identifier.token, isNotNull);
9423 expect(identifier.name, lexeme); 9458 expect(identifier.name, lexeme);
9424 } 9459 }
9425 9460
9426 void test_parseStatement_emptyTypeArgumentListt() { 9461 void test_parseStatement_emptyTypeArgumentList() {
9427 VariableDeclarationStatement statement = parse4( 9462 VariableDeclarationStatement statement = parse4(
9428 "parseStatement", "C<> c;", [ParserErrorCode.EXPECTED_TYPE_NAME]); 9463 "parseStatement", "C<> c;", [ParserErrorCode.EXPECTED_TYPE_NAME]);
9429 VariableDeclarationList variables = statement.variables; 9464 VariableDeclarationList variables = statement.variables;
9430 TypeName type = variables.type; 9465 TypeName type = variables.type;
9431 TypeArgumentList argumentList = type.typeArguments; 9466 TypeArgumentList argumentList = type.typeArguments;
9432 expect(argumentList.leftBracket, isNotNull); 9467 expect(argumentList.leftBracket, isNotNull);
9433 expect(argumentList.arguments, hasLength(1)); 9468 expect(argumentList.arguments, hasLength(1));
9434 expect(argumentList.arguments[0].isSynthetic, isTrue); 9469 expect(argumentList.arguments[0].isSynthetic, isTrue);
9435 expect(argumentList.rightBracket, isNotNull); 9470 expect(argumentList.rightBracket, isNotNull);
9436 } 9471 }
(...skipping 1248 matching lines...) Expand 10 before | Expand all | Expand 10 after
10685 new Scanner(null, new CharSequenceReader(source), listener); 10720 new Scanner(null, new CharSequenceReader(source), listener);
10686 Token tokenStream = scanner.tokenize(); 10721 Token tokenStream = scanner.tokenize();
10687 // 10722 //
10688 // Parse the source. 10723 // Parse the source.
10689 // 10724 //
10690 Parser parser = new Parser(null, listener); 10725 Parser parser = new Parser(null, listener);
10691 return invokeParserMethodImpl( 10726 return invokeParserMethodImpl(
10692 parser, methodName, <Object>[tokenStream], tokenStream) as Token; 10727 parser, methodName, <Object>[tokenStream], tokenStream) as Token;
10693 } 10728 }
10694 } 10729 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/src/generated/parser.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698