func (p *parser) parseConditionTree() (*query.ConditionNode, error) {
	stack := lane.NewStack()
	errFailedToParse := errors.New("failed to parse conditions")
	for {
		if p.current = p.tokenizer.Next(); p.current == nil {
			break
		}
		switch p.current.Type {
		case tokenizer.Not:
			fallthrough
		case tokenizer.Identifier:
			condition, err := p.parseCondition()
			if err != nil {
				return nil, err
			}
			if condition == nil {
				return nil, p.currentError()
			}
			if condition.IsSubquery {
				if err := p.parseSubquery(condition); err != nil {
					return nil, err
				}
			}
			leafNode := &query.ConditionNode{Condition: condition}
			if prevNode, ok := stack.Pop().(*query.ConditionNode); !ok {
				stack.Push(leafNode)
			} else if prevNode.Condition == nil {
				prevNode.Right = leafNode
				stack.Push(prevNode)
			} else {
				return nil, errFailedToParse
			}
		case tokenizer.And, tokenizer.Or:
			leftNode, ok := stack.Pop().(*query.ConditionNode)
			if !ok {
				return nil, errFailedToParse
			}
			node := query.ConditionNode{
				Type: &p.current.Type,
				Left: leftNode,
			}
			stack.Push(&node)
		case tokenizer.OpenParen:
			stack.Push(nil)
		case tokenizer.CloseParen:
			rightNode, ok := stack.Pop().(*query.ConditionNode)
			if !ok {
				return nil, errFailedToParse
			}
			if rootNode, ok := stack.Pop().(*query.ConditionNode); !ok {
				stack.Push(rightNode)
			} else {
				rootNode.Right = rightNode
				stack.Push(rootNode)
			}
		}
	}
	if stack.Size() == 0 {
		return nil, p.currentError()
	}
	if stack.Size() > 1 {
		return nil, errFailedToParse
	}
	node, ok := stack.Pop().(*query.ConditionNode)
	if !ok {
		return nil, errFailedToParse
	}
	return node, nil
}
parseConditionTree parses the condition tree passed to the WHERE clause.
package parser
import (
	"errors"
	"os"
	"github.com/oleiade/lane"
	"github.com/kashav/fsql/query"
	"github.com/kashav/fsql/tokenizer"
)