parent
af7d5c6e14
commit
c15bafe55d
@ -1,5 +1,35 @@
|
||||
package argh
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
// NOTE: much of this is lifted from
|
||||
// https://blog.gopheracademy.com/advent-2014/parsers-lexers/
|
||||
|
||||
var (
|
||||
tracingEnabled = os.Getenv("ARGH_TRACING") == "enabled"
|
||||
)
|
||||
|
||||
type Argh struct {
|
||||
AST *AST
|
||||
ParseTree *ParseTree `json:"parse_tree"`
|
||||
}
|
||||
|
||||
func (a *Argh) AST() []TypedNode {
|
||||
return a.ParseTree.toAST()
|
||||
}
|
||||
|
||||
/*
|
||||
func (a *Argh) String() string {
|
||||
return a.ParseTree.String()
|
||||
}
|
||||
*/
|
||||
|
||||
func tracef(format string, v ...any) {
|
||||
if !tracingEnabled {
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf(format, v...)
|
||||
}
|
||||
|
@ -1,10 +0,0 @@
|
||||
package argh
|
||||
|
||||
type AST struct {
|
||||
Nodes []*Node `json:"nodes"`
|
||||
}
|
||||
|
||||
type Node struct {
|
||||
Token string `json:"token"`
|
||||
Literal string `json:"literal"`
|
||||
}
|
@ -1,2 +1,12 @@
|
||||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
@ -0,0 +1,82 @@
|
||||
package argh
|
||||
|
||||
import "fmt"
|
||||
|
||||
type ParseTree struct {
|
||||
Nodes []Node `json:"nodes"`
|
||||
}
|
||||
|
||||
func (pt *ParseTree) toAST() []TypedNode {
|
||||
ret := []TypedNode{}
|
||||
|
||||
for _, node := range pt.Nodes {
|
||||
if _, ok := node.(ArgDelimiter); ok {
|
||||
continue
|
||||
}
|
||||
|
||||
if _, ok := node.(StopFlag); ok {
|
||||
continue
|
||||
}
|
||||
|
||||
ret = append(
|
||||
ret,
|
||||
TypedNode{
|
||||
Type: fmt.Sprintf("%T", node),
|
||||
Node: node,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
type Node interface{}
|
||||
|
||||
type TypedNode struct {
|
||||
Type string `json:"type"`
|
||||
Node Node `json:"node"`
|
||||
}
|
||||
|
||||
type Args struct {
|
||||
Pos int `json:"pos"`
|
||||
Nodes []Node `json:"nodes"`
|
||||
}
|
||||
|
||||
type Statement struct {
|
||||
Pos int `json:"pos"`
|
||||
Nodes []Node `json:"nodes"`
|
||||
}
|
||||
|
||||
type Program struct {
|
||||
Pos int `json:"pos"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type Ident struct {
|
||||
Pos int `json:"pos"`
|
||||
Literal string `json:"literal"`
|
||||
}
|
||||
|
||||
type Command struct {
|
||||
Pos int `json:"pos"`
|
||||
Name string `json:"name"`
|
||||
Nodes []Node `json:"nodes"`
|
||||
}
|
||||
|
||||
type Flag struct {
|
||||
Pos int `json:"pos"`
|
||||
Name string `json:"name"`
|
||||
Value *string `json:"value,omitempty"`
|
||||
}
|
||||
|
||||
type StdinFlag struct {
|
||||
Pos int `json:"pos"`
|
||||
}
|
||||
|
||||
type StopFlag struct {
|
||||
Pos int `json:"pos"`
|
||||
}
|
||||
|
||||
type ArgDelimiter struct {
|
||||
Pos int `json:"pos"`
|
||||
}
|
@ -0,0 +1,119 @@
|
||||
package argh_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"git.meatballhat.com/x/box-o-sand/argh"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func ptr[T any](v T) *T {
|
||||
return &v
|
||||
}
|
||||
|
||||
func TestParser(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
args []string
|
||||
cfg *argh.ParserConfig
|
||||
expected *argh.Argh
|
||||
expectedErr error
|
||||
skip bool
|
||||
}{
|
||||
{
|
||||
name: "bare",
|
||||
args: []string{"pizzas"},
|
||||
expected: &argh.Argh{
|
||||
ParseTree: &argh.ParseTree{
|
||||
Nodes: []argh.Node{
|
||||
argh.Program{Name: "pizzas"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "long value-less flags",
|
||||
args: []string{"pizzas", "--tasty", "--fresh", "--super-hot-right-now"},
|
||||
expected: &argh.Argh{
|
||||
ParseTree: &argh.ParseTree{
|
||||
Nodes: []argh.Node{
|
||||
argh.Program{Name: "pizzas", Pos: 0},
|
||||
argh.ArgDelimiter{Pos: 6},
|
||||
argh.Flag{Name: "tasty", Pos: 7},
|
||||
argh.ArgDelimiter{Pos: 14},
|
||||
argh.Flag{Name: "fresh", Pos: 15},
|
||||
argh.ArgDelimiter{Pos: 22},
|
||||
argh.Flag{Name: "super-hot-right-now", Pos: 23},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "long flags mixed",
|
||||
args: []string{"pizzas", "--tasty", "--fresh", "soon", "--super-hot-right-now"},
|
||||
cfg: &argh.ParserConfig{
|
||||
Commands: []string{},
|
||||
ValueFlags: []string{"fresh"},
|
||||
},
|
||||
expected: &argh.Argh{
|
||||
ParseTree: &argh.ParseTree{
|
||||
Nodes: []argh.Node{
|
||||
argh.Program{Name: "pizzas", Pos: 0},
|
||||
argh.ArgDelimiter{Pos: 6},
|
||||
argh.Flag{Name: "tasty", Pos: 7},
|
||||
argh.ArgDelimiter{Pos: 14},
|
||||
argh.Flag{Name: "fresh", Pos: 15, Value: ptr("soon")},
|
||||
argh.ArgDelimiter{Pos: 27},
|
||||
argh.Flag{Name: "super-hot-right-now", Pos: 28},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
skip: true,
|
||||
|
||||
name: "typical",
|
||||
args: []string{"pizzas", "-a", "--ca", "-b", "1312", "-lol"},
|
||||
cfg: &argh.ParserConfig{
|
||||
Commands: []string{},
|
||||
ValueFlags: []string{"b"},
|
||||
},
|
||||
expected: &argh.Argh{
|
||||
ParseTree: &argh.ParseTree{
|
||||
Nodes: []argh.Node{
|
||||
argh.Program{Name: "pizzas", Pos: 0},
|
||||
argh.ArgDelimiter{Pos: 6},
|
||||
argh.Flag{Name: "a", Pos: 7},
|
||||
argh.ArgDelimiter{Pos: 9},
|
||||
argh.Flag{Name: "ca", Pos: 10},
|
||||
argh.ArgDelimiter{Pos: 14},
|
||||
argh.Flag{Name: "b", Pos: 15, Value: ptr("1312")},
|
||||
argh.ArgDelimiter{Pos: 22},
|
||||
argh.Statement{
|
||||
Pos: 23,
|
||||
Nodes: []argh.Node{
|
||||
argh.Flag{Name: "l", Pos: 29},
|
||||
argh.Flag{Name: "o", Pos: 30},
|
||||
argh.Flag{Name: "l", Pos: 31},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
} {
|
||||
if tc.skip {
|
||||
continue
|
||||
}
|
||||
|
||||
t.Run(tc.name, func(ct *testing.T) {
|
||||
actual, err := argh.ParseArgs(tc.args, tc.cfg)
|
||||
if err != nil {
|
||||
assert.ErrorIs(ct, err, tc.expectedErr)
|
||||
return
|
||||
}
|
||||
|
||||
assert.Equal(ct, tc.expected, actual)
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in new issue