Skip to content

Protobuf3.g4 cannot distinguish between a field of type message and enum #4535

@dsyer

Description

@dsyer

Example:

syntax = "proto3";
enum TestEnum {
        UNKNOWN = 0;
        FOO = 1;
        BAR = 2;
}
message TestMessage {
        TestEnum value = 1;
}

TestMessage.value is of type TestEnum (which is an enum by construction). The grammar can't tell the difference between a message type field and an enum type, so it matches as a message type (I assume because it is listed first in the grammar). The grammar looks like this:

...
messageType
    : (DOT)? (ident DOT)* messageName
    ;

enumType
    : (DOT)? (ident DOT)* enumName
    ;
...
messageName
    : ident
    ;

enumName
    : ident
    ;
...

Maybe there is no way for Antlr to express this difference? it's definitely a problem for anyone who wanted to use the generated parser.

Example command line to test:

$ grun Protobuf3 proto -tree
syntax = "proto3";
enum TestEnum {
        UNKNOWN = 0;
        FOO = 1;
        BAR = 2;
}
message TestMessage {
        TestEnum value = 1;
}
(proto (syntax syntax = "proto3" ;) (topLevelDef (enumDef enum (enumName (ident TestEnum)) (enumBody { (enumElement (enumField (ident UNKNOWN) = (intLit 0) ;)) (enumElement (enumField (ident FOO) = (intLit 1) ;)) (enumElement (enumField (ident BAR) = (intLit 2) ;)) }))) (topLevelDef (messageDef message (messageName (ident TestMessage)) (messageBody { (messageElement (field (type (messageType (messageName (ident TestEnum)))) (fieldName (ident value)) = (fieldNumber (intLit 1)) ;)) }))) <EOF>)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions