func (src *Source) PeekN(n int) []byte {
	rest := len(src.readBytes) - src.nextIdx
	for src.Error() == nil && rest < n {
		src.consume()
		rest = len(src.readBytes) - src.nextIdx
	}
	if rest >= n {
		return src.readBytes[src.nextIdx : src.nextIdx+n]
	}
	src.ReportError(io.ErrUnexpectedEOF)
	return src.readBytes[src.nextIdx:]
}
PeekN return the first N bytes in the buffer to parse.
If N is longer than current buffer, it will read from reader.
The cursor will not be moved.
package parse
import (
	"bytes"
	"errors"
	"io"
	"io/ioutil"
	"unicode/utf8"
)
type stack struct {
	buf []breakInfo
}
type breakInfo struct {
	nextIdx int
}
func (s *stack) Push(info breakInfo) {
	s.buf = append(s.buf, info)
}
func (s *stack) Pop() breakInfo {
	last := len(s.buf) - 1
	brkInfo := s.buf[last]
	s.buf = s.buf[:last]
	return brkInfo
}
func (s *stack) Empty() bool {
	return len(s.buf) == 0
}
type Source struct {
	err            error
	reader         io.Reader
	readBytes      []byte
	buf            []byte
	nextIdx        int
	savepointStack *stack
}
const (
	_maxBufLen = 40
)
func NewSource(reader io.Reader, bufLen int) (*Source, error) {
	if bufLen > _maxBufLen {
		bufLen = _maxBufLen
	}
	buf := make([]byte, bufLen)
	n, err := reader.Read(buf)
	if n == 0 {
		return nil, err
	}
	readByes := make([]byte, n)
	copy(readByes, buf)
	return &Source{
		reader:         reader,
		readBytes:      readByes,
		buf:            buf,
		savepointStack: new(stack),
	}, nil
}
func NewSourceString(str string) (*Source, error) {
	if len(str) == 0 {
		return nil, errors.New("source string is empty")
	}
	reader := bytes.NewReader([]byte(str))
	return NewSource(reader, _maxBufLen)
}
func (src *Source) StoreSavepoint() {
	src.savepointStack.Push(breakInfo{nextIdx: src.nextIdx})
}
var errNoSavepoint = errors.New("no savepoint in stack")
func (src *Source) DeleteSavepoint() {
	if src.savepointStack.Empty() {
		src.ReportError(errNoSavepoint)
		return
	}
	src.savepointStack.Pop()
}
func (src *Source) RollbackToSavepoint() {
	if src.savepointStack.Empty() {
		src.ReportError(errNoSavepoint)
		return
	}
	brkInfo := src.savepointStack.Pop()
	src.nextIdx = brkInfo.nextIdx
	src.err = nil
}
func (src *Source) Peek1() byte {
	if src.Error() != nil {
		return 0x0
	}
	if src.nextIdx < len(src.readBytes) {
		return src.readBytes[src.nextIdx]
	}
	src.consume()
	if src.nextIdx < len(src.readBytes) {
		return src.readBytes[src.nextIdx]
	}
	src.ReportError(io.ErrUnexpectedEOF)
	return 0x0  
}
func (src *Source) Peek() []byte {
	return src.readBytes[src.nextIdx:]
}
func (src *Source) PeekAll() []byte {
	data, _ := ioutil.ReadAll(src.reader)
	if len(data) > 0 {
		src.readBytes = append(src.readBytes, data...)
	}
	return src.readBytes[src.nextIdx:]
}