diff --git a/pkg/front_end/lib/src/fasta/parser/parser.dart b/pkg/front_end/lib/src/fasta/parser/parser.dart index 8aa932d2a92..861b15ca50b 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 @@ class Parser { 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 @@ class Parser { 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 @@ class Parser { 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 38b68b69abc..27af960d0fc 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 @@ class TokenStreamRewriter { 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