expr

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: MIT Imports: 14 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package expr parses and evaluates the expression language of Vega (D9).

An expression is a subset of JavaScript expressions, such as datum.price * 2 or datum.open < datum.close ? 'up' : 'down'. Compile parses an expression into a Program, and a Program evaluates against a Scope: the datum, the signals and the functions that the runtime gives it. The rules for +, == and truth follow JavaScript, as Vega does. docs/EXPRESSIONS.md holds the design.

Index

Constants

This section is empty.

Variables

View Source
var Undefined any = undefined{}

Undefined is the value undefined of JavaScript.

Functions

func Ascending

func Ascending(a, b any) int

Ascending compares two values in the natural order that the sorts of Vega use: null and undefined first, then numbers, dates and strings by their values. NaN is equal to everything, as it is not ordered.

func IsUndefined

func IsUndefined(v any) bool

IsUndefined reports whether v is undefined.

func Number

func Number(v any) float64

Number converts a value to a number, as Number() of JavaScript does.

func String

func String(v any) string

String converts a value to a string, as String() of JavaScript does.

func Truthy

func Truthy(v any) bool

Truthy reports whether a value is true in a condition, as JavaScript decides: false, 0, NaN, the empty string, null and undefined are false.

Types

type Color

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

Color is a color that the functions rgb, hsl, lab and hcl make. An expression reads its channels as properties, such as .r of an RGB color or .h of an HSL color, and writes it as rgb() or rgba(), as d3 does.

func (*Color) String

func (c *Color) String() string

String writes the color as rgb() or rgba().

type Error

type Error string

Error is an error of the package expr.

const (
	// ErrSyntax is the error of an expression that does not parse.
	ErrSyntax Error = "syntax error"
	// ErrUnknownFunction is the error of a call to a function that the
	// scope does not hold.
	ErrUnknownFunction Error = "unknown function"
	// ErrNotAllowed is the error of a construct that Vega does not allow in
	// an expression, such as an assignment.
	ErrNotAllowed Error = "not allowed in an expression"
)

The errors of the package expr.

const ErrArgument Error = "invalid argument"

ErrArgument is the error of a function called with an argument that it cannot use, such as a format spec that does not parse.

const ErrUnknownName Error = "unknown name"

ErrUnknownName is the error of a name that is neither datum, parent, a constant nor a signal.

func (Error) Error

func (err Error) Error() string

Error satisfies the error interface.

type Func

type Func func(s *Scope, args []any) (any, error)

Func is a function that an expression can call. It takes the scope of the call, so that a function of the runtime, such as scale, reaches the view.

type Program

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

Program is a parsed expression.

func Compile

func Compile(src string) (*Program, error)

Compile parses an expression.

func (*Program) Eval

func (p *Program) Eval(s *Scope) (any, error)

Eval evaluates the expression in a scope.

func (*Program) String

func (p *Program) String() string

String returns the text of the expression.

type RegExp

type RegExp struct {
	*regexp.Regexp

	Source string
	Global bool
}

RegExp is a regular expression, which the function regexp makes. Go reads the pattern, so a pattern that only JavaScript reads, such as one with a lookahead, is an error.

type Scope

type Scope struct {
	// Datum is the row that the expression reads as datum.
	Datum any
	// Parent is the datum of the enclosing group, which it reads as parent.
	Parent any
	// Signals holds the signals of the view. It can be nil.
	Signals Signals
	// Functions holds functions beyond the built in ones, such as the
	// functions that read the data and the scales of a view, and those that a
	// caller adds. A function here takes the place of a built in one of the
	// same name.
	Functions map[string]Func
	// Location is the time zone of the functions of local time (D27). Nil is
	// time.Local.
	Location *time.Location
	// Rand is the source of random numbers (D28). Nil is a source seeded
	// from the system.
	Rand *rand.Rand
	// Locale holds the names and symbols of the formats (D27). Nil is US
	// English.
	Locale *format.Locale
}

Scope is what an expression reads when it runs.

type Signals

type Signals interface {
	Signal(name string) (any, bool)
}

Signals gives the value of a signal by name.

Jump to

Keyboard shortcuts

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