box-o-sand/argh/scanner.go

195 lines
3.0 KiB
Go
Raw Normal View History

package argh
import (
"bufio"
"bytes"
"errors"
"io"
"log"
"unicode"
)
const (
nul = rune(0)
eol = rune(-1)
)
var (
DefaultScannerConfig = &ScannerConfig{
AssignmentOperator: '=',
FlagPrefix: '-',
MultiValueDelim: ',',
}
)
type Scanner struct {
r *bufio.Reader
2022-05-14 00:58:55 +00:00
i int
cfg *ScannerConfig
}
type ScannerConfig struct {
AssignmentOperator rune
FlagPrefix rune
MultiValueDelim rune
}
func NewScanner(r io.Reader, cfg *ScannerConfig) *Scanner {
if cfg == nil {
cfg = DefaultScannerConfig
}
return &Scanner{
r: bufio.NewReader(r),
cfg: cfg,
}
}
2022-05-14 00:58:55 +00:00
func (s *Scanner) Scan() (Token, string, int) {
ch, pos := s.read()
if s.isBlankspace(ch) {
2022-05-14 00:58:55 +00:00
_ = s.unread()
return s.scanBlankspace()
}
if s.isAssignmentOperator(ch) {
2022-05-14 00:58:55 +00:00
return ASSIGN, string(ch), pos
}
if s.isMultiValueDelim(ch) {
2022-05-14 00:58:55 +00:00
return MULTI_VALUE_DELIMITER, string(ch), pos
}
if ch == eol {
2022-05-14 00:58:55 +00:00
return EOL, "", pos
}
if ch == nul {
2022-05-14 00:58:55 +00:00
return ARG_DELIMITER, string(ch), pos
}
if unicode.IsGraphic(ch) {
2022-05-14 00:58:55 +00:00
_ = s.unread()
return s.scanArg()
}
2022-05-14 00:58:55 +00:00
return ILLEGAL, string(ch), pos
}
2022-05-14 00:58:55 +00:00
func (s *Scanner) read() (rune, int) {
ch, _, err := s.r.ReadRune()
2022-05-14 00:58:55 +00:00
s.i++
if errors.Is(err, io.EOF) {
2022-05-14 00:58:55 +00:00
return eol, s.i
} else if err != nil {
log.Printf("unknown scanner error=%+v", err)
2022-05-14 00:58:55 +00:00
return eol, s.i
}
2022-05-14 00:58:55 +00:00
return ch, s.i
}
2022-05-14 00:58:55 +00:00
func (s *Scanner) unread() int {
_ = s.r.UnreadRune()
2022-05-14 00:58:55 +00:00
s.i--
return s.i
}
func (s *Scanner) isBlankspace(ch rune) bool {
return ch == ' ' || ch == '\t' || ch == '\n'
}
func (s *Scanner) isUnderscore(ch rune) bool {
return ch == '_'
}
func (s *Scanner) isFlagPrefix(ch rune) bool {
return ch == s.cfg.FlagPrefix
}
func (s *Scanner) isMultiValueDelim(ch rune) bool {
return ch == s.cfg.MultiValueDelim
}
func (s *Scanner) isAssignmentOperator(ch rune) bool {
return ch == s.cfg.AssignmentOperator
}
2022-05-14 00:58:55 +00:00
func (s *Scanner) scanBlankspace() (Token, string, int) {
buf := &bytes.Buffer{}
2022-05-14 00:58:55 +00:00
ch, pos := s.read()
buf.WriteRune(ch)
for {
2022-05-14 00:58:55 +00:00
ch, pos = s.read()
if ch == eol {
break
} else if !s.isBlankspace(ch) {
2022-05-14 00:58:55 +00:00
pos = s.unread()
break
} else {
_, _ = buf.WriteRune(ch)
}
}
2022-05-14 00:58:55 +00:00
return BS, buf.String(), pos
}
2022-05-14 00:58:55 +00:00
func (s *Scanner) scanArg() (Token, string, int) {
buf := &bytes.Buffer{}
2022-05-14 00:58:55 +00:00
ch, pos := s.read()
buf.WriteRune(ch)
for {
2022-05-14 00:58:55 +00:00
ch, pos = s.read()
if ch == eol || ch == nul || s.isAssignmentOperator(ch) || s.isMultiValueDelim(ch) {
2022-05-14 00:58:55 +00:00
pos = s.unread()
break
}
_, _ = buf.WriteRune(ch)
}
str := buf.String()
if len(str) == 0 {
2022-05-14 00:58:55 +00:00
return EMPTY, str, pos
}
ch0 := rune(str[0])
if len(str) == 1 {
if s.isFlagPrefix(ch0) {
2022-05-14 00:58:55 +00:00
return STDIN_FLAG, str, pos
}
2022-05-14 00:58:55 +00:00
return IDENT, str, pos
}
ch1 := rune(str[1])
if len(str) == 2 {
if str == string(s.cfg.FlagPrefix)+string(s.cfg.FlagPrefix) {
2022-05-14 00:58:55 +00:00
return STOP_FLAG, str, pos
}
if s.isFlagPrefix(ch0) {
2022-05-14 00:58:55 +00:00
return SHORT_FLAG, str, pos
}
}
if s.isFlagPrefix(ch0) {
if s.isFlagPrefix(ch1) {
2022-05-14 00:58:55 +00:00
return LONG_FLAG, str, pos
}
2022-05-14 00:58:55 +00:00
return COMPOUND_SHORT_FLAG, str, pos
}
2022-05-14 00:58:55 +00:00
return IDENT, str, pos
}