uast

package
v2.10.0 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2018 License: GPL-3.0 Imports: 7 Imported by: 43

Documentation

Overview

Package uast defines a UAST (Universal Abstract Syntax Tree) representation and operations to manipulate them.

Index

Constants

View Source
const (
	KeyType  = "@type"  // InternalType
	KeyToken = "@token" // Token
	KeyRoles = "@role"  // Roles, for representations see RoleList
	KeyPos   = "@pos"   // All positional information is stored in this field
)

Special field keys for nodes.Object

View Source
const (
	// NS is a namespace for the UAST types.
	NS = "uast"

	// TypePosition is a node type for positional information in AST. See AsPosition.
	TypePosition = NS + ":Position"
	// TypePositions is a node type for a root node of positional information in AST. See AsPositions.
	TypePositions = NS + ":Positions"
	// TypeOperator is a node type for an operator AST node. See Operator.
	TypeOperator = NS + ":Operator"
	// KeyPosOff is a name for a Position object field that stores a bytes offset.
	KeyPosOff = "offset"
	// KeyPosLine is a name for a Position object field that stores a source line.
	KeyPosLine = "line"
	// KeyPosCol is a name for a Position object field that stores a source column.
	KeyPosCol = "col"

	KeyStart = "start" // StartPosition
	KeyEnd   = "end"   // EndPosition
)

Variables

View Source
var (
	ErrIncorrectType     = errors.NewKind("incorrect object type: %q, expected: %q")
	ErrTypeNotRegistered = errors.NewKind("type is not registered: %q")
)

Functions

func HashNoPos added in v2.2.0

func HashNoPos(n nodes.External) nodes.Hash

HashNoPos hashes the node, but skips positional information.

func LookupType

func LookupType(typ string) (reflect.Type, bool)

func NewObjectByType

func NewObjectByType(typ string) nodes.Object

func NewObjectByTypeOpt

func NewObjectByTypeOpt(typ string) (obj, opt nodes.Object)

func NewPositionalIterator added in v2.2.0

func NewPositionalIterator(root nodes.External) nodes.Iterator

NewPositionalIterator creates a new iterator that enumerates all object nodes, sorting them by positions in the source file. Nodes with no positions will be enumerated last.

func NewValue

func NewValue(typ string) (reflect.Value, error)

func NodeAs

func NodeAs(n nodes.External, dst interface{}) error

func RegisterPackage

func RegisterPackage(ns string, types ...interface{})

func RoleList

func RoleList(roles ...role.Role) nodes.Array

RoleList converts a set of roles into a list node.

func RolesOf

func RolesOf(m nodes.Object) role.Roles

RolesOf is a helper for getting node UAST roles (see KeyRoles).

func ToNode

func ToNode(o interface{}) (nodes.Node, error)

ToNode converts objects returned by schema-less encodings such as JSON to Node objects. It also supports types from packages registered via RegisterPackage.

func TokenOf

func TokenOf(m nodes.Object) string

TokenOf is a helper for getting node token (see KeyToken).

func Tokens

func Tokens(n nodes.Node) []string

Tokens collects all tokens of the tree recursively (pre-order).

func TypeOf

func TypeOf(o interface{}) string

Types

type Alias

type Alias struct {
	GenNode
	Name Identifier `json:"Name"`
	Node Any        `json:"Node"`
}

type Any

type Any interface{}

Any is an alias type for any UAST node.

type Argument

type Argument struct {
	GenNode
	Name        *Identifier `json:"Name"`
	Type        Any         `json:"Type"`
	Init        Any         `json:"Init"`
	Variadic    bool        `json:"Variadic"`
	MapVariadic bool        `json:"MapVariadic"`
	Receiver    bool        `json:"Receiver"`
}

type Block

type Block struct {
	GenNode
	Statements []Any `json:"Statements"`
}

type Comment

type Comment struct {
	GenNode
	Text   string `json:"Text"`
	Prefix string `json:"Prefix"`
	Suffix string `json:"Suffix"`
	Tab    string `json:"Tab"`
	Block  bool   `json:"Block"`
}

type Function

type Function struct {
	GenNode
	Type FunctionType `json:"Type"`
	Body *Block       `json:"Body"`
}

type FunctionGroup

type FunctionGroup Group

type FunctionType

type FunctionType struct {
	GenNode
	Arguments []Argument `json:"Arguments"`
	Returns   []Argument `json:"Returns"`
}

type GenNode

type GenNode struct {
	Positions Positions `json:"@pos,omitempty"`
}

type Group

type Group struct {
	GenNode
	Nodes []Any `json:"Nodes"`
}

type Identifier

type Identifier struct {
	GenNode
	Name string `json:"Name"`
}

type Import

type Import struct {
	GenNode
	Path   Any   `json:"Path"`
	All    bool  `json:"All"`
	Names  []Any `json:"Names"`
	Target Scope `json:"Target"`
}

type InlineImport added in v2.10.0

type InlineImport Import

type Position

type Position struct {
	// Offset is the position as an absolute byte offset. It is a 0-based index.
	Offset uint32 `json:"offset"`
	// Line is the line number. It is a 1-based index.
	Line uint32 `json:"line"`
	// Col is the column number (the byte offset of the position relative to
	// a line. It is a 1-based index.
	Col uint32 `json:"col"`
}

Position represents a position in a source code file.

func AsPosition

func AsPosition(m nodes.Object) *Position

AsPosition transforms a generic AST node to a Position object.

func (Position) HasLineCol added in v2.2.0

func (p Position) HasLineCol() bool

func (Position) HasOffset added in v2.2.0

func (p Position) HasOffset() bool

func (Position) Less added in v2.2.0

func (p Position) Less(p2 Position) bool

func (Position) ToObject

func (p Position) ToObject() nodes.Object

ToObject converts Position to a generic AST node.

func (Position) Valid added in v2.2.0

func (p Position) Valid() bool

type Positions

type Positions map[string]Position

Positions is a container object that stores all positional information for a node.

func PositionsOf

func PositionsOf(m nodes.Object) Positions

PositionsOf returns an object with all positional information for a node.

func (Positions) End

func (p Positions) End() *Position

End returns an end position of the node.

func (Positions) Keys added in v2.2.0

func (p Positions) Keys() []string

Keys returns a sorted slice of position names.

func (Positions) Start

func (p Positions) Start() *Position

Start returns a start position of the node.

func (Positions) ToObject

func (p Positions) ToObject() nodes.Object

ToObject converts positions to a generic object.

type QualifiedIdentifier

type QualifiedIdentifier struct {
	GenNode
	Names []Identifier `json:"Names"`
}

type RuntimeImport

type RuntimeImport Import

type RuntimeReImport

type RuntimeReImport RuntimeImport

type Scope

type Scope = Any

Scope is a temporary definition of a scope semantic type.

type String

type String struct {
	GenNode
	Value  string `json:"Value"`
	Format string `json:"Format"` // TODO: make an enum later
}

Directories

Path Synopsis
nodesproto
Package nodesproto is a generated protocol buffer package.
Package nodesproto is a generated protocol buffer package.
Package role is a generated protocol buffer package.
Package role is a generated protocol buffer package.

Jump to

Keyboard shortcuts

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