-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_7_test.go
91 lines (76 loc) · 1.96 KB
/
example_7_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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package scan_test
import (
"fmt"
"github.com/blackchip-org/scan"
)
type IntRule struct {
isDigit scan.Class
}
func NewIntRule(isDigit scan.Class) IntRule {
return IntRule{isDigit: isDigit}
}
func (r IntRule) Eval(s *scan.Scanner) bool {
if !r.isDigit(s.This) {
return false
}
s.Type = scan.IntType
scan.While(s, r.isDigit, s.Keep)
return true
}
type WordRule struct {
isLetter scan.Class
}
func NewWordRule(isLetter scan.Class) WordRule {
return WordRule{isLetter: isLetter}
}
func (r WordRule) Eval(s *scan.Scanner) bool {
if !r.isLetter(s.This) {
return false
}
s.Type = scan.WordType
scan.While(s, r.isLetter, s.Keep)
return true
}
type SpaceRule struct {
isSpace scan.Class
}
func NewSpaceRule(isSpace scan.Class) SpaceRule {
return SpaceRule{isSpace: isSpace}
}
func (r SpaceRule) Eval(s *scan.Scanner) bool {
if !r.isSpace(s.This) {
return false
}
s.Type = scan.SpaceType
scan.While(s, r.isSpace, s.Keep)
return true
}
func UnexpectedUntil(c scan.Class) func(*scan.Scanner) {
return func(s *scan.Scanner) {
scan.Until(s, c, s.Keep)
s.Illegal("unexpected %s", scan.Quote(s.Lit.String()))
}
}
func Example_example7() {
rules := scan.NewRuleSet(
NewSpaceRule(scan.IsSpace),
NewWordRule(scan.IsLetter),
NewIntRule(scan.IsDigit),
).WithNoMatchFunc(UnexpectedUntil(scan.IsSpace))
s := scan.NewScannerFromString("example7", "abc 123 !@# \tdef456")
runner := scan.NewRunner(s, rules)
toks := runner.All()
fmt.Println(scan.FormatTokenTable(toks))
// Output:
//
// Pos Type Value Literal
// example7:1:1 word "abc" "abc"
// example7:1:4 space " " " "
// example7:1:5 int "123" "123"
// example7:1:8 space " " " "
// example7:1:9 illegal "!@#" "!@#"
// example7:1:12: error: unexpected "!@#"
// example7:1:12 space " {!ch:\t}" " {!ch:\t}"
// example7:1:15 word "def" "def"
// example7:1:18 int "456" "456"
}