func (p *Parser) parseRequirement() (*Requirement, error) {
	key, operator, err := p.parseKeyAndInferOperator()
	if err != nil {
		return nil, err
	}
	if operator == ExistsOperator {  
		return NewRequirement(key, operator, nil)
	}
	operator, err = p.parseOperator()
	if err != nil {
		return nil, err
	}
	var values util.StringSet
	switch operator {
	case InOperator, NotInOperator:
		values, err = p.parseValues()
	case EqualsOperator, DoubleEqualsOperator, NotEqualsOperator:
		values, err = p.parseExactValue()
	}
	if err != nil {
		return nil, err
	}
	return NewRequirement(key, operator, values)
}
parse a Requirement data structure
package labels
import (
	"bytes"
	"fmt"
	"sort"
	"strings"
	"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
	"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
)
type Selector interface {
	Matches(Labels) bool
	Empty() bool
	RequiresExactMatch(label string) (value string, found bool)
	String() string
}
func Everything() Selector {
	return andTerm{}
}
type hasTerm struct {
	label, value string
}
func (t *hasTerm) Matches(ls Labels) bool {
	return ls.Get(t.label) == t.value
}
func (t *hasTerm) Empty() bool {
	return false
}
func (t *hasTerm) RequiresExactMatch(label string) (value string, found bool) {
	if t.label == label {
		return t.value, true
	}
	return "", false
}
func (t *hasTerm) String() string {
	return fmt.Sprintf("%v=%v", t.label, t.value)
}
type notHasTerm struct {
	label, value string
}
func (t *notHasTerm) Matches(ls Labels) bool {
	return ls.Get(t.label) != t.value
}
func (t *notHasTerm) Empty() bool {
	return false
}
func (t *notHasTerm) RequiresExactMatch(label string) (value string, found bool) {
	return "", false
}
func (t *notHasTerm) String() string {
	return fmt.Sprintf("%v!=%v", t.label, t.value)
}
type andTerm []Selector
func (t andTerm) Matches(ls Labels) bool {
	for _, q := range t {
		if !q.Matches(ls) {
			return false
		}
	}
	return true
}
func (t andTerm) Empty() bool {
	if t == nil {
		return true
	}
	if len([]Selector(t)) == 0 {
		return true
	}
	for i := range t {
		if !t[i].Empty() {
			return false
		}
	}
	return true
}
func (t andTerm) RequiresExactMatch(label string) (string, bool) {
	if t == nil || len([]Selector(t)) == 0 {
		return "", false
	}
	for i := range t {
		if value, found := t[i].RequiresExactMatch(label); found {
			return value, found
		}
	}
	return "", false
}
func (t andTerm) String() string {
	var terms []string
	for _, q := range t {
		terms = append(terms, q.String())
	}
	return strings.Join(terms, ",")
}
type Operator string
const (
	EqualsOperator       Operator = "="
	DoubleEqualsOperator Operator = "=="
	InOperator           Operator = "in"
	NotEqualsOperator    Operator = "!="
	NotInOperator        Operator = "notin"
	ExistsOperator       Operator = "exists"
)
type LabelSelector struct {
	Requirements []Requirement
}
type ByKey []Requirement
func (a ByKey) Len() int { return len(a) }
func (a ByKey) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByKey) Less(i, j int) bool { return a[i].key < a[j].key }
type Requirement struct {
	key       string
	operator  Operator
	strValues util.StringSet
}
func NewRequirement(key string, op Operator, vals util.StringSet) (*Requirement, error) {
	if err := validateLabelKey(key); err != nil {
		return nil, err
	}
	switch op {
	case InOperator, NotInOperator:
		if len(vals) == 0 {
			return nil, fmt.Errorf("for In,NotIn operators, values set can't be empty")
		}
	case EqualsOperator, DoubleEqualsOperator, NotEqualsOperator:
		if len(vals) != 1 {
			return nil, fmt.Errorf("exact match compatibility requires one single value")
		}
	case ExistsOperator:
	default:
		return nil, fmt.Errorf("operator '%v' is not recognized", op)
	}
	for v := range vals {
		if err := validateLabelValue(v); err != nil {
			return nil, err
		}
	}
	return &Requirement{key: key, operator: op, strValues: vals}, nil
}
func (r *Requirement) Matches(ls Labels) bool {
	switch r.operator {
	case InOperator, EqualsOperator, DoubleEqualsOperator:
		if !ls.Has(r.key) {
			return false
		}
		return r.strValues.Has(ls.Get(r.key))
	case NotInOperator, NotEqualsOperator:
		if !ls.Has(r.key) {
			return true
		}
		return !r.strValues.Has(ls.Get(r.key))
	case ExistsOperator:
		return ls.Has(r.key)
	default:
		return false
	}
}
func (lsel LabelSelector) Empty() bool {
	if len(lsel.Requirements) == 0 {
		return true
	}
	return false
}
func (r *Requirement) RequiresExactMatch(label string) (string, bool) {
	if len(r.strValues) == 1 && r.operator == InOperator && r.key == label {
		return r.strValues.List()[0], true
	}
	return "", false
}
func (r *Requirement) String() string {
	var buffer bytes.Buffer
	buffer.WriteString(r.key)
	switch r.operator {
	case EqualsOperator:
		buffer.WriteString("=")
	case DoubleEqualsOperator:
		buffer.WriteString("==")
	case NotEqualsOperator:
		buffer.WriteString("!=")
	case InOperator:
		buffer.WriteString(" in ")
	case NotInOperator:
		buffer.WriteString(" notin ")
	case ExistsOperator:
		return buffer.String()
	}
	switch r.operator {
	case InOperator, NotInOperator:
		buffer.WriteString("(")
	}
	if len(r.strValues) == 1 {
		buffer.WriteString(r.strValues.List()[0])
	} else {  
		buffer.WriteString(strings.Join(r.strValues.List(), ","))
	}
	switch r.operator {
	case InOperator, NotInOperator:
		buffer.WriteString(")")
	}
	return buffer.String()
}
func (lsel LabelSelector) Matches(l Labels) bool {
	for _, req := range lsel.Requirements {
		if matches := req.Matches(l); !matches {
			return false
		}
	}
	return true
}
func (lsel LabelSelector) RequiresExactMatch(label string) (value string, found bool) {
	for _, req := range lsel.Requirements {
		if value, found = req.RequiresExactMatch(label); found {
			return value, found
		}
	}
	return "", false
}
func (lsel LabelSelector) String() string {
	var reqs []string
	for _, req := range lsel.Requirements {
		reqs = append(reqs, req.String())
	}
	return strings.Join(reqs, ",")
}
type Token int
const (
	ERROR Token = iota
	EOS          
	CPAR
	COMMA
	EEQUAL
	EQUAL
	IDENTIFIER
	IN
	NEQUAL
	NOTIN
	OPAR
	OR
)
var string2token = map[string]Token{
	")":     CPAR,
	",":     COMMA,
	"==":    EEQUAL,
	"=":     EQUAL,
	"in":    IN,
	"!=":    NEQUAL,
	"notin": NOTIN,
	"(":     OPAR,
}
type ScannedItem struct {
	tok     Token
	literal string
}
func isWhitespace(ch byte) bool {
	return ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n'
}
func isSpecialSymbol(ch byte) bool {
	switch ch {
	case '=', '!', '(', ')', ',':
		return true
	}
	return false
}
type Lexer struct {
	s string
	pos int
}
func (l *Lexer) read() (b byte) {
	b = 0
	if l.pos < len(l.s) {
		b = l.s[l.pos]
		l.pos++
	}
	return b
}
func (l *Lexer) unread() {
	l.pos--
}
func (l *Lexer) scanIdOrKeyword() (tok Token, lit string) {
	var buffer []byte
	for {
		if ch := l.read(); ch == 0 {  
			break
		} else if isSpecialSymbol(ch) || isWhitespace(ch) {
			l.unread()  
			break
		} else {
			buffer = append(buffer, ch)
		}
	}
	s := string(buffer)
	if val, ok := string2token[s]; ok {  
		return val, s
	}
	return IDENTIFIER, s  
}
func (l *Lexer) scanSpecialSymbol() (Token, string) {
	lastScannedItem := ScannedItem{}
	var buffer []byte
	for {
		if ch := l.read(); ch == 0 {
			break
		} else if isSpecialSymbol(ch) {
			buffer = append(buffer, ch)
			if token, ok := string2token[string(buffer)]; ok {
				lastScannedItem = ScannedItem{tok: token, literal: string(buffer)}
			} else if lastScannedItem.tok != 0 {
				l.unread()
				break
			}
		} else {  
			l.unread()
			break
		}
	}
	if lastScannedItem.tok == 0 {
		return ERROR, fmt.Sprintf("error expected keyword found '%s'", buffer)
	} else {
		return lastScannedItem.tok, lastScannedItem.literal
	}
}
func (l *Lexer) Lex() (tok Token, lit string) {
	ch := l.read()
	for {  
		if !isWhitespace(ch) {
			break
		}
		ch = l.read()
	}
	if ch == 0 {  
		return EOS, ""
	} else if isSpecialSymbol(ch) {
		l.unread()
		return l.scanSpecialSymbol()  
	} else {
		l.unread()
		return l.scanIdOrKeyword()
	}
}
type Parser struct {
	l            *Lexer
	scannedItems []ScannedItem
	position     int
}
type ParserContext int
const (
	KeyAndOperator ParserContext = iota
	Values
)
func (p *Parser) lookahead(context ParserContext) (Token, string) {
	tok, lit := p.scannedItems[p.position].tok, p.scannedItems[p.position].literal
	if context == Values {
		switch tok {
		case IN, NOTIN:
			tok = IDENTIFIER
		}
	}
	return tok, lit
}
func (p *Parser) consume(context ParserContext) (Token, string) {
	p.position++
	tok, lit := p.scannedItems[p.position-1].tok, p.scannedItems[p.position-1].literal
	if context == Values {
		switch tok {
		case IN, NOTIN:
			tok = IDENTIFIER
		}
	}
	return tok, lit
}
func (p *Parser) scan() {
	for {
		token, literal := p.l.Lex()
		p.scannedItems = append(p.scannedItems, ScannedItem{token, literal})
		if token == EOS {
			break
		}
	}
}
func (p *Parser) parse() ([]Requirement, error) {
	p.scan()  
	var requirements []Requirement
	for {
		tok, lit := p.lookahead(Values)
		switch tok {
		case IDENTIFIER:
			r, err := p.parseRequirement()
			if err != nil {
				return nil, fmt.Errorf("Error: ", err)
			}
			requirements = append(requirements, *r)
			t, l := p.consume(Values)
			switch t {
			case EOS:
				return requirements, nil
			case COMMA:
				t2, l2 := p.lookahead(Values)
				if t2 != IDENTIFIER {
					return nil, fmt.Errorf("Expected identifier after comma, found '%s'", l2)
				}
			default:
				return nil, fmt.Errorf("Bad value '%s', expetected comma or 'end of string'", l)
			}
		case EOS:
			return requirements, nil
		default:
			return nil, fmt.Errorf("Bad value %s. Expected identifier or 'end of string'", lit)
		}
	}
	return requirements, nil
}