func (parser *Parser) parseToSegment(re *regexp.Regexp, kind SegmentKind) *Segment {
	match := parser.source.ConsumeRegex(re)
	if len(match) == 0 {
		return nil
	}
	return &Segment{Kind: kind, Value: match}
}
parseToSegment is a helper function that creates a Segment of the specified kind if the next
characters in the parse stream match the expression re.
package gomodel
import (
	"fmt"
	"regexp"
	"strings"
	"github.com/googleapis/gapic-showcase/util/genrest/resttools"
)
func ParseTemplate(template string) (parsed PathTemplate, err error) {
	return NewParser(template).parse()
}
type Parser struct {
	source *Source
	haveLastSegment bool
	regex map[string]*regexp.Regexp
}
func NewParser(template string) *Parser {
	return &Parser{
		source: &Source{
			str: template,
		},
		regex: make(map[string]*regexp.Regexp),
	}
}
func (parser *Parser) parse() (pt PathTemplate, err error) {
	defer func() {
		if err != nil {
			indent := strings.Repeat(" ", parser.source.idx)
			err = fmt.Errorf("parsing template, position %d: %s   haveLastSegment: %v\n  %q\n   %s^\n   -> %s", parser.source.idx, err, parser.haveLastSegment, parser.source.str, indent, pt)
		}
	}()
	if !parser.source.ConsumeIf('/') {
		return nil, fmt.Errorf("template does not start with slash")
	}
	pt, err = parser.parseSegments()
	pt = append(PathTemplate{slashSegment}, pt...)
	if err != nil {
		return pt, err
	}
	if parser.source.ConsumeIf(':') {
		verb, err := parser.parseLiteral()
		if err != nil {
			return pt, fmt.Errorf("could not parse verb")
		}
		pt = append(pt, &Segment{Kind: Literal, Value: ":"}, verb)
	}
	if parser.source.InRange() {
		return pt, fmt.Errorf("unexpected character")
	}
	return pt, nil
}
func (parser *Parser) parseSegments() (PathTemplate, error) {
	pt := PathTemplate{}
	proceed := true
	for proceed {
		if parser.haveLastSegment {
			return pt, fmt.Errorf("already encountered last segment")
		}
		segment, err := parser.parseOneSegment()
		pt = append(pt, segment)
		if err != nil {
			return pt, err
		}
		if proceed = parser.source.ConsumeIf('/'); proceed {
			pt = append(pt, slashSegment)
		}
	}
	return pt, nil
}
type segmentParser func() (*Segment, error)
func (parser *Parser) parseOneSegment() (*Segment, error) {
	orderedParsers := []segmentParser{parser.parseVariable, parser.parseLiteral, parser.parseMultipleValue, parser.parseSingleValue}
	for _, parse := range orderedParsers {
		seg, err := parse()
		if err != nil || seg != nil {
			return seg, err
		}
	}
	return nil, fmt.Errorf("could not parse path segment")
}
func (parser *Parser) parseSingleValue() (*Segment, error) {
	re := parser.GetRegexp(`^\*`)
	return parser.parseToSegment(re, SingleValue), nil
}
func (parser *Parser) parseMultipleValue() (*Segment, error) {
	re := parser.GetRegexp(`^\*\*`)
	seg := parser.parseToSegment(re, MultipleValue)
	if seg != nil {
		parser.haveLastSegment = true
	}
	return seg, nil
}
func (parser *Parser) parseLiteral() (*Segment, error) {
	re := parser.GetRegexp(resttools.RegexLiteral)
	return parser.parseToSegment(re, Literal), nil
}