Browse Source

lexer: NewIsNot(), NewIsIn(), NewIsOneOf()

Signed-off-by: Alejandro Mery <amery@jpi.io>
pull/9/head
Alejandro Mery 1 year ago
parent
commit
eb9261b478
  1. 34
      lexer/runes.go

34
lexer/runes.go

@ -1,6 +1,38 @@
package lexer package lexer
import "unicode" import (
"strings"
"unicode"
)
// NewIsNot generates a rune condition checker that reverses the
// decision of the given checker.
func NewIsNot(cond func(rune) bool) func(rune) bool {
return func(r rune) bool {
return !cond(r)
}
}
// NewIsIn generates a rune condition checker that accepts runes
// contained on the provided string
func NewIsIn(s string) func(rune) bool {
return func(r rune) bool {
return strings.ContainsRune(s, r)
}
}
// NewIsOneOf generates a run condition checker that accepts runes
// accepted by any of the given checkers
func NewIsOneOf(s ...func(rune) bool) func(rune) bool {
return func(r rune) bool {
for _, cond := range s {
if cond(r) {
return true
}
}
return false
}
}
// IsSpace reports whether the rune is a space character as // IsSpace reports whether the rune is a space character as
// defined by Unicode's White Space property // defined by Unicode's White Space property

Loading…
Cancel
Save