From a02326a0a6851080601fd70d7ab7431e2d5f622b Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Tue, 29 Aug 2023 16:19:04 +0000 Subject: [PATCH 1/3] lexer: ErrUnacceptableRune and ErrNotImplemented Signed-off-by: Alejandro Mery --- lexer/error.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lexer/error.go b/lexer/error.go index 6ffd479..541c7f8 100644 --- a/lexer/error.go +++ b/lexer/error.go @@ -1,6 +1,7 @@ package lexer import ( + "errors" "fmt" "strings" ) @@ -9,6 +10,14 @@ var ( _ error = (*Error)(nil) ) +var ( + // ErrUnacceptableRune indicates the read rune isn't acceptable in the context + ErrUnacceptableRune = errors.New("rune not acceptable in context") + + // ErrNotImplemented indicates something hasn't been implemented yet + ErrNotImplemented = errors.New("not implemented") +) + // Error represents a generic parsing error type Error struct { Filename string -- 2.17.1 From 0098052142bd2149287b5a62fb9805fc2de649e9 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Tue, 29 Aug 2023 20:18:15 +0000 Subject: [PATCH 2/3] lexer: IsSpace() Signed-off-by: Alejandro Mery --- lexer/runes.go | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 lexer/runes.go diff --git a/lexer/runes.go b/lexer/runes.go new file mode 100644 index 0000000..9b13b5a --- /dev/null +++ b/lexer/runes.go @@ -0,0 +1,9 @@ +package lexer + +import "unicode" + +// IsSpace reports whether the rune is a space character as +// defined by Unicode's White Space property +func IsSpace(r rune) bool { + return unicode.IsSpace(r) +} -- 2.17.1 From eb9261b47807cd464aced3d93fbdba6dcd1b6f0a Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Tue, 29 Aug 2023 23:06:38 +0000 Subject: [PATCH 3/3] lexer: NewIsNot(), NewIsIn(), NewIsOneOf() Signed-off-by: Alejandro Mery --- lexer/runes.go | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/lexer/runes.go b/lexer/runes.go index 9b13b5a..a69992d 100644 --- a/lexer/runes.go +++ b/lexer/runes.go @@ -1,6 +1,38 @@ 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 // defined by Unicode's White Space property -- 2.17.1