Skip to content
This repository was archived by the owner on Mar 8, 2020. It is now read-only.

Commit 3a0c75e

Browse files
Denys Smirnovdennwc
authored andcommitted
split import paths; also fix issues with relative-only paths
Signed-off-by: Denys Smirnov <[email protected]>
1 parent 765e9de commit 3a0c75e

16 files changed

+438
-90
lines changed

driver/normalizer/normalizer.go

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -397,10 +397,9 @@ var Normalizers = []Mapping{
397397
uast.KeyPos: Any(),
398398
"name": Check(OfKind(nodes.KindString), Var("name")),
399399
"asname": Is(nil),
400-
}, UASTType(uast.Identifier{}, Obj{
401-
uast.KeyPos: UASTType(uast.Positions{}, nil),
402-
"Name": Var("name"),
403-
})),
400+
}, OpSplitPath{
401+
path: Var("name"),
402+
}),
404403

405404
// FIXME: aliases doesn't have a position (can't be currently fixed by the tokenizer
406405
// because they don't even have a line in the native AST)
@@ -411,11 +410,12 @@ var Normalizers = []Mapping{
411410
},
412411
Obj{
413412
"Name": UASTType(uast.Identifier{}, Obj{
414-
"Name": Var("alias"),
413+
uast.KeyPos: UASTType(uast.Positions{}, nil),
414+
"Name": Var("alias"),
415415
}),
416-
"Node": UASTType(uast.Identifier{},
417-
Obj{"Name": Var("name")},
418-
),
416+
"Node": OpSplitPath{
417+
path: Var("name"),
418+
},
419419
},
420420
)),
421421

@@ -439,14 +439,11 @@ var Normalizers = []Mapping{
439439
},
440440
Obj{
441441
"All": Bool(true),
442-
"Path": UASTType(uast.Identifier{}, Obj{
443-
"Name": OpPrependPath{
444-
// FIXME: no position for the module (path) in the native AST, only when the import starts
445-
numLevel: Var("level"),
446-
path: Var("module"),
447-
prefix: "../",
448-
},
449-
}),
442+
"Path": OpSplitPath{
443+
// FIXME: no position for the module (path) in the native AST, only when the import starts
444+
numLevel: Var("level"),
445+
path: Var("module"),
446+
},
450447
},
451448
)),
452449

@@ -463,13 +460,10 @@ var Normalizers = []Mapping{
463460
},
464461
Obj{
465462
"Names": Var("names"),
466-
"Path": UASTType(uast.Identifier{}, Obj{
467-
"Name": OpPrependPath{
468-
numLevel: Var("level"),
469-
path: Var("module"),
470-
prefix: "../",
471-
},
472-
}),
463+
"Path": OpSplitPath{
464+
numLevel: Var("level"),
465+
path: Var("module"),
466+
},
473467
},
474468
)),
475469
}

driver/normalizer/util.go

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package normalizer
22

33
import (
4+
"github.com/bblfsh/sdk/v3/uast"
45
"strings"
56

67
"github.com/bblfsh/sdk/v3/uast/nodes"
@@ -15,49 +16,57 @@ func num2dots(n nodes.Value, prefix string) nodes.Value {
1516
return n
1617
}
1718

18-
// FIXME: not reversible
19-
type OpPrependPath struct {
19+
type OpSplitPath struct {
2020
numLevel Op
2121
path Op
22-
prefix string
2322
}
2423

25-
func (op OpPrependPath) Kinds() nodes.Kind {
26-
return nodes.KindString
24+
func (op OpSplitPath) Kinds() nodes.Kind {
25+
return nodes.KindObject
2726
}
2827

29-
func (op OpPrependPath) Check(st *State, n nodes.Node) (bool, error) {
30-
v, ok := n.(nodes.Value)
31-
if !ok {
32-
return false, nil
33-
}
28+
func (op OpSplitPath) Check(st *State, n nodes.Node) (bool, error) {
29+
panic("TODO") // TODO: not reversible
30+
return true, nil
31+
}
3432

35-
res1, err := op.numLevel.Check(st, n)
36-
if err != nil || !res1 {
37-
return false, err
33+
func (op OpSplitPath) Construct(st *State, n nodes.Node) (nodes.Node, error) {
34+
var idents []uast.Identifier
35+
if op.numLevel != nil {
36+
nd, err := op.numLevel.Construct(st, nil)
37+
if err != nil {
38+
return nil, err
39+
}
40+
levels, ok := nd.(nodes.Int)
41+
if !ok {
42+
return nil, ErrUnexpectedType.New(nodes.Int(0), nd)
43+
}
44+
for i := 0; i < int(levels); i++ {
45+
idents = append(idents, uast.Identifier{Name: ".."})
46+
}
3847
}
3948

40-
res2, err := op.path.Check(st, v)
41-
if err != nil || !res2 {
42-
return false, err
49+
nd, err := op.path.Construct(st, n)
50+
if err != nil {
51+
return nil, err
4352
}
44-
45-
return res1 && res2, nil
46-
}
47-
48-
func (op OpPrependPath) Construct(st *State, n nodes.Node) (nodes.Node, error) {
49-
first, err := op.numLevel.Construct(st, n)
50-
if err != nil || first == nil {
51-
return n, err
53+
if nd == nil {
54+
if len(idents) == 0 {
55+
idents = append(idents, uast.Identifier{Name: "."})
56+
}
57+
} else {
58+
path, ok := nd.(nodes.String)
59+
if !ok {
60+
return nil, ErrUnexpectedType.New(nodes.String(""), nd)
61+
}
62+
for _, name := range strings.Split(string(path), ".") {
63+
idents = append(idents, uast.Identifier{Name: name})
64+
}
5265
}
53-
firstVal := first.(nodes.Value)
54-
prependVal := num2dots(firstVal, op.prefix)
55-
56-
path, err := op.path.Construct(st, n)
57-
if err != nil || path == nil {
58-
return n, err
66+
if len(idents) == 1 {
67+
return uast.ToNode(idents[0])
5968
}
60-
return prependVal.(nodes.String) + path.(nodes.String), nil
69+
return uast.ToNode(uast.QualifiedIdentifier{Names: idents})
6170
}
6271

6372
type OpLevelDotsNumConv struct {

fixtures/bench_ethopian_multiplication.py.sem.uast

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@
8080
},
8181
],
8282
Path: { '@type': "uast:Identifier",
83+
'@pos': { '@type': "uast:Positions",
84+
},
8385
Name: "itertools",
8486
},
8587
Target: ~,

fixtures/bench_javaobs.py.sem.uast

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,14 +201,20 @@
201201
'@pos': { '@type': "uast:Positions",
202202
},
203203
Name: { '@type': "uast:Identifier",
204+
'@pos': { '@type': "uast:Positions",
205+
},
204206
Name: "BytesIO",
205207
},
206208
Node: { '@type': "uast:Identifier",
209+
'@pos': { '@type': "uast:Positions",
210+
},
207211
Name: "StringIO",
208212
},
209213
},
210214
],
211215
Path: { '@type': "uast:Identifier",
216+
'@pos': { '@type': "uast:Positions",
217+
},
212218
Name: "StringIO",
213219
},
214220
Target: ~,
@@ -257,6 +263,8 @@
257263
},
258264
],
259265
Path: { '@type': "uast:Identifier",
266+
'@pos': { '@type': "uast:Positions",
267+
},
260268
Name: "io",
261269
},
262270
Target: ~,

fixtures/issue62.py.sem.uast

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
},
2626
],
2727
Path: { '@type': "uast:Identifier",
28+
'@pos': { '@type': "uast:Positions",
29+
},
2830
Name: "os",
2931
},
3032
Target: ~,
@@ -60,9 +62,13 @@
6062
'@pos': { '@type': "uast:Positions",
6163
},
6264
Name: { '@type': "uast:Identifier",
65+
'@pos': { '@type': "uast:Positions",
66+
},
6367
Name: "np",
6468
},
6569
Node: { '@type': "uast:Identifier",
70+
'@pos': { '@type': "uast:Positions",
71+
},
6672
Name: "numpy",
6773
},
6874
},

fixtures/issue62_b.py.sem.uast

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
},
2626
],
2727
Path: { '@type': "uast:Identifier",
28+
'@pos': { '@type': "uast:Positions",
29+
},
2830
Name: "collections",
2931
},
3032
Target: ~,
@@ -45,8 +47,21 @@
4547
Name: "SIMPLE_IDENTIFIER",
4648
},
4749
],
48-
Path: { '@type': "uast:Identifier",
49-
Name: "ast2vec.bblfsh_roles",
50+
Path: { '@type': "uast:QualifiedIdentifier",
51+
'@pos': { '@type': "uast:Positions",
52+
},
53+
Names: [
54+
{ '@type': "uast:Identifier",
55+
'@pos': { '@type': "uast:Positions",
56+
},
57+
Name: "ast2vec",
58+
},
59+
{ '@type': "uast:Identifier",
60+
'@pos': { '@type': "uast:Positions",
61+
},
62+
Name: "bblfsh_roles",
63+
},
64+
],
5065
},
5166
Target: ~,
5267
},
@@ -66,8 +81,26 @@
6681
Name: "Repo2Base",
6782
},
6883
],
69-
Path: { '@type': "uast:Identifier",
70-
Name: "ast2vec.repo2.base",
84+
Path: { '@type': "uast:QualifiedIdentifier",
85+
'@pos': { '@type': "uast:Positions",
86+
},
87+
Names: [
88+
{ '@type': "uast:Identifier",
89+
'@pos': { '@type': "uast:Positions",
90+
},
91+
Name: "ast2vec",
92+
},
93+
{ '@type': "uast:Identifier",
94+
'@pos': { '@type': "uast:Positions",
95+
},
96+
Name: "repo2",
97+
},
98+
{ '@type': "uast:Identifier",
99+
'@pos': { '@type': "uast:Positions",
100+
},
101+
Name: "base",
102+
},
103+
],
71104
},
72105
Target: ~,
73106
},

fixtures/issue94.py.sem.uast

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,21 @@
3030
},
3131
All: false,
3232
Names: ~,
33-
Path: { '@type': "uast:Identifier",
33+
Path: { '@type': "uast:QualifiedIdentifier",
3434
'@pos': { '@type': "uast:Positions",
3535
},
36-
Name: "lib2.lib21",
36+
Names: [
37+
{ '@type': "uast:Identifier",
38+
'@pos': { '@type': "uast:Positions",
39+
},
40+
Name: "lib2",
41+
},
42+
{ '@type': "uast:Identifier",
43+
'@pos': { '@type': "uast:Positions",
44+
},
45+
Name: "lib21",
46+
},
47+
],
3748
},
3849
Target: ~,
3950
},
@@ -51,9 +62,13 @@
5162
'@pos': { '@type': "uast:Positions",
5263
},
5364
Name: { '@type': "uast:Identifier",
65+
'@pos': { '@type': "uast:Positions",
66+
},
5467
Name: "lib3_alias",
5568
},
5669
Node: { '@type': "uast:Identifier",
70+
'@pos': { '@type': "uast:Positions",
71+
},
5772
Name: "lib3",
5873
},
5974
},
@@ -81,6 +96,8 @@
8196
},
8297
],
8398
Path: { '@type': "uast:Identifier",
99+
'@pos': { '@type': "uast:Positions",
100+
},
84101
Name: "lib4",
85102
},
86103
Target: ~,
@@ -101,8 +118,21 @@
101118
Name: "lib511",
102119
},
103120
],
104-
Path: { '@type': "uast:Identifier",
105-
Name: "lib5.lib51",
121+
Path: { '@type': "uast:QualifiedIdentifier",
122+
'@pos': { '@type': "uast:Positions",
123+
},
124+
Names: [
125+
{ '@type': "uast:Identifier",
126+
'@pos': { '@type': "uast:Positions",
127+
},
128+
Name: "lib5",
129+
},
130+
{ '@type': "uast:Identifier",
131+
'@pos': { '@type': "uast:Positions",
132+
},
133+
Name: "lib51",
134+
},
135+
],
106136
},
107137
Target: ~,
108138
},
@@ -125,14 +155,20 @@
125155
'@pos': { '@type': "uast:Positions",
126156
},
127157
Name: { '@type': "uast:Identifier",
158+
'@pos': { '@type': "uast:Positions",
159+
},
128160
Name: "lib611",
129161
},
130162
Node: { '@type': "uast:Identifier",
163+
'@pos': { '@type': "uast:Positions",
164+
},
131165
Name: "lib61",
132166
},
133167
},
134168
],
135169
Path: { '@type': "uast:Identifier",
170+
'@pos': { '@type': "uast:Positions",
171+
},
136172
Name: "lib6",
137173
},
138174
Target: ~,

0 commit comments

Comments
 (0)