goccmack / gocc

Parser / Scanner Generator
Other
622 stars 48 forks source link

improvement: make generated code safer #84

Closed onokonem closed 5 years ago

onokonem commented 5 years ago

how to reproduce a closing multiline comment sequence problem

mkdir mlcomment-test
cd mlcomment-test
go mod init github.com/goccmack/gocc/mlcomment-test
echo 'MLComment: "/*" "test" "*/";' > mlcomment-test.ebnf
gocc -a -v -p github.com/goccmack/gocc/mlcomment-test mlcomment-test.ebnf
go build ./parser
shivansh commented 5 years ago

Thanks for the PR. Could you also please include a small description explaining why separate handling of */ symbol is required ?

onokonem commented 5 years ago

I've updated the PR description and added a brief comment to the source

shivansh commented 5 years ago

Sorry for the delay on this.

For others' convenience, this is the generated (buggy) actiontable from the EBNF mentioned above, and this is the result after applying the fix.

As an aside, Go's spec uses the term general comments instead of multiline comments so maybe we could switch to using it.

I would personally suggest safeSym to be something along the lines of the following, although deferring to @awalterschulze @mewmew for suggestions -

func safeSym(sym string) string {
    if sym == `*/` {
        // A '*/' token in the EBNF causes compilation error in the
        // generated actiontable due to buggy comments like `/* */ */`.
        // Avoid this by replacing them in the generated code with -
        // `/* closing general comment token */`.
        return "closing general comment token"
    } else {
        return sym
    }
}
shivansh commented 5 years ago

Alternatively, we can also replace the general comments with line comments in actiontable. This will be the resulting actiontable.

awalterschulze commented 5 years ago

I like the line comments.

shivansh commented 5 years ago

Fixed in 07ce4bc.