antlr / grammars-v4

Grammars written for ANTLR v4; expectation that the grammars are free of actions.
MIT License
10.17k stars 3.7k forks source link

Java9 Grammar doesn't properly handle nested typeArguments within unannClassType #1149

Open mattkindy opened 6 years ago

mattkindy commented 6 years ago

As an example,

Map<String, Class<?>>

will cause an error, while

Map<String, Class<?> >

is handled properly.

mattkindy commented 6 years ago

Think the reason this is happening is greedy checks on the following:

shiftExpression
    :    additiveExpression
    |    shiftExpression '<<' additiveExpression
    |    shiftExpression '>>' additiveExpression
    |    shiftExpression '>>>' additiveExpression
    ;

These tokens are implicit during lexing and have match priority. The solution is probably

shiftExpression
    :    additiveExpression
    |    shiftExpression '<' '<' additiveExpression
    |    shiftExpression '>' '>' additiveExpression
    |    shiftExpression '>' '>' '>' additiveExpression
    ;

As another note, this seems to have happened when the Java9 grammar was made to support shiftExpression since the Java8 grammar here seems to lack them. When shift tokens were commented out, it didn't cause errors because they weren't used in the parser rules. In general, the parser rules should not use string literals. I would prefer

shiftExpression
    :    additiveExpression
    |    shiftExpression LT LT additiveExpression
    |    shiftExpression GT GT additiveExpression
    |    shiftExpression GT GT GT additiveExpression
    ;