parser

package
v0.1.0 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: 8 Imported by: 0

Documentation

Overview

Package parser provides a parser and an Abstract Syntax Tree (AST) for the Pipeline Query Language.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SplitStatements

func SplitStatements(source string) []string

SplitStatements splits the given string by semicolons.

func Walk

func Walk(n Node, visit func(n Node) bool)

Walk traverses an AST in depth-first order. If the visit function returns true for a node, the visit function will be called for its children.

Types

type AsOperator

type AsOperator struct {
	Pipe    Span
	Keyword Span
	Name    *Ident
}

AsOperator represents a `| as` operator in a TabularExpr. It implements TabularOperator.

func (*AsOperator) Span

func (op *AsOperator) Span() Span

type BasicLit

type BasicLit struct {
	ValueSpan Span
	Kind      TokenKind // [TokenNumber] or [TokenString]
	Value     string
}

A BasicLit node represents a numeric or string literal.

func (*BasicLit) Float64

func (lit *BasicLit) Float64() float64

Float64 returns the numeric value of the literal as an unsigned integer. It returns 0 if the literal's kind is not TokenNumber.

func (*BasicLit) IsFloat

func (lit *BasicLit) IsFloat() bool

IsFloat reports whether the literal is a floating point literal.

func (*BasicLit) IsInteger

func (lit *BasicLit) IsInteger() bool

IsInteger reports whether the literal is a integer literal.

func (*BasicLit) Span

func (lit *BasicLit) Span() Span

func (*BasicLit) Uint64

func (lit *BasicLit) Uint64() uint64

Uint64 returns the numeric value of the literal as an unsigned integer. It returns 0 if the literal's kind is not TokenNumber.

type BinaryExpr

type BinaryExpr struct {
	X      Expr
	OpSpan Span
	Op     TokenKind
	Y      Expr
}

A BinaryExpr represents a binary expression.

func (*BinaryExpr) Span

func (expr *BinaryExpr) Span() Span

type CallExpr

type CallExpr struct {
	Func   *Ident
	Lparen Span
	Args   []Expr
	Rparen Span
}

A CallExpr node represents an unquoted identifier followed by an argument list.

func (*CallExpr) Span

func (call *CallExpr) Span() Span

type CountOperator

type CountOperator struct {
	Pipe    Span
	Keyword Span
}

CountOperator represents a `| count` operator in a TabularExpr. It implements TabularOperator.

func (*CountOperator) Span

func (op *CountOperator) Span() Span

type Expr

type Expr interface {
	Node
	// contains filtered or unexported methods
}

Expr is the interface implemented by all expression AST node types.

type Ident

type Ident struct {
	Name     string
	NameSpan Span

	// Quoted is true if the identifier is quoted.
	Quoted bool
}

An Ident node represents an identifier.

Ident does not implement Expr, but QualifiedIdent does. You can use *Ident.AsQualified to convert an *Ident to a *QualifiedIdent.

func (*Ident) AsQualified

func (id *Ident) AsQualified() *QualifiedIdent

AsQualified converts the identifier to a QualifiedIdent with a single part.

func (*Ident) Span

func (id *Ident) Span() Span

type InExpr

type InExpr struct {
	X      Expr
	In     Span
	Lparen Span
	Vals   []Expr
	Rparen Span
}

An InExpr represents an "in" operator expression.

func (*InExpr) Span

func (expr *InExpr) Span() Span

type IndexExpr

type IndexExpr struct {
	X      Expr
	Lbrack Span
	Index  Expr
	Rbrack Span
}

An IndexExpr node represents an array or map index.

func (*IndexExpr) Span

func (idx *IndexExpr) Span() Span

type JoinOperator

type JoinOperator struct {
	Pipe    Span
	Keyword Span

	Kind       Span
	KindAssign Span
	// Flavor is the type of join to use.
	// If absent, innerunique is implied.
	Flavor *Ident

	Lparen Span
	Right  *TabularExpr
	Rparen Span

	On Span
	// Conditions is one or more AND-ed conditions.
	// If the expression is a single identifier x,
	// then it is treated as equivalent to "$left.x == $right.x".
	Conditions []Expr
}

JoinOperator represents a `| join` operator in a TabularExpr. It implements TabularOperator.

func (*JoinOperator) Span

func (op *JoinOperator) Span() Span

type Node

type Node interface {
	Span() Span
}

Node is the interface implemented by all AST node types.

type ParenExpr

type ParenExpr struct {
	Lparen Span
	X      Expr
	Rparen Span
}

A ParenExpr represents a parenthized expression.

func (*ParenExpr) Span

func (expr *ParenExpr) Span() Span

type ProjectColumn

type ProjectColumn struct {
	Name   *Ident
	Assign Span
	X      Expr
}

A ProjectColumn is a single column term in a ProjectOperator. It consists of a column name, optionally followed by an expression specifying how to compute the column. If the expression is omitted, it is equivalent to using the Name as the expression.

func (*ProjectColumn) Span

func (op *ProjectColumn) Span() Span

type ProjectOperator

type ProjectOperator struct {
	Pipe    Span
	Keyword Span
	Cols    []*ProjectColumn
}

ProjectOperator represents a `| project` operator in a TabularExpr. It implements TabularOperator.

func (*ProjectOperator) Span

func (op *ProjectOperator) Span() Span

type QualifiedIdent

type QualifiedIdent struct {
	Parts []*Ident
}

A QualifiedIdent is one or more dot-separated identifiers.

func (*QualifiedIdent) Span

func (id *QualifiedIdent) Span() Span

type SortOperator

type SortOperator struct {
	Pipe    Span
	Keyword Span
	Terms   []*SortTerm
}

SortOperator represents a `| sort by` operator in a TabularExpr. It implements TabularOperator.

func (*SortOperator) Span

func (op *SortOperator) Span() Span

type SortTerm

type SortTerm struct {
	X           Expr
	Asc         bool
	AscDescSpan Span
	NullsFirst  bool
	NullsSpan   Span
}

SortTerm is a single sort constraint in the SortOperator.

func (*SortTerm) Span

func (term *SortTerm) Span() Span

type Span

type Span struct {
	// Start is the index of the first byte of the span,
	// relative to the beginning of the query.
	Start int
	// End is the end index of the span (exclusive),
	// relative to the beginning of the query.
	End int
}

A Span is a reference contiguous sequence of bytes in a query.

func (Span) IsValid

func (span Span) IsValid() bool

IsValid reports whether the span has a non-negative length and non-negative indices.

func (Span) Len

func (span Span) Len() int

Len returns the length of the span or zero if the span is invalid.

func (Span) String

func (span Span) String() string

String formats the span indices as a mathematical range like "[12,34)".

type SummarizeColumn

type SummarizeColumn struct {
	Name   *Ident
	Assign Span
	X      Expr
}

A SummarizeColumn is a single column term in a SummarizeOperator. It consists of an expression, optionally preceded by a column name. If the column name is omitted, one is derived from the expression.

func (*SummarizeColumn) Span

func (op *SummarizeColumn) Span() Span

type SummarizeOperator

type SummarizeOperator struct {
	Pipe    Span
	Keyword Span
	Cols    []*SummarizeColumn
	By      Span
	GroupBy []*SummarizeColumn
}

SummarizeOperator represents a `| summarize` operator in a TabularExpr. It implements TabularOperator.

func (*SummarizeOperator) Span

func (op *SummarizeOperator) Span() Span

type TableRef

type TableRef struct {
	Table *Ident
}

A TableRef node refers to a specific table. It implements TabularDataSource.

func (*TableRef) Span

func (ref *TableRef) Span() Span

type TabularDataSource

type TabularDataSource interface {
	Node
	// contains filtered or unexported methods
}

TabularDataSource is the interface implemented by all AST node types that can be used as the data source of a TabularExpr. At the moment, this can only be a TableRef.

type TabularExpr

type TabularExpr struct {
	Source    TabularDataSource
	Operators []TabularOperator
}

TabularExpr is a query expression that produces a table.

func Parse

func Parse(query string) (*TabularExpr, error)

Parse converts a Pipeline Query Language tabular expression into an Abstract Syntax Tree (AST).

func (*TabularExpr) Span

func (x *TabularExpr) Span() Span

type TabularOperator

type TabularOperator interface {
	Node
	// contains filtered or unexported methods
}

TabularOperator is the interface implemented by all AST node types that can be used as operators in a TabularExpr.

type TakeOperator

type TakeOperator struct {
	Pipe     Span
	Keyword  Span
	RowCount Expr
}

TakeOperator represents a `| take` operator in a TabularExpr. It implements TabularOperator.

func (*TakeOperator) Span

func (op *TakeOperator) Span() Span

type Token

type Token struct {
	// Kind is the token's type.
	Kind TokenKind
	// Span holds the location of the token.
	Span Span
	// Value contains kind-specific information about the token.
	// See the docs for [TokenKind] for what Value represents.
	Value string
}

Token is a syntactical element in a query.

func Scan

func Scan(query string) []Token

Scan turns a Pipeline Query Language statement into a sequence of Token values. Errors will be indicated with the TokenError kind.

type TokenKind

type TokenKind int

TokenKind is an enumeration of types of Token that can be returned by Scan.

const (
	// TokenIdentifier is a plain identifier
	// that might be a keyword, depending on position.
	// The Value will be the identifier itself.
	TokenIdentifier TokenKind = 1 + iota
	// TokenQuotedIdentifier is an identifier
	// surrounded by backticks
	// The Value will be the content between the backticks
	// with any double backticks reduced.
	TokenQuotedIdentifier
	// TokenNumber is a numeric literal like "123", "3.14", "1e-9", or "0xdeadbeef".
	// The Value will be a decimal formatted string.
	TokenNumber
	// TokenString is a string literal enclosed by single or double quotes.
	// The Value will be the literal's value (i.e. any escape sequences are evaluated).
	TokenString

	// TokenAnd is the keyword "and".
	// The Value will be the empty string.
	TokenAnd
	// TokenOr is the keyword "or".
	// The Value will be the empty string.
	TokenOr

	// TokenPipe is a single pipe character ("|").
	// The Value will be the empty string.
	TokenPipe
	// TokenDot is a period character (".").
	// The Value will be the empty string.
	TokenDot
	// TokenDot is a comma character (",").
	// The Value will be the empty string.
	TokenComma
	// TokenPlus is a single plus character ("+").
	// The Value will be the empty string.
	TokenPlus
	// TokenMinus is a single hyphen character ("-").
	// The Value will be the empty string.
	TokenMinus
	// TokenStar is a single asterisk character ("*").
	// The Value will be the empty string.
	TokenStar
	// TokenSlash is a single forward slash character ("/").
	// The Value will be the empty string.
	TokenSlash
	// TokenMod is a single percent sign character ("%").
	// The Value will be the empty string.
	TokenMod
	// TokenAssign is a single equals sign character ("=").
	// The Value will be the empty string.
	TokenAssign
	// TokenEq is a sequence of two equals sign characters ("==").
	// The Value will be the empty string.
	TokenEq
	// TokenNE is the sequence "!=", representing an inequality test.
	// The Value will be the empty string.
	TokenNE
	// TokenLT is the less than symbol ("<").
	// The Value will be the empty string.
	TokenLT
	// TokenLE is the less than or equal sequence "<=".
	// The Value will be the empty string.
	TokenLE
	// TokenGT is the greater than symbol (">").
	// The Value will be the empty string.
	TokenGT
	// TokenGE is the greater than or equal sequence ">=".
	// The Value will be the empty string.
	TokenGE
	// TokenCaseInsensitiveEq is the sequence "=~".
	// The Value will be the empty string.
	TokenCaseInsensitiveEq
	// TokenCaseInsensitiveNE is the sequence "!~".
	// The Value will be the empty string.
	TokenCaseInsensitiveNE

	// TokenLParen is a left parenthesis.
	// The Value will be the empty string.
	TokenLParen
	// TokenRParen is a right parenthesis.
	// The Value will be the empty string.
	TokenRParen

	// TokenLBracket is a left bracket ("[").
	// The Value will be the empty string.
	TokenLBracket
	// TokenRBracket is a right bracket ("]").
	// The Value will be the empty string.
	TokenRBracket

	// TokenBy is the keyword "in".
	// The Value will be the empty string.
	TokenIn
	// TokenBy is the keyword "by".
	// The Value will be the empty string.
	TokenBy

	// TokenSemi is the semicolon character (";").
	// The Value will be the empty string.
	TokenSemi

	// TokenError is a marker for a scan error.
	// The Value will contain the error message.
	TokenError TokenKind = -1
)

Token kinds.

func (TokenKind) String

func (i TokenKind) String() string

type TopOperator

type TopOperator struct {
	Pipe     Span
	Keyword  Span
	RowCount Expr
	By       Span
	Col      *SortTerm
}

TopOperator represents a `| top` operator in a TabularExpr. It implements TabularOperator.

func (*TopOperator) Span

func (op *TopOperator) Span() Span

type UnaryExpr

type UnaryExpr struct {
	OpSpan Span
	Op     TokenKind
	X      Expr
}

A UnaryExpr represents a unary expression.

func (*UnaryExpr) Span

func (expr *UnaryExpr) Span() Span

type WhereOperator

type WhereOperator struct {
	Pipe      Span
	Keyword   Span
	Predicate Expr
}

WhereOperator represents a `| where` operator in a TabularExpr. It implements TabularOperator.

func (*WhereOperator) Span

func (op *WhereOperator) Span() Span

Jump to

Keyboard shortcuts

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