Clean up error handling a bit.

This commit is contained in:
Alec Thomas
2018-11-03 09:49:00 +11:00
parent c0df056b14
commit fddfb973d6
4 changed files with 48 additions and 20 deletions
+33 -3
View File
@@ -5,8 +5,6 @@ import (
"strings"
)
//go:generate stringer -type=TokenType
// TokenType is the type of a token.
type TokenType int
@@ -21,6 +19,26 @@ const (
PositionalArgumentToken // <arg>
)
func (t TokenType) String() string {
switch t {
case UntypedToken:
return "untyped"
case EOLToken:
return "<EOL>"
case FlagToken: // --<flag>
return "long flag"
case FlagValueToken: // =<value>
return "flag value"
case ShortFlagToken: // -<short>[<tail]
return "short flag"
case ShortFlagTailToken: // <tail>
return "short flag remainder"
case PositionalArgumentToken: // <arg>
return "positional argument"
}
panic("unsupported type")
}
// Token created by Scanner.
type Token struct {
Value string
@@ -58,6 +76,18 @@ func (t Token) IsAny(types ...TokenType) bool {
return false
}
// InferredType tries to infer the type of a token.
func (t Token) InferredType() TokenType {
if t.Type == UntypedToken {
if strings.HasPrefix(t.Value, "--") {
return FlagToken
} else if strings.HasPrefix(t.Value, "-") {
return ShortFlagToken
}
}
return t.Type
}
// IsValue returns true if token is usable as a parseable value.
//
// A parseable value is either a value typed token, or an untyped token NOT starting with a hyphen.
@@ -113,7 +143,7 @@ func (s *Scanner) Pop() Token {
func (s *Scanner) PopValue(context string) string {
t := s.Pop()
if !t.IsValue() {
fail("expected %s value but got %s", context, t)
fail("expected %s value but got %s (%s)", context, t, t.InferredType())
}
return t.Value
}