2022-05-15 18:22:56 +00:00
|
|
|
//go:generate stringer -type NValue
|
|
|
|
|
2022-05-12 02:11:05 +00:00
|
|
|
package argh
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2022-05-15 18:22:56 +00:00
|
|
|
const (
|
2022-05-16 12:24:24 +00:00
|
|
|
OneOrMoreValue NValue = -2
|
|
|
|
ZeroOrMoreValue NValue = -1
|
|
|
|
ZeroValue NValue = 0
|
2022-05-15 18:22:56 +00:00
|
|
|
)
|
|
|
|
|
2022-05-12 02:11:05 +00:00
|
|
|
var (
|
2022-05-16 12:24:24 +00:00
|
|
|
ErrSyntax = errors.New("syntax error")
|
2022-05-14 00:58:55 +00:00
|
|
|
|
|
|
|
DefaultParserConfig = &ParserConfig{
|
2022-05-15 18:22:56 +00:00
|
|
|
Commands: map[string]NValue{},
|
|
|
|
Flags: map[string]NValue{},
|
2022-05-14 00:58:55 +00:00
|
|
|
ScannerConfig: DefaultScannerConfig,
|
|
|
|
}
|
2022-05-12 02:11:05 +00:00
|
|
|
)
|
|
|
|
|
2022-05-15 18:22:56 +00:00
|
|
|
type NValue int
|
|
|
|
|
2022-05-14 00:58:55 +00:00
|
|
|
func ParseArgs(args []string, pCfg *ParserConfig) (*Argh, error) {
|
2022-05-12 02:11:05 +00:00
|
|
|
reEncoded := strings.Join(args, string(nul))
|
|
|
|
|
|
|
|
return NewParser(
|
|
|
|
strings.NewReader(reEncoded),
|
2022-05-14 00:58:55 +00:00
|
|
|
pCfg,
|
2022-05-12 02:11:05 +00:00
|
|
|
).Parse()
|
|
|
|
}
|
|
|
|
|
|
|
|
type Parser struct {
|
2022-05-15 18:22:56 +00:00
|
|
|
s *Scanner
|
2022-05-14 00:58:55 +00:00
|
|
|
|
2022-05-15 18:22:56 +00:00
|
|
|
buf []ScanEntry
|
|
|
|
|
2022-05-16 00:55:54 +00:00
|
|
|
cfg *ParserConfig
|
2022-05-14 00:58:55 +00:00
|
|
|
|
|
|
|
nodes []Node
|
|
|
|
stopSeen bool
|
2022-05-12 02:11:05 +00:00
|
|
|
}
|
|
|
|
|
2022-05-15 18:22:56 +00:00
|
|
|
type ScanEntry struct {
|
2022-05-12 02:11:05 +00:00
|
|
|
tok Token
|
|
|
|
lit string
|
2022-05-14 00:58:55 +00:00
|
|
|
pos int
|
2022-05-12 02:11:05 +00:00
|
|
|
}
|
|
|
|
|
2022-05-14 00:58:55 +00:00
|
|
|
type ParserConfig struct {
|
2022-05-16 12:24:24 +00:00
|
|
|
ProgValues NValue
|
|
|
|
Commands map[string]NValue
|
|
|
|
Flags map[string]NValue
|
|
|
|
|
|
|
|
OnUnknownFlag func(string) error
|
|
|
|
OnUnknownCommand func(string) error
|
|
|
|
|
2022-05-14 00:58:55 +00:00
|
|
|
ScannerConfig *ScannerConfig
|
2022-05-12 02:11:05 +00:00
|
|
|
}
|
|
|
|
|
2022-05-14 00:58:55 +00:00
|
|
|
func NewParser(r io.Reader, pCfg *ParserConfig) *Parser {
|
|
|
|
if pCfg == nil {
|
|
|
|
pCfg = DefaultParserConfig
|
2022-05-12 02:11:05 +00:00
|
|
|
}
|
|
|
|
|
2022-05-14 00:58:55 +00:00
|
|
|
parser := &Parser{
|
2022-05-16 00:55:54 +00:00
|
|
|
buf: []ScanEntry{},
|
|
|
|
s: NewScanner(r, pCfg.ScannerConfig),
|
|
|
|
cfg: pCfg,
|
2022-05-14 00:58:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tracef("NewParser parser=%+#v", parser)
|
|
|
|
tracef("NewParser pCfg=%+#v", pCfg)
|
|
|
|
|
|
|
|
return parser
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Parser) Parse() (*Argh, error) {
|
|
|
|
p.nodes = []Node{}
|
|
|
|
|
2022-05-12 02:11:05 +00:00
|
|
|
for {
|
2022-05-16 00:55:54 +00:00
|
|
|
br, err := p.parseArg()
|
2022-05-14 00:58:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-05-12 02:11:05 +00:00
|
|
|
}
|
|
|
|
|
2022-05-16 00:55:54 +00:00
|
|
|
if br {
|
2022-05-12 02:11:05 +00:00
|
|
|
break
|
|
|
|
}
|
2022-05-14 00:58:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &Argh{ParseTree: &ParseTree{Nodes: p.nodes}}, nil
|
|
|
|
}
|
|
|
|
|
2022-05-16 00:55:54 +00:00
|
|
|
func (p *Parser) parseArg() (bool, error) {
|
2022-05-14 00:58:55 +00:00
|
|
|
tok, lit, pos := p.scan()
|
|
|
|
if tok == ILLEGAL {
|
2022-05-16 12:24:24 +00:00
|
|
|
return false, errors.Wrapf(ErrSyntax, "illegal value %q at pos=%v", lit, pos)
|
2022-05-14 00:58:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if tok == EOL {
|
2022-05-16 00:55:54 +00:00
|
|
|
return true, nil
|
2022-05-14 00:58:55 +00:00
|
|
|
}
|
|
|
|
|
2022-05-15 18:22:56 +00:00
|
|
|
p.unscan(tok, lit, pos)
|
2022-05-14 00:58:55 +00:00
|
|
|
|
|
|
|
node, err := p.nodify()
|
|
|
|
|
|
|
|
tracef("parseArg node=%+#v err=%+#v", node, err)
|
|
|
|
|
|
|
|
if err != nil {
|
2022-05-16 00:55:54 +00:00
|
|
|
return false, errors.Wrapf(err, "value %q at pos=%v", lit, pos)
|
2022-05-14 00:58:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if node != nil {
|
|
|
|
p.nodes = append(p.nodes, node)
|
|
|
|
}
|
|
|
|
|
2022-05-16 00:55:54 +00:00
|
|
|
return false, nil
|
2022-05-14 00:58:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Parser) nodify() (Node, error) {
|
|
|
|
tok, lit, pos := p.scan()
|
|
|
|
|
|
|
|
tracef("nodify tok=%s lit=%q pos=%v", tok, lit, pos)
|
|
|
|
|
|
|
|
switch tok {
|
2022-05-16 12:24:24 +00:00
|
|
|
case ARG_DELIMITER:
|
|
|
|
return ArgDelimiter{}, nil
|
|
|
|
case ASSIGN:
|
|
|
|
return nil, errors.Wrapf(ErrSyntax, "bare assignment operator at pos=%v", pos)
|
2022-05-14 00:58:55 +00:00
|
|
|
case IDENT:
|
|
|
|
if len(p.nodes) == 0 {
|
2022-05-16 00:55:54 +00:00
|
|
|
values, err := p.scanValues(lit, pos, p.cfg.ProgValues)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return Program{Name: lit, Values: values}, nil
|
2022-05-14 00:58:55 +00:00
|
|
|
}
|
2022-05-15 00:58:09 +00:00
|
|
|
|
2022-05-16 00:55:54 +00:00
|
|
|
if n, ok := p.cfg.Commands[lit]; ok {
|
|
|
|
values, err := p.scanValues(lit, pos, n)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return Command{Name: lit, Values: values}, nil
|
2022-05-15 00:58:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Ident{Literal: lit}, nil
|
2022-05-14 00:58:55 +00:00
|
|
|
case COMPOUND_SHORT_FLAG:
|
|
|
|
flagNodes := []Node{}
|
|
|
|
|
2022-05-16 00:55:54 +00:00
|
|
|
withoutFlagPrefix := lit[1:]
|
|
|
|
|
|
|
|
for i, r := range withoutFlagPrefix {
|
|
|
|
if i == len(withoutFlagPrefix)-1 {
|
|
|
|
flagName := string(r)
|
|
|
|
|
|
|
|
if n, ok := p.cfg.Flags[flagName]; ok {
|
|
|
|
values, err := p.scanValues(flagName, pos, n)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
flagNodes = append(flagNodes, Flag{Name: flagName, Values: values})
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-14 00:58:55 +00:00
|
|
|
flagNodes = append(
|
|
|
|
flagNodes,
|
|
|
|
Flag{
|
|
|
|
Name: string(r),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-05-15 18:22:56 +00:00
|
|
|
return CompoundShortFlag{Nodes: flagNodes}, nil
|
2022-05-14 00:58:55 +00:00
|
|
|
case SHORT_FLAG:
|
|
|
|
flagName := string(lit[1:])
|
2022-05-16 00:55:54 +00:00
|
|
|
if n, ok := p.cfg.Flags[flagName]; ok {
|
|
|
|
values, err := p.scanValues(flagName, pos, n)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return Flag{Name: flagName, Values: values}, nil
|
2022-05-14 00:58:55 +00:00
|
|
|
}
|
|
|
|
|
2022-05-15 00:58:09 +00:00
|
|
|
return Flag{Name: flagName}, nil
|
2022-05-14 00:58:55 +00:00
|
|
|
case LONG_FLAG:
|
|
|
|
flagName := string(lit[2:])
|
2022-05-16 00:55:54 +00:00
|
|
|
if n, ok := p.cfg.Flags[flagName]; ok {
|
|
|
|
values, err := p.scanValues(flagName, pos, n)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return Flag{Name: flagName, Values: values}, nil
|
2022-05-14 00:58:55 +00:00
|
|
|
}
|
2022-05-12 02:11:05 +00:00
|
|
|
|
2022-05-15 00:58:09 +00:00
|
|
|
return Flag{Name: flagName}, nil
|
2022-05-14 00:58:55 +00:00
|
|
|
default:
|
2022-05-12 02:11:05 +00:00
|
|
|
}
|
|
|
|
|
2022-05-15 00:58:09 +00:00
|
|
|
return Ident{Literal: lit}, nil
|
2022-05-12 02:11:05 +00:00
|
|
|
}
|
|
|
|
|
2022-05-16 00:55:54 +00:00
|
|
|
func (p *Parser) scanValues(lit string, pos int, n NValue) ([]string, error) {
|
|
|
|
tracef("scanValues lit=%q pos=%v n=%v", lit, pos, n)
|
2022-05-15 18:22:56 +00:00
|
|
|
|
|
|
|
values, err := func() ([]string, error) {
|
|
|
|
if n == ZeroValue {
|
|
|
|
return []string{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ret := []string{}
|
|
|
|
|
|
|
|
for {
|
|
|
|
lit, err := p.scanIdent()
|
|
|
|
if err != nil {
|
2022-05-16 12:24:24 +00:00
|
|
|
if n == NValue(1) {
|
2022-05-15 18:22:56 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if n == OneOrMoreValue {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = append(ret, lit)
|
|
|
|
|
2022-05-16 12:24:24 +00:00
|
|
|
if n == NValue(1) && len(ret) == 1 {
|
2022-05-15 18:22:56 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}()
|
2022-05-14 00:58:55 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-05-16 00:55:54 +00:00
|
|
|
if len(values) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2022-05-15 18:22:56 +00:00
|
|
|
|
2022-05-16 00:55:54 +00:00
|
|
|
return values, nil
|
2022-05-14 00:58:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Parser) scanIdent() (string, error) {
|
|
|
|
tok, lit, pos := p.scan()
|
|
|
|
|
2022-05-16 12:24:24 +00:00
|
|
|
tracef("scanIdent scanned tok=%s lit=%q pos=%v", tok, lit, pos)
|
|
|
|
|
2022-05-15 18:22:56 +00:00
|
|
|
unscanBuf := []ScanEntry{}
|
2022-05-14 00:58:55 +00:00
|
|
|
|
|
|
|
if tok == ASSIGN || tok == ARG_DELIMITER {
|
2022-05-16 12:24:24 +00:00
|
|
|
entry := ScanEntry{tok: tok, lit: lit, pos: pos}
|
|
|
|
|
|
|
|
tracef("scanIdent tok=%s; scanning next and pushing to unscan buffer entry=%+#v", tok, entry)
|
|
|
|
|
|
|
|
unscanBuf = append([]ScanEntry{entry}, unscanBuf...)
|
2022-05-15 18:22:56 +00:00
|
|
|
|
2022-05-14 00:58:55 +00:00
|
|
|
tok, lit, pos = p.scan()
|
|
|
|
}
|
|
|
|
|
|
|
|
if tok == IDENT {
|
|
|
|
return lit, nil
|
|
|
|
}
|
|
|
|
|
2022-05-16 12:24:24 +00:00
|
|
|
entry := ScanEntry{tok: tok, lit: lit, pos: pos}
|
|
|
|
|
|
|
|
tracef("scanIdent tok=%s; unscanning entry=%+#v", tok, entry)
|
|
|
|
|
|
|
|
unscanBuf = append([]ScanEntry{entry}, unscanBuf...)
|
2022-05-15 18:22:56 +00:00
|
|
|
|
|
|
|
for _, entry := range unscanBuf {
|
|
|
|
p.unscan(entry.tok, entry.lit, entry.pos)
|
2022-05-14 00:58:55 +00:00
|
|
|
}
|
|
|
|
|
2022-05-16 12:24:24 +00:00
|
|
|
return "", errors.Wrapf(ErrSyntax, "expected ident at pos=%v but got %s (%q)", pos, tok, lit)
|
2022-05-14 00:58:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Parser) scan() (Token, string, int) {
|
2022-05-15 18:22:56 +00:00
|
|
|
if len(p.buf) != 0 {
|
|
|
|
entry, buf := p.buf[len(p.buf)-1], p.buf[:len(p.buf)-1]
|
|
|
|
p.buf = buf
|
|
|
|
|
|
|
|
tracef("scan returning buffer entry=%s %+#v", entry.tok, entry)
|
|
|
|
return entry.tok, entry.lit, entry.pos
|
2022-05-12 02:11:05 +00:00
|
|
|
}
|
|
|
|
|
2022-05-14 00:58:55 +00:00
|
|
|
tok, lit, pos := p.s.Scan()
|
2022-05-12 02:11:05 +00:00
|
|
|
|
2022-05-15 18:22:56 +00:00
|
|
|
tracef("scan returning next=%s %+#v", tok, ScanEntry{tok: tok, lit: lit, pos: pos})
|
2022-05-12 02:11:05 +00:00
|
|
|
|
2022-05-14 00:58:55 +00:00
|
|
|
return tok, lit, pos
|
2022-05-12 02:11:05 +00:00
|
|
|
}
|
|
|
|
|
2022-05-15 18:22:56 +00:00
|
|
|
func (p *Parser) unscan(tok Token, lit string, pos int) {
|
|
|
|
entry := ScanEntry{tok: tok, lit: lit, pos: pos}
|
|
|
|
|
|
|
|
tracef("unscan entry=%s %+#v", tok, entry)
|
2022-05-14 00:58:55 +00:00
|
|
|
|
2022-05-15 18:22:56 +00:00
|
|
|
p.buf = append(p.buf, entry)
|
2022-05-14 00:58:55 +00:00
|
|
|
}
|