-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner.go
203 lines (161 loc) · 4.18 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package markers
import (
"fmt"
)
const Whitespace = 1<<'\t' | 1<<'\r' | 1<<' '
const (
EOF = -(iota + 1)
Identifier
IntegerValue
StringValue
)
type Scanner struct {
source []byte
tokenStartPosition int
tokenEndPosition int
searchIndex int
character rune
errorCount int
ErrorCallback func(scanner *Scanner, message string)
}
func NewScanner(source string) *Scanner {
return &Scanner{
source: []byte(source),
character: Identifier,
tokenStartPosition: -1,
tokenEndPosition: 0,
searchIndex: -1,
errorCount: 0,
}
}
func (scanner *Scanner) SearchIndex() int {
return scanner.searchIndex
}
func (scanner *Scanner) SourceLength() int {
return len(scanner.source)
}
func (scanner *Scanner) ErrorCount() int {
return scanner.errorCount
}
func (scanner *Scanner) AddError(message string) {
scanner.errorCount++
if scanner.ErrorCallback != nil {
scanner.ErrorCallback(scanner, message)
}
}
func (scanner *Scanner) Peek() rune {
if scanner.character == Identifier {
scanner.character = scanner.Next()
}
return scanner.character
}
func (scanner *Scanner) Expect(expected rune, description string) bool {
token := scanner.Scan()
if token != expected {
//scanner.AddError(fmt.Sprintf("got %q; want %s", scanner.Token(), description))
return false
}
return true
}
func (scanner *Scanner) Reset() {
scanner.searchIndex = 0
scanner.character = rune(scanner.source[0])
scanner.tokenStartPosition = 0
scanner.tokenEndPosition = 0
}
func (scanner *Scanner) SetSearchIndex(searchIndex int) {
if searchIndex >= scanner.SourceLength() {
scanner.searchIndex = scanner.SourceLength()
return
}
scanner.searchIndex = searchIndex
scanner.character = rune(scanner.source[searchIndex])
}
func (scanner *Scanner) Next() rune {
scanner.searchIndex++
if scanner.searchIndex >= scanner.SourceLength() {
return EOF
}
return rune(scanner.source[scanner.searchIndex])
}
func (scanner *Scanner) SkipWhitespaces() rune {
character := scanner.Peek()
for Whitespace&(1<<uint(character)) != 0 {
character = scanner.Next()
}
scanner.character = character
return character
}
func (scanner *Scanner) Scan() rune {
character := scanner.SkipWhitespaces()
token := character
if IsIdentifier(character, 0) {
token = Identifier
character = scanner.ScanIdentifier()
} else if IsDecimal(character) {
token = IntegerValue
character = scanner.ScanNumber()
} else if character == EOF {
return EOF
} else if character == '"' {
token = StringValue
scanner.ScanString('"')
character = scanner.Peek()
} else if character == '`' {
token = StringValue
scanner.ScanString('`')
character = scanner.Peek()
} else {
scanner.tokenStartPosition = scanner.searchIndex
character = scanner.Next()
scanner.tokenEndPosition = scanner.searchIndex
}
scanner.character = character
return token
}
func (scanner *Scanner) ScanNumber() rune {
if IsDecimal(scanner.SkipWhitespaces()) {
scanner.tokenStartPosition = scanner.searchIndex
}
character := scanner.SkipWhitespaces()
for IsDecimal(character) {
character = scanner.Next()
}
scanner.tokenEndPosition = scanner.searchIndex
scanner.character = character
return character
}
func (scanner *Scanner) ScanIdentifier() rune {
if IsIdentifier(scanner.SkipWhitespaces(), 1) {
scanner.tokenStartPosition = scanner.searchIndex
}
character := scanner.SkipWhitespaces()
for index := 1; IsIdentifier(character, index); index++ {
character = scanner.Next()
}
scanner.tokenEndPosition = scanner.searchIndex
scanner.character = character
return character
}
func (scanner *Scanner) ScanString(quote rune) (len int) {
scanner.tokenStartPosition = scanner.searchIndex
character := scanner.Next()
for character != quote {
if character == '\n' || character < 0 {
scanner.AddError(fmt.Sprintf("'%c' is missing", quote))
return
}
character = scanner.Next()
len++
}
character = scanner.Next()
scanner.tokenEndPosition = scanner.searchIndex
scanner.character = character
return
}
func (scanner *Scanner) Token() string {
if scanner.tokenStartPosition < 0 {
return ""
}
return string(scanner.source[scanner.tokenStartPosition:scanner.tokenEndPosition])
}