normalizer

package
v2.7.4+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: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Annotations = []Mapping{}/* 160 elements not displayed */
View Source
var Code = []CodeTransformer{
	positioner.FromOffset(),
}
View Source
var Native = Transformers([][]Transformer{
	{Mappings(Annotations...)},
	{RolesDedup()},
}...)
View Source
var Normalize = Transformers([][]Transformer{
	{Mappings(PreNormilizers...)},
	{Mappings(Normalizers...)},
}...)
View Source
var Normalizers = []Mapping{
	MapSemantic("Name", uast.Identifier{}, MapObj(
		Fields{
			{Name: uast.KeyToken, Op: Var("name")},
			{Name: "comments", Drop: true, Op: Any()},
		},
		Obj{
			"Name": Var("name"),
		},
	)),
	MapSemantic("Name", uast.Identifier{}, MapObj(
		Fields{
			{Name: "parts", Op: One(Var("name"))},
			{Name: "comments", Drop: true, Op: Any()},
		},
		Obj{
			"Name": Var("name"),
		},
	)),
	MapSemantic("Name", uast.QualifiedIdentifier{}, MapObj(
		Fields{
			{Name: "parts", Op: Each("names", Var("name"))},
			{Name: "comments", Drop: true, Op: Any()},
		},
		Obj{
			"Names": Each("names", UASTType(uast.Identifier{}, Obj{
				"Name": Var("name"),
			})),
		},
	)),

	MapSemantic("Scalar_String", uast.String{}, MapObj(
		Obj{
			"value": Var("val"),
			"attributes": Obj{"kind": Cases("kind",
				Int(1),
				Int(2),
			)},
		},
		Obj{
			"Value": Var("val"),
			"Format": Cases("kind",
				String("raw"),
				String(""),
			),
		},
	)),

	MapSemantic("Scalar_String", uast.String{}, MapObj(
		Obj{
			"value": Var("val"),
			"attributes": Obj{
				"kind":     Int(3),
				"docLabel": AnyVal(nil),
			},
		},
		Obj{
			"Value":  Var("val"),
			"Format": String("raw_custom"),
		},
	)),
	MapSemantic("Scalar_String", uast.String{}, MapObj(
		Obj{
			"value": Var("val"),
		},
		Obj{
			"Value":  Var("val"),
			"Format": String(""),
		},
	)),
	MapSemantic("Scalar_EncapsedStringPart", uast.String{}, MapObj(
		Obj{
			"value": Var("val"),
		},
		Obj{
			"Value":  Var("val"),
			"Format": String("encapsed"),
		},
	)),
	MapSemantic("Comment", uast.Comment{}, MapObj(
		Obj{
			"text": CommentText([2]string{"//", "\n"}, "text"),
		},
		CommentNode(false, "text", nil),
	)),
	MapSemantic("Comment", uast.Comment{}, MapObj(
		Obj{
			"text": CommentText([2]string{"#", "\n"}, "text"),
		},
		CommentNode(false, "text", nil),
	)),
	MapSemantic("Comment", uast.Comment{}, MapObj(
		Obj{
			"text": CommentText([2]string{"/*", "*/"}, "text"),
		},
		CommentNode(true, "text", nil),
	)),
	MapSemantic("Comment_Doc", uast.Comment{}, MapObj(
		Obj{
			"text": CommentText([2]string{"/**", "*/"}, "text"),
		},
		CommentNode(true, "text", nil),
	)),

	convertBlock("Stmt_If", ""),
	convertBlock("Stmt_ElseIf", ""),
	convertBlock("Stmt_Else", ""),
	convertBlock("Stmt_For", ""),
	convertBlock("Stmt_Foreach", ""),
	convertBlock("Stmt_While", ""),
	convertBlock("Stmt_Class", ""),
	convertBlock("Stmt_Do", ""),
	convertBlock("Stmt_ClassMethod", ""),
	convertBlock("Expr_Closure", ""),
	convertBlock("Stmt_Namespace", ""),
	convertBlock("Stmt_Interface", ""),
	convertBlock("Stmt_Catch", ""),
	convertBlock("Stmt_Finally", ""),
	convertBlock("Stmt_TryCatch", ""),
	convertBlock("Stmt_Case", ""),
	convertBlock("Stmt_Switch", ""),
	convertBlock("Stmt_Trait", ""),
	convertBlock("Stmt_Declare", "body_stmts"),

	MapSemantic("Expr_Include", uast.RuntimeReImport{}, MapObj(
		Obj{
			"expr": Var("path"),
			"type": Cases("typ",
				Int(1),
				Int(3),
			),
		},
		Obj{
			"Path": Var("path"),
			"All": Cases("typ",
				Bool(true),
				Bool(true),
			),
		},
	)),

	MapSemantic("Expr_Include", uast.RuntimeImport{}, MapObj(
		Obj{
			"expr": Var("path"),
			"type": Cases("typ",
				Int(2),
				Int(4),
			),
		},
		Obj{
			"Path": Var("path"),
			"All": Cases("typ",
				Bool(true),
				Bool(true),
			),
		},
	)),

	MapSemantic("Stmt_GroupUse", uast.RuntimeImport{}, MapObj(
		Obj{
			"prefix": Var("path"),
			"type":   Int(0),
			"uses": Each("names", Obj{
				uast.KeyType: String("Stmt_UseUse"),
				uast.KeyPos:  Var("name_pos"),
				"type":       Int(1),
				"alias":      Var("alias"),
				"name":       Var("name"),
			}),
		},
		Obj{
			"Path": Var("path"),
			"All":  Bool(false),
			"Names": Each("names", UASTType(uast.Alias{}, Obj{
				uast.KeyPos: Var("name_pos"),
				"Name": UASTType(uast.Identifier{}, Obj{
					"Name": Var("alias"),
				}),
				"Node": Var("name"),
			})),
		},
	)),
	MapSemantic("Stmt_Use", uast.RuntimeImport{}, MapObj(
		Obj{
			"type": Cases("typ",

				Int(1),
				Int(2),
				Int(3),
			),
			"uses": One(Obj{
				uast.KeyType: String("Stmt_UseUse"),
				uast.KeyPos:  Var("name_pos"),
				"type":       Int(0),
				"alias":      Var("alias"),
				"name": Cases("name_case",
					Check(HasType(uast.Identifier{}), Var("name")),
					UASTTypePart("path", uast.QualifiedIdentifier{}, Obj{
						"Names": Append(Var("path_pref"), One(Var("name"))),
					}),
				),
			}),
		},
		CasesObj("name_case",

			Obj{
				"All": Cases("typ",
					Bool(false),
					Bool(false),
					Bool(false),
				),
				"Names": One(UASTType(uast.Alias{}, Obj{
					uast.KeyPos: Var("name_pos"),
					"Name": UASTType(uast.Identifier{}, Obj{
						"Name": Var("alias"),
					}),
					"Node": Var("name"),
				})),
			},
			Objs{
				{
					"Path": Is(nil),
				},
				{
					"Path": UASTTypePart("path", uast.QualifiedIdentifier{}, Obj{
						"Names": Var("path_pref"),
					}),
				},
			},
		),
	)),

	MapSemantic("Param", uast.Argument{}, MapObj(
		Obj{
			"byRef": Cases("by_ref",
				Bool(false),
				Bool(true),
			),
			"default":  Var("init"),
			"name":     Var("name"),
			"type":     typeCaseLeft("typ"),
			"variadic": Var("variadic"),
		},
		Obj{
			"Name": Var("name"),
			"Type": Cases("by_ref",
				typeCaseRight("typ"),
				Obj{
					uast.KeyType: String("ByRef"),
					"Type":       typeCaseRight("typ"),
				},
			),
			"Init":     Var("init"),
			"Variadic": Var("variadic"),
		},
	)),
	MapSemantic("Stmt_Function", uast.FunctionGroup{}, MapObj(
		Fields{
			{Name: "byRef", Op: Cases("by_ref",
				Bool(false),
				Bool(true),
			)},
			{Name: "name", Op: Var("name")},
			{Name: "params", Op: Var("params")},
			{Name: "returnType", Op: typeCaseLeft("return")},
			{Name: "stmts", Op: Var("body")},
			{Name: "comments", Drop: true, Op: Any()},
		},
		Obj{
			"Nodes": Arr(
				UASTType(uast.Alias{}, Obj{
					"Name": Var("name"),
					"Node": UASTType(uast.Function{}, Obj{
						"Type": UASTType(uast.FunctionType{}, Obj{
							"Arguments": Var("params"),
							"Returns": One(UASTType(uast.Argument{}, Obj{
								"Type": Cases("by_ref",

									typeCaseRight("return"),

									Obj{
										uast.KeyType: String("ByRef"),
										"Type":       typeCaseRight("return"),
									},
								),
							})),
						}),
						"Body": UASTType(uast.Block{}, Obj{
							"Statements": Var("body"),
						}),
					}),
				}),
			),
		},
	)),
}

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

View Source
var PreNormilizers = []Mapping{
	Map(
		splitUse{vr: "uses"},
		VarKind("uses", nodes.KindArray),
	),
}
View Source
var Preprocess = Transformers([][]Transformer{
	{Mappings(Preprocessors...)},
}...)
View Source
var PreprocessCode = []CodeTransformer{}
View Source
var Preprocessors = []Mapping{
	MapPart("root", ObjMap{
		"name": Map(
			VarKind("name", nodes.KindString),

			Obj{
				uast.KeyType:  String("Name"),
				uast.KeyToken: Var("name"),
				uast.KeyPos:   UASTType(uast.Positions{}, Obj{}),
			},
		),
	}),

	MapPart("root", MapObj(
		Obj{
			"attributes": Part("attrs", Fields{

				{Name: "startLine", Op: AnyVal(nil)},
				{Name: "endLine", Op: AnyVal(nil)},
				{Name: "startTokenPos", Op: AnyVal(nil)},
				{Name: "endTokenPos", Op: AnyVal(nil)},
				{Name: "startFilePos", Op: Var("sfile")},
				{Name: "endFilePos", Op: Var("efile")},
				{Name: "comments", Op: Var("comments"), Optional: "comments_exists"},
			}),
		},

		Fields{
			{Name: uast.KeyPos, Op: UASTType(uast.Positions{}, Obj{
				uast.KeyStart: UASTType(uast.Position{}, Obj{

					uast.KeyPosOff: Var("sfile"),
				}),
				uast.KeyEnd: UASTType(uast.Position{}, Obj{

					uast.KeyPosOff: opAdd{op: Var("efile"), n: +1},
				}),
			})},
			{Name: "attributes", Op: Var("attrs")},
			{Name: "comments", Op: Var("comments"), Optional: "comments_exists"},
		},
	)),

	MapPart("root", MapObj(Obj{
		"filePos": Var("fp"),
		"line":    AnyVal(nil),
	}, Obj{
		uast.KeyPos: UASTType(uast.Positions{}, Obj{
			uast.KeyStart: UASTType(uast.Position{}, Obj{
				uast.KeyPosOff: Var("fp"),
			}),
		}),
	})),

	Map(
		Part("_", Obj{
			"attributes": EmptyObj(),
		}),
		Part("_", Obj{}),
	),

	ObjectToNode{
		InternalTypeKey: "nodeType",
	}.Mapping(),
}

Preprocessors is a block of AST preprocessing rules rules.

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

Functions

This section is empty.

Types

This section is empty.

Directories

Path Synopsis
Package phpast defines constants for PHP-Parser lib nodes
Package phpast defines constants for PHP-Parser lib nodes

Jump to

Keyboard shortcuts

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