Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle dropped fields in methods #31

Merged
merged 6 commits into from
Feb 19, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
155 changes: 144 additions & 11 deletions driver/normalizer/normalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ func (op opArrToChain) Construct(st *State, n nodes.Node) (nodes.Node, error) {

func funcDefMap(typ string, returns bool, other Obj) Mapping {
src := Obj{
"Body": Var("body"),
"Identifier": Var("name"),
"ParameterList": Obj{
uast.KeyType: String("ParameterList"),
Expand All @@ -183,11 +182,14 @@ func funcDefMap(typ string, returns bool, other Obj) Mapping {
"IsMissing": Bool(false),
"IsStructuredTrivia": Bool(false),
"SemicolonToken": Any(),
"AttributeLists": Cases("caseAttrs",
Arr(),
Check(OfKind(nodes.KindArray), Var("attr")),
Check(OfKind(nodes.KindObject), Var("attr")),
),

// FIXME(dennwc): driver drops them currently
"AttributeLists": Any(),
"ExpressionBody": Any(),
"Modifiers": Any(),
"Modifiers": Any(),
}
for k, v := range other {
src[k] = v
Expand All @@ -204,15 +206,71 @@ func funcDefMap(typ string, returns bool, other Obj) Mapping {
)
}
return MapSemantic(typ, uast.FunctionGroup{}, MapObj(
src,
// Either Body or ExpressionBody will be set.
CasesObj("isArrow",
src,
Objs{
// case 1: arrow expression
{
"Body": Is(nil),
"ExpressionBody": Obj{
uast.KeyType: String("ArrowExpressionClause"),
// will use this positions for Block in Body
uast.KeyPos: Var("arrow_pos"),
"ArrowToken": Obj{
uast.KeyType: String("EqualsGreaterThanToken"),
// will use this position for Return in Body
uast.KeyPos: Var("arrow_pos_tok"),
"IsMissing": Bool(false),
"Text": Any(),
"Value": Any(),
"ValueText": Any(),
},
"IsMissing": Bool(false),
"IsStructuredTrivia": Bool(false),
"Expression": Var("arrow"),
},
},
// case 2: full body
{
"ExpressionBody": Is(nil),
"Body": Var("body"),
},
},
),

Obj{
"Nodes": Arr(
Cases("caseAttrs",
Is(nil),
Var("attr"),
Arr(Var("attr")),
),
UASTType(uast.Alias{}, Obj{
"Name": Var("name"),
"Node": UASTType(uast.Function{}, Obj{
"Body": Var("body"),
"Type": UASTType(uast.FunctionType{}, dstType),
// If the function was defined with an arrow expression, we will
// generate a uast:Block with a since csharp:Return node containing
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I can understand this sentence generate a uast:Block with a ...? since csharp:Return node containing

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Fixed the typo in the latest commit.

// the expression.
"Body": Cases("isArrow",
// case 1: arrow expression
UASTType(uast.Block{}, Obj{
uast.KeyPos: Var("arrow_pos"),
"Statements": Arr(
Obj{
uast.KeyType: String("ReturnStatement"),
uast.KeyPos: Var("arrow_pos_tok"),
"Expression": Var("arrow"),
},
),
}),
// case 2: full body
// TODO(dennwc): this will definitely fail the reverse transform
// make a more specific node check when we need it
// see https://github.com/bblfsh/sdk/issues/355
Var("body"),
),
}),
}),
),
Expand Down Expand Up @@ -794,16 +852,91 @@ var Normalizers = []Mapping{
)),

funcDefMap("MethodDeclaration", true, Obj{
// number of parameters - safe to ignore
"Arity": Any(),
// FIXME(dennwc): driver drops them currently
"Arity": Any(),
"ConstraintClauses": Any(),
"ExplicitInterfaceSpecifier": Any(),
"TypeParameterList": Any(),
}),
funcDefMap("ConstructorDeclaration", false, Obj{
// FIXME(dennwc): driver drops them currently
"Initializer": Any(),
}),
// ConstructorDeclaration is similar to MethodDeclaration, but it may include a
// base class initializer that require a special transformation.
MapSemantic("ConstructorDeclaration", uast.FunctionGroup{}, MapObj(
Obj{
// Same as to MethodDeclaration above
"Identifier": Var("name"),
"ParameterList": Obj{
uast.KeyType: String("ParameterList"),
uast.KeyPos: Any(),
"OpenParenToken": Any(),
creachadair marked this conversation as resolved.
Show resolved Hide resolved
"CloseParenToken": Any(),
"IsMissing": Bool(false),
"IsStructuredTrivia": Bool(false),
"Parameters": Var("params"),
},
"IsMissing": Bool(false),
"IsStructuredTrivia": Bool(false),
"SemicolonToken": Any(),
"AttributeLists": Cases("caseAttrs",
Arr(),
Check(OfKind(nodes.KindArray), Var("attr")),
Check(OfKind(nodes.KindObject), Var("attr")),
),

// won't be set for a constructor
"ExpressionBody": Is(nil),

// Initializer is an expression to initialize the base class.
// Here we consider case if it's exists in the AST node.
//
// Initializer is basically a function call that will init the base class.
// So we will consider it a first statement of a function body.
//
// For this we dig into Body above to get the list of statements.
// If hasBaseInit is set (initializer exists), we will prepend it
// to the array of statement in Body.
"Initializer": If("hasBaseInit",
// case 1: has base initializer
Check(HasType("BaseConstructorInitializer"), Var("baseInit")),
// case 2: no initializer
Is(nil),
),
"Body": Part("bodyMap", Obj{
"Statements": Var("stmts"),
}),

// FIXME(dennwc): driver drops them currently
"Modifiers": Any(),
},
Obj{
"Nodes": Arr(
Cases("caseAttrs",
Is(nil),
Var("attr"),
Arr(Var("attr")),
),
UASTType(uast.Alias{}, Obj{
"Name": Var("name"),
"Node": UASTType(uast.Function{}, Obj{
// Restore the function body.
//
// We will also prepend a base class initializer here, see above.
"Body": Part("bodyMap", Obj{
"Statements": If("hasBaseInit",
// case 1: has base initializer
PrependOne(Var("baseInit"), Var("stmts")),
// case 2: no initializer
Var("stmts"),
),
}),
"Type": UASTType(uast.FunctionType{}, Obj{
"Arguments": Var("params"),
}),
}),
}),
),
},
)),
funcDefMap("DestructorDeclaration", false, Obj{
"TildeToken": Any(),
}),
Expand Down
6 changes: 6 additions & 0 deletions fixtures/Program.cs.sem.uast
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,7 @@
},
},
Nodes: [
~,
{ '@type': "uast:Alias",
Name: { '@type': "uast:Identifier",
'@pos': { '@type': "uast:Positions",
Expand Down Expand Up @@ -4569,6 +4570,7 @@
},
},
Nodes: [
~,
{ '@type': "uast:Alias",
Name: { '@type': "uast:Identifier",
'@pos': { '@type': "uast:Positions",
Expand Down Expand Up @@ -5679,6 +5681,7 @@
},
},
Nodes: [
~,
{ '@type': "uast:Alias",
Name: { '@type': "uast:Identifier",
'@pos': { '@type': "uast:Positions",
Expand Down Expand Up @@ -9070,6 +9073,7 @@
},
},
Nodes: [
~,
{ '@type': "uast:Alias",
Name: { '@type': "uast:Identifier",
'@pos': { '@type': "uast:Positions",
Expand Down Expand Up @@ -9300,6 +9304,7 @@
},
},
Nodes: [
~,
{ '@type': "uast:Alias",
Name: { '@type': "uast:Identifier",
'@pos': { '@type': "uast:Positions",
Expand Down Expand Up @@ -9497,6 +9502,7 @@
},
},
Nodes: [
~,
{ '@type': "uast:Alias",
Name: { '@type': "uast:Identifier",
'@pos': { '@type': "uast:Positions",
Expand Down
1 change: 1 addition & 0 deletions fixtures/_integration.cs.sem.uast
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@
},
},
Nodes: [
~,
{ '@type': "uast:Alias",
Name: { '@type': "uast:Identifier",
'@pos': { '@type': "uast:Positions",
Expand Down
2 changes: 2 additions & 0 deletions fixtures/accumulator_factory.cs.sem.uast
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
},
},
Nodes: [
~,
{ '@type': "uast:Alias",
Name: { '@type': "uast:Identifier",
'@pos': { '@type': "uast:Positions",
Expand Down Expand Up @@ -570,6 +571,7 @@
},
},
Nodes: [
~,
{ '@type': "uast:Alias",
Name: { '@type': "uast:Identifier",
'@pos': { '@type': "uast:Positions",
Expand Down
1 change: 1 addition & 0 deletions fixtures/anonymous_object.cs.sem.uast
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
},
},
Nodes: [
~,
{ '@type': "uast:Alias",
Name: { '@type': "uast:Identifier",
'@pos': { '@type': "uast:Positions",
Expand Down
1 change: 1 addition & 0 deletions fixtures/argument_passtype.cs.sem.uast
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
},
},
Nodes: [
~,
{ '@type': "uast:Alias",
Name: { '@type': "uast:Identifier",
'@pos': { '@type': "uast:Positions",
Expand Down
1 change: 1 addition & 0 deletions fixtures/arithmetic_ops.cs.sem.uast
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
},
},
Nodes: [
~,
{ '@type': "uast:Alias",
Name: { '@type': "uast:Identifier",
'@pos': { '@type': "uast:Positions",
Expand Down
1 change: 1 addition & 0 deletions fixtures/array_literal.cs.sem.uast
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
},
},
Nodes: [
~,
{ '@type': "uast:Alias",
Name: { '@type': "uast:Identifier",
'@pos': { '@type': "uast:Positions",
Expand Down
1 change: 1 addition & 0 deletions fixtures/array_stackalloc.cs.sem.uast
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@
},
},
Nodes: [
~,
{ '@type': "uast:Alias",
Name: { '@type': "uast:Identifier",
'@pos': { '@type': "uast:Positions",
Expand Down
2 changes: 2 additions & 0 deletions fixtures/as.cs.sem.uast
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
},
},
Nodes: [
~,
{ '@type': "uast:Alias",
Name: { '@type': "uast:Identifier",
'@pos': { '@type': "uast:Positions",
Expand Down Expand Up @@ -598,6 +599,7 @@
},
},
Nodes: [
~,
{ '@type': "uast:Alias",
Name: { '@type': "uast:Identifier",
'@pos': { '@type': "uast:Positions",
Expand Down
1 change: 1 addition & 0 deletions fixtures/assignment.cs.sem.uast
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
},
},
Nodes: [
~,
{ '@type': "uast:Alias",
Name: { '@type': "uast:Identifier",
'@pos': { '@type': "uast:Positions",
Expand Down
1 change: 1 addition & 0 deletions fixtures/async.cs.sem.uast
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
},
},
Nodes: [
~,
{ '@type': "uast:Alias",
Name: { '@type': "uast:Identifier",
'@pos': { '@type': "uast:Positions",
Expand Down
Loading