parse

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: May 12, 2020 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package parse builds parse trees for expressions as defined by expr. Clients should use that package to construct expressions rather than this one, which provides shared internal data structures not intended for general use.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Walk

func Walk(n Node, f func(Node))

Walk invokes f on n and sub-nodes of n.

Types

type BinaryNode

type BinaryNode struct {
	NodeType
	Pos
	Args     [2]Node
	Operator item
	OpStr    string
}

BinaryNode holds two arguments and an operator.

func (*BinaryNode) Check

func (b *BinaryNode) Check(t *Tree) error

Check performs parse time checking on the BinaryNode so it fulfills the Node interface.

func (*BinaryNode) Return

func (b *BinaryNode) Return() ReturnType

Return returns the result type of the BinaryNode so it fulfills the Node interface.

func (*BinaryNode) String

func (b *BinaryNode) String() string

String returns the string representation of the BinaryNode so it fulfills the Node interface.

func (*BinaryNode) StringAST

func (b *BinaryNode) StringAST() string

StringAST returns the string representation of abstract syntax tree of the BinaryNode so it fulfills the Node interface.

type Func

type Func struct {
	Args          []ReturnType
	Return        ReturnType
	F             interface{}
	VariantReturn bool
	Check         func(*Tree, *FuncNode) error
}

Func holds the structure of a parsed function call.

type FuncNode

type FuncNode struct {
	NodeType
	Pos
	Name   string
	F      *Func
	Args   []Node
	Prefix string
}

FuncNode holds a function invocation.

func (*FuncNode) Check

func (f *FuncNode) Check(t *Tree) error

Check performs parse time checking on the FuncNode so it fulfills the Node interface.

func (*FuncNode) Return

func (f *FuncNode) Return() ReturnType

Return returns the result type of the FuncNode so it fulfills the Node interface.

func (*FuncNode) String

func (f *FuncNode) String() string

String returns the string representation of the FuncNode so it fulfills the Node interface.

func (*FuncNode) StringAST

func (f *FuncNode) StringAST() string

StringAST returns the string representation of abstract syntax tree of the FuncNode so it fulfills the Node interface.

type Node

type Node interface {
	Type() NodeType
	String() string
	StringAST() string
	Position() Pos     // byte position of start of node in full original input string
	Check(*Tree) error // performs type checking for itself and sub-nodes
	Return() ReturnType
	// contains filtered or unexported methods
}

A Node is an element in the parse tree. The interface is trivial. The interface contains an unexported method so that only types local to this package can satisfy it.

type NodeType

type NodeType int

NodeType identifies the type of a parse tree node.

const (
	// NodeFunc is a function call.
	NodeFunc NodeType = iota
	// NodeBinary is a binary operator: math, logical, compare
	NodeBinary
	// NodeUnary is unary operator: !, -
	NodeUnary
	// NodeString is string constant.
	NodeString
	// NodeNumber is a numerical constant (Scalar).
	NodeNumber
	// NodeVar is variable: $A
	NodeVar
)

func (NodeType) String

func (t NodeType) String() string

String returns the string representation of the NodeType

func (NodeType) Type

func (t NodeType) Type() NodeType

Type returns itself and provides an easy default implementation for embedding in a Node. Embedded in all non-trivial Nodes.

type Pos

type Pos int

Pos represents a byte position in the original input text from which this template was parsed.

func (Pos) Position

func (p Pos) Position() Pos

Position returns the integer Position of p

type ReturnType

type ReturnType int

ReturnType represents the type that is returned from a node.

const (
	// TypeString is a single string.
	TypeString ReturnType = iota
	// TypeScalar is a unlabled number constant.
	TypeScalar
	// TypeNumberSet is a collection of labelled numbers.
	TypeNumberSet
	// TypeSeriesSet is a collection of labelled time series.
	TypeSeriesSet
	// TypeVariantSet is a collection of the same type Number, Series, or Scalar.
	TypeVariantSet
)

func (ReturnType) String

func (f ReturnType) String() string

String returns a string representation of the ReturnType.

type ScalarNode

type ScalarNode struct {
	NodeType
	Pos
	IsUint  bool    // Number has an unsigned integral value.
	IsFloat bool    // Number has a floating-point value.
	Uint64  uint64  // The unsigned integer value.
	Float64 float64 // The floating-point value.
	Text    string  // The original textual representation from the input.
}

ScalarNode holds a number: signed or unsigned integer or float. The value is parsed and stored under all the types that can represent the value. This simulates in a small amount of code the behavior of Go's ideal constants.

func (*ScalarNode) Check

func (n *ScalarNode) Check(*Tree) error

Check performs parse time checking on the ScalarNode so it fulfills the Node interface.

func (*ScalarNode) Return

func (n *ScalarNode) Return() ReturnType

Return returns the result type of the ScalarNode so it fulfills the Node interface.

func (*ScalarNode) String

func (n *ScalarNode) String() string

String returns the string representation of the ScalarNode so it fulfills the Node interface.

func (*ScalarNode) StringAST

func (n *ScalarNode) StringAST() string

StringAST returns the string representation of abstract syntax tree of the ScalarNode so it fulfills the Node interface.

type StringNode

type StringNode struct {
	NodeType
	Pos
	Quoted string // The original text of the string, with quotes.
	Text   string // The string, after quote processing.
}

StringNode holds a string constant. The value has been "unquoted".

func (*StringNode) Check

func (s *StringNode) Check(*Tree) error

Check performs parse time checking on the StringNode so it fulfills the Node interface.

func (*StringNode) Return

func (s *StringNode) Return() ReturnType

Return returns the result type of the TypeString so it fulfills the Node interface.

func (*StringNode) String

func (s *StringNode) String() string

String returns the string representation of the StringNode so it fulfills the Node interface.

func (*StringNode) StringAST

func (s *StringNode) StringAST() string

StringAST returns the string representation of abstract syntax tree of the StringNode so it fulfills the Node interface.

type Tree

type Tree struct {
	Text     string // text parsed to create the expression.
	Root     Node   // top-level root of the tree, returns a number.
	VarNames []string
	// contains filtered or unexported fields
}

Tree is the representation of a single parsed expression.

func New

func New(funcs ...map[string]Func) *Tree

New allocates a new parse tree with the given name.

func Parse

func Parse(text string, funcs ...map[string]Func) (t *Tree, err error)

Parse returns a Tree, created by parsing the expression described in the argument string. If an error is encountered, parsing stops and an empty Tree is returned with the error.

func (*Tree) A

func (t *Tree) A() Node

A is C {"&&" C} in the grammar.

func (*Tree) C

func (t *Tree) C() Node

C is C -> P {( "==" | "!=" | ">" | ">=" | "<" | "<=") P} in the grammar.

func (*Tree) E

func (t *Tree) E() Node

E is F {( "**" ) F} in the grammar.

func (*Tree) F

func (t *Tree) F() Node

F is v | "(" O ")" | "!" O | "-" O in the grammar.

func (*Tree) Func

func (t *Tree) Func() (f *FuncNode)

Func parses a FuncNode.

func (*Tree) GetFunction

func (t *Tree) GetFunction(name string) (v Func, ok bool)

GetFunction gets a parsed Func from the functions available on the tree's func property.

func (*Tree) M

func (t *Tree) M() Node

M is E {( "*" | "/" ) F} in the grammar.

func (*Tree) O

func (t *Tree) O() Node

O is A {"||" A} in the grammar.

func (*Tree) P

func (t *Tree) P() Node

P is M {( "+" | "-" ) M} in the grammar.

func (*Tree) Parse

func (t *Tree) Parse(text string, funcs ...map[string]Func) (err error)

Parse parses the expression definition string to construct a representation of the expression for execution.

func (*Tree) String

func (t *Tree) String() string

String returns a string representation of the parse tree.

func (*Tree) Var

func (t *Tree) Var() (v *VarNode)

Var is queryVar in the grammar.

type UnaryNode

type UnaryNode struct {
	NodeType
	Pos
	Arg      Node
	Operator item
	OpStr    string
}

UnaryNode holds one argument and an operator.

func (*UnaryNode) Check

func (u *UnaryNode) Check(t *Tree) error

Check performs parse time checking on the UnaryNode so it fulfills the Node interface.

func (*UnaryNode) Return

func (u *UnaryNode) Return() ReturnType

Return returns the result type of the UnaryNode so it fulfills the Node interface.

func (*UnaryNode) String

func (u *UnaryNode) String() string

String returns the string representation of the UnaryNode so it fulfills the Node interface.

func (*UnaryNode) StringAST

func (u *UnaryNode) StringAST() string

StringAST returns the string representation of abstract syntax tree of the UnaryNode so it fulfills the Node interface.

type VarNode

type VarNode struct {
	NodeType
	Pos
	Name string // Without the $
	Text string // Raw
}

VarNode holds a variable reference.

func (*VarNode) Check

func (n *VarNode) Check(*Tree) error

Check performs parse time checking on the VarNode so it fulfills the Node interface.

func (*VarNode) Return

func (n *VarNode) Return() ReturnType

Return returns the result type of the VarNode so it fulfills the Node interface.

func (*VarNode) String

func (n *VarNode) String() string

String returns the string representation of the VarNode so it fulfills the Node interface.

func (*VarNode) StringAST

func (n *VarNode) StringAST() string

StringAST returns the string representation of abstract syntax tree of the VarNode so it fulfills the Node interface.

func (*VarNode) Type

func (n *VarNode) Type() NodeType

Type returns the Type of the VarNode so it fulfills the Node interface.

Jump to

Keyboard shortcuts

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