tick

package
v0.13.1 Latest Latest
Warning

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

Go to latest
Published: May 13, 2016 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

TICKscript is a simple invocation chaining DSL.

See the examples for how its used and example syntax of the DSL.

Index

Examples

Constants

View Source
const (
	KW_And    = "AND"
	KW_Or     = "OR"
	KW_True   = "TRUE"
	KW_False  = "FALSE"
	KW_Var    = "var"
	KW_Lambda = "lambda"
)

Variables

View Source
var ErrEmptyStack = errors.New("stack is empty")
View Source
var ErrNotFloat = errors.New("value is not a float")

Functions

func Evaluate

func Evaluate(script string, scope *Scope) (err error)

Parse and evaluate a given script for the scope. This evaluation method uses reflection to call methods on objects within the scope.

Example
package main

import (
	"fmt"
)

type Process struct {
	Name     string
	Children []*Process
}

func (p *Process) Spawn() *Process {
	child := &Process{}
	p.Children = append(p.Children, child)
	return child
}

func (p *Process) String() string {
	return fmt.Sprintf("{%q %s}", p.Name, p.Children)
}

func main() {

	//Run a test that evaluates the DSL against the Process struct.
	script := `
//Name the parent
parent.name('parent')

// Spawn a first child
var child1 = parent|spawn()

// Name the first child
child1.name('child1')

//Spawn a grandchild and name it
child1|spawn().name('grandchild')

//Spawn a second child and name it
parent|spawn().name('child2')
`

	scope := NewScope()
	parent := &Process{}
	scope.Set("parent", parent)

	err := Evaluate(script, scope)
	if err != nil {
		fmt.Println(err)
	}

	fmt.Println(parent)
}
Output:

{"parent" [{"child1" [{"grandchild" []}]} {"child2" []}]}

func Format added in v0.12.0

func Format(script string) (string, error)

Formats a TICKscript according to the standard.

func IsCompOperator added in v0.13.0

func IsCompOperator(typ TokenType) bool

True if token type is an operator used in comparisons.

func IsExprOperator added in v0.13.0

func IsExprOperator(typ TokenType) bool

True if token type is an operator used in mathematical or boolean expressions.

func IsMathOperator added in v0.13.0

func IsMathOperator(typ TokenType) bool

True if token type is an operator used in mathematical expressions.

func SetLogger added in v0.12.0

func SetLogger(l *log.Logger)

Types

type BinaryNode

type BinaryNode struct {
	Left      Node
	Right     Node
	Operator  TokenType
	Comment   *CommentNode
	Parens    bool
	MultiLine bool
	// contains filtered or unexported fields
}

binaryNode holds two arguments and an operator.

func (BinaryNode) Char added in v0.12.0

func (p BinaryNode) Char() int

func (*BinaryNode) Format added in v0.12.0

func (n *BinaryNode) Format(buf *bytes.Buffer, indent string, onNewLine bool)

func (BinaryNode) Line added in v0.12.0

func (p BinaryNode) Line() int

func (BinaryNode) Position

func (p BinaryNode) Position() int

func (*BinaryNode) SetComment added in v0.13.0

func (n *BinaryNode) SetComment(c *CommentNode)

func (*BinaryNode) String

func (n *BinaryNode) String() string

type BoolNode

type BoolNode struct {
	Bool    bool
	Comment *CommentNode
	// contains filtered or unexported fields
}

boolNode holds one argument and an operator.

func (BoolNode) Char added in v0.12.0

func (p BoolNode) Char() int

func (*BoolNode) Format added in v0.12.0

func (n *BoolNode) Format(buf *bytes.Buffer, indent string, onNewLine bool)

func (BoolNode) Line added in v0.12.0

func (p BoolNode) Line() int

func (BoolNode) Position

func (p BoolNode) Position() int

func (*BoolNode) SetComment added in v0.13.0

func (n *BoolNode) SetComment(c *CommentNode)

func (*BoolNode) String

func (n *BoolNode) String() string

type ChainNode added in v0.12.0

type ChainNode struct {
	Left     Node
	Right    Node
	Operator TokenType
	Comment  *CommentNode
	// contains filtered or unexported fields
}

func (ChainNode) Char added in v0.12.0

func (p ChainNode) Char() int

func (*ChainNode) Format added in v0.12.0

func (n *ChainNode) Format(buf *bytes.Buffer, indent string, onNewLine bool)

func (ChainNode) Line added in v0.12.0

func (p ChainNode) Line() int

func (ChainNode) Position added in v0.12.0

func (p ChainNode) Position() int

func (*ChainNode) SetComment added in v0.13.0

func (n *ChainNode) SetComment(c *CommentNode)

func (*ChainNode) String added in v0.12.0

func (n *ChainNode) String() string

type CommentNode added in v0.12.0

type CommentNode struct {
	Comments []string
	// contains filtered or unexported fields
}

Hold the contents of a comment

func (CommentNode) Char added in v0.12.0

func (p CommentNode) Char() int

func (*CommentNode) Format added in v0.12.0

func (n *CommentNode) Format(buf *bytes.Buffer, indent string, onNewLine bool)

func (CommentNode) Line added in v0.12.0

func (p CommentNode) Line() int

func (CommentNode) Position added in v0.12.0

func (p CommentNode) Position() int

func (*CommentNode) String added in v0.12.0

func (n *CommentNode) String() string

type DeclarationNode added in v0.12.0

type DeclarationNode struct {
	Left    *IdentifierNode
	Right   Node
	Comment *CommentNode
	// contains filtered or unexported fields
}

func (DeclarationNode) Char added in v0.12.0

func (p DeclarationNode) Char() int

func (*DeclarationNode) Format added in v0.12.0

func (n *DeclarationNode) Format(buf *bytes.Buffer, indent string, onNewLine bool)

func (DeclarationNode) Line added in v0.12.0

func (p DeclarationNode) Line() int

func (DeclarationNode) Position added in v0.12.0

func (p DeclarationNode) Position() int

func (*DeclarationNode) SetComment added in v0.13.0

func (n *DeclarationNode) SetComment(c *CommentNode)

func (*DeclarationNode) String added in v0.12.0

func (n *DeclarationNode) String() string

type DurationNode

type DurationNode struct {
	Dur     time.Duration //the duration
	Comment *CommentNode
	// contains filtered or unexported fields
}

durationNode 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 (DurationNode) Char added in v0.12.0

func (p DurationNode) Char() int

func (*DurationNode) Format added in v0.12.0

func (n *DurationNode) Format(buf *bytes.Buffer, indent string, onNewLine bool)

func (DurationNode) Line added in v0.12.0

func (p DurationNode) Line() int

func (DurationNode) Position

func (p DurationNode) Position() int

func (*DurationNode) SetComment added in v0.13.0

func (n *DurationNode) SetComment(c *CommentNode)

func (*DurationNode) String

func (n *DurationNode) String() string

type DynamicMethod added in v0.10.0

type DynamicMethod func(self interface{}, args ...interface{}) (interface{}, error)

type Func

type Func interface {
	Reset()
	Call(...interface{}) (interface{}, error)
}

A callable function from within the expression

type Funcs

type Funcs map[string]Func

Lookup for functions

func NewFunctions

func NewFunctions() Funcs

Return set of built-in Funcs

type FunctionNode

type FunctionNode struct {
	Type      funcType
	Func      string // The identifier
	Args      []Node
	Comment   *CommentNode
	MultiLine bool
	// contains filtered or unexported fields
}

Holds the a function call with its args

func (FunctionNode) Char added in v0.12.0

func (p FunctionNode) Char() int

func (*FunctionNode) Format added in v0.12.0

func (n *FunctionNode) Format(buf *bytes.Buffer, indent string, onNewLine bool)

func (FunctionNode) Line added in v0.12.0

func (p FunctionNode) Line() int

func (FunctionNode) Position

func (p FunctionNode) Position() int

func (*FunctionNode) SetComment added in v0.13.0

func (n *FunctionNode) SetComment(c *CommentNode)

func (*FunctionNode) String

func (n *FunctionNode) String() string

type IdentifierNode

type IdentifierNode struct {
	Ident   string // The identifier
	Comment *CommentNode
	// contains filtered or unexported fields
}

Holds the textual representation of an identifier

func (IdentifierNode) Char added in v0.12.0

func (p IdentifierNode) Char() int

func (*IdentifierNode) Format added in v0.12.0

func (n *IdentifierNode) Format(buf *bytes.Buffer, indent string, onNewLine bool)

func (IdentifierNode) Line added in v0.12.0

func (p IdentifierNode) Line() int

func (IdentifierNode) Position

func (p IdentifierNode) Position() int

func (*IdentifierNode) SetComment added in v0.13.0

func (n *IdentifierNode) SetComment(c *CommentNode)

func (*IdentifierNode) String

func (n *IdentifierNode) String() string

type LambdaNode

type LambdaNode struct {
	Node    Node
	Comment *CommentNode
	// contains filtered or unexported fields
}

Represents the beginning of a lambda expression

func (LambdaNode) Char added in v0.12.0

func (p LambdaNode) Char() int

func (*LambdaNode) Format added in v0.12.0

func (n *LambdaNode) Format(buf *bytes.Buffer, indent string, onNewLine bool)

func (LambdaNode) Line added in v0.12.0

func (p LambdaNode) Line() int

func (LambdaNode) Position

func (p LambdaNode) Position() int

func (*LambdaNode) SetComment added in v0.13.0

func (n *LambdaNode) SetComment(c *CommentNode)

func (*LambdaNode) String

func (n *LambdaNode) String() string

type ListNode

type ListNode struct {
	Nodes []Node
	// contains filtered or unexported fields
}

Holds a function call with its args

func (*ListNode) Add

func (n *ListNode) Add(node Node)

func (ListNode) Char added in v0.12.0

func (p ListNode) Char() int

func (*ListNode) Format added in v0.12.0

func (n *ListNode) Format(buf *bytes.Buffer, indent string, onNewLine bool)

func (ListNode) Line added in v0.12.0

func (p ListNode) Line() int

func (ListNode) Position

func (p ListNode) Position() int

func (*ListNode) String

func (n *ListNode) String() string

type Node

type Node interface {
	Position
	String() string
	Format(buf *bytes.Buffer, indent string, onNewLine bool)
}

type NumberNode

type NumberNode struct {
	IsInt   bool    // Number has an integral value.
	IsFloat bool    // Number has a floating-point value.
	Int64   int64   // The integer value.
	Float64 float64 // The floating-point value.
	Comment *CommentNode
	// contains filtered or unexported fields
}

numberNode 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 (NumberNode) Char added in v0.12.0

func (p NumberNode) Char() int

func (*NumberNode) Format added in v0.12.0

func (n *NumberNode) Format(buf *bytes.Buffer, indent string, onNewLine bool)

func (NumberNode) Line added in v0.12.0

func (p NumberNode) Line() int

func (NumberNode) Position

func (p NumberNode) Position() int

func (*NumberNode) SetComment added in v0.13.0

func (n *NumberNode) SetComment(c *CommentNode)

func (*NumberNode) String

func (n *NumberNode) String() string

type PartialDescriber added in v0.13.0

type PartialDescriber interface {
	ChainMethods() map[string]reflect.Value
}

PartialDescriber can provide a description of its chain methods that hide embedded property methods.

type Position added in v0.12.0

type Position interface {
	Position() int // byte position of start of node in full original input string
	Line() int
	Char() int
}

type ReferenceNode

type ReferenceNode struct {
	Reference string // The field reference
	Comment   *CommentNode
	// contains filtered or unexported fields
}

Holds the textual representation of an identifier

func (ReferenceNode) Char added in v0.12.0

func (p ReferenceNode) Char() int

func (*ReferenceNode) Format added in v0.12.0

func (n *ReferenceNode) Format(buf *bytes.Buffer, indent string, onNewLine bool)

func (ReferenceNode) Line added in v0.12.0

func (p ReferenceNode) Line() int

func (ReferenceNode) Position

func (p ReferenceNode) Position() int

func (*ReferenceNode) SetComment added in v0.13.0

func (n *ReferenceNode) SetComment(c *CommentNode)

func (*ReferenceNode) String

func (n *ReferenceNode) String() string

type ReflectionDescriber added in v0.10.0

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

Wraps any object as a SelfDescriber using reflection.

Uses tags on fields to determine if a method is really a PropertyMethod Can disambiguate property fields and chain methods of the same name but from different composed anonymous fields. Cannot disambiguate property methods and chain methods of the same name. See NewReflectionDescriber for providing explicit chain methods in this case.

Example:

type MyType struct {
    UseX `tick:"X"`
}
func (m *MyType) X() *MyType{
    m.UseX = true
    return m
}

UseX will be ignored as a property and the method X will become a property method.

Expects that all callable methods are pointer receiver methods.

func NewReflectionDescriber added in v0.10.0

func NewReflectionDescriber(obj interface{}, chainMethods map[string]reflect.Value) (*ReflectionDescriber, error)

Create a NewReflectionDescriber from an object. The object must be a pointer type. Use the chainMethods parameter to provide a set of explicit methods that should be considered chain methods even if an embedded type declares them as property methods

Example:

type MyType struct {
    UseX `tick:"X"`
}
func (m *MyType) X() *MyType{
    m.UseX = true
    return m
}

type AnotherType struct {
    MyType
}
func (a *AnotherType) X() *YetAnotherType {
    // do chain method work here...
}

// Now create NewReflectionDescriber with X as a chain method and property method
at := new(AnotherType)
rd := NewReflectionDescriber(at, map[string]reflect.Value{
    "X": reflect.ValueOf(at.X),
})
rd.HasProperty("x") // true
rd.HasChainMethod("x") // true

func (*ReflectionDescriber) CallChainMethod added in v0.12.0

func (r *ReflectionDescriber) CallChainMethod(name string, args ...interface{}) (interface{}, error)

func (*ReflectionDescriber) Desc added in v0.10.0

func (r *ReflectionDescriber) Desc() string

func (*ReflectionDescriber) HasChainMethod added in v0.12.0

func (r *ReflectionDescriber) HasChainMethod(name string) bool

Using reflection check if the object has the method or field. A field is a valid method because we can set it via reflection too.

func (*ReflectionDescriber) HasProperty added in v0.10.0

func (r *ReflectionDescriber) HasProperty(name string) bool

Using reflection check if the object has a field with the property name.

func (*ReflectionDescriber) Property added in v0.10.0

func (r *ReflectionDescriber) Property(name string) interface{}

func (*ReflectionDescriber) SetProperty added in v0.10.0

func (r *ReflectionDescriber) SetProperty(name string, values ...interface{}) (interface{}, error)

type RegexNode

type RegexNode struct {
	Regex   *regexp.Regexp
	Comment *CommentNode
	// contains filtered or unexported fields
}

Holds the textual representation of a regex literal

func (RegexNode) Char added in v0.12.0

func (p RegexNode) Char() int

func (*RegexNode) Format added in v0.12.0

func (n *RegexNode) Format(buf *bytes.Buffer, indent string, onNewLine bool)

func (RegexNode) Line added in v0.12.0

func (p RegexNode) Line() int

func (RegexNode) Position

func (p RegexNode) Position() int

func (*RegexNode) SetComment added in v0.13.0

func (n *RegexNode) SetComment(c *CommentNode)

func (*RegexNode) String

func (n *RegexNode) String() string

type Scope

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

Contains a set of variables references and their values.

func NewScope

func NewScope() *Scope

Initialize a new Scope object.

func (*Scope) DynamicMethod added in v0.10.0

func (s *Scope) DynamicMethod(name string) DynamicMethod

func (*Scope) Get

func (s *Scope) Get(name string) (interface{}, error)

Get returns the value of 'name'.

func (*Scope) Set

func (s *Scope) Set(name string, value interface{})

Set defines a name -> value pairing in the scope.

func (*Scope) SetDynamicMethod added in v0.10.0

func (s *Scope) SetDynamicMethod(name string, m DynamicMethod)

type SelfDescriber added in v0.10.0

type SelfDescriber interface {
	//A description the object
	Desc() string

	HasChainMethod(name string) bool
	CallChainMethod(name string, args ...interface{}) (interface{}, error)

	HasProperty(name string) bool
	Property(name string) interface{}
	SetProperty(name string, args ...interface{}) (interface{}, error)
}

Interface for interacting with objects. If an object does not self describe via this interface than a reflection based implemenation will be used.

type StarNode

type StarNode struct {
	Comment *CommentNode
	// contains filtered or unexported fields
}

Represents a standalone '*' token.

func (StarNode) Char added in v0.12.0

func (p StarNode) Char() int

func (*StarNode) Format added in v0.12.0

func (n *StarNode) Format(buf *bytes.Buffer, indent string, onNewLine bool)

func (StarNode) Line added in v0.12.0

func (p StarNode) Line() int

func (StarNode) Position

func (p StarNode) Position() int

func (*StarNode) SetComment added in v0.13.0

func (n *StarNode) SetComment(c *CommentNode)

func (*StarNode) String

func (n *StarNode) String() string

type StringNode

type StringNode struct {
	Literal      string // The string literal
	TripleQuotes bool
	Comment      *CommentNode
	// contains filtered or unexported fields
}

Holds the textual representation of a string literal

func (StringNode) Char added in v0.12.0

func (p StringNode) Char() int

func (*StringNode) Format added in v0.12.0

func (n *StringNode) Format(buf *bytes.Buffer, indent string, onNewLine bool)

func (StringNode) Line added in v0.12.0

func (p StringNode) Line() int

func (StringNode) Position

func (p StringNode) Position() int

func (*StringNode) SetComment added in v0.13.0

func (n *StringNode) SetComment(c *CommentNode)

func (*StringNode) String

func (n *StringNode) String() string

type TokenType added in v0.13.0

type TokenType int
const (
	TokenError TokenType = iota
	TokenEOF
	TokenVar
	TokenAsgn
	TokenDot
	TokenPipe
	TokenAt
	TokenIdent
	TokenReference
	TokenLambda
	TokenNumber
	TokenString
	TokenDuration
	TokenLParen
	TokenRParen
	TokenComma
	TokenNot
	TokenTrue
	TokenFalse
	TokenRegex
	TokenComment

	TokenPlus
	TokenMinus
	TokenMult
	TokenDiv
	TokenMod

	TokenAnd
	TokenOr
	TokenEqual
	TokenNotEqual
	TokenLess
	TokenGreater
	TokenLessEqual
	TokenGreaterEqual
	TokenRegexEqual
	TokenRegexNotEqual
)

func (TokenType) String added in v0.13.0

func (t TokenType) String() string

String representation of an TokenType

type UnaryNode

type UnaryNode struct {
	Node     Node
	Operator TokenType
	Comment  *CommentNode
	// contains filtered or unexported fields
}

unaryNode holds one argument and an operator.

func (UnaryNode) Char added in v0.12.0

func (p UnaryNode) Char() int

func (*UnaryNode) Format added in v0.12.0

func (n *UnaryNode) Format(buf *bytes.Buffer, indent string, onNewLine bool)

func (UnaryNode) Line added in v0.12.0

func (p UnaryNode) Line() int

func (UnaryNode) Position

func (p UnaryNode) Position() int

func (*UnaryNode) SetComment added in v0.13.0

func (n *UnaryNode) SetComment(c *CommentNode)

func (*UnaryNode) String

func (n *UnaryNode) String() string

Directories

Path Synopsis
cmd
tickdoc
Tickdoc is a simple utility similar to godoc that generates documentation from comments.
Tickdoc is a simple utility similar to godoc that generates documentation from comments.

Jump to

Keyboard shortcuts

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