-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner.go
83 lines (73 loc) · 1.35 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
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"bytes"
"fmt"
"io"
"strconv"
"strings"
)
//Scanner is a cool struct
type Scanner struct {
r *strings.Reader
}
//NewScanner is a cool func
func NewScanner(reader *strings.Reader) Scanner {
return Scanner{r: reader}
}
//Scan is a cool func
func (s Scanner) Scan() (Token, string) {
var buffer bytes.Buffer
ch, _, err := s.r.ReadRune()
buffer.WriteRune(ch)
fmt.Println(buffer.String())
if err == io.EOF {
return EOF, ""
}
for {
ch, _, err := s.r.ReadRune()
if err != io.EOF {
fmt.Println("Writing")
buffer.WriteRune(ch)
} else {
fmt.Println("Breaking")
break
}
}
switch bufferValue := buffer.String(); {
case bufferValue == "SELECT":
return SELECT, "SELECT"
case bufferValue == "FROM":
return FROM, "FROM"
case bufferValue == "*":
return ASTERISK, "*"
case isWhitespace(rune(bufferValue[0])):
return WS, bufferValue
default:
return IDENT, buffer.String()
}
return ASTERISK, "*"
}
func isWhitespace(ch rune) bool {
return ch == '\t' || ch == '\n' || ch == '\r' || ch == ' '
}
func (t Token) ToS() string {
defaultValue := strconv.Itoa(int(t))
switch t {
case SELECT:
return "SELECT"
case FROM:
return "FROM"
case ASTERISK:
return "ASTERISK"
case EOF:
return "EOF"
case ILLEGAL:
return "ILLEGAL"
case IDENT:
return "IDENT"
case WS:
return "WS"
default:
return defaultValue
}
}