2020-11-03 23:48:43 +00:00
|
|
|
package yqlib
|
2020-09-18 07:08:33 +00:00
|
|
|
|
2020-12-17 03:02:54 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
2020-09-18 07:08:33 +00:00
|
|
|
|
2021-01-12 23:18:53 +00:00
|
|
|
type ExpressionNode struct {
|
2020-10-20 04:33:20 +00:00
|
|
|
Operation *Operation
|
2022-02-07 00:55:55 +00:00
|
|
|
LHS *ExpressionNode
|
|
|
|
RHS *ExpressionNode
|
2020-09-25 06:23:33 +00:00
|
|
|
}
|
2020-09-18 07:08:33 +00:00
|
|
|
|
2022-02-01 03:47:51 +00:00
|
|
|
type ExpressionParserInterface interface {
|
2021-01-12 23:18:53 +00:00
|
|
|
ParseExpression(expression string) (*ExpressionNode, error)
|
2020-09-25 06:23:33 +00:00
|
|
|
}
|
2020-09-18 07:08:33 +00:00
|
|
|
|
2021-01-12 23:18:53 +00:00
|
|
|
type expressionParserImpl struct {
|
2021-02-25 05:47:55 +00:00
|
|
|
pathTokeniser expressionTokeniser
|
|
|
|
pathPostFixer expressionPostFixer
|
2020-09-25 06:23:33 +00:00
|
|
|
}
|
2020-09-18 07:08:33 +00:00
|
|
|
|
2022-02-01 03:47:51 +00:00
|
|
|
func newExpressionParser() ExpressionParserInterface {
|
2022-07-13 04:45:28 +00:00
|
|
|
return &expressionParserImpl{newParticipleLexer(), newExpressionPostFixer()}
|
2020-09-18 07:08:33 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 23:18:53 +00:00
|
|
|
func (p *expressionParserImpl) ParseExpression(expression string) (*ExpressionNode, error) {
|
2022-01-27 02:54:29 +00:00
|
|
|
log.Debug("Parsing expression: [%v]", expression)
|
2021-02-25 05:47:55 +00:00
|
|
|
tokens, err := p.pathTokeniser.Tokenise(expression)
|
2020-10-08 23:59:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-10-20 04:33:20 +00:00
|
|
|
var Operations []*Operation
|
2021-02-25 05:47:55 +00:00
|
|
|
Operations, err = p.pathPostFixer.ConvertToPostfix(tokens)
|
2020-10-08 23:59:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-01-12 23:18:53 +00:00
|
|
|
return p.createExpressionTree(Operations)
|
2020-10-08 23:59:03 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 23:18:53 +00:00
|
|
|
func (p *expressionParserImpl) createExpressionTree(postFixPath []*Operation) (*ExpressionNode, error) {
|
|
|
|
var stack = make([]*ExpressionNode, 0)
|
2020-09-25 06:23:33 +00:00
|
|
|
|
2020-10-13 01:51:37 +00:00
|
|
|
if len(postFixPath) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2020-10-20 04:33:20 +00:00
|
|
|
for _, Operation := range postFixPath {
|
2021-01-12 23:18:53 +00:00
|
|
|
var newNode = ExpressionNode{Operation: Operation}
|
2020-10-20 04:33:20 +00:00
|
|
|
log.Debugf("pathTree %v ", Operation.toString())
|
|
|
|
if Operation.OperationType.NumArgs > 0 {
|
|
|
|
numArgs := Operation.OperationType.NumArgs
|
2020-10-16 01:29:26 +00:00
|
|
|
if numArgs == 1 {
|
2020-12-17 03:02:54 +00:00
|
|
|
if len(stack) < 1 {
|
|
|
|
return nil, fmt.Errorf("'%v' expects 1 arg but received none", strings.TrimSpace(Operation.StringValue))
|
|
|
|
}
|
2020-10-12 01:24:59 +00:00
|
|
|
remaining, rhs := stack[:len(stack)-1], stack[len(stack)-1]
|
2022-02-07 00:55:55 +00:00
|
|
|
newNode.RHS = rhs
|
2020-10-12 01:24:59 +00:00
|
|
|
stack = remaining
|
2020-10-17 11:10:47 +00:00
|
|
|
} else if numArgs == 2 {
|
2020-12-17 03:02:54 +00:00
|
|
|
if len(stack) < 2 {
|
|
|
|
return nil, fmt.Errorf("'%v' expects 2 args but there is %v", strings.TrimSpace(Operation.StringValue), len(stack))
|
|
|
|
}
|
2020-10-12 01:24:59 +00:00
|
|
|
remaining, lhs, rhs := stack[:len(stack)-2], stack[len(stack)-2], stack[len(stack)-1]
|
2022-02-07 00:55:55 +00:00
|
|
|
newNode.LHS = lhs
|
|
|
|
newNode.RHS = rhs
|
2020-10-12 01:24:59 +00:00
|
|
|
stack = remaining
|
|
|
|
}
|
2020-09-25 06:23:33 +00:00
|
|
|
}
|
|
|
|
stack = append(stack, &newNode)
|
|
|
|
}
|
|
|
|
if len(stack) != 1 {
|
2022-10-08 01:44:47 +00:00
|
|
|
return nil, fmt.Errorf("bad expression, please check expression syntax")
|
2020-09-18 07:08:33 +00:00
|
|
|
}
|
2020-09-25 06:23:33 +00:00
|
|
|
return stack[0], nil
|
2020-09-18 07:08:33 +00:00
|
|
|
}
|