parsley

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2025 License: MIT Imports: 11 Imported by: 0

README ¶

🥬 Parsley

Documentation ¶

Overview ¶

Package parsley a parser/tokeniser used to evaluate expressions

Index ¶

Constants ¶

View Source
const ErrCacheSetup = cache.ErrCacheSetup

ErrCacheSetup is returned when setting up the cache fails

View Source
const ErrComparisonFailed = nodes.ErrComparisonFailed

ErrComparisonFailed is returned when the comparison of two values fails

View Source
const ErrFunctionNotFound = helpers.ConstError("function not found")

ErrFunctionNotFound is returned when an unrecognised function is found

View Source
const ErrNodeEvalFailed = nodes.ErrNodeEvalFailed

ErrNodeEvalFailed is returned when a node failed to evaluate correctly

Variables ¶

This section is empty.

Functions ¶

func ToBool ¶

func ToBool(e any) (bool, error)

ToBool converts the input value in to a bool.

- If the type is a number then any value over 0 will return true - Strings will be checked against known values - Strings not matching a known value will attempt to parse as a float

func ToFloat64 ¶

func ToFloat64(input any) (float64, error)

ToFloat64 attempts to convert the input value in to a float64. It will cast int/uint/flaot types, and attempt to parse strings as floats

func TypesMatch ¶

func TypesMatch(a, b any) bool

TypesMatch check if the types of the two values are the same

Types ¶

type BinaryNodeFunc ¶

type BinaryNodeFunc func(left, right Node) Node

BinaryNodeFunc is a constructor for a binary node

type Function ¶

type Function func(args ...any) (any, error)

Function defines the shape of a function that can be called inside an expression

type Node ¶

type Node = nodes.Node

Node defines the interface for new nodes to adhere

type Parser ¶

type Parser struct {
	Registry *registry
	// contains filtered or unexported fields
}

Parser provides parsing and evaluation functionality

func NewParser ¶

func NewParser(withCache bool) (m *Parser, err error)

NewParser configures a new parser. If a cache it required one will be set up using github.com/dgraph-io/ristretto/v2

func (*Parser) Close ¶

func (m *Parser) Close()

Close releases the underlying resources

func (*Parser) ParseAsAny ¶

func (m *Parser) ParseAsAny(str string, data map[string]any) (any, error)

ParseAsAny will parse and evaluate the expression provided. Returning the result as a whichever type is most appropriate

func (*Parser) ParseAsBool ¶

func (m *Parser) ParseAsBool(str string, data map[string]any) (bool, error)

ParseAsBool is used to test whether the incoming data matches the given expression. Any numeric value over 0, or strings evaluating to true will match

func (*Parser) ParseAsFloat ¶

func (m *Parser) ParseAsFloat(str string, data map[string]any) (float64, error)

ParseAsFloat will parse and evaluate the expression provided. Returning the result as a float64

func (*Parser) ParseAsString ¶

func (m *Parser) ParseAsString(str string, data map[string]any) (string, error)

ParseAsString will parse and evaluate the expression provided. Returning the result as a string, if the expression results in any other type it will be printed as a string

func (*Parser) RegisterBinaryNode ¶

func (p *Parser) RegisterBinaryNode(token string, fun BinaryNodeFunc)

RegisterBinaryNode adds a new binary node to the registry

func (*Parser) RegisterFunction ¶

func (p *Parser) RegisterFunction(name string, fun Function)

RegisterFunction registers a new function in the available set. Repeated calls will result in the latest one being registered

func (*Parser) RegisterUnaryNode ¶

func (p *Parser) RegisterUnaryNode(token string, fun UnaryNodeFunc)

RegisterUnaryNode adds a new unary node to the registry

type UnaryNodeFunc ¶

type UnaryNodeFunc func(right Node) Node

UnaryNodeFunc is a constructor for a unary node

Directories ¶

Path Synopsis
internal
assert
Package assert is used exclusively for asserting unit test output
Package assert is used exclusively for asserting unit test output
cache
Package cache is used to caches parsed expressions ready to be re-evaluated with different data
Package cache is used to caches parsed expressions ready to be re-evaluated with different data
helpers
Package helpers is a bunch of useful functions
Package helpers is a bunch of useful functions
nodes
Package nodes provides implementation and interfaces for the various supported node types
Package nodes provides implementation and interfaces for the various supported node types

Jump to

Keyboard shortcuts

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