Do AST better maybe?
This commit is contained in:
parent
dc3a40b19d
commit
d1ffbe25a3
@ -59,31 +59,25 @@ func TestParser2(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
/*
|
||||
expAST: []argh.Node{
|
||||
&argh.Command{
|
||||
Name: "pies",
|
||||
Values: map[string]string{},
|
||||
Nodes: []argh.Node{
|
||||
&argh.CompoundShortFlag{
|
||||
Nodes: []argh.Node{
|
||||
&argh.Flag{Name: "e"},
|
||||
&argh.Flag{Name: "a"},
|
||||
&argh.Flag{Name: "t"},
|
||||
},
|
||||
},
|
||||
&argh.Flag{Name: "wat"},
|
||||
&argh.Command{
|
||||
Name: "hello",
|
||||
Values: map[string]string{
|
||||
"name": "mario",
|
||||
},
|
||||
Nodes: []argh.Node{},
|
||||
expAST: []argh.Node{
|
||||
&argh.Command{
|
||||
Name: "pies",
|
||||
Values: map[string]string{},
|
||||
Nodes: []argh.Node{
|
||||
&argh.Flag{Name: "e"},
|
||||
&argh.Flag{Name: "a"},
|
||||
&argh.Flag{Name: "t"},
|
||||
&argh.Flag{Name: "wat"},
|
||||
&argh.Command{
|
||||
Name: "hello",
|
||||
Values: map[string]string{
|
||||
"name": "mario",
|
||||
},
|
||||
Nodes: []argh.Node{},
|
||||
},
|
||||
},
|
||||
},
|
||||
*/
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "bare",
|
||||
@ -432,12 +426,13 @@ func TestParser2(t *testing.T) {
|
||||
},
|
||||
},
|
||||
} {
|
||||
if tc.skip {
|
||||
continue
|
||||
}
|
||||
|
||||
if tc.expPT != nil {
|
||||
t.Run(tc.name+" parse tree", func(ct *testing.T) {
|
||||
if tc.skip {
|
||||
ct.SkipNow()
|
||||
return
|
||||
}
|
||||
|
||||
pt, err := argh.ParseArgs2(tc.args, tc.cfg)
|
||||
if err != nil {
|
||||
assert.ErrorIs(ct, err, tc.expErr)
|
||||
@ -452,6 +447,11 @@ func TestParser2(t *testing.T) {
|
||||
|
||||
if tc.expAST != nil {
|
||||
t.Run(tc.name+" ast", func(ct *testing.T) {
|
||||
if tc.skip {
|
||||
ct.SkipNow()
|
||||
return
|
||||
}
|
||||
|
||||
pt, err := argh.ParseArgs2(tc.args, tc.cfg)
|
||||
if err != nil {
|
||||
ct.Logf("err=%+#v", err)
|
||||
|
@ -1,10 +1,7 @@
|
||||
package argh
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Querier interface {
|
||||
Program() (Command, bool)
|
||||
TypedAST() []TypedNode
|
||||
AST() []Node
|
||||
}
|
||||
|
||||
@ -25,50 +22,34 @@ func (dq *defaultQuerier) Program() (Command, bool) {
|
||||
return v, ok
|
||||
}
|
||||
|
||||
func (dq *defaultQuerier) TypedAST() []TypedNode {
|
||||
ret := []TypedNode{}
|
||||
|
||||
for _, node := range dq.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
|
||||
}
|
||||
|
||||
func (dq *defaultQuerier) AST() []Node {
|
||||
ret := []Node{}
|
||||
|
||||
for _, node := range dq.nodes {
|
||||
if _, ok := node.(ArgDelimiter); ok {
|
||||
for i, node := range dq.nodes {
|
||||
tracef("AST i=%d node type=%T", i, node)
|
||||
|
||||
if _, ok := node.(*ArgDelimiter); ok {
|
||||
continue
|
||||
}
|
||||
|
||||
if _, ok := node.(StopFlag); ok {
|
||||
if _, ok := node.(*StopFlag); ok {
|
||||
continue
|
||||
}
|
||||
|
||||
if v, ok := node.(CompoundShortFlag); ok {
|
||||
if v, ok := node.(*CompoundShortFlag); ok {
|
||||
ret = append(ret, NewQuerier(v.Nodes).AST()...)
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
if v, ok := node.(Command); ok {
|
||||
ret = append(ret, NewQuerier(v.Nodes).AST()...)
|
||||
if v, ok := node.(*Command); ok {
|
||||
ret = append(
|
||||
ret,
|
||||
&Command{
|
||||
Name: v.Name,
|
||||
Values: v.Values,
|
||||
Nodes: NewQuerier(v.Nodes).AST(),
|
||||
})
|
||||
|
||||
continue
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user