Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-10236

Parser fails to parse a single-statement lambda when omitting parentheses for method call

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 4.0.0-beta-1
    • 4.0.0-beta-2
    • parser, parser-antlr4
    • None

    Description

      The parser fails to parse the following code:

      class Test {
      
          public void test() {
              [1, 2, 3, 4, 5].stream().forEach(num -> println "$num");
          }
      
      }
      

      The produced error is:

      Exception in thread "main" org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
      Script_30c2a85fbeaac50f95df1e8ce776b5dd.groovy: 4: Unexpected input: '(' @ line 4, column 41.
         , 2, 3, 4, 5].stream().forEach(num -> pr
                                       ^
      

      This problem appears only after 4.0.0-beta-1.
      The following expressions are parsed successfully:

      [1, 2, 3, 4, 5].stream().forEach(num -> { println "$num" });
      [1, 2, 3, 4, 5].stream().forEach(num -> println("$num"));
      

      There is also something curious: if I modify the original code and wrap the parameter num in parentheses (.forEach((num) -> println "$num")), the parser does not complain, but println is recognized as a variable rather than a method.

      Attachments

        Activity

          People

            daniel_sun Daniel Sun
            latanasov Lyuben Atanasov
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: