j-brant / SmaCC

MIT License
24 stars 16 forks source link

Can't parse default method in Java Interface #9

Closed badetitou closed 5 years ago

badetitou commented 5 years ago

Hello, it is impossible to parse default method in Java inteface.

For example, it is possible de parse

interface TestInterface1 
{ 
    // default method 
    default void show() 
    { 
        System.out.println("Default TestInterface1"); 
    } 
} 

But not

interface TestInterface1 
{ 
    // default method 
    public default void show() 
    { 
        System.out.println("Default TestInterface1"); 
    } 
} 

In the second example, I had the 'public' modifier. It is possible (see: https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html) in section "Static Methods"

I thought the error is probably there

interface_method_declaration 
    : interface_method_header block 'body' {{MethodDeclaration}}
    ;    
interface_method_header 
    : interface_modifiers_opt type_parameters_opt 'typeParameters' type 'type' method_declarator 'declarator' throws_opt 'throws'
    | interface_modifiers_opt type_parameters_opt 'typeParameters' void 'type' method_declarator 'declarator' throws_opt 'throws'
    ;
interface_modifiers_opt
    : modifiers_opt
    | default_modifier 'modifier'
    ;
default_modifier 
    : "default" 'token' {{Modifier}}

But I need help to solve it

If someone has time?

j-brant commented 5 years ago

The problem is in

interface_modifiers_opt
    : modifiers_opt
    | default_modifier 'modifier'
    ;

This should be something like

interface_modifiers_opt
    : (modifier 'modifier' | default_modifier 'modifier')*
    ;

I should rewrite the grammar to eliminate the _opt and list productions by using the newer syntax.

badetitou commented 5 years ago

Thanks a lot!