@@ -27,9 +27,6 @@ class CodeFragmentParser {
27
27
/// The index in the [tokens] of the next token to be consumed.
28
28
int currentIndex = 0 ;
29
29
30
- /// The accessors that have been parsed.
31
- List <Accessor > accessors = [];
32
-
33
30
/// Initialize a newly created parser to report errors to the [errorReporter] .
34
31
CodeFragmentParser (this .errorReporter, {VariableScope scope})
35
32
: variableScope = scope ?? VariableScope (null , {});
@@ -51,18 +48,29 @@ class CodeFragmentParser {
51
48
///
52
49
/// <content> ::=
53
50
/// <accessor> ('.' <accessor>)*
54
- List <Accessor > parse (String content, int delta) {
51
+ List <Accessor > parseAccessors (String content, int delta) {
55
52
this .delta = delta;
56
53
tokens = _CodeFragmentScanner (content, delta, errorReporter).scan ();
57
54
if (tokens == null ) {
58
55
// The error has already been reported.
59
56
return null ;
60
57
}
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;
64
67
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);
66
74
} else {
67
75
errorReporter.reportErrorForOffset (TransformSetErrorCode .wrongToken,
68
76
token.offset + delta, token.length, ['.' , token.kind.displayName]);
@@ -95,10 +103,9 @@ class CodeFragmentParser {
95
103
return expression;
96
104
}
97
105
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) {
102
109
String validKindsDisplayString () {
103
110
var buffer = StringBuffer ();
104
111
for (var i = 0 ; i < validKinds.length; i++ ) {
@@ -114,7 +121,8 @@ class CodeFragmentParser {
114
121
return buffer.toString ();
115
122
}
116
123
117
- if (index >= tokens.length) {
124
+ var token = currentToken;
125
+ if (token == null ) {
118
126
var offset = 0 ;
119
127
var length = 0 ;
120
128
if (tokens.isNotEmpty) {
@@ -126,7 +134,6 @@ class CodeFragmentParser {
126
134
offset + delta, length, [validKindsDisplayString ()]);
127
135
return null ;
128
136
}
129
- var token = tokens[index];
130
137
if (! validKinds.contains (token.kind)) {
131
138
errorReporter.reportErrorForOffset (
132
139
TransformSetErrorCode .wrongToken,
@@ -142,23 +149,25 @@ class CodeFragmentParser {
142
149
///
143
150
/// <accessor> ::=
144
151
/// <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]);
147
154
if (token == null ) {
148
155
// The error has already been reported.
149
- return tokens.length ;
156
+ return null ;
150
157
}
151
158
var identifier = token.lexeme;
152
159
if (identifier == 'arguments' ) {
153
- token = _expect (index + 1 , const [_TokenKind .openSquareBracket]);
160
+ advance ();
161
+ token = _expect (const [_TokenKind .openSquareBracket]);
154
162
if (token == null ) {
155
163
// The error has already been reported.
156
- return tokens.length ;
164
+ return null ;
157
165
}
158
- token = _expect (index + 2 , [_TokenKind .identifier, _TokenKind .integer]);
166
+ advance ();
167
+ token = _expect (const [_TokenKind .identifier, _TokenKind .integer]);
159
168
if (token == null ) {
160
169
// The error has already been reported.
161
- return tokens.length ;
170
+ return null ;
162
171
}
163
172
ParameterReference reference;
164
173
if (token.kind == _TokenKind .identifier) {
@@ -167,36 +176,40 @@ class CodeFragmentParser {
167
176
var argumentIndex = int .parse (token.lexeme);
168
177
reference = PositionalParameterReference (argumentIndex);
169
178
}
170
- token = _expect (index + 3 , [_TokenKind .closeSquareBracket]);
179
+ advance ();
180
+ token = _expect (const [_TokenKind .closeSquareBracket]);
171
181
if (token == null ) {
172
182
// The error has already been reported.
173
- return tokens.length ;
183
+ return null ;
174
184
}
175
- accessors. add ( ArgumentAccessor (reference) );
176
- return index + 4 ;
185
+ advance ( );
186
+ return ArgumentAccessor (reference) ;
177
187
} else if (identifier == 'typeArguments' ) {
178
- token = _expect (index + 1 , const [_TokenKind .openSquareBracket]);
188
+ advance ();
189
+ token = _expect (const [_TokenKind .openSquareBracket]);
179
190
if (token == null ) {
180
191
// The error has already been reported.
181
- return tokens.length ;
192
+ return null ;
182
193
}
183
- token = _expect (index + 2 , [_TokenKind .integer]);
194
+ advance ();
195
+ token = _expect (const [_TokenKind .integer]);
184
196
if (token == null ) {
185
197
// The error has already been reported.
186
- return tokens.length ;
198
+ return null ;
187
199
}
200
+ advance ();
188
201
var argumentIndex = int .parse (token.lexeme);
189
- token = _expect (index + 3 , [_TokenKind .closeSquareBracket]);
202
+ token = _expect (const [_TokenKind .closeSquareBracket]);
190
203
if (token == null ) {
191
204
// The error has already been reported.
192
- return tokens.length ;
205
+ return null ;
193
206
}
194
- accessors. add ( TypeArgumentAccessor (argumentIndex) );
195
- return index + 4 ;
207
+ advance ( );
208
+ return TypeArgumentAccessor (argumentIndex) ;
196
209
} else {
197
210
errorReporter.reportErrorForOffset (TransformSetErrorCode .unknownAccessor,
198
211
token.offset + delta, token.length, [identifier]);
199
- return tokens.length ;
212
+ return null ;
200
213
}
201
214
}
202
215
0 commit comments