Documentation ¶
Overview ¶
Package pot (Pieces of Text) provides a lightweight text serialization parser that is intended to be used together with Go's encoding.TextUnmarshaler interface.
There is only de-serialization support, it's easy enough to generate properly formated POT directly from a encoding.Textmarshaler.
Format ¶
The POT format is similar to JSON but with some differences required by the application that POT was created for.
There are two compound types, a dictionary and a list and two string types. The dictionary type may hold duplicate keys and the key order is maintained. This makes it more of an itemized list than a dictionary type. The list type simply holds a sequence of other types.
There are no numeric or boolean types, all parsing eventually produces strings. It's up to an applications TextUnmarshaler functions to parse these strings. Strings are separated by space, strings may contain space if quoted or escaped.
The characters that may be used for keys in dictionaries are artificially limited similar to variable names in most programming languages. The characters a-z, A-Z, 0-9 are allowed in any position, the character '-' is allowed in any position but the first. Dictionary keys are delimited by values by ':'.
Syntax Examples ¶
Dictionaries:
{ fruit: orange price: 10.5 } { animal: zebra class: mammal weight-range: [ 240kg 370kg ] }
Lists:
[ this is a list with seven strings ] [ "this is a list with one string" ]
Strings:
this-is-a-string "this is a string"
Escape Codes ¶
The escape character is '\'. Characters '{', '}', '[', ']', ':', ' ' must be quoted or escaped in strings. Characters '\' and '"' must be escaped in strings. Additionally '\n' produces a new-line, '\r' a carriage return and '\t' a tab.
Usage ¶
Create a new root level parser and call parser.Next() until it returns nil or an error. There is also ParserScanner type that wraps a parser interface to provide a bufio.Scanner like API
Example:
parser := pot.NewParser([]byte("{ fruit: orange price: 10.5 }")) parse(parser) func parse(parser pot.Parser) { switch parser := parser.(type) { case *pot.DictKey: fmt.Printf("%s ", parser) case *pot.String: fmt.Printf("%s ", parser) } var node pot.Parser var err error for node, err = parser.Next(); node != nil; node, err = parser.Next() { parse(node) } if err != nil { fmt.Printf("error: %s\n", err) } }
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func PrettyPrint ¶
Pretty print POT text buffer. Returns a byte slice or an error on parsing errors.
Example ¶
testPrettyPrint(examplePrint1)
Output: words as root parser strings [ and a list ] { a: dictionary that: { should: be indented: [ properly { and: { dictionaries: [ in a ] list: that } } [ "should not" ] ] } } { a: dictionary that: contains an: empty dictionary: { } } { }
Types ¶
type Dict ¶
type Dict struct {
// contains filtered or unexported fields
}
Dictionary parser.
func NewDictParser ¶
Create a new dictionary parser parsing the supplied text.
type DictKey ¶
type DictKey String
Dictionary key type. A unique "string" type is used to be able to separate keys from string values in type switches.
type List ¶
type List struct {
// contains filtered or unexported fields
}
List parser.
func NewListParser ¶
Create a new list parser parsing the supplied text.
type Location ¶
type Location struct { Line uint32 // Line number counting from zero. Column uint32 // Column number counting from zero. }
Parser location in text input.
func (*Location) Add ¶
Add two locations together. An application can use this to adjust location information provided by the parser.
func (Location) Errorf ¶
func (location Location) Errorf(format string, a ...interface{}) *ParseError
Format an error with the parser location in text input.
type ParseError ¶
Parse error containing location information and optional identifier (file name or similar).
type Parser ¶
type Parser interface { // Parser name. Name() string // Get the next parser, nil or an error. Next() (Parser, error) // Get text the parser was initialized with. Bytes() []byte // Get parser start location in the original text input. // The location is reset when using NewParser, NewDictParser or NewListParser. Location() Location }
Parser interface implemented by Dict, DictKey, List and String parsers.
type ParserScanner ¶
type ParserScanner struct {
// contains filtered or unexported fields
}
Wraps a parser to provide a bufio.Scanner like API for easy parsing.
func NewParserScanner ¶
func NewParserScanner(parser Parser) *ParserScanner
Create a new scanner operating on parser.
func (*ParserScanner) Err ¶
func (scanner *ParserScanner) Err() error
Returns the first error that occured while scanning. This should be called after Scan() has returned false to check for errors.
func (*ParserScanner) InjectError ¶
func (scanner *ParserScanner) InjectError(err error)
Inject an error into the scanner. This will cause the scanner to abort parser iteration and the injected error will be returned by the Err method.
func (*ParserScanner) Scan ¶
func (scanner *ParserScanner) Scan() bool
Scan the parser for a sub parser. Returns true if a sub parser was found.
func (*ParserScanner) SubParser ¶
func (scanner *ParserScanner) SubParser() Parser
Returns the previously scanned sub parser.
type Root ¶
type Root struct {
// contains filtered or unexported fields
}
Root level parser capable of parsing multiple root level objects from the same text input.