-
Notifications
You must be signed in to change notification settings - Fork 4
/
tokenizer.go
193 lines (179 loc) · 4.31 KB
/
tokenizer.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package twowaysql
import (
"errors"
"strings"
"unicode"
)
type tokenKind int
const (
tkSQLStmt tokenKind = iota + 1
tkIf
tkElif
tkElse
tkEnd
tkBind
tkEndOfProgram
)
type token struct {
kind tokenKind
str string
value string /* for Bind */
condition string /* for IF/ELIF */
}
// tokenizeは文字列を受け取ってトークンの列を返す
func tokenize(str string) ([]token, error) {
var tokens []token
index := 0
start := 0
length := len(str)
//index out of boundsを避けるため末尾に空白を追加する。
str = str + " "
for index < length {
if str[index:index+2] == "/*" {
if str[index:index+3] == "/*+" {
// hint句の場合はskipする
index++
continue
}
//コメントの直前の塊をTKSQLStmtとしてappend
tokens = append(tokens, token{
kind: tkSQLStmt,
str: str[start:index],
})
start = index
index += 2
tok := token{}
for index < length && str[index:index+2] != "*/" {
if str[index:index+2] == "IF" {
tok.kind = tkIf
index += 2
continue
}
if str[index:index+4] == "ELIF" {
tok.kind = tkElif
index += 4
continue
}
if str[index:index+4] == "ELSE" {
tok.kind = tkElse
index += 4
continue
}
if str[index:index+3] == "END" {
tok.kind = tkEnd
index += 3
continue
}
index++
}
// */がなければ不正なフォーマット
if str[index:index+2] != "*/" {
return []token{}, errors.New("Comment enclosing characters do not match")
}
index += 2
if tok.kind == 0 {
tok.kind = tkBind
if quote := str[index]; quote == '(' {
// /* ... */( ... ) or /* ... */( (...), (...) )
var quoteStack []interface{}
index++
for index < length {
if str[index] == '(' {
quoteStack = append(quoteStack, struct{}{})
index++
continue
}
if str[index] == ')' {
if len(quoteStack) == 0 {
break
}
quoteStack = quoteStack[0 : len(quoteStack)-1]
index++
continue
}
index++
}
if str[index] != ')' {
return nil, errors.New("Enclosing characters do not match")
}
index++
} else if quote := str[index]; quote == '\'' || quote == '"' {
// /* ... */"..."
// /* ... */'...'
// 文字列が続いている。
// 実装汚い...
index++
for index < length && str[index] != quote {
index++
}
if str[index] != quote {
return nil, errors.New("Enclosing characters do not match")
}
index++
} else {
for index < length && str[index] != '\t' && str[index] != '\n' && str[index] != ' ' && str[index] != ',' && str[index] != ')' {
index++
}
}
}
tok.str = str[start:index]
switch tok.kind {
case tkIf, tkElif:
tok.condition = retrieveCondition(tok.kind, tok.str)
case tkBind:
tok.str = bindLiteral(tok.str)
tok.value = retrieveValue(tok.str)
}
start = index
tokens = append(tokens, tok)
}
if index == length-1 {
tokens = append(tokens, token{
kind: tkSQLStmt,
str: str[start : index+1],
})
}
index++
}
// 処理しやすいように終点Tokenを付与する
tokens = append(tokens, token{
kind: tkEndOfProgram,
})
return tokens, nil
}
// ?/*value*/から value1を取り出す
func retrieveValue(str string) string {
retStr := strings.Trim(str, " ")
retStr = strings.TrimLeft(retStr, "?")
retStr = removeCommentSymbol(retStr)
return strings.Trim(retStr, " ")
}
// /*value*/1000 -> ?/*value*/ みたいに変換する
func bindLiteral(str string) string {
str = strings.TrimRightFunc(str, func(r rune) bool {
return r != unicode.SimpleFold('/')
})
return "?" + str
}
// /* (IF|ELIF) condition */ -> conditionを返す
// kind must be tkIf or tkElif
func retrieveCondition(kind tokenKind, str string) string {
str = removeCommentSymbol(str)
str = strings.Trim(str, " ")
str = strings.Trim(str, "\n")
str = strings.Trim(str, "\t")
switch kind {
case tkIf:
str = strings.TrimPrefix(str, "IF")
case tkElif:
str = strings.TrimPrefix(str, "ELIF")
default:
panic("kind must be tKIF or tkElif")
}
return strings.TrimLeft(str, " ")
}
// input: /*value*/ -> output: value
func removeCommentSymbol(str string) string {
str = strings.TrimPrefix(str, "/*")
return strings.TrimSuffix(str, "*/")
}