aboutsummaryrefslogtreecommitdiff
path: root/scanners.go
diff options
context:
space:
mode:
authorAlexander Kiryukhin <a.kiryukhin@mail.ru>2022-06-13 04:31:31 +0300
committerAlexander Kiryukhin <a.kiryukhin@mail.ru>2022-06-13 04:31:31 +0300
commit05626592208f56d88523887e2b80c0514f8ac210 (patch)
treeb0db34890e7366008754e975a8f28e878c5b28bf /scanners.go
initial
Diffstat (limited to 'scanners.go')
-rw-r--r--scanners.go50
1 files changed, 50 insertions, 0 deletions
diff --git a/scanners.go b/scanners.go
new file mode 100644
index 0000000..323e7a1
--- /dev/null
+++ b/scanners.go
@@ -0,0 +1,50 @@
+package lexpr
+
+import (
+ "strings"
+)
+
+const (
+ digits = "0123456789"
+ alpha = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM"
+ chars = "+-*/=<>@&|:!."
+)
+
+// scanNumber simplest scanner that accepts decimal int and float.
+func scanNumber(l *lex) bool {
+ l.acceptWhile(digits, false)
+ if l.atStart() {
+ // not found any digit
+ return false
+ }
+ l.accept(".")
+ l.acceptWhile(digits, false)
+ return !l.atStart()
+}
+
+// scanWord returns true if next input token contains alphanum sequence that not starts from digit and not contains.
+// spaces or special characters.
+func scanWord(l *lex) bool {
+ if !l.accept(alpha) {
+ return false
+ }
+ l.acceptWhile(alpha+digits, false)
+ return true
+}
+
+func scanOps(l *lex) bool {
+ return l.acceptWhile(chars, false)
+}
+
+// scanQuotedString returns true if next input tokens is quoted string. Can be used with any type of quotes.
+func scanQuotedString(l *lex, quote string) bool {
+ start := l.pos
+ if !strings.ContainsRune(quote, l.next()) {
+ l.pos = start
+ return false
+ }
+ if l.acceptWhileNot(quote, true) {
+ l.next()
+ }
+ return !l.atStart()
+}