-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
53 lines (50 loc) · 898 Bytes
/
example_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
package ybase_test
import (
"bytes"
"fmt"
"unicode"
"github.com/berquerant/ybase"
)
func ExampleLexer() {
input := "1 + 12 - (34-56)"
s := ybase.NewLexer(ybase.NewScanner(ybase.NewReader(bytes.NewBufferString(input), nil), func(r ybase.Reader) int {
r.DiscardWhile(unicode.IsSpace)
top := r.Peek()
switch {
case unicode.IsDigit(top):
r.NextWhile(unicode.IsDigit)
return 901
default:
switch top {
case '+':
_ = r.Next()
return 911
case '-':
_ = r.Next()
return 912
case '(':
_ = r.Next()
return 921
case ')':
_ = r.Next()
return 922
}
}
return ybase.EOF
}))
for s.DoLex(func(tok ybase.Token) { fmt.Printf("%d %s\n", tok.Type(), tok.Value()) }) != ybase.EOF {
}
if err := s.Err(); err != nil {
panic(err)
}
// Output:
// 901 1
// 911 +
// 901 12
// 912 -
// 921 (
// 901 34
// 912 -
// 901 56
// 922 )
}