@@ -50,6 +50,14 @@ public static Node parse(List<Token> tokens) {
5050 ASSIGN_OPERATORS .put (TokenType .ATEQ , BinaryExpression .Operator .AT );
5151 }
5252
53+ private static final EnumSet <TokenType > NUMBER_TOKEN_TYPES = EnumSet .of (
54+ TokenType .NUMBER ,
55+ TokenType .LONG_NUMBER ,
56+ TokenType .DECIMAL_NUMBER ,
57+ TokenType .HEX_NUMBER ,
58+ TokenType .HEX_LONG_NUMBER
59+ );
60+
5361 private final List <Token > tokens ;
5462 private final int size ;
5563 private final ParseErrors parseErrors ;
@@ -347,7 +355,7 @@ private Node functionChain(Node qualifiedNameExpr) {
347355 }
348356 if (lookMatch (0 , TokenType .DOT )) {
349357 final List <Node > indices = variableSuffix ();
350- if (indices == null || indices .isEmpty ()) {
358+ if (indices .isEmpty ()) {
351359 return expr ;
352360 }
353361
@@ -411,20 +419,10 @@ private MatchExpression match() {
411419 consume (TokenType .CASE );
412420 MatchExpression .Pattern pattern = null ;
413421 final Token current = get (0 );
414- if (match (TokenType .NUMBER )) {
415- // case 20:
416- pattern = new MatchExpression .ConstantPattern (
417- NumberValue .of (createNumber (current .text (), 10 ))
418- );
419- } else if (match (TokenType .DECIMAL_NUMBER )) {
420- // case 0.5:
421- pattern = new MatchExpression .ConstantPattern (
422- NumberValue .of (createDecimalNumber (current .text ()))
423- );
424- } else if (match (TokenType .HEX_NUMBER )) {
425- // case #FF:
422+ if (isNumberToken (current .type ())) {
423+ // case 20: / case 0.5: / case #FF:
426424 pattern = new MatchExpression .ConstantPattern (
427- NumberValue .of (createNumber (current . text (), 16 ))
425+ NumberValue .of (getAsNumber (current ))
428426 );
429427 } else if (match (TokenType .TEXT )) {
430428 // case "text":
@@ -859,7 +857,7 @@ private Node qualifiedName() {
859857 final List <Node > indices = variableSuffix ();
860858 final var variable = new VariableExpression (current .text ());
861859 variable .setRange (getRange (startTokenIndex , index - 1 ));
862- if (indices == null || indices .isEmpty ()) {
860+ if (indices .isEmpty ()) {
863861 return variable ;
864862 } else {
865863 return new ContainerAccessExpression (variable , indices , variable .getRange ());
@@ -869,7 +867,7 @@ private Node qualifiedName() {
869867 private List <Node > variableSuffix () {
870868 // .key1.arr1[expr1][expr2].key2
871869 if (!lookMatch (0 , TokenType .DOT ) && !lookMatch (0 , TokenType .LBRACKET )) {
872- return null ;
870+ return Collections . emptyList () ;
873871 }
874872 final List <Node > indices = new ArrayList <>();
875873 while (lookMatch (0 , TokenType .DOT ) || lookMatch (0 , TokenType .LBRACKET )) {
@@ -888,47 +886,72 @@ private List<Node> variableSuffix() {
888886
889887 private Node value () {
890888 final Token current = get (0 );
891- if (match (TokenType .NUMBER )) {
892- return new ValueExpression (createNumber (current .text (), 10 ));
893- }
894- if (match (TokenType .DECIMAL_NUMBER )) {
895- return new ValueExpression (createDecimalNumber (current .text ()));
896- }
897- if (match (TokenType .HEX_NUMBER )) {
898- return new ValueExpression (createNumber (current .text (), 16 ));
889+ if (isNumberToken (current .type ())) {
890+ return new ValueExpression (getAsNumber (current ));
899891 }
900892 if (match (TokenType .TEXT )) {
901893 final ValueExpression strExpr = new ValueExpression (current .text ());
902894 // "text".property || "text".func()
903895 if (lookMatch (0 , TokenType .DOT )) {
904- if (lookMatch (1 , TokenType .WORD ) && lookMatch (2 , TokenType .LPAREN )) {
905- match (TokenType .DOT );
906- return functionChain (new ContainerAccessExpression (
907- strExpr ,
908- Collections .singletonList (new ValueExpression (consume (TokenType .WORD ).text ())),
909- getRange ()
910- ));
911- }
912- final List <Node > indices = variableSuffix ();
913- if (indices == null || indices .isEmpty ()) {
914- return strExpr ;
915- }
916- return new ContainerAccessExpression (strExpr , indices , getRange ());
896+ return stringProperty (strExpr );
917897 }
918898 return strExpr ;
919899 }
920900 throw error ("Unknown expression: " + current );
921901 }
922902
903+ private Node stringProperty (ValueExpression strExpr ) {
904+ if (lookMatch (1 , TokenType .WORD ) && lookMatch (2 , TokenType .LPAREN )) {
905+ match (TokenType .DOT );
906+ return functionChain (new ContainerAccessExpression (
907+ strExpr ,
908+ Collections .singletonList (new ValueExpression (consume (TokenType .WORD ).text ())),
909+ getRange ()
910+ ));
911+ }
912+ final List <Node > indices = variableSuffix ();
913+ if (indices .isEmpty ()) {
914+ return strExpr ;
915+ }
916+ return new ContainerAccessExpression (strExpr , indices , getRange ());
917+ }
918+
919+ private boolean isNumberToken (TokenType type ) {
920+ return NUMBER_TOKEN_TYPES .contains (type );
921+ }
922+
923+ private Number getAsNumber (Token current ) {
924+ if (match (TokenType .NUMBER )) {
925+ return createNumber (current .text (), 10 );
926+ }
927+ if (match (TokenType .LONG_NUMBER )) {
928+ return createLongNumber (current .text (), 10 );
929+ }
930+ if (match (TokenType .DECIMAL_NUMBER )) {
931+ return createDecimalNumber (current .text ());
932+ }
933+ if (match (TokenType .HEX_NUMBER )) {
934+ return createNumber (current .text (), 16 );
935+ }
936+ if (match (TokenType .HEX_LONG_NUMBER )) {
937+ return createLongNumber (current .text (), 16 );
938+ }
939+ throw error ("Unknown number expression: " + current );
940+ }
941+
923942 private Number createNumber (String text , int radix ) {
924943 // Integer
925944 try {
926945 return Integer .parseInt (text , radix );
927946 } catch (NumberFormatException nfe ) {
928- return Long . parseLong (text , radix );
947+ return createLongNumber (text , radix );
929948 }
930949 }
931950
951+ private Number createLongNumber (String text , int radix ) {
952+ return Long .parseLong (text , radix );
953+ }
954+
932955 private Number createDecimalNumber (String text ) {
933956 // Double
934957 return Double .parseDouble (text );
0 commit comments