mvdan / sh

A shell parser, formatter, and interpreter with bash support; includes shfmt
https://pkg.go.dev/mvdan.cc/sh/v3
BSD 3-Clause "New" or "Revised" License
7.2k stars 340 forks source link

How hard would it be to creating a custom parser based on an existing shell language? #1005

Closed hwittenborn closed 1 year ago

hwittenborn commented 1 year ago

I was looking into making some sort-of custom syntax for a project I'm working on:

It would be sort of like the Bats @test syntax, but would ommit the @test part and change the string part after it to have no quotes like so:

package {
    # Anything in here is normal shell syntax again, until the closing `}` is found.
    a_function() { true; }
    echo "Run commands, and do any normal shell stuff in here"
}

It's also like just defining a package() { ... } function, I'm just wanting to omit the () for stylistic purposes in my program.

Does the public API expose anything to do the things I'm looking for? If not, would there be anywhere in the codebase you'd recommend I'd look at if a fork would be necessary?

mvdan commented 1 year ago

There is no easy API for you to do this, no - you would have to modify the recursive descent parser to allow for it. Looking at how the Bats syntax is implemented would be a good start. I don't think we want to have API for supporting arbitrary syntax like this, because I'm not even sure what the API would look like. A soft fork seems like a good way to experiment.