forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 1
/
scanner.go
71 lines (63 loc) · 1.36 KB
/
scanner.go
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package wavefront
import (
"bufio"
"io"
)
// Lexical Point Scanner
type PointScanner struct {
r *bufio.Reader
}
func NewScanner(r io.Reader) *PointScanner {
return &PointScanner{r: bufio.NewReader(r)}
}
// read reads the next rune from the buffered reader.
// Returns rune(0) if an error occurs (or io.EOF is returned).
func (s *PointScanner) read() rune {
ch, _, err := s.r.ReadRune()
if err != nil {
return eof
}
return ch
}
// unread places the previously read rune back on the reader.
func (s *PointScanner) unread() {
_ = s.r.UnreadRune()
}
// Scan returns the next token and literal value.
func (s *PointScanner) Scan() (Token, string) {
// Read the next rune
ch := s.read()
if isWhitespace(ch) {
return WS, string(ch)
} else if isLetter(ch) {
return LETTER, string(ch)
} else if isNumber(ch) {
return NUMBER, string(ch)
} else if isDelta(ch) {
return DELTA, string(ch)
}
// Otherwise read the individual character.
switch ch {
case eof:
return EOF, ""
case '\n':
return NEWLINE, string(ch)
case '.':
return DOT, string(ch)
case '-':
return MINUS_SIGN, string(ch)
case '_':
return UNDERSCORE, string(ch)
case '/':
return SLASH, string(ch)
case '\\':
return BACKSLASH, string(ch)
case ',':
return COMMA, string(ch)
case '"':
return QUOTES, string(ch)
case '=':
return EQUALS, string(ch)
}
return ILLEGAL, string(ch)
}