-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_8_test.go
68 lines (59 loc) · 1.49 KB
/
example_8_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
package scan_test
import (
"fmt"
"github.com/blackchip-org/scan"
)
type IntRule2 struct {
isDigit scan.Class
isDigitSep scan.Class
}
func NewIntRule2(isDigit scan.Class) IntRule2 {
return IntRule2{
isDigit: isDigit,
isDigitSep: scan.IsNone,
}
}
func (r IntRule2) WithDigitSep(isDigitSep scan.Class) IntRule2 {
r.isDigitSep = isDigitSep
return r
}
func (r IntRule2) Eval(s *scan.Scanner) bool {
if !r.isDigit(s.This) {
return false
}
s.Keep()
for s.HasMore() {
if r.isDigit(s.This) {
s.Keep()
} else if r.isDigitSep(s.This) {
s.Skip()
} else {
break
}
}
return true
}
func Example_example8() {
rules := scan.NewRuleSet(
NewSpaceRule(scan.IsSpace),
NewWordRule(scan.IsLetter),
NewIntRule2(scan.IsDigit).
WithDigitSep(scan.Rune(',')),
).WithNoMatchFunc(UnexpectedUntil(scan.IsSpace))
s := scan.NewScannerFromString("example8", "abc 1,234 !@# \tdef45,678")
runner := scan.NewRunner(s, rules)
toks := runner.All()
fmt.Println(scan.FormatTokenTable(toks))
// Output:
//
// Pos Type Value Literal
// example8:1:1 word "abc" "abc"
// example8:1:4 space " " " "
// example8:1:5 1234 "1234" "1,234"
// example8:1:10 space " " " "
// example8:1:11 illegal "!@#" "!@#"
// example8:1:14: error: unexpected "!@#"
// example8:1:14 space " {!ch:\t}" " {!ch:\t}"
// example8:1:17 word "def" "def"
// example8:1:20 45678 "45678" "45,678"
}