java-driver: github.com/bblfsh/java-driver/driver/normalizer Index | Files

package normalizer

import "github.com/bblfsh/java-driver/driver/normalizer"

Index

Package Files

annotation.go normalizer.go transforms.go

Variables

var Annotations = []Mapping{
    AnnotateType("CompilationUnit", nil, role.File),

    AnnotateType("QualifiedName", nil, role.Expression, role.Identifier, role.Qualified),
    AnnotateType("SimpleName",
        FieldRoles{
            "identifier": {Rename: uast.KeyToken},
        },
        role.Expression, role.Identifier,
    ),
    AnnotateTypeCustom("PrimitiveType",
        FieldRoles{
            "primitiveTypeCode": {Rename: uast.KeyToken, Op: Var("typ")},
        },
        LookupArrOpVar("typ", primitiveRoles),
        role.Type, role.Primitive,
    ),

    AnnotateType("TypeDeclaration",
        ObjMap{"modifiers": Is(nil)},
        role.Visibility, role.Package,
    ),
    AnnotateType("MethodDeclaration",
        ObjMap{"modifiers": Is(nil)},
        role.Visibility, role.Package,
    ),

    annotateModifiers("TypeDeclaration", "", role.Visibility, role.Package),
    annotateModifiers("TypeDeclaration", "public", role.Visibility, role.World),
    annotateModifiers("TypeDeclaration", "private", role.Visibility, role.Type),
    annotateModifiers("TypeDeclaration", "protected", role.Visibility, role.Subtype),

    annotateModifiers("MethodDeclaration", "", role.Visibility, role.Package),
    annotateModifiers("MethodDeclaration", "public", role.Visibility, role.World),
    annotateModifiers("MethodDeclaration", "private", role.Visibility, role.Type),
    annotateModifiers("MethodDeclaration", "protected", role.Visibility, role.Subtype),

    AnnotateType("ImportDeclaration", nil, role.Declaration, role.Import),
    AnnotateType("ImportDeclaration", FieldRoles{
        "name": {Sub: ObjMap{
            uast.KeyType: String("QualifiedName"),
        }, Roles: role.Roles{role.Pathname, role.Import}},
    }),
    AnnotateType("ImportDeclaration", FieldRoles{
        "qualifier": {Sub: ObjMap{
            uast.KeyType: String("QualifiedName"),
        }, Roles: role.Roles{role.Pathname, role.Import}},
    }),

    AnnotateType("PackageDeclaration",
        FieldRoles{
            uast.KeyToken: {Add: true, Op: String("package")},
        },
        role.Declaration, role.Package,
    ),

    AnnotateType("AnonymousClassDeclaration",
        FieldRoles{
            "bodyDeclarations": {Arr: true, Roles: role.Roles{role.Body}},
        },
        role.Expression, role.Declaration, role.Type, role.Anonymous,
    ),

    AnnotateType("AnnotationTypeDeclaration",
        FieldRoles{
            "bodyDeclarations": {Arr: true, Roles: role.Roles{role.Body}},
        },
        role.Declaration, role.Type, role.Annotation,
    ),
    AnnotateType("EnumDeclaration", nil, role.Declaration, role.Type, role.Enumeration),

    AnnotateType("TypeDeclaration", nil, role.Declaration, role.Type),

    AnnotateType("TypeDeclarationStatement", nil, role.Statement, role.Declaration, role.Type),

    AnnotateType("MethodDeclaration", MapObj(Fields{
        {Name: "name", Op: ObjectRoles("name")},
        {Name: "body", Op: OptObjectRoles("body")},
        {Name: "parameters", Op: Each("param", ObjectRolesCustom("p", Obj{
            "name":    ObjectRoles("pname"),
            "varargs": Var("variadic"),
        }))},
    }, Fields{
        {Name: "name", Op: ObjectRoles("name", role.Function, role.Name)},
        {Name: "body", Op: OptObjectRoles("body", role.Function, role.Body)},
        {Name: "parameters", Op: Each("param", ObjectRolesCustomOp("p", Obj{
            "name":    ObjectRoles("pname", role.Function, role.Name, role.Argument),
            "varargs": Var("variadic"),
        }, LookupArrOpVar("variadic", variadicRoles), role.Function, role.Argument))},
    }), role.Declaration, role.Function),

    AnnotateType("LambdaExpression", FieldRoles{
        "body": {Roles: role.Roles{role.Function, role.Body}},
        "parameters": {Arr: true, Sub: FieldRoles{
            "name": {Roles: role.Roles{role.Function, role.Name, role.Argument}},
        }, Roles: role.Roles{role.Function, role.Argument}},
    }, role.Declaration, role.Function, role.Anonymous),

    AnnotateType("TypeMethodReference",
        FieldRoles{
            "name":          {Roles: role.Roles{role.Function, role.Name}},
            "type":          {Roles: role.Roles{role.Function, role.Return}},
            "typeArguments": {Arr: true, Roles: role.Roles{role.Function, role.Argument}},
        },
        role.Declaration, role.Function,
    ),

    AnnotateType("AnnotationTypeMemberDeclaration", nil, role.Declaration, role.Type, role.Annotation),
    AnnotateType("EnumConstantDeclaration", nil, role.Declaration, role.Enumeration),
    AnnotateType("FieldDeclaration", nil, role.Declaration, role.Variable),

    AnnotateType("Initializer", nil, role.Initialization, role.Block, role.Incomplete),
    AnnotateType("SingleVariableDeclaration", nil, role.Declaration, role.Variable),
    AnnotateType("VariableDeclarationExpression", nil, role.Expression, role.Declaration, role.Variable),
    AnnotateType("VariableDeclarationFragment", nil, role.Declaration, role.Variable),
    AnnotateType("VariableDeclarationStatement", nil, role.Statement, role.Declaration, role.Variable),

    AnnotateType("TypeLiteral", nil, role.Expression, role.Literal, role.Type),
    AnnotateType("BooleanLiteral",
        FieldRoles{
            "booleanValue": {Rename: uast.KeyToken},
        },
        role.Expression, role.Literal, role.Boolean,
    ),
    AnnotateType("NumberLiteral",
        FieldRoles{
            "token": {Rename: uast.KeyToken},
        },
        role.Expression, role.Literal, role.Number,
    ),

    AnnotateType("NullLiteral",
        FieldRoles{
            uast.KeyToken: {Add: true, Op: String("null")},
        },
        role.Expression, role.Literal, role.Null,
    ),
    AnnotateType("StringLiteral",
        FieldRoles{
            "escapedValue": {Rename: uast.KeyToken},
        },
        role.Expression, role.Literal, role.String,
    ),
    AnnotateType("CharacterLiteral",
        FieldRoles{
            "escapedValue": {Rename: uast.KeyToken},
        },
        role.Expression, role.Literal, role.Character,
    ),

    AnnotateType("ClassInstanceCreation",
        FieldRoles{
            "type":      {Roles: role.Roles{role.Call, role.Callee}},
            "arguments": {Arr: true, Roles: role.Roles{role.Call, role.Argument, role.Positional}},
        },
        role.Expression, role.Call, role.Instance,
    ),
    AnnotateType("ConstructorInvocation",
        FieldRoles{

            "arguments": {Arr: true, Roles: role.Roles{role.Call, role.Argument, role.Positional}},
        },
        role.Statement, role.Call, role.Incomplete,
    ),
    AnnotateType("MethodInvocation",
        FieldRoles{
            "expression": {Opt: true, Roles: role.Roles{role.Call, role.Receiver}},
            "name":       {Opt: true, Roles: role.Roles{role.Call, role.Callee}},
            "arguments":  {Arr: true, Roles: role.Roles{role.Call, role.Argument, role.Positional}},
        },
        role.Expression, role.Call,
    ),
    AnnotateType("SuperConstructorInvocation",
        ObjRoles{
            "expression": {role.Call, role.Receiver},
            "arguments":  {role.Call, role.Argument, role.Positional},
        },
        role.Statement, role.Call, role.Base, role.Incomplete,
    ),
    AnnotateType("SuperMethodInvocation",
        FieldRoles{
            "qualifier": {Opt: true, Roles: role.Roles{role.Call, role.Callee}},
            "name":      {Opt: true, Roles: role.Roles{role.Call, role.Callee}},
            "arguments": {Arr: true, Roles: role.Roles{role.Call, role.Argument, role.Positional}},
        },
        role.Expression, role.Call, role.Base,
    ),

    AnnotateType("IfStatement",
        FieldRoles{
            uast.KeyToken:   {Add: true, Op: String("if")},
            "expression":    {Roles: role.Roles{role.If, role.Condition}},
            "thenStatement": {Roles: role.Roles{role.If, role.Then, role.Body}},
            "elseStatement": {Opt: true, Roles: role.Roles{role.If, role.Else, role.Body}},
        },
        role.Statement, role.If,
    ),
    AnnotateType("ConditionalExpression",
        ObjRoles{
            "expression":     {role.If, role.Condition},
            "thenExpression": {role.If, role.Then},
            "elseExpression": {role.If, role.Else},
        },
        role.Expression, role.If,
    ),

    AnnotateType("SwitchStatement", MapObj(Obj{
        "expression": ObjectRoles("expr"),
        "statements": Var("stmts"),
    }, Obj{
        "expression": ObjectRoles("expr", role.Switch, role.Expression),
        "statements": opSwitchStmtGroup{vr: "stmts"},
    }), role.Statement, role.Switch),

    AnnotateType("EnhancedForStatement",
        ObjRoles{
            "parameter":  {role.For, role.Iterator},
            "expression": {role.Expression, role.For},
            "body":       {role.For, role.Body},
        },
        role.Statement, role.For, role.Iterator,
    ),
    AnnotateType("ForStatement",
        FieldRoles{
            "initializers": {Arr: true, Roles: role.Roles{role.For, role.Initialization}},
            "expression":   {Roles: role.Roles{role.Expression, role.For, role.Condition}},
            "updaters":     {Arr: true, Roles: role.Roles{role.For, role.Update}},
            "body":         {Roles: role.Roles{role.For, role.Body}},
        },
        role.Statement, role.For,
    ),
    AnnotateType("WhileStatement",
        ObjRoles{
            "expression": {role.Expression, role.While, role.Condition},
            "body":       {role.While, role.Body},
        },
        role.Statement, role.While,
    ),
    AnnotateType("DoStatement",
        ObjRoles{
            "expression": {role.DoWhile, role.Condition},
            "body":       {role.DoWhile, role.Body},
        },
        role.Statement, role.DoWhile,
    ),

    AnnotateTypeCustom("InfixExpression",
        MapObj(Obj{
            "leftOperand":  ObjectRoles("left"),
            "rightOperand": ObjectRoles("right"),
            "operator":     Var("op"),
        }, Fields{
            {Name: "operator", Op: Operator("op", infixRoles, role.Binary)},
            {Name: "leftOperand", Op: ObjectRoles("left", role.Expression, role.Binary, role.Left)},
            {Name: "rightOperand", Op: ObjectRoles("right", role.Expression, role.Binary, role.Right)},
        }),
        LookupArrOpVar("op", infixRoles), role.Expression, role.Binary, role.Operator,
    ),

    AnnotateTypeCustom("PostfixExpression",
        MapObj(Obj{
            "operator": Var("op"),
        }, Fields{
            {Name: "operator", Op: Operator("op", postfixRoles, role.Unary, role.Postfix)},
        }),
        LookupArrOpVar("op", postfixRoles), role.Expression, role.Operator, role.Unary, role.Postfix,
    ),

    AnnotateTypeCustom("PrefixExpression",
        MapObj(Obj{
            "operator": Var("op"),
        }, Fields{
            {Name: "operator", Op: Operator("op", prefixRoles, role.Unary)},
        }),
        LookupArrOpVar("op", prefixRoles), role.Expression, role.Operator, role.Unary,
    ),

    AnnotateTypeCustom("Assignment",
        MapObj(Obj{
            "leftHandSide":  ObjectRoles("left"),
            "rightHandSide": ObjectRoles("right"),
            "operator":      Var("op"),
        }, Fields{
            {Name: "operator", Op: Operator("op", assignRoles, role.Assignment, role.Binary)},
            {Name: "leftHandSide", Op: ObjectRoles("left", role.Assignment, role.Binary, role.Left)},
            {Name: "rightHandSide", Op: ObjectRoles("right", role.Assignment, role.Binary, role.Right)},
        }),
        LookupArrOpVar("op", assignRoles), role.Expression, role.Assignment, role.Operator, role.Binary,
    ),

    AnnotateType("ArrayType", nil, role.Type, role.Primitive, role.List),
    AnnotateType("IntersectionType", nil, role.Type, role.And),
    AnnotateType("NameQualifiedType", nil, role.Type, role.Name, role.Qualified),
    AnnotateType("ParameterizedType", nil, role.Type, role.Incomplete),
    AnnotateType("QualifiedType", nil, role.Type, role.Qualified),
    AnnotateType("SimpleType", nil, role.Type),
    AnnotateType("UnionType", nil, role.Type, role.Or),
    AnnotateType("WildcardType", nil, role.Type, role.Incomplete),

    AnnotateTypeCustom("Modifier",
        FieldRoles{
            "keyword": {Rename: uast.KeyToken, Op: Var("mod")},
        },
        LookupArrOpVar("mod", modifierRoles),
    ),

    AnnotateType("TryStatement",
        FieldRoles{
            "resources":    {Opt: true, Roles: role.Roles{role.Try}},
            "body":         {Roles: role.Roles{role.Try, role.Body}},
            "catchClauses": {Arr: true, Roles: role.Roles{role.Try, role.Catch}},
            "finally":      {Opt: true, Roles: role.Roles{role.Try, role.Finally}},
        },
        role.Statement, role.Try,
    ),
    AnnotateType("ThrowStatement", nil, role.Statement, role.Throw),
    AnnotateType("AssertStatement", nil, role.Statement, role.Assert),

    AnnotateType("MarkerAnnotation", nil, role.Annotation, role.Incomplete),
    AnnotateType("NormalAnnotation", nil, role.Annotation, role.Incomplete),
    AnnotateType("SingleMemberAnnotation", nil, role.Annotation, role.Incomplete),
    AnnotateType("MemberValuePair", nil, role.Annotation, role.Incomplete),

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

    AnnotateType("Javadoc", MapObj(Fields{
        {Name: "text", Op: UncommentCLike("text"), Optional: "txt"},
    }, Fields{
        {Name: uast.KeyToken, Op: Var("text"), Optional: "txt"},
    }), role.Documentation, role.Comment),

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

    AnnotateType("MemberRef", nil, role.Documentation, role.Variable, role.Incomplete),
    AnnotateType("MethodRef", nil, role.Documentation, role.Function, role.Incomplete),
    AnnotateType("MethodRefParameter", nil, role.Documentation, role.Function, role.Incomplete),
    AnnotateType("TagElement", nil, role.Comment, role.Documentation, role.Incomplete),
    AnnotateType("TextElement", nil, role.Comment, role.Documentation, role.Incomplete),

    AnnotateType("ArrayAccess", nil, role.Expression, role.Incomplete),
    AnnotateType("ArrayCreation", nil, role.Expression, role.Incomplete),
    AnnotateType("CastExpression", nil, role.Expression, role.Incomplete),
    AnnotateType("CreationReference", nil, role.Expression, role.Incomplete),
    AnnotateType("ExpressionMethodReference", nil, role.Expression, role.Incomplete),
    AnnotateType("ParenthesizedExpression", nil, role.Expression, role.Incomplete),
    AnnotateType("SuperMethodReference", nil, role.Expression, role.Incomplete),
    AnnotateType("ThisExpression", nil, role.Expression, role.This),

    AnnotateType("Block", nil, role.Statement, role.Block, role.Scope),
    AnnotateType("BreakStatement", nil, role.Statement, role.Break),
    AnnotateType("EmptyStatement", nil, role.Statement),
    AnnotateType("ExpressionStatement", nil, role.Statement),
    AnnotateType("LabeledStatement", nil, role.Statement, role.Incomplete),
    AnnotateType("ReturnStatement", nil, role.Statement, role.Return),
    AnnotateType("SynchronizedStatement", nil, role.Statement, role.Incomplete),

    AnnotateType("ArrayInitializer", nil, role.Expression, role.List, role.Literal),
    AnnotateType("Dimension", nil, role.Type, role.Incomplete),
    AnnotateType("TypeParameter", nil, role.Type, role.Incomplete),
}

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

var Native = Transformers([][]Transformer{
    {

        ResponseMetadata{
            TopLevelIsRootNode: false,
        },
    },

    {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/github.com/bblfsh/sdk/v3/uast/transformer

var Normalize = Transformers([][]Transformer{

    {Mappings(Normalizers...)},
}...)
var Normalizers = []Mapping{
    MapSemantic("StringLiteral", uast.String{}, MapObj(
        Fields{
            {Name: "unescapedValue", Op: Var("val")},
            {Name: "escapedValue", Drop: true, Op: Any()},
        },
        Obj{
            "Value":  Var("val"),
            "Format": String(""),
        },
    )),
    MapSemantic("SimpleName", uast.Identifier{}, MapObj(
        Obj{
            "identifier": Var("name"),
        },
        Obj{
            "Name": Var("name"),
        },
    )),
    MapSemantic("QualifiedName", uast.QualifiedIdentifier{}, MapObj(
        CasesObj("case",

            Obj{"name": Var("name")},
            Objs{

                {
                    "qualifier": Check(Has{
                        uast.KeyType: String(uast.TypeOf(uast.Identifier{})),
                    }, Var("par")),
                },

                {
                    "qualifier": UASTType(uast.QualifiedIdentifier{}, Obj{

                        uast.KeyPos: AnyNode(nil),
                        "Names":     Var("names"),
                    }),
                },
            },
        ),
        CasesObj("case", nil,
            Objs{

                {
                    "Names": Arr(Var("par"), Var("name")),
                },

                {
                    "Names": Append(Var("names"), Arr(Var("name"))),
                },
            },
        ),
    )),
    MapSemantic("BlockComment", uast.Comment{}, MapObj(
        Obj{
            "text": CommentText([2]string{"/*", "*/"}, "comm"),
        },
        CommentNode(true, "comm", nil),
    )),
    MapSemantic("LineComment", uast.Comment{}, MapObj(
        Obj{
            "text": CommentText([2]string{"//", ""}, "comm"),
        },
        CommentNode(false, "comm", nil),
    )),
    MapSemantic("Block", uast.Block{}, MapObj(
        Obj{
            "statements": Var("stmts"),
        },
        Obj{
            "Statements": Var("stmts"),
        },
    )),
    MapSemantic("ImportDeclaration", uast.Import{}, MapObj(
        CasesObj("case",

            Obj{
                "static": Var("static"),
            },
            Objs{

                {
                    "name":     Var("name"),
                    "onDemand": String("true"),
                },

                {
                    "name": Part("path", UASTType(uast.QualifiedIdentifier{}, Obj{
                        "Names": Append(Var("names"), Arr(Var("name"))),
                    })),
                    "onDemand": String("false"),
                },
            },
        ),
        CasesObj("case",

            Obj{

                "Target": Obj{"static": Var("static")},
            },
            Objs{

                {
                    "Path":  Var("name"),
                    "All":   Bool(true),
                    "Names": Arr(),
                },
                {
                    "Path": Part("path", UASTType(uast.QualifiedIdentifier{}, Obj{
                        "Names": Var("names"),
                    })),
                    "All":   Bool(false),
                    "Names": Arr(Var("name")),
                },
            },
        ),
    )),

    MapSemantic("MethodDeclaration", uast.FunctionGroup{}, MapObj(
        Obj{
            "constructor":      Var("constr"),
            "extraDimensions2": Is(nil),
            "javadoc":          Var("doc"),
            "modifiers":        Var("ann"),
            "name":             Var("name"),
            "body":             Var("body"),
            "parameters": Each("args", Fields{
                {Name: uast.KeyType, Op: String("SingleVariableDeclaration")},
                {Name: uast.KeyPos, Op: Var("apos")},
                {Name: "extraDimensions2", Op: Is(nil)},
                {Name: "initializer", Op: Var("ainit")},
                {Name: "modifiers", Op: Any(), Drop: true},
                {Name: "name", Op: Var("aname")},
                {Name: "type", Op: Var("atype")},
                {Name: "varargs", Op: Cases("varg", String("false"), String("true"))},
                {Name: "varargsAnnotations", Op: Is(nil)},
            }),
            "receiverQualifier": Var("recv_name"),
            "receiverType": Cases("recv",
                Is(nil),
                Check(NotNil(), Var("recv_type")),
            ),
            "returnType2": Cases("out_case",

                Is(nil),

                Obj{
                    uast.KeyType:        String("PrimitiveType"),
                    uast.KeyPos:         AnyNode(nil),
                    "annotations":       Is(nil),
                    "primitiveTypeCode": String("void"),
                },

                Check(
                    Not(And(
                        Is(nil),
                        Has{
                            uast.KeyType:        String("PrimitiveType"),
                            "annotations":       Is(nil),
                            "primitiveTypeCode": String("void"),
                        },
                    )),
                    Var("out1"),
                ),
            ),
            "thrownExceptionTypes": Var("exc"),
            "typeParameters":       Var("tmpl"),
        },
        Obj{
            "Nodes": Arr(
                Var("doc"),
                Var("ann"),
                UASTType(uast.Alias{}, Obj{

                    "Name": Var("name"),
                    "Node": UASTType(uast.Function{}, Obj{
                        "Type": UASTType(uast.FunctionType{}, Obj{
                            "Arguments": Cases("recv",

                                argsPart,

                                PrependOne(
                                    UASTType(uast.Argument{}, Obj{
                                        "Name":     Var("recv_name"),
                                        "Type":     Var("recv_type"),
                                        "Receiver": Bool(true),
                                    }),
                                    argsPart,
                                ),
                            ),
                            "Returns": Cases("out_case",

                                Is(nil),

                                Is(nil),

                                Arr(
                                    UASTType(uast.Argument{}, Obj{
                                        "Type": Var("out1"),
                                    }),
                                ),
                            ),
                        }),
                        "Body": Var("body"),
                    }),
                }),
                Obj{
                    "constructor":          Var("constr"),
                    "thrownExceptionTypes": Var("exc"),
                    "typeParameters":       Var("tmpl"),
                },
            ),
        },
    )),

    MapSemantic("Javadoc", uast.Comment{}, MapObj(
        Obj{
            "text": CommentText([2]string{"/**", "*/"}, "comm"),
        },
        CommentNode(true, "comm", nil),
    )),

    MapSemantic("Javadoc", uast.Group{}, MapObj(
        Obj{
            "tags": Var("parts"),
        },
        Obj{
            "Nodes": Var("parts"),
        },
    )),
    MapSemantic("TagElement", uast.Group{}, MapObj(
        Obj{
            "fragments": Var("parts"),
            "tagName":   Is(nil),
        },
        Obj{
            "Nodes": Var("parts"),
        },
    )),
    MapSemantic("TextElement", uast.Comment{}, MapObj(
        Obj{
            "text": CommentText([2]string{"", ""}, "comm"),
        },
        CommentNode(false, "comm", nil),
    )),
}
var Preprocess []Transformer
var PreprocessCode []CodeTransformer

PreprocessCode is a preprocessor stage that can use the source code to fix tokens and positional information. Java already provides all the information we need.

var Transforms = driver.Transforms{
    Namespace:      "java",
    Preprocess:     Preprocess,
    PreprocessCode: PreprocessCode,
    Normalize:      Normalize,
    Annotations:    Native,
}

Package normalizer imports 5 packages (graph) and is imported by 1 packages. Updated 2019-05-24. Refresh now. Tools for package owners.