-
Notifications
You must be signed in to change notification settings - Fork 0
/
stmt.go
163 lines (151 loc) · 3.32 KB
/
stmt.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
package goastutil
import (
"go/ast"
"strings"
"github.com/samber/lo"
)
type StmtType int
func (st StmtType) String() string {
switch st {
case AssignStmtType:
return "AssignStmtType"
case BadStmtType:
return "BadStmtType"
case BlockStmtType:
return "BlockStmtType"
case BranchStmtType:
return "BranchStmtType"
case CaseClauseType:
return "CaseClauseType"
case CommClauseType:
return "CommClauseType"
case DeclStmtType:
return "DeclStmtType"
case DeferStmtType:
return "DeferStmtType"
case EmptyStmtType:
return "EmptyStmtType"
case ExprStmtType:
return "ExprStmtType"
case ForStmtType:
return "ForStmtType"
case GoStmtType:
return "GoStmtType"
case IfStmtType:
return "IfStmtType"
case IncDecStmtType:
return "IncDecStmtType"
case LabeledStmtType:
return "LabeledStmtType"
case RangeStmtType:
return "RangeStmtType"
case ReturnStmtType:
return "ReturnStmtType"
case SendStmtType:
return "SendStmtType"
case SelectStmtType:
return "SelectStmtType"
case SwitchStmtType:
return "SwitchStmtType"
case TypeAssertStmtType:
return "TypeAssertStmtType"
case TypeSwitchStmtType:
return "TypeSwitchStmtType"
default:
return "UnknownStmtType"
}
}
const (
AssignStmtType StmtType = iota
BadStmtType
BlockStmtType
BranchStmtType
CaseClauseType
CommClauseType
DeclStmtType
DeferStmtType
EmptyStmtType
ExprStmtType
ForStmtType
GoStmtType
IfStmtType
IncDecStmtType
LabeledStmtType
RangeStmtType
ReturnStmtType
SendStmtType
SelectStmtType
SwitchStmtType
TypeAssertStmtType
TypeSwitchStmtType
UnknownType
)
type StmtAppendPrepender interface {
PrependStmt(string) error
AppendStmt(string) error
}
type Stmt interface {
ast.Stmt
StmtAppendPrepender
String() string
StmtType() StmtType
}
type Stmts []Stmt
func (s Stmts) String() string {
return strings.Join(lo.Map(s, func(x Stmt, _ int) string {
return x.String()
}), "\n\t")
}
func toStmt(parent Stmt, stmts []ast.Stmt) Stmts {
return lo.Map(stmts, func(stmt ast.Stmt, _ int) Stmt {
return NewStmt(parent, stmt)
})
}
func NewStmt(parent Stmt, stmt ast.Stmt) Stmt {
switch stmt := stmt.(type) {
case *ast.AssignStmt:
return NewAssignStmt(parent, stmt)
case *ast.BadStmt:
return NewBadStmt(parent, stmt)
case *ast.BlockStmt:
return NewBlockStmt(parent, stmt)
case *ast.BranchStmt:
return NewBranchStmt(parent, stmt)
case *ast.CaseClause:
return NewCaseClause(parent, stmt)
case *ast.CommClause:
return NewCommClause(parent, stmt)
case *ast.DeclStmt:
return NewDeclStmt(parent, stmt)
case *ast.DeferStmt:
return NewDeferStmt(parent, stmt)
case *ast.EmptyStmt:
return NewEmptyStmt(parent, stmt)
case *ast.ExprStmt:
return NewExprStmt(parent, stmt)
case *ast.ForStmt:
return NewForStmt(parent, stmt)
case *ast.GoStmt:
return NewGoStmt(parent, stmt)
case *ast.IfStmt:
return NewIfStmt(parent, stmt)
case *ast.IncDecStmt:
return NewIncDecStmt(parent, stmt)
case *ast.LabeledStmt:
return NewLabeledStmt(parent, stmt)
case *ast.RangeStmt:
return NewRangeStmt(parent, stmt)
case *ast.ReturnStmt:
return NewReturnStmt(parent, stmt)
case *ast.SelectStmt:
return NewSelectStmt(parent, stmt)
case *ast.SendStmt:
return NewSendStmt(parent, stmt)
case *ast.SwitchStmt:
return NewSwitchStmt(parent, stmt)
case *ast.TypeSwitchStmt:
return NewTypeSwitchStmt(parent, stmt)
default:
return nil
}
}