-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
207 lines (159 loc) · 4.73 KB
/
util.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package goastutil
import (
"errors"
"go/ast"
"go/parser"
"go/token"
"slices"
"github.com/samber/lo"
"github.com/shurcooL/go/parserutil"
)
func parseStmts(s string) ([]ast.Stmt, error) {
file, err := parser.ParseFile(token.NewFileSet(), "", "package p;func _(){\n//line :1\n"+s+"\n;}", 0)
if err != nil {
// TODO: use our own error
return nil, err
}
// Filter out empty statements, the empty statement would impact the expected order of statements.
return lo.Filter(file.Decls[0].(*ast.FuncDecl).Body.List, func(stmt ast.Stmt, _ int) bool {
_, ok := stmt.(*ast.EmptyStmt)
return !ok
}), nil
}
func parseDecls(s string) ([]ast.Decl, error) {
file, err := parser.ParseFile(token.NewFileSet(), "", "package p\n//line :1\n"+s+"\n", 0)
if err != nil {
return nil, err
}
if len(file.Decls) == 0 {
return nil, errors.New("no declaration")
}
return file.Decls, nil
}
func parseCaseStmts(s string) ([]ast.Stmt, error) {
stmt, err := parserutil.ParseStmt("switch {\n" + s + "\n}")
if err != nil {
return nil, err
}
return stmt.(*ast.SwitchStmt).Body.List, nil
}
func parseCommStmts(s string) ([]ast.Stmt, error) {
stmt, err := parserutil.ParseStmt("select {\n" + s + "\n}")
if err != nil {
return nil, err
}
return stmt.(*ast.SelectStmt).Body.List, nil
}
func prependStmt(st string, parent Stmt, stmt ast.Stmt) error {
parsedStmts, err := parseStmts(st)
if err != nil {
return ErrInvalidStmt
}
blockStmt, ok := parent.(*BlockStmt)
if !ok {
return ErrNotBlockStmt
}
insertIndex := slices.Index(blockStmt.BlockStmt.List, stmt)
// No index check needed because statement always in the block statement list
blockStmt.BlockStmt.List = slices.Insert(blockStmt.BlockStmt.List, insertIndex, parsedStmts...)
return nil
}
func appendStmt(st string, parent Stmt, stmt ast.Stmt) error {
// parsedStmt, err := parserutil.ParseStmt(st)
parsedStmts, err := parseStmts(st)
if err != nil {
return ErrInvalidStmt
}
blockStmt, ok := parent.(*BlockStmt)
if !ok {
return ErrNotBlockStmt
}
insertIndex := slices.Index(blockStmt.BlockStmt.List, stmt)
// No index check needed because statement always in the block statement list
blockStmt.BlockStmt.List = slices.Insert(blockStmt.BlockStmt.List, insertIndex+1, parsedStmts...)
return nil
}
func prependCaseStmt(st string, parent Stmt, stmt ast.Stmt) error {
parsedStmts, err := parseCaseStmts(st)
if err != nil {
return ErrInvalidStmt
}
blockStmt, ok := parent.(*BlockStmt)
if !ok {
return ErrNotBlockStmt
}
grandNode := blockStmt.parent
if grandNode == nil {
return ErrCaseClause
}
if _, ok := grandNode.(*SwitchStmt); !ok {
return ErrCaseClause
}
insertIndex := slices.Index(blockStmt.BlockStmt.List, stmt)
// No index check needed because statement always in the block statement list
blockStmt.BlockStmt.List = slices.Insert(blockStmt.BlockStmt.List, insertIndex, parsedStmts...)
return nil
}
func appendCaseStmt(st string, parent Stmt, stmt ast.Stmt) error {
parsedStmts, err := parseCaseStmts(st)
if err != nil {
return ErrInvalidStmt
}
blockStmt, ok := parent.(*BlockStmt)
if !ok {
return ErrNotBlockStmt
}
grandNode := blockStmt.parent
if grandNode == nil {
return ErrCaseClause
}
if _, ok := grandNode.(*SwitchStmt); !ok {
return ErrCaseClause
}
insertIndex := slices.Index(blockStmt.BlockStmt.List, stmt)
// No index check needed because statement always in the block statement list
blockStmt.BlockStmt.List = slices.Insert(blockStmt.BlockStmt.List, insertIndex+1, parsedStmts...)
return nil
}
func prependCommStmt(st string, parent Stmt, stmt ast.Stmt) error {
parsedStmts, err := parseCommStmts(st)
if err != nil {
return ErrInvalidStmt
}
blockStmt, ok := parent.(*BlockStmt)
if !ok {
return ErrNotBlockStmt
}
grandNode := blockStmt.parent
if grandNode == nil {
return ErrCaseClause
}
if _, ok := grandNode.(*SelectStmt); !ok {
return ErrCaseClause
}
insertIndex := slices.Index(blockStmt.BlockStmt.List, stmt)
// No index check needed because statement always in the block statement list
blockStmt.BlockStmt.List = slices.Insert(blockStmt.BlockStmt.List, insertIndex, parsedStmts...)
return nil
}
func appendCommStmt(st string, parent Stmt, stmt ast.Stmt) error {
parsedStmts, err := parseCommStmts(st)
if err != nil {
return ErrInvalidStmt
}
blockStmt, ok := parent.(*BlockStmt)
if !ok {
return ErrNotBlockStmt
}
grandNode := blockStmt.parent
if grandNode == nil {
return ErrCaseClause
}
if _, ok := grandNode.(*SelectStmt); !ok {
return ErrCaseClause
}
insertIndex := slices.Index(blockStmt.BlockStmt.List, stmt)
// No index check needed because statement always in the block statement list
blockStmt.BlockStmt.List = slices.Insert(blockStmt.BlockStmt.List, insertIndex+1, parsedStmts...)
return nil
}