/
scanner.go
executable file
·506 lines (463 loc) · 12.5 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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
// Copyright (c) 2024 Dez Little <deslittle@gmail.com>
// All rights reserved. Use of this source code is governed by a LGPL v3
// license that can be found in the LICENSE file.
// scanner.go implements a Scanner for any DSL source text.
// When a Scanner is created it takes a bufio.Reader as the source of
// the text, scans a number of characters (runes) as defined by the
// user ScanFunc and returns a token to the Parser.
package dsl
import (
"bufio"
"bytes"
"fmt"
"log"
)
type dslLogger struct {
indent int
log *log.Logger
buf []interface{}
}
// The Scanner contains a reference to the user scan function, the
// user input buffer, various state variables and the parser log.
type Scanner struct {
fn ScanFunc
r *bufio.Reader
buf struct {
runes []rune
unread int
}
curLineBuffer bytes.Buffer
startLine int
curLine int
startPos int
curPos int
options ScanOptions
expRunes []rune
tok Token
error *Error
logger *dslLogger
eof bool
}
// NewScanner returns a new instance of Scanner.
func newScanner(sf ScanFunc, r *bufio.Reader, l *log.Logger) *Scanner {
s := &Scanner{
fn: sf,
r: r,
curLine: 1,
curPos: 1,
}
if l != nil {
s.logger = &dslLogger{log: l}
}
return s
}
type ScanFunc func(*Scanner) Token
// If the Optional option is false and a match is not found, an error is returned to the
// parser.
//
// If the Multiple option is set to true the scanner continues to read, consume runes and
// take branches until a rune is read that is not matched by any of the branches or branch
// ranges. If the Multiple option is set to false, only the first branch (or branch range)
// to be matched is taken and consumed.
//
// If the Invert option is set to true the scanner consumes the rune and takes a branch if
// it doesn't match any of the branch or branch ranges.
//
// If the the Skip option is set to true the scanner will take the branch if a match is
// found but will not consume the rune.
//
// If ScanOptions is omitted when creating ExpectRune{}, all options will be set to
// false.
type ScanOptions struct {
Optional bool
Multiple bool
Invert bool
Error func(*Scanner)
}
type ExpectRune struct {
Branches []Branch
BranchRanges []BranchRange
Options ScanOptions
}
type Branch struct {
Rn rune
Fn func(*Scanner)
}
type BranchRange struct {
StartRn rune
EndRn rune
Fn func(*Scanner)
}
type BranchString struct {
BranchString string
Fn func(*Scanner)
}
type Match struct {
Literal string
ID TokenType
}
// The user scan function should return the result of Exit()
func (s *Scanner) Exit() Token {
if s.tok.ID == "" {
return Token{
ID: TOKEN_UNKNOWN,
Literal: "UNKNOWN",
Line: s.curLine,
Position: s.curPos,
}
}
return s.tok
}
// Expect first reads a rune from s.read() and then tries to match it against input
// branches. If a match is found, the rune is 'consumed' (i.e. rune is put on the
// scanned buffer) and the branch is 'taken' (i.e. the branch function is called).
// If a match is not found, the read rune is then compared to each of the branch ranges.
//
// If a match is not found in either the branches or branch ranges and the Optional
// option is set to true, the scanner returns to the user scan function without calling
// any of the branch functions, but still consumes the rune.
//
// Any runes that are read but not consumed or skipped will be unread.
func (s *Scanner) Expect(expect ExpectRune) {
var found1orMore bool
var found1inverted bool
var found bool
var rn rune
if logenb {
s.log(fmt.Sprintf("Expect %v ", getScanOptions(expect.Options)), NEWLINE) //TODO Custom Print Function
s.log(fmt.Sprintf("Rune: %v ", branchesToStrings(expect.Branches)), NO_PREFIX)
s.log(fmt.Sprintf("Range: %v ", branchRangesToStrings(expect.BranchRanges)), NO_PREFIX)
}
for {
found = false
rn = s.read()
for _, branch := range expect.Branches {
if branch.Rn == rn {
if !expect.Options.Invert {
s.consume(rn, found1orMore)
}
found1orMore = true
found = true
s.callFn(branch.Fn)
break
}
}
if !found {
for _, branch := range expect.BranchRanges {
if branch.StartRn <= rn && rn <= branch.EndRn {
if !expect.Options.Invert {
s.consume(rn, found1orMore)
}
found1orMore = true
found = true
s.callFn(branch.Fn)
break
}
}
}
if (!expect.Options.Invert && !found) || (expect.Options.Invert && found) {
s.unread()
break
}
if expect.Options.Invert && !found {
s.consume(rn, found1inverted)
found1inverted = true
}
if !expect.Options.Multiple {
break
}
}
if !expect.Options.Invert && !found1orMore && !expect.Options.Optional {
s.expRunes = append(s.expRunes, rn)
strings := append(branchesToStrings(expect.Branches), branchRangesToStrings(expect.BranchRanges)...)
s.error = s.newError(ERROR_RUNE_EXPECTED_NOT_FOUND, fmt.Errorf("found [%v], expected any of %v", string(rn), strings))
} else if expect.Options.Invert && !found1inverted && !expect.Options.Optional {
s.expRunes = append(s.expRunes, rn)
strings := append(branchesToStrings(expect.Branches), branchRangesToStrings(expect.BranchRanges)...)
s.error = s.newError(ERROR_RUNE_EXPECTED_NOT_FOUND, fmt.Errorf("found [%v], expected any except %v", string(rn), strings))
}
}
func (s *Scanner) consume(rn rune, found1orMore bool) {
if logenb {
if !found1orMore {
s.log(fmt.Sprintf("Pos:%v ", s.curPos), NO_PREFIX)
s.log("Found: ", NO_PREFIX)
s.log(sanitize(string(rn), true), NO_PREFIX)
} else {
s.log(", ", NO_PREFIX)
s.log(sanitize(string(rn), true), NO_PREFIX)
}
}
s.expRunes = append(s.expRunes, rn)
s.curPos++
if rn == '\n' {
s.curLine++
s.curPos = 1
s.curLineBuffer.Reset()
if logenb {
s.log(fmt.Sprintf("Line %v:", s.curLine), STARTLINE)
}
} else {
s.curLineBuffer.WriteRune(rn)
}
}
func (s *Scanner) Call(fn func(*Scanner)) {
if logenb {
s.log("Call", NEWLINE)
}
s.callFn(fn)
}
func (s *Scanner) callFn(fn func(*Scanner)) {
if fn != nil {
if logenb {
s.log("Scanning: "+getFuncName(fn), INCREMENT)
}
fn(s)
if logenb {
s.log("Returning: "+getFuncName(fn), DECREMENT)
}
}
}
// Match is required to be called by the user scan function before it
// returns to the user parse function, otherwise it will return the
// token NOT_MATCHED. Match will match every rune currently accepted by
// Expect() and not skipped (s.scanStr), against the input string.
//
// Once matched a Token is generated from the input ID, s.scanStr and
// the current line and position of the scanner. Once the user scan
// function has matched a token, any subsequent calls to Match will
// do nothing until the user scan function returns and is called again
// and reset (by s.init()) by the parser.
func (s *Scanner) Match(matches []Match) {
if s.tok.ID != "" {
return
}
expString := runesToString(s.expRunes)
for _, match := range matches {
if expString == match.Literal || match.Literal == "" {
if logenb {
s.log("Matched: "+match.ID.String(), NEWLINE)
s.log(" - ", NO_PREFIX)
s.log(sanitize(expString, true), NO_PREFIX)
}
s.tok = Token{match.ID, expString, s.curLine, s.curPos - len(expString)}
break
}
}
}
func (s *Scanner) SkipRune() {
if logenb {
s.log("Skip Rune: ", NEWLINE)
}
if len(s.expRunes) > 0 {
rn := s.expRunes[len(s.expRunes)-1]
s.expRunes = s.expRunes[:len(s.expRunes)-1]
if logenb {
s.log(sanitize(string(rn), true)+", ", NO_PREFIX)
}
} else {
if logenb {
s.log("Warning: No Runes to Skip", ERROR)
}
}
}
// Creates a new error and passes it to the parser. Only one error is generated by the
// scanner as it exits immediately after an error
func (s *Scanner) newError(code ErrorCode, err error) *Error {
if logenb {
s.log(err.Error(), ERROR)
}
lineString := s.getLine()
if s.error == nil {
return &Error{
code,
err,
lineString,
s.startLine,
s.startPos,
s.curLine,
s.curPos,
}
}
return nil
}
// scan is the entry point from the parser.
func (s *Scanner) scan() (Token, *Error) {
s.init()
if logenb {
s.log("Scanning: "+getFuncName(s.fn), INCREMENT)
defer s.log("Returning: "+getFuncName(s.fn), DECREMENT) // use defer keyword to log after the fn has returned
}
return s.fn(s), s.error // Call the user ScanFunc with a reference to the p.s scanner
}
// read reads the next rune from the bufferred reader. Only read from the
// bufio reader s.r if it hasn't already been read. Using another buffer
// s.buf means we can read and unread as many runes as we like.
func (s *Scanner) read() rune {
if s.buf.unread > 0 {
rn := s.buf.runes[len(s.buf.runes)-s.buf.unread]
s.buf.unread--
return rn
}
rn, _, err := s.r.ReadRune() // We don't use s.r.UnreadRune as it can only be called once
s.buf.runes = append(s.buf.runes, rn)
// Assume an err means we have reached End of File
if err != nil {
return rune(0)
}
return rn
}
// To unread simply increment the index to the rune buffer
func (s *Scanner) unread() {
if s.buf.unread < len(s.buf.runes) { // Ensure we don't unread more runes than have been read
s.buf.unread++
}
}
// Reset the scanner after every s.callFn() call
func (s *Scanner) init() {
s.tok.ID = ""
s.expRunes = nil
s.error = nil
s.startLine = s.curLine
s.startPos = s.curPos
}
// log is where all lines are added to the log.
// It is invoked with a number of indent options.
func (s *Scanner) log(msg string, indent indent) {
if s.logger == nil {
return
}
l := s.logger
switch indent {
case INCREMENT:
{
if l.buf != nil {
l.log.Print(l.buf...)
l.buf = nil
}
l.indent++
prefix := ""
for i := 0; i < l.indent; i++ {
prefix += "\t"
}
l.log.SetPrefix(prefix)
}
case DECREMENT:
{
if l.buf != nil {
l.log.Print(l.buf...)
l.buf = nil
}
l.indent--
prefix := ""
for i := 0; i < l.indent; i++ {
prefix += "\t"
}
l.log.Print(msg)
l.log.SetPrefix(prefix)
return
}
case NEWLINE:
{
if l.buf != nil {
l.log.Print(l.buf...)
l.buf = nil
}
}
case NO_PREFIX:
{
//noop
}
case STARTLINE:
{
if l.buf != nil {
l.log.Print(l.buf...)
l.buf = nil
}
prefix := l.log.Prefix()
l.log.SetPrefix("")
l.log.Print(msg)
l.log.SetPrefix(prefix)
return
}
case ERROR:
{
if l.buf != nil {
l.log.Print(l.buf...)
l.buf = nil
}
prefix := l.log.Prefix()
l.log.SetPrefix("***")
l.log.Print(msg)
l.log.SetPrefix(prefix)
return
}
}
l.buf = append(l.buf, msg)
}
// getLine is used to scan the rest of the current line to display in the Error
func (s *Scanner) getLine() string {
var numRunes int
var tempBuffer bytes.Buffer
tempBuffer.WriteString(s.curLineBuffer.String())
for {
rn := s.read()
numRunes++
if rn == '\n' || rn == rune(0) { // Make sure you break on New Line or End of File
break
} else {
tempBuffer.WriteRune(rn)
}
}
// Put all read runes back onto the buffer
for i := 0; i < numRunes; i++ {
s.unread()
}
s.unread() // Unread the rune that was skipped
return tempBuffer.String()
}
// -------------------------------- Scanner Helper Functions---------------------------------------
// Used to log which options were used during a branch function call
func getScanOptions(opts ScanOptions) string {
var buf bytes.Buffer
buf.WriteRune('(')
if opts.Invert {
buf.WriteString("Invert ")
}
if opts.Optional {
buf.WriteString("Optional ")
}
if opts.Multiple {
buf.WriteString("Multiple ")
}
buf.WriteRune(')')
return buf.String()
}
// Used to list possible scan branches in the log and errors in Expect()
func runesToString(runes []rune) (str string) {
for _, rn := range runes {
str = str + string(rn)
}
return
}
// Used to list possible scan branches in the log and errors in Expect()
func branchesToStrings(branches []Branch) (branchStrings []string) {
for _, branch := range branches {
branchStrings = append(branchStrings, sanitize(string(branch.Rn), true))
}
return
}
// Similar to runesToStrings except that it accepts the BranchString type used in Peek()
func branchStringsToStrings(branches []BranchString) (branchStrings []string) {
for _, branch := range branches {
branchStrings = append(branchStrings, branch.BranchString)
}
return
}
// Similar to runesToStrings except that it accepts the BranchRange type used in Expect()
func branchRangesToStrings(branches []BranchRange) (branchStrings []string) {
for _, branch := range branches {
branchStrings = append(branchStrings, sanitize(string(branch.StartRn), true)+"-"+sanitize(string(branch.EndRn), true))
}
return
}