mna / pigeon

Command pigeon generates parsers in Go from a PEG grammar.
BSD 3-Clause "New" or "Revised" License
835 stars 66 forks source link

Build failure only when --optimize-grammar is set #65

Closed mna closed 6 years ago

mna commented 6 years ago

Hello,

When building a particular grammar generated with --optimize-grammar, it may result in build failures. The same grammar builds correctly when this option is not set. I know this flag is documented as "experimental", but still, this is probably fixable.

I have tried to reduce the grammar to the minimum required to reproduce the issue, and the result is in the github repo https://github.com/mna/pigeon-bug-optimize-grammar .

The grammar looks like this:

Start <- List         // NOTE: changing List to X here makes the bug disappear
List <- X ( ',' X )*  // NOTE: changing X to Y here makes the bug disappear
X <- 'X' Y?           // NOTE: removing the "?" makes the bug disappear
Y <- 'Y'
  {
    // NOTE: code block required for the bug, gets rendered twice with --optimize-grammar
    return nil, nil
  }

And with the --optimize-grammar option set, the output of go build (go1.10.2 on macOS for me) is:

bug/grammar.go:43:22: (*parser).callonList4 undefined (type *parser has no method callonList4)
bug/grammar.go:94:6: (*current).onList10 redeclared in this block
    previous declaration at bug/grammar.go:82:6
bug/grammar.go:100:6: (*parser).callonList10 redeclared in this block
    previous declaration at bug/grammar.go:88:6

I'll investigate and see if I can find a fix and send a PR, but I wanted to open the issue first in case you have ideas on what may cause this.

Thanks! Martin