Skip to content

Commit 56cd6cc

Browse files
Denys Smirnovdennwc
authored andcommitted
handle method modifiers
Signed-off-by: Denys Smirnov <denys@sourced.tech>
1 parent da0a3a1 commit 56cd6cc

File tree

91 files changed

+4996
-14
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

91 files changed

+4996
-14
lines changed

driver/normalizer/normalizer.go

Lines changed: 90 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,10 @@ func funcDefMap(typ string, returns bool, other Obj) Mapping {
187187
Check(OfKind(nodes.KindArray), Var("attr")),
188188
Check(OfKind(nodes.KindObject), Var("attr")),
189189
),
190-
191-
// FIXME(dennwc): driver drops them currently
192-
"Modifiers": Any(),
190+
"Modifiers": Cases("caseMods",
191+
Arr(),
192+
NotEmpty(Var("modifiers")),
193+
),
193194
}
194195
for k, v := range other {
195196
src[k] = v
@@ -246,6 +247,10 @@ func funcDefMap(typ string, returns bool, other Obj) Mapping {
246247
Var("attr"),
247248
Arr(Var("attr")),
248249
),
250+
Cases("caseMods",
251+
Is(nil),
252+
NotEmpty(Var("modifiers")),
253+
),
249254
UASTType(uast.Alias{}, Obj{
250255
"Name": Var("name"),
251256
"Node": UASTType(uast.Function{}, Obj{
@@ -853,11 +858,11 @@ var Normalizers = []Mapping{
853858

854859
funcDefMap("MethodDeclaration", true, Obj{
855860
// number of parameters - safe to ignore
856-
"Arity": Any(),
861+
"Arity": Any(),
862+
"ExplicitInterfaceSpecifier": Is(nil),
857863
// FIXME(dennwc): driver drops them currently
858-
"ConstraintClauses": Any(),
859-
"ExplicitInterfaceSpecifier": Any(),
860-
"TypeParameterList": Any(),
864+
"ConstraintClauses": Any(),
865+
"TypeParameterList": Any(),
861866
}),
862867
// ConstructorDeclaration is similar to MethodDeclaration, but it may include a
863868
// base class initializer that require a special transformation.
@@ -904,9 +909,10 @@ var Normalizers = []Mapping{
904909
"Body": Part("bodyMap", Obj{
905910
"Statements": Var("stmts"),
906911
}),
907-
908-
// FIXME(dennwc): driver drops them currently
909-
"Modifiers": Any(),
912+
"Modifiers": Cases("caseMods",
913+
Arr(),
914+
NotEmpty(Var("modifiers")),
915+
),
910916
},
911917
Obj{
912918
"Nodes": Arr(
@@ -915,6 +921,10 @@ var Normalizers = []Mapping{
915921
Var("attr"),
916922
Arr(Var("attr")),
917923
),
924+
Cases("caseMods",
925+
Is(nil),
926+
NotEmpty(Var("modifiers")),
927+
),
918928
UASTType(uast.Alias{}, Obj{
919929
"Name": Var("name"),
920930
"Node": UASTType(uast.Function{}, Obj{
@@ -944,7 +954,13 @@ var Normalizers = []Mapping{
944954
// Merge uast:Group with uast:FunctionGroup.
945955
Map(
946956
opMergeGroups{Var("group")},
947-
Check(Has{uast.KeyType: String(uast.TypeOf(uast.FunctionGroup{}))}, Var("group")),
957+
Check(
958+
Has{uast.KeyType: In(
959+
nodes.String(typeFuncGroup),
960+
nodes.String(typeGroup),
961+
)},
962+
Var("group"),
963+
),
948964
),
949965
}
950966

@@ -1152,13 +1168,26 @@ func (op opMergeGroups) Kinds() nodes.Kind {
11521168
return nodes.KindObject
11531169
}
11541170

1155-
// Check tests if the current node is uast:Group and if it contains a uast:FunctionGroup
1156-
// node, it will remove the current node and merge other children into the FunctionGroup.
1171+
// Check tests if a current node is uast:Group and uast:FuncGroup and contains group of
1172+
// another kind. It will remove the second group and merge children into current one.
1173+
// uast:FuncGroup is preferred.
11571174
func (op opMergeGroups) Check(st *State, n nodes.Node) (bool, error) {
11581175
group, ok := n.(nodes.Object)
1159-
if !ok || uast.TypeOf(group) != typeGroup {
1176+
if !ok {
11601177
return false, nil
11611178
}
1179+
switch uast.TypeOf(group) {
1180+
case typeGroup:
1181+
return op.checkGroup(st, group)
1182+
case typeFuncGroup:
1183+
return op.checkFuncGroup(st, group)
1184+
}
1185+
return false, nil
1186+
}
1187+
1188+
// checkGroup tests if the current node is uast:Group and if it contains a uast:FunctionGroup
1189+
// node, it will remove the current node and merge other children into the FunctionGroup.
1190+
func (op opMergeGroups) checkGroup(st *State, group nodes.Object) (bool, error) {
11621191
arr, ok := group["Nodes"].(nodes.Array)
11631192
if !ok {
11641193
return false, errors.New("expected an array in Group.Nodes")
@@ -1188,6 +1217,53 @@ func (op opMergeGroups) Check(st *State, n nodes.Node) (bool, error) {
11881217
return op.sub.Check(st, fgroup)
11891218
}
11901219

1220+
// checkFuncGroup tests if the current node is uast:FuncGroup and if any of its sub-arrays
1221+
// contain a ust:Group, it will be removed and the children will be flattened into a sub-array.
1222+
func (op opMergeGroups) checkFuncGroup(st *State, fgroup nodes.Object) (bool, error) {
1223+
// primary nodes array in the function group
1224+
arr, ok := fgroup["Nodes"].(nodes.Array)
1225+
if !ok {
1226+
return false, errors.New("expected an array in FuncGroup.Nodes")
1227+
}
1228+
modified := false
1229+
for i, v := range arr {
1230+
// secondary arrays the group annotations/modifiers, etc
1231+
arr2, ok := v.(nodes.Array)
1232+
if !ok {
1233+
continue
1234+
}
1235+
// find a group node there
1236+
ind := firstWithType(arr2, func(typ string) bool {
1237+
return typ == typeGroup
1238+
})
1239+
if ind < 0 {
1240+
continue
1241+
}
1242+
group := arr2[ind].(nodes.Object)
1243+
// children array of an inner group
1244+
arr3, ok := group["Nodes"].(nodes.Array)
1245+
if !ok {
1246+
return false, errors.New("expected an array in Group.Nodes")
1247+
}
1248+
// flatten inner array into the secondary array
1249+
out := make(nodes.Array, 0, len(arr2)-1+len(arr3))
1250+
out = append(out, arr2[:ind]...)
1251+
out = append(out, arr3...)
1252+
out = append(out, arr2[ind+1:]...)
1253+
if !modified {
1254+
arr = arr.CloneList()
1255+
modified = true
1256+
}
1257+
arr[i] = out
1258+
}
1259+
if !modified {
1260+
return false, nil
1261+
}
1262+
fgroup = fgroup.CloneObject()
1263+
fgroup["Nodes"] = arr
1264+
return op.sub.Check(st, fgroup)
1265+
}
1266+
11911267
func (op opMergeGroups) Construct(st *State, n nodes.Node) (nodes.Node, error) {
11921268
// TODO(dennwc): implement when we will need a reversal
11931269
// see https://github.com/bblfsh/sdk/issues/355

fixtures/Program.cs.sem.uast

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,6 +1102,27 @@
11021102
},
11031103
Nodes: [
11041104
~,
1105+
[
1106+
{ '@type': "csharp:StaticKeyword",
1107+
'@token': "static",
1108+
'@role': [Incomplete],
1109+
'@pos': { '@type': "uast:Positions",
1110+
start: { '@type': "uast:Position",
1111+
offset: 458,
1112+
line: 26,
1113+
col: 9,
1114+
},
1115+
end: { '@type': "uast:Position",
1116+
offset: 464,
1117+
line: 26,
1118+
col: 15,
1119+
},
1120+
},
1121+
IsMissing: false,
1122+
Text: "static",
1123+
ValueText: "static",
1124+
},
1125+
],
11051126
{ '@type': "uast:Alias",
11061127
Name: { '@type': "uast:Identifier",
11071128
'@pos': { '@type': "uast:Positions",
@@ -4571,6 +4592,27 @@
45714592
},
45724593
Nodes: [
45734594
~,
4595+
[
4596+
{ '@type': "csharp:StaticKeyword",
4597+
'@token': "static",
4598+
'@role': [Incomplete],
4599+
'@pos': { '@type': "uast:Positions",
4600+
start: { '@type': "uast:Position",
4601+
offset: 1429,
4602+
line: 53,
4603+
col: 9,
4604+
},
4605+
end: { '@type': "uast:Position",
4606+
offset: 1435,
4607+
line: 53,
4608+
col: 15,
4609+
},
4610+
},
4611+
IsMissing: false,
4612+
Text: "static",
4613+
ValueText: "static",
4614+
},
4615+
],
45744616
{ '@type': "uast:Alias",
45754617
Name: { '@type': "uast:Identifier",
45764618
'@pos': { '@type': "uast:Positions",
@@ -5682,6 +5724,46 @@
56825724
},
56835725
Nodes: [
56845726
~,
5727+
[
5728+
{ '@type': "csharp:ProtectedKeyword",
5729+
'@token': "protected",
5730+
'@role': [Subtype, Visibility],
5731+
'@pos': { '@type': "uast:Positions",
5732+
start: { '@type': "uast:Position",
5733+
offset: 1720,
5734+
line: 63,
5735+
col: 9,
5736+
},
5737+
end: { '@type': "uast:Position",
5738+
offset: 1729,
5739+
line: 63,
5740+
col: 18,
5741+
},
5742+
},
5743+
IsMissing: false,
5744+
Text: "protected",
5745+
ValueText: "protected",
5746+
},
5747+
{ '@type': "csharp:OverrideKeyword",
5748+
'@token': "override",
5749+
'@role': [Incomplete],
5750+
'@pos': { '@type': "uast:Positions",
5751+
start: { '@type': "uast:Position",
5752+
offset: 1730,
5753+
line: 63,
5754+
col: 19,
5755+
},
5756+
end: { '@type': "uast:Position",
5757+
offset: 1738,
5758+
line: 63,
5759+
col: 27,
5760+
},
5761+
},
5762+
IsMissing: false,
5763+
Text: "override",
5764+
ValueText: "override",
5765+
},
5766+
],
56855767
{ '@type': "uast:Alias",
56865768
Name: { '@type': "uast:Identifier",
56875769
'@pos': { '@type': "uast:Positions",
@@ -9074,6 +9156,27 @@
90749156
},
90759157
Nodes: [
90769158
~,
9159+
[
9160+
{ '@type': "csharp:PublicKeyword",
9161+
'@token': "public",
9162+
'@role': [Visibility, World],
9163+
'@pos': { '@type': "uast:Positions",
9164+
start: { '@type': "uast:Position",
9165+
offset: 2679,
9166+
line: 93,
9167+
col: 9,
9168+
},
9169+
end: { '@type': "uast:Position",
9170+
offset: 2685,
9171+
line: 93,
9172+
col: 15,
9173+
},
9174+
},
9175+
IsMissing: false,
9176+
Text: "public",
9177+
ValueText: "public",
9178+
},
9179+
],
90779180
{ '@type': "uast:Alias",
90789181
Name: { '@type': "uast:Identifier",
90799182
'@pos': { '@type': "uast:Positions",
@@ -9305,6 +9408,27 @@
93059408
},
93069409
Nodes: [
93079410
~,
9411+
[
9412+
{ '@type': "csharp:PublicKeyword",
9413+
'@token': "public",
9414+
'@role': [Visibility, World],
9415+
'@pos': { '@type': "uast:Positions",
9416+
start: { '@type': "uast:Position",
9417+
offset: 2767,
9418+
line: 97,
9419+
col: 9,
9420+
},
9421+
end: { '@type': "uast:Position",
9422+
offset: 2773,
9423+
line: 97,
9424+
col: 15,
9425+
},
9426+
},
9427+
IsMissing: false,
9428+
Text: "public",
9429+
ValueText: "public",
9430+
},
9431+
],
93089432
{ '@type': "uast:Alias",
93099433
Name: { '@type': "uast:Identifier",
93109434
'@pos': { '@type': "uast:Positions",
@@ -9503,6 +9627,27 @@
95039627
},
95049628
Nodes: [
95059629
~,
9630+
[
9631+
{ '@type': "csharp:PublicKeyword",
9632+
'@token': "public",
9633+
'@role': [Visibility, World],
9634+
'@pos': { '@type': "uast:Positions",
9635+
start: { '@type': "uast:Position",
9636+
offset: 2859,
9637+
line: 101,
9638+
col: 9,
9639+
},
9640+
end: { '@type': "uast:Position",
9641+
offset: 2865,
9642+
line: 101,
9643+
col: 15,
9644+
},
9645+
},
9646+
IsMissing: false,
9647+
Text: "public",
9648+
ValueText: "public",
9649+
},
9650+
],
95069651
{ '@type': "uast:Alias",
95079652
Name: { '@type': "uast:Identifier",
95089653
'@pos': { '@type': "uast:Positions",

fixtures/_integration.cs.sem.uast

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,27 @@
162162
},
163163
Nodes: [
164164
~,
165+
[
166+
{ '@type': "csharp:StaticKeyword",
167+
'@token': "static",
168+
'@role': [Incomplete],
169+
'@pos': { '@type': "uast:Positions",
170+
start: { '@type': "uast:Position",
171+
offset: 101,
172+
line: 7,
173+
col: 9,
174+
},
175+
end: { '@type': "uast:Position",
176+
offset: 107,
177+
line: 7,
178+
col: 15,
179+
},
180+
},
181+
IsMissing: false,
182+
Text: "static",
183+
ValueText: "static",
184+
},
185+
],
165186
{ '@type': "uast:Alias",
166187
Name: { '@type': "uast:Identifier",
167188
'@pos': { '@type': "uast:Positions",

0 commit comments

Comments
 (0)