@@ -38,7 +38,8 @@ public class StringFormat {
38
38
39
39
private static final String SYNTAX_ERROR_MESSAGE = "Fix this formatted string's syntax." ;
40
40
41
- private static final BiConsumer <SubscriptionContext , Expression > DO_NOTHING_VALIDATOR = (ctx , expr ) -> {};
41
+ private static final BiConsumer <SubscriptionContext , Expression > DO_NOTHING_VALIDATOR = (ctx , expr ) -> {
42
+ };
42
43
43
44
// See https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting
44
45
private static final Pattern PRINTF_PARAMETER_PATTERN = Pattern .compile (
@@ -193,26 +194,22 @@ public StrFormatParser(Consumer<String> issueReporter, String value) {
193
194
this .pos = 0 ;
194
195
}
195
196
196
-
197
197
public Optional <StringFormat > parse () {
198
198
pos = 0 ;
199
199
result = new ArrayList <>();
200
200
201
201
while (pos < value .length ()) {
202
202
char current = value .charAt (pos );
203
- switch (state ) {
204
- case INIT -> {
205
- if (!tryParsingInitial (current )) {
206
- return Optional .empty ();
207
- }
203
+ if (state == ParseState .INIT ) {
204
+ if (!tryParsingInitial (current )) {
205
+ return Optional .empty ();
208
206
}
209
- case RCURLY -> {
210
- if (current != '}' ) {
211
- issueReporter .accept (SYNTAX_ERROR_MESSAGE );
212
- return Optional .empty ();
213
- }
214
- state = ParseState .INIT ;
207
+ } else if (state == ParseState .RCURLY ) {
208
+ if (current != '}' ) {
209
+ issueReporter .accept (SYNTAX_ERROR_MESSAGE );
210
+ return Optional .empty ();
215
211
}
212
+ state = ParseState .INIT ;
216
213
}
217
214
218
215
pos += 1 ;
@@ -263,7 +260,6 @@ private boolean tryParsingField() {
263
260
return successful ;
264
261
}
265
262
266
-
267
263
public void reportIssue (String issue ) {
268
264
issueReporter .accept (issue );
269
265
}
@@ -331,7 +327,7 @@ public boolean tryParse() {
331
327
case FINISHED -> throw new IllegalStateException ("Unexpected value: " + state );
332
328
};
333
329
334
- if (!successful ) {
330
+ if (!successful ) {
335
331
return false ;
336
332
}
337
333
@@ -342,7 +338,7 @@ public boolean tryParse() {
342
338
}
343
339
344
340
private boolean checkParserState () {
345
- if (state != FieldParseState .FINISHED ) {
341
+ if (state != FieldParseState .FINISHED ) {
346
342
parent .reportIssue (SYNTAX_ERROR_MESSAGE );
347
343
return false ;
348
344
}
@@ -351,7 +347,7 @@ private boolean checkParserState() {
351
347
352
348
private boolean tryParseFormatSpecifier (char current ) {
353
349
if (current == '{' ) {
354
- if (!tryParsingNestedField ()) {
350
+ if (!tryParsingNestedField ()) {
355
351
return false ;
356
352
}
357
353
} else if (current == '}' ) {
@@ -362,7 +358,7 @@ private boolean tryParseFormatSpecifier(char current) {
362
358
}
363
359
364
360
private boolean tryParsingNestedField () {
365
- if (this .nesting > 0 ) {
361
+ if (this .nesting > 0 ) {
366
362
parent .reportIssue ("Fix this formatted string's syntax; Deep nesting is not allowed." );
367
363
return false ;
368
364
}
@@ -431,7 +427,6 @@ private void addCurrentField() {
431
427
}
432
428
}
433
429
434
-
435
430
public static Optional <StringFormat > createFromStrFormatStyle (Consumer <String > issueReporter , String value ) {
436
431
// Format -> '{' [FieldName] ['!' Conversion] [':' FormatSpec*] '}'
437
432
// FormatSpec -> '{' [FieldName] '}' | Character
@@ -526,7 +521,8 @@ private static BiConsumer<SubscriptionContext, Expression> printfConversionValid
526
521
}
527
522
528
523
// No case for '%s', '%r' and '%a' - anything can be formatted with those.
529
- return (ctx , expression ) -> {};
524
+ return (ctx , expression ) -> {
525
+ };
530
526
}
531
527
532
528
private static boolean cannotBeOfType (Expression expression , String ... types ) {
0 commit comments