func (p *Parser) parseRenameContentCmd(db, ctx string) {
	_token := p.expect(itemPath, ctx)
	_path := _token.val
	_token = p.expect(itemString, ctx)
	_name, err := formatString(_token.val)
	if err != nil {
		p.errorf("Improperly quoted new name in %s", ctx)
	}
	_filter := p.parseEndofCommand(ctx)
	cmd := bytengine.Command{
		Name:    ctx,
		IsAdmin: false,
		Args:    make(map[string]interface{}),
		Options: make(map[string]interface{}),
	}
	cmd.Database = db
	cmd.Args["path"] = _path
	cmd.Args["name"] = _name
	cmd.Filter = _filter
	p.commands = append(p.commands, cmd)
}
rename file/directory parser
package base
import (
	"encoding/json"
	"fmt"
	"gopkg.in/mgo.v2/bson"
	"math"
	"runtime"
	"strconv"
	"strings"
	"unicode/utf8"
	"github.com/johnwilson/bytengine"
)
type Parser struct {
	commands  []bytengine.Command
	cmdlookup map[string]map[string]interface{}
	registry  *FunctionRegistry
	lex       *lexer
	token     [2]item  
	peekCount int
}
type ServerFunction func(ctx string)
type UserFunction func(ctx string)
type DatabaseFunction func(db, ctx string)
func (p *Parser) next() item {
	if p.peekCount > 0 {
		p.peekCount--
	} else {
		p.token[0] = p.lex.nextItem()
	}
	return p.token[p.peekCount]
}
func (p *Parser) backup() {
	p.peekCount++
}
func (p *Parser) backup2(t1 item) {
	p.token[1] = t1
	p.peekCount = 2
}
func (p *Parser) peek() item {
	if p.peekCount > 0 {
		return p.token[p.peekCount-1]
	}
	p.peekCount = 1
	p.token[0] = p.lex.nextItem()
	return p.token[0]
}
func NewParser() *Parser {
	p := &Parser{}
	p.registry = NewFunctionRegistry()
	return p
}
func (p *Parser) errorf(format string, args ...interface{}) {
	p.commands = nil
	format = fmt.Sprintf("line[%d]: %s", p.lex.lineNumber(), format)
	panic(fmt.Errorf(format, args...))
}
func (p *Parser) error(err error) {
	p.errorf("%s", err)
}
func (p *Parser) expect(expected itemType, context string) item {
	token := p.next()
	if token.typ != expected {
		p.errorf("expected %s in %s; got %s", expected, context, token)
	}
	return token
}
func (p *Parser) expectOneOf(expected1, expected2 itemType, context string) item {
	token := p.next()
	if token.typ != expected1 && token.typ != expected2 {
		p.errorf("expected %s or %s in %s; got %s", expected1, expected2, context, token)
	}
	return token
}
func (p *Parser) unexpected(token item, context string) {
	p.errorf("unexpected %s in %s", token, context)
}
func (p *Parser) recover(errp *error) {
	e := recover()
	if e != nil {
		if _, ok := e.(runtime.Error); ok {
			panic(e)
		}
		if p != nil {
			p.stopParse()
		}
		*errp = e.(error)
	}
	return
}
func (p *Parser) startParse(lex *lexer) {
	p.commands = make([]bytengine.Command, 0)
	p.lex = lex
}
func (p *Parser) stopParse() {
	p.lex = nil
	p.token = [2]item{}
	p.peekCount = 0
}
func (p *Parser) atEOF() bool {
	for {
		token := p.peek()
		switch token.typ {
		case itemEOF:
			return true
		}
		break
	}
	return false
}
func (p *Parser) Parse(s string) (c []bytengine.Command, err error) {
	defer p.recover(&err)
	p.startParse(lex(s))
	p.parse()
	p.stopParse()
	return p.commands, nil
}
func (p *Parser) parse() map[string]interface{} {
	for p.peek().typ != itemEOF {
		switch _next := p.peek(); {
		case _next.typ == itemError:
			p.errorf("Parsing error: %s", p.peek().val)
		case _next.typ == itemIdentifier:
			switch cmdprefix := strings.ToLower(_next.val); {
			case cmdprefix == "server":
				p.next()
				p.expect(itemDot, cmdprefix)
				item := p.expect(itemIdentifier, cmdprefix)
				key := strings.ToLower(item.val)
				fn, name := p.registry.GetServerItem(key)
				if fn == nil {
					p.errorf("%s.%s not found", cmdprefix, key)
				}
				ctx := fmt.Sprintf("%s.%s", cmdprefix, name)
				fn(ctx)
			case cmdprefix == "user":
				p.next()
				p.expect(itemDot, cmdprefix)
				item := p.expect(itemIdentifier, cmdprefix)
				key := strings.ToLower(item.val)
				fn, name := p.registry.GetUserItem(key)
				if fn == nil {
					p.errorf("%s.%s not found", cmdprefix, key)
				}
				ctx := fmt.Sprintf("%s.%s", cmdprefix, name)
				fn(ctx)
			default:
				p.errorf("Invalid command prefix '%s'", cmdprefix)
			}
		case _next.typ == itemDatabase:
			cmdprefix := "database"
			db := p.next().val
			p.expect(itemDot, cmdprefix)
			item := p.expect(itemIdentifier, cmdprefix)
			key := strings.ToLower(item.val)
			fn, name := p.registry.GetDatabaseItem(key)
			if fn == nil {
				p.errorf("%s.%s not found", cmdprefix, key)
			}
			ctx := fmt.Sprintf("%s.%s", cmdprefix, name)
			fn(db, ctx)
		default:
			p.next()
		}
	}
	return nil
}
func unquote(s string) (t string, err error) {
	n := len(s)
	if n < 2 {
		return "", strconv.ErrSyntax
	}
	quote := s[0]
	if quote != s[n-1] {
		return "", strconv.ErrSyntax
	}
	s = s[1 : n-1]
	if quote == '`' {
		if contains(s, '`') {
			return "", strconv.ErrSyntax
		}
		return s, nil
	}
	if quote != '"' && quote != '\'' {
		return "", strconv.ErrSyntax
	}
	if contains(s, '\n') {
		return "", strconv.ErrSyntax
	}
	if !contains(s, '\\') && !contains(s, quote) {
		switch quote {
		case '"':
			return s, nil
		case '\'':
			r, size := utf8.DecodeRuneInString(s)
			if size == len(s) && (r != utf8.RuneError || size != 1) {
				return s, nil
			}
		}
	}
	var runeTmp [utf8.UTFMax]byte
	buf := make([]byte, 0, 3*len(s)/2)  
	for len(s) > 0 {
		c, multibyte, ss, err := strconv.UnquoteChar(s, quote)
		if err != nil {
			return "", err
		}
		s = ss
		if c < utf8.RuneSelf || !multibyte {
			buf = append(buf, byte(c))
		} else {
			n := utf8.EncodeRune(runeTmp[:], c)
			buf = append(buf, runeTmp[:n]...)
		}
	}
	return string(buf), nil
}
func contains(s string, c byte) bool {
	for i := 0; i < len(s); i++ {
		if s[i] == c {
			return true
		}
	}
	return false
}
func formatString(s string) (string, error) {
	_tmp, err := unquote(s)
	if err != nil {
		return "", err
	}
	return _tmp, nil
}
const FieldPrefix string = "content."
const HeaderPrefix string = "__header__."
const BytesPrefix string = "__bytes__."
func (p *Parser) parseFilterResult() string {
	if p.peek().typ == itemSendTo {
		p.next()
		_nxt := p.expect(itemIdentifier, "Result assignment")
		return _nxt.val
	}
	return ""
}
func (p *Parser) parseEndofCommand(ctx string) string {
	filter := p.parseFilterResult()
	_nxt := p.expectOneOf(itemSemiColon, itemEOF, ctx)
	if _nxt.typ == itemEOF {
		p.backup()
	}
	return filter
}
func (p *Parser) parseLoginCmd(ctx string) {
	_token := p.expect(itemString, ctx)
	_usr, err := formatString(_token.val)
	if err != nil {
		p.errorf("Improperly quoted username in %s", ctx)
	}
	_token = p.expect(itemString, ctx)
	_pw, err := formatString(_token.val)
	if err != nil {
		p.errorf("Improperly quoted password in %s", ctx)
	}
	_filter := p.parseEndofCommand(ctx)
	cmd := bytengine.Command{
		Name:    ctx,
		IsAdmin: false,
		Args:    make(map[string]interface{}),
		Options: make(map[string]interface{}),
	}
	cmd.Args["username"] = _usr
	cmd.Args["password"] = _pw
	cmd.Filter = _filter
	p.commands = append(p.commands, cmd)
}
func (p *Parser) parseListDatabasesCmd(ctx string) {
	cmd := bytengine.Command{
		Name:    ctx,
		IsAdmin: true,
		Args:    make(map[string]interface{}),
		Options: make(map[string]interface{})}  
	ac := newOptList()
	ac.Add("regex", optString)
	p.parseOptions(ctx, ac)
	arg := ac.Get("regex")
	if arg != nil {
		cmd.Options["regex"] = arg
	}
	_filter := p.parseEndofCommand(ctx)
	cmd.Filter = _filter
	p.commands = append(p.commands, cmd)
}
func (p *Parser) parseNewDatabaseCmd(ctx string) {
	_token := p.expect(itemString, ctx)
	_db, err := formatString(_token.val)
	if err != nil {
		p.errorf("Improperly quoted database name in %s", ctx)
	}
	_filter := p.parseEndofCommand(ctx)
	cmd := bytengine.Command{
		Name:    ctx,
		IsAdmin: true,
		Args:    make(map[string]interface{}),
		Options: make(map[string]interface{}),
	}
	cmd.Args["database"] = _db
	cmd.Filter = _filter
	p.commands = append(p.commands, cmd)
}
func (p *Parser) parseDropDatabaseCmd(ctx string) {
	_token := p.expect(itemString, ctx)
	_db, err := formatString(_token.val)
	if err != nil {
		p.errorf("Improperly quoted database name in %s", ctx)
	}
	_filter := p.parseEndofCommand(ctx)
	cmd := bytengine.Command{
		Name:    ctx,
		IsAdmin: true,
		Args:    make(map[string]interface{}),
		Options: make(map[string]interface{}),
	}
	cmd.Args["database"] = _db
	cmd.Filter = _filter
	p.commands = append(p.commands, cmd)
}
func (p *Parser) parseWhoamiCmd(ctx string) {
	_filter := p.parseEndofCommand(ctx)
	cmd := bytengine.Command{
		Name:    ctx,
		IsAdmin: false,
		Args:    make(map[string]interface{}),
		Options: make(map[string]interface{}),
	}
	cmd.Filter = _filter
	p.commands = append(p.commands, cmd)
}
func (p *Parser) parseNewUserCmd(ctx string) {
	_token := p.expect(itemString, ctx)
	_user, err := formatString(_token.val)
	if err != nil {
		p.errorf("Improperly quoted username in %s", ctx)
	}
	_token = p.expect(itemString, ctx)
	_pw, err2 := formatString(_token.val)
	if err2 != nil {
		p.errorf("Improperly quoted password in %s", ctx)
	}
	_filter := p.parseEndofCommand(ctx)
	cmd := bytengine.Command{
		Name:    ctx,
		IsAdmin: true,
		Args:    make(map[string]interface{}),
		Options: make(map[string]interface{}),
	}
	cmd.Args["username"] = _user
	cmd.Args["password"] = _pw
	cmd.Filter = _filter
	p.commands = append(p.commands, cmd)
}
func (p *Parser) parseListUsersCmd(ctx string) {
	cmd := bytengine.Command{
		Name:    ctx,
		IsAdmin: true,
		Args:    make(map[string]interface{}),
		Options: make(map[string]interface{})}  
	ac := newOptList()
	ac.Add("regex", optString)
	p.parseOptions(ctx, ac)
	arg := ac.Get("regex")
	if arg != nil {
		cmd.Options["regex"] = arg
	}
	_filter := p.parseEndofCommand(ctx)
	cmd.Filter = _filter
	p.commands = append(p.commands, cmd)
}
func (p *Parser) parseUserInfoCmd(ctx string) {
	_token := p.expect(itemString, ctx)
	_user, err := formatString(_token.val)
	if err != nil {
		p.errorf("Improperly quoted username in %s", ctx)
	}
	_filter := p.parseEndofCommand(ctx)
	cmd := bytengine.Command{
		Name:    ctx,
		IsAdmin: true,
		Args:    make(map[string]interface{}),
		Options: make(map[string]interface{}),
	}
	cmd.Args["username"] = _user
	cmd.Filter = _filter
	p.commands = append(p.commands, cmd)
}
func (p *Parser) parseDropUserCmd(ctx string) {
	_token := p.expect(itemString, ctx)
	_user, err := formatString(_token.val)
	if err != nil {
		p.errorf("Improperly quoted username in %s", ctx)
	}
	_filter := p.parseEndofCommand(ctx)
	cmd := bytengine.Command{
		Name:    ctx,
		IsAdmin: true,
		Args:    make(map[string]interface{}),
		Options: make(map[string]interface{}),
	}
	cmd.Args["username"] = _user
	cmd.Filter = _filter
	p.commands = append(p.commands, cmd)
}
func (p *Parser) parseNewPasswordCmd(ctx string) {
	_token := p.expect(itemString, ctx)
	_user, err := formatString(_token.val)
	if err != nil {
		p.errorf("Improperly quoted username in %s", ctx)
	}
	_token = p.expect(itemString, ctx)
	_pw, err2 := formatString(_token.val)
	if err2 != nil {
		p.errorf("Improperly quoted password in %s", ctx)
	}
	_filter := p.parseEndofCommand(ctx)
	cmd := bytengine.Command{
		Name:    ctx,
		IsAdmin: true,
		Args:    make(map[string]interface{}),
		Options: make(map[string]interface{}),
	}
	cmd.Args["username"] = _user
	cmd.Args["password"] = _pw
	cmd.Filter = _filter
	p.commands = append(p.commands, cmd)
}
func (p *Parser) parseUserSystemAccessCmd(ctx string) {
	_token := p.expect(itemString, ctx)
	_user, err := formatString(_token.val)
	if err != nil {
		p.errorf("Improperly quoted username in %s", ctx)
	}
	_token = p.expect(itemIdentifier, ctx)
	_grant := false
	switch _token.val {
	case "grant":
		_grant = true
	case "deny":
		break
	default:
		p.errorf("Invalid indentifier "+_token.val+" in %s", ctx)
	}
	_filter := p.parseEndofCommand(ctx)
	cmd := bytengine.Command{
		Name:    ctx,
		IsAdmin: true,
		Args:    make(map[string]interface{}),
		Options: make(map[string]interface{}),
	}
	cmd.Args["username"] = _user
	cmd.Args["grant"] = _grant
	cmd.Filter = _filter
	p.commands = append(p.commands, cmd)
}
func (p *Parser) parseUserDatabaseAccessCmd(ctx string) {
	_token := p.expect(itemString, ctx)
	_user, err := formatString(_token.val)
	if err != nil {
		p.errorf("Improperly quoted username in %s", ctx)
	}
	_token = p.expect(itemString, ctx)
	_db, err2 := formatString(_token.val)
	if err2 != nil {
		p.errorf("Improperly quoted database in %s", ctx)
	}
	_token = p.expect(itemIdentifier, ctx)
	_grant := false
	switch _token.val {
	case "grant":
		_grant = true
	case "deny":
		break
	default:
		p.errorf("Invalid indentifier "+_token.val+" in %s", ctx)
	}
	_filter := p.parseEndofCommand(ctx)
	cmd := bytengine.Command{
		Name:    ctx,
		IsAdmin: true,
		Args:    make(map[string]interface{}),
		Options: make(map[string]interface{}),
	}
	cmd.Args["username"] = _user
	cmd.Args["database"] = _db
	cmd.Args["grant"] = _grant
	cmd.Filter = _filter
	p.commands = append(p.commands, cmd)
}
func (p *Parser) parseServerInitCmd(ctx string) {
	_filter := p.parseEndofCommand(ctx)
	cmd := bytengine.Command{
		Name:    ctx,
		IsAdmin: true,
		Args:    make(map[string]interface{}),
		Options: make(map[string]interface{}),
	}
	cmd.Filter = _filter
	p.commands = append(p.commands, cmd)
}
func (p *Parser) parseNewDirectoryCmd(db, ctx string) {
	_token := p.expect(itemPath, ctx)
	_path := _token.val
	_filter := p.parseEndofCommand(ctx)
	cmd := bytengine.Command{
		Name:    ctx,
		IsAdmin: false,
		Args:    make(map[string]interface{}),
		Options: make(map[string]interface{}),
	}
	cmd.Database = db
	cmd.Args["path"] = _path
	cmd.Filter = _filter
	p.commands = append(p.commands, cmd)
}
func (p *Parser) parseNewFileCmd(db, ctx string) {
	_token := p.expect(itemPath, ctx)
	_path := _token.val
	var _json interface{}
	if p.peek().typ == itemLeftBrace {
		_json = p.parseJSON(ctx)
	} else {
		p.errorf("Expecting a JSON object in %s", ctx)
	}
	_filter := p.parseEndofCommand(ctx)
	cmd := bytengine.Command{
		Name:    ctx,
		IsAdmin: false,
		Args:    make(map[string]interface{}),
		Options: make(map[string]interface{}),
	}
	cmd.Database = db
	cmd.Args["path"] = _path
	cmd.Args["data"] = _json
	cmd.Filter = _filter
	p.commands = append(p.commands, cmd)
}
func (p *Parser) parseListDirectoryCmd(db, ctx string) {
	_token := p.expect(itemPath, ctx)
	_path := _token.val
	cmd := bytengine.Command{
		Name:    ctx,
		IsAdmin: false,
		Args:    make(map[string]interface{}),
		Options: make(map[string]interface{}),
	}
	cmd.Database = db
	cmd.Args["path"] = _path
	ac := newOptList()
	ac.Add("regex", optString)
	p.parseOptions(ctx, ac)
	arg := ac.Get("regex")
	if arg != nil {
		cmd.Options["regex"] = arg
	}
	_filter := p.parseEndofCommand(ctx)
	cmd.Filter = _filter
	p.commands = append(p.commands, cmd)
}