-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathstatement.go
More file actions
326 lines (298 loc) · 9.81 KB
/
statement.go
File metadata and controls
326 lines (298 loc) · 9.81 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package main
import (
"fmt"
"go/ast"
"go/token"
"github.com/NickyBoy89/java2go/astutil"
"github.com/NickyBoy89/java2go/nodeutil"
log "github.com/sirupsen/logrus"
sitter "github.com/smacker/go-tree-sitter"
)
func ParseStmt(node *sitter.Node, source []byte, ctx Ctx) ast.Stmt {
if stmt := TryParseStmt(node, source, ctx); stmt != nil {
return stmt
}
panic(fmt.Errorf("Unhandled stmt type: %v", node.Type()))
}
func TryParseStmt(node *sitter.Node, source []byte, ctx Ctx) ast.Stmt {
switch node.Type() {
case "ERROR":
log.WithFields(log.Fields{
"parsed": node.Content(source),
"className": ctx.className,
}).Warn("Statement parse error")
return &ast.BadStmt{}
case "comment":
return &ast.BadStmt{}
case "local_variable_declaration":
variableType := astutil.ParseType(node.ChildByFieldName("type"), source)
variableDeclarator := node.ChildByFieldName("declarator")
// If a variable is being declared, but not set to a value
// Ex: `int value;`
if variableDeclarator.NamedChildCount() == 1 {
return &ast.DeclStmt{
Decl: &ast.GenDecl{
Tok: token.VAR,
Specs: []ast.Spec{
&ast.ValueSpec{
Names: []*ast.Ident{ParseExpr(variableDeclarator.ChildByFieldName("name"), source, ctx).(*ast.Ident)},
Type: variableType,
},
},
},
}
}
ctx.lastType = variableType
declaration := ParseStmt(variableDeclarator, source, ctx).(*ast.AssignStmt)
// Now, if a variable is assigned to `null`, we can't infer its type, so
// don't throw out the type information associated with it
var containsNull bool
// Go through the values and see if there is a `null_literal`
for _, child := range nodeutil.NamedChildrenOf(variableDeclarator) {
if child.Type() == "null_literal" {
containsNull = true
break
}
}
names := make([]*ast.Ident, len(declaration.Lhs))
for ind, decl := range declaration.Lhs {
names[ind] = decl.(*ast.Ident)
}
// If the declaration contains null, declare it with the `var` keyword instead
// of implicitly
if containsNull {
return &ast.DeclStmt{
Decl: &ast.GenDecl{
Tok: token.VAR,
Specs: []ast.Spec{
&ast.ValueSpec{
Names: names,
Type: variableType,
Values: declaration.Rhs,
},
},
},
}
}
return declaration
case "variable_declarator":
var names, values []ast.Expr
// If there is only one node, then that node is just a name
if node.NamedChildCount() == 1 {
names = append(names, ParseExpr(node.NamedChild(0), source, ctx))
}
// Loop through every pair of name and value
for ind := 0; ind < int(node.NamedChildCount())-1; ind += 2 {
names = append(names, ParseExpr(node.NamedChild(ind), source, ctx))
values = append(values, ParseExpr(node.NamedChild(ind+1), source, ctx))
}
return &ast.AssignStmt{Lhs: names, Tok: token.DEFINE, Rhs: values}
case "assignment_expression":
assignVar := ParseExpr(node.Child(0), source, ctx)
assignVal := ParseExpr(node.Child(2), source, ctx)
// Unsigned right shift
if node.Child(1).Content(source) == ">>>=" {
return &ast.ExprStmt{X: &ast.CallExpr{
Fun: &ast.Ident{Name: "UnsignedRightShiftAssignment"},
Args: []ast.Expr{assignVar, assignVal},
}}
}
return &ast.AssignStmt{
Lhs: []ast.Expr{assignVar},
Tok: StrToToken(node.Child(1).Content(source)),
Rhs: []ast.Expr{assignVal},
}
case "update_expression":
if node.Child(0).IsNamed() {
return &ast.IncDecStmt{
X: ParseExpr(node.Child(0), source, ctx),
Tok: StrToToken(node.Child(1).Content(source)),
}
}
return &ast.IncDecStmt{
X: ParseExpr(node.Child(1), source, ctx),
Tok: StrToToken(node.Child(0).Content(source)),
}
case "resource_specification":
return ParseStmt(node.NamedChild(0), source, ctx)
case "resource":
var offset int
if node.NamedChild(0).Type() == "modifiers" {
offset = 1
}
return &ast.AssignStmt{
Lhs: []ast.Expr{ParseExpr(node.NamedChild(1+offset), source, ctx)},
Tok: token.DEFINE,
Rhs: []ast.Expr{ParseExpr(node.NamedChild(2+offset), source, ctx)},
}
case "method_invocation":
return &ast.ExprStmt{X: ParseExpr(node, source, ctx)}
case "constructor_body", "block":
body := &ast.BlockStmt{}
for _, line := range nodeutil.NamedChildrenOf(node) {
if line.Type() == "comment" {
continue
}
if stmt := TryParseStmt(line, source, ctx); stmt != nil {
body.List = append(body.List, stmt)
} else {
// Try statements are ignored, so they return a list of statements
body.List = append(body.List, ParseNode(line, source, ctx).([]ast.Stmt)...)
}
}
return body
case "expression_statement":
if stmt := TryParseStmt(node.NamedChild(0), source, ctx); stmt != nil {
return stmt
}
return &ast.ExprStmt{X: ParseExpr(node.NamedChild(0), source, ctx)}
case "explicit_constructor_invocation":
// This is when a constructor calls another constructor with the use of
// something such as `this(args...)`
return &ast.ExprStmt{
&ast.CallExpr{
Fun: &ast.Ident{Name: "New" + ctx.className},
Args: ParseNode(node.NamedChild(1), source, ctx).([]ast.Expr),
},
}
case "return_statement":
if node.NamedChildCount() < 1 {
return &ast.ReturnStmt{Results: []ast.Expr{}}
}
return &ast.ReturnStmt{Results: []ast.Expr{ParseExpr(node.NamedChild(0), source, ctx)}}
case "labeled_statement":
return &ast.LabeledStmt{
Label: ParseExpr(node.NamedChild(0), source, ctx).(*ast.Ident),
Stmt: ParseStmt(node.NamedChild(1), source, ctx),
}
case "break_statement":
if node.NamedChildCount() > 0 {
return &ast.BranchStmt{Tok: token.BREAK, Label: ParseExpr(node.NamedChild(0), source, ctx).(*ast.Ident)}
}
return &ast.BranchStmt{Tok: token.BREAK}
case "continue_statement":
if node.NamedChildCount() > 0 {
return &ast.BranchStmt{Tok: token.CONTINUE, Label: ParseExpr(node.NamedChild(0), source, ctx).(*ast.Ident)}
}
return &ast.BranchStmt{Tok: token.CONTINUE}
case "throw_statement":
return &ast.ExprStmt{X: &ast.CallExpr{
Fun: &ast.Ident{Name: "panic"},
Args: []ast.Expr{ParseExpr(node.NamedChild(0), source, ctx)},
}}
case "if_statement":
var other ast.Stmt
if node.ChildByFieldName("alternative") != nil {
other = ParseStmt(node.ChildByFieldName("alternative"), source, ctx)
}
// If the `if` statement is inline, replace the line with a full block
var body ast.Stmt = ParseStmt(node.ChildByFieldName("consequence"), source, ctx)
if _, ok := body.(*ast.BlockStmt); !ok {
body = &ast.BlockStmt{List: []ast.Stmt{
body,
}}
}
return &ast.IfStmt{
Cond: ParseExpr(node.ChildByFieldName("condition"), source, ctx),
Body: body.(*ast.BlockStmt),
Else: other,
}
case "enhanced_for_statement":
// An enhanced for statement has the following fields:
// variables for the variable being declared (ex: int n)
// then the expression that is being ranged over
// and finally, the block of the expression
total := int(node.NamedChildCount())
return &ast.RangeStmt{
// We don't need the type of the variable for the range expression
Key: &ast.Ident{Name: "_"},
Value: ParseExpr(node.NamedChild(total-3), source, ctx),
Tok: token.DEFINE,
X: ParseExpr(node.NamedChild(total-2), source, ctx),
Body: ParseStmt(node.NamedChild(total-1), source, ctx).(*ast.BlockStmt),
}
case "for_statement":
var init, post ast.Stmt
if node.ChildByFieldName("init") != nil {
init = ParseStmt(node.ChildByFieldName("init"), source, ctx)
}
if node.ChildByFieldName("update") != nil {
post = ParseStmt(node.ChildByFieldName("update"), source, ctx)
}
var cond ast.Expr
if node.ChildByFieldName("condition") != nil {
cond = ParseExpr(node.ChildByFieldName("condition"), source, ctx)
}
return &ast.ForStmt{
Init: init,
Cond: cond,
Post: post,
Body: ParseStmt(node.ChildByFieldName("body"), source, ctx).(*ast.BlockStmt),
}
case "while_statement":
return &ast.ForStmt{
Cond: ParseExpr(node.NamedChild(0), source, ctx),
Body: ParseStmt(node.NamedChild(1), source, ctx).(*ast.BlockStmt),
}
case "do_statement":
// A do statement is handled as a blank for loop with the condition
// inserted as a break condition in the final part of the loop
body := ParseStmt(node.NamedChild(0), source, ctx).(*ast.BlockStmt)
body.List = append(body.List, &ast.IfStmt{
Cond: &ast.UnaryExpr{
X: &ast.ParenExpr{
X: ParseExpr(node.NamedChild(1), source, ctx),
},
},
Body: &ast.BlockStmt{List: []ast.Stmt{&ast.BranchStmt{Tok: token.BREAK}}},
})
return &ast.ForStmt{
Body: body,
}
case "switch_statement":
return &ast.SwitchStmt{
Tag: ParseExpr(node.NamedChild(0), source, ctx),
Body: ParseStmt(node.NamedChild(1), source, ctx).(*ast.BlockStmt),
}
case "switch_block":
switchBlock := &ast.BlockStmt{}
var currentCase *ast.CaseClause
for _, c := range nodeutil.NamedChildrenOf(node) {
switch c.Type() {
case "switch_label":
// When a new switch label comes, append it to the switch block
if currentCase != nil {
switchBlock.List = append(switchBlock.List, currentCase)
}
currentCase = ParseNode(c, source, ctx).(*ast.CaseClause)
default:
if exprs := TryParseStmts(c, source, ctx); exprs != nil {
currentCase.Body = append(currentCase.Body, exprs...)
} else {
currentCase.Body = append(currentCase.Body, ParseStmt(c, source, ctx))
}
}
}
return switchBlock
}
return nil
}
func ParseStmts(node *sitter.Node, source []byte, ctx Ctx) []ast.Stmt {
if stmts := TryParseStmts(node, source, ctx); stmts != nil {
return stmts
}
panic(fmt.Errorf("Unhandled stmts type: %v", node.Type()))
}
func TryParseStmts(node *sitter.Node, source []byte, ctx Ctx) []ast.Stmt {
switch node.Type() {
case "assignment_expression":
if stmts, ok := ParseNode(node, source, ctx).([]ast.Stmt); ok {
return stmts
}
case "try_statement":
if stmts, ok := ParseNode(node, source, ctx).([]ast.Stmt); ok {
return stmts
}
}
return nil
}