ann

package
v1.17.0 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2019 License: GPL-3.0 Imports: 4 Imported by: 2

Documentation

Overview

Package ann provides a DSL to annotate UAST.

Index

Constants

This section is empty.

Variables

View Source
var Any = &any{}

Any matches any path.

Functions

This section is empty.

Types

type Action

type Action interface {
	fmt.Stringer
	Do(n *uast.Node) error
}

Action is the interface that wraps an operation to be made on a uast.Node, possibly mutatin it. The Do function applies the operation to the provided node and returns an optional error. The string method returns a string describing the operation.

func AddRoles

func AddRoles(roles ...uast.Role) Action

AddRoles creates an action to add the given roles to a node.

func ReturnError

func ReturnError(err error) Action

ReturnError creates an action that always returns a RuleError wrapping the given error with the offending node information.

type Predicate

type Predicate interface {
	fmt.Stringer
	Eval(n *uast.Node) bool
}

Predicate is the interface that wraps boolean tests for uast.Nodes.

The Eval function evaluates the test over a node and returns its boolean output. The String function returns a description of the predicate in a syntax similar to XPath.

func And

func And(ps ...Predicate) Predicate

And returns a predicate that returns true if all the given predicates returns true.

func HasChild

func HasChild(pred Predicate) Predicate

HasChild matches a node that contains a child matching the given predicate.

func HasInternalRole

func HasInternalRole(r string) Predicate

HasInternalRole is a convenience shortcut for:

HasProperty(uast.InternalRoleKey, r)

func HasInternalType

func HasInternalType(it string) Predicate

HasInternalType matches a node if its internal type matches the given one.

func HasProperty

func HasProperty(k, v string) Predicate

HasProperty matches a node if it has a property matching the given key and value.

func HasToken

func HasToken(tk string) Predicate

HasToken matches a node if its token matches the given one.

func Not

func Not(p Predicate) Predicate

Not negates a node predicate.

func Or

func Or(ps ...Predicate) Predicate

Or returns a predicate that returns true if any of the given predicates returns true.

type Rule

type Rule struct {
	// contains filtered or unexported fields
}

Rule is a conversion rule that can visit a tree, match nodes against path matchers and apply actions to the matching node.

func On

func On(predicates ...Predicate) *Rule

On is the *Rule constructor. It takes a list of predicates and returns a new *Rule that matches all of them.

func (*Rule) Apply

func (r *Rule) Apply(n *uast.Node) (err error)

Apply applies the rule to the given node.

func (*Rule) Children

func (r *Rule) Children(rules ...*Rule) *Rule

Children applies the given rules to children of nodes matched by the current rule.

func (*Rule) Descendants

func (r *Rule) Descendants(rules ...*Rule) *Rule

Descendants applies the given rules to any descendant matched of nodes matched by the current rule.

func (*Rule) DescendantsOrSelf

func (r *Rule) DescendantsOrSelf(rules ...*Rule) *Rule

DescendantsOrSelf applies the given rules to self and any descendant matched of nodes matched by the current rule.

func (*Rule) Do

func (r *Rule) Do(actions ...Action) *Rule

Do attaches actions to the rule.

func (*Rule) Error

func (r *Rule) Error(err error) *Rule

Error makes the rule application fail if the current rule matches.

func (*Rule) Roles

func (r *Rule) Roles(roles ...uast.Role) *Rule

Roles attaches an action to the rule that adds the given roles.

func (*Rule) Self

func (r *Rule) Self(rules ...*Rule) *Rule

Self applies the given rules to nodes matched by the current rule.

func (*Rule) String

func (r *Rule) String() string

String returns a Markdown table representation of the rule. The table contains two columns: an XPath-like path for nodes and a human readable description of the actions associated with those paths.

type RuleError

type RuleError interface {
	// Error implements the error interface.
	Error() string
	// Inner returns the wrapped error.
	Inner() error
	// Node returns the offending node.
	Node() *uast.Node
}

RuleError values are returned by the annotation process when a rule created by the Error function is activated. A RuleError wraps the desired error and carries the node that provoke the error.

Jump to

Keyboard shortcuts

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