Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 6edefda

Browse files
Dan Rubelcommit-bot@chromium.org
authored andcommitted
cleanup token rewriter and clients
Change-Id: I05f3549c37abccaa7ce7119c0b9d3a084a381f80 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/103500 Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 4ccf7b8 commit 6edefda

File tree

3 files changed

+62
-84
lines changed

3 files changed

+62
-84
lines changed

pkg/analyzer/lib/src/fasta/ast_builder.dart

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2291,10 +2291,7 @@ class AstBuilder extends StackListener {
22912291
if (variableOrDeclaration is! SimpleIdentifier) {
22922292
// Parser has already reported the error.
22932293
if (!leftParenthesis.next.isIdentifier) {
2294-
parser.rewriter.insertToken(
2295-
leftParenthesis,
2296-
new SyntheticStringToken(
2297-
TokenType.IDENTIFIER, '', leftParenthesis.next.charOffset));
2294+
parser.rewriter.insertSyntheticIdentifier(leftParenthesis);
22982295
}
22992296
variableOrDeclaration = ast.simpleIdentifier(leftParenthesis.next);
23002297
}

pkg/front_end/lib/src/fasta/parser/parser.dart

Lines changed: 32 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -500,13 +500,8 @@ class Parser {
500500
reportRecoverableError(next, fasta.messageTopLevelOperator);
501501
// Insert a synthetic identifier
502502
// and continue parsing as a top level function.
503-
rewriter.insertToken(
504-
next,
505-
new SyntheticStringToken(
506-
TokenType.IDENTIFIER,
507-
'#synthetic_function_${next.charOffset}',
508-
next.next.charOffset,
509-
0));
503+
rewriter.insertSyntheticIdentifier(
504+
next, '#synthetic_function_${next.charOffset}');
510505
return parseTopLevelMemberImpl(next);
511506
}
512507
// Ignore any preceding modifiers and just report the unexpected token
@@ -1109,9 +1104,7 @@ class Parser {
11091104
// Recovery: Report an error and insert synthetic `with` clause.
11101105
reportRecoverableError(
11111106
withKeyword, fasta.templateExpectedButGot.withArguments('with'));
1112-
withKeyword =
1113-
new SyntheticKeywordToken(Keyword.WITH, withKeyword.charOffset);
1114-
rewriter.insertToken(token, withKeyword);
1107+
withKeyword = rewriter.insertSyntheticKeyword(token, Keyword.WITH);
11151108
if (!isValidTypeReference(withKeyword.next)) {
11161109
rewriter.insertSyntheticIdentifier(withKeyword);
11171110
}
@@ -2223,13 +2216,8 @@ class Parser {
22232216
token = next;
22242217
next = token.next;
22252218
if (optional('(', next.next)) {
2226-
rewriter.insertToken(
2227-
next,
2228-
new SyntheticStringToken(
2229-
TokenType.IDENTIFIER,
2230-
'#synthetic_identifier_${next.charOffset}',
2231-
next.charOffset,
2232-
0));
2219+
rewriter.insertSyntheticIdentifier(
2220+
next, '#synthetic_identifier_${next.charOffset}');
22332221
}
22342222
}
22352223
}
@@ -2496,8 +2484,7 @@ class Parser {
24962484
// expecting one of `,` or `;` or `{`
24972485
reportRecoverableError(
24982486
token, fasta.templateExpectedAfterButGot.withArguments(','));
2499-
next = rewriter.insertToken(
2500-
token, new SyntheticToken(TokenType.COMMA, token.next.charOffset));
2487+
next = rewriter.insertSyntheticToken(token, TokenType.COMMA);
25012488
}
25022489
}
25032490
mayParseFunctionExpressions = old;
@@ -2559,8 +2546,7 @@ class Parser {
25592546
// `this.<fieldname>=` is expected.
25602547
reportRecoverableError(
25612548
next, fasta.templateExpectedButGot.withArguments('.'));
2562-
rewriter.insertToken(
2563-
token, new SyntheticToken(TokenType.PERIOD, next.offset));
2549+
rewriter.insertSyntheticToken(token, TokenType.PERIOD);
25642550
token = rewriter.insertSyntheticIdentifier(token.next);
25652551
next = token.next;
25662552
}
@@ -2574,8 +2560,7 @@ class Parser {
25742560
// then fall through to insert the LHS and `=` of the assignment,
25752561
// otherwise insert an `=` and synthetic identifier.
25762562
if (!next2.isOperator && !optional('.', next2)) {
2577-
token = rewriter.insertToken(
2578-
next, new SyntheticToken(TokenType.EQ, next2.offset));
2563+
token = rewriter.insertSyntheticToken(next, TokenType.EQ);
25792564
token = insertSyntheticIdentifier(token, IdentifierContext.expression,
25802565
message: fasta.messageMissingAssignmentInInitializer,
25812566
messageOnToken: next);
@@ -2586,8 +2571,7 @@ class Parser {
25862571
token = insertSyntheticIdentifier(
25872572
token, IdentifierContext.fieldInitializer,
25882573
message: fasta.messageExpectedAnInitializer, messageOnToken: token);
2589-
token = rewriter.insertToken(
2590-
token, new SyntheticToken(TokenType.EQ, token.offset));
2574+
token = rewriter.insertSyntheticToken(token, TokenType.EQ);
25912575
token = rewriter.insertSyntheticIdentifier(token);
25922576
return parseInitializerExpressionRest(beforeExpression);
25932577
}
@@ -2600,7 +2584,7 @@ class Parser {
26002584
token = insertSyntheticIdentifier(
26012585
beforeExpression, IdentifierContext.fieldInitializer,
26022586
message: fasta.messageMissingAssignmentInInitializer);
2603-
rewriter.insertToken(token, new SyntheticToken(TokenType.EQ, token.offset));
2587+
rewriter.insertSyntheticToken(token, TokenType.EQ);
26042588
return parseInitializerExpressionRest(beforeExpression);
26052589
}
26062590

@@ -2745,9 +2729,7 @@ class Parser {
27452729
// for users to understand and fix the error.
27462730
reportRecoverableError(findPreviousNonZeroLengthToken(token),
27472731
fasta.templateExpectedAfterButGot.withArguments(';'));
2748-
2749-
return rewriter.insertToken(
2750-
token, new SyntheticToken(TokenType.SEMICOLON, next.charOffset));
2732+
return rewriter.insertSyntheticToken(token, TokenType.SEMICOLON);
27512733
}
27522734

27532735
/// Report an error at the token after [token] that has the given [message].
@@ -4523,7 +4505,6 @@ class Parser {
45234505
token = typeParamOrArg.parseArguments(start, this);
45244506
if (optional('{', next)) {
45254507
if (typeParamOrArg.typeArgumentCount > 2) {
4526-
// TODO(danrubel): remove code in listeners which report this error
45274508
listener.handleRecoverableError(
45284509
fasta.messageSetOrMapLiteralTooManyTypeArguments,
45294510
start.next,
@@ -5364,34 +5345,27 @@ class Parser {
53645345
// Recovery
53655346
reportRecoverableError(
53665347
leftParenthesis, fasta.templateExpectedButGot.withArguments('('));
5367-
int offset = leftParenthesis.offset;
53685348

5369-
BeginToken openParen = forToken
5370-
.setNext(new SyntheticBeginToken(TokenType.OPEN_PAREN, offset));
5349+
BeginToken openParen = rewriter.insertToken(
5350+
forToken,
5351+
new SyntheticBeginToken(
5352+
TokenType.OPEN_PAREN, leftParenthesis.offset));
53715353

5372-
Token loopPart;
5354+
Token token;
53735355
if (awaitToken != null) {
5374-
loopPart = openParen.setNext(
5375-
new SyntheticStringToken(TokenType.IDENTIFIER, '', offset));
5376-
loopPart =
5377-
loopPart.setNext(new SyntheticKeywordToken(Keyword.IN, offset));
5378-
loopPart = loopPart.setNext(
5379-
new SyntheticStringToken(TokenType.IDENTIFIER, '', offset));
5356+
token = rewriter.insertSyntheticIdentifier(openParen);
5357+
token = rewriter.insertSyntheticKeyword(token, Keyword.IN);
5358+
token = rewriter.insertSyntheticIdentifier(token);
53805359
} else {
5381-
loopPart =
5382-
openParen.setNext(new SyntheticToken(TokenType.SEMICOLON, offset));
5383-
loopPart =
5384-
loopPart.setNext(new SyntheticToken(TokenType.SEMICOLON, offset));
5360+
token = rewriter.insertSyntheticToken(openParen, TokenType.SEMICOLON);
5361+
token = rewriter.insertSyntheticToken(token, TokenType.SEMICOLON);
53855362
}
53865363

5387-
Token closeParen =
5388-
loopPart.setNext(new SyntheticToken(TokenType.CLOSE_PAREN, offset));
5389-
openParen.endGroup = closeParen;
5390-
Token identifier = closeParen
5391-
.setNext(new SyntheticStringToken(TokenType.IDENTIFIER, '', offset));
5392-
Token semicolon =
5393-
identifier.setNext(new SyntheticToken(TokenType.SEMICOLON, offset));
5394-
semicolon.setNext(leftParenthesis);
5364+
openParen.endGroup = token = rewriter.insertToken(token,
5365+
new SyntheticToken(TokenType.CLOSE_PAREN, leftParenthesis.offset));
5366+
5367+
token = rewriter.insertSyntheticIdentifier(token);
5368+
rewriter.insertSyntheticToken(token, TokenType.SEMICOLON);
53955369

53965370
leftParenthesis = openParen;
53975371
}
@@ -5400,7 +5374,7 @@ class Parser {
54005374
// parses the metadata, modifiers, and type of a local variable
54015375
// declaration if it exists. This enables capturing [beforeIdentifier]
54025376
// for later error reporting.
5403-
return parseExpressionStatementOrDeclaration(forToken.next, true);
5377+
return parseExpressionStatementOrDeclaration(leftParenthesis, true);
54045378
}
54055379

54065380
/// Parse the remainder of the local variable declaration
@@ -5586,8 +5560,7 @@ class Parser {
55865560
if (!optional('while', whileToken)) {
55875561
reportRecoverableError(
55885562
whileToken, fasta.templateExpectedButGot.withArguments('while'));
5589-
whileToken = rewriter.insertToken(token,
5590-
new SyntheticKeywordToken(Keyword.WHILE, whileToken.charOffset));
5563+
whileToken = rewriter.insertSyntheticKeyword(token, Keyword.WHILE);
55915564
}
55925565
token = ensureParenthesizedCondition(whileToken);
55935566
token = ensureSemicolon(token);
@@ -5813,8 +5786,8 @@ class Parser {
58135786
rewriter.moveSynthetic(exceptionName, openParens.endGroup);
58145787
comma = null;
58155788
} else {
5816-
comma = rewriter.insertToken(exceptionName,
5817-
new SyntheticToken(TokenType.COMMA, comma.charOffset));
5789+
comma =
5790+
rewriter.insertSyntheticToken(exceptionName, TokenType.COMMA);
58185791
}
58195792
}
58205793
if (comma != null) {
@@ -6168,8 +6141,7 @@ class Parser {
61686141
next = next.next;
61696142
} else {
61706143
reportRecoverableError(next, fasta.messageMissingOperatorKeyword);
6171-
rewriter.insertToken(
6172-
beforeName, new SyntheticToken(Keyword.OPERATOR, next.offset));
6144+
rewriter.insertSyntheticKeyword(beforeName, Keyword.OPERATOR);
61736145
}
61746146

61756147
assert((next.isOperator && next.endGroup == null) ||
@@ -6265,9 +6237,7 @@ class Parser {
62656237
Token recoverFromStackOverflow(Token token) {
62666238
Token next = token.next;
62676239
reportRecoverableError(next, fasta.messageStackOverflow);
6268-
6269-
next = new SyntheticToken(TokenType.SEMICOLON, token.offset);
6270-
rewriter.insertToken(token, next);
6240+
next = rewriter.insertSyntheticToken(token, TokenType.SEMICOLON);
62716241
listener.handleEmptyStatement(next);
62726242

62736243
while (notEofOrValue('}', next)) {

pkg/front_end/lib/src/fasta/parser/token_stream_rewriter.dart

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ import '../scanner/error_token.dart' show UnmatchedToken;
77
import '../../scanner/token.dart'
88
show
99
BeginToken,
10+
Keyword,
1011
SimpleToken,
1112
SyntheticBeginToken,
13+
SyntheticKeywordToken,
1214
SyntheticStringToken,
1315
SyntheticToken,
1416
Token,
@@ -17,7 +19,7 @@ import '../../scanner/token.dart'
1719
/// Provides the capability of inserting tokens into a token stream. This
1820
/// implementation does this by rewriting the previous token to point to the
1921
/// inserted token.
20-
class TokenStreamRewriter {
22+
class TokenStreamRewriter with _TokenStreamMixin {
2123
// TODO(brianwilkerson):
2224
//
2325
// When we get to the point of removing `token.previous`, the plan is to
@@ -59,14 +61,6 @@ class TokenStreamRewriter {
5961
return leftParen;
6062
}
6163

62-
/// Insert a synthetic identifier after [token] and return the new identifier.
63-
Token insertSyntheticIdentifier(Token token) {
64-
return insertToken(
65-
token,
66-
new SyntheticStringToken(
67-
TokenType.IDENTIFIER, '', token.next.charOffset, 0));
68-
}
69-
7064
/// Insert [newToken] after [token] and return [newToken].
7165
Token insertToken(Token token, Token newToken) {
7266
newToken.setNext(token.next);
@@ -141,7 +135,9 @@ class TokenStreamRewriter {
141135
/// Provides the capability of adding tokens that lead into a token stream
142136
/// without modifying the original token stream and not setting the any token's
143137
/// `previous` field.
144-
class TokenStreamGhostWriter implements TokenStreamRewriter {
138+
class TokenStreamGhostWriter
139+
with _TokenStreamMixin
140+
implements TokenStreamRewriter {
145141
@override
146142
Token insertParens(Token token, bool includeIdentifier) {
147143
Token next = token.next;
@@ -161,14 +157,6 @@ class TokenStreamGhostWriter implements TokenStreamRewriter {
161157
return leftParen;
162158
}
163159

164-
/// Insert a synthetic identifier after [token] and return the new identifier.
165-
Token insertSyntheticIdentifier(Token token) {
166-
return insertToken(
167-
token,
168-
new SyntheticStringToken(
169-
TokenType.IDENTIFIER, '', token.next.charOffset, 0));
170-
}
171-
172160
@override
173161
Token insertToken(Token token, Token newToken) {
174162
newToken.next = token.next;
@@ -204,3 +192,26 @@ class TokenStreamGhostWriter implements TokenStreamRewriter {
204192
return current;
205193
}
206194
}
195+
196+
mixin _TokenStreamMixin {
197+
/// Insert a synthetic identifier after [token] and return the new identifier.
198+
Token insertSyntheticIdentifier(Token token, [String value]) {
199+
return insertToken(
200+
token,
201+
new SyntheticStringToken(
202+
TokenType.IDENTIFIER, value ?? '', token.next.charOffset, 0));
203+
}
204+
205+
/// Insert a new synthetic [keyword] after [token] and return the new token.
206+
Token insertSyntheticKeyword(Token token, Keyword keyword) => insertToken(
207+
token, new SyntheticKeywordToken(keyword, token.next.charOffset));
208+
209+
/// Insert a new simple synthetic token of [newTokenType] after [token]
210+
/// and return the new token.
211+
Token insertSyntheticToken(Token token, TokenType newTokenType) =>
212+
insertToken(
213+
token, new SyntheticToken(newTokenType, token.next.charOffset));
214+
215+
/// Insert [newToken] after [token] and return [newToken].
216+
Token insertToken(Token token, Token newToken);
217+
}

0 commit comments

Comments
 (0)