ast

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package ast is designed to tokenizer and parse jsonpath expressions. The module contains representations of the components of the abstract tree representation of use of the grammar.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ArgumentList added in v0.3.0

type ArgumentList struct {
	Arguments []Node
}

ArgumentList is a list of expressions being used to specify values to input into function parameters.

func (*ArgumentList) GetChild added in v0.3.0

func (l *ArgumentList) GetChild(index int) (Node, error)

func (*ArgumentList) NumChildren added in v0.3.0

func (l *ArgumentList) NumChildren() int

func (*ArgumentList) String added in v0.3.0

func (l *ArgumentList) String() string

String gives a comma-separated list of the arguments

type BinaryNode added in v0.3.0

type BinaryNode interface {
	Left() Node
	Right() Node
}

type BinaryOperation added in v0.3.0

type BinaryOperation struct {
	LeftNode  Node
	RightNode Node
	Operation MathOperationType
}

func (*BinaryOperation) Left added in v0.3.0

func (b *BinaryOperation) Left() Node

func (*BinaryOperation) Right added in v0.3.0

func (b *BinaryOperation) Right() Node

func (*BinaryOperation) String added in v0.3.0

func (b *BinaryOperation) String() string

String returns the left node, followed by the operator, followed by the right node. The left and right nodes are clarified with (), because context that determined order of operations, like parentheses, are not explicitly retained in the tree. But the structure of the tree represents the evaluation order present in the original expression.

type BooleanLiteral added in v0.3.0

type BooleanLiteral struct {
	BooleanValue bool
}

BooleanLiteral represents a boolean literal value in the abstract syntax tree. true or false

func (*BooleanLiteral) String added in v0.3.0

func (l *BooleanLiteral) String() string

String returns a string representation of the boolean contained.

func (*BooleanLiteral) Value added in v0.3.0

func (l *BooleanLiteral) Value() interface{}

Value returns the float contained.

type BracketAccessor

type BracketAccessor struct {
	LeftNode        Node
	RightExpression Node
}

BracketAccessor represents a part of the abstract syntax tree that is accessing the value at a key in a map/object, or index of a list. The format is the value to the left, followed by an open/right square bracket, followed by the key, followed by a close/left square bracket.

func (*BracketAccessor) Left

func (m *BracketAccessor) Left() Node

Left returns the node being accessed.

func (*BracketAccessor) Right

func (m *BracketAccessor) Right() Node

Right returns the key.

func (*BracketAccessor) String

func (m *BracketAccessor) String() string

String returns the string from the accessed node, followed by '[', followed by the string from the key, followed by ']'.

type DotNotation

type DotNotation struct {
	// The identifier on the right of the dot
	RightAccessIdentifier Node
	// The expression on the left could be one of several nodes.
	// I.e. An Identifier, a MapAccessor, or another DotNotation
	LeftAccessibleNode Node
}

DotNotation represents the access of an identifier in a node.

func (*DotNotation) Left

func (d *DotNotation) Left() Node

Left returns the left node being accessed.

func (*DotNotation) Right

func (d *DotNotation) Right() Node

Right returns the identifier being accessed in the left node.

func (*DotNotation) String

func (d *DotNotation) String() string

String returns the string representing the left node, followed by '.', followed by the string representing the right identifier.

type FloatLiteral added in v0.3.0

type FloatLiteral struct {
	FloatValue float64
}

FloatLiteral represents a floating point literal value in the abstract syntax tree.

func (*FloatLiteral) String added in v0.3.0

func (l *FloatLiteral) String() string

String returns a string representation of the float contained.

func (*FloatLiteral) Value added in v0.3.0

func (l *FloatLiteral) Value() interface{}

Value returns the float contained.

type FunctionCall added in v0.3.0

type FunctionCall struct {
	FuncIdentifier *Identifier
	ArgumentInputs *ArgumentList
}

FunctionCall represents a call to a function with 0 or more parameters.

func (*FunctionCall) Left added in v0.3.0

func (f *FunctionCall) Left() Node

Left returns nil, because an identifier does not branch left and right.

func (*FunctionCall) Right added in v0.3.0

func (f *FunctionCall) Right() Node

Right returns nil, because an identifier does not branch left and right.

func (*FunctionCall) String added in v0.3.0

func (f *FunctionCall) String() string

String returns the identifier name.

type Identifier

type Identifier struct {
	IdentifierName string
}

Identifier represents a valid identifier in the abstract syntax tree.

func (*Identifier) String

func (i *Identifier) String() string

String returns the identifier name.

type IntLiteral

type IntLiteral struct {
	IntValue int64
}

IntLiteral represents an integer literal value in the abstract syntax tree.

func (*IntLiteral) String

func (l *IntLiteral) String() string

String returns a string representation of the integer contained.

func (*IntLiteral) Value

func (l *IntLiteral) Value() interface{}

Value returns the integer contained.

type InvalidGrammarError

type InvalidGrammarError struct {
	FoundToken     *TokenValue
	ExpectedTokens []TokenID // Nil for end, no expected token
}

InvalidGrammarError represents when the order of tokens is not valid for the language.

func (*InvalidGrammarError) Error

func (e *InvalidGrammarError) Error() string

type InvalidTokenError

type InvalidTokenError struct {
	InvalidToken TokenValue
}

InvalidTokenError represents an error when the tokenizer doesn't recognise the token pattern. This is often caused by invalid characters, or characters in the wrong order.

func (*InvalidTokenError) Error

func (e *InvalidTokenError) Error() string

type MathOperationType added in v0.3.0

type MathOperationType int
const (
	Invalid MathOperationType = iota
	Add
	Subtract
	Multiply
	Divide
	Modulus
	Power
	EqualTo
	NotEqualTo
	GreaterThan
	LessThan
	GreaterThanEqualTo
	LessThanEqualTo
	And
	Or
	Not
)

func (MathOperationType) String added in v0.3.0

func (e MathOperationType) String() string

type NNode added in v0.3.0

type NNode interface {
	NumChildren() int
	GetChild(int) (Node, error)
}

type Node

type Node interface {
	String() string
}

Node represents any node in the abstract syntax tree. Left() and Right() can return nil for any node types that do not have left and right sides.

type Parser

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

Parser represents the object that handles parsing the grammar for the expression. Create this with the function expressions.InitParser This struct and its functions are used to parse the expression it was initialized with.

func InitParser

func InitParser(expression string, fileName string) (*Parser, error)

InitParser initializes the parser with the given raw expression.

func (*Parser) ParseExpression

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

ParseExpression is the correct entrypoint for parsing an expression. It advances to the first token, and parses the expression.

type StringLiteral

type StringLiteral struct {
	StrValue string
}

StringLiteral represents a string literal value in the abstract syntax tree.

func (*StringLiteral) String

func (l *StringLiteral) String() string

String returns the string surrounded by double quotes.

func (*StringLiteral) Value

func (l *StringLiteral) Value() interface{}

Value returns the string contained. It can be cast to a string.

type TokenID

type TokenID string

TokenID Represents the name of a type of token that has a pattern.

const (
	// IdentifierToken represents a token with any valid object name.
	IdentifierToken TokenID = "identifier"
	// StringLiteralToken represents a token that has a sequence of characters.
	// Supports the string format used in golang, and will include
	// the " before and after the contents of the string.
	// Characters can be escaped the common way with a backslash.
	StringLiteralToken    TokenID = "string"
	RawStringLiteralToken TokenID = "raw-string"
	// IntLiteralToken represents an integer token. Must not start with 0.
	IntLiteralToken TokenID = "int"
	// FloatLiteralToken represents a float token.
	FloatLiteralToken TokenID = "float"
	// BooleanLiteralToken represents true or false.
	BooleanLiteralToken TokenID = "boolean"
	// BracketAccessDelimiterStartToken represents the token before an object
	//  access. The '[' in 'obj["key"]'.
	//nolint:gosec
	BracketAccessDelimiterStartToken TokenID = "map-delimiter-start"
	// BracketAccessDelimiterEndToken represents the token before an object
	// access. The '[' in 'obj["key"]'.
	//nolint:gosec
	BracketAccessDelimiterEndToken TokenID = "map-delimiter-end"
	// ParenthesesStartToken represents the start token of an argument list or a parenthesized expression. '('
	ParenthesesStartToken TokenID = "parentheses-start"
	// ParenthesesEndToken represents the closing of the argument list. ')'
	ParenthesesEndToken TokenID = "parentheses-end"
	// DotObjectAccessToken represents the '.' token in 'a.b' (dot notation).
	DotObjectAccessToken TokenID = "object-access"
	// RootAccessToken represents the token that identifies accessing the
	// root object.
	RootAccessToken TokenID = "root-access"
	// CurrentObjectAccessToken represents the token, @, that identifies the current
	// object in a filter.
	CurrentObjectAccessToken TokenID = "current-object-access"
	// EqualsToken represents the token that represents a single equals sign.
	EqualsToken TokenID = "equals-sign"
	// SelectorToken Represents the ':' character used in selector expressions in bracket
	// object access.
	SelectorToken TokenID = "selector"
	// FilterToken represents the '?' used in filter expressions in bracket object access.
	FilterToken TokenID = "filter"
	// NegationToken represents a negation sign '-'.
	//nolint:gosec
	NegationToken TokenID = "negation-sign"
	// AsteriskToken represents a wildcard/multiplication token '*'.
	AsteriskToken TokenID = "asterisk"
	// ListSeparatorToken represents a comma in a parameter list
	ListSeparatorToken TokenID = "list-separator" //nolint:gosec // not a security credential
	// DivideToken represents the forward slash used to specify division.
	DivideToken TokenID = "divide"
	// GreaterThanToken represents a > symbol.
	GreaterThanToken TokenID = "greater-than"
	// LessThanToken represents a < symbol.
	LessThanToken TokenID = "less-than"
	// PlusToken represents a + symbol.
	PlusToken TokenID = "plus"
	// NotToken represents an ! symbol.
	NotToken TokenID = "not"
	// PowerToken represents a caret symbol for exponentiation.
	PowerToken TokenID = "power"
	// ModulusToken represents a percent symbol for remainder.
	ModulusToken TokenID = "mod"
	// AndToken represents logical-and &&
	AndToken TokenID = "and"
	// OrToken represents logical-or ||
	OrToken TokenID = "or"
	// UnknownToken is a placeholder for when there was an error in the token.
	UnknownToken TokenID = "error"
)

type TokenValue

type TokenValue struct {
	Value    string
	TokenID  TokenID
	Filename string
	Line     int
	Column   int
}

TokenValue represents the token parsed from the expression the tokenizer was initialized with. The line number and column is relative to the beginning of the expression. If part of a greater file, it's recommended that you offset those values to get the line and column within the file to prevent confusion.

type UnaryOperation added in v0.3.0

type UnaryOperation struct {
	LeftOperation MathOperationType
	RightNode     Node
}

func (*UnaryOperation) String added in v0.3.0

func (b *UnaryOperation) String() string

String returns the operation, followed by the string representation of the right node. The wrapped node is surrounded by parentheses to remove ambiguity.

type ValueLiteral

type ValueLiteral interface {
	Value() interface{}
	String() string
}

ValueLiteral represents any kind of literals that can be represented by the abstract syntax tree. Examples: ints, strings.

Jump to

Keyboard shortcuts

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