parser

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2025 License: BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const CTBool = xl.CTBool
View Source
const CTEmpty = xl.CTEmpty
View Source
const CTFormula = xl.CTFormula
View Source
const CTNumber = xl.CTNumber
View Source
const CTString = xl.CTString

Variables

View Source
var IsCommutative = map[string]bool{

	" ": true,
	",": false,

	"^": false,

	"*": true,
	"/": false,

	"+": true,
	"-": false,

	"&": false,

	"=":  true,
	"<>": true,
	"<=": false,
	">=": false,
	">":  false,
	"<":  false,
}

IsCommutative is a map of binary operators to whether they are commutative. True if A OP B == B OP A False if A OP B != B OP A

View Source
var PrecedenceMap = map[string]int{

	" ": 8,
	",": 8,

	"^": 5,

	"*": 4,
	"/": 4,

	"+": 3,
	"-": 3,

	"&": 2,

	"=":  1,
	"<>": 1,
	"<=": 1,
	">=": 1,
	">":  1,
	"<":  1,
}

PrecedenceMap is a map of binary operators to their precedence. It lets us know which operators should be evaluated first.

Functions

This section is empty.

Types

type AnyClosure

type AnyClosure func() error

type BinaryExpressionNode

type BinaryExpressionNode struct {
	Operator string `json:"operator"`
	Left     Node   `json:"left"`
	Right    Node   `json:"right"`
}

func (BinaryExpressionNode) Children

func (b BinaryExpressionNode) Children() []Node

func (BinaryExpressionNode) IsEq

func (b BinaryExpressionNode) IsEq(node Node) bool

func (BinaryExpressionNode) Type

func (b BinaryExpressionNode) Type() NodeType

type CType

type CType = xl.CType

type CVal

type CVal = xl.CVal

type Cell

type Cell = xl.Cell

type CellNode

type CellNode struct {
	Cell Cell `json:"cell"`
}

func (CellNode) Children

func (c CellNode) Children() []Node

func (CellNode) IsEq

func (c CellNode) IsEq(n Node) bool

func (CellNode) Type

func (c CellNode) Type() NodeType

type CellRangeNode

type CellRangeNode struct {
	// Make sure that start and end are in the same sheet!
	Start CellNode `json:"startCell"`
	End   CellNode `json:"endCell"`
}

func (CellRangeNode) Children

func (c CellRangeNode) Children() []Node

func (CellRangeNode) IsEq

func (c CellRangeNode) IsEq(n Node) bool

func (CellRangeNode) Range

func (c CellRangeNode) Range() Range

func (CellRangeNode) Type

func (c CellRangeNode) Type() NodeType

type Context

type Context struct {
	// Sheet the formula is located in
	CurrentSheet string
}

type Formula

type Formula = xl.Formula

func ShiftFormula deprecated

func ShiftFormula(f Formula, rowDiff int, colDiff int, sheetName string) (Formula, error)

Shifts a formula from one cell to another, inside the same sheet.

Deprecated: This is very slow, since we now do it in three steps: 1. Parse the formula into a tree. 2. Shift the tree. 3. Serialize the tree back into a formula. Use Parse/ShiftNode/MoveNode/StringifyNode instead depending on your needs.

func StringifyNode

func StringifyNode(n Node, sheetName string) Formula

type FunctionNode

type FunctionNode struct {
	Name      string `json:"name"`
	Arguments []Node `json:"arguments"`
}

func (FunctionNode) Children

func (c FunctionNode) Children() []Node

func (FunctionNode) IsEq

func (f FunctionNode) IsEq(n Node) bool

func (FunctionNode) Type

func (f FunctionNode) Type() NodeType

type LogicalNode

type LogicalNode struct {
	Value bool `json:"value"`
}

func (LogicalNode) Children

func (l LogicalNode) Children() []Node

func (LogicalNode) IsEq

func (l LogicalNode) IsEq(node Node) bool

func (LogicalNode) String

func (l LogicalNode) String() string

func (LogicalNode) Type

func (l LogicalNode) Type() NodeType

type Node

type Node interface {
	Type() NodeType
	IsEq(Node) bool
	Children() []Node
}

Node is the interface that all nodes in the AST implement. It represent a node such as a SUM function, a cell reference, a + binary expression, etc.

func BuildTree

func BuildTree(ctx Context, tokens []Token) (Node, error)

BuildTree takes a slice of tokens and returns a Node representing the formula root. The context parameter is used to resolve relative cell references, and contains the current sheet name, and other context information necessary.

func MoveNode

func MoveNode(n Node, origin Cell, dest Cell) (Node, error)

func Parse

func Parse(formula string, currentSheet string) (Node, error)

Parse takes a formula string and returns a Node representing the formula root. The currentSheet parameter is used to resolve relative cell references.

func ShiftNode

func ShiftNode(n Node, shiftRow int, shiftCol int) (Node, error)

type NodeJSON

type NodeJSON struct {
	Type  string `json:"type"`
	Value any    `json:"value"`
}

func ToNodeJson

func ToNodeJson(n Node) NodeJSON

type NodeType

type NodeType uint8
const (
	NodeTypeCell NodeType = iota
	NodeTypeCellRange
	NodeTypeFunction
	NodeTypeBinaryExpression
	NodeTypeUnaryExpression
	NodeTypeNumber
	NodeTypeText
	NodeTypeLogical
)

func (NodeType) IsTerminal

func (NodeType NodeType) IsTerminal() bool

func (NodeType) String

func (nodeType NodeType) String() string

type NumberNode

type NumberNode struct {
	Value float64 `json:"value"`
}

func (NumberNode) Children

func (n NumberNode) Children() []Node

func (NumberNode) IsEq

func (n NumberNode) IsEq(node Node) bool

func (NumberNode) String

func (n NumberNode) String() string

func (NumberNode) Type

func (n NumberNode) Type() NodeType

type Range

type Range = xl.Range

type RawSheet

type RawSheet = xl.RawSheet

type Sheet

type Sheet = xl.Sheet

type ShuntingYard

type ShuntingYard = shuntingyard.ShuntingYardState[Node]

type TextNode

type TextNode struct {
	Value string `json:"value"`
}

func (TextNode) Children

func (t TextNode) Children() []Node

func (TextNode) IsEq

func (t TextNode) IsEq(node Node) bool

func (TextNode) String

func (t TextNode) String() string

func (TextNode) Type

func (t TextNode) Type() NodeType

type Token

type Token struct {
	Type    string
	Subtype string
	Value   string
}

func Tokenize

func Tokenize(formula string) []Token

Tokenizes a formula string into a slice of tokens, for later parsing into a tree.

type TokenStream

type TokenStream interface {
	Consume() error
	GetNext() Token
	NextIs(ttype string, tsubtype string) bool
	NextIsOpenParen() bool
	NextIsTerminal() bool
	NextIsFunctionCall() bool
	NextIsFunctionArgumentSeparator() bool
	NextIsEndOfFunctionCall() bool
	NextIsBinaryOperator() bool
	NextIsPrefixOperator() bool
	NextIsPostfixOperator() bool
	NextIsRange() bool
	NextIsCell() bool
	NextIsNumber() bool
	NextIsText() bool
	NextIsLogical() bool
	Position() int
}

func NewTokenStream

func NewTokenStream(tokens []Token) TokenStream

type TokenStreamImpl

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

func (*TokenStreamImpl) Consume

func (ts *TokenStreamImpl) Consume() error

func (*TokenStreamImpl) GetNext

func (ts *TokenStreamImpl) GetNext() Token

func (*TokenStreamImpl) NextIs

func (ts *TokenStreamImpl) NextIs(ttype string, tsubtype string) bool

func (*TokenStreamImpl) NextIsBinaryOperator

func (ts *TokenStreamImpl) NextIsBinaryOperator() bool

func (*TokenStreamImpl) NextIsCell

func (ts *TokenStreamImpl) NextIsCell() bool

func (*TokenStreamImpl) NextIsEndOfFunctionCall

func (ts *TokenStreamImpl) NextIsEndOfFunctionCall() bool

func (*TokenStreamImpl) NextIsFunctionArgumentSeparator

func (ts *TokenStreamImpl) NextIsFunctionArgumentSeparator() bool

func (*TokenStreamImpl) NextIsFunctionCall

func (ts *TokenStreamImpl) NextIsFunctionCall() bool

func (*TokenStreamImpl) NextIsLogical

func (ts *TokenStreamImpl) NextIsLogical() bool

func (*TokenStreamImpl) NextIsNumber

func (ts *TokenStreamImpl) NextIsNumber() bool

func (*TokenStreamImpl) NextIsOpenParen

func (ts *TokenStreamImpl) NextIsOpenParen() bool

func (*TokenStreamImpl) NextIsPostfixOperator

func (ts *TokenStreamImpl) NextIsPostfixOperator() bool

func (*TokenStreamImpl) NextIsPrefixOperator

func (ts *TokenStreamImpl) NextIsPrefixOperator() bool

func (*TokenStreamImpl) NextIsRange

func (ts *TokenStreamImpl) NextIsRange() bool

func (*TokenStreamImpl) NextIsTerminal

func (ts *TokenStreamImpl) NextIsTerminal() bool

func (*TokenStreamImpl) NextIsText

func (ts *TokenStreamImpl) NextIsText() bool

func (*TokenStreamImpl) Position

func (ts *TokenStreamImpl) Position() int

type UnaryExpressionNode

type UnaryExpressionNode struct {
	Operator string `json:"operator"`
	Operand  Node   `json:"operand"`
}

func (UnaryExpressionNode) Children

func (u UnaryExpressionNode) Children() []Node

func (UnaryExpressionNode) IsEq

func (u UnaryExpressionNode) IsEq(node Node) bool

func (UnaryExpressionNode) Type

func (u UnaryExpressionNode) Type() NodeType

type ValueNode

type ValueNode interface {
	Node
	String() string
}

type Workbook

type Workbook = xl.Workbook

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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