normalizer

package
v2.5.3+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 8, 2019 License: GPL-3.0 Imports: 8 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Annotations = []Mapping{

	annotateType("File", nil, role.File),

	annotateType("CommentGroup", nil, role.Comment, role.List),

	mapAST("Comment", MapObj(Obj{
		"Text": UncommentCLike("text"),
	}, Obj{
		uast.KeyToken: Var("text"),
	}), role.Comment),

	annotateType("BadExpr", nil, role.Incomplete),

	annotateType("Ident", FieldRoles{
		"Name": {Rename: uast.KeyToken},
	}, role.Identifier),

	annotateTypeCustom("BasicLit", MapObj(Obj{
		"Value": Var("val"),
		"Kind":  Var("kind"),
	}, Fields{
		{Name: "Kind", Op: Var("kind")},
		{Name: uast.KeyToken, Op: Var("val")},
	}), LookupArrOpVar("kind", literalRoles),
		role.Literal, role.Primitive),

	annotateTypeCustom("BinaryExpr", MapObj(Obj{
		"X":  ObjectRoles("left"),
		"Y":  ObjectRoles("right"),
		"Op": Var("op"),
	}, JoinObj(Fields{
		operator("Op", "op", opRolesBinary, role.Binary),
	}, Obj{
		"X": ObjectRoles("left", role.Binary, role.Left),
		"Y": ObjectRoles("right", role.Binary, role.Right),
	})), LookupArrOpVar("op", opRolesBinary), role.Binary),

	annotateTypeCustom("UnaryExpr", MapObj(Obj{
		"X":  Var("x"),
		"Op": Var("op"),
	}, Fields{
		operator("Op", "op", opRolesUnary, role.Unary),
		{Name: "X", Op: Var("x")},
	}), LookupArrOpVar("op", opRolesUnary), role.Unary),

	annotateTypeCustom("IncDecStmt", MapObj(Obj{
		"X":   Var("x"),
		"Tok": Var("op"),
	}, Fields{
		operator("Op", "op", opIncDec, role.Unary),
		{Name: "X", Op: Var("x")},
	}), LookupArrOpVar("op", opIncDec), role.Unary),

	annotateType("BlockStmt", nil, role.Block, role.Scope),

	annotateTypeCustom("AssignStmt", MapObj(Obj{
		"Lhs": EachObjectRoles("left"),
		"Rhs": EachObjectRoles("right"),
		"Tok": Var("op"),
	}, Fields{
		operator("Op", "op", opAssign, role.Operator, role.Assignment, role.Binary),
		{Name: "Lhs", Op: EachObjectRoles("left", role.Assignment, role.Binary, role.Left)},
		{Name: "Rhs", Op: EachObjectRoles("right", role.Assignment, role.Binary, role.Right)},
	}), LookupArrOpVar("op", opAssign),
		role.Assignment, role.Binary),

	annotateType("IfStmt", FieldRoles{
		"Init": {Opt: true, Roles: role.Roles{role.If, role.Initialization}},
		"Cond": {Roles: role.Roles{role.If, role.Condition}},
		"Body": {Roles: role.Roles{role.Then, role.Body}},
		"Else": {Opt: true, Roles: role.Roles{role.Else}},
	}, role.If),

	annotateType("SwitchStmt", FieldRoles{
		"Init": {Opt: true, Roles: role.Roles{role.Switch, role.Initialization}},
		"Body": {Roles: role.Roles{role.Switch, role.Body}},
	}, role.Switch),

	annotateType("TypeSwitchStmt", FieldRoles{
		"Init": {Opt: true, Roles: role.Roles{role.Switch, role.Initialization}},
		"Body": {Roles: role.Roles{role.Switch, role.Body}},
	}, role.Switch, role.Incomplete),

	annotateType("SelectStmt", ObjRoles{
		"Body": {role.Switch, role.Body},
	}, role.Switch, role.Incomplete),

	annotateType("ForStmt", FieldRoles{
		"Init": {Opt: true, Roles: role.Roles{role.For, role.Initialization}},
		"Cond": {Opt: true, Roles: role.Roles{role.For, role.Condition}},
		"Body": {Roles: role.Roles{role.For, role.Body}},
		"Post": {Opt: true, Roles: role.Roles{role.For, role.Update}},
	}, role.For),

	annotateType("RangeStmt", FieldRoles{
		"Key":   {Opt: true, Roles: role.Roles{role.For, role.Iterator, role.Key}},
		"Value": {Opt: true, Roles: role.Roles{role.For, role.Iterator, role.Value}},
		"X":     {},
		"Body":  {Roles: role.Roles{role.For, role.Body}},
	}, role.For, role.Iterator),

	annotateTypeCustom("BranchStmt", FieldRoles{
		"Tok":   {Op: Var("tok")},
		"Label": {},
	}, LookupArrOpVar("tok", branchRoles)),

	annotateType("ImportSpec", FieldRoles{
		"Name": {Opt: true, Roles: role.Roles{role.Import, role.Alias}},
		"Path": {Roles: role.Roles{role.Import, role.Pathname}},
	}, role.Import, role.Declaration),

	annotateType("ValueSpec", ObjRoles{
		"Type": {role.Type},
	}, role.Declaration),

	annotateType("TypeSpec", ObjRoles{
		"Type": {role.Type},
	}, role.Declaration),

	annotateType("ArrayType", ObjRoles{
		"Elt": {role.Entry},
	}, role.Type, role.List),

	annotateType("MapType", ObjRoles{
		"Key":   {role.Key},
		"Value": {role.Entry},
	}, role.Type, role.Map),

	annotateType("FuncLit", ObjRoles{
		"Type": {role.Type},
		"Body": {role.Body},
	}),

	annotateType("StructType", FieldRoles{
		"Fields": {Sub: FieldRoles{
			"List": {Arr: true, Sub: astField{
				Roles: role.Roles{role.Entry},
			}},
		}},
	}, role.Type),

	annotateType("InterfaceType", FieldRoles{
		"Methods": {Sub: FieldRoles{
			"List": {Arr: true,
				Sub: astField{Roles: role.Roles{role.Entry}},
			},
		}, Roles: role.Roles{role.Function, role.List}},
	}, role.Type, role.Incomplete),

	annotateType("FuncType", FieldRoles{
		"Params": {Sub: FieldRoles{
			"List": {Arr: true,
				Sub: astField{Roles: role.Roles{role.Argument}},
			},
		}, Roles: role.Roles{role.ArgsList}},
		"Results": {Opt: true, Sub: FieldRoles{
			"List": {Arr: true,
				Sub: astField{Roles: role.Roles{role.Return, role.Argument}},
			},
		}, Roles: role.Roles{role.Return, role.ArgsList}},
	}, role.Function, role.Type),

	annotateType("FuncDecl", FieldRoles{
		"Recv": {Opt: true, Sub: FieldRoles{
			"List": {Arr: true, Sub: astField{
				Inherit: true, Roles: role.Roles{role.Function, role.Receiver},
			}},
		}, Roles: role.Roles{role.Function, role.Receiver, role.List}},
		"Name": {Roles: role.Roles{role.Function, role.Name}},
		"Type": {Roles: role.Roles{role.Function, role.Type}},
		"Body": {Roles: role.Roles{role.Function, role.Body}},
	}, role.Function, role.Declaration),

	annotateType("GenDecl", FieldRoles{
		"Tok": {Op: isGoTok(token.VAR)},
		"Specs": {Arr: true, Sub: FieldRoles{
			"Names": {Arr: true, Roles: role.Roles{role.Variable, role.Name}},
		}},
	}, role.Variable, role.Declaration),

	annotateType("GenDecl", FieldRoles{
		"Tok": {Op: isGoTok(token.CONST)},
		"Specs": {Arr: true, Sub: FieldRoles{
			"Names": {Arr: true, Roles: role.Roles{role.Name}},
		}},
	}, role.Incomplete, role.Declaration),

	annotateType("GenDecl", FieldRoles{
		"Tok": {Op: isGoTok(token.TYPE)},
		"Specs": {Arr: true, Sub: FieldRoles{
			"Name": {Roles: role.Roles{role.Type, role.Name}},
		}},
	}, role.Type, role.Declaration),

	annotateType("GenDecl", FieldRoles{
		"Tok": {Op: isGoTok(token.IMPORT)},
	}, role.Declaration),

	annotateType("CallExpr", FieldRoles{
		"Fun":  {Roles: role.Roles{role.Callee}},
		"Args": {Arr: true, Roles: role.Roles{role.Argument, role.Positional}},
	}, role.Call),

	annotateType("KeyValueExpr", ObjRoles{
		"Key":   {role.Key},
		"Value": {role.Value},
	}),

	annotateType("CaseClause", nil, role.Case),
	annotateType("CommClause", nil, role.Case, role.Incomplete),

	annotateType("ReturnStmt", nil, role.Return),
	annotateType("GoStmt", nil, role.Incomplete),
	annotateType("SelectorExpr", nil, role.Incomplete),

	annotateType("CompositeLit", nil, role.Literal),
	annotateType("ChanType", nil, role.Type, role.Incomplete),

	annotateType("ExprStmt", nil),
	annotateType("DeclStmt", nil),
	annotateType("DeferStmt", nil, role.Incomplete),
	annotateType("SendStmt", nil, role.Incomplete),
	annotateType("LabeledStmt", nil),
	annotateType("Ellipsis", nil),
	annotateType("SliceExpr", nil),
	annotateType("IndexExpr", nil),
	annotateType("StarExpr", nil),
	annotateType("TypeAssertExpr", nil, role.Incomplete),
	annotateType("ParenExpr", nil, role.Incomplete),
}

Annotations is a list of individual transformations to annotate a native AST with roles.

View Source
var Code []CodeTransformer // deprecated
View Source
var Native = Transformers([][]Transformer{

	{Mappings(Annotations...)},
	{
		Mappings(
			AnnotateIfNoRoles("FieldList", role.Incomplete),
		),

		RolesDedup(),
	},
}...)

Native is the of list `transformer.Transformer` to apply to a native AST. To learn more about the Transformers and the available ones take a look to: https://godoc.org/gopkg.in/bblfsh/sdk.v2/uast/transformer

View Source
var Normalize = Transformers([][]Transformer{

	{Mappings(Normalizers...)},
}...)
View Source
var Normalizers = []Mapping{
	MapSemanticPos("Ident", uast.Identifier{},
		map[string]string{
			"NamePos": "start",
		},
		ObjMap{"Name": Var("name")},
	),

	MapSemanticPos("BasicLit", uast.String{},
		map[string]string{
			"ValuePos": "start",
		},
		MapObj(
			Obj{
				"Kind":  isGoTok(token.STRING),
				"Value": Quote(Var("val")),
			},
			Obj{
				"Value":  Var("val"),
				"Format": String(""),
			},
		),
	),

	MapSemanticPos("Comment", uast.Comment{},
		map[string]string{
			"Slash": "start",
		},
		MapObj(
			Obj{
				"Text": commentNorm{
					text: "text", block: "block",
					pref: "pref", suff: "suff", tab: "tab",
				},
			},
			Fields{
				{Name: "Block", Op: Var("block")},
				{Name: "Prefix", Op: Var("pref")},
				{Name: "Suffix", Op: Var("suff")},
				{Name: "Tab", Op: Var("tab")},
				{Name: "Text", Op: Var("text")},
			},
		),
	),

	MapSemanticPos("BlockStmt", uast.Block{},
		map[string]string{
			"Lbrace": "start",
			"Rbrace": "rbrace",
		},
		MapObj(
			Obj{
				"List": Var("stmts"),
			},
			Obj{
				"Statements": Var("stmts"),
			},
		),
	),

	MapPart("flist", ObjMap{
		uast.KeyType: String("FieldList"),
		"List": Map(
			fieldSplit{vr: "fields"},
			Var("fields"),
		),
	}),

	MapSemanticPos("ImportSpec", uast.Import{},
		map[string]string{
			"EndPos": "endp",
		},
		MapObj(
			Obj{
				"Comment": Is(nil),
				"Doc":     Is(nil),
				"Path":    Var("path"),
				"Name": Cases("case",

					Is(nil),

					UASTType(uast.Identifier{}, Obj{
						uast.KeyPos: AnyNode(nil),
						"Name":      String("_"),
					}),

					UASTType(uast.Identifier{}, Obj{
						uast.KeyPos: AnyNode(nil),
						"Name":      String("."),
					}),
				),
			},

			CasesObj("case",

				Obj{
					"Path": Var("path"),
				},
				Objs{

					{
						"All":    Bool(true),
						"Names":  Arr(),
						"Target": Is(nil),
					},

					{
						"All":    Bool(false),
						"Names":  Arr(),
						"Target": Is(nil),
					},

					{
						"All":    Bool(true),
						"Names":  Arr(),
						"Target": Obj{"Scope": String(".")},
					},
				},
			),
		),
	),

	MapSemanticPos("ImportSpec", uast.Import{},
		map[string]string{
			"EndPos": "endp",
		},

		MapObj(
			Obj{
				"Comment": Is(nil),
				"Doc":     Is(nil),
				"Name": UASTType(uast.Identifier{}, Obj{
					uast.KeyPos: Var("alias_pos"),
					"Name":      Var("alias"),
				}),
				"Path": UASTType(uast.String{}, Obj{
					uast.KeyPos: Var("path_pos"),
					"Value":     Var("path"),
					"Format":    String(""),
				}),
			},

			Obj{
				"Path": UASTType(uast.Alias{}, Obj{

					"Name": UASTType(uast.Identifier{}, Obj{
						uast.KeyPos: Var("alias_pos"),
						"Name":      Var("alias"),
					}),
					"Node": UASTType(uast.String{}, Obj{
						uast.KeyPos: Var("path_pos"),
						"Value":     Var("path"),
						"Format":    String(""),
					}),
				}),
				"All":   Bool(true),
				"Names": Arr(),
			},
		),
	),

	MapPart("func", ObjMap{
		uast.KeyType: String("FuncType"),
		"Params": Map(
			Obj{
				uast.KeyType: String("FieldList"),

				uast.KeyPos: AnyNode(nil),
				"List": Cases("list",
					Is(nil),
					Check(NotNil(), Var("args")),
				),
			},
			Cases("list",
				Arr(),
				Check(NotNil(), Var("args")),
			),
		),
		"Results": Map(
			Cases("res",
				Is(nil),
				Obj{
					uast.KeyType: String("FieldList"),

					uast.KeyPos: AnyNode(nil),
					"List":      Var("out"),
				},
			),
			Cases("res",
				Is(nil),
				Var("out"),
			),
		),
	}),
	MapPart("func", ObjMap{
		uast.KeyType: String("FuncType"),
		"Params":     MapEach("args", fieldMap),
		"Results":    MapEach("res", fieldMap),
	}),
	MapSemanticPos("FuncType", uast.FunctionType{},
		map[string]string{
			"Func": "pos_func",
		},
		MapObj(
			Obj{
				"Params":  Var("args"),
				"Results": Var("out"),
			},
			Obj{
				"Arguments": Var("args"),
				"Returns":   Var("out"),
			},
		),
	),
	MapPart("func", ObjMap{
		uast.KeyType: String("FuncDecl"),
		"Recv": Map(
			Cases("recv",
				Is(nil),
				Obj{
					uast.KeyType: String("FieldList"),

					uast.KeyPos: AnyNode(nil),
					"List":      Var("out"),
				},
			),
			Cases("recv",
				Is(nil),
				Var("out"),
			),
		),
	}),
	MapPart("func", ObjMap{
		uast.KeyType: String("FuncDecl"),
		"Recv":       MapEach("recv", fieldMap),
	}),
	MapSemantic("FuncDecl", uast.FunctionGroup{},
		MapObj(
			CasesObj("recv_case",

				Obj{
					"Name": Var("name"),
					"Body": Var("body"),
					"Doc":  Var("doc"),
				},
				Objs{

					{
						"Type": Var("type"),
						"Recv": Is(nil),
					},

					{
						"Type": UASTTypePart("type", uast.FunctionType{}, Obj{
							"Arguments": Var("args"),
						}),
						"Recv": One(
							UASTTypePart("recv", uast.Argument{}, Obj{
								"Receiver": Bool(false),
							}),
						),
					},
				},
			),

			Obj{
				"Nodes": Arr(
					Var("doc"),
					UASTType(uast.Alias{}, Obj{

						"Name": Var("name"),
						"Node": UASTType(uast.Function{}, CasesObj("recv_case",

							Obj{
								"Body": Var("body"),
							},
							Objs{

								{
									"Type": Var("type"),
								},

								{
									"Type": UASTTypePart("type", uast.FunctionType{}, Obj{
										"Arguments": PrependOne(
											UASTTypePart("recv", uast.Argument{}, Obj{
												"Receiver": Bool(true),
											}),
											Var("args"),
										),
									}),
								},
							},
						)),
					}),
				),
			},
		),
	),
}
View Source
var Preprocess = Transformers([][]Transformer{
	{Mappings(
		MapPart("_", MapObj(
			Obj{
				uast.KeyType: String("File"),
				"Unresolved": AnyNode(nil),
			},
			Obj{
				uast.KeyType: String("File"),
			},
		)),
	)},
}...)
View Source
var PreprocessCode []CodeTransformer

PreprocessCode is a special block of transformations that are applied at the end and can access original source code file. It can be used to improve or fix positional information.

https://godoc.org/gopkg.in/bblfsh/sdk.v2/uast/transformer/positioner

View Source
var Transforms = driver.Transforms{
	Namespace:      "go",
	Preprocess:     Preprocess,
	PreprocessCode: PreprocessCode,
	Normalize:      Normalize,
	Annotations:    Native,
	Code:           Code,
}

Functions

func TokenToRolesMap

func TokenToRolesMap(m map[token.Token][]role.Role) map[nodes.Value]ArrayOp

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL