cleanup insert synthetic identifier

Change-Id: Ia7f2a94664f25ac51c0876d0c3317b78e5c2f63c
Reviewed-on: https://dart-review.googlesource.com/64780
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Dan Rubel <danrubel@google.com>
diff --git a/pkg/front_end/lib/src/fasta/parser/parser.dart b/pkg/front_end/lib/src/fasta/parser/parser.dart
index 8aa932d..861b15c 100644
--- a/pkg/front_end/lib/src/fasta/parser/parser.dart
+++ b/pkg/front_end/lib/src/fasta/parser/parser.dart
@@ -1083,10 +1083,7 @@
           new SyntheticKeywordToken(Keyword.WITH, withKeyword.charOffset);
       rewriter.insertTokenAfter(token, withKeyword);
       if (!isValidTypeReference(withKeyword.next)) {
-        rewriter.insertTokenAfter(
-            withKeyword,
-            new SyntheticStringToken(
-                TokenType.IDENTIFIER, '', withKeyword.charOffset));
+        rewriter.insertSyntheticIdentifier(withKeyword);
       }
     }
     listener.beginMixinApplication(withKeyword);
@@ -1872,10 +1869,7 @@
     Token next = token.next;
     reportRecoverableError(messageOnToken ?? next,
         message ?? context.recoveryTemplate.withArguments(next));
-    Token identifier =
-        new SyntheticStringToken(TokenType.IDENTIFIER, '', next.charOffset, 0);
-    rewriter.insertTokenAfter(token, identifier);
-    return token.next;
+    return rewriter.insertSyntheticIdentifier(token);
   }
 
   /// Parse a simple identifier at the given [token], and return the identifier
@@ -2308,10 +2302,7 @@
             next, fasta.templateExpectedButGot.withArguments('.'));
         rewriter.insertTokenAfter(
             token, new SyntheticToken(TokenType.PERIOD, next.offset));
-        token = token.next;
-        rewriter.insertTokenAfter(token,
-            new SyntheticStringToken(TokenType.IDENTIFIER, '', next.offset));
-        token = token.next;
+        token = rewriter.insertSyntheticIdentifier(token.next);
         next = token.next;
       }
       // Fall through to recovery
diff --git a/pkg/front_end/lib/src/fasta/parser/token_stream_rewriter.dart b/pkg/front_end/lib/src/fasta/parser/token_stream_rewriter.dart
index 38b68b6..27af960 100644
--- a/pkg/front_end/lib/src/fasta/parser/token_stream_rewriter.dart
+++ b/pkg/front_end/lib/src/fasta/parser/token_stream_rewriter.dart
@@ -55,20 +55,29 @@
     next = next.setNext(new SyntheticToken(TokenType.CLOSE_PAREN, offset));
     leftParen.endGroup = next;
     next.setNext(token.next);
+
+    // A no-op rewriter could skip this step.
     token.setNext(leftParen);
+
     return leftParen;
   }
 
   /// Insert a synthetic identifier after [token] and return the new identifier.
   Token insertSyntheticIdentifier(Token token) {
-    Token identifier = new SyntheticStringToken(
-        TokenType.IDENTIFIER, '', token.next.charOffset, 0)
-      ..setNext(token.next);
+    return insertToken(
+        token,
+        new SyntheticStringToken(
+            TokenType.IDENTIFIER, '', token.next.charOffset, 0));
+  }
 
-    // A no-op rewriter could simply return the synthetic identifier here.
+  /// Insert [newToken] after [token] and return [newToken].
+  Token insertToken(Token token, Token newToken) {
+    newToken.setNext(token.next);
 
-    token.setNext(identifier);
-    return identifier;
+    // A no-op rewriter could skip this step.
+    token.setNext(newToken);
+
+    return newToken;
   }
 
   /// Insert the chain of tokens starting at the [insertedToken] immediately