You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Joseph Athman (Jira)" <ji...@apache.org> on 2021/12/30 01:59:00 UTC

[jira] [Commented] (GROOVY-10152) Parrot can't parse closure

    [ https://issues.apache.org/jira/browse/GROOVY-10152?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17466658#comment-17466658 ] 

Joseph Athman commented on GROOVY-10152:
----------------------------------------

I ran into this same issue, and while I agree that the error message is extremely confusing, I think you do have a subtle bug in your code (same one I had).

 

This is the code you want which should compile:
{code:java}
static void groovy10152(IdentityRecord record, Map<String, String> normalized) {
        normalized.collectEntries { ctx, tr -> [(ctx): tr.value] }
    } {code}
Note the parenthesis around the key.

> Parrot can't parse closure
> --------------------------
>
>                 Key: GROOVY-10152
>                 URL: https://issues.apache.org/jira/browse/GROOVY-10152
>             Project: Groovy
>          Issue Type: Bug
>          Components: parser-antlr4
>    Affects Versions: 3.0.8
>            Reporter: Christopher Smith
>            Priority: Minor
>
> I have this "cleanup" method in an ETL project. The intent is to call a static method returning {{Try<String>}} that attempts to normalize a phone number, then check whether we had an error processing any of them; if so, return a failure, but if success, replace the existing phone numbers with the normalized ones and return the record.
> {code:groovy}
>     static Try<IdentityRecord> normalizePhones(IdentityRecord record) {
>         Map<String, Try<String>> normalized = record.phones.collectEntries { ctx, number ->
>             [ctx, PhoneRules.normalize(number)]
>         }
>         return normalized.entrySet().stream()
>             .filter { it.value.failure }
>             .findFirst()
>             .map { Try.failure(new ValidationException("${it.key}: ${it.value.cause.message}")) }
>             .orElseGet {
>                 record.phones = normalized.collectEntries { ctx, tr -> ctx, tr.value }
>                 Try.success(record)
>             }
>     }
> {code}
> Parrot is giving me a generic parse error on the closure for the {{orElseGet}} call, and I can't figure out why. Commenting out the assignment statement removes the error (but of course prevents the intended modification). Adding {{_ ->}} does not change the error, and neither does inserting semicolons in various places.
> {code}
> Groovy:Unexpected input: '{'
> {code}
> Update: It appears that my attempted more-minimal workaround still triggers the problem:
> {code:groovy}
>     static void groovy10152(IdentityRecord record, Map<String, String> normalized) {
>         normalized.collectEntries { ctx, tr -> ctx, tr.value }
>     }
> {code}
> results in the same error at the opening of the closure:
> {code}
> Groovy:Unexpected input: '{'
> {code}
> (And now, finally, I am realizing that what's confusing it is the comma expression at the end. While this *should* be legal syntax and result in a type-checking error, it's causing a parser error instead. Even in this case, though, the grammar appears to be causing the error to be reported at a substantially incorrect location. Marking the priority down since fixing the semantic error also fixes the parser error.)



--
This message was sent by Atlassian Jira
(v8.20.1#820001)