ast

package
v0.0.0-...-4aab756 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Used to represent different nodes in the AST task add "Buy milk" due:2016-01-02 priority:high

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Walk

func Walk(v Visitor, node Node)

Walk traverses an AST in depth-first order: It starts by calling v.Visit(node); node must not be nil.

func WalkList

func WalkList(v Visitor, nodes []Node)

WalkList traverses a list of nodes in depth-first order: It starts by calling v.Visit(node) for each node in the list.

Types

type AddCol

type AddCol func(string)

type AddError

type AddError func(error)

type AddValue

type AddValue func(interface{})

type BinaryExpression

type BinaryExpression struct {
	Left     Node
	Operator BinaryOperator
	Right    Node
	NodePosition
}

BinaryExpression represents a binary expression. EXAMPLE: 1 + 2 Left: 1 Operator: + Right: 2

func (*BinaryExpression) EvalInsert

func (bin *BinaryExpression) EvalInsert(transpiler *Transpiler) interface{}

func (*BinaryExpression) EvalSelect

func (b *BinaryExpression) EvalSelect(builder *sqlbuilder.SelectBuilder, addError AddError) interface{}

func (*BinaryExpression) Expression

func (b *BinaryExpression) Expression()

func (*BinaryExpression) Type

func (b *BinaryExpression) Type() NodeType

type BinaryOperator

type BinaryOperator int // BinaryOperator is an enum for binary operators.
const (
	BinaryOperatorAdd BinaryOperator = iota // e.g 1 + 2
	BinaryOperatorSub BinaryOperator = iota // e.g 1 - 2
	BinaryOperatorMul BinaryOperator = iota // e.g 1 * 2
	BinaryOperatorDiv BinaryOperator = iota // e.g 1 / 2
	BinaryOperatorMod BinaryOperator = iota // e.g 1 % 2
	BinaryOperatorEq  BinaryOperator = iota // e.g 1 == 2
	BinaryOperatorNe  BinaryOperator = iota // e.g 1 != 2
	BinaryOperatorLt  BinaryOperator = iota // e.g 1 < 2
	BinaryOperatorLe  BinaryOperator = iota // e.g 1 <= 2
	BinaryOperatorGt  BinaryOperator = iota // e.g 1 > 2
	BinaryOperatorGe  BinaryOperator = iota // e.g 1 >= 2
)

type Column

type Column int // Column represents a column in the source code.

type Command

type Command struct {
	Kind    CommandKind // Kind represents the type of command. e.g add
	Param   *Param      // Param represents a parameter in the command. e.g "buy dog"
	Options []Statement // Option represents an option in the command. e.g priority:high
	NodePosition
}

Command represents a command in the AST. Example: add "buy dog" priority:high -----------------------^^^^^^^^^^^^^ options -------------^^^^^^^^^ parameter

func (*Command) EvalInsert

func (c *Command) EvalInsert(transpiler *Transpiler) interface{}

func (*Command) EvalSelect

func (c *Command) EvalSelect(builder *sqlbuilder.SelectBuilder, addError AddError) interface{}

func (*Command) Statement

func (c *Command) Statement()

func (*Command) Type

func (c *Command) Type() NodeType

type CommandKind

type CommandKind int
const (
	CommandKindAdd     CommandKind = iota // e.g add "buy dog"
	CommandKindDepends                    // e.g depends 1 on 2
	CommandKindList                       // e.g list +HOME
	CommandKindNext                       // e.g list +HOME
)

func (*CommandKind) String

func (c *CommandKind) String() string

type Expression

type Expression interface {
	Node
	Expression()
	Type() NodeType
}

type ExpressionStatement

type ExpressionStatement struct {
	Expr Expression // binary, logical, tag, pair, literal
	NodePosition
}

ExpressionStatement represents an expression statement. Example (Binary): 1 + 2 Example (Logical): 1 and 2 Example (Pair): priority:<High Example (Pair): priority:High Example (Literal): "high" Example (Tag): +HOME

func (*ExpressionStatement) EvalInsert

func (stmt *ExpressionStatement) EvalInsert(transpiler *Transpiler) interface{}

func (*ExpressionStatement) EvalSelect

func (stmt *ExpressionStatement) EvalSelect(builder *sqlbuilder.SelectBuilder, addError AddError) interface{}

func (*ExpressionStatement) Expression

func (stmt *ExpressionStatement) Expression()

func (*ExpressionStatement) Statement

func (stmt *ExpressionStatement) Statement()

func (*ExpressionStatement) Type

func (stmt *ExpressionStatement) Type() NodeType

type Key

type Key struct {
	Key  string
	Expr Expression
	NodePosition
}

============================================================================= Key =============================================================================

func (*Key) EvalInsert

func (key *Key) EvalInsert(transpiler *Transpiler) interface{}

func (*Key) EvalSelect

func (key *Key) EvalSelect(builder *sqlbuilder.SelectBuilder, addError AddError) interface{}

func (*Key) Expression

func (key *Key) Expression()

func (*Key) Type

func (key *Key) Type() NodeType

type Line

type Line int // Line represents a line in the source code.

type Literal

type Literal struct {
	Value string      // e.g "buy dog"
	Kind  LiteralKind // e.g String/Number
	NodePosition
}

Literal represents a literal value in the AST. Example (string): "buy dog" Example (number): 5

func (*Literal) EvalInsert

func (lit *Literal) EvalInsert(transpiler *Transpiler) interface{}

func (*Literal) EvalSelect

func (l *Literal) EvalSelect(builder *sqlbuilder.SelectBuilder, addError AddError) interface{}

func (*Literal) Expression

func (l *Literal) Expression()

func (*Literal) ToPriorityInt

func (lit *Literal) ToPriorityInt() (db.TaskPriority, error)

ToPriorityInt converts the literal to a priority integer.

func (*Literal) ToValue

func (l *Literal) ToValue(transpiler *Transpiler) interface{}

func (*Literal) Type

func (l *Literal) Type() NodeType

type LiteralKind

type LiteralKind int // LiteralType is an enum for literal types.
const (
	LiteralKindString LiteralKind = iota // e.g "buy dog"
	LiteralKindNumber LiteralKind = iota // e.g 5
)

func (*LiteralKind) String

func (l *LiteralKind) String() string

type LogicalExpression

type LogicalExpression struct {
	Left     Node
	Operator LogicalOperator
	Right    Node
	NodePosition
}

LogicalExpression represents a logical expression. EXAMPLE (and): 1 and 2 EXAMPLE (or): 1 or 2

func (*LogicalExpression) EvalInsert

func (logical *LogicalExpression) EvalInsert(transpiler *Transpiler) interface{}

func (*LogicalExpression) EvalSelect

func (l *LogicalExpression) EvalSelect(builder *sqlbuilder.SelectBuilder, addError AddError) interface{}

func (*LogicalExpression) Expression

func (l *LogicalExpression) Expression()

func (*LogicalExpression) Type

func (l *LogicalExpression) Type() NodeType

type LogicalOperator

type LogicalOperator int // LogicalOperator is an enum for logical operators.
const (
	LogicalOperatorAnd LogicalOperator = iota // e.g 1 and 2
	LogicalOperatorOr  LogicalOperator = iota // e.g 1 or 2
)

type Node

type Node interface {
	Type() NodeType
	StartColumn() Column
	EndColumn() Column
	StartLine() Line
	EndLine() Line
	Transpile
}

Node represents a node in the AST.

type NodePosition

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

func (*NodePosition) EndColumn

func (n *NodePosition) EndColumn() Column

func (*NodePosition) EndLine

func (n *NodePosition) EndLine() Line

func (*NodePosition) StartColumn

func (n *NodePosition) StartColumn() Column

Emulate the Node interface with embedded struct.

func (*NodePosition) StartLine

func (n *NodePosition) StartLine() Line

type NodeType

type NodeType int // NodeType represents the type of a node. e.g BinaryExpression, Literal, Command, etc.
const (
	NodeTypeBinaryExpression    NodeType = iota // NodeTypeBinaryExpression represents a binary expression. e.g 1 + 2
	NodeTypeCommand                             // NodeTypeCommand represents a command. e.g add "buy dog" priority:High
	NodeTypeExpressionStatement                 // NodeTypeExpressionStatement represents an expression statement. e.g 1 + 2
	NodeTypeLiteral                             // NodeTypeLiteral represents a literal. e.g "buy dog"
	NodeTypeLogicalExpression                   // NodeTypeLogicalExpression represents a logical expression. e.g 1 and 2
	NodeTypeOption                              // NodeTypeOption represents an option. e.g priority:High
	NodeTypePair                                // NodeTypePair represents a pair. e.g priority:High
	NodeTypeParam                               // NodeTypeParam represents a param. e.g "buy dog"
	NodeTypeProgram                             // NodeTypeProgram represents a program. e.g add "buy dog" priority:High
	NodeTypeTag                                 // NodeTypeTag represents a tag. e.g +tag
)

type Param

type Param struct {
	Kind  ParamType   // e.g TaskId, Description
	Value interface{} // e.g 1, "buy dog"
	NodePosition
}

Param represents a parameter in the AST. Some command require parameters like `task 1 modify` Here the parameter is 1

func (*Param) EvalInsert

func (p *Param) EvalInsert(transpiler *Transpiler) interface{}

func (*Param) EvalSelect

func (p *Param) EvalSelect(builder *sqlbuilder.SelectBuilder, addError AddError) interface{}

func (*Param) Expression

func (p *Param) Expression()

func (*Param) Type

func (p *Param) Type() NodeType

type ParamDependency

type ParamDependency struct {
	TaskId      int64
	DependsOnId int64
}

ParamDependency represents a dependency parameter in the AST. Some command require dependencies like `task 1 depends 2`

type ParamType

type ParamType int
const (
	ParamTypeTaskId      ParamType = iota // e.g 1"
	ParamTypeDescription                  // e.g "buy dog"
	ParamTypeDependency                   // e.g 1
)

type SqlArgs

type SqlArgs []interface{} // SqlArgs is a list of arguments for a SQL statement.

type SqlStatement

type SqlStatement string // SqlStatement is a SQL statement.

type Statement

type Statement interface {
	Node
	Statement()
	Type() NodeType
}

type Tag

type Tag struct {
	Operator TagOperator // e.g. + or -
	Value    string      // e.g. HOME
	NodePosition
}

Tag represents a tag in the AST. Example: +HOME Example: -HOME

func (*Tag) EvalInsert

func (t *Tag) EvalInsert(transpiler *Transpiler) interface{}

func (*Tag) EvalSelect

func (t *Tag) EvalSelect(builder *sqlbuilder.SelectBuilder, addError AddError) interface{}

func (*Tag) Statement

func (t *Tag) Statement()

func (*Tag) Type

func (t *Tag) Type() NodeType

type TagOperator

type TagOperator int // TagOperator is an enum for tag operators.
const (
	TagOperatorPlus  TagOperator = iota // e.g. +HOME
	TagOperatorMinus TagOperator = iota // e.g. -HOME
)

type Transpile

type Transpile interface {
	EvalSelect(*sqlbuilder.SelectBuilder, AddError) interface{}
	EvalInsert(*Transpiler) interface{}
}

Transpile represents a node that can be transpiled to SQL.

type TranspileCallback

type TranspileCallback func(tx *sqlx.Tx, taskId int64) error

TranspileCallback is a function that is called after the transpiler has executed a SQL statement. This is useful for executing additional SQL statements that are not part of the main transpilation process.

type TranspileError

type TranspileError struct {
	Message error // The error message
	Node    Node
}

TranspileError represents an error that occurred during transpilation.

type Transpiler

type Transpiler struct {
	Selecter *sqlbuilder.SelectBuilder // A select builder
	Inserter *sqlbuilder.InsertBuilder // An insert builder
	// contains filtered or unexported fields
}

func NewTranspiler

func NewTranspiler(store *db.Store) *Transpiler

NewTranspiler creates a new transpiler with the given store.

func (*Transpiler) AddCol

func (transpiler *Transpiler) AddCol(col string)

AddCol adds a column to the transpilers SQL statement.

func (*Transpiler) AddError

func (transpiler *Transpiler) AddError(message error, node Node)

AddError adds an error to the transpiler.

func (*Transpiler) AddValue

func (transpiler *Transpiler) AddValue(value interface{})

AddValue adds a value to the transpilers SQL args.

func (*Transpiler) Reset

func (transpiler *Transpiler) Reset() *Transpiler

Reset resets the transpiler to it's original state, ready for the next command.

func (*Transpiler) Transpile

func (transpiler *Transpiler) Transpile(
	command *Command,
	tx *sqlx.Tx,
) (SqlStatement, SqlArgs, []TranspileError)

Transpile transpiles a command to a SQL statement.

type TranspilerContext

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

type Visitor

type Visitor interface {
	Visit(node Node) Visitor // Visit returns the visitor to use for the children of the node.
}

Jump to

Keyboard shortcuts

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