normalizer

package
v2.5.2+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: 5 Imported by: 0

README

Problems

Tokens are the original full text, not just the symbol

For code like $A the bash parser returns a leaf like this:

{
  "children": [],
  "elementType": "[Bash] variable",
  "startOffset": 0,
  "text": "$A",
  "textLength": 2
}

The text is "$A", not "A" as you would expect from parsers of other languages like Java.

Tokens of parents hold the text of their children

For code like $A the bash parser returns a tree like this:

{
  "children": [
  {
    "children": [
    {
      "children": [
      {
        "children": [
        {
          "children": [],
          "elementType": "[Bash] variable",
          "startOffset": 0,
          "text": "$A",
          "textLength": 2
        }
        ],
        "elementType": "var-use-element",
        "startOffset": 0,
        "text": "$A",
        "textLength": 2
      }
      ],
      "elementType": "[Bash] combined word",
      "startOffset": 0,
      "text": "$A",
      "textLength": 2
    }
    ],
    "elementType": "[Bash] generic bash command",
    "startOffset": 0,
    "text": "$A",
    "textLength": 2
  }
  ],
  "elementType": "simple-command",
  "startOffset": 0,
  "text": "$A",
  "textLength": 2
},

Note how every node contains the text of their children.

This is particularly painfull in the case of the "FILE" node that contains the full sourced text of the file being analysed.

We need a predicate to match then nth child

In bash, the IfCondition, IfBody and IfElse are identified as the 3, 8, and 11 children of the a node idenfied as "if shellcommand", for instance:

if /bin/false; then /bin/true; fi

Has the following native:

"if shellcommand"
|__ "[Bash] if"
|__ "WHITE_SPACE"
|__ "simple-command  <-- this is the if condition, but you cannot
     |                   know that from its element type, you need its position
     |                   in the array.
     |__ ...
     |__ ...
     |__ ...
|__ "[Bash] ;"
|__ "WHITE_SPACE"
|__ "[Bash] then"
|__ "WHITE_SPACE"
|__ "logical block"  <-- this is the if body, but you cannot know that from
     |                   its element type, you need its position in the array.
     |__ ...
     |__ ...
     |__ ...
|__ "[Bash] ;"
|__ "WHITE_SPACE"
|__ "[Bash] fi"

Ideally I would like to say, On("if shellcommand"), the 3rd child is the
condition and the 8th thing is the body.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Annotations = []Mapping{}/* 134 elements not displayed */
View Source
var Code = []CodeTransformer{
	positioner.NewFillLineColFromOffset(),
}
View Source
var Native = Transformers([][]Transformer{
	{Mappings(Annotations...)},
	{RolesDedup()},
}...)
View Source
var Normalize = Transformers([][]Transformer{
	{Mappings(Normalizers...)},
}...)
View Source
var Normalizers = []Mapping{

	MapSemantic("function-def-element", uast.FunctionGroup{}, MapObj(
		Obj{
			"children": Arr(
				Obj{
					uast.KeyType: Var("_type_namedsymbol"),
					uast.KeyPos:  Var("_pos_namedsymbol"),
					"children": Arr(
						Obj{
							uast.KeyType: Var("_type_identifier"),
							uast.KeyPos:  Var("_pos_identifier"),
							"Name":       Var("name"),
						},
					),
				},
				Obj{
					uast.KeyType: Var("_type_groupelem"),
					uast.KeyPos:  Var("_pos_groupelem"),
					"children":   Var("body"),
				},
			),
		},
		Obj{
			"Nodes": Arr(
				UASTType(uast.Alias{}, Obj{
					"Name": UASTType(uast.Identifier{}, Obj{
						"Name": Var("name"),
					}),
					"Node": UASTType(uast.Function{}, Obj{
						"Type": UASTType(uast.FunctionType{}, Obj{}),
						"Body": UASTType(uast.Block{}, Obj{
							"Statements": Var("body"),
						}),
					}),
				}),
			),
		},
	)),

	mapString("string_content"),
	mapString("string"),

	Map(
		Obj{
			uast.KeyType:  String("string"),
			uast.KeyToken: Any(),
			uast.KeyPos:   Var("pos"),
			"children": One(
				Part("inner", Obj{
					uast.KeyType: String(uast.TypeOf(uast.String{})),
					uast.KeyPos:  Any(),
				}),
			),
		},

		Part("inner", Obj{
			uast.KeyType: String(uast.TypeOf(uast.String{})),
			uast.KeyPos:  Var("pos"),
		}),
	),

	mapString("unevaluated_string2"),
	mapString("File_reference"),

	mapIdentifier("word"),
	mapIdentifier("variable"),
	mapIdentifier("assignment_word"),

	MapSemantic("Comment", uast.Comment{}, MapObj(
		Obj{
			uast.KeyToken: CommentText([2]string{"#", ""}, "comm"),
			"children":    Arr(),
		},
		CommentNode(false, "comm", nil),
	)),

	MapSemantic("file_reference", uast.RuntimeImport{}, MapObj(
		Obj{
			uast.KeyToken: Var("file"),
			"children":    Arr(),
		},
		Obj{
			"Path": Var("file"),
		},
	)),
}
View Source
var Preprocess = Transformers([][]Transformer{
	{
		ResponseMetadata{
			TopLevelIsRootNode: false,
		},
	},
	{Mappings(Preprocessors...)},
}...)
View Source
var PreprocessCode = []CodeTransformer{}
View Source
var Preprocessors = []Mapping{
	ObjectToNode{
		OffsetKey:    "startOffset",
		EndOffsetKey: "endOffset",
	}.Mapping(),
}

Preprocessors is a block of AST preprocessing rules rules.

View Source
var Transforms = driver.Transforms{
	Namespace:      "bash",
	Preprocess:     Preprocess,
	PreprocessCode: PreprocessCode,
	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