2024-04-10 10:45:58 +00:00
|
|
|
package cmdlang
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/alecthomas/participle/v2"
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
|
|
|
type astLiteral struct {
|
|
|
|
Str *string `parser:"@String"`
|
|
|
|
Ident *string `parser:" | @Ident"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type astCmdArg struct {
|
2024-04-10 12:19:11 +00:00
|
|
|
Literal *astLiteral `parser:"@@"`
|
2024-04-11 10:58:59 +00:00
|
|
|
Var *string `parser:"| '$' @Ident"`
|
2024-04-10 12:44:20 +00:00
|
|
|
Sub *astPipeline `parser:"| '(' @@ ')'"`
|
2024-04-10 10:45:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type astCmd struct {
|
|
|
|
Name string `parser:"@Ident"`
|
|
|
|
Args []astCmdArg `parser:"@@*"`
|
|
|
|
}
|
|
|
|
|
2024-04-10 11:58:06 +00:00
|
|
|
type astPipeline struct {
|
|
|
|
First *astCmd `parser:"@@"`
|
|
|
|
Rest []*astCmd `parser:"( '|' @@ )*"`
|
|
|
|
}
|
|
|
|
|
2024-04-11 12:05:05 +00:00
|
|
|
type astStatements struct {
|
|
|
|
First *astPipeline `parser:"@@"`
|
|
|
|
Rest []*astPipeline `parser:"( ';' @@ )*"` // TODO: also add support for newlines
|
|
|
|
}
|
|
|
|
|
|
|
|
var parser = participle.MustBuild[astStatements]()
|
2024-04-10 10:45:58 +00:00
|
|
|
|
2024-04-11 12:05:05 +00:00
|
|
|
func parse(r io.Reader) (*astStatements, error) {
|
2024-04-10 10:45:58 +00:00
|
|
|
return parser.Parse("test", r)
|
|
|
|
}
|