normalizer

package
v2.1.1+incompatible Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2018 License: GPL-3.0 Imports: 5 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Annotations = []Mapping{}/* 114 elements not displayed */

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

View Source
var Code = []CodeTransformer{
	positioner.NewFillLineColFromOffset(),
}

Code 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 Native = Transformers([][]Transformer{

	{Mappings(Annotations...)},
	{

		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{
	MapSemantic("Identifier", uast.Identifier{}, MapObj(
		Obj{
			"name": Var("name"),
		},
		Obj{
			"Name": Var("name"),
		},
	)),
	MapSemantic("StringLiteral", uast.String{}, MapObj(
		Obj{
			"value": Var("val"),
		},
		Obj{
			"Value": Var("val"),
		},
	)),
	MapSemantic("CommentLine", uast.Comment{}, MapObj(
		Obj{
			"value": CommentText([2]string{"", ""}, "comm"),
		},
		CommentNode(false, "comm", nil),
	)),
	MapSemantic("CommentBlock", uast.Comment{}, MapObj(
		Obj{
			"value": CommentText([2]string{"", ""}, "comm"),
		},
		CommentNode(true, "comm", nil),
	)),
	MapSemantic("BlockStatement", uast.Block{}, MapObj(
		Obj{
			"body":       Var("stmts"),
			"directives": Arr(),
		},
		Obj{
			"Statements": Var("stmts"),
		},
	)),
	MapSemantic("ImportDeclaration", uast.Import{}, MapObj(
		Obj{
			"source":     Var("path"),
			"specifiers": Arr(),
		},
		Obj{
			"Path": Var("path"),
		},
	)),
	MapSemantic("ImportDeclaration", uast.Import{}, MapObj(
		CasesObj("case",

			Obj{
				"importKind": String("value"),
				"source":     Var("path"),
			},
			Objs{

				{
					"specifiers": ArrWith(Var("names"), Obj{
						uast.KeyType: String("ImportNamespaceSpecifier"),
						uast.KeyPos:  Var("local_pos"),
						"local":      Var("local"),
					}),
				},

				{
					"specifiers": Check(Not(Arr()), Var("names")),
				},
			},
		),
		CasesObj("case", nil,
			Objs{

				{
					"Path": UASTType(uast.Alias{}, Obj{
						uast.KeyPos: Var("local_pos"),
						"Name":      Var("local"),
						"Node":      Var("path"),
					}),
					"Names": Var("names"),
					"All":   Bool(true),
				},

				{
					"Path":  Var("path"),
					"Names": Var("names"),
					"All":   Bool(false),
				},
			},
		),
	)),
	MapSemantic("ImportSpecifier", uast.Alias{}, MapObj(
		Obj{
			"importKind": Is(nil),
			"local":      Var("local"),
			"imported":   Var("imp"),
		},
		Obj{
			"Name": Var("local"),
			"Node": Var("imp"),
		},
	)),
	MapSemantic("ImportDefaultSpecifier", uast.Alias{}, MapObj(
		Obj{
			"local": Var("local"),
		},
		Obj{
			"Name": Var("local"),
			"Node": UASTType(uast.Identifier{}, Obj{
				uast.KeyPos: AnyNode(nil),
				"Name":      String("."),
			}),
		},
	)),
	MapSemantic("FunctionDeclaration", uast.FunctionGroup{}, MapObj(
		Obj{
			"id":        Var("name"),
			"generator": Var("gen"),
			"async":     Var("async"),
			"body":      Var("body"),
			"params": Each("params", Cases("param_case",

				Check(
					HasType(uast.Identifier{}),
					Var("arg_name"),
				),

				Obj{
					uast.KeyType: String("AssignmentPattern"),
					uast.KeyPos:  Var("arg_pos"),
					"left":       Var("arg_name"),
					"right":      Var("arg_init"),
				},

				Obj{
					uast.KeyType: String("RestElement"),
					uast.KeyPos:  Var("arg_pos"),
					"argument":   Var("arg_name"),
				},
			)),
		},
		Obj{
			"Nodes": Arr(
				Obj{
					"async":     Var("async"),
					"generator": Var("gen"),
				},
				UASTType(uast.Alias{}, Obj{
					"Name": Var("name"),
					"Node": UASTType(uast.Function{}, Obj{
						"Type": UASTType(uast.FunctionType{}, Obj{
							"Arguments": Each("params", Cases("param_case",

								UASTType(uast.Argument{}, Obj{
									"Name": Var("arg_name"),
								}),

								UASTType(uast.Argument{}, Obj{
									uast.KeyPos: Var("arg_pos"),
									"Name":      Var("arg_name"),
									"Init":      Var("arg_init"),
								}),

								UASTType(uast.Argument{}, Obj{
									uast.KeyPos: Var("arg_pos"),
									"Name":      Var("arg_name"),
									"Variadic":  Bool(true),
								}),
							)),
							"Returns": Arr(
								UASTType(uast.Argument{}, Obj{
									"Init": Is(uast.Identifier{
										Name: "undefined",
									}),
								}),
							),
						}),
						"Body": Var("body"),
					}),
				}),
			),
		},
	)),
}

Normalizers is the main block of normalization rules to convert native AST to semantic UAST.

View Source
var Preprocess = Transformers([][]Transformer{
	{Mappings(Preprocessors...)},
}...)
View Source
var Preprocessors = []Mapping{

	ObjectToNode{
		InternalTypeKey: "type",
		OffsetKey:       "start",
		EndOffsetKey:    "end",
	}.Mapping(),

	Map(
		Part("_", Obj{"loc": AnyNode(nil)}),
		Part("_", Obj{}),
	),
	Map(
		Part("_", Obj{"extra": AnyNode(nil)}),
		Part("_", Obj{}),
	),
}

Preprocessors is a block of AST preprocessing rules rules.

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

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

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