@@ -219,12 +219,15 @@ private Statement doWhileStatement() {
219
219
220
220
private Statement forStatement () {
221
221
int foreachIndex = lookMatch (0 , TokenType .LPAREN ) ? 1 : 0 ;
222
- if (lookMatch (foreachIndex , TokenType .WORD ) && lookMatch (foreachIndex + 1 , TokenType .COLON )) {
222
+ if (lookMatch (foreachIndex , TokenType .WORD )
223
+ && lookMatch (foreachIndex + 1 , TokenType .COLON )) {
223
224
// for v : arr || for (v : arr)
224
225
return foreachArrayStatement ();
225
226
}
226
- if (lookMatch (foreachIndex , TokenType .WORD ) && lookMatch (foreachIndex + 1 , TokenType .COMMA )
227
- && lookMatch (foreachIndex + 2 , TokenType .WORD ) && lookMatch (foreachIndex + 3 , TokenType .COLON )) {
227
+ if (lookMatch (foreachIndex , TokenType .WORD )
228
+ && lookMatch (foreachIndex + 1 , TokenType .COMMA )
229
+ && lookMatch (foreachIndex + 2 , TokenType .WORD )
230
+ && lookMatch (foreachIndex + 3 , TokenType .COLON )) {
228
231
// for key, value : arr || for (key, value : arr)
229
232
return foreachMapStatement ();
230
233
}
@@ -242,23 +245,29 @@ && lookMatch(foreachIndex + 2, TokenType.WORD) && lookMatch(foreachIndex + 3, To
242
245
}
243
246
244
247
private ForeachArrayStatement foreachArrayStatement () {
248
+ // for x : arr
245
249
boolean optParentheses = match (TokenType .LPAREN );
246
250
final String variable = consume (TokenType .WORD ).getText ();
247
251
consume (TokenType .COLON );
248
252
final Expression container = expression ();
249
- if (optParentheses ) consume (TokenType .RPAREN ); // close opt parentheses
253
+ if (optParentheses ) {
254
+ consume (TokenType .RPAREN ); // close opt parentheses
255
+ }
250
256
final Statement statement = statementOrBlock ();
251
257
return new ForeachArrayStatement (variable , container , statement );
252
258
}
253
259
254
260
private ForeachMapStatement foreachMapStatement () {
261
+ // for k, v : map
255
262
boolean optParentheses = match (TokenType .LPAREN );
256
263
final String key = consume (TokenType .WORD ).getText ();
257
264
consume (TokenType .COMMA );
258
265
final String value = consume (TokenType .WORD ).getText ();
259
266
consume (TokenType .COLON );
260
267
final Expression container = expression ();
261
- if (optParentheses ) consume (TokenType .RPAREN ); // close opt parentheses
268
+ if (optParentheses ) {
269
+ consume (TokenType .RPAREN ); // close opt parentheses
270
+ }
262
271
final Statement statement = statementOrBlock ();
263
272
return new ForeachMapStatement (key , value , container , statement );
264
273
}
@@ -440,13 +449,14 @@ private Expression assignment() {
440
449
}
441
450
442
451
private Expression assignmentStrict () {
452
+ // x[0].prop += ...
443
453
final int position = pos ;
444
454
final Expression targetExpr = qualifiedName ();
445
455
if (!(targetExpr instanceof Accessible )) {
446
456
pos = position ;
447
457
return null ;
448
458
}
449
-
459
+
450
460
final TokenType currentType = get (0 ).getType ();
451
461
if (!ASSIGN_OPERATORS .containsKey (currentType )) {
452
462
pos = position ;
@@ -696,13 +706,15 @@ private Expression primary() {
696
706
}
697
707
698
708
if (match (TokenType .COLONCOLON )) {
709
+ // ::method reference
699
710
final String functionName = consume (TokenType .WORD ).getText ();
700
711
return new FunctionReferenceExpression (functionName );
701
712
}
702
713
if (match (TokenType .MATCH )) {
703
714
return match ();
704
715
}
705
716
if (match (TokenType .DEF )) {
717
+ // anonymous function def(args) ...
706
718
final Arguments arguments = arguments ();
707
719
final Statement statement = statementBody ();
708
720
return new ValueExpression (new UserDefinedFunction (arguments , statement ));
@@ -818,14 +830,18 @@ private Number createNumber(String text, int radix) {
818
830
819
831
private Token consume (TokenType type ) {
820
832
final Token current = get (0 );
821
- if (type != current .getType ()) throw new ParseException ("Token " + current + " doesn't match " + type );
833
+ if (type != current .getType ()) {
834
+ throw new ParseException ("Token " + current + " doesn't match " + type );
835
+ }
822
836
pos ++;
823
837
return current ;
824
838
}
825
839
826
840
private boolean match (TokenType type ) {
827
841
final Token current = get (0 );
828
- if (type != current .getType ()) return false ;
842
+ if (type != current .getType ()) {
843
+ return false ;
844
+ }
829
845
pos ++;
830
846
return true ;
831
847
}
0 commit comments