ast

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 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 different types of nodes. * A terminal node (leaf node) can contain a token and a value. * A nil node (leaf node) only contains a position and evaluates to nil * A non-terminal node (branch node) can contain a token and multiple child 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

View Source
const NIL = "NIL"

NIL is the nil token

Variables

This section is empty.

Functions

func AppendNode added in v0.6.0

func AppendNode(n1, n2 parsley.Node) parsley.Node

AppendNode appends

func SetReaderPos added in v0.6.0

func SetReaderPos(node parsley.Node, f func(parsley.Pos) parsley.Pos) parsley.Node

SetReaderPos sets the reader position on a node

func WalkNode added in v0.6.0

func WalkNode(node parsley.Node, f func(i int, n parsley.Node) bool)

WalkNode applies the given function to the node

Types

type InterpreterFunc

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

InterpreterFunc defines a helper to implement the Interpreter interface with functions

func (InterpreterFunc) Eval

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

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

type NilNode added in v0.6.0

type NilNode parsley.Pos

NilNode represents an nil node

func (NilNode) Pos added in v0.6.0

func (n NilNode) Pos() parsley.Pos

Pos returns with NilPosition

func (NilNode) ReaderPos added in v0.6.0

func (n NilNode) ReaderPos() parsley.Pos

ReaderPos returns the reader position

func (NilNode) String added in v0.6.0

func (n NilNode) String() string

String returns with a string representation of the node

func (NilNode) Token added in v0.6.0

func (n NilNode) Token() string

Token returns with NIL

func (NilNode) Value added in v0.6.0

func (n NilNode) Value(ctx interface{}) (interface{}, parsley.Error)

Value returns with nil

type NodeList added in v0.6.0

type NodeList []parsley.Node

NodeList contains a list of nodes, should be used when a parser returns with multiple results

func (*NodeList) Append added in v0.6.0

func (nl *NodeList) Append(node parsley.Node)

Append appends a new node to the list

func (NodeList) Pos added in v0.6.0

func (nl NodeList) Pos() parsley.Pos

Pos returns the value of the first pos (all nodes should have the same position)

func (NodeList) ReaderPos added in v0.6.0

func (nl NodeList) ReaderPos() parsley.Pos

ReaderPos should not be called on a NodeList

func (NodeList) Token added in v0.6.0

func (nl NodeList) Token() string

Token returns with NODE_LIST

func (NodeList) Value added in v0.6.0

func (nl NodeList) Value(ctx interface{}) (interface{}, parsley.Error)

Value returns with the value of the first result

func (NodeList) Walk added in v0.6.0

func (nl NodeList) Walk(f func(i int, n parsley.Node) bool)

Walk runs the given function on all nodes

type NonTerminalNode

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

NonTerminalNode represents a branch node in the AST

func NewEmptyNonTerminalNode added in v0.6.0

func NewEmptyNonTerminalNode(token string, pos parsley.Pos, interpreter parsley.Interpreter) *NonTerminalNode

NewEmptyNonTerminalNode creates a new NonTerminalNode without children

func NewNonTerminalNode

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

NewNonTerminalNode creates a new NonTerminalNode instance

func (*NonTerminalNode) Children

func (n *NonTerminalNode) Children() []parsley.Node

Children returns with the children

func (*NonTerminalNode) Pos

func (n *NonTerminalNode) Pos() parsley.Pos

Pos returns the position

func (*NonTerminalNode) ReaderPos added in v0.6.0

func (n *NonTerminalNode) ReaderPos() parsley.Pos

ReaderPos returns the position of the first character immediately after this node

func (*NonTerminalNode) SetReaderPos added in v0.6.0

func (n *NonTerminalNode) SetReaderPos(f func(parsley.Pos) parsley.Pos)

SetReaderPos amends the reader position using the given function

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{}, parsley.Error)

Value returns with the value of the node

type ReaderPosSetter added in v0.6.0

type ReaderPosSetter interface {
	SetReaderPos(f func(parsley.Pos) parsley.Pos)
}

ReaderPosSetter allows to change the reader position on a 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, value interface{}, pos parsley.Pos, readerPos parsley.Pos) *TerminalNode

NewTerminalNode creates a new TerminalNode instance

func (*TerminalNode) Pos

func (t *TerminalNode) Pos() parsley.Pos

Pos returns the position

func (*TerminalNode) ReaderPos added in v0.6.0

func (t *TerminalNode) ReaderPos() parsley.Pos

ReaderPos returns the position of the first character immediately after this node

func (*TerminalNode) SetReaderPos added in v0.6.0

func (t *TerminalNode) SetReaderPos(f func(parsley.Pos) parsley.Pos)

SetReaderPos changes the reader 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{}, parsley.Error)

Value returns with the value of the node

type Walkable added in v0.6.0

type Walkable interface {
	Walk(f func(i int, n parsley.Node) bool)
}

Walkable is a generic interface to allow to apply a function on the node

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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