Skip to content

Commit 813b0d0

Browse files
bwilkersoncommit-bot@chromium.org
authored andcommitted
Rename a parse method and make it consistent with newer methods
Change-Id: I3cde98229649557eb32a291afeab7558943cf140 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/171631 Commit-Queue: Brian Wilkerson <[email protected]> Reviewed-by: Phil Quitslund <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent 546fdf5 commit 813b0d0

File tree

3 files changed

+50
-37
lines changed

3 files changed

+50
-37
lines changed

pkg/analysis_server/lib/src/services/correction/fix/data_driven/code_fragment_parser.dart

Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ class CodeFragmentParser {
2727
/// The index in the [tokens] of the next token to be consumed.
2828
int currentIndex = 0;
2929

30-
/// The accessors that have been parsed.
31-
List<Accessor> accessors = [];
32-
3330
/// Initialize a newly created parser to report errors to the [errorReporter].
3431
CodeFragmentParser(this.errorReporter, {VariableScope scope})
3532
: variableScope = scope ?? VariableScope(null, {});
@@ -51,18 +48,29 @@ class CodeFragmentParser {
5148
///
5249
/// <content> ::=
5350
/// <accessor> ('.' <accessor>)*
54-
List<Accessor> parse(String content, int delta) {
51+
List<Accessor> parseAccessors(String content, int delta) {
5552
this.delta = delta;
5653
tokens = _CodeFragmentScanner(content, delta, errorReporter).scan();
5754
if (tokens == null) {
5855
// The error has already been reported.
5956
return null;
6057
}
61-
var index = _parseAccessor(0);
62-
while (index < tokens.length) {
63-
var token = tokens[index];
58+
currentIndex = 0;
59+
var accessors = <Accessor>[];
60+
var accessor = _parseAccessor();
61+
if (accessor == null) {
62+
return accessors;
63+
}
64+
accessors.add(accessor);
65+
while (currentIndex < tokens.length) {
66+
var token = currentToken;
6467
if (token.kind == _TokenKind.period) {
65-
index = _parseAccessor(index + 1);
68+
advance();
69+
accessor = _parseAccessor();
70+
if (accessor == null) {
71+
return accessors;
72+
}
73+
accessors.add(accessor);
6674
} else {
6775
errorReporter.reportErrorForOffset(TransformSetErrorCode.wrongToken,
6876
token.offset + delta, token.length, ['.', token.kind.displayName]);
@@ -95,10 +103,9 @@ class CodeFragmentParser {
95103
return expression;
96104
}
97105

98-
/// Return the token at the given [index] if it exists and if it has one of
99-
/// the [validKinds]. Report an error and return `null` if those conditions
100-
/// aren't met.
101-
_Token _expect(int index, List<_TokenKind> validKinds) {
106+
/// Return the current token if it exists and has one of the [validKinds].
107+
/// Report an error and return `null` if those conditions aren't met.
108+
_Token _expect(List<_TokenKind> validKinds) {
102109
String validKindsDisplayString() {
103110
var buffer = StringBuffer();
104111
for (var i = 0; i < validKinds.length; i++) {
@@ -114,7 +121,8 @@ class CodeFragmentParser {
114121
return buffer.toString();
115122
}
116123

117-
if (index >= tokens.length) {
124+
var token = currentToken;
125+
if (token == null) {
118126
var offset = 0;
119127
var length = 0;
120128
if (tokens.isNotEmpty) {
@@ -126,7 +134,6 @@ class CodeFragmentParser {
126134
offset + delta, length, [validKindsDisplayString()]);
127135
return null;
128136
}
129-
var token = tokens[index];
130137
if (!validKinds.contains(token.kind)) {
131138
errorReporter.reportErrorForOffset(
132139
TransformSetErrorCode.wrongToken,
@@ -142,23 +149,25 @@ class CodeFragmentParser {
142149
///
143150
/// <accessor> ::=
144151
/// <identifier> '[' (<integer> | <identifier>) ']'
145-
int _parseAccessor(int index) {
146-
var token = _expect(index, const [_TokenKind.identifier]);
152+
Accessor _parseAccessor() {
153+
var token = _expect(const [_TokenKind.identifier]);
147154
if (token == null) {
148155
// The error has already been reported.
149-
return tokens.length;
156+
return null;
150157
}
151158
var identifier = token.lexeme;
152159
if (identifier == 'arguments') {
153-
token = _expect(index + 1, const [_TokenKind.openSquareBracket]);
160+
advance();
161+
token = _expect(const [_TokenKind.openSquareBracket]);
154162
if (token == null) {
155163
// The error has already been reported.
156-
return tokens.length;
164+
return null;
157165
}
158-
token = _expect(index + 2, [_TokenKind.identifier, _TokenKind.integer]);
166+
advance();
167+
token = _expect(const [_TokenKind.identifier, _TokenKind.integer]);
159168
if (token == null) {
160169
// The error has already been reported.
161-
return tokens.length;
170+
return null;
162171
}
163172
ParameterReference reference;
164173
if (token.kind == _TokenKind.identifier) {
@@ -167,36 +176,40 @@ class CodeFragmentParser {
167176
var argumentIndex = int.parse(token.lexeme);
168177
reference = PositionalParameterReference(argumentIndex);
169178
}
170-
token = _expect(index + 3, [_TokenKind.closeSquareBracket]);
179+
advance();
180+
token = _expect(const [_TokenKind.closeSquareBracket]);
171181
if (token == null) {
172182
// The error has already been reported.
173-
return tokens.length;
183+
return null;
174184
}
175-
accessors.add(ArgumentAccessor(reference));
176-
return index + 4;
185+
advance();
186+
return ArgumentAccessor(reference);
177187
} else if (identifier == 'typeArguments') {
178-
token = _expect(index + 1, const [_TokenKind.openSquareBracket]);
188+
advance();
189+
token = _expect(const [_TokenKind.openSquareBracket]);
179190
if (token == null) {
180191
// The error has already been reported.
181-
return tokens.length;
192+
return null;
182193
}
183-
token = _expect(index + 2, [_TokenKind.integer]);
194+
advance();
195+
token = _expect(const [_TokenKind.integer]);
184196
if (token == null) {
185197
// The error has already been reported.
186-
return tokens.length;
198+
return null;
187199
}
200+
advance();
188201
var argumentIndex = int.parse(token.lexeme);
189-
token = _expect(index + 3, [_TokenKind.closeSquareBracket]);
202+
token = _expect(const [_TokenKind.closeSquareBracket]);
190203
if (token == null) {
191204
// The error has already been reported.
192-
return tokens.length;
205+
return null;
193206
}
194-
accessors.add(TypeArgumentAccessor(argumentIndex));
195-
return index + 4;
207+
advance();
208+
return TypeArgumentAccessor(argumentIndex);
196209
} else {
197210
errorReporter.reportErrorForOffset(TransformSetErrorCode.unknownAccessor,
198211
token.offset + delta, token.length, [identifier]);
199-
return tokens.length;
212+
return null;
200213
}
201214
}
202215

pkg/analysis_server/lib/src/services/correction/fix/data_driven/transform_set_parser.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ class TransformSetParser {
451451
return null;
452452
}
453453
var accessors = CodeFragmentParser(errorReporter)
454-
.parse(value, _offsetOfString(valueNode));
454+
.parseAccessors(value, _offsetOfString(valueNode));
455455
if (accessors == null) {
456456
// The error has already been reported.
457457
return null;

pkg/analysis_server/test/src/services/correction/fix/data_driven/code_fragment_parser_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ abstract class AbstractCodeFragmentParserTest {
2727
List<Accessor> assertErrors(
2828
String content, List<ExpectedError> expectedErrors) {
2929
var errorListener = GatheringErrorListener();
30-
var accessors = _parser(errorListener).parse(content, 0);
30+
var accessors = _parser(errorListener).parseAccessors(content, 0);
3131
errorListener.assertErrors(expectedErrors);
3232
return accessors;
3333
}
@@ -43,7 +43,7 @@ abstract class AbstractCodeFragmentParserTest {
4343

4444
List<Accessor> assertNoErrors(String content) {
4545
var errorListener = GatheringErrorListener();
46-
var accessors = _parser(errorListener).parse(content, 0);
46+
var accessors = _parser(errorListener).parseAccessors(content, 0);
4747
errorListener.assertNoErrors();
4848
return accessors;
4949
}

0 commit comments

Comments
 (0)