-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken_test.go
161 lines (136 loc) · 3.83 KB
/
token_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
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
package simplexer_test
import (
"fmt"
"strings"
"testing"
"github.com/macrat/simplexer"
)
func ExampleNewRegexpTokenType() {
const (
NUMBER simplexer.TokenID = iota
OTHERS
)
lexer := simplexer.NewLexer(strings.NewReader("123this is test456"))
lexer.TokenTypes = []simplexer.TokenType{
simplexer.NewRegexpTokenType(NUMBER, `[0-9]+`),
simplexer.NewRegexpTokenType(OTHERS, `[^0-9]+`),
}
for {
token, _ := lexer.Scan()
if token == nil {
break
}
if token.Type.GetID() == NUMBER {
fmt.Printf("%s is number\n", token.Literal)
}
if token.Type.GetID() == OTHERS {
fmt.Printf("%s is not number\n", token.Literal)
}
}
// Output:
// 123 is number
// this is test is not number
// 456 is number
}
func ExampleNewPatternTokenType() {
const (
HOGE simplexer.TokenID = iota
OTHERS
)
lexer := simplexer.NewLexer(strings.NewReader("this is hoge and HOGE or Hoge"))
lexer.TokenTypes = []simplexer.TokenType{
simplexer.NewPatternTokenType(HOGE, []string{"hoge", "HOGE"}),
simplexer.NewRegexpTokenType(OTHERS, `[^ ]+`),
}
for {
token, _ := lexer.Scan()
if token == nil {
break
}
if token.Type.GetID() == HOGE {
fmt.Printf("!!! %s !!!\n", token.Literal)
}
if token.Type.GetID() == OTHERS {
fmt.Println(token.Literal)
}
}
// Output:
// this
// is
// !!! hoge !!!
// and
// !!! HOGE !!!
// or
// Hoge
}
func TestRegexpTokenType(t *testing.T) {
tt := simplexer.NewRegexpTokenType(1, `[0-9]+(\.[0-9]+)?`)
if tok := tt.FindToken("not match 123", simplexer.Position{}); tok != nil {
t.Errorf("excepted nil but got %#v", tok)
}
pos := simplexer.Position{Line: 1, Column: 2}
if tok := tt.FindToken("123.1abc", pos); tok == nil {
t.Errorf("excepted token but got nil")
} else {
if tok.Type != tt {
t.Errorf("excepted token type is %#v but got %#v", &tt, &tok.Type)
}
if tok.Type.GetID() != 1 {
t.Errorf("excepted token type ID is 1 but got %#v", tok.Type.GetID())
}
if tok.Literal != "123.1" {
t.Errorf("excepted literal of token is 123.1 but got %#v", tok.Literal)
}
if len(tok.Submatches) != 1 || tok.Submatches[0] != ".1" {
t.Errorf("excepted submatches of token is %#v but got %#v", []string{".1"}, tok.Submatches)
}
if tok.Position != pos {
t.Errorf("excepted position of token is %#v but got %#v", pos, tok.Position)
}
}
}
func TestPatternTokenType(t *testing.T) {
tt := simplexer.NewPatternTokenType(1, []string{"abc", "def"})
if tok := tt.FindToken("not match abc", simplexer.Position{}); tok != nil {
t.Errorf("excepted nil but got %#v", tok)
}
pos := simplexer.Position{Line: 1, Column: 2}
if tok := tt.FindToken("abc def", pos); tok == nil {
t.Errorf("excepted token but got nil")
} else {
if tok.Type != tt {
t.Errorf("excepted token type is %#v but got %#v", &tt, &tok.Type)
}
if tok.Type.GetID() != 1 {
t.Errorf("excepted token type ID is 1 but got %#v", tok.Type.GetID())
}
if tok.Literal != "abc" {
t.Errorf("excepted literal of token is abc but got %#v", tok.Literal)
}
if len(tok.Submatches) != 0 {
t.Errorf("excepted submatches of token is empty but got %#v", tok.Submatches)
}
if tok.Position != pos {
t.Errorf("excepted position of token is %#v but got %#v", pos, tok.Position)
}
}
if tok := tt.FindToken("def", pos); tok == nil {
t.Errorf("excepted token but got nil")
} else {
if tok.Type != tt {
t.Errorf("excepted token type is %#v but got %#v", &tt, &tok.Type)
}
if tok.Type.GetID() != 1 {
t.Errorf("excepted token type ID is 1 but got %#v", tok.Type.GetID())
}
if tok.Literal != "def" {
t.Errorf("excepted literal of token is def but got %#v", tok.Literal)
}
if len(tok.Submatches) != 0 {
t.Errorf("excepted submatches of token is empty but got %#v", tok.Submatches)
}
if tok.Position != pos {
t.Errorf("excepted position of token is %#v but got %#v", pos, tok.Position)
}
}
}