Skip to content
This repository was archived by the owner on Mar 8, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
182 changes: 0 additions & 182 deletions ANNOTATION.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Dockerfile.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ ARG PYDETECTOR_VER=0.14.2

RUN apk add --no-cache --update python python3 py-pip py2-pip git

ADD build /opt/driver/bin
ADD native/python_package /tmp/python_driver

ADD ${DEVDEPS} ${CONTAINER_DEVDEPS}
Expand All @@ -20,4 +19,5 @@ RUN yes|rm -rf ${CONTAINER_DEVDEPS}
RUN pip3 install /tmp/python_driver
RUN yes|rm -rf /tmp/python_driver

CMD /opt/driver/bin/driver
ADD build /opt/driver
ENTRYPOINT ["/opt/driver/bin/driver"]
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ build-native-internal:
pip3 install --user ${DEV_DEPS}/python-pydetector/ || pip3 install --user pydetector-bblfsh
cd native/python_package/ && \
pip3 install -U --user .
cp native/sh/native.sh $(BUILD_PATH)/native;
chmod +x $(BUILD_PATH)/native
cp native/sh/native.sh $(BUILD_PATH)/bin/native;
chmod +x $(BUILD_PATH)/bin/native


include .sdk/Makefile
21 changes: 10 additions & 11 deletions driver/main.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package main

import (
"gopkg.in/bblfsh/sdk.v1/protocol/driver"

"github.com/bblfsh/python-driver/driver/normalizer"
)

var version string
var build string
"gopkg.in/bblfsh/sdk.v1/sdk/driver"
)

func main() {
d := driver.Driver{
Version: version,
Build: build,
ParserBuilder: normalizer.ParserBuilder,
Annotate: normalizer.AnnotationRules,
d, err := driver.NewDriver(normalizer.ToNode, normalizer.Transformers)
if err != nil {
panic(err)
}

s := driver.NewServer(d)
if err := s.Start(); err != nil {
panic(err)
}
d.Exec()
}
17 changes: 15 additions & 2 deletions driver/normalizer/annotation.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (

"gopkg.in/bblfsh/sdk.v1/uast"
. "gopkg.in/bblfsh/sdk.v1/uast/ann"
"gopkg.in/bblfsh/sdk.v1/uast/transformer"
"gopkg.in/bblfsh/sdk.v1/uast/transformer/annotatter"
"gopkg.in/bblfsh/sdk.v1/uast/transformer/positioner"
)

/*
Expand Down Expand Up @@ -37,7 +40,17 @@ Unmarked nodes or nodes needing new features from the SDK:
(see: https://greentreesnakes.readthedocs.io/en/latest/nodes.html#Compare)
*/

// AnnotationRules for the Python driver
// Transformers is the of list `transformer.Transfomer` to apply to a UAST, to
// learn more about the Transformers and the available ones take a look to:
// https://godoc.org/gopkg.in/bblfsh/sdk.v1/uast/transformers
var Transformers = []transformer.Tranformer{
annotatter.NewAnnotatter(AnnotationRules),
positioner.NewFillOffsetFromLineCol(),
}

// AnnotationRules describes how a UAST should be annotated with `uast.Role`.
//
// https://godoc.org/gopkg.in/bblfsh/sdk.v1/uast/ann
var AnnotationRules = On(Any).Self(
On(Not(pyast.Module)).Error(errors.New("root must be uast.Module")),
On(pyast.Module).Roles(uast.File).Descendants(
Expand Down Expand Up @@ -239,7 +252,7 @@ var AnnotationRules = On(Any).Self(
On(pyast.AliasAsName).Roles(uast.Import, uast.Alias, uast.Identifier),
On(pyast.ImportFrom).Roles(uast.Import, uast.Declaration, uast.Statement),
On(pyast.ClassDef).Roles(uast.Type, uast.Declaration, uast.Identifier, uast.Statement).Children(
On(pyast.ClassDefDecorators).Roles(uast.Type, uast.Call, uast.Incomplete),
On(pyast.ClassDefDecorators).Roles(uast.Type, uast.Call, uast.Incomplete),
On(pyast.ClassDefBody).Roles(uast.Type, uast.Declaration, uast.Body),
On(pyast.ClassDefBases).Roles(uast.Type, uast.Declaration, uast.Base),
On(pyast.ClassDefKeywords).Roles(uast.Incomplete).Children(
Expand Down
8 changes: 4 additions & 4 deletions driver/normalizer/annotation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestAnnotate(t *testing.T) {
f, err := getFixture("python_example_1.json")
require.NoError(err)

n, err := ToNoder.ToNode(f)
n, err := ToNode.ToNode(f)
require.NoError(err)
require.NotNil(n)

Expand All @@ -45,7 +45,7 @@ func TestAnnotatePrettyAnnotationsOnly(t *testing.T) {
f, err := getFixture("python_example_1.json")
require.NoError(err)

n, err := ToNoder.ToNode(f)
n, err := ToNode.ToNode(f)
require.NoError(err)
require.NotNil(n)

Expand All @@ -59,7 +59,7 @@ func TestNodeTokens(t *testing.T) {
f, err := getFixture("python_example_1.json")
require.NoError(err)

n, err := ToNoder.ToNode(f)
n, err := ToNode.ToNode(f)
require.NoError(err)
require.NotNil(n)

Expand All @@ -76,7 +76,7 @@ func TestAll(t *testing.T) {
f, err := getFixture("python_example_1.json")
require.NoError(err)

n, err := ToNoder.ToNode(f)
n, err := ToNode.ToNode(f)
require.NoError(err)
require.NotNil(n)

Expand Down
113 changes: 45 additions & 68 deletions driver/normalizer/parser.go → driver/normalizer/tonode.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package normalizer

import (
"gopkg.in/bblfsh/sdk.v1/protocol/driver"
"gopkg.in/bblfsh/sdk.v1/protocol/native"
)
import "gopkg.in/bblfsh/sdk.v1/uast"

var ToNoder = &native.ObjectToNoder{
// ToNode is an instance of `uast.ObjectToNode`, defining how to transform an
// into a UAST (`uast.Node`).
//
// https://godoc.org/gopkg.in/bblfsh/sdk.v1/uast#ObjectToNode
var ToNode = &uast.ObjectToNode{
InternalTypeKey: "ast_type",
LineKey: "lineno",
EndLineKey: "end_lineno",
ColumnKey: "col_offset",
EndColumnKey: "end_col_offset",
PositionFill: native.OffsetFromLineCol,

TokenKeys: map[string]bool{
"name": true,
Expand All @@ -32,45 +32,45 @@ var ToNoder = &native.ObjectToNoder{
"BitOr": "|",
"BitXor": "^",
"Break": "break",
"Continue": "continue",
"Delete": "delete",
"Div": "/",
"Ellipsis": "...",
"Eq": "==",
"False": "false",
"For": "for",
"FloorDiv": "//",
"Global": "global",
"Gt": ">",
"GtE": ">=",
"If": "if",
"In": "in",
"Invert": "~",
"Is": "is",
"IsNot": "not is",
"LShift": "<<",
"Lt": "<",
"LtE": "<=",
"Mod": "%%",
"Mult": "*",
"None": "None",
"Nonlocal": "nonlocal",
"Not": "!",
"NotEq": "!=",
"NotIn": "not in",
"Pass": "pass",
"Pow": "**",
"Print": "print",
"Raise": "raise",
"Return": "return",
"RShift": ">>",
"Sub": "-",
"True": "true",
"UAdd": "+",
"USub": "-",
"While": "while",
"With": "with",
"Yield": "yield",
"Continue": "continue",
"Delete": "delete",
"Div": "/",
"Ellipsis": "...",
"Eq": "==",
"False": "false",
"For": "for",
"FloorDiv": "//",
"Global": "global",
"Gt": ">",
"GtE": ">=",
"If": "if",
"In": "in",
"Invert": "~",
"Is": "is",
"IsNot": "not is",
"LShift": "<<",
"Lt": "<",
"LtE": "<=",
"Mod": "%%",
"Mult": "*",
"None": "None",
"Nonlocal": "nonlocal",
"Not": "!",
"NotEq": "!=",
"NotIn": "not in",
"Pass": "pass",
"Pow": "**",
"Print": "print",
"Raise": "raise",
"Return": "return",
"RShift": ">>",
"Sub": "-",
"True": "true",
"UAdd": "+",
"USub": "-",
"While": "while",
"With": "with",
"Yield": "yield",
},
PromoteAllPropertyLists: false,
PromotedPropertyLists: map[string]map[string]bool{
Expand All @@ -96,26 +96,3 @@ var ToNoder = &native.ObjectToNoder{
},
// FIXME: test[ast_type=Compare].comparators is a list?? (should be "right")
}

// ParserBuilder creates a parser that transform source code files into *uast.Node.
func ParserBuilder(opts driver.ParserOptions) (parser driver.Parser, err error) {
parser, err = native.ExecParser(ToNoder, opts.NativeBin)
if err != nil {
return
}

switch ToNoder.PositionFill {
case native.OffsetFromLineCol:
parser = &driver.TransformationParser{
Parser: parser,
Transformation: driver.FillOffsetFromLineCol,
}
case native.LineColFromOffset:
parser = &driver.TransformationParser{
Parser: parser,
Transformation: driver.FillLineColFromOffset,
}
}

return
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func TestNativeToNoder(t *testing.T) {
f, err := getFixture("python_example_1.json")
require.NoError(err)

n, err := ToNoder.ToNode(f)
n, err := ToNode.ToNode(f)
require.NoError(err)
require.NotNil(n)
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.