func (d *Decoder) prev() (*Value, error) {
	if len(d.values) > 0 {
		return d.values[len(d.values)-1], nil
	}
	return nil, errors.New("no previous element")
}
prev returns the previous value.
package ufwb
import (
	"bytes"
	"encoding/binary"
	"fmt"
	"math"
	"strings"
	"bramp.net/dsector/input"
	"errors"
	log "github.com/Sirupsen/logrus"
	"io"
)
const MAX_STACK = 10
const MAX_VALUES = 10000
type ElementBounds struct {
	Element Element
	Start   int64  
	End     int64  
}
func (bounds *ElementBounds) Length() int64 {
	return bounds.End - bounds.Start
}
func (bounds *ElementBounds) String() string {
	element := ""
	if bounds.Element != nil {
		element = bounds.Element.IdString()
	}
	return fmt.Sprintf("[0x%x-0x%x] %s", bounds.Start, bounds.End, element)
}
type StackPrinter []ElementBounds
func (stack StackPrinter) String() string {
	var buffer bytes.Buffer
	for _, s := range stack {
		buffer.WriteString(fmt.Sprintf("[%d %s] ", s.Start, s.Element.IdString()))
	}
	return buffer.String()
}
type Decoder struct {
	u   *Ufwb
	f   input.Input
	err error  
	stack  []ElementBounds
	values []*Value
	dynamicEndian binary.ByteOrder
	debugFunc func(interface{})
}
func getBounds(f input.Input) (int64, int64, error) {
	start, err := f.Tell()
	if err != nil {
		return start, 0, err
	}
	end, err := f.Seek(0, io.SeekEnd)
	if err != nil {
		return start, end, err
	}
	_, err = f.Seek(start, io.SeekStart)
	if err != nil {
		return start, end, err
	}
	return start, end, nil
}
func NewDecoder(u *Ufwb, f input.Input) *Decoder {
	start, end, err := getBounds(f)
	if err != nil {
		return &Decoder{err: err}
	}
	return NewDecoderWithBounds(u, f, start, end)
}
func NewDecoderWithBounds(u *Ufwb, f input.Input, start, end int64) *Decoder {
	d := &Decoder{
		u: u,
		f: f,
		stack: []ElementBounds{{
			Start: start,
			End:   end,
		}},
		dynamicEndian: binary.BigEndian,
	}
	return d
}
func (d *Decoder) Decode() (*Value, error) {
	if d.err != nil {
		return nil, d.err
	}
	assert(len(d.stack) == 1, "Stack is in a unclean state")
	start := d.ParentBounds().Start
	_, err := d.f.Seek(start, io.SeekStart)
	if err != nil {
		return nil, err
	}
	d.values = nil
	v, err := d.u.Read(d)
	assert(len(d.stack) == 1, "Stack left in unclean state")
	if err == io.EOF {
		err = nil
	}
	return v, err
}
func (d *Decoder) ParentBounds() *ElementBounds {
	if len(d.stack) > 0 {
		return &d.stack[len(d.stack)-1]
	}
	panic("The stack should never be empty")
}
func (d *Decoder) read(e Element) (*Value, error) {
	if len(d.stack) > MAX_STACK {
		log.Debugf("%s", StackPrinter(d.stack))
		panic(fmt.Sprintf("Exceeded max parsing stack depth of %d", MAX_STACK))
	}
	bounds := d.ParentBounds()
	end := bounds.End
	start, err := d.f.Tell()
	if err != nil {
		return nil, err
	}
	assert(start >= bounds.Start && start <= bounds.End,
		"seek position (%d) is outside of bounds %s", start, bounds.String())
	log.Debugf("[0x%x] Reading: %s", start, e.IdString())
	if e.Length() != nil {
		length, err := d.eval(e.Length())
		if err != nil {
			return nil, &validationError{e: e, err: err}
		}
		if length < (end - start) {
			end = start + length
		}
	}
	d.stack = append(d.stack, ElementBounds{
		Element: e,
		Start:   start,
		End:     end,
	})
	v, err := e.Read(d)
	vformat := ""
	if v != nil {
		vformat, _ = v.Format(d.f)
	}
	log.Debugf("[0x%x] Read: %s %s %q err:%v", start, e.IdString(), v, ellipsis(vformat, 10), err)
	d.stack = d.stack[:len(d.stack)-1]
	if v != nil {
		if DEBUG {
			v.mustValidiate()
			if pos, err := d.f.Tell(); err != nil {
				if (v.Offset + v.Len) != pos {
					panic(fmt.Sprintf("Decoder was not left at right position after %v", v))
				}
			}
			if (v.Offset + v.Len) > end {
				panic(fmt.Sprintf("Element went beyond bounds!"))
			}
		}
		if len(d.values) > MAX_VALUES {
			log.Debugf("%s", StackPrinter(d.stack))
			panic(fmt.Sprintf("Exceeded parsing max values of %d", MAX_VALUES))
		}
		d.values = append(d.values, v)
	} else {
		assert(err != nil, fmt.Sprintf("%s returned nil value and nil error", e.IdString()))
	}
	return v, err
}
func (d *Decoder) eval(r Expression) (i int64, err error) {
	if i, ok := r.(ConstExpression); ok {
		return int64(i), nil
	}
	str := string(r.(StringExpression))
	switch {
	case str == "remaining":
		return d.remaining()
	case str == "unlimited":
		return math.MaxInt64, nil  
	case strings.HasPrefix(str, "prev."):
		name := strings.TrimPrefix(str, "prev.")
		v, err := d.prevByName(name)
		if err != nil {
			return -1, err
		}
		n, ok := v.Element.(*Number)
		if !ok {
			return -1, fmt.Errorf("previous element %q must be a Number", name)
		}
		log.Debugf("prev(%q) value: %s", name, v)
		return n.Int(d.f, v)
	}
	log.Debugf("eval(%q) = %d, %v", str, i, err)
	return -1, fmt.Errorf("unable to eval %q", str)
}
func (d *Decoder) currentStruct() (int64, *Structure, error) {
	for i := len(d.stack) - 1; i >= 0; i-- {
		if s, ok := d.stack[i].Element.(*Structure); ok {
			return d.stack[i].Start, s, nil
		}
	}
	return -1, nil, errors.New("No structure found. This should never happen")
}
func (d *Decoder) remaining() (int64, error) {
	pos, err := d.f.Tell()
	if err != nil {
		return -1, err
	}
	start, s, err := d.currentStruct()
	if err != nil {
		return -1, err
	}
	len, err := d.eval(s.Length())
	if err != nil {
		return -1, err
	}
	return (start - pos + len), nil
}