You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Paul King (Jira)" <ji...@apache.org> on 2023/11/15 06:25:00 UTC

[jira] [Commented] (GROOVY-11222) Cannot parse methods that return types starting with lowercase letters

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

Paul King commented on GROOVY-11222:
------------------------------------

[~blackdrag], I wasn't sure which change you were referring to?

I remember the pre-Groovy 2 changes for resolving, e.g. we didn't look up lowercase names for star imports, requiring an explicit import for that case (but then allowing the lowercase in most places).

I also remember a Groovy 3 change which I think was needed for the grammar (I believe for efficiency reasons but it might have been an ambiguity issue) but I believe was only for method return type:
[https://github.com/apache/groovy/commit/d3c68531]

The fix for that as mentioned is an import alias which can also be done in the same file (in case that wasn't obvious in Jochen's earlier comment):
{code:java}
record person(String firstName, String lastName) { }

import person as Person

Person createPerson(String fn, String ln) {
    new Person(fn, ln)
}
{code}
I thought that was the issue at play here but perhaps there was another issue as well?

If it's just the issue above, then the impact on DSLs only impacts places where you need strongly-typed return types for method declarations. In some cases, you could use dynamic return types or extract method definitions to separate files to keep such noise out of your DSLs.


In any case, we wouldn't role back that change unless we found some other way to make that case very efficient.

> Cannot parse methods that return types starting with lowercase letters
> ----------------------------------------------------------------------
>
>                 Key: GROOVY-11222
>                 URL: https://issues.apache.org/jira/browse/GROOVY-11222
>             Project: Groovy
>          Issue Type: Bug
>          Components: parser
>    Affects Versions: 5.0.0-alpha-2, 4.0.15
>            Reporter: Lyuben Atanasov
>            Assignee: Jochen Theodorou
>            Priority: Major
>
> According to the language specification, it is allowed to have class names that start with a lowercase letter:
> {quote}Identifiers start with a letter, a dollar or an underscore. They cannot start with a number.
> A letter can be in the following ranges:
>  * 'a' to 'z' (lowercase ascii letter)
>  * 'A' to 'Z' (uppercase ascii letter)
>  * '\u00C0' to '\u00D6'
>  * '\u00D8' to '\u00F6'
>  * '\u00F8' to '\u00FF'
>  * '\u0100' to '\uFFFE'{quote}
> This is the same as in Java.
> Defining types that start with lowercase letters works fine. As far as I can tell, I can also use such types as method arguments. What I cannot do, is to have methods that return types starting with lowercase letters.
> Here's a failing example:
> {code:groovy}
> public class person {
>     private String firstName;
>     private String lastName;
>     
>     person(String firstName, String lastName) {
>         this.firstName = firstName;
>         this.lastName = lastName;
>     }
> }
> public class PersonFactory {
>     public person createPerson(String firstName, String lastName) {
>         return new person(firstName, lastName);
>     }
>     
> }
> {code}
> When parsing this code, it fails with the following error:
> {noformat}
> Script_b41cda4fe6804e170255542089871d79.groovy: 13: Unexpected input: '(' @ line 13, column 31.
>        public person createPerson(String firstName, String lastName) {
>                                  ^
> 1 error
> {noformat}
> If I change the class name to start with a capital letter, everything works:
> {code:groovy}
> public class Person {
>     private String firstName;
>     private String lastName;
>     
>     Person(String firstName, String lastName) {
>         this.firstName = firstName;
>         this.lastName = lastName;
>     }
> }
> public class PersonFactory {
>     public Person createPerson(String firstName, String lastName) {
>         return new Person(firstName, lastName);
>     }
>     
> }
> {code}
> So it looks like there is a discrepancy between the language specification and the grammar definition. I looked into the grammar and managed to extract the following relevant lines from it, which should make the issue evident:
> {code:java}
> methodDeclaration[int t, int ct]
>     :   modifiersOpt typeParameters? (returnType[$ct] nls)?
>         methodName formalParameters
>         (
>             DEFAULT nls elementValue
>         |
>             (nls THROWS nls qualifiedClassNameList)?
>             (nls methodBody)?
>         )?
>     ;
>     
> returnType[int ct]
>     :
>         standardType
>     |   VOID
>     ;
>     
> standardType
> options { baseContext = type; }
>     :   annotationsOpt
>         (
>             primitiveType
>         |
>             standardClassOrInterfaceType
>         )
>         emptyDimsOpt
>     ;
>     
> standardClassOrInterfaceType
> options { baseContext = classOrInterfaceType; }
>     :   qualifiedStandardClassName typeArguments?
>     ;
>     
> qualifiedStandardClassName
>     :   qualifiedNameElements className (DOT className)*
>     ;
>     
> className
>     :   CapitalizedIdentifier
>     ;
> {code}
> Here, it is allowed only to have a CapitalizedIdentifier as the class name. There is an obvious simple fix:
> {code:java}
> className
>     :   CapitalizedIdentifier | Identifier
>     ;
> {code}
> This should make the parser work in the same way as expected after reading the language spec. I am not sure, however, whether this won't break anything else.
> In order to avoid any unnecessary discussions about naming conventions: I am aware that by convention it is not OK to start a type name with anything other than a capital letter, but there are valid cases where it is desired to have such names (e.g. generated code based on external input, DSLs).



--
This message was sent by Atlassian Jira
(v8.20.10#820010)