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

Commit c95458b

Browse files
committed
*: changes driver refactor
Signed-off-by: Máximo Cuadros <[email protected]>
1 parent 00c31fe commit c95458b

File tree

190 files changed

+79
-272
lines changed

Some content is hidden

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

190 files changed

+79
-272
lines changed

ANNOTATION.md

Lines changed: 0 additions & 182 deletions

Dockerfile.tpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ ARG PYDETECTOR_VER=0.14.2
77

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

10-
ADD build /opt/driver/bin
1110
ADD native/python_package /tmp/python_driver
1211

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

23-
CMD /opt/driver/bin/driver
22+
ADD build /opt/driver
23+
ENTRYPOINT ["/opt/driver/bin/driver"]

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ build-native-internal:
1010
pip3 install --user ${DEV_DEPS}/python-pydetector/ || pip3 install --user pydetector-bblfsh
1111
cd native/python_package/ && \
1212
pip3 install -U --user .
13-
cp native/sh/native.sh $(BUILD_PATH)/native;
14-
chmod +x $(BUILD_PATH)/native
13+
cp native/sh/native.sh $(BUILD_PATH)/bin/native;
14+
chmod +x $(BUILD_PATH)/bin/native
1515

1616

1717
include .sdk/Makefile

driver/main.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
package main
22

33
import (
4-
"gopkg.in/bblfsh/sdk.v1/protocol/driver"
5-
64
"github.com/bblfsh/python-driver/driver/normalizer"
7-
)
85

9-
var version string
10-
var build string
6+
"gopkg.in/bblfsh/sdk.v1/sdk/driver"
7+
)
118

129
func main() {
13-
d := driver.Driver{
14-
Version: version,
15-
Build: build,
16-
ParserBuilder: normalizer.ParserBuilder,
17-
Annotate: normalizer.AnnotationRules,
10+
d, err := driver.NewDriver(normalizer.ToNode, normalizer.Transformers)
11+
if err != nil {
12+
panic(err)
13+
}
14+
15+
s := driver.NewServer(d)
16+
if err := s.Start(); err != nil {
17+
panic(err)
1818
}
19-
d.Exec()
2019
}

driver/normalizer/annotation.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import (
77

88
"gopkg.in/bblfsh/sdk.v1/uast"
99
. "gopkg.in/bblfsh/sdk.v1/uast/ann"
10+
"gopkg.in/bblfsh/sdk.v1/uast/transformer"
11+
"gopkg.in/bblfsh/sdk.v1/uast/transformer/annotatter"
12+
"gopkg.in/bblfsh/sdk.v1/uast/transformer/positioner"
1013
)
1114

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

40-
// AnnotationRules for the Python driver
43+
// Transformers is the of list `transformer.Transfomer` to apply to a UAST, to
44+
// learn more about the Transformers and the available ones take a look to:
45+
// https://godoc.org/gopkg.in/bblfsh/sdk.v1/uast/transformers
46+
var Transformers = []transformer.Tranformer{
47+
annotatter.NewAnnotatter(AnnotationRules),
48+
positioner.NewFillOffsetFromLineCol(),
49+
}
50+
51+
// AnnotationRules describes how a UAST should be annotated with `uast.Role`.
52+
//
53+
// https://godoc.org/gopkg.in/bblfsh/sdk.v1/uast/ann
4154
var AnnotationRules = On(Any).Self(
4255
On(Not(pyast.Module)).Error(errors.New("root must be uast.Module")),
4356
On(pyast.Module).Roles(uast.File).Descendants(
@@ -239,7 +252,7 @@ var AnnotationRules = On(Any).Self(
239252
On(pyast.AliasAsName).Roles(uast.Import, uast.Alias, uast.Identifier),
240253
On(pyast.ImportFrom).Roles(uast.Import, uast.Declaration, uast.Statement),
241254
On(pyast.ClassDef).Roles(uast.Type, uast.Declaration, uast.Identifier, uast.Statement).Children(
242-
On(pyast.ClassDefDecorators).Roles(uast.Type, uast.Call, uast.Incomplete),
255+
On(pyast.ClassDefDecorators).Roles(uast.Type, uast.Call, uast.Incomplete),
243256
On(pyast.ClassDefBody).Roles(uast.Type, uast.Declaration, uast.Body),
244257
On(pyast.ClassDefBases).Roles(uast.Type, uast.Declaration, uast.Base),
245258
On(pyast.ClassDefKeywords).Roles(uast.Incomplete).Children(

driver/normalizer/annotation_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func TestAnnotate(t *testing.T) {
2020
f, err := getFixture("python_example_1.json")
2121
require.NoError(err)
2222

23-
n, err := ToNoder.ToNode(f)
23+
n, err := ToNode.ToNode(f)
2424
require.NoError(err)
2525
require.NotNil(n)
2626

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

48-
n, err := ToNoder.ToNode(f)
48+
n, err := ToNode.ToNode(f)
4949
require.NoError(err)
5050
require.NotNil(n)
5151

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

62-
n, err := ToNoder.ToNode(f)
62+
n, err := ToNode.ToNode(f)
6363
require.NoError(err)
6464
require.NotNil(n)
6565

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

79-
n, err := ToNoder.ToNode(f)
79+
n, err := ToNode.ToNode(f)
8080
require.NoError(err)
8181
require.NotNil(n)
8282

Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
package normalizer
22

3-
import (
4-
"gopkg.in/bblfsh/sdk.v1/protocol/driver"
5-
"gopkg.in/bblfsh/sdk.v1/protocol/native"
6-
)
3+
import "gopkg.in/bblfsh/sdk.v1/uast"
74

8-
var ToNoder = &native.ObjectToNoder{
5+
// ToNode is an instance of `uast.ObjectToNode`, defining how to transform an
6+
// into a UAST (`uast.Node`).
7+
//
8+
// https://godoc.org/gopkg.in/bblfsh/sdk.v1/uast#ObjectToNode
9+
var ToNode = &uast.ObjectToNode{
910
InternalTypeKey: "ast_type",
1011
LineKey: "lineno",
1112
EndLineKey: "end_lineno",
1213
ColumnKey: "col_offset",
1314
EndColumnKey: "end_col_offset",
14-
PositionFill: native.OffsetFromLineCol,
1515

1616
TokenKeys: map[string]bool{
1717
"name": true,
@@ -32,45 +32,45 @@ var ToNoder = &native.ObjectToNoder{
3232
"BitOr": "|",
3333
"BitXor": "^",
3434
"Break": "break",
35-
"Continue": "continue",
36-
"Delete": "delete",
37-
"Div": "/",
38-
"Ellipsis": "...",
39-
"Eq": "==",
40-
"False": "false",
41-
"For": "for",
42-
"FloorDiv": "//",
43-
"Global": "global",
44-
"Gt": ">",
45-
"GtE": ">=",
46-
"If": "if",
47-
"In": "in",
48-
"Invert": "~",
49-
"Is": "is",
50-
"IsNot": "not is",
51-
"LShift": "<<",
52-
"Lt": "<",
53-
"LtE": "<=",
54-
"Mod": "%%",
55-
"Mult": "*",
56-
"None": "None",
57-
"Nonlocal": "nonlocal",
58-
"Not": "!",
59-
"NotEq": "!=",
60-
"NotIn": "not in",
61-
"Pass": "pass",
62-
"Pow": "**",
63-
"Print": "print",
64-
"Raise": "raise",
65-
"Return": "return",
66-
"RShift": ">>",
67-
"Sub": "-",
68-
"True": "true",
69-
"UAdd": "+",
70-
"USub": "-",
71-
"While": "while",
72-
"With": "with",
73-
"Yield": "yield",
35+
"Continue": "continue",
36+
"Delete": "delete",
37+
"Div": "/",
38+
"Ellipsis": "...",
39+
"Eq": "==",
40+
"False": "false",
41+
"For": "for",
42+
"FloorDiv": "//",
43+
"Global": "global",
44+
"Gt": ">",
45+
"GtE": ">=",
46+
"If": "if",
47+
"In": "in",
48+
"Invert": "~",
49+
"Is": "is",
50+
"IsNot": "not is",
51+
"LShift": "<<",
52+
"Lt": "<",
53+
"LtE": "<=",
54+
"Mod": "%%",
55+
"Mult": "*",
56+
"None": "None",
57+
"Nonlocal": "nonlocal",
58+
"Not": "!",
59+
"NotEq": "!=",
60+
"NotIn": "not in",
61+
"Pass": "pass",
62+
"Pow": "**",
63+
"Print": "print",
64+
"Raise": "raise",
65+
"Return": "return",
66+
"RShift": ">>",
67+
"Sub": "-",
68+
"True": "true",
69+
"UAdd": "+",
70+
"USub": "-",
71+
"While": "while",
72+
"With": "with",
73+
"Yield": "yield",
7474
},
7575
PromoteAllPropertyLists: false,
7676
PromotedPropertyLists: map[string]map[string]bool{
@@ -96,26 +96,3 @@ var ToNoder = &native.ObjectToNoder{
9696
},
9797
// FIXME: test[ast_type=Compare].comparators is a list?? (should be "right")
9898
}
99-
100-
// ParserBuilder creates a parser that transform source code files into *uast.Node.
101-
func ParserBuilder(opts driver.ParserOptions) (parser driver.Parser, err error) {
102-
parser, err = native.ExecParser(ToNoder, opts.NativeBin)
103-
if err != nil {
104-
return
105-
}
106-
107-
switch ToNoder.PositionFill {
108-
case native.OffsetFromLineCol:
109-
parser = &driver.TransformationParser{
110-
Parser: parser,
111-
Transformation: driver.FillOffsetFromLineCol,
112-
}
113-
case native.LineColFromOffset:
114-
parser = &driver.TransformationParser{
115-
Parser: parser,
116-
Transformation: driver.FillLineColFromOffset,
117-
}
118-
}
119-
120-
return
121-
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func TestNativeToNoder(t *testing.T) {
1212
f, err := getFixture("python_example_1.json")
1313
require.NoError(err)
1414

15-
n, err := ToNoder.ToNode(f)
15+
n, err := ToNode.ToNode(f)
1616
require.NoError(err)
1717
require.NotNil(n)
1818
}

0 commit comments

Comments
 (0)