func()

in variables.go [284:322]


func (st parseState) finalize(pathSep string, maxIdx int64, enableNumKeys, allowEscapePath bool) (varEvaler, error) {
	if !st.isvar {
		return nil, errors.New("fatal: processing non-variable state")
	}
	if len(st.pieces[stLeft]) == 0 {
		return nil, errors.New("empty expansion")
	}

	if st.st == stLeft {
		pieces := st.pieces[stLeft]

		if len(pieces) == 0 {
			return constExp(""), nil
		}

		if len(pieces) == 1 {
			if str, ok := pieces[0].(constExp); ok {
				return newReference(parsePath(string(str), pathSep, maxIdx, enableNumKeys, allowEscapePath)), nil
			}
		}

		return &expansionSingle{&splice{pieces}, pathSep}, nil
	}

	extract := func(pieces []varEvaler) varEvaler {
		switch len(pieces) {
		case 0:
			return constExp("")
		case 1:
			return pieces[0]
		default:
			return &splice{pieces}
		}
	}

	left := extract(st.pieces[stLeft])
	right := extract(st.pieces[stRight])
	return makeOpExpansion(left, right, st.op, pathSep), nil
}