Improvement a few TokanHandlers by letting them make use of the new MatchRuneByCallback method, instead of having them implement their own logic.

This commit is contained in:
Maurice Makaay 2019-06-07 15:57:53 +00:00
parent 9a5bf8b9af
commit 40bad51064
1 changed files with 4 additions and 29 deletions

View File

@ -313,30 +313,14 @@ var T = struct {
// MatchRune creates a TokenHandler function that matches against the provided rune.
func MatchRune(expected rune) TokenHandler {
return func(t *TokenAPI) bool {
input, err := t.NextRune()
if err == nil && input == expected {
t.Accept()
return true
}
return false
}
return MatchRuneByCallback(func(r rune) bool { return r == expected })
}
// MatchRunes creates a TokenHandler function that checks if the input matches
// one of the provided runes.
func MatchRunes(expected ...rune) TokenHandler {
s := string(expected)
return func(t *TokenAPI) bool {
input, err := t.NextRune()
if err == nil {
if strings.ContainsRune(s, input) {
t.Accept()
return true
}
}
return false
}
return MatchRuneByCallback(func(r rune) bool { return strings.ContainsRune(s, r) })
}
// MatchRuneRange creates a TokenHandler function that checks if the input
@ -350,14 +334,7 @@ func MatchRuneRange(start rune, end rune) TokenHandler {
if end < start {
callerPanic(1, "TokenHandler: MatchRuneRange definition error at {caller}: start %q must not be < end %q", start, end)
}
return func(t *TokenAPI) bool {
input, err := t.NextRune()
if err == nil && input >= start && input <= end {
t.Accept()
return true
}
return false
}
return MatchRuneByCallback(func(r rune) bool { return r >= start && r <= end })
}
// MatchBlank creates a TokenHandler that matches one rune from the input
@ -366,7 +343,7 @@ func MatchRuneRange(start rune, end rune) TokenHandler {
// When you need whitespace matching, which also includes characters like
// newlines, then take a look at MatchWhitespace().
func MatchBlank() TokenHandler {
return MatchAny(MatchRune(' '), MatchRune('\t'))
return MatchRuneByCallback(func(r rune) bool { return r == ' ' || r == '\t' })
}
// MatchBlanks creates a TokenHandler that matches the input against one
@ -407,7 +384,6 @@ func MatchEndOfLine() TokenHandler {
}
// MatchStr creates a TokenHandler that matches the input against the provided string.
// TODO make this a more efficient string-level match?
func MatchStr(expected string) TokenHandler {
var handlers = []TokenHandler{}
for _, r := range expected {
@ -418,7 +394,6 @@ func MatchStr(expected string) TokenHandler {
// MatchStrNoCase creates a TokenHandler that matches the input against the
// provided string in a case-insensitive manner.
// TODO make this a more efficient string-level match?
func MatchStrNoCase(expected string) TokenHandler {
var handlers = []TokenHandler{}
for _, r := range expected {