2022-05-22 12:47:45 +00:00
|
|
|
package argh
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type parser2 struct {
|
|
|
|
s *Scanner
|
|
|
|
|
2022-05-23 00:49:11 +00:00
|
|
|
cfg *ParserConfig
|
2022-05-22 12:47:45 +00:00
|
|
|
|
|
|
|
errors ScannerErrorList
|
|
|
|
|
|
|
|
tok Token
|
|
|
|
lit string
|
|
|
|
pos Pos
|
|
|
|
}
|
|
|
|
|
2022-05-23 00:49:11 +00:00
|
|
|
func ParseArgs2(args []string, pCfg *ParserConfig) (*ParseTree, error) {
|
2022-05-22 12:47:45 +00:00
|
|
|
parser := &parser2{}
|
|
|
|
parser.init(
|
|
|
|
strings.NewReader(strings.Join(args, string(nul))),
|
2022-05-23 00:49:11 +00:00
|
|
|
pCfg,
|
2022-05-22 12:47:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
tracef("ParseArgs2 parser=%+#v", parser)
|
|
|
|
|
|
|
|
return parser.parseArgs()
|
|
|
|
}
|
|
|
|
|
2022-05-23 00:49:11 +00:00
|
|
|
func (p *parser2) init(r io.Reader, pCfg *ParserConfig) {
|
2022-05-22 12:47:45 +00:00
|
|
|
p.errors = ScannerErrorList{}
|
|
|
|
|
2022-05-23 00:49:11 +00:00
|
|
|
if pCfg == nil {
|
|
|
|
pCfg = POSIXyParserConfig
|
2022-05-22 12:47:45 +00:00
|
|
|
}
|
|
|
|
|
2022-05-23 00:49:11 +00:00
|
|
|
p.cfg = pCfg
|
|
|
|
|
|
|
|
p.s = NewScanner(r, pCfg.ScannerConfig)
|
2022-05-22 12:47:45 +00:00
|
|
|
|
|
|
|
p.next()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *parser2) parseArgs() (*ParseTree, error) {
|
|
|
|
if p.errors.Len() != 0 {
|
|
|
|
tracef("parseArgs bailing due to initial error")
|
|
|
|
return nil, p.errors.Err()
|
|
|
|
}
|
|
|
|
|
2022-05-23 00:49:11 +00:00
|
|
|
prog := p.parseCommand(&p.cfg.Prog)
|
2022-05-22 12:47:45 +00:00
|
|
|
|
2022-05-23 00:49:11 +00:00
|
|
|
nodes := []Node{prog}
|
|
|
|
if v := p.parsePassthrough(); v != nil {
|
|
|
|
nodes = append(nodes, v)
|
2022-05-22 12:47:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &ParseTree{
|
2022-05-23 00:49:11 +00:00
|
|
|
Nodes: nodes,
|
2022-05-22 12:47:45 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *parser2) next() {
|
2022-05-23 00:49:11 +00:00
|
|
|
tracef("parser2.next() current: %v %q %v", p.tok, p.lit, p.pos)
|
2022-05-22 12:47:45 +00:00
|
|
|
|
|
|
|
p.tok, p.lit, p.pos = p.s.Scan()
|
|
|
|
|
2022-05-23 00:49:11 +00:00
|
|
|
tracef("parser2.next() next: %v %q %v", p.tok, p.lit, p.pos)
|
2022-05-22 12:47:45 +00:00
|
|
|
}
|
|
|
|
|
2022-05-23 00:49:11 +00:00
|
|
|
func (p *parser2) parseCommand(cCfg *CommandConfig) Node {
|
|
|
|
tracef("parseCommand cfg=%+#v", cCfg)
|
|
|
|
|
|
|
|
node := &Command{
|
|
|
|
Name: p.lit,
|
|
|
|
Values: map[string]string{},
|
|
|
|
Nodes: []Node{},
|
2022-05-22 12:47:45 +00:00
|
|
|
}
|
|
|
|
|
2022-05-23 00:49:11 +00:00
|
|
|
identIndex := 0
|
2022-05-22 12:47:45 +00:00
|
|
|
|
|
|
|
for i := 0; p.tok != EOL; i++ {
|
|
|
|
p.next()
|
|
|
|
|
2022-05-23 00:49:11 +00:00
|
|
|
tracef("parseCommand for=%d node.Values=%+#v", i, node.Values)
|
|
|
|
tracef("parseCommand for=%d node.Nodes=%+#v", i, node.Values)
|
|
|
|
|
|
|
|
if subCfg, ok := cCfg.Commands[p.lit]; ok {
|
|
|
|
subCommand := p.lit
|
|
|
|
|
|
|
|
node.Nodes = append(node.Nodes, p.parseCommand(&subCfg))
|
|
|
|
|
|
|
|
tracef("parseCommand breaking after sub-command=%v", subCommand)
|
2022-05-22 12:47:45 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
switch p.tok {
|
|
|
|
case ARG_DELIMITER:
|
2022-05-23 00:49:11 +00:00
|
|
|
tracef("parseCommand handling %s", p.tok)
|
|
|
|
|
|
|
|
node.Nodes = append(node.Nodes, &ArgDelimiter{})
|
|
|
|
|
2022-05-22 12:47:45 +00:00
|
|
|
continue
|
|
|
|
case IDENT, STDIN_FLAG:
|
2022-05-23 00:49:11 +00:00
|
|
|
tracef("parseCommand handling %s", p.tok)
|
|
|
|
|
|
|
|
if !cCfg.NValue.Contains(identIndex) {
|
|
|
|
tracef("parseCommand identIndex=%d exceeds expected=%s; breaking", identIndex, cCfg.NValue)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
name := fmt.Sprintf("%d", identIndex)
|
|
|
|
|
|
|
|
tracef("parseCommand checking for name of identIndex=%d", identIndex)
|
|
|
|
|
|
|
|
if len(cCfg.ValueNames) > identIndex {
|
|
|
|
name = cCfg.ValueNames[identIndex]
|
|
|
|
tracef("parseCommand setting name=%s from config value names", name)
|
|
|
|
} else if len(cCfg.ValueNames) == 1 && (cCfg.NValue == OneOrMoreValue || cCfg.NValue == ZeroOrMoreValue) {
|
|
|
|
name = fmt.Sprintf("%s.%d", cCfg.ValueNames[0], identIndex)
|
|
|
|
tracef("parseCommand setting name=%s from repeating value name", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
node.Values[name] = p.lit
|
|
|
|
|
|
|
|
identIndex++
|
2022-05-22 12:47:45 +00:00
|
|
|
case LONG_FLAG, SHORT_FLAG, COMPOUND_SHORT_FLAG:
|
2022-05-23 00:49:11 +00:00
|
|
|
tok := p.tok
|
|
|
|
flagNode := p.parseFlag()
|
|
|
|
|
|
|
|
tracef("parseCommand appending %s node=%+#v", tok, flagNode)
|
|
|
|
|
|
|
|
node.Nodes = append(node.Nodes, flagNode)
|
2022-05-22 12:47:45 +00:00
|
|
|
default:
|
2022-05-23 00:49:11 +00:00
|
|
|
tracef("parseCommand breaking on %s", p.tok)
|
2022-05-22 12:47:45 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-23 00:49:11 +00:00
|
|
|
tracef("parseCommand returning node=%+#v", node)
|
2022-05-22 12:47:45 +00:00
|
|
|
return node
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *parser2) parseIdent() Node {
|
|
|
|
defer p.next()
|
|
|
|
|
|
|
|
node := &Ident{Literal: p.lit}
|
|
|
|
return node
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *parser2) parseFlag() Node {
|
|
|
|
defer p.next()
|
|
|
|
|
|
|
|
switch p.tok {
|
|
|
|
case SHORT_FLAG:
|
|
|
|
return p.parseShortFlag()
|
|
|
|
case LONG_FLAG:
|
|
|
|
return p.parseLongFlag()
|
|
|
|
case COMPOUND_SHORT_FLAG:
|
|
|
|
return p.parseCompoundShortFlag()
|
|
|
|
}
|
|
|
|
|
|
|
|
panic(fmt.Sprintf("token %v cannot be parsed as flag", p.tok))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *parser2) parseShortFlag() Node {
|
|
|
|
node := &Flag{Name: string(p.lit[1])}
|
|
|
|
// TODO: moar stuff
|
|
|
|
return node
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *parser2) parseLongFlag() Node {
|
|
|
|
node := &Flag{Name: string(p.lit[2:])}
|
|
|
|
// TODO: moar stuff
|
|
|
|
return node
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *parser2) parseCompoundShortFlag() Node {
|
|
|
|
flagNodes := []Node{}
|
|
|
|
|
|
|
|
withoutFlagPrefix := p.lit[1:]
|
|
|
|
|
|
|
|
for _, r := range withoutFlagPrefix {
|
|
|
|
flagNodes = append(flagNodes, &Flag{Name: string(r)})
|
|
|
|
}
|
|
|
|
|
|
|
|
return &CompoundShortFlag{Nodes: flagNodes}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *parser2) parsePassthrough() Node {
|
|
|
|
nodes := []Node{}
|
|
|
|
|
|
|
|
for ; p.tok != EOL; p.next() {
|
|
|
|
nodes = append(nodes, &Ident{Literal: p.lit})
|
|
|
|
}
|
|
|
|
|
2022-05-23 00:49:11 +00:00
|
|
|
if len(nodes) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-22 12:47:45 +00:00
|
|
|
return &PassthroughArgs{Nodes: nodes}
|
|
|
|
}
|