ast

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2018 License: MPL-2.0 Imports: 2 Imported by: 6

Documentation

Overview

Package ast defines structs and interfaces for building and evaluating an abstract syntax tree.

Abstract Syntax Tree

ASTs can have two types of nodes. A non-terminal node (branch node) can contain a token and multiple child nodes. A terminal node (leaf node) can contain a token and a value.

Builders

Builder are used to build a node from multiple nodes.

Interpreters

Interpreters get a list of nodes and return with a single value.

Index

Constants

View Source
const EOF = "EOF"

EOF is the end of file token

Variables

This section is empty.

Functions

This section is empty.

Types

type Interpreter

type Interpreter interface {
	Eval(ctx interface{}, nodes []Node) (interface{}, reader.Error)
}

Interpreter defines an interface to evaluate AST nodes

type InterpreterFunc

type InterpreterFunc func(ctx interface{}, nodes []Node) (interface{}, reader.Error)

InterpreterFunc defines a helper to implement the Interpreter interface with functions

func (InterpreterFunc) Eval

func (f InterpreterFunc) Eval(ctx interface{}, nodes []Node) (interface{}, reader.Error)

Eval evaluates the given nodes and returns with a single result.

type Node

type Node interface {
	Token() string
	Value(ctx interface{}) (interface{}, reader.Error)
	Pos() reader.Position
}

Node represents an AST node

type NodeBuilder

type NodeBuilder interface {
	BuildNode([]Node) Node
}

NodeBuilder defines an interface to build a node from multiple nodes

type NodeBuilderFunc

type NodeBuilderFunc func([]Node) Node

NodeBuilderFunc defines a helper to implement the NodeBuilder interface with functions

func (NodeBuilderFunc) BuildNode

func (f NodeBuilderFunc) BuildNode(nodes []Node) Node

BuildNode combines the given nodes into a single node

type NonTerminalNode

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

NonTerminalNode represents a branch node in the AST

func NewNonTerminalNode

func NewNonTerminalNode(token string, children []Node, interpreter Interpreter) NonTerminalNode

NewNonTerminalNode creates a new NonTerminalNode instance

func (NonTerminalNode) Children

func (n NonTerminalNode) Children() []Node

Children returns with the children

func (NonTerminalNode) Pos

Pos returns the position

func (NonTerminalNode) String

func (n NonTerminalNode) String() string

String returns with a string representation of the node

func (NonTerminalNode) Token

func (n NonTerminalNode) Token() string

Token returns with the node token

func (NonTerminalNode) Value

func (n NonTerminalNode) Value(ctx interface{}) (interface{}, reader.Error)

Value returns with the value of the node

type TerminalNode

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

TerminalNode is a leaf node in the AST

func NewTerminalNode

func NewTerminalNode(token string, pos reader.Position, value interface{}) TerminalNode

NewTerminalNode creates a new TerminalNode instance

func (TerminalNode) Pos

func (t TerminalNode) Pos() reader.Position

Pos returns the position

func (TerminalNode) String

func (t TerminalNode) String() string

String returns with a string representation of the node

func (TerminalNode) Token

func (t TerminalNode) Token() string

Token returns with the node token

func (TerminalNode) Value

func (t TerminalNode) Value(ctx interface{}) (interface{}, reader.Error)

Value returns with the value of the node

Directories

Path Synopsis
Package builder defines functions for generating basic node builder functions
Package builder defines functions for generating basic node builder functions
Code generated by mockery v1.0.0 Code generated by mockery v1.0.0 Code generated by mockery v1.0.0
Code generated by mockery v1.0.0 Code generated by mockery v1.0.0 Code generated by mockery v1.0.0

Jump to

Keyboard shortcuts

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