expr

package
v1.1.16 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2023 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

The `expr` package implements a generic expression parser and evaluator, and interpolation for strings.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewParseError

func NewParseError(t *Token) error

func NewPosError

func NewPosError(err error, pos Pos) error

Types

type BinaryFunction

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

func (*BinaryFunction) Kind

func (n *BinaryFunction) Kind() TokenKind

func (*BinaryFunction) String

func (n *BinaryFunction) String() string

type ExpressionListNode

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

func (*ExpressionListNode) Kind

func (n *ExpressionListNode) Kind() TokenKind

func (*ExpressionListNode) String

func (n *ExpressionListNode) String() string

type FloatNumberNode

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

FloatNumberNode is a node representing a number in unsigned float64 format

func (*FloatNumberNode) Kind

func (n *FloatNumberNode) Kind() TokenKind

func (*FloatNumberNode) String

func (n *FloatNumberNode) String() string

type IntNumberNode

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

IntNumberNode is a node representing a number in signed uint64 format

func (*IntNumberNode) Kind

func (n *IntNumberNode) Kind() TokenKind

func (*IntNumberNode) String

func (n *IntNumberNode) String() string

type Node

type Node interface {
	Kind() TokenKind
}

Node is a node in the graph

type Parser

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

Parser represents an expression parser

func NewParser

func NewParser(r io.Reader, pos Pos) *Parser

NewParser returns a new instance of Parser

func (*Parser) Parse

func (p *Parser) Parse() (Node, error)

Parse is the main function call given a lexer instance it will parse and construct an abstract syntax tree for the given input.

type Pos

type Pos struct {
	Path *string
	Line uint
	Col  uint
	// contains filtered or unexported fields
}

Pos marks the position within a context, like a file path. If path is nil then Pos is not used. Note that Line and Col are zero-indexed, so 1 needs to be added to them when printing.

func (*Pos) String

func (p *Pos) String() string

type PosError

type PosError struct {
	Err error
	Pos Pos
}

Error with a position

func (*PosError) Error

func (e *PosError) Error() string

type Scanner

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

Scanner represents a lexical scanner.

func NewScanner

func NewScanner(r io.Reader, pos Pos) *Scanner

NewScanner returns a new instance of Scanner.

func (*Scanner) Scan

func (s *Scanner) Scan() *Token

Scan returns the next token and literal value.

func (*Scanner) Tokens

func (s *Scanner) Tokens() ([]*Token, error)

Tokens returns tokens from the scanner until EOF or illegal input is encountered

type Token

type Token struct {
	Kind TokenKind
	Val  any
	Pos  Pos
}

Token is decomposed from []byte stream to represent a kind of token and the vaoue of the token

func Interpolate

func Interpolate(r io.Reader, pos Pos) ([]*Token, error)

Interpolation of a stream to return a set of tokens, some of which are strings, and some of which are expression tokens

func NewToken

func NewToken(kind TokenKind, val any, pos Pos) *Token

func (*Token) String

func (t *Token) String() string

type TokenKind

type TokenKind uint

Classifies the kind of token

const (
	Any TokenKind = iota
	String
	Expr
	Space
	Ident
	Number
	Punkt
	Question
	Colon
	Comma
	OpenParen
	CloseParen
	OpenSquare
	CloseSquare
	OpenBrace
	CloseBrace
	Ampersand
	Equal
	Less
	Greater
	Plus
	Minus
	Multiply
	Divide
	Not
	True
	False
	Null
	EOF
	// The following types are used for nodes only
	List
	Lowest = Equal // Lowest precedence
)

func (TokenKind) String

func (k TokenKind) String() string

type UintNumberNode

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

UintNumberNode is a node representing a number in unsigned uint64 format

func (*UintNumberNode) Kind

func (n *UintNumberNode) Kind() TokenKind

func (*UintNumberNode) String

func (n *UintNumberNode) String() string

type UnaryFunction

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

func (*UnaryFunction) Kind

func (n *UnaryFunction) Kind() TokenKind

func (*UnaryFunction) String

func (n *UnaryFunction) String() string

Jump to

Keyboard shortcuts

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