forked from bblfsh/java-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnormalizer.go
268 lines (260 loc) · 6.13 KB
/
normalizer.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
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
package normalizer
import (
"gopkg.in/bblfsh/sdk.v2/uast"
. "gopkg.in/bblfsh/sdk.v2/uast/transformer"
)
var Preprocess []Transformer
var Normalize = Transformers([][]Transformer{
// The main block of normalization rules.
{Mappings(Normalizers...)},
}...)
var Normalizers = []Mapping{
MapSemantic("StringLiteral", uast.String{}, MapObj(
Fields{
{Name: "unescapedValue", Op: Var("val")},
{Name: "escapedValue", Drop: true, Op: Any()}, // only used in Annotated
},
Obj{
"Value": Var("val"),
"Format": String(""),
},
)),
MapSemantic("SimpleName", uast.Identifier{}, MapObj(
Obj{
"identifier": Var("name"),
},
Obj{
"Name": Var("name"),
},
)),
MapSemantic("QualifiedName", uast.QualifiedIdentifier{}, MapObj(
CasesObj("case",
// common
Obj{"name": Var("name")},
Objs{
// the last name = identifier
{
"qualifier": Check(Has{
uast.KeyType: String(uast.TypeOf(uast.Identifier{})),
}, Var("par")),
},
// linked list
{
"qualifier": UASTType(uast.QualifiedIdentifier{}, Obj{
// FIXME: start position
uast.KeyPos: AnyNode(nil),
"Names": Var("names"),
}),
},
},
),
CasesObj("case", nil,
Objs{
// the last name = identifier
{
"Names": Arr(Var("par"), Var("name")),
},
// linked list
{
"Names": Append(Var("names"), Arr(Var("name"))),
},
},
),
)),
MapSemantic("BlockComment", uast.Comment{}, MapObj(
Obj{
"text": CommentText([2]string{"/*", "*/"}, "comm"),
},
CommentNode(true, "comm", nil),
)),
MapSemantic("LineComment", uast.Comment{}, MapObj(
Obj{
"text": CommentText([2]string{"//", ""}, "comm"),
},
CommentNode(false, "comm", nil),
)),
MapSemantic("Block", uast.Block{}, MapObj(
Obj{
"statements": Var("stmts"),
},
Obj{
"Statements": Var("stmts"),
},
)),
MapSemantic("ImportDeclaration", uast.Import{}, MapObj(
CasesObj("case",
// common
Obj{
"static": Var("static"),
},
Objs{
// star import (on demand)
{
"name": Var("name"),
"onDemand": String("true"),
},
// normal import
{
"name": Part("path", UASTType(uast.QualifiedIdentifier{}, Obj{
"Names": Append(Var("names"), Arr(Var("name"))),
})),
"onDemand": String("false"),
},
},
),
CasesObj("case",
// common
Obj{
// TODO: handle static when we have scopes
"Target": Obj{"static": Var("static")},
},
Objs{
// star import (on demand)
{
"Path": Var("name"),
"All": Bool(true),
"Names": Arr(),
},
{
"Path": Part("path", UASTType(uast.QualifiedIdentifier{}, Obj{
"Names": Var("names"),
})),
"All": Bool(false),
"Names": Arr(Var("name")),
},
},
),
)),
MapSemantic("MethodDeclaration", uast.FunctionGroup{}, MapObj(
Obj{
"constructor": Var("constr"),
"extraDimensions2": Is(nil), // TODO: find an example
"javadoc": Var("doc"),
"modifiers": Var("ann"), // TODO: it's an array, we should expand it somewhere
"name": Var("name"),
"body": Var("body"),
"parameters": Each("args", Obj{
uast.KeyType: String("SingleVariableDeclaration"),
uast.KeyPos: Var("apos"),
"extraDimensions2": Is(nil),
"initializer": Var("ainit"),
"modifiers": Is(nil),
"name": Var("aname"),
"type": Var("atype"),
"varargs": Cases("varg", String("false"), String("true")),
"varargsAnnotations": Is(nil),
}),
"receiverQualifier": Var("recv_name"),
"receiverType": Cases("recv",
Is(nil),
Check(NotNil(), Var("recv_type")),
),
"returnType2": Cases("out_case",
// no return type (constructor)
Is(nil),
// void
Obj{
uast.KeyType: String("PrimitiveType"),
uast.KeyPos: AnyNode(nil),
"annotations": Is(nil),
"primitiveTypeCode": String("void"),
},
// any other type
Check(
Not(And(
Is(nil),
Has{
uast.KeyType: String("PrimitiveType"),
"annotations": Is(nil),
"primitiveTypeCode": String("void"),
},
)),
Var("out1"),
),
),
"thrownExceptionTypes": Var("exc"),
"typeParameters": Var("tmpl"),
},
Obj{
"Nodes": Arr(
Var("doc"),
Var("ann"),
UASTType(uast.Alias{}, Obj{
// FIXME: add position
"Name": Var("name"),
"Node": UASTType(uast.Function{}, Obj{
"Type": UASTType(uast.FunctionType{}, Obj{
"Arguments": Cases("recv",
// default receiver
argsPart,
// additional receiver
PrependOne(
UASTType(uast.Argument{}, Obj{
"Name": Var("recv_name"),
"Type": Var("recv_type"),
"Receiver": Bool(true),
}),
argsPart,
),
),
"Returns": Cases("out_case",
// no return (constructor)
Is(nil),
// void return
Is(nil),
// normal return type
Arr(
UASTType(uast.Argument{}, Obj{
"Type": Var("out1"),
}),
),
),
}),
"Body": Var("body"),
}),
}),
Obj{ // FIXME: store them as annotations at least
"constructor": Var("constr"),
"thrownExceptionTypes": Var("exc"),
"typeParameters": Var("tmpl"),
},
),
},
)),
MapSemantic("Javadoc", uast.Comment{}, MapObj(
Obj{
"text": CommentText([2]string{"/**", "*/"}, "comm"),
},
CommentNode(true, "comm", nil),
)),
MapSemantic("Javadoc", uast.Group{}, MapObj(
Obj{
"tags": Var("parts"),
},
Obj{
"Nodes": Var("parts"),
},
)),
MapSemantic("TagElement", uast.Group{}, MapObj(
Obj{
"fragments": Var("parts"),
"tagName": Is(nil),
},
Obj{
"Nodes": Var("parts"),
},
)),
MapSemantic("TextElement", uast.Comment{}, MapObj(
Obj{
"text": CommentText([2]string{"", ""}, "comm"),
},
CommentNode(false, "comm", nil),
)),
}
var argsPart = Each("args", UASTType(uast.Argument{}, Obj{
uast.KeyPos: Var("apos"),
"Name": Var("aname"),
"Type": Var("atype"),
"Init": Var("ainit"),
"Variadic": Cases("varg", Bool(false), Bool(true)),
}))