parse

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: May 17, 2021 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	NIL token = iota
	OP
	LEAF
)

Define valid token types

Variables

This section is empty.

Functions

This section is empty.

Types

type Leaf

type Leaf struct {
	Val uint
}

Leaf is a concrete Node that will hold a single value

func (*Leaf) Eval

func (l *Leaf) Eval(w io.Writer) error

Eval will print the leaf's value to a writer

func (*Leaf) Index

func (l *Leaf) Index(start int) Node

Index Leaf will add start to the current value

func (*Leaf) Remove

func (l *Leaf) Remove(v uint) Node

Remove a node by value

n.Remove(1)

Removes any leaf that has a value of 1, shifting up the tree where needed

func (*Leaf) String

func (l *Leaf) String() string

String is our stringer for pretty printing the tree. Well, ok, it is ugly printing, but it is printing. It will take a tree and print something like: 1 <- AND -> 2 <- OR -> 3

type Node

type Node interface {
	Remove(uint) Node
	Eval(io.Writer) error
	Index(int) Node
}

Node is our interface and represents a node in a binary expression tree.

func Parse

func Parse(logic string) (Node, error)

Parse will take a logic string (e.g. "1 AND 2 OR (3 AND 4)"), parse it and turn it into a binary expression tree.

func Sequence

func Sequence(n Node) Node

Sequence will walk all of the leaves and re-sequence the values, starting at 0 and removing sparseness

func WalkLeaves

func WalkLeaves(n Node, visit Visitor) Node

WalkLeaves will visit every leaf and run the Visitor action on each

type Op

type Op struct {
	Left  Node
	Val   string
	Right Node
}

Op is a concrete Node that will hold an operation with pointers to a left and right node, both of which can be a Leaf or another Op

func (*Op) Eval

func (o *Op) Eval(w io.Writer) error

Eval will print the left node, the operation, and then the right node to a writer

func (*Op) Index

func (o *Op) Index(start int) Node

Index Op will index the left and right nodes

func (*Op) Remove

func (o *Op) Remove(v uint) Node

Remove a node by value from an operation

n.Remove(1)

Removes any leaf that has a value of 1, shifting up the tree where needed

func (*Op) String

func (o *Op) String() string

String is our stringer for pretty printing the tree. Well, ok, it is ugly printing, but it is printing. It will take a tree and print something like: 1 <- AND -> 2 <- OR -> 3

type ParseError

type ParseError struct {
	Position int
	Logic    string
	Reason   string
}

ParseError holds information about syntactic errors when trying to eval

func (*ParseError) Error

func (e *ParseError) Error() string

type SerializeError

type SerializeError struct {
	Op     string
	Reason string
}

SerializeError holds information about syntactic errors when trying to eval

func (*SerializeError) Error

func (e *SerializeError) Error() string

type Visitor

type Visitor func(Node) Node

Visitor visits a node, allowing you to take action on a node

Jump to

Keyboard shortcuts

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