wowlua

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2021 License: MIT Imports: 9 Imported by: 0

README

wowlua

Go Reference

Some time around 2015 I wrote tooling to synchronize our WoW guild calendar to a Google Calendar. This had two parts: one was a WoW UI addon that would retrieve the guild calendar data and ensure it was written out to disk as Lua data. The other would read that data and synchronize it to Google Calendar. This package was written to support the latter. I don't have any of the othe code.

I'm not sure that this package entirely works and I haven't really touched it since 2015.

It's simple enough to use:

table, err := wowlua.ParseLua(string(luaTableDataByteSlice))

The top-level data structure must be a table. The package only parses into types defined by this package, not arbitrary Go types like encoding/json. To use the table you must either node how data is stored with in it or be willing to inspect the keys and check node types.

Documentation

Index

Constants

View Source
const (
	// LogLevelDebug will log debug messages
	LogLevelDebug = iota
	// LogLevelErrors will output errors-only
	LogLevelErrors
)
View Source
const (
	// NodeTypeString is a node containing a string
	NodeTypeString = iota
	// NodeTypeIdentifier is a node containing an identifier
	NodeTypeIdentifier
	// NodeTypeNumber is a node containing a number
	NodeTypeNumber
	// NodeTypeBool is a node containing a bool
	NodeTypeBool
	// NodeTypeTable is a node containing a table
	NodeTypeTable
	// NodeTypeTableEntry is a node containing a table entry
	NodeTypeTableEntry
)
View Source
const (
	StateTokenNone = iota
	StateTokenBareHyphen
	StateTokenFindNewline
	StateTokenString
	StateTokenEscapedChar
	StateTokenNumber
	StateTokenIdentifier
	StateTokenInvalid
)
View Source
const (
	TokenTypeStartTable = iota
	TokenTypeEndTable
	TokenTypeStartKey
	TokenTypeEndKey
	TokenTypeEquals
	TokenTypeComma
	TokenTypeIgnore
	TokenTypeString
	TokenTypeNumber
	TokenTypeIdentifier
)

Variables

View Source
var (
	// ErrNotFound indicates the requested node wasn't found
	ErrNotFound = errors.New("Node not found")
	// ErrNotTable indicates the node wasn't a table
	ErrNotTable = errors.New("Node not a table")
	// ErrWrongType indicates that the node was the wrong type
	ErrWrongType = errors.New("Node is wrong type")
)
View Source
var (
	// NaN is not a number
	NaN = math.NaN()
)

Functions

This section is empty.

Types

type DefaultLogger

type DefaultLogger int

DefaultLogger is a wrapper around the log package that conditionally logs based on its log level

func (DefaultLogger) Debugf

func (level DefaultLogger) Debugf(tmpl string, v ...interface{})

Debugf outputs a debug message if logging is set to LogLevelDebug

func (DefaultLogger) Errorf

func (level DefaultLogger) Errorf(tmpl string, v ...interface{})

Errorf outputs and error message

type Logger

type Logger interface {
	Debugf(tmpl string, v ...interface{})
	Errorf(tmpl string, v ...interface{})
}

A Logger has a debug and error logging function

type Node

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

Node contains a parsed value. This value can be a scalar or a table

func NewNode

func NewNode(nType int, v interface{}) *Node

NewNode creates a node of the given type with the provided value. It assumes the specified type is appropriate for the value.

func (*Node) Equals

func (n *Node) Equals(o *Node) bool

Equals returns whether this node is equal to another. For this to be true the types and values must match. Not all types support equality testing.

func (*Node) GetBool

func (n *Node) GetBool() bool

GetBool returns the value of the node if it's a bool and false if it is not. You should check that the node type is a bool first.

func (*Node) GetFloat64

func (n *Node) GetFloat64() float64

GetFloat64 returns the underlying value of the node if it is numeric and NaN if not

func (*Node) GetString

func (n *Node) GetString() string

GetString returns the underlying string value of the node if it is a string or identifier type and empty string if not

func (*Node) GetTable

func (n *Node) GetTable() *Table

GetTable returns the underlying table if the node is of type table and nil if not

func (*Node) GetType

func (n *Node) GetType() int

GetType returns the type the node holds

func (*Node) String

func (n *Node) String() string

String returns a string representation of the value

type Parser

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

Parser handles parsing tokens into Nodes

func NewParser

func NewParser() *Parser

NewParser creates a new parser.

func (*Parser) Finish

func (p *Parser) Finish() (*Table, error)

Finish parses all available tokens and returns the resulting table.

func (*Parser) Next

func (p *Parser) Next(t *Token) error

Next parses the next token

func (*Parser) Peek

func (p *Parser) Peek() *Node

Peek returns the node on top of the stack without removing it. It returns nil if the stack is empty.

func (*Parser) Pop

func (p *Parser) Pop() *Node

Pop a node off the current parse stack. Returns nil if the stack is empty.

func (*Parser) Push

func (p *Parser) Push(n *Node)

Push a node onto the current parse stack

type Table

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

Table is the top-level data structure returned by parsing. The table consists of table entries. Each entry has a key and a value, each of type Node.

func NewTable

func NewTable() *Table

NewTable creates a new, empty table

func ParseLua

func ParseLua(data string) (*Table, error)

ParseLua handles end to end parsing of a string containing Lua table data

func (*Table) AddIndexed

func (t *Table) AddIndexed(n *Node) int

AddIndexed appends the node to the table. The key for the new node is the current number of entries. This value is returned.

func (*Table) Equals

func (t *Table) Equals(o *Table) bool

Equals returns whether this table is equivalent to another table.

func (*Table) Get

func (t *Table) Get(k *Node) *Node

Get retrieves a node from the table with a key equal to the provided key

func (*Table) GetByString

func (t *Table) GetByString(s string) *Node

GetByString looks for an entry with a key node of type string matching the provided string value. If no matching node is found nil is returned.

func (*Table) GetFloat64ByString

func (t *Table) GetFloat64ByString(s string) (float64, error)

GetFloatByString looks for an entry in the table with a string key equal to the provided string and a value of type Number. It returns an error if no entry has a matching key or the matching entry node is not a Number.

func (*Table) GetPath

func (t *Table) GetPath(path ...*Node) (*Table, *Node, error)

GetPath walks through nested tables to find a node matching the path.

func (*Table) GetStringByString

func (t *Table) GetStringByString(s string) (string, error)

GetStringByString looks for an entry in the table with a string key equal to the provided string and a value of type string. It returns an error if no entry has a matching key or the matching entry node is not a string.

func (*Table) GetStringPath

func (t *Table) GetStringPath(path ...string) (*Table, *Node, error)

GetStringByPath walks through nested tables to find a node matching the path. All keys in the path must be strings.

func (*Table) HasKey

func (t *Table) HasKey(k *Node) bool

HasKey checks whether the table has an entry with a key equal to the provided key node.

func (*Table) HasKeyByString

func (t *Table) HasKeyByString(s string) bool

HasKeyByString checks whether the table has an entry with the provided key string. This does not parse numeric strings to compare to numeric keys.

func (*Table) Keys

func (t *Table) Keys() []*Node

Keys returns all the keys in the table as a slice.

func (*Table) Len

func (t *Table) Len() int

Len returns the number of entries in the table

func (*Table) Set

func (t *Table) Set(k, v *Node)

Set an entry in table with the provided key-value pair. If an entry exists with that key it is overwritten. If not it is added.

func (*Table) String

func (t *Table) String() string

String returns a string representation of the table

type Token

type Token struct {
	Type  int
	Value string
}

A Token is a symbol identified by the tokenizer.

func NewToken

func NewToken(nType int, v string) *Token

Create a new token.

func (Token) String

func (t Token) String() string

type Tokenizer

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

A Tokenizer processes a string, yielding Tokens.

func NewTokenizer

func NewTokenizer(s string, c func(*Token) error) *Tokenizer

NewTokenizer creates a new Tokenizer to process the supplied string. It will provide each token to the supplied callback function. If the callback returns an error, tokenization will stop.

func (*Tokenizer) Buffer

func (t *Tokenizer) Buffer(r rune)

Add a rune to the buffer.

func (*Tokenizer) Emit

func (t *Tokenizer) Emit(tok *Token)

If there's been no previous error, send a token to the callback and capture any returned error.

func (*Tokenizer) Send

func (t *Tokenizer) Send(pType int)

Create a new token from the buffer of the specified type, Emit() the token, then clear the buffer.

func (*Tokenizer) SetStateToken

func (t *Tokenizer) SetStateToken(state int)

Set the current state. If the specified state is invalid, nothing happens.

func (*Tokenizer) Tokenize

func (t *Tokenizer) Tokenize() error

Process in the input stream until it's finished or an error is encountered.

Directories

Path Synopsis
cmd
parse_demo command
tree_nav command

Jump to

Keyboard shortcuts

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