func NewParser(anonFunctionName int) *Parser {
	return &Parser{
		finalizers:       map[string][]*ast.Finally{},
		Constants:        map[string]*ast.Literal{},
		imports:          map[string]string{},
		funcs:            map[string]*ast.Func{},
		anonFunctionName: anonFunctionName,
	}
}
NewParser creates an empty parser.
package parser
import (
	"fmt"
	"reflect"
	"sort"
	"github.com/elliotchance/ok/ast"
	"github.com/elliotchance/ok/lexer"
	"github.com/elliotchance/ok/types"
	"github.com/pkg/errors"
)
type Parser struct {
	errors        []error
	funcs         map[string]*ast.Func
	tests         []*ast.Test
	finalizers    map[string][]*ast.Finally
	functionNames []string
	imports       map[string]string
	comments      []*ast.Comment
	anonFunctionName int
	Constants map[string]*ast.Literal
	tokens []lexer.Token
}
func (parser *Parser) appendErrorAt(pos string, message string) {
	var err error
	if pos != "" {
		err = fmt.Errorf("%s %s", pos, message)
	} else {
		err = errors.New(message)
	}
	parser.errors = append(parser.errors, err)
}
func (parser *Parser) appendError(node ast.Node, message string) {
	if v := reflect.ValueOf(node); v.IsValid() && !v.IsNil() {
		parser.appendErrorAt(node.Position(), message)
	} else {
		parser.appendErrorAt("", message)
	}
}
func (parser *Parser) appendErrorf(node ast.Node, format string, args ...interface{}) {
	parser.appendError(node, fmt.Sprintf(format, args...))
}
func (parser *Parser) Errors() Errors {
	return parser.errors
}
func (parser *Parser) appendFinally(finally *ast.Finally) {
	funcName := parser.functionNames[len(parser.functionNames)-1]
	finally.Index = len(parser.finalizers[funcName])
	parser.finalizers[funcName] = append(parser.finalizers[funcName], finally)
}
func (parser *Parser) nextFunctionName() string {
	parser.anonFunctionName++
	return fmt.Sprintf("%d", parser.anonFunctionName)
}
func (parser *Parser) Package(packageAlias string) *ast.Func {
	properties := map[string]*types.Type{}
	var constantNames []string
	for name := range parser.Constants {
		constantNames = append(constantNames, name)
	}
	sort.Strings(constantNames)
	var statements []ast.Node
	for _, name := range constantNames {
		if name[0] == '_' {
			continue
		}
		c := parser.Constants[name]
		properties[name] = c.Kind
		if !c.IsGlobal {
			statements = append(statements, &ast.Assign{
				Lefts:  []ast.Node{&ast.Identifier{Name: name}},
				Rights: []ast.Node{c},
			})
		}
	}
	var funcNames []string
	for name := range parser.funcs {
		funcNames = append(funcNames, name)
	}
	sort.Strings(funcNames)
	for _, name := range funcNames {
		fn := parser.funcs[name]
		properties[name] = fn.Type()
		statements = append(statements, &ast.Assign{
			Lefts:  []ast.Node{&ast.Identifier{Name: name}},
			Rights: []ast.Node{fn},
		})
	}
	return &ast.Func{
		Name: packageAlias,
		Returns: []*types.Type{
			types.NewInterface(packageAlias, properties),
		},
		Statements: statements,
		Pos:        parser.pos(0),
		UniqueName: parser.nextFunctionName(),
	}
}
func (parser *Parser) Funcs() map[string]*ast.Func {
	return parser.funcs
}
func (parser *Parser) SortedFuncNames() []string {
	if len(parser.funcs) == 0 {
		return nil
	}
	names := make([]string, len(parser.funcs))
	i := 0
	for name := range parser.funcs {
		names[i] = name
		i++
	}
	sort.Strings(names)
	return names
}
func (parser *Parser) Tests() []*ast.Test {
	return parser.tests
}
func (parser *Parser) Imports() map[string]string {
	return parser.imports
}
func (parser *Parser) Comments() []*ast.Comment {
	return parser.comments
}