mna / pigeon

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

[question/feature request] generate parser with private functions #158

Closed atticus-sullivan closed 1 month ago

atticus-sullivan commented 1 month ago

Hi,

working with pigeon has been very nice so far. Now after placing the parser into a dedicated package of my project I'm wondering if I somehow can generate the parser but all the functions like ParseFile() etc should be private (lowercase). This way, I would be able to write a simple wrapper (in my parser package) and avoid exposing the internal functions of the generated parser (thereby making it impossible to directly call these functions which makes the wrapper useless). In particular, I'm parsing to an AST and I want to hide all the any/interface{} types of the generated parser from the rest of the project.

Is something like that possible already? (didn't find something until to now) And if not, please consider it as a (probably easy to implement) feature.

breml commented 1 month ago

I am currently AFK, but wouldn't placing the parser into an internal package solve your issue?

atticus-sullivan commented 1 month ago

Not entirely. Placing it into internal/ only means other projects importing my project won't have access to the generated parser. But in my own project other packages would still have access. (at least that's what I know about the internal package.

So a version of my setup would be

myProject/
    parser/
        parser.go
        parser_gen.peg
        parser_gen.go
    main.go

now main.go should only have access to my wrapper function in parser.go but not to any automatically generated code in parser_gen.go.

But I also see this is mostly about code quality (might prevent some problems though) and might not have the highest priority.

EDIT: Oh and thanks for the fast reply (even though you say you're AFK currently)

breml commented 1 month ago

What I meant is something like this:

myProject/
    parser/
        internal/
            parser_gen.peg
            parser_gen.go
        parser.go
    main.go

With this structure, only the parser package has access to the generated parser in the internal package and you can define exactly the API surface you like in the parser package.

atticus-sullivan commented 1 month ago

Maybe not the nicest solution, but totally works for me.

Should we keep this open for the feature request or is this (with the workaround) not really relevant for you anymore?

breml commented 1 month ago

I quickly checked the code and give the solution with the ´internal´ package, I prefer to keep pigeon as is.