@@ -30,86 +30,6 @@ static boolean isAsciiDigit(int ch) {
3030 return ch >= '0' && ch <= '9' ;
3131 }
3232
33- static Token parseNumberToken (String value ) {
34- if (value .endsWith ("_" )) {
35- throw new IllegalArgumentException ("Invalid number literal: " + value );
36- }
37-
38- String clean = value .replaceAll ("_" , "" ); // Remove underscores
39- if (clean .isEmpty ()) {
40- throw new IllegalArgumentException ("Invalid number literal: " + value );
41- }
42-
43- if (clean .indexOf ('.' ) >= 0 || clean .indexOf ('e' ) >= 0 || clean .indexOf ('E' ) >= 0 ) {
44- int endIndex = clean .length ();
45-
46- FloatingType floatingType ;
47- if (clean .endsWith ("f" ) || clean .endsWith ("F" )) {
48- floatingType = FloatingType .FLOAT ;
49- endIndex -= 1 ;
50- } else if (clean .endsWith ("d" ) || clean .endsWith ("D" )) {
51- floatingType = FloatingType .DOUBLE ;
52- endIndex -= 1 ;
53- } else {
54- floatingType = FloatingType .DOUBLE ;
55- }
56-
57- return new Token .FloatingToken (Double .parseDouble (clean .substring (0 , endIndex )), floatingType );
58- } else {
59- int beginIndex ;
60- int radix ;
61- if (clean .startsWith ("0x" ) || clean .startsWith ("0X" )) {
62- beginIndex = 2 ;
63- radix = 16 ;
64- } else if (clean .startsWith ("0b" ) || clean .startsWith ("0B" )) {
65- beginIndex = 2 ;
66- radix = 2 ;
67- } else {
68- beginIndex = 0 ;
69- radix = 10 ;
70- }
71-
72- int endIndex = clean .length ();
73-
74- @ Nullable
75- Boolean unsignedSuffixChar = endIndex - beginIndex > 2 ? switch (clean .charAt (endIndex - 2 )) {
76- case 's' , 'S' -> false ;
77- case 'u' , 'U' -> true ;
78- default -> null ;
79- } : null ;
80-
81- @ Nullable
82- IntegralType typeSuffixChar = endIndex - beginIndex > 1 ? switch (clean .charAt (endIndex - 1 )) {
83- case 'b' , 'B' -> radix != 16 || unsignedSuffixChar != null ? IntegralType .BYTE : null ;
84- case 's' , 'S' -> IntegralType .SHORT ;
85- case 'i' , 'I' -> IntegralType .INT ;
86- case 'l' , 'L' -> IntegralType .LONG ;
87- default -> null ;
88- } : null ;
89-
90- if (typeSuffixChar != null ) {
91- endIndex -= 1 ;
92- }
93- if (unsignedSuffixChar != null ) {
94- endIndex -= 1 ;
95- }
96-
97- boolean unsigned = unsignedSuffixChar != null ? unsignedSuffixChar : radix != 10 ;
98- IntegralType type = typeSuffixChar != null ? typeSuffixChar : IntegralType .INT ;
99-
100- try {
101- long parsed = unsigned
102- ? Long .parseUnsignedLong (clean , beginIndex , endIndex , radix )
103- : Long .parseLong (clean , beginIndex , endIndex , radix );
104- type .check (parsed , unsigned );
105- return new Token .IntegralToken (parsed , type , unsigned );
106- } catch (NumberFormatException e ) {
107- throw new IllegalArgumentException ("Invalid number literal: " + value , e );
108- }
109- }
110-
111- }
112-
11333 private final CharSequence input ;
11434 private final int beginIndex ;
11535 private final int endIndex ;
@@ -338,7 +258,7 @@ private Token nextToken() {
338258 }
339259 }
340260
341- return parseNumberToken (input .subSequence (firstCharCursor , cursor ).toString ());
261+ return Token . NumberToken . parse (input .subSequence (firstCharCursor , cursor ).toString ());
342262 }
343263
344264 throw new UnsupportedOperationException ("Not implemented" ); // TODO
@@ -365,74 +285,4 @@ private Token nextToken() {
365285// }
366286// }
367287
368- enum IntegralType {
369- BYTE (Byte .MAX_VALUE , Byte .MIN_VALUE ),
370- SHORT (Short .MAX_VALUE , Short .MIN_VALUE ),
371- INT (Integer .MAX_VALUE , Integer .MIN_VALUE ),
372- LONG (Long .MAX_VALUE , Long .MIN_VALUE );
373-
374- final long maxSigned ;
375- final long maxUnsigned ;
376- final long min ;
377-
378- IntegralType (long maxSigned , long min ) {
379- this .maxSigned = maxSigned ;
380- this .maxUnsigned = maxSigned == Long .MAX_VALUE ? Long .MAX_VALUE : maxSigned - min ;
381- this .min = min ;
382- }
383-
384- void check (long value , boolean unsigned ) {
385- long max = unsigned ? maxUnsigned : maxSigned ;
386- if (value < min || value > max ) {
387- throw new IllegalArgumentException ("Value out of range: " + value + " (min: " + min + ", max: " + max + ")" );
388- }
389- }
390- }
391-
392- enum FloatingType {
393- FLOAT , DOUBLE
394- }
395-
396- sealed interface Token {
397- enum SimpleToken implements Token {
398- LEFT_BRACE , // {
399- RIGHT_BRACE , // }
400- LEFT_BRACKET , // [
401- RIGHT_BRACKET , // ]
402- COMMA , // ,
403- COLON , // :
404- DOT , // .
405- EOF
406- }
407-
408- enum ArrayBeginToken implements Token {
409- BYTE_ARRAY ,
410- INT_ARRAY ,
411- LONG_ARRAY ,
412- }
413-
414- enum BooleanToken implements Token {
415- TRUE (true ),
416- FALSE (false );
417-
418- private final boolean value ;
419-
420- BooleanToken (boolean value ) {
421- this .value = value ;
422- }
423- }
424-
425- record StringToken (String value ) implements Token {
426- }
427-
428- record IntegralToken (long value ,
429- IntegralType type ,
430- boolean unsigned ) implements Token {
431-
432- }
433-
434- record FloatingToken (double value , FloatingType type ) implements Token {
435-
436- }
437- }
438288}
0 commit comments