func Many(inner Parser) Parser {
	return &ManyParser{inner: inner, combine: true}
}
Many returns a parser that matches the given parser zero or more
times, and combines the results.
package parser
import (
	"bytes"
	"fmt"
	"github.com/jmikkola/parsego/parser/result"
	"github.com/jmikkola/parsego/parser/scanner"
	"github.com/jmikkola/parsego/parser/textpos"
)
type Parser interface {
	Parse(sc scanner.Scanner) result.ParseResult
}
func fail(at textpos.TextPos, format string, a ...interface{}) result.ParseResult {
	return result.Failed(textpos.Single(at), fmt.Errorf(format, a...))
}
type EOFParser struct{}
func EOF() Parser {
	return &EOFParser{}
}
func (p *EOFParser) Parse(sc scanner.Scanner) result.ParseResult {
	r, err := sc.Read()
	if err == nil {
		return fail(sc.GetPos(), "expected EOF, got %c", r)
	}
	return result.Success(textpos.Single(sc.GetPos()), "")
}
type CharRangeParser struct {
	min rune  
	max rune
}
func Char(c rune) Parser {
	return &CharRangeParser{c, c}
}
func CharRange(min, max rune) Parser {
	return &CharRangeParser{min, max}
}
func (p *CharRangeParser) Parse(sc scanner.Scanner) result.ParseResult {
	start := sc.GetPos()
	r, err := sc.Read()
	if err != nil {
		return fail(sc.GetPos(), "expected a character, got error %v", err)
	}
	if r < p.min || r > p.max {
		return fail(sc.GetPos(),
			"expected a character in the range '%c' to '%c', got error %c",
			p.min, p.max, r)
	}
	return result.Success(
		textpos.Range(start, sc.GetPos()),
		string(r))
}
type TokenParser struct {
	token string
}
func Token(token string) Parser {
	return &TokenParser{token}
}
func (p *TokenParser) Parse(sc scanner.Scanner) result.ParseResult {
	start := sc.GetPos()
	seen := []rune{}
	for _, c := range p.token {
		r, err := sc.Read()
		seen = append(seen, r)
		if err != nil {
			return fail(sc.GetPos(), "expected '%s', got error %v", p.token, err)
		}
		if r != c {
			return fail(sc.GetPos(), "expected '%s', got '%s'", p.token, string(seen))
		}
	}
	return result.Success(
		textpos.Range(start, sc.GetPos()),
		string(seen))
}
type CharSetParser struct {
	allowed map[rune]struct{}
	invert  bool
}
func AnyChar(rs ...rune) Parser {
	return &CharSetParser{allowed: rarray2rmap(rs), invert: false}
}
func NoneOf(rs ...rune) Parser {
	return &CharSetParser{allowed: rarray2rmap(rs), invert: true}
}
func AnyCharIn(s string) Parser {
	return &CharSetParser{allowed: s2runemap(s), invert: false}
}
func AnyCharNotIn(s string) Parser {
	return &CharSetParser{allowed: s2runemap(s), invert: true}
}
func s2runemap(s string) map[rune]struct{} {
	m := make(map[rune]struct{}, len(s))
	for _, r := range s {
		m[r] = struct{}{}
	}
	return m
}
func rarray2rmap(rs []rune) map[rune]struct{} {
	m := make(map[rune]struct{}, len(rs))
	for _, r := range rs {
		m[r] = struct{}{}
	}
	return m
}
func (p *CharSetParser) Parse(sc scanner.Scanner) result.ParseResult {
	start := sc.GetPos()
	r, err := sc.Read()
	if err != nil {
		return fail(sc.GetPos(), "expected a character, got error %v", err)
	}
	if _, ok := p.allowed[r]; ok == p.invert {
		return fail(sc.GetPos(), "expected a character in the set, got error %c", r)
	}
	return result.Success(textpos.Range(start, sc.GetPos()), string(r))
}
type SeqParser struct {
	parsers []Parser
}
func Sequence(parsers ...Parser) Parser {
	return &SeqParser{parsers}
}
func (p *SeqParser) Parse(sc scanner.Scanner) result.ParseResult {
	start := sc.GetPos()
	var end textpos.TextPos
	results := []interface{}{}
	for _, inner := range p.parsers {
		innerResult := inner.Parse(sc)
		if !innerResult.Matched() {
			return innerResult
		}
		end = innerResult.TextRange().End()
		results = append(results, innerResult.Result())
	}
	return result.Success(textpos.Range(start, end), cleanupResult(results))
}
func cleanupResult(results []interface{}) interface{} {
	var buffer bytes.Buffer
	allStr := true
	for _, result := range results {
		if result == "" {
			continue
		}
		if s, ok := result.(string); ok {
			buffer.WriteString(s)
		} else {
			allStr = false
			break
		}
	}
	if allStr {
		return buffer.String()
	}
	return results
}
type Wrapper struct {
	inner Parser
	fn    func(interface{}) interface{}
}
func ParseWith(p Parser, fn func(interface{}) interface{}) Parser {
	return &Wrapper{inner: p, fn: fn}
}
func (p *Wrapper) Parse(sc scanner.Scanner) result.ParseResult {
	innerResult := p.inner.Parse(sc)
	if innerResult.Matched() {
		return result.Success(
			innerResult.TextRange(),
			p.fn(innerResult.Result()))
	}
	return innerResult
}
type MaybeParser struct {
	inner Parser
}
func Maybe(inner Parser) Parser {
	return &MaybeParser{inner}
}
func (p *MaybeParser) Parse(sc scanner.Scanner) result.ParseResult {
	sc.StartSnapshot()
	innerResult := p.inner.Parse(sc)
	if innerResult.Matched() {
		sc.PopSnapshot()
		return innerResult
	}
	sc.RewindSnapshot()
	return result.Success(textpos.Single(sc.GetPos()), "")
}
type ManyParser struct {
	inner   Parser
	combine bool
}
func ListOf(inner Parser) Parser {
	return &ManyParser{inner: inner, combine: false}
}