Skip to content

Numeric literals with multiple underscores are parsed as ERROR #227

@cubewhy

Description

@cubewhy

In Java, multiple underscores are allowed within numeric literals (e.g., 1____1234). However, the current grammar seems to fail when more than one underscore is used, resulting in an ERROR node in the syntax tree.

JLS 3.10.1

Source:

package org.example;

public class Main  {
    public static void main(String[] args) {
        int a = 1____1234;

        System.out.println(a);
    }
}

Actual Tree:

program [0, 0] - [9, 0]
  package_declaration [0, 0] - [0, 20]
    package [0, 0] - [0, 7]
    scoped_identifier [0, 8] - [0, 19]
      scope: identifier [0, 8] - [0, 11]
      . [0, 11] - [0, 12]
      name: identifier [0, 12] - [0, 19]
    ; [0, 19] - [0, 20]
  class_declaration [2, 0] - [8, 1]
    modifiers [2, 0] - [2, 6]
      public [2, 0] - [2, 6]
    class [2, 7] - [2, 12]
    name: identifier [2, 13] - [2, 17]
    body: class_body [2, 19] - [8, 1]
      { [2, 19] - [2, 20]
      method_declaration [3, 4] - [7, 5]
        modifiers [3, 4] - [3, 17]
          public [3, 4] - [3, 10]
          static [3, 11] - [3, 17]
        type: void_type [3, 18] - [3, 22]
        name: identifier [3, 23] - [3, 27]
        parameters: formal_parameters [3, 27] - [3, 42]
          ( [3, 27] - [3, 28]
          formal_parameter [3, 28] - [3, 41]
            type: array_type [3, 28] - [3, 36]
              element: type_identifier [3, 28] - [3, 34]
              dimensions: dimensions [3, 34] - [3, 36]
                [ [3, 34] - [3, 35]
                ] [3, 35] - [3, 36]
            name: identifier [3, 37] - [3, 41]
          ) [3, 41] - [3, 42]
        body: block [3, 43] - [7, 5]
          { [3, 43] - [3, 44]
          local_variable_declaration [4, 8] - [4, 26]
            type: integral_type [4, 8] - [4, 11]
              int [4, 8] - [4, 11]
            declarator: variable_declarator [4, 12] - [4, 25]
              name: identifier [4, 12] - [4, 13]
              = [4, 14] - [4, 15]
              ERROR [4, 16] - [4, 17] <---------------------------------------------------------------- ERROR Here
                decimal_integer_literal [4, 16] - [4, 17]
              value: identifier [4, 17] - [4, 25]
            ; [4, 25] - [4, 26]
          expression_statement [6, 8] - [6, 30]
            method_invocation [6, 8] - [6, 29]
              object: field_access [6, 8] - [6, 18]
                object: identifier [6, 8] - [6, 14]
                . [6, 14] - [6, 15]
                field: identifier [6, 15] - [6, 18]
              . [6, 18] - [6, 19]
              name: identifier [6, 19] - [6, 26]
              arguments: argument_list [6, 26] - [6, 29]
                ( [6, 26] - [6, 27]
                identifier [6, 27] - [6, 28]
                ) [6, 28] - [6, 29]
            ; [6, 29] - [6, 30]
          } [7, 4] - [7, 5]
      } [8, 0] - [8, 1]

Expected behavior: only decimal_integer_literal should be emitted

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions