// parse dependency with possible version restriction
554451
func parseDependency(dep string, deps []*Dependency) ([]*Dependency, error) {
	var name string
	var dependency *Dependency
	index := -1

	if dep == "" {
		return deps, nil
	}

	if dep[0] == '-' {
		return nil, fmt.Errorf("invalid dependency name")
	}

	i := 0
	for _, c := range dep {
		if !isValidPkgnameChar(c) {
			break
		}
		i++
	}

	// check if the dependency has been set before
	name = dep[0:i]
	for n, d := range deps {
		if d.Name == name {
			index = n
			break
		}
	}

	dependency = &Dependency{
		Name: name,
		sgt:  false,
		slt:  false,
	}

	if len(dep) != len(name) {
		var eq bytes.Buffer
		for _, c := range dep[i:] {
			if c == '<' || c == '>' || c == '=' {
				i++
				eq.WriteRune(c)
				continue
			}
			break
		}

		version, err := NewCompleteVersion(dep[i:])
		if err != nil {
			return nil, err
		}

		switch eq.String() {
		case "=":
			dependency.MinVer = version
			dependency.MaxVer = version
		case "<=":
			dependency.MaxVer = version
		case ">=":
			dependency.MinVer = version
		case "<":
			dependency.MaxVer = version
			dependency.slt = true
		case ">":
			dependency.MinVer = version
			dependency.sgt = true
		}
	}

	if index == -1 {
		deps = append(deps, dependency)
	} else {
		deps[index] = deps[index].Restrict(dependency)
	}

	return deps, nil
}
340093
func ParseVersion(version string) (ver Version, err error) {
	if version == "" {
		return
	}

	parts := strings.SplitN(version, ".", 4)
	if len(parts) < 2 || len(parts) > 4 {
		err = errors.New(fmt.Sprintf(
			"Version is not in form major.minor(.bugfix.prerelease) (got: %v)",
			version))
		return
	}

	var major, minor, bugfix, prerelease int
	if major, err = parse(parts[0], version, "major"); err != nil {
		return
	}
	if minor, err = parse(parts[1], version, "minor"); err != nil {
		return
	}

	if len(parts) > 2 {
		if bugfix, err = parse(parts[2], version, "bugfix"); err != nil {
			return
		}

		if len(parts) > 3 {
			if prerelease, err = parse(parts[3], version, "prerelease"); err != nil {
				return
			}
			if prerelease == 0 {
				err = errors.New(fmt.Sprintf(
					"Invalid value %v for prerelease; should be 1 or 2 (got: %v)",
					prerelease, version))
				return
			}
		}
	}

	return newVersion(major, minor, bugfix, prerelease), nil
}
818100
func parseVersion(v string) (semver.Version, error) {
	parts := strings.Split(v, ".")
	var err error
	for i := len(parts); i > 0; i-- {
		var ver semver.Version
		ver, err = semver.ParseTolerant(strings.Join(parts[:i], "."))
		if err == nil {
			return ver, nil
		}
	}
	return semver.Version{}, err
}
1049114
func (p *Parser) Parse(srcPath string, files []models.Path) (*Result, error) {
	b, err := p.readFile(srcPath)
	if err != nil {
		return nil, err
	}
	fset := token.NewFileSet()
	f, err := p.parseFile(fset, srcPath)
	if err != nil {
		return nil, err
	}
	fs, err := p.parseFiles(fset, f, files)
	if err != nil {
		return nil, err
	}
	return &Result{
		Header: &models.Header{
			Comments: parsePkgComment(f, f.Package),
			Package:  f.Name.String(),
			Imports:  parseImports(f.Imports),
			Code:     goCode(b, f),
		},
		Funcs: p.parseFunctions(fset, f, fs),
	}, nil
}
298492
func Parse(args ...string) (Value, error) {
	v, _, err := ParseWithAliases(args...)
	return v, err
}
581942
func (pub *DSAPublicKey) Parse(in []byte) (index []byte, ok bool) {
	var typeTag uint16
	if index, typeTag, ok = gotrax.ExtractShort(in); !ok || typeTag != dsaKeyTypeValue {
		return in, false
	}
	if index, pub.P, ok = gotrax.ExtractMPI(index); !ok {
		return in, false
	}
	if index, pub.Q, ok = gotrax.ExtractMPI(index); !ok {
		return in, false
	}
	if index, pub.G, ok = gotrax.ExtractMPI(index); !ok {
		return in, false
	}
	if index, pub.Y, ok = gotrax.ExtractMPI(index); !ok {
		return in, false
	}
	return
}
1221937
func Parse(v interface{}) ([]string, error) {
	if command, _, err := generateCommand(v, true); err == nil {
		return command, err
	} else {
		return nil, err
	}
}
627343
func (c *Conf) Parse(path string) {
	c.loadDefaults()

	if path == "" {
		log.Info("Loaded configuration defaults")
		return
	}

	if _, err := toml.DecodeFile(path, c); err != nil {
		panic(err)
	}
}
412081
func ParseVersionsFile(r io.Reader) (VersionsFile, error) {
	res := VersionsFile{}

	lineNo := 0
	makeError := func(fmtStr string, args ...interface{}) error {
		args = append([]interface{}{lineNo}, args...)
		return fmt.Errorf("failed to parse versions file (line %d): "+fmtStr, args...)
	}

	const (
		stWaitingPkg = "a package name"
		stWaitingVer = "a package version"
		stWaitingIID = "an instance ID"
		stWaitingNL  = "a new line"
	)
	state := stWaitingPkg
	pkg := ""
	ver := ""
	iid := ""

	scanner := bufio.NewScanner(r)
	for scanner.Scan() {
		lineNo++

		line := strings.TrimSpace(scanner.Text())

		// Comments are grammatically insignificant (unlike empty lines), so skip
		// the completely.
		if len(line) > 0 && line[0] == '#' {
			continue
		}

		switch state {
		case stWaitingPkg:
			if line == "" {
				continue // can have more than one empty line between triples
			}
			pkg = line
			if err := common.ValidatePackageName(pkg); err != nil {
				return nil, makeError("%s", err)
			}
			state = stWaitingVer

		case stWaitingVer:
			if line == "" {
				return nil, makeError("expecting a version name, not a new line")
			}
			ver = line
			if err := common.ValidateInstanceVersion(ver); err != nil {
				return nil, makeError("%s", err)
			}
			state = stWaitingIID

		case stWaitingIID:
			if line == "" {
				return nil, makeError("expecting an instance ID, not a new line")
			}
			iid = line
			if err := common.ValidateInstanceID(iid, common.AnyHash); err != nil {
				return nil, makeError("%s", err)
			}
			if err := res.AddVersion(pkg, ver, iid); err != nil {
				panic(err) // impossible, everything has been validated already
			}
			pkg, ver, iid = "", "", ""
			state = stWaitingNL

		case stWaitingNL:
			if line == "" {
				state = stWaitingPkg
				continue
			}
			return nil, makeError("expecting an empty line between each version definition triple")
		}
	}

	if state != stWaitingPkg && state != stWaitingNL {
		return nil, makeError("unexpected EOF, expecting %s", state)
	}

	if err := scanner.Err(); err != nil {
		return nil, err
	}
	return res, nil
}
473301
func Parse(filePath string, objName string) (*ParsedPkg, error) {
	fs := token.NewFileSet()
	pkg, err := parser.ParseFile(fs, filePath, nil, parser.AllErrors)
	if err != nil {
		return nil, err
	}
	p := &ParsedPkg{}
	p.Name = pkg.Name.Name
	obj, exists := pkg.Scope.Objects[objName]
	if !exists {
		return nil, fmt.Errorf("could not find object %s in %s", objName, filePath)
	}
	if obj.Kind != ast.Typ {
		return nil, fmt.Errorf("exected type, got %s", obj.Kind)
	}
	spec, ok := obj.Decl.(*ast.TypeSpec)
	if !ok {
		return nil, errUnexpectedType{"*ast.TypeSpec", obj.Decl}
	}
	iface, ok := spec.Type.(*ast.InterfaceType)
	if !ok {
		return nil, errUnexpectedType{"*ast.InterfaceType", spec.Type}
	}

	p.Functions, err = parseInterface(iface)
	if err != nil {
		return nil, err
	}

	// figure out what imports will be needed
	imports := make(map[string]importSpec)
	for _, f := range p.Functions {
		args := append(f.Args, f.Returns...)
		for _, arg := range args {
			if len(arg.PackageSelector) == 0 {
				continue
			}

			for _, i := range pkg.Imports {
				if i.Name != nil {
					if i.Name.Name != arg.PackageSelector {
						continue
					}
					imports[i.Path.Value] = importSpec{Name: arg.PackageSelector, Path: i.Path.Value}
					break
				}

				_, name := path.Split(i.Path.Value)
				splitName := strings.Split(name, "-")
				if len(splitName) > 1 {
					name = splitName[len(splitName)-1]
				}
				// import paths have quotes already added in, so need to remove them for name comparison
				name = strings.TrimPrefix(name, `"`)
				name = strings.TrimSuffix(name, `"`)
				if name == arg.PackageSelector {
					imports[i.Path.Value] = importSpec{Path: i.Path.Value}
					break
				}
			}
		}
	}

	for _, spec := range imports {
		p.Imports = append(p.Imports, spec)
	}

	return p, nil
}
81196
func (b *BuildSource) Parse(pkg *genbase.PackageInfo, typeInfos genbase.TypeInfos) error {
	if b.g == nil {
		b.g = genbase.NewGenerator(pkg)
	}
	b.pkg = pkg
	b.typeInfos = typeInfos

	if b.UseDatastoreWrapper {
		b.g.AddImport("go.mercari.io/datastore", "")
	} else {
		b.g.AddImport("google.golang.org/appengine/datastore", "")
	}
	if !b.InlineInterfaces {
		b.g.AddImport("github.com/favclip/qbg/qbgutils", "")
	}

	for _, typeInfo := range typeInfos {
		err := b.parseStruct(typeInfo)
		if err != nil {
			return err
		}
	}

	return nil
}
1321339
func parse(addr string) (string, error) {
	proxyurl, err := url.Parse(addr)
	if err != nil || !strings.HasPrefix(proxyurl.Scheme, "http") {
		proxyurl, err = url.Parse("http://" + addr)
		if err != nil {
			return "", trace.Wrap(err)
		}
	}

	return proxyurl.Host, nil
}
724272
func ParseVersion(version string) (int, int, int, error) {
	var major, minor, micro int
	if version == "" {
		return -1, -1, -1, fmt.Errorf("invalid version %q: the version is empty", version)
	}

	parts := strings.Split(version, ".")
	if len(parts) >= 4 {
		return -1, -1, -1, fmt.Errorf("invalid version %q: too many parts", version)
	}

	major, err := strconv.Atoi(parts[0])
	if err != nil {
		return -1, -1, -1, fmt.Errorf("failed to convert major version part %q: %v", parts[0], err)
	}

	if len(parts) >= 2 {
		minor, err = strconv.Atoi(parts[1])
		if err != nil {
			return -1, -1, -1, fmt.Errorf("failed to convert minor version part %q: %v", parts[1], err)
		}
	}

	if len(parts) >= 3 {
		micro, err = strconv.Atoi(parts[2])
		if err != nil {
			return -1, -1, -1, fmt.Errorf("failed to convert micro version part %q: %v", parts[2], err)
		}
	}

	return major, minor, micro, nil
}
277397
func Parse(src []byte) (*ast.File, error) {
	p := newParser(src)
	return p.Parse()
}
53930
func ParseReleaseVersion(bytes []byte) (int, error) {
	var hook BuildHookResponse
	if err := json.Unmarshal(bytes, &hook); err != nil {
		return 0, fmt.Errorf("invalid application json configuration")
	}

	if hook.Release == nil {
		return 0, fmt.Errorf("invalid application version")
	}

	return hook.Release["version"], nil
}
680002
func (s *StringSource) Parse() (tree.RawPolicy, error) {
	return parseLines(s.Name, strings.Split(s.Content, "\n"))
}
980202
func (b *boundedBuilder) Parse(s string) (*Literal, error) {
	l, err := defaultBuilder.Parse(s)
	if err != nil {
		return nil, err
	}
	t := l.Type()
	switch t {
	case Text:
		if text, err := l.Text(); err != nil || len(text) > b.max {
			return nil, fmt.Errorf("literal.Parse: cannot create literal due to size of %v (%d>%d)", t, len(text), b.max)
		}
	case Blob:
		if blob, err := l.Blob(); err != nil || len(blob) > b.max {
			return nil, fmt.Errorf("literal.Parse: cannot create literal due to size of %v (%d>%d)", t, len(blob), b.max)
		}
	}
	return l, nil
}