normalizer

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 31, 2017 License: GPL-3.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AnnotationRules = On(Any).Self(
	On(Not(HasInternalType(pyast.Module))).Error(errors.New("root must be Module")),
	On(HasInternalType(pyast.Module)).Roles(File).Descendants(

		On(HasInternalType(pyast.BinOp)).Roles(BinaryExpression).Children(
			On(HasInternalRole("op")).Roles(BinaryExpressionOp),
			On(HasInternalRole("left")).Roles(BinaryExpressionLeft),
			On(HasInternalRole("right")).Roles(BinaryExpressionRight),
		),

		On(HasInternalType(pyast.Eq)).Roles(OpEqual),
		On(HasInternalType(pyast.NotEq)).Roles(OpNotEqual),
		On(HasInternalType(pyast.Lt)).Roles(OpLessThan),
		On(HasInternalType(pyast.LtE)).Roles(OpLessThanEqual),
		On(HasInternalType(pyast.Gt)).Roles(OpGreaterThan),
		On(HasInternalType(pyast.GtE)).Roles(OpGreaterThanEqual),
		On(HasInternalType(pyast.Is)).Roles(OpSame),
		On(HasInternalType(pyast.IsNot)).Roles(OpNotSame),
		On(HasInternalType(pyast.In)).Roles(OpContains),
		On(HasInternalType(pyast.NotIn)).Roles(OpNotContains),

		On(HasInternalType(pyast.Add)).Roles(OpAdd),
		On(HasInternalType(pyast.Sub)).Roles(OpSubstract),
		On(HasInternalType(pyast.Mult)).Roles(OpMultiply),
		On(HasInternalType(pyast.Div)).Roles(OpDivide),
		On(HasInternalType(pyast.Mod)).Roles(OpMod),

		On(HasInternalType(pyast.LShift)).Roles(OpBitwiseLeftShift),
		On(HasInternalType(pyast.RShift)).Roles(OpBitwiseRightShift),
		On(HasInternalType(pyast.BitOr)).Roles(OpBitwiseOr),
		On(HasInternalType(pyast.BitXor)).Roles(OpBitwiseXor),
		On(HasInternalType(pyast.BitAnd)).Roles(OpBitwiseAnd),

		On(HasInternalType(pyast.And)).Roles(OpBooleanAnd),
		On(HasInternalType(pyast.Or)).Roles(OpBooleanOr),
		On(HasInternalType(pyast.Not)).Roles(OpBooleanNot),

		On(HasInternalType(pyast.UnaryOp)).Roles(Expression),

		On(HasInternalType(pyast.Invert)).Roles(OpBitwiseComplement),
		On(HasInternalType(pyast.UAdd)).Roles(OpPositive),
		On(HasInternalType(pyast.USub)).Roles(OpNegative),

		On(HasInternalType(pyast.StringLiteral)).Roles(StringLiteral),
		On(HasInternalType(pyast.ByteLiteral)).Roles(ByteStringLiteral),
		On(HasInternalType(pyast.NumLiteral)).Roles(NumberLiteral),
		On(HasInternalType(pyast.Str)).Roles(StringLiteral),
		On(HasInternalType(pyast.BoolLiteral)).Roles(BooleanLiteral),
		On(HasInternalType(pyast.JoinedStr)).Roles(StringLiteral).Children(

			On(HasInternalType(pyast.FormattedValue)).Roles(Expression),
		),
		On(HasInternalType(pyast.NoneLiteral)).Roles(NullLiteral),
		On(HasInternalType(pyast.Set)).Roles(SetLiteral),
		On(HasInternalType(pyast.List)).Roles(ListLiteral),
		On(HasInternalType(pyast.Dict)).Roles(MapLiteral).Children(
			On(HasInternalRole("keys")).Roles(MapKey),
			On(HasInternalRole("values")).Roles(MapValue),
		),
		On(HasInternalType(pyast.Tuple)).Roles(TupleLiteral),

		On(HasInternalType(pyast.FunctionDef)).Roles(FunctionDeclaration, FunctionDeclarationName).Children(
			On(HasInternalType("FunctionDef.body")).Roles(FunctionDeclarationBody),

			On(HasInternalType("arguments")).Roles(FunctionDeclarationArgument).Children(
				On(HasInternalRole("args")).Roles(FunctionDeclarationArgument, FunctionDeclarationArgumentName),
				On(HasInternalRole("vararg")).Roles(FunctionDeclarationArgument, FunctionDeclarationVarArgsList,
					FunctionDeclarationArgumentName),

				On(HasInternalRole("kwarg")).Roles(FunctionDeclarationArgument, FunctionDeclarationVarArgsList,
					FunctionDeclarationArgumentName),

				On(HasInternalType("arguments.defaults")).Roles(FunctionDeclarationArgumentDefaultValue),
			),
		),

		On(HasInternalType(pyast.Call)).Roles(Call).Children(
			On(HasInternalRole("args")).Roles(CallPositionalArgument),
			On(HasInternalRole("keywords")).Roles(CallNamedArgument).Children(
				On(HasInternalRole("value")).Roles(CallNamedArgumentValue),
			),
			On(HasInternalRole("func")).Self(On(HasInternalRole("id"))).Roles(CallCallee),
			On(HasInternalRole("func")).Self(On(HasInternalRole("attr"))).Roles(CallCallee),
			On(HasInternalRole("func")).Self(On(HasInternalType(pyast.Attribute))).Children(
				On(HasInternalRole("id")).Roles(CallReceiver),
			),
		),

		On(HasInternalType(pyast.Assign)).Roles(Assignment).Children(
			On(HasInternalRole("targets")).Roles(AssignmentVariable),
			On(HasInternalRole("value")).Roles(AssignmentValue),
		),

		On(HasInternalType(pyast.AugAssign)).Roles(AugmentedAssignment).Children(
			On(HasInternalRole("op")).Roles(AugmentedAssignmentOperator),
			On(HasInternalRole("target")).Roles(AugmentedAssignmentVariable),
			On(HasInternalRole("value")).Roles(AugmentedAssignmentValue),
		),

		On(HasInternalType(pyast.Expression)).Roles(Expression),
		On(HasInternalType(pyast.Expr)).Roles(Expression),
		On(HasInternalType(pyast.Name)).Roles(SimpleIdentifier),
		On(HasInternalType(pyast.Attribute)).Roles(QualifiedIdentifier),

		On(HasInternalType(pyast.SameLineNoops)).Roles(Comment),
		On(HasInternalType(pyast.PreviousNoops)).Roles(Whitespace).Children(
			On(HasInternalRole("lines")).Roles(Comment),
		),
		On(HasInternalType(pyast.RemainderNoops)).Roles(Whitespace).Children(
			On(HasInternalRole("lines")).Roles(Comment),
		),

		On(HasInternalType(pyast.Constant)).Roles(SimpleIdentifier),
		On(HasInternalType(pyast.Try)).Roles(Try).Children(
			On(HasInternalRole("body")).Roles(TryBody),
			On(HasInternalRole("finalbody")).Roles(TryFinally),
			On(HasInternalRole("handlers")).Roles(TryCatch),
			On(HasInternalRole("orelse")).Roles(IfElse),
		),
		On(HasInternalType(pyast.TryExcept)).Roles(TryCatch),
		On(HasInternalType(pyast.ExceptHandler)).Roles(TryCatch),
		On(HasInternalType(pyast.TryFinally)).Roles(TryFinally),
		On(HasInternalType(pyast.Raise)).Roles(Throw),

		On(HasInternalType(pyast.With)).Roles(BlockScope),
		On(HasInternalType(pyast.Return)).Roles(Return),
		On(HasInternalType(pyast.Break)).Roles(Break),
		On(HasInternalType(pyast.Continue)).Roles(Continue),

		On(HasInternalType(pyast.If)).Roles(If).Children(
			On(HasInternalType("If.body")).Roles(IfBody),
			On(HasInternalType("If.orelse")).Roles(IfElse),
			On(HasInternalType(pyast.Compare)).Roles(IfCondition, BinaryExpression).Children(
				On(HasInternalType("Compare.ops")).Roles(BinaryExpressionOp),
				On(HasInternalType("Compare.comparators")).Roles(BinaryExpressionRight),
				On(HasInternalRole("left")).Roles(BinaryExpressionLeft),
			),
		),
		On(HasInternalType(pyast.IfExp)).Roles(If, Expression).Children(

			On(HasInternalRole("body")).Roles(IfBody),
			On(HasInternalRole("test")).Roles(IfCondition),
			On(HasInternalRole("orelse")).Roles(IfElse),
		),

		On(HasInternalType(pyast.IfExp)).Roles(If),
		On(HasInternalType(pyast.Import)).Roles(ImportDeclaration),
		On(HasInternalType(pyast.ImportFrom)).Roles(ImportDeclaration),
		On(HasInternalType(pyast.Alias)).Roles(ImportAlias),
		On(HasInternalType(pyast.ClassDef)).Roles(TypeDeclaration).Children(
			On(HasInternalType("ClassDef.body")).Roles(TypeDeclarationBody),
			On(HasInternalType("ClassDef.bases")).Roles(TypeDeclarationBases),
		),

		On(HasInternalType(pyast.For)).Roles(ForEach).Children(
			On(HasInternalType("For.body")).Roles(ForBody),
			On(HasInternalRole("iter")).Roles(ForExpression),
			On(HasInternalRole("target")).Roles(ForUpdate),
			On(HasInternalType("For.orelse")).Roles(IfElse),
		),
		On(HasInternalType(pyast.While)).Roles(While).Children(
			On(HasInternalType("While.body")).Roles(WhileBody),
			On(HasInternalRole("test")).Roles(WhileCondition),
			On(HasInternalType("While.orelse")).Roles(IfElse),
		),
		On(HasInternalType(pyast.Pass)).Roles(Noop),
		On(HasInternalType(pyast.Num)).Roles(NumberLiteral),

		On(HasInternalType(pyast.AnnAssign)).Roles(Assignment),
		On(HasInternalType(pyast.Assert)).Roles(Assert),

		On(HasInternalType(pyast.Exec)).Roles(Call).Children(
			On(HasInternalRole("body")).Roles(CallPositionalArgument),
			On(HasInternalRole("globals")).Roles(CallPositionalArgument),
			On(HasInternalRole("locals")).Roles(CallPositionalArgument),
		),

		On(HasInternalType(pyast.Print)).Roles(Call, CallCallee).Children(
			On(HasInternalRole("dest")).Roles(CallPositionalArgument),
			On(HasInternalRole("nl")).Roles(CallPositionalArgument),
			On(HasInternalRole("values")).Roles(CallPositionalArgument).Children(
				On(Any).Roles(CallPositionalArgument),
			),
		),

		On(HasInternalRole("annotation")).Roles(Comment),
		On(HasInternalRole("returns")).Roles(Comment),

		On(HasInternalType(pyast.Ellipsis)).Roles(SimpleIdentifier),

		On(HasInternalType(pyast.Comprehension)).Roles(ForEach).Children(
			On(HasInternalRole("iter")).Roles(ForUpdate),
			On(HasInternalRole("target")).Roles(ForExpression),

			On(HasInternalType(pyast.Compare)).Roles(IfCondition, BinaryExpression).Children(
				On(HasInternalType("Compare.ops")).Roles(BinaryExpressionOp),
				On(HasInternalType("Compare.comparators")).Roles(BinaryExpressionRight),
				On(HasInternalRole("left")).Roles(BinaryExpressionLeft),
			),
		),
	),
)
View Source
var ToNoder = &native.ObjectToNoder{
	InternalTypeKey: "ast_type",
	LineKey:         "lineno",
	ColumnKey:       "col_offset",

	TokenKeys: map[string]bool{
		"module": true,
		"name":   true,

		"id":           true,
		"attr":         true,
		"arg":          true,
		"LiteralValue": true,
		"noop_line":    true,
	},
	SyntheticTokens: map[string]string{
		"Print":     "print",
		"Ellipsis":  "PythonEllipsisOperator",
		"Add":       "+",
		"Sub":       "-",
		"Mult":      "*",
		"Div":       "/",
		"FloorDiv":  "//",
		"Mod":       "%%",
		"Pow":       "**",
		"AugAssign": "?=",
		"BitAnd":    "&",
		"BitOr":     "|",
		"BitXor":    "^",
		"LShift":    "<<",
		"RShift":    ">>",
		"Eq":        "==",
		"NotEq":     "!=",
		"Not":       "!",
		"Lt":        "<",
		"LtE":       "<=",
		"Gt":        ">",
		"GtE":       ">=",
		"Is":        "is",
		"IsNot":     "not is",
		"In":        "in",
		"NotIn":     "not in",
		"UAdd":      "+",
		"USub":      "-",
		"Invert":    "~",
	},
	PromoteAllPropertyLists: false,
	PromotedPropertyLists: map[string]map[string]bool{
		"If":    {"body": true, "orelse": true},
		"For":   {"body": true, "orelse": true},
		"While": {"body": true, "orelse": true},

		"Compare":    {"comparators": true, "ops": true},
		"Import":     {"names": true},
		"ImportFrom": {"names": true},

		"With":                    {"body": true, "items": true},
		"FunctionDef":             {"body": true, "decorator_list": true},
		"arguments":               {"defaults": true},
		"Try":                     {"body": true, "orelse": true, "finalbody": true, "handlers": true},
		"Raise":                   {"args": true},
		"ClassDef":                {"body": true, "bases": true, "decorator_list": true, "keywords": true},
		"ListComp":                {"generators": true},
		"ListComp.generators":     {"ifs": true},
		"ListComp.generators.ifs": {"comparators": true, "ops": true},
	},
}

Functions

func UASTParserBuilder

func UASTParserBuilder(opts driver.UASTParserOptions) (driver.UASTParser, error)

ASTParserBuilder creates a parser that transform source code files into *uast.Node.

Types

This section is empty.

Directories

Path Synopsis
Package pyast defines constants from Python 2 and 3 AST.
Package pyast defines constants from Python 2 and 3 AST.

Jump to

Keyboard shortcuts

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