Skip to content

Commit 4ceffe5

Browse files
Denys Smirnovdennwc
authored andcommitted
switch to Span instead of FullSpan for positions; validate node tokens
Signed-off-by: Denys Smirnov <denys@sourced.tech>
1 parent 46f1524 commit 4ceffe5

241 files changed

Lines changed: 76138 additions & 56784 deletions

File tree

Some content is hidden

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

driver/fixtures/fixtures_test.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"gopkg.in/bblfsh/sdk.v2/driver"
99
"gopkg.in/bblfsh/sdk.v2/driver/fixtures"
1010
"gopkg.in/bblfsh/sdk.v2/driver/native"
11+
"gopkg.in/bblfsh/sdk.v2/uast/transformer/positioner"
1112
)
1213

1314
const projectRoot = "../../"
@@ -28,6 +29,7 @@ var Suite = &fixtures.Suite{
2829
"ConstructorDeclaration",
2930
"DestructorDeclaration",
3031
"FalseLiteralExpression",
32+
//"IdentifierName", // FIXME
3133
"IdentifierToken",
3234
"MethodDeclaration",
3335
"MultiLineCommentTrivia",
@@ -40,8 +42,18 @@ var Suite = &fixtures.Suite{
4042
"UsingDirective",
4143
},
4244
},
43-
Docker: fixtures.DockerConfig{
44-
//Image:"image:tag", // TODO: specify a docker image with language runtime
45+
VerifyTokens: []positioner.VerifyToken{
46+
{Types: []string{
47+
"IdentifierToken",
48+
"ClassKeyword",
49+
"FalseLiteralExpression",
50+
"MultiLineCommentTrivia",
51+
"QualifiedName",
52+
"SingleLineCommentTrivia",
53+
"SingleLineDocumentationCommentTrivia",
54+
"StringLiteralExpression",
55+
"TrueLiteralExpression",
56+
}},
4557
},
4658
}
4759

driver/normalizer/normalizer.go

Lines changed: 66 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ func funcDefMap(typ string) Mapping {
107107
))
108108
}
109109

110+
// useFullSpan is a set of node types that use FullSpan for positions instead of Span
111+
var useFullSpan = []nodes.Value{
112+
nodes.String("SingleLineDocumentationCommentTrivia"),
113+
}
114+
110115
// Preprocessors is a block of AST preprocessing rules rules.
111116
var Preprocessors = []Mapping{
112117
// Erase Whitespace and EndOfLine trivias.
@@ -168,7 +173,7 @@ var Preprocessors = []Mapping{
168173
Part("_", Obj{}),
169174
),
170175

171-
// Positional info is stored in a child node in FullSpan field.
176+
// Positional info is stored in a child node in Span field.
172177
//
173178
// This is not supported by ObjectToNode helper, and we are
174179
// too lazy to create positional node by hand.
@@ -177,22 +182,64 @@ var Preprocessors = []Mapping{
177182
// "spanStart" and "spanEnd" fields of the root node, and
178183
// ObjectToNode will pick them up later to build a proper
179184
// positional node.
185+
//
186+
// There is also a FullSpan field that includes leading/trailing
187+
// whitespaces and sometimes node tokens. We ignore this second
188+
// position for most nodes, but there are few exceptions where
189+
// we use FullSpan and ignore Span.
180190
Map(
181-
Part("_", Obj{
182-
"FullSpan": Obj{
183-
uast.KeyType: String("TextSpan"),
184-
"Length": Any(),
185-
"Start": Var("start"),
186-
"End": Var("end"),
191+
Part("_", CasesObj("case", Obj{}, Objs{
192+
// exceptions - use FullSpan
193+
{
194+
uast.KeyType: Check(
195+
In(useFullSpan...),
196+
Var("typ"),
197+
),
198+
"FullSpan": Obj{
199+
uast.KeyType: String("TextSpan"),
200+
"Length": Any(),
201+
"Start": Var("start"),
202+
"End": Var("end"),
203+
},
204+
// TODO(dennwc): add it as a custom position field?
205+
"Span": Any(),
187206
},
188-
// TODO(dennwc): add it as a custom position field?
189-
"Span": Any(),
190-
}),
191-
Part("_", Obj{
207+
// other nodes - use Span
208+
{
209+
uast.KeyType: Check(
210+
Not(In(useFullSpan...)),
211+
Var("typ"),
212+
),
213+
"Span": Obj{
214+
uast.KeyType: String("TextSpan"),
215+
"Length": Any(),
216+
"Start": Var("start"),
217+
"End": Var("end"),
218+
},
219+
// TODO(dennwc): add it as a custom position field?
220+
"FullSpan": Any(),
221+
},
222+
})),
223+
Part("_", CasesObj("case", Obj{
192224
// remap to temporary keys and let ObjectToNode to pick them up
193225
"spanStart": Var("start"),
194226
"spanEnd": Var("end"),
195-
}),
227+
}, Objs{
228+
// exceptions
229+
{
230+
uast.KeyType: Check(
231+
In(useFullSpan...),
232+
Var("typ"),
233+
),
234+
},
235+
// other nodes
236+
{
237+
uast.KeyType: Check(
238+
Not(In(useFullSpan...)),
239+
Var("typ"),
240+
),
241+
},
242+
})),
196243
),
197244

198245
// Use temporary fields from the previous transform to create positional node.
@@ -379,14 +426,13 @@ var Normalizers = []Mapping{
379426
CommentNode(false, "text", nil),
380427
)),
381428

382-
// FIXME: doesn't work
383-
//MapSemantic("MultiLineCommentTrivia", uast.Comment{}, MapObj(
384-
//Obj{
385-
//uast.KeyToken: CommentText([2]string{"/*", "*/"}, uast.KeyToken),
386-
//"IsDirective": Bool(false),
387-
//},
388-
//CommentNode(true, uast.KeyToken, nil),
389-
//)),
429+
MapSemantic("MultiLineCommentTrivia", uast.Comment{}, MapObj(
430+
Obj{
431+
uast.KeyToken: CommentText([2]string{"/*", "*/"}, uast.KeyToken),
432+
"IsDirective": Bool(false),
433+
},
434+
CommentNode(true, uast.KeyToken, nil),
435+
)),
390436

391437
// TODO(dennwc): differentiate from regular comments
392438
MapSemantic("SingleLineDocumentationCommentTrivia", uast.Comment{}, MapObj(

0 commit comments

Comments
 (0)