-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_6_test.go
73 lines (65 loc) · 1.57 KB
/
example_6_test.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
package scan_test
import (
"fmt"
"github.com/blackchip-org/scan"
)
func scanInt(s *scan.Scanner) bool {
if !scan.IsDigit09(s.This) {
return false
}
s.Type = scan.IntType
scan.While(s, scan.IsDigit09, s.Keep)
return true
}
func scanWord(s *scan.Scanner) bool {
if !scan.IsLetter(s.This) {
return false
}
s.Type = scan.WordType
scan.While(s, scan.IsLetter, s.Keep)
return true
}
func scanSpace(s *scan.Scanner) bool {
if !scan.IsSpace(s.This) {
return false
}
s.Type = scan.SpaceType
scan.While(s, scan.IsSpace, s.Keep)
return true
}
func Example_example6() {
scanFuncs := []func(*scan.Scanner) bool{
scanSpace,
scanWord,
scanInt,
}
var toks []scan.Token
s := scan.NewScannerFromString("example6", "abc 123 !@# \tdef456")
for s.HasMore() {
match := false
for _, fn := range scanFuncs {
if match = fn(s); match {
toks = append(toks, s.Emit())
break
}
}
if !match {
scan.Until(s, scan.IsSpace, s.Keep)
s.Illegal("unexpected %v", scan.Quote(s.Val.String()))
toks = append(toks, s.Emit())
}
}
fmt.Println(scan.FormatTokenTable(toks))
// Output:
//
// Pos Type Value Literal
// example6:1:1 word "abc" "abc"
// example6:1:4 space " " " "
// example6:1:5 int "123" "123"
// example6:1:8 space " " " "
// example6:1:9 illegal "!@#" "!@#"
// example6:1:12: error: unexpected "!@#"
// example6:1:12 space " {!ch:\t}" " {!ch:\t}"
// example6:1:15 word "def" "def"
// example6:1:18 int "456" "456"
}