query

package
v0.0.0-...-9ec3720 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2021 License: AGPL-3.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	LOWEST = iota
	PCONDOR
	PCONDAND
	EQUALS
	LESSGREATER
	PPRODUCT
	CALL
	INDEX
)

Variables

View Source
var (
	// UnknownToken can be used as a sentinel token for a unknown state.
	UnknownToken = Token{
		Type: UNKNOWN,
	}
)

Functions

func ErrInvalidIdentifier

func ErrInvalidIdentifier(name string) error

ErrInvalidIdentifier defines a sentinel error for invalid identifiers.

func ForEach

func ForEach(box Box, fn func(value interface{}) bool)

ForEach will call the function on every value within a Box. If a Box isn't an iterable then we perform a no-op.

func IsInvalidIdentifierErr

func IsInvalidIdentifierErr(err error) bool

IsInvalidIdentifierErr returns if the error is an ErrInvalidIdentifier error

func IsRuntimeError

func IsRuntimeError(err error) bool

IsRuntimeError returns if the error is an ErrInvalidIndex error

func RuntimeErrorf

func RuntimeErrorf(msg string, args ...interface{}) error

RuntimeErrorf defines a sentinel error for invalid index.

Types

type AccessorExpression

type AccessorExpression struct {
	Token Token
	Right Expression
	Left  Expression
}

AccessorExpression represents an expression that is associated with an operator.

func (*AccessorExpression) End

func (ie *AccessorExpression) End() Position

End returns the last position of the identifier.

func (*AccessorExpression) Pos

func (ie *AccessorExpression) Pos() Position

Pos returns the first position of the identifier.

func (*AccessorExpression) String

func (ie *AccessorExpression) String() string

type Bool

type Bool struct {
	Token Token
	Value bool
}

Bool represents an bool for a given AST block

func (*Bool) End

func (i *Bool) End() Position

End returns the last position of the bool.

func (*Bool) Pos

func (i *Bool) Pos() Position

Pos returns the first position of the bool.

func (*Bool) String

func (i *Bool) String() string

type Box

type Box interface {
	Ord

	// IsZero returns if the underlying value is zero.
	IsZero() bool

	// Value defines the shadow type value of the Box.
	Value() interface{}
}

Box represents a boxed datatype.

func ConvertRawResult

func ConvertRawResult(value interface{}) (Box, error)

type BoxBool

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

BoxBool defines an ordered float.

func NewBool

func NewBool(value bool) *BoxBool

NewBool creates a new Box value

func (*BoxBool) Equal

func (o *BoxBool) Equal(other Ord) bool

Equal checks if an BoxBool is equal to another BoxBool.

func (*BoxBool) ForEach

func (o *BoxBool) ForEach(fn func(interface{}) bool)

ForEach iterates over each value in the box.

func (*BoxBool) IsZero

func (o *BoxBool) IsZero() bool

IsZero returns if the underlying value is zero.

func (*BoxBool) Less

func (o *BoxBool) Less(other Ord) bool

Less checks if a BoxBool is less than another BoxBool.

func (*BoxBool) Value

func (o *BoxBool) Value() interface{}

Value defines the shadow type value of the Box.

type BoxFloat

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

BoxFloat defines an ordered float.

func NewFloat

func NewFloat(value float64) *BoxFloat

NewFloat creates a new Box value

func (*BoxFloat) Equal

func (o *BoxFloat) Equal(other Ord) bool

Equal checks if an BoxFloat is equal to another BoxFloat.

func (*BoxFloat) ForEach

func (o *BoxFloat) ForEach(fn func(interface{}) bool)

ForEach iterates over each value in the box.

func (*BoxFloat) IsZero

func (o *BoxFloat) IsZero() bool

IsZero returns if the underlying value is zero.

func (*BoxFloat) Less

func (o *BoxFloat) Less(other Ord) bool

Less checks if a BoxFloat is less than another BoxFloat.

func (*BoxFloat) Value

func (o *BoxFloat) Value() interface{}

Value defines the shadow type value of the Box.

type BoxInteger

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

BoxInteger defines an ordered integer.

func NewInteger

func NewInteger(value int64) *BoxInteger

NewInteger creates a new Box value

func (*BoxInteger) Equal

func (o *BoxInteger) Equal(other Ord) bool

Equal checks if an BoxInteger is equal to another BoxInteger.

func (*BoxInteger) ForEach

func (o *BoxInteger) ForEach(fn func(interface{}) bool)

ForEach iterates over each value in the box.

func (*BoxInteger) IsZero

func (o *BoxInteger) IsZero() bool

IsZero returns if the underlying value is zero.

func (*BoxInteger) Less

func (o *BoxInteger) Less(other Ord) bool

Less checks if a BoxInteger is less than another BoxInteger.

func (*BoxInteger) Value

func (o *BoxInteger) Value() interface{}

Value defines the shadow type value of the Box.

type BoxLambda

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

BoxLambda defines an ordered integer.

func NewLambda

func NewLambda(arg *Identifier, fn func(Scope) ([]Box, error)) *BoxLambda

NewLambda creates a new Box value

func (*BoxLambda) ArgName

func (o *BoxLambda) ArgName() string

ArgName is the name expected to be seen in the lambda.

func (*BoxLambda) Call

func (o *BoxLambda) Call(scope Scope) ([]Box, error)

Call the underlying lambda

func (*BoxLambda) Equal

func (o *BoxLambda) Equal(other Ord) bool

Equal checks if an BoxLambda is equal to another BoxLambda.

func (*BoxLambda) IsZero

func (o *BoxLambda) IsZero() bool

IsZero returns if the underlying value is zero.

func (*BoxLambda) Less

func (o *BoxLambda) Less(other Ord) bool

Less checks if a BoxLambda is less than another BoxLambda.

func (*BoxLambda) Value

func (o *BoxLambda) Value() interface{}

Value defines the shadow type value of the Box.

type BoxMapInterfaceInterface

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

BoxMapInterfaceInterface defines an ordered map[interface{}]interface{}.

func NewMapInterfaceInterface

func NewMapInterfaceInterface(value map[interface{}]interface{}) *BoxMapInterfaceInterface

NewMapInterfaceInterface creates a new Box value

func (*BoxMapInterfaceInterface) Equal

func (o *BoxMapInterfaceInterface) Equal(other Ord) bool

Equal checks if an BoxMapInterfaceInterface is equal to another BoxMapInterfaceInterface.

func (*BoxMapInterfaceInterface) ForEach

func (o *BoxMapInterfaceInterface) ForEach(fn func(interface{}) bool)

ForEach iterates over each value in the box.

func (*BoxMapInterfaceInterface) IsZero

func (o *BoxMapInterfaceInterface) IsZero() bool

IsZero returns if the underlying value is zero.

func (*BoxMapInterfaceInterface) Less

func (o *BoxMapInterfaceInterface) Less(other Ord) bool

Less checks if a BoxMapInterfaceInterface is less than another BoxMapInterfaceInterface.

func (*BoxMapInterfaceInterface) Value

func (o *BoxMapInterfaceInterface) Value() interface{}

Value defines the shadow type value of the Box.

type BoxMapStringInterface

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

BoxMapStringInterface defines an ordered map[string]interface{}.

func NewMapStringInterface

func NewMapStringInterface(value map[string]interface{}) *BoxMapStringInterface

NewMapStringInterface creates a new Box value

func (*BoxMapStringInterface) Equal

func (o *BoxMapStringInterface) Equal(other Ord) bool

Equal checks if an BoxMapStringInterface is equal to another BoxMapStringInterface.

func (*BoxMapStringInterface) ForEach

func (o *BoxMapStringInterface) ForEach(fn func(interface{}) bool)

ForEach iterates over each value in the box.

func (*BoxMapStringInterface) IsZero

func (o *BoxMapStringInterface) IsZero() bool

IsZero returns if the underlying value is zero.

func (*BoxMapStringInterface) Less

func (o *BoxMapStringInterface) Less(other Ord) bool

Less checks if a BoxMapStringInterface is less than another BoxMapStringInterface.

func (*BoxMapStringInterface) Value

func (o *BoxMapStringInterface) Value() interface{}

Value defines the shadow type value of the Box.

type BoxNestedScope

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

BoxNestedScope defines an ordered integer.

func (*BoxNestedScope) Equal

func (o *BoxNestedScope) Equal(other Ord) bool

Equal checks if an BoxNestedScope is equal to another BoxNestedScope.

func (*BoxNestedScope) IsZero

func (o *BoxNestedScope) IsZero() bool

IsZero returns if the underlying value is zero.

func (*BoxNestedScope) Less

func (o *BoxNestedScope) Less(other Ord) bool

Less checks if a BoxNestedScope is less than another BoxNestedScope.

func (*BoxNestedScope) Value

func (o *BoxNestedScope) Value() interface{}

Value defines the shadow type value of the Box.

type BoxSliceString

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

BoxSliceString defines an ordered []string.

func NewSliceString

func NewSliceString(value []string) *BoxSliceString

NewSliceString creates a new Box value

func (*BoxSliceString) Equal

func (o *BoxSliceString) Equal(other Ord) bool

Equal checks if an BoxSliceString is equal to another BoxSliceString.

func (*BoxSliceString) ForEach

func (o *BoxSliceString) ForEach(fn func(interface{}) bool)

ForEach iterates over each value in the box.

func (*BoxSliceString) IsZero

func (o *BoxSliceString) IsZero() bool

IsZero returns if the underlying value is zero.

func (*BoxSliceString) Less

func (o *BoxSliceString) Less(other Ord) bool

Less checks if a BoxSliceString is less than another BoxSliceString.

func (*BoxSliceString) Value

func (o *BoxSliceString) Value() interface{}

Value defines the shadow type value of the Box.

type BoxString

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

BoxString defines an ordered string.

func NewString

func NewString(value string) *BoxString

NewString creates a new Box value

func (*BoxString) Equal

func (o *BoxString) Equal(other Ord) bool

Equal checks if an BoxString is equal to another BoxString.

func (*BoxString) ForEach

func (o *BoxString) ForEach(fn func(interface{}) bool)

ForEach iterates over each value in the box.

func (*BoxString) IsZero

func (o *BoxString) IsZero() bool

IsZero returns if the underlying value is zero.

func (*BoxString) Less

func (o *BoxString) Less(other Ord) bool

Less checks if a BoxString is less than another BoxString.

func (*BoxString) Value

func (o *BoxString) Value() interface{}

Value defines the shadow type value of the Box.

type CallExpression

type CallExpression struct {
	Token     Token
	Name      Expression
	Arguments []Expression
}

CallExpression represents an expression that is associated with an operator.

func (*CallExpression) End

func (ie *CallExpression) End() Position

End returns the last position of the identifier.

func (*CallExpression) Pos

func (ie *CallExpression) Pos() Position

Pos returns the first position of the identifier.

func (*CallExpression) String

func (ie *CallExpression) String() string

type Empty

type Empty struct {
	Token Token
}

Empty represents an empty expression

func (*Empty) End

func (i *Empty) End() Position

End returns the last position of the empty expression.

func (*Empty) Pos

func (i *Empty) Pos() Position

Pos returns the first position of the empty expression.

func (*Empty) String

func (i *Empty) String() string

type Expression

type Expression interface {
	Pos() Position
	End() Position

	String() string
}

Expression defines a type of AST node for outlining an expression.

type ExpressionStatement

type ExpressionStatement struct {
	Token      Token
	Expression Expression
}

ExpressionStatement is a group of expressions that allows us to group a subset of expressions.

func (*ExpressionStatement) End

func (es *ExpressionStatement) End() Position

End returns the last position of the expression statement.

func (*ExpressionStatement) Pos

func (es *ExpressionStatement) Pos() Position

Pos returns the first position of the expression statement.

func (*ExpressionStatement) String

func (es *ExpressionStatement) String() string

type Float

type Float struct {
	Token Token
	Value float64
}

Float represents an float for a given AST block

func (*Float) End

func (i *Float) End() Position

End returns the last position of the float.

func (*Float) Pos

func (i *Float) Pos() Position

Pos returns the first position of the float.

func (*Float) String

func (i *Float) String() string

type FuncScope

type FuncScope interface {
	Add(string, interface{})
	Call(*Identifier, []Box) (interface{}, error)
}

FuncScope is used to call functions for a given identifier.

type GlobalFuncScope

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

GlobalFuncScope defines a set of builtin functions that can be executed based on a set of arguments.

func NewGlobalFuncScope

func NewGlobalFuncScope(scope Scope) *GlobalFuncScope

NewGlobalFuncScope creates a new scope for executing functions.

func (*GlobalFuncScope) Add

func (s *GlobalFuncScope) Add(name string, fn interface{})

Add a function to the global scope.

func (*GlobalFuncScope) Call

func (s *GlobalFuncScope) Call(ident *Identifier, params []Box) (interface{}, error)

Call a function with a set of arguments.

type Identifier

type Identifier struct {
	Token Token
}

Identifier represents an identifier for a given AST block

func (*Identifier) End

func (i *Identifier) End() Position

End returns the last position of the identifier.

func (*Identifier) Pos

func (i *Identifier) Pos() Position

Pos returns the first position of the identifier.

func (*Identifier) String

func (i *Identifier) String() string

type IndexExpression

type IndexExpression struct {
	Token    Token
	Operator string
	Left     Expression
	Index    Expression
}

IndexExpression represents an expression that is associated with an operator.

func (*IndexExpression) End

func (ie *IndexExpression) End() Position

End returns the last position of the identifier.

func (*IndexExpression) Pos

func (ie *IndexExpression) Pos() Position

Pos returns the first position of the identifier.

func (*IndexExpression) String

func (ie *IndexExpression) String() string

type InfixExpression

type InfixExpression struct {
	Token    Token
	Operator string
	Right    Expression
	Left     Expression
}

AccessorExpression represents an expression that is associated with an operator.

func (*InfixExpression) End

func (ie *InfixExpression) End() Position

End returns the last position of the identifier.

func (*InfixExpression) Pos

func (ie *InfixExpression) Pos() Position

Pos returns the first position of the identifier.

func (*InfixExpression) String

func (ie *InfixExpression) String() string

type InfixFunc

type InfixFunc func(Expression) Expression

type Integer

type Integer struct {
	Token Token
	Value int64
}

Integer represents an integer for a given AST block

func (*Integer) End

func (i *Integer) End() Position

End returns the last position of the integer.

func (*Integer) Pos

func (i *Integer) Pos() Position

Pos returns the first position of the integer.

func (*Integer) String

func (i *Integer) String() string

type InvalidIdentifierError

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

InvalidIdentifierError creates an invalid error.

func (*InvalidIdentifierError) Error

func (e *InvalidIdentifierError) Error() string

func (*InvalidIdentifierError) Name

func (e *InvalidIdentifierError) Name() string

Name returns the name associated with the identifier error.

type LambdaExpression

type LambdaExpression struct {
	Token       Token
	Argument    Expression
	Expressions []Expression
}

LambdaExpression represents an expression that is associated with an operator.

func (*LambdaExpression) End

func (ie *LambdaExpression) End() Position

End returns the last position of the identifier.

func (*LambdaExpression) Pos

func (ie *LambdaExpression) Pos() Position

Pos returns the first position of the identifier.

func (*LambdaExpression) String

func (ie *LambdaExpression) String() string

type Lexer

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

Lexer takes a query and breaks it down into tokens that can be consumed at at later date. The lexer in question is lazy and requires the calling of next to move it forward.

func NewLexer

func NewLexer(input string) *Lexer

NewLexer creates a new Lexer from a given input.

func (*Lexer) NextToken

func (l *Lexer) NextToken() Token

NextToken attempts to grab the next token available.

func (*Lexer) Peek

func (l *Lexer) Peek() rune

Peek will attempt to read the next rune if it's available.

func (*Lexer) PeekN

func (l *Lexer) PeekN(n int) rune

PeekN attempts to read the next rune by a given offset, it it's available.

func (*Lexer) ReadNext

func (l *Lexer) ReadNext()

ReadNext will attempt to read the next character and correctly setup the positional values for the input.

type NestedScope

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

NestedScope allows scopes to be nested together in a named manor.

func MakeNestedScope

func MakeNestedScope(base Scope) NestedScope

MakeNestedScope creates a new NestedScope.

func (NestedScope) GetIdentValue

func (m NestedScope) GetIdentValue(name string) (Box, error)

GetIdentValue returns the value of the identifier in a given scope.

func (NestedScope) GetIdents

func (m NestedScope) GetIdents() []string

GetIdents returns the identifiers with in a given scope.

func (NestedScope) SetScope

func (m NestedScope) SetScope(name string, scope Scope)

SetScope will set a scope on a given scope.

type Ord

type Ord interface {
	// Less checks if a Box is less than another Box
	Less(Ord) bool

	// Equal checks if an Box is equal to another Box.
	Equal(Ord) bool
}

Ord represents an ordered type

type Parser

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

func NewParser

func NewParser(lex *Lexer) *Parser

NewParser creates a parser for consuming a lexer tokens.

func (*Parser) Run

func (p *Parser) Run() (*QueryExpression, error)

Run the parser to the end, which is either an EOF or an error.

type Position

type Position struct {
	Offset int
	Line   int
	Column int
}

Position holds the location of the token within the query.

func (Position) String

func (p Position) String() string

type PrefixFunc

type PrefixFunc func() Expression

type Query

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

Query holds all the arguments for a given query.

func Parse

func Parse(src string) (Query, error)

Parse attempts to parse a given query into a argument query. Returns an error if it's not in the correct layout.

func (Query) BuiltinsRun

func (q Query) BuiltinsRun(scope Scope) (bool, error)

BuiltinsRun runs the query with a set of builtin functions.

func (Query) Run

func (q Query) Run(fnScope FuncScope, scope Scope) (bool, error)

Run the query over a given scope.

type QueryExpression

type QueryExpression struct {
	Expressions []Expression
}

QueryExpression represents a query full of expressions

func (*QueryExpression) End

func (e *QueryExpression) End() Position

End returns the last position of the query expression.

func (*QueryExpression) Pos

func (e *QueryExpression) Pos() Position

Pos returns the first position of the query expression.

func (*QueryExpression) String

func (e *QueryExpression) String() string

type RuntimeError

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

RuntimeError creates an invalid error.

func (*RuntimeError) Error

func (e *RuntimeError) Error() string

type Scope

type Scope interface {

	// GetIdents returns the identifiers that are supported for a given scope.
	GetIdents() []string

	// GetIdentValue returns the value of the identifier in a given scope.
	GetIdentValue(string) (Box, error)
}

Scope is used to identify a given expression of a global mutated object.

type String

type String struct {
	Token Token
}

String represents an string for a given AST block

func (*String) End

func (i *String) End() Position

End returns the last position of the string.

func (*String) Pos

func (i *String) Pos() Position

Pos returns the first position of the string.

func (*String) String

func (i *String) String() string

type Token

type Token struct {
	Pos     Position
	Type    TokenType
	Literal string
}

Token defines a token found with in a query, along with the position and what type it is.

func MakeToken

func MakeToken(tokenType TokenType, char rune) Token

MakeToken creates a new token value.

type TokenType

type TokenType int

TokenType represents a way to identify an individual token.

const (
	UNKNOWN TokenType = (iota - 1)
	EOF

	IDENT
	INT   //int literal
	FLOAT //float literal
	STRING

	EQ     // ==
	NEQ    // !=
	ASSIGN // =
	BANG   // !

	LT // <
	LE // <=
	GT // >
	GE // >=

	COMMA     // ,
	SEMICOLON // ;

	LPAREN   // (
	RPAREN   // )
	LBRACKET // [
	RBRACKET // ]

	BITAND  // &
	BITOR   // |
	CONDAND // &&
	CONDOR  // ||
	TRUE    // TRUE
	FALSE   // FALSE

	LAMBDA     // =>
	UNDERSCORE // _
	PERIOD     // .
)

func (TokenType) String

func (t TokenType) String() string

Jump to

Keyboard shortcuts

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