aboutsummaryrefslogtreecommitdiff
path: root/scanners.go
blob: 323e7a14ee82206c22f997d745b7311d74f50988 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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()
}