Cleanup fasta parser rewriter insert token methods
Change-Id: Ic776cae52cbfae4c1892884a6d9e179147c5396e
Reviewed-on: https://dart-review.googlesource.com/75760
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Dan Rubel <danrubel@google.com>
diff --git a/pkg/analyzer/lib/src/fasta/ast_builder.dart b/pkg/analyzer/lib/src/fasta/ast_builder.dart
index 67e0502..35f40b1 100644
--- a/pkg/analyzer/lib/src/fasta/ast_builder.dart
+++ b/pkg/analyzer/lib/src/fasta/ast_builder.dart
@@ -1111,7 +1111,7 @@
if (variableOrDeclaration is! SimpleIdentifier) {
// Parser has already reported the error.
if (!leftParenthesis.next.isIdentifier) {
- parser.rewriter.insertTokenAfter(
+ parser.rewriter.insertToken(
leftParenthesis,
new SyntheticStringToken(
TokenType.IDENTIFIER, '', leftParenthesis.next.charOffset));
diff --git a/pkg/front_end/lib/src/fasta/parser/parser.dart b/pkg/front_end/lib/src/fasta/parser/parser.dart
index 0ffe218..b12f4fe 100644
--- a/pkg/front_end/lib/src/fasta/parser/parser.dart
+++ b/pkg/front_end/lib/src/fasta/parser/parser.dart
@@ -488,7 +488,7 @@
reportRecoverableError(next, fasta.messageTopLevelOperator);
// Insert a synthetic identifier
// and continue parsing as a top level function.
- rewriter.insertTokenAfter(
+ rewriter.insertToken(
next,
new SyntheticStringToken(
TokenType.IDENTIFIER,
@@ -1099,7 +1099,7 @@
withKeyword, fasta.templateExpectedButGot.withArguments('with'));
withKeyword =
new SyntheticKeywordToken(Keyword.WITH, withKeyword.charOffset);
- rewriter.insertTokenAfter(token, withKeyword);
+ rewriter.insertToken(token, withKeyword);
if (!isValidTypeReference(withKeyword.next)) {
rewriter.insertSyntheticIdentifier(withKeyword);
}
@@ -1203,7 +1203,7 @@
next.next.kind == IDENTIFIER_TOKEN) {
// Looks like a missing comma
Token comma = new SyntheticToken(TokenType.COMMA, next.charOffset);
- token = rewriter.insertTokenAfter(token, comma).next;
+ token = rewriter.insertToken(token, comma);
continue;
} else {
token = ensureCloseParen(token, begin);
@@ -1324,10 +1324,9 @@
if (!optional('.', next)) {
// Recover from a missing period by inserting one.
next = rewriteAndRecover(
- token,
- fasta.templateExpectedButGot.withArguments('.'),
- new SyntheticToken(TokenType.PERIOD, next.charOffset))
- .next;
+ token,
+ fasta.templateExpectedButGot.withArguments('.'),
+ new SyntheticToken(TokenType.PERIOD, next.charOffset));
}
periodAfterThis = token = next;
next = token.next;
@@ -1476,7 +1475,7 @@
token = next;
}
if (parameterCount == 0) {
- token = rewriteAndRecover(
+ rewriteAndRecover(
token,
fasta.messageEmptyOptionalParameterList,
new SyntheticStringToken(
@@ -1527,7 +1526,7 @@
token = next;
}
if (parameterCount == 0) {
- token = rewriteAndRecover(
+ rewriteAndRecover(
token,
fasta.messageEmptyNamedParameterList,
new SyntheticStringToken(
@@ -1971,10 +1970,8 @@
if (!errorReported) {
reportRecoverableErrorWithToken(next, fasta.templateExpectedString);
}
- next = rewriter
- .insertTokenAfter(token,
- new SyntheticStringToken(TokenType.STRING, '', next.charOffset))
- .next;
+ next = rewriter.insertToken(token,
+ new SyntheticStringToken(TokenType.STRING, '', next.charOffset));
}
}
listener.handleStringPart(next);
@@ -2114,7 +2111,7 @@
token = next;
next = token.next;
if (optional('(', next.next)) {
- rewriter.insertTokenAfter(
+ rewriter.insertToken(
next,
new SyntheticStringToken(
TokenType.IDENTIFIER,
@@ -2423,7 +2420,7 @@
// `this.<fieldname>=` is expected.
reportRecoverableError(
next, fasta.templateExpectedButGot.withArguments('.'));
- rewriter.insertTokenAfter(
+ rewriter.insertToken(
token, new SyntheticToken(TokenType.PERIOD, next.offset));
token = rewriter.insertSyntheticIdentifier(token.next);
next = token.next;
@@ -2464,8 +2461,7 @@
token = insertSyntheticIdentifier(
beforeExpression, IdentifierContext.fieldInitializer,
message: fasta.messageMissingAssignmentInInitializer);
- rewriter.insertTokenAfter(
- token, new SyntheticToken(TokenType.EQ, token.offset));
+ rewriter.insertToken(token, new SyntheticToken(TokenType.EQ, token.offset));
return parseInitializerExpressionRest(beforeExpression);
}
@@ -2532,11 +2528,12 @@
Token insertBlock(Token token) {
Token next = token.next;
- Token replacement = link(
- new SyntheticBeginToken(TokenType.OPEN_CURLY_BRACKET, next.offset),
+ BeginToken beginGroup = rewriter.insertToken(token,
+ new SyntheticBeginToken(TokenType.OPEN_CURLY_BRACKET, next.offset));
+ Token endGroup = rewriter.insertToken(beginGroup,
new SyntheticToken(TokenType.CLOSE_CURLY_BRACKET, next.offset));
- rewriter.insertTokenAfter(token, replacement);
- return replacement;
+ beginGroup.endGroup = endGroup;
+ return beginGroup;
}
/// If the next token is a closing parenthesis, return it.
@@ -2570,7 +2567,7 @@
if (optional(':', next)) return next;
Message message = fasta.templateExpectedButGot.withArguments(':');
Token newToken = new SyntheticToken(TokenType.COLON, next.charOffset);
- return rewriteAndRecover(token, message, newToken).next;
+ return rewriteAndRecover(token, message, newToken);
}
/// If the token after [token] is a not literal string,
@@ -2608,11 +2605,10 @@
}
/// Report an error at the token after [token] that has the given [message].
- /// Insert the [newToken] after [token] and return [token].
+ /// Insert the [newToken] after [token] and return [newToken].
Token rewriteAndRecover(Token token, Message message, Token newToken) {
reportRecoverableError(token.next, message);
- rewriter.insertTokenAfter(token, newToken);
- return token;
+ return rewriter.insertToken(token, newToken);
}
/// Replace the token after [token] with `[` followed by `]`
@@ -3305,10 +3301,8 @@
// If `return` used instead of `=>`, then report an error and continue
if (optional('return', next)) {
reportRecoverableError(next, fasta.messageExpectedBody);
- next = rewriter
- .insertTokenAfter(next,
- new SyntheticToken(TokenType.FUNCTION, next.next.charOffset))
- .next;
+ next = rewriter.insertToken(
+ next, new SyntheticToken(TokenType.FUNCTION, next.next.charOffset));
return parseExpressionFunctionBody(next, ofFunctionExpression);
}
// If there is a stray simple identifier in the function expression
@@ -4142,10 +4136,9 @@
// This looks like the start of an expression.
// Report an error, insert the comma, and continue parsing.
next = rewriteAndRecover(
- token,
- fasta.templateExpectedButGot.withArguments(','),
- new SyntheticToken(TokenType.COMMA, next.offset))
- .next;
+ token,
+ fasta.templateExpectedButGot.withArguments(','),
+ new SyntheticToken(TokenType.COMMA, next.offset));
}
token = next;
}
@@ -4190,10 +4183,9 @@
// If this looks like the start of an expression,
// then report an error, insert the comma, and continue parsing.
next = rewriteAndRecover(
- token,
- fasta.templateExpectedButGot.withArguments(','),
- new SyntheticToken(TokenType.COMMA, next.offset))
- .next;
+ token,
+ fasta.templateExpectedButGot.withArguments(','),
+ new SyntheticToken(TokenType.COMMA, next.offset));
} else {
reportRecoverableError(
next, fasta.templateExpectedButGot.withArguments('}'));
@@ -4258,7 +4250,7 @@
// TODO(danrubel): Improve this error message.
reportRecoverableError(
next, fasta.templateExpectedButGot.withArguments('['));
- rewriter.insertTokenAfter(
+ rewriter.insertToken(
token, new SyntheticToken(TokenType.INDEX, next.charOffset));
}
return parseLiteralListSuffix(token, constKeyword);
@@ -4623,10 +4615,9 @@
// If this looks like the start of an expression,
// then report an error, insert the comma, and continue parsing.
next = rewriteAndRecover(
- token,
- fasta.templateExpectedButGot.withArguments(','),
- new SyntheticToken(TokenType.COMMA, next.offset))
- .next;
+ token,
+ fasta.templateExpectedButGot.withArguments(','),
+ new SyntheticToken(TokenType.COMMA, next.offset));
} else {
token = ensureCloseParen(token, begin);
break;
@@ -5166,10 +5157,8 @@
if (!optional('while', whileToken)) {
reportRecoverableError(
whileToken, fasta.templateExpectedButGot.withArguments('while'));
- whileToken = rewriter
- .insertTokenAfter(token,
- new SyntheticKeywordToken(Keyword.WHILE, whileToken.charOffset))
- .next;
+ whileToken = rewriter.insertToken(token,
+ new SyntheticKeywordToken(Keyword.WHILE, whileToken.charOffset));
}
token = parseParenthesizedCondition(whileToken);
token = ensureSemicolon(token);
@@ -5254,7 +5243,7 @@
// checking the next token as we are doing here.
reportRecoverableError(
throwToken.next, fasta.messageMissingExpressionInThrow);
- rewriter.insertTokenAfter(
+ rewriter.insertToken(
throwToken,
new SyntheticStringToken(
TokenType.STRING, '""', throwToken.next.charOffset, 0));
@@ -5724,7 +5713,7 @@
next = next.next;
} else {
reportRecoverableError(next, fasta.messageMissingOperatorKeyword);
- rewriter.insertTokenAfter(
+ rewriter.insertToken(
beforeName, new SyntheticToken(Keyword.OPERATOR, next.offset));
}
@@ -5819,7 +5808,7 @@
reportRecoverableError(next, fasta.messageStackOverflow);
next = new SyntheticToken(TokenType.SEMICOLON, token.offset);
- rewriter.insertTokenAfter(token, next);
+ rewriter.insertToken(token, next);
listener.handleEmptyStatement(next);
while (notEofOrValue('}', next)) {
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 cc01a67..86170d4 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
@@ -80,18 +80,6 @@
return newToken;
}
- /// Insert the chain of tokens starting at the [insertedToken] immediately
- /// after the [previousToken]. Return the [previousToken].
- Token insertTokenAfter(Token previousToken, Token insertedToken) {
- Token afterToken = previousToken.next;
- previousToken.setNext(insertedToken);
-
- Token lastReplacement = _lastTokenInChain(insertedToken);
- lastReplacement.setNext(afterToken);
-
- return previousToken;
- }
-
/// Move [endGroup] (a synthetic `)`, `]`, or `}` token) and associated
/// error token after [token] in the token stream and return [endGroup].
Token moveSynthetic(Token token, Token endGroup) {
diff --git a/pkg/front_end/test/fasta/parser/token_stream_rewriter_test.dart b/pkg/front_end/test/fasta/parser/token_stream_rewriter_test.dart
index ed8df9c..f47b461 100644
--- a/pkg/front_end/test/fasta/parser/token_stream_rewriter_test.dart
+++ b/pkg/front_end/test/fasta/parser/token_stream_rewriter_test.dart
@@ -22,46 +22,30 @@
/// Indicates whether the tests should set up [Token.previous].
bool get setPrevious;
- void test_insertTokenAfter_end_single() {
+ void test_insertToken_end() {
var a = _makeToken(0, 'a');
var b = _makeToken(1, 'b');
var eof = _link([a]);
var rewriter = new TokenStreamRewriter();
- expect(rewriter.insertTokenAfter(a, b), same(a));
+ expect(rewriter.insertToken(a, b), same(b));
expect(a.next, same(b));
expect(b.next, same(eof));
expect(eof.previous, same(b));
expect(b.previous, same(a));
}
- void test_insertTokenAfter_middle_multiple() {
- var a = _makeToken(0, 'a');
- var b = _makeToken(1, 'b');
- var c = _makeToken(2, 'c');
- var d = _makeToken(3, 'd');
- var e = _makeToken(4, 'e');
- _link([a, b, e]);
- _link([c, d]);
- var rewriter = new TokenStreamRewriter();
- rewriter.insertTokenAfter(b, c);
- expect(a.next, same(b));
- expect(b.next, same(c));
- expect(c.next, same(d));
- expect(d.next, same(e));
- }
-
- void test_insertTokenAfter_middle_single() {
+ void test_insertToken_middle() {
var a = _makeToken(0, 'a');
var b = _makeToken(1, 'b');
var c = _makeToken(2, 'c');
_link([a, c]);
var rewriter = new TokenStreamRewriter();
- rewriter.insertTokenAfter(a, b);
+ rewriter.insertToken(a, b);
expect(a.next, same(b));
expect(b.next, same(c));
}
- void test_insertTokenAfter_second_insertion_earlier_in_stream() {
+ void test_insertToken_second_insertion_earlier_in_stream() {
var a = _makeToken(0, 'a');
var b = _makeToken(1, 'b');
var c = _makeToken(2, 'c');
@@ -69,12 +53,12 @@
var e = _makeToken(4, 'e');
_link([a, c, e]);
var rewriter = new TokenStreamRewriter();
- rewriter.insertTokenAfter(c, d);
+ rewriter.insertToken(c, d);
expect(c.next, same(d));
expect(d.next, same(e));
// The next call to rewriter should be able to find the insertion point
// even though it is before the insertion point used above.
- rewriter.insertTokenAfter(a, b);
+ rewriter.insertToken(a, b);
expect(a.next, same(b));
expect(b.next, same(c));
}