-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner.go
373 lines (346 loc) · 8.29 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package svcparse
import (
"bufio"
"bytes"
"fmt"
"io"
"io/ioutil"
"strings"
"unicode"
)
type RuneReader struct {
Contents []rune
ContentLen int
RunePos int
LineNo int
}
func (self *RuneReader) ReadRune() (rune, error) {
var toret rune = 0
var err error
if self.RunePos < self.ContentLen {
toret = self.Contents[self.RunePos]
if toret == '\n' {
self.LineNo += 1
}
self.RunePos += 1
} else {
err = io.EOF
}
return toret, err
}
func (self *RuneReader) UnreadRune() error {
if self.RunePos == 0 {
return bufio.ErrInvalidUnreadRune
}
self.RunePos -= 1
switch self.Contents[self.RunePos] {
case '\n':
self.LineNo -= 1
}
return nil
}
func NewRuneReader(r io.Reader) *RuneReader {
//contents := bytes.Runes(ioutil.ReadAll(r))
b, _ := ioutil.ReadAll(r)
contents := bytes.Runes(b)
return &RuneReader{
Contents: contents,
ContentLen: len(contents),
RunePos: 0,
LineNo: 1,
}
}
func isIdent(r rune) bool {
switch {
case unicode.IsLetter(r):
return true
case unicode.IsDigit(r):
return true
case r == '_':
return true
default:
return false
}
}
type ScanUnit struct {
InRpcDefinition bool
InRpcBody bool
BraceLevel int
LineNo int
Value []rune
}
func (self ScanUnit) String() string {
cleanval := strings.Replace(string(self.Value), "\n", "\\n", -1)
cleanval = strings.Replace(cleanval, "\t", "\\t", -1)
cleanval = strings.Replace(cleanval, "\"", "\\\"", -1)
return fmt.Sprintf(`{"value": "%v", "InRpcDefinition": %v, "InRpcBody": %v, "BraceLevel": %v, "LineNo": %v},`, cleanval, self.InRpcDefinition, self.InRpcBody, self.BraceLevel, self.LineNo)
}
var (
withinBody bool = false
withinDef bool = false
braceLev int = 0
)
func BuildScanUnit(rr *RuneReader) (*ScanUnit, error) {
rv := &ScanUnit{
false,
false,
0,
1,
[]rune{},
}
var ch rune
buf := make([]rune, 0)
setReturn := func() *ScanUnit {
rv.BraceLevel = braceLev
rv.LineNo = rr.LineNo
rv.InRpcBody = withinBody
rv.InRpcDefinition = withinDef
rv.Value = buf
return rv
}
// Populate the buffer with at least one rune so even if it's an unknown
// character it will at least return this
ch, err := rr.ReadRune()
if err != nil {
return setReturn(), err
}
buf = append(buf, ch)
switch {
case ch == '/':
// Searching for comments beginning with '/'
ch, err = rr.ReadRune()
if err != nil {
return setReturn(), err
} else if ch == '/' {
// Handle single line comments of the form '//'
buf = append(buf, ch)
for {
ch, err = rr.ReadRune()
if err != nil {
return setReturn(), err
} else if ch == '\n' {
buf = append(buf, ch)
return setReturn(), nil
}
buf = append(buf, ch)
}
} else if ch == '*' {
// Handle (potentially) multi-line comments of the form '/**/'
buf = append(buf, ch)
for {
ch, err = rr.ReadRune()
if err != nil {
return setReturn(), err
} else if ch == '*' {
buf = append(buf, ch)
ch, err = rr.ReadRune()
if err != nil {
return setReturn(), err
} else if ch == '/' {
buf = append(buf, ch)
return setReturn(), nil
}
} else {
// Add the body of the comment to the buffer
buf = append(buf, ch)
}
}
} else {
// Not a comment, so unread the last Rune and return this '/' only
rr.UnreadRune()
return setReturn(), nil
}
case ch == '"':
// Handle strings
for {
ch, err = rr.ReadRune()
if err != nil {
return setReturn(), err
} else if ch == '\\' {
// Handle escape sequences within strings
buf = append(buf, ch)
ch, err = rr.ReadRune()
if err != nil {
return setReturn(), err
} else {
buf = append(buf, ch)
}
} else if ch == '"' {
// Closing quotation
buf = append(buf, ch)
return setReturn(), nil
} else {
buf = append(buf, ch)
}
}
case unicode.IsSpace(ch):
// Group consecutive white space characters
for {
ch, err = rr.ReadRune()
if err != nil {
// Don't pass along this EOF since we did find a valid 'Unit'
// to return. This way, the next call of this function will
// return EOF and nothing else, a more clear behavior.
if err == io.EOF {
return setReturn(), nil
}
return setReturn(), err
} else if !unicode.IsSpace(ch) {
rr.UnreadRune()
break
}
buf = append(buf, ch)
}
case isIdent(ch):
// Group consecutive letters
for {
ch, err = rr.ReadRune()
if err != nil {
if err == io.EOF {
return setReturn(), nil
}
return setReturn(), err
} else if !isIdent(ch) {
rr.UnreadRune()
if string(buf) == "service" {
withinDef = true
}
break
}
buf = append(buf, ch)
}
case ch == '{':
braceLev += 1
if withinDef == true {
withinDef = false
withinBody = true
}
case ch == '}':
braceLev -= 1
if withinBody == true && braceLev == 0 {
withinBody = false
}
}
// Implicitly, everything that's not a group of letters, not a group of
// whitespace, not a comment, and not a string-literal, (common examples of
// runes in this category are numbers and symbols like '&' or '9') will be
// returned one rune at a time.
return setReturn(), nil
}
// Service scanner conducts many of the basic scanning operatiions of a Lexer,
// with some additional service-specific behavior.
//
// Since this scanners is specifically for scanning the Protobuf service
// definitions, it will only scan the sections of the input from the reader
// that it believes are part of a service definition. This means that it will
// "fast forward" through its input reader until it finds the start of a
// service definition. It will keep track of braces (the "{}" characters) till
// it finds the final closing brace marking the end of the service definition.
type SvcScanner struct {
R *RuneReader
InDefinition bool
InBody bool
BraceLevel int
Buf []*ScanUnit
UnitPos int
lineNo int
}
func NewSvcScanner(r io.Reader) *SvcScanner {
b := make([]*ScanUnit, 0)
rr := NewRuneReader(r)
for {
unit, err := BuildScanUnit(rr)
if err == nil {
b = append(b, unit)
} else {
break
}
}
return &SvcScanner{
R: NewRuneReader(r),
InDefinition: false,
InBody: false,
BraceLevel: 0,
Buf: b,
UnitPos: 0,
}
}
// FastForward will move the current position of the internal RuneReader to the
// beginning of the next service definition. If the scanner is in the middle of
// an existing service definition, this method will do nothing.
func (self *SvcScanner) FastForward() error {
if self.InBody || self.InDefinition {
return nil
}
search_str := string("service")
for {
buf, err := self.ReadUnit()
if err != nil {
return err
}
if string(buf) == search_str {
self.UnitPos -= 1
break
}
}
return nil
}
// ReadUnit returns the next "group" of runes found in the input stream. If the
// end of the stream is reached, io.EOF will be returned as error. No other
// errors will be returned.
func (self *SvcScanner) ReadUnit() ([]rune, error) {
var rv []rune
var err error
if self.UnitPos < len(self.Buf) {
unit := self.Buf[self.UnitPos]
self.InBody = unit.InRpcBody
self.InDefinition = unit.InRpcDefinition
self.BraceLevel = unit.BraceLevel
self.lineNo = unit.LineNo
rv = unit.Value
self.UnitPos += 1
} else {
err = io.EOF
}
return rv, err
}
func (self *SvcScanner) UnreadUnit() error {
if self.UnitPos == 0 {
return fmt.Errorf("Cannot unread when scanner is at start of input")
}
// If we're on the first unit, Unreading means setting the state of the
// scanner back to it's defaults.
if self.UnitPos == 1 {
self.UnitPos = 0
self.InBody = false
self.InDefinition = false
self.BraceLevel = 0
self.lineNo = 0
}
self.UnitPos -= 1
// Since the state of the scanner usually tracks one behind the `unit`
// indicated by `UnitPos` we further subtract one when selecting the unit
// to reflect the state of
unit := self.Buf[self.UnitPos-1]
self.InBody = unit.InRpcBody
self.InDefinition = unit.InRpcDefinition
self.BraceLevel = unit.BraceLevel
self.lineNo = unit.LineNo
return nil
}
func (self *SvcScanner) UnReadToPosition(position int) error {
for {
if self.UnitPos != position {
err := self.UnreadUnit()
if err != nil {
return err
}
} else {
break
}
}
return nil
}
func (self *SvcScanner) GetLineNumber() int {
return self.lineNo
}