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

Commit c5a26bc

Browse files
author
Juanjo Alvarez
committed
Some fixes from review.
Use strings.repeat instead of reinventing the wheel Keep the numeric level as internal property in the UAST Remove the now unneeded dots2num and simplify Construct Signed-off-by: Juanjo Alvarez <[email protected]>
1 parent 421878f commit c5a26bc

14 files changed

+87
-142
lines changed

.gitignore

Lines changed: 2 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1,111 +1,2 @@
1-
# Byte-compiled / optimized / DLL files
2-
__pycache__/
3-
*.py[cod]
4-
*$py.class
5-
6-
# C extensions
7-
*.so
8-
9-
# Distribution / packaging
10-
.Python
11-
env/
12-
build/
13-
develop-eggs/
14-
dist/
15-
downloads/
16-
eggs/
17-
.eggs/
18-
lib/
19-
lib64/
20-
parts/
21-
sdist/
22-
var/
23-
*.egg-info/
24-
.installed.cfg
25-
*.egg
26-
27-
# PyInstaller
28-
# Usually these files are written by a python script from a template
29-
# before PyInstaller builds the exe, so as to inject date/other infos into it.
30-
*.manifest
31-
*.spec
32-
33-
# Installer logs
34-
pip-log.txt
35-
pip-delete-this-directory.txt
36-
37-
# Unit test / coverage reports
38-
htmlcov/
39-
.tox/
40-
.coverage
41-
.coverage.*
42-
.cache
43-
nosetests.xml
44-
coverage.xml
45-
*,cover
46-
.hypothesis/
47-
48-
# Translations
49-
*.mo
50-
*.pot
51-
52-
# Django stuff:
53-
*.log
54-
local_settings.py
55-
56-
# Flask stuff:
57-
instance/
58-
.webassets-cache
59-
60-
# Scrapy stuff:
61-
.scrapy
62-
63-
# Sphinx documentation
64-
docs/_build/
65-
66-
# PyBuilder
67-
target/
68-
69-
# IPython Notebook
70-
.ipynb_checkpoints
71-
72-
# pyenv
73-
.python-version
74-
75-
# celery beat schedule file
76-
celerybeat-schedule
77-
78-
# dotenv
79-
.env
80-
81-
# virtualenv
82-
venv/
83-
ENV/
84-
85-
# Spyder project settings
86-
.spyderproject
87-
88-
# Rope project settings
89-
.ropeproject
90-
91-
.idea/*
92-
93-
test/nimcache/*
94-
test/sendmsg
95-
96-
# pycharm
97-
.idea/*
98-
99-
# virtualenv
100-
36env/*
101-
install.sh
102-
mypy/*
103-
104-
Dockerfile
105-
106-
.sdk/*
107-
driver/main
108-
main
109-
.local/*
110-
111-
.mypy_cache
1+
.sdk
2+
build

Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,5 @@ build-native-internal:
1212
cd native/python_package/ && pip3 install -U --user .
1313
cp native/sh/native.sh $(BUILD_PATH)/bin/native;
1414
chmod +x $(BUILD_PATH)/bin/native
15-
cat $(BUILD_PATH)/bin/native
1615

1716
include .sdk/Makefile

driver/normalizer/annotation.go

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package normalizer
22

33
import (
4+
"strings"
5+
46
"gopkg.in/bblfsh/sdk.v2/uast"
57
"gopkg.in/bblfsh/sdk.v2/uast/role"
68
. "gopkg.in/bblfsh/sdk.v2/uast/transformer"
@@ -88,6 +90,54 @@ func loopAnnotate(typ string, mainRole role.Role, roles ...role.Role) Mapping {
8890
}, roles...)
8991
}
9092

93+
94+
func num2dots(n uast.Value) uast.Value {
95+
if intval, ok := n.(uast.Int); ok {
96+
i64val := int(intval)
97+
return uast.String(strings.Repeat(".", int(i64val)))
98+
}
99+
return n
100+
}
101+
102+
type opLevelDotsNumConv struct {
103+
op Op
104+
orig Op
105+
}
106+
107+
func (op opLevelDotsNumConv) Check(st *State, n uast.Node) (bool, error) {
108+
v, ok := n.(uast.Value)
109+
if !ok {
110+
return false, nil
111+
}
112+
113+
nv := num2dots(v)
114+
res1, err := op.op.Check(st, nv)
115+
if err != nil || !res1{
116+
return false, err
117+
}
118+
119+
res2, err := op.orig.Check(st, v)
120+
if err != nil || !res2{
121+
return false, err
122+
}
123+
124+
return res1 && res2, nil
125+
}
126+
127+
func (op opLevelDotsNumConv) Construct(st *State, n uast.Node) (uast.Node, error) {
128+
n, err := op.orig.Construct(st, n)
129+
if err != nil {
130+
return nil, err
131+
}
132+
133+
v, ok := n.(uast.Int)
134+
if !ok {
135+
return nil, ErrExpectedValue.New(n)
136+
}
137+
138+
return v, nil
139+
}
140+
91141
var Annotations = []Mapping{
92142
ObjectToNode{
93143
InternalTypeKey: "ast_type",
@@ -402,7 +452,7 @@ var Annotations = []Mapping{
402452

403453
MapAST("ImportFrom", Obj{
404454
"module": Var("module"),
405-
"level": Var("level"),
455+
"level": opLevelDotsNumConv{op: Var("level"), orig: Var("origlevel")},
406456
"names": Var("names"),
407457
}, Obj{
408458
"names": Obj{
@@ -417,10 +467,11 @@ var Annotations = []Mapping{
417467
uast.KeyRoles: Roles(role.Import, role.Incomplete),
418468
},
419469
"module": Obj{
420-
uast.KeyType: String("ImportFrom.module"),
470+
uast.KeyType: String("ImportFrom.module"),
421471
uast.KeyToken: Var("module"),
422472
uast.KeyRoles: Roles(role.Import, role.Pathname, role.Identifier),
423473
},
474+
"num_level": Var("origlevel"),
424475
}, role.Import, role.Declaration, role.Statement),
425476

426477
MapAST("alias", Obj{

fixtures/issue62.py.uast

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ Module {
1919
. . . }
2020
. . . Properties: {
2121
. . . . internalRole: body
22+
. . . . num_level: 0
2223
. . . }
2324
. . . Children: {
2425
. . . . 0: ImportFrom.level {
2526
. . . . . Roles: Import,Incomplete
26-
. . . . . TOKEN "0"
2727
. . . . . Properties: {
2828
. . . . . . internalRole: level
2929
. . . . . }

fixtures/issue62_b.py.uast

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ Module {
1919
. . . }
2020
. . . Properties: {
2121
. . . . internalRole: body
22+
. . . . num_level: 0
2223
. . . }
2324
. . . Children: {
2425
. . . . 0: ImportFrom.level {
2526
. . . . . Roles: Import,Incomplete
26-
. . . . . TOKEN "0"
2727
. . . . . Properties: {
2828
. . . . . . internalRole: level
2929
. . . . . }
@@ -69,11 +69,11 @@ Module {
6969
. . . }
7070
. . . Properties: {
7171
. . . . internalRole: body
72+
. . . . num_level: 0
7273
. . . }
7374
. . . Children: {
7475
. . . . 0: ImportFrom.level {
7576
. . . . . Roles: Import,Incomplete
76-
. . . . . TOKEN "0"
7777
. . . . . Properties: {
7878
. . . . . . internalRole: level
7979
. . . . . }
@@ -135,11 +135,11 @@ Module {
135135
. . . }
136136
. . . Properties: {
137137
. . . . internalRole: body
138+
. . . . num_level: 0
138139
. . . }
139140
. . . Children: {
140141
. . . . 0: ImportFrom.level {
141142
. . . . . Roles: Import,Incomplete
142-
. . . . . TOKEN "0"
143143
. . . . . Properties: {
144144
. . . . . . internalRole: level
145145
. . . . . }

fixtures/issue94.py.uast

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,11 @@ Module {
131131
. . . }
132132
. . . Properties: {
133133
. . . . internalRole: body
134+
. . . . num_level: 0
134135
. . . }
135136
. . . Children: {
136137
. . . . 0: ImportFrom.level {
137138
. . . . . Roles: Import,Incomplete
138-
. . . . . TOKEN "0"
139139
. . . . . Properties: {
140140
. . . . . . internalRole: level
141141
. . . . . }
@@ -181,11 +181,11 @@ Module {
181181
. . . }
182182
. . . Properties: {
183183
. . . . internalRole: body
184+
. . . . num_level: 0
184185
. . . }
185186
. . . Children: {
186187
. . . . 0: ImportFrom.level {
187188
. . . . . Roles: Import,Incomplete
188-
. . . . . TOKEN "0"
189189
. . . . . Properties: {
190190
. . . . . . internalRole: level
191191
. . . . . }
@@ -236,11 +236,11 @@ Module {
236236
. . . }
237237
. . . Properties: {
238238
. . . . internalRole: body
239+
. . . . num_level: 0
239240
. . . }
240241
. . . Children: {
241242
. . . . 0: ImportFrom.level {
242243
. . . . . Roles: Import,Incomplete
243-
. . . . . TOKEN "0"
244244
. . . . . Properties: {
245245
. . . . . . internalRole: level
246246
. . . . . }

fixtures/issue96.py.uast

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,11 @@ Module {
131131
. . . }
132132
. . . Properties: {
133133
. . . . internalRole: body
134+
. . . . num_level: 0
134135
. . . }
135136
. . . Children: {
136137
. . . . 0: ImportFrom.level {
137138
. . . . . Roles: Import,Incomplete
138-
. . . . . TOKEN "0"
139139
. . . . . Properties: {
140140
. . . . . . internalRole: level
141141
. . . . . }
@@ -181,11 +181,11 @@ Module {
181181
. . . }
182182
. . . Properties: {
183183
. . . . internalRole: body
184+
. . . . num_level: 0
184185
. . . }
185186
. . . Children: {
186187
. . . . 0: ImportFrom.level {
187188
. . . . . Roles: Import,Incomplete
188-
. . . . . TOKEN "0"
189189
. . . . . Properties: {
190190
. . . . . . internalRole: level
191191
. . . . . }
@@ -236,11 +236,11 @@ Module {
236236
. . . }
237237
. . . Properties: {
238238
. . . . internalRole: body
239+
. . . . num_level: 0
239240
. . . }
240241
. . . Children: {
241242
. . . . 0: ImportFrom.level {
242243
. . . . . Roles: Import,Incomplete
243-
. . . . . TOKEN "0"
244244
. . . . . Properties: {
245245
. . . . . . internalRole: level
246246
. . . . . }

0 commit comments

Comments
 (0)