taocpp / PEGTL

Parsing Expression Grammar Template Library
Boost Software License 1.0
1.95k stars 229 forks source link

Example grammar proto3 does not accept enum fields starting from zero #314

Closed knivil closed 2 years ago

knivil commented 2 years ago

Issue with proto3 example: Can not parse this simple proto-file

syntax = "proto3";

enum Error {
    hello = 0;
}

Reason: int_lit does not allow for a single zero, which is understandable for field numbers in messages, but field numbers in enum start at zero.

struct dec_lit : seq< range< '1', '9' >, star< digit > >  {};
struct int_lit : sor< hex_lit, oct_lit, dec_lit > {};
struct enum_field : ... ident, sps, equ, sps, int_lit, ...

A possible solution is to introduce enum_number that allows zero.

Ref: There must be a zero value, so that we can use 0 as a numeric default value https://developers.google.com/protocol-buffers/docs/proto3#enum

knivil commented 2 years ago

I know that the grammar suffers from same issue.

ColinH commented 2 years ago

So there was a bug in the original grammar that I copied into the PEGTL grammar. I'll change enum_field to use a new rule enum_int that uses enum_dec aka. plus< digit > instead of dec_lit.