tsl

package
v6.0.7 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2025 License: Apache-2.0 Imports: 5 Imported by: 3

Documentation

Overview

Package tsl describe and parse the Tree Search Language (TSL), TSL is a wonderful search language, With similar grammar to SQL's where part.

TSL grammar examples:

"name = 'joe' or name = 'jane'"
"city in ('paris', 'rome', 'milan') and state != 'spane'"
"pages between 100 and 200 and author.name ~= 'Hilbert'"

The package provide the ParseTSL method to convert TSL string into TSL tree.

Usage:

// Parse input string into a TSL tree.
tree, err := tsl.ParseTSL("user.name like 'Joe'")
if err != nil {
    log.Fatal(err)
}

TSL tree can be used to generate SQL and MongoDB query filters, see the walkers package for TSL tree walking examples.

https://pkg.go.dev/github.com/yaacov/tree-search-language/v6/pkg/walkers

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BetweenOperatorError

type BetweenOperatorError struct {
	Message string
}

BetweenOperatorError is returned when BETWEEN operator is used incorrectly

func (BetweenOperatorError) Error

func (e BetweenOperatorError) Error() string

type DivisionByZeroError

type DivisionByZeroError struct {
	Operation string
}

DivisionByZeroError is returned when attempting to divide by zero

func (DivisionByZeroError) Error

func (e DivisionByZeroError) Error() string

type KeyNotFoundError

type KeyNotFoundError struct {
	Key string
}

KeyNotFoundError is returned when a requested key is not found in the data

func (KeyNotFoundError) Error

func (e KeyNotFoundError) Error() string

type Kind

type Kind int

Kind represents the kind of node in the AST These values must match the ast_node_type enum in tsl_ast.h

const (
	KindNumericLiteral   Kind = 0 // AST_NUMBER
	KindStringLiteral    Kind = 1 // AST_STRING
	KindIdentifier       Kind = 2 // AST_IDENTIFIER
	KindBinaryExpr       Kind = 3 // AST_BINARY_OP
	KindUnaryExpr        Kind = 4 // AST_UNARY_OP
	KindDateLiteral      Kind = 5 // AST_DATE
	KindTimestampLiteral Kind = 6 // AST_RFC3339
	KindArrayLiteral     Kind = 7 // AST_ARRAY
	KindBooleanLiteral   Kind = 8 // AST_BOOL
	KindNullLiteral      Kind = 9 // AST_NULL
)

func (Kind) String

func (kind Kind) String() string

String returns the string representation of a NodeKind

type Operator

type Operator int

Operator represents the type definitions. These values must match the yytokentype enum tsl_parser.tab.h

const (
	OpLike    Operator = 258 // K_LIKE (SQL LIKE)
	OpILike   Operator = 259 // K_ILIKE (SQL ILIKE)
	OpAnd     Operator = 260 // K_AND
	OpOr      Operator = 261 // K_OR
	OpBetween Operator = 262 // K_BETWEEN
	OpIn      Operator = 263 // K_IN
	OpIs      Operator = 264 // K_IS (Only for NULL and NOT NULL)
	// 265 K_NULL
	OpNot Operator = 266 // K_NOT
	// 267 K_TRUE
	// 268 K_FALSE
	OpLen Operator = 269 // K_LEN
	OpAny Operator = 270 // K_ANY
	OpAll Operator = 271 // K_ALL
	OpSum Operator = 272 // K_SUM (Sum operator)

	// Arithmetic Operators
	OpPlus    Operator = 278 // PLUS
	OpMinus   Operator = 279 // MINUS
	OpStar    Operator = 280 // STAR (MULTIPLY)
	OpSlash   Operator = 281 // SLASH (DIVIDE)
	OpPercent Operator = 282 // PERCENT (MODULUS)

	// Comparison Operators
	OpEQ  Operator = 288 // EQ
	OpNE  Operator = 289 // NE
	OpLT  Operator = 290 // LT
	OpLE  Operator = 291 // LE
	OpGT  Operator = 292 // GT
	OpGE  Operator = 293 // GE
	OpREQ Operator = 294 // REQ (Regular expression equals)
	OpRNE Operator = 295 // RNE (Regular expression not equals)

	// Unary Operators
	OpUMinus Operator = 296 // UMINUS
)

func (Operator) String

func (op Operator) String() string

String returns the string representation of an OperatorType

type SyntaxError

type SyntaxError struct {
	Message  string
	Position int
	Context  string
	Input    string
}

SyntaxError represents a TSL parsing error with context

func (*SyntaxError) Error

func (e *SyntaxError) Error() string

Error implements the error interface

type TSLArrayLiteral

type TSLArrayLiteral struct {
	Values []*TSLNode
}

TSLArrayLiteral represents an array of nodes

type TSLExpressionOp

type TSLExpressionOp struct {
	Operator    Operator
	Left, Right *TSLNode
}

TSLExpressionOp represents both unary and binary operations

type TSLNode

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

TSLNode represents an tree search language AST (Abstract Syntax Tree) node

func ParseTSL

func ParseTSL(input string) (*TSLNode, error)

ParseTSL parses a TSL expression and returns the AST root node

func (*TSLNode) AttachChild

func (n *TSLNode) AttachChild(child *TSLNode) bool

AttachChild safely attaches a child node to a unary expression node

func (*TSLNode) AttachLeft

func (n *TSLNode) AttachLeft(child *TSLNode) bool

AttachLeft safely attaches a child node as the left child of a binary expression node

func (*TSLNode) AttachRight

func (n *TSLNode) AttachRight(child *TSLNode) bool

AttachRight safely attaches a child node as the right child of a binary expression node

func (*TSLNode) Clone

func (n *TSLNode) Clone() *TSLNode

Clone creates a deep copy of the TSLNode and its children

func (*TSLNode) DetachChild

func (n *TSLNode) DetachChild() *TSLNode

DetachChild safely detaches and returns the child of a unary expression node

func (*TSLNode) DetachLeft

func (n *TSLNode) DetachLeft() *TSLNode

DetachLeft safely detaches and returns the left child of a binary expression node

func (*TSLNode) DetachRight

func (n *TSLNode) DetachRight() *TSLNode

DetachRight safely detaches and returns the right child of a binary expression node

func (*TSLNode) Free

func (n *TSLNode) Free()

Free releases the memory allocated for the AST

func (*TSLNode) MarshalJSON

func (n *TSLNode) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler interface

func (*TSLNode) MarshalYAML

func (n *TSLNode) MarshalYAML() (interface{}, error)

MarshalYAML implements yaml.Marshaler interface

func (*TSLNode) Type

func (n *TSLNode) Type() Kind

Type returns the type of the node

func (*TSLNode) Value

func (n *TSLNode) Value() interface{}

Value returns the node's value based on its type

type TypeMismatchError

type TypeMismatchError struct {
	Expected string
	Got      interface{}
}

TypeMismatchError is returned when operand types don't match the operator

func (TypeMismatchError) Error

func (e TypeMismatchError) Error() string

type UnexpectedLiteralError

type UnexpectedLiteralError struct {
	Literal interface{}
}

UnexpectedLiteralError is returned when encountering an unexpected literal or operator

func (UnexpectedLiteralError) Error

func (e UnexpectedLiteralError) Error() string

type UnexpectedOperatorError

type UnexpectedOperatorError struct {
	Operator interface{}
}

UnexpectedOperatorError is returned when encountering an unexpected operator

func (UnexpectedOperatorError) Error

func (e UnexpectedOperatorError) Error() string

type UnexpectedTypeError

type UnexpectedTypeError struct {
	Type interface{}
}

UnexpectedTypeError is returned when encountering an unexpected type in the AST

func (UnexpectedTypeError) Error

func (e UnexpectedTypeError) Error() string

Jump to

Keyboard shortcuts

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