syntaxtree

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2025 License: MIT Imports: 4 Imported by: 1

README ΒΆ

🌲 go-syntax-tree

Go package

This package provides a way to construct a simple Syntax Tree for a certain query based on predefined Operators and Functions with there mutual priority. It will also parse and take into account grouping using () in the operator precedence.

After parsing and constructing the tree from a given query string, the SyntaxTree will consist of a Root Node. This one acts as a starting point to go over the tree in pre-, in- or post-order (ref: Tree Traversal). It will also contain a list of all Nodes in the SyntaxTree.

There is a .String() function that will print the SyntaxTree in dot file syntax. This can be used to write the SyntaxTree to a .dot file that can be parsed to an image to visualize the tree. (eg. ❯ dot -Tpng tree.dot > tree.png)

πŸ“‹ Example

import (
	"fmt"
	"regexp"

	syntaxtree "github.com/bramca/go-syntax-tree"
)

func firstExample() {
	query := "1-sqrt(pow(2,3)+1)*2/(sqrt(1+1)*pow(3+3,pow(3,sqrt(2))))"
	operatorsPrecedence := []string{
		"pow",
		"sqrt",
		"/",
		"*",
		"+",
		"-",
	}

	operators := []string{
		"*",
		"/",
		"+",
		"-",
	}

	binaryFunctions := []string{
		"pow",
	}

	binaryFunctionParsers := make([]syntaxtree.BinaryFunctionParser, len(binaryFunctions))
	for i, binaryFunction := range binaryFunctions {
		binaryFunctionParsers[i] = syntaxtree.BinaryFunctionParser{
			FunctionName:     binaryFunction,
			OpeningDelimiter: '(',
			ClosingDelimiter: ')',
			OperandSeparator: ',',
		}
	}

	unaryFunctions := []string{
		"sqrt",
	}

	unaryFunctionParsers :=  make([]syntaxtree.UnaryFunctionParser, len(unaryFunctions))
	for i, unaryFunction := range unaryFunctions {
		unaryFunctionParsers[i] = syntaxtree.UnaryFunctionParser{
			FunctionName:     unaryFunction,
			OpeningDelimiter: '(',
			ClosingDelimiter: ')',
		}
	}

	operatorParsers := make([]syntaxtree.OperatorParser, len(operators))
	for i, operator := range operators {
		operatorParsers[i] = syntaxtree.OperatorParser{
			OperatorString:  operator,
			OperatorPattern: regexp.MustCompile(fmt.Sprintf(`([\d\(\)]*)\%s([\d\(\)]*|pow|sqrt)`, operator)),
		}
	}

	syntaxTree := syntaxtree.SyntaxTree{
		OperatorPrecedence: operatorsPrecedence,
		OperatorParsers: operatorParsers,
		BinaryFunctionParsers: binaryFunctionParsers,
		UnaryFunctionParsers: unaryFunctionParsers,
		Separator: ";",
	}

    err := syntaxTree.ConstructTree(query)
    if err != nil {
		panic(fmt.Sprintf("Could not construct tree for query '%s': %s\n", query, err))
    }

    fmt.Printf("%s", &syntaxTree)
}

will result in the following tree: Syntax Tree

Documentation ΒΆ

Index ΒΆ

Constants ΒΆ

This section is empty.

Variables ΒΆ

This section is empty.

Functions ΒΆ

This section is empty.

Types ΒΆ

type BinaryFunctionParser ΒΆ

type BinaryFunctionParser struct {
	FunctionName     string
	OpeningDelimiter byte
	ClosingDelimiter byte
	OperandSeparator byte
}

type Node ΒΆ

type Node struct {
	Id         int
	Parent     *Node
	Value      string
	Type       NodeType
	LeftChild  *Node
	RightChild *Node
	IsGroup    bool
}

type NodeType ΒΆ

type NodeType int
const (
	Unknown NodeType = iota // 0 by default
	Operator
	UnaryOperator
	LeftOperand
	RightOperand
)

func (NodeType) String ΒΆ

func (e NodeType) String() string

type OperatorParser ΒΆ

type OperatorParser struct {
	OperatorString  string
	OperatorPattern *regexp.Regexp
}

type ParseError ΒΆ added in v0.0.2

type ParseError struct {
	Msg string
}

func (*ParseError) Error ΒΆ added in v0.0.2

func (p *ParseError) Error() string

type SyntaxTree ΒΆ

type SyntaxTree struct {
	// Root node of the tree
	Root *Node

	// List of all nodes of the tree
	Nodes []*Node

	// Precedence of the operators and functions in the syntax
	// Operators with a lower index in this array have a higher precedence over operators with a lower index
	OperatorPrecedence []string

	// Define the patterns of the syntax operators
	OperatorParsers []OperatorParser

	// Define the format of the syntax binary functions
	BinaryFunctionParsers []BinaryFunctionParser

	// Define the format of the syntax unary functions
	UnaryFunctionParsers []UnaryFunctionParser

	// Define a separator that can be used to separate the operators and operands during parsing
	// This is a string that cannot exist in the query character space
	Separator string
}

SyntaxTree Construct a syntax tree based on a defined syntax containing of simple Operators, Binary Functions and Unary Functions with there mutual precedence The construction of the tree will also take into account grouping using brackets '()' in the precedence

func (*SyntaxTree) ConstructTree ΒΆ

func (t *SyntaxTree) ConstructTree(query string) error

func (*SyntaxTree) ParseQuery ΒΆ

func (t *SyntaxTree) ParseQuery(query string) (string, error)

func (SyntaxTree) String ΒΆ

func (t SyntaxTree) String() string

type UnaryFunctionParser ΒΆ

type UnaryFunctionParser struct {
	FunctionName     string
	OpeningDelimiter byte
	ClosingDelimiter byte
}

Jump to

Keyboard shortcuts

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