-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathast.go
191 lines (169 loc) · 3.72 KB
/
ast.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
package twowaysql
import (
"errors"
"fmt"
)
type nodeKind int
const (
ndSQLStmt nodeKind = iota + 1
ndBind
ndIf
ndElif
ndElse
ndEnd
ndEndOfProgram
)
// tree is a component of an abstract syntax tree
type tree struct {
Kind nodeKind
Left *tree
Right *tree
Token *token
}
// astはトークン列から抽象構文木を生成する。
// 生成規則:
// program = stmt
// stmt = SQLStmt stmt |
//
// BIND stmt |
// "IF" stmt ("ELLF" stmt)* ("ELSE" stmt)? "END" stmt |
// EndOfProgram
func ast(tokens []token) (*tree, error) {
node, err := program(tokens)
if err != nil {
return nil, err
}
if node.nodeCount() != len(tokens) {
return nil, errors.New("can not generate abstract syntax tree")
}
return node, nil
}
func program(tokens []token) (*tree, error) {
index := 0
return stmt(tokens, &index)
}
// token index token[index]を見ている
func stmt(tokens []token, index *int) (*tree, error) {
var node *tree
var err error
if consume(tokens, index, tkSQLStmt) {
// SQLStmt stmt
node = &tree{
Kind: ndSQLStmt,
Token: &tokens[*index-1],
}
node.Left, err = stmt(tokens, index)
if err != nil {
return nil, err
}
} else if consume(tokens, index, tkBind) {
// Bind stmt
node = &tree{
Kind: ndBind,
Token: &tokens[*index-1],
}
node.Left, err = stmt(tokens, index)
if err != nil {
return nil, err
}
} else if consume(tokens, index, tkEndOfProgram) {
// EndOfProgram
node = &tree{
Kind: nodeKind(tkEndOfProgram),
// consumeはTkEndOfProgramの時はインクリメントしないから1を引かない
// かなりよくない設計、一貫性がない。
Token: &tokens[*index],
}
return node, nil
} else if consume(tokens, index, tkIf) {
// "IF" stmt ("ELLF" stmt)* ("ELSE" stmt)? "END" stmt
node = &tree{
Kind: ndIf,
Token: &tokens[*index-1],
}
node.Left, err = stmt(tokens, index)
if err != nil {
return nil, err
}
tmpNode := node
for {
// ("ELLF" stmt)*
if consume(tokens, index, tkElif) {
child := &tree{
Kind: ndElif,
Token: &tokens[*index-1],
}
tmpNode.Right = child
tmpNode = child
child.Left, err = stmt(tokens, index)
if err != nil {
return nil, err
}
continue
}
break
}
if consume(tokens, index, tkElse) {
// ("ELSE" stmt)?
child := &tree{
Kind: ndElse,
Token: &tokens[*index-1],
}
tmpNode.Right = child
tmpNode = child
child.Left, err = stmt(tokens, index)
if err != nil {
return nil, err
}
}
if consume(tokens, index, tkEnd) {
// "END"
child := &tree{
Kind: ndEnd,
Token: &tokens[*index-1],
}
tmpNode.Right = child
child.Left, err = stmt(tokens, index)
if err != nil {
return nil, err
}
} else {
return nil, fmt.Errorf("can not parse: expected /* END */, but got %v", tokens[*index].kind)
}
// どれも一致しなかった
return node, nil
}
return node, nil
}
// tokenが所望のものか調べる。一致していればインデックスを一つ進める
func consume(tokens []token, index *int, kind tokenKind) bool {
//println("str: ", tokens[*index].str, "kind: ", tokens[*index].kind, "want kind: ", kind)
if tokens[*index].kind == kind {
// TkEndOfProgramでインクリメントしてしまうと
// その後のconsume呼び出しでIndex Out Of Bounds例外が発生してしまう
if kind != tkEndOfProgram {
*index++
}
return true
}
return false
}
func (t *tree) nodeCount() int {
count := 1
if t.Left != nil {
t.Left.countInner(&count)
}
if t.Right != nil {
t.Right.countInner(&count)
}
return count
}
func (t *tree) countInner(count *int) {
*count++
if t.Left != nil {
t.Left.countInner(count)
}
if t.Right != nil {
t.Right.countInner(count)
}
}