2020-09-20 12:40:09 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
lex "github.com/timtadh/lexmachine"
|
|
|
|
)
|
|
|
|
|
|
|
|
var precedenceMap map[int]int
|
|
|
|
|
|
|
|
type PathElementType uint32
|
|
|
|
|
|
|
|
const (
|
|
|
|
PathKey PathElementType = 1 << iota
|
|
|
|
ArrayIndex
|
|
|
|
Operation
|
|
|
|
)
|
|
|
|
|
|
|
|
type OperationType uint32
|
|
|
|
|
|
|
|
const (
|
|
|
|
None OperationType = 1 << iota
|
2020-09-24 03:20:02 +00:00
|
|
|
Traverse
|
2020-09-20 12:40:09 +00:00
|
|
|
Or
|
|
|
|
And
|
|
|
|
Equals
|
2020-09-24 03:20:02 +00:00
|
|
|
EqualsSelf
|
2020-09-20 12:40:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type PathElement struct {
|
|
|
|
PathElementType PathElementType
|
|
|
|
OperationType OperationType
|
|
|
|
Value interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// debugging purposes only
|
|
|
|
func (p *PathElement) toString() string {
|
2020-09-24 03:20:02 +00:00
|
|
|
var result string = ``
|
2020-09-20 12:40:09 +00:00
|
|
|
switch p.PathElementType {
|
|
|
|
case PathKey:
|
2020-09-24 03:20:02 +00:00
|
|
|
result = result + fmt.Sprintf("PathKey - '%v'\n", p.Value)
|
2020-09-20 12:40:09 +00:00
|
|
|
case ArrayIndex:
|
2020-09-24 03:20:02 +00:00
|
|
|
result = result + fmt.Sprintf("ArrayIndex - '%v'\n", p.Value)
|
2020-09-20 12:40:09 +00:00
|
|
|
case Operation:
|
|
|
|
result = result + "Operation - "
|
|
|
|
switch p.OperationType {
|
|
|
|
case Or:
|
|
|
|
result = result + "OR\n"
|
|
|
|
case And:
|
|
|
|
result = result + "AND\n"
|
|
|
|
case Equals:
|
|
|
|
result = result + "EQUALS\n"
|
2020-09-24 03:20:02 +00:00
|
|
|
case EqualsSelf:
|
|
|
|
result = result + "EQUALS SELF\n"
|
|
|
|
case Traverse:
|
|
|
|
result = result + "TRAVERSE\n"
|
2020-09-20 12:40:09 +00:00
|
|
|
}
|
2020-09-24 03:20:02 +00:00
|
|
|
|
2020-09-20 12:40:09 +00:00
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
var operationTypeMapper map[int]OperationType
|
|
|
|
|
|
|
|
func initMaps() {
|
|
|
|
precedenceMap = make(map[int]int)
|
|
|
|
operationTypeMapper = make(map[int]OperationType)
|
|
|
|
|
|
|
|
precedenceMap[TokenIds["("]] = 0
|
|
|
|
|
|
|
|
precedenceMap[TokenIds["OR_OPERATOR"]] = 10
|
|
|
|
operationTypeMapper[TokenIds["OR_OPERATOR"]] = Or
|
|
|
|
|
|
|
|
precedenceMap[TokenIds["AND_OPERATOR"]] = 20
|
|
|
|
operationTypeMapper[TokenIds["AND_OPERATOR"]] = And
|
|
|
|
|
|
|
|
precedenceMap[TokenIds["EQUALS_OPERATOR"]] = 30
|
|
|
|
operationTypeMapper[TokenIds["EQUALS_OPERATOR"]] = Equals
|
2020-09-24 03:20:02 +00:00
|
|
|
|
|
|
|
precedenceMap[TokenIds["EQUALS_SELF_OPERATOR"]] = 30
|
|
|
|
operationTypeMapper[TokenIds["EQUALS_SELF_OPERATOR"]] = EqualsSelf
|
|
|
|
|
|
|
|
precedenceMap[TokenIds["TRAVERSE_OPERATOR"]] = 40
|
|
|
|
operationTypeMapper[TokenIds["TRAVERSE_OPERATOR"]] = Traverse
|
2020-09-20 12:40:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func createOperationPathElement(opToken *lex.Token) PathElement {
|
2020-09-24 03:20:02 +00:00
|
|
|
var pathElement = PathElement{PathElementType: Operation, OperationType: operationTypeMapper[opToken.Type]}
|
2020-09-20 12:40:09 +00:00
|
|
|
return pathElement
|
|
|
|
}
|
|
|
|
|
|
|
|
type PathPostFixer interface {
|
|
|
|
ConvertToPostfix([]*lex.Token) ([]*PathElement, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type pathPostFixer struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPathPostFixer() PathPostFixer {
|
|
|
|
return &pathPostFixer{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func popOpToResult(opStack []*lex.Token, result []*PathElement) ([]*lex.Token, []*PathElement) {
|
|
|
|
var operatorToPushToPostFix *lex.Token
|
|
|
|
opStack, operatorToPushToPostFix = opStack[0:len(opStack)-1], opStack[len(opStack)-1]
|
|
|
|
var pathElement = createOperationPathElement(operatorToPushToPostFix)
|
|
|
|
return opStack, append(result, &pathElement)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *pathPostFixer) ConvertToPostfix(infixTokens []*lex.Token) ([]*PathElement, error) {
|
|
|
|
var result []*PathElement
|
|
|
|
// surround the whole thing with quotes
|
|
|
|
var opStack = []*lex.Token{&lex.Token{Type: TokenIds["("]}}
|
|
|
|
var tokens = append(infixTokens, &lex.Token{Type: TokenIds[")"]})
|
|
|
|
|
|
|
|
for _, token := range tokens {
|
|
|
|
switch token.Type {
|
2020-09-24 03:28:47 +00:00
|
|
|
case TokenIds["PATH_KEY"], TokenIds["ARRAY_INDEX"], TokenIds["[+]"], TokenIds["[*]"], TokenIds["**"]:
|
2020-09-24 03:20:02 +00:00
|
|
|
var pathElement = PathElement{PathElementType: PathKey, Value: token.Value}
|
|
|
|
result = append(result, &pathElement)
|
2020-09-20 12:40:09 +00:00
|
|
|
case TokenIds["("]:
|
|
|
|
opStack = append(opStack, token)
|
2020-09-24 03:20:02 +00:00
|
|
|
case TokenIds["OR_OPERATOR"], TokenIds["AND_OPERATOR"], TokenIds["EQUALS_OPERATOR"], TokenIds["EQUALS_SELF_OPERATOR"], TokenIds["TRAVERSE_OPERATOR"]:
|
2020-09-20 12:40:09 +00:00
|
|
|
var currentPrecedence = precedenceMap[token.Type]
|
|
|
|
// pop off higher precedent operators onto the result
|
2020-09-24 03:20:02 +00:00
|
|
|
for len(opStack) > 0 && precedenceMap[opStack[len(opStack)-1].Type] >= currentPrecedence {
|
2020-09-20 12:40:09 +00:00
|
|
|
opStack, result = popOpToResult(opStack, result)
|
|
|
|
}
|
|
|
|
// add this operator to the opStack
|
|
|
|
opStack = append(opStack, token)
|
|
|
|
case TokenIds[")"]:
|
|
|
|
for len(opStack) > 0 && opStack[len(opStack)-1].Type != TokenIds["("] {
|
|
|
|
opStack, result = popOpToResult(opStack, result)
|
|
|
|
}
|
|
|
|
if len(opStack) == 0 {
|
|
|
|
return nil, errors.New("Bad path expression, got close brackets without matching opening bracket")
|
|
|
|
}
|
|
|
|
// now we should have ( as the last element on the opStack, get rid of it
|
|
|
|
opStack = opStack[0 : len(opStack)-1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|