Reland: Fix more parser bugs

R=scheglov@google.com

Review-Url: https://codereview.chromium.org/2812243006 .
diff --git a/pkg/analyzer/lib/src/generated/parser.dart b/pkg/analyzer/lib/src/generated/parser.dart
index 1427454..e87b77b 100644
--- a/pkg/analyzer/lib/src/generated/parser.dart
+++ b/pkg/analyzer/lib/src/generated/parser.dart
@@ -548,7 +548,7 @@
       // There was no type name, so this can't be a declaration.
       return false;
     }
-    if (_atGenericFunctionTypeAfterReturnType(token)) {
+    while (_atGenericFunctionTypeAfterReturnType(token)) {
       token = skipGenericFunctionTypeAfterReturnType(token);
       if (token == null) {
         // There was no type name, so this can't be a declaration.
@@ -4066,24 +4066,22 @@
             ])) {
           return _parseFunctionDeclarationStatementAfterReturnType(
               commentAndMetadata, returnType);
-        } else {
-          //
-          // We have found an error of some kind. Try to recover.
-          //
-          if (_matchesIdentifier()) {
-            if (next.matchesAny(const <TokenType>[
+        } else if (_matchesIdentifier() &&
+            next.matchesAny(const <TokenType>[
               TokenType.EQ,
               TokenType.COMMA,
               TokenType.SEMICOLON
             ])) {
-              //
-              // We appear to have a variable declaration with a type of "void".
-              //
-              _reportErrorForNode(ParserErrorCode.VOID_VARIABLE, returnType);
-              return parseVariableDeclarationStatementAfterMetadata(
-                  commentAndMetadata);
-            }
-          } else if (_matches(TokenType.CLOSE_CURLY_BRACKET)) {
+          if (returnType is! GenericFunctionType) {
+            _reportErrorForNode(ParserErrorCode.VOID_VARIABLE, returnType);
+          }
+          return _parseVariableDeclarationStatementAfterType(
+              commentAndMetadata, null, returnType);
+        } else {
+          //
+          // We have found an error of some kind. Try to recover.
+          //
+          if (_matches(TokenType.CLOSE_CURLY_BRACKET)) {
             //
             // We appear to have found an incomplete statement at the end of a
             // block. Parse it as a variable declaration.
diff --git a/pkg/analyzer/test/generated/parser_test.dart b/pkg/analyzer/test/generated/parser_test.dart
index c88bb10..e577850 100644
--- a/pkg/analyzer/test/generated/parser_test.dart
+++ b/pkg/analyzer/test/generated/parser_test.dart
@@ -4106,18 +4106,12 @@
 
   void test_voidVariable_statement_initializer() {
     parseStatement("void x = 0;");
-    assertErrorsWithCodes([
-      ParserErrorCode.VOID_VARIABLE,
-      ParserErrorCode.MISSING_CONST_FINAL_VAR_OR_TYPE
-    ]);
+    assertErrorsWithCodes([ParserErrorCode.VOID_VARIABLE]);
   }
 
   void test_voidVariable_statement_noInitializer() {
     parseStatement("void x;");
-    assertErrorsWithCodes([
-      ParserErrorCode.VOID_VARIABLE,
-      ParserErrorCode.MISSING_CONST_FINAL_VAR_OR_TYPE
-    ]);
+    assertErrorsWithCodes([ParserErrorCode.VOID_VARIABLE]);
   }
 
   void test_withBeforeExtends() {
@@ -11348,6 +11342,15 @@
   }
 
   void
+      test_parseNonLabeledStatement_variableDeclaration_gftType_functionReturnType() {
+    createParser(
+        'Function Function(int x1, {Function x}) Function<B extends core.int>(int x) l771;');
+    Statement statement = parser.parseNonLabeledStatement();
+    expectNotNullIfNoErrors(statement);
+    listener.assertNoErrors();
+  }
+
+  void
       test_parseNonLabeledStatement_variableDeclaration_gftType_gftReturnType() {
     createParser('Function(int) Function(int) v;');
     Statement statement = parser.parseNonLabeledStatement();
@@ -11356,6 +11359,14 @@
   }
 
   void
+      test_parseNonLabeledStatement_variableDeclaration_gftType_gftReturnType2() {
+    createParser('int Function(int) Function(int) v;');
+    Statement statement = parser.parseNonLabeledStatement();
+    expectNotNullIfNoErrors(statement);
+    listener.assertNoErrors();
+  }
+
+  void
       test_parseNonLabeledStatement_variableDeclaration_gftType_noReturnType() {
     createParser('Function(int) v;');
     Statement statement = parser.parseNonLabeledStatement();
@@ -11370,6 +11381,14 @@
     listener.assertNoErrors();
   }
 
+  void
+      test_parseNonLabeledStatement_variableDeclaration_gftType_voidReturnType() {
+    createParser('void Function() v;');
+    Statement statement = parser.parseNonLabeledStatement();
+    expectNotNullIfNoErrors(statement);
+    listener.assertNoErrors();
+  }
+
   void test_parseOptionalReturnType() {
     // TODO(brianwilkerson) Implement tests for this method.
   }