-
Notifications
You must be signed in to change notification settings - Fork 2
/
scanner.go
229 lines (197 loc) · 4.57 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package scanner
import (
"strings"
"unicode/utf8"
)
// Pos represents a byte position in the original input text from which
// this template was parsed.
type Pos int
func (p Pos) Position() Pos {
return p
}
const eof = -1
//Scanner, Iterates through a string.
type Scanner struct {
input string
start Pos
pos Pos
width Pos
curr rune
prevState ScannerState
safebackup bool //insure backup is called only once after next.
}
// NewScanner Creates a New Scanner pointer.
func NewScanner(template string) *Scanner {
return &Scanner{input: template}
}
func (this *Scanner) Position() int {
return int(this.pos)
}
func (this *Scanner) StartPosition() int {
return int(this.start)
}
func (this *Scanner) SetPosition(pos int) {
this.pos = Pos(pos)
}
func (this *Scanner) SetStartPosition(pos int) {
this.start = Pos(pos)
}
// Token return the current selected text and move the start position to the current position
func (this *Scanner) Commit() string {
r1 := this.input[this.start:this.pos]
this.start = this.pos
this.prevState = this.SaveState()
return r1
}
//IsEOF check if the end of the current string has been reached.
func (this *Scanner) IsEOF() bool {
return int(this.pos) >= len(this.input)
}
func (this *Scanner) Size() int {
return len(this.input)
}
func (this *Scanner) MoveStart(pos int) {
this.start = this.start + Pos(pos)
}
//Next returns the next rune in the input.
func (this *Scanner) Next() rune {
this.safebackup = true
if this.IsEOF() {
this.width = 0
return eof
}
r, w := utf8.DecodeRuneInString(this.input[this.pos:])
this.width = Pos(w)
this.pos += this.width
this.curr = r
return r
}
func (this *Scanner) Skip() {
this.Next()
this.Commit()
}
// Peek returns but does not consume the next rune in the input.
func (this *Scanner) Peek() rune {
r := this.Next()
this.Backup()
return r
}
// Backup steps back one rune. Can only be called once per call of next.
func (this *Scanner) Backup() {
this.pos -= this.width
}
// Rollback move the curr pos back to the start pos.
func (this *Scanner) Rollback() {
this.LoadState(this.prevState)
}
// Ignore skips over the pending input before this point.
func (this *Scanner) Ignore() {
this.start = this.pos
}
// accept consumes the next rune if it's from the valid set.
func (this *Scanner) Accept(valid string) bool {
if strings.IndexRune(valid, this.Next()) >= 0 {
return true
}
this.Backup()
return false
}
// accept consumes the next rune if it's from the valid set.
func (this *Scanner) AcceptNewLine() bool {
r := this.Next()
if r == '\r' {
r = this.Next()
if r == '\n' {
this.Ignore()
return true
}
//ignore \r char if standing allone
this.Backup()
this.Ignore()
return false
}
if r == '\n' {
this.Ignore()
return true
}
this.Backup()
return false
}
// accept consumes the next rune if it's from the valid set.
func (this *Scanner) IsNewLine() bool {
if this.Prefix("\r\n") {
return true
}
if this.Prefix("\n") {
return true
}
return false
}
// acceptRun consumes a run of runes from the valid set.
func (this *Scanner) AcceptRun(valid string) (found int) {
for strings.IndexRune(valid, this.Next()) >= 0 {
found++
}
this.Backup()
return found
}
// runTo consumes a run of runes until an item in the valid set is found.
func (this *Scanner) RunTo(valid string) rune {
state := this.SaveState()
for {
r := this.Next()
if r == eof {
this.LoadState(state)
return r
}
if strings.IndexRune(valid, r) >= 0 {
return r
}
}
this.LoadState(state)
return eof
}
// Prefix Check if content starts with same string. Does not Commit
func (this *Scanner) Prefix(pre string) bool {
if strings.HasPrefix(this.input[this.pos:], pre) {
this.pos += Pos(len(pre))
return true
}
return false
}
func (this *Scanner) SkipSpaces() {
for IsSpace(this.Next()) {
}
this.Backup()
this.Ignore()
}
func (this *Scanner) SkipToNewLine() {
for {
r := this.Next()
if this.IsEOF() {
break
}
if r == '\n' {
break
}
}
this.Ignore()
return
}
// lineNumber reports which line we're on, based on the position of
// the previous item returned by nextItem. Doing it this way
// means we don't have to worry about peek double counting.
func (this *Scanner) LineNumber() int {
return 1 + strings.Count(this.input[:this.pos], "\n")
}
type ScannerState struct {
start Pos
pos Pos
width Pos
}
func (this *Scanner) SaveState() ScannerState {
return ScannerState{start: this.start, pos: this.pos, width: this.width}
}
func (this *Scanner) LoadState(state ScannerState) {
this.start, this.pos, this.width = state.start, state.pos, state.width
}