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

Commit

Permalink
Some fixes from review.
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
Juanjo Alvarez committed May 16, 2018
1 parent 421878f commit c5a26bc
Show file tree
Hide file tree
Showing 14 changed files with 87 additions and 142 deletions.
113 changes: 2 additions & 111 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,111 +1,2 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover
.hypothesis/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# IPython Notebook
.ipynb_checkpoints

# pyenv
.python-version

# celery beat schedule file
celerybeat-schedule

# dotenv
.env

# virtualenv
venv/
ENV/

# Spyder project settings
.spyderproject

# Rope project settings
.ropeproject

.idea/*

test/nimcache/*
test/sendmsg

# pycharm
.idea/*

# virtualenv
36env/*
install.sh
mypy/*

Dockerfile

.sdk/*
driver/main
main
.local/*

.mypy_cache
.sdk
build
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@ build-native-internal:
cd native/python_package/ && pip3 install -U --user .
cp native/sh/native.sh $(BUILD_PATH)/bin/native;
chmod +x $(BUILD_PATH)/bin/native
cat $(BUILD_PATH)/bin/native

include .sdk/Makefile
55 changes: 53 additions & 2 deletions driver/normalizer/annotation.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package normalizer

import (
"strings"

"gopkg.in/bblfsh/sdk.v2/uast"
"gopkg.in/bblfsh/sdk.v2/uast/role"
. "gopkg.in/bblfsh/sdk.v2/uast/transformer"
Expand Down Expand Up @@ -88,6 +90,54 @@ func loopAnnotate(typ string, mainRole role.Role, roles ...role.Role) Mapping {
}, roles...)
}


func num2dots(n uast.Value) uast.Value {
if intval, ok := n.(uast.Int); ok {
i64val := int(intval)
return uast.String(strings.Repeat(".", int(i64val)))
}
return n
}

type opLevelDotsNumConv struct {
op Op
orig Op
}

func (op opLevelDotsNumConv) Check(st *State, n uast.Node) (bool, error) {
v, ok := n.(uast.Value)
if !ok {
return false, nil
}

nv := num2dots(v)
res1, err := op.op.Check(st, nv)
if err != nil || !res1{
return false, err
}

res2, err := op.orig.Check(st, v)
if err != nil || !res2{
return false, err
}

return res1 && res2, nil
}

func (op opLevelDotsNumConv) Construct(st *State, n uast.Node) (uast.Node, error) {
n, err := op.orig.Construct(st, n)
if err != nil {
return nil, err
}

v, ok := n.(uast.Int)
if !ok {
return nil, ErrExpectedValue.New(n)
}

return v, nil
}

var Annotations = []Mapping{
ObjectToNode{
InternalTypeKey: "ast_type",
Expand Down Expand Up @@ -402,7 +452,7 @@ var Annotations = []Mapping{

MapAST("ImportFrom", Obj{
"module": Var("module"),
"level": Var("level"),
"level": opLevelDotsNumConv{op: Var("level"), orig: Var("origlevel")},
"names": Var("names"),
}, Obj{
"names": Obj{
Expand All @@ -417,10 +467,11 @@ var Annotations = []Mapping{
uast.KeyRoles: Roles(role.Import, role.Incomplete),
},
"module": Obj{
uast.KeyType: String("ImportFrom.module"),
uast.KeyType: String("ImportFrom.module"),
uast.KeyToken: Var("module"),
uast.KeyRoles: Roles(role.Import, role.Pathname, role.Identifier),
},
"num_level": Var("origlevel"),
}, role.Import, role.Declaration, role.Statement),

MapAST("alias", Obj{
Expand Down
2 changes: 1 addition & 1 deletion fixtures/issue62.py.uast
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ Module {
. . . }
. . . Properties: {
. . . . internalRole: body
. . . . num_level: 0
. . . }
. . . Children: {
. . . . 0: ImportFrom.level {
. . . . . Roles: Import,Incomplete
. . . . . TOKEN "0"
. . . . . Properties: {
. . . . . . internalRole: level
. . . . . }
Expand Down
6 changes: 3 additions & 3 deletions fixtures/issue62_b.py.uast
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ Module {
. . . }
. . . Properties: {
. . . . internalRole: body
. . . . num_level: 0
. . . }
. . . Children: {
. . . . 0: ImportFrom.level {
. . . . . Roles: Import,Incomplete
. . . . . TOKEN "0"
. . . . . Properties: {
. . . . . . internalRole: level
. . . . . }
Expand Down Expand Up @@ -69,11 +69,11 @@ Module {
. . . }
. . . Properties: {
. . . . internalRole: body
. . . . num_level: 0
. . . }
. . . Children: {
. . . . 0: ImportFrom.level {
. . . . . Roles: Import,Incomplete
. . . . . TOKEN "0"
. . . . . Properties: {
. . . . . . internalRole: level
. . . . . }
Expand Down Expand Up @@ -135,11 +135,11 @@ Module {
. . . }
. . . Properties: {
. . . . internalRole: body
. . . . num_level: 0
. . . }
. . . Children: {
. . . . 0: ImportFrom.level {
. . . . . Roles: Import,Incomplete
. . . . . TOKEN "0"
. . . . . Properties: {
. . . . . . internalRole: level
. . . . . }
Expand Down
6 changes: 3 additions & 3 deletions fixtures/issue94.py.uast
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,11 @@ Module {
. . . }
. . . Properties: {
. . . . internalRole: body
. . . . num_level: 0
. . . }
. . . Children: {
. . . . 0: ImportFrom.level {
. . . . . Roles: Import,Incomplete
. . . . . TOKEN "0"
. . . . . Properties: {
. . . . . . internalRole: level
. . . . . }
Expand Down Expand Up @@ -181,11 +181,11 @@ Module {
. . . }
. . . Properties: {
. . . . internalRole: body
. . . . num_level: 0
. . . }
. . . Children: {
. . . . 0: ImportFrom.level {
. . . . . Roles: Import,Incomplete
. . . . . TOKEN "0"
. . . . . Properties: {
. . . . . . internalRole: level
. . . . . }
Expand Down Expand Up @@ -236,11 +236,11 @@ Module {
. . . }
. . . Properties: {
. . . . internalRole: body
. . . . num_level: 0
. . . }
. . . Children: {
. . . . 0: ImportFrom.level {
. . . . . Roles: Import,Incomplete
. . . . . TOKEN "0"
. . . . . Properties: {
. . . . . . internalRole: level
. . . . . }
Expand Down
6 changes: 3 additions & 3 deletions fixtures/issue96.py.uast
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,11 @@ Module {
. . . }
. . . Properties: {
. . . . internalRole: body
. . . . num_level: 0
. . . }
. . . Children: {
. . . . 0: ImportFrom.level {
. . . . . Roles: Import,Incomplete
. . . . . TOKEN "0"
. . . . . Properties: {
. . . . . . internalRole: level
. . . . . }
Expand Down Expand Up @@ -181,11 +181,11 @@ Module {
. . . }
. . . Properties: {
. . . . internalRole: body
. . . . num_level: 0
. . . }
. . . Children: {
. . . . 0: ImportFrom.level {
. . . . . Roles: Import,Incomplete
. . . . . TOKEN "0"
. . . . . Properties: {
. . . . . . internalRole: level
. . . . . }
Expand Down Expand Up @@ -236,11 +236,11 @@ Module {
. . . }
. . . Properties: {
. . . . internalRole: body
. . . . num_level: 0
. . . }
. . . Children: {
. . . . 0: ImportFrom.level {
. . . . . Roles: Import,Incomplete
. . . . . TOKEN "0"
. . . . . Properties: {
. . . . . . internalRole: level
. . . . . }
Expand Down
Loading

0 comments on commit c5a26bc

Please sign in to comment.