Skip to content

Commit

Permalink
Merge pull request #9 from yaacov/add-like-op-to-semantic-walk
Browse files Browse the repository at this point in the history
Add like op to semantic walk
  • Loading branch information
yaacov authored Jun 1, 2019
2 parents 2ff217e + 554de2c commit 36e7e9f
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion pkg/walkers/semantics/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package semantics
import (
"fmt"
"regexp"
"strings"

"github.com/yaacov/tree-search-language/pkg/tsl"
)
Expand Down Expand Up @@ -83,7 +84,7 @@ func Walk(n tsl.Node, eval EvalFunc) (bool, error) {
// Implement tree semantics.
switch n.Func {
case tsl.EqOp, tsl.NotEqOp, tsl.LtOp, tsl.LteOp, tsl.GtOp, tsl.GteOp, tsl.RegexOp, tsl.NotRegexOp,
tsl.BetweenOp, tsl.NotBetweenOp, tsl.NotInOp, tsl.InOp:
tsl.BetweenOp, tsl.NotBetweenOp, tsl.NotInOp, tsl.InOp, tsl.LikeOp, tsl.NotLikeOp:
r := n.Right.(tsl.Node)

switch l.Func {
Expand Down Expand Up @@ -209,6 +210,18 @@ func handleStringOp(n tsl.Node, eval EvalFunc) (bool, error) {
return left > right, nil
case tsl.GteOp:
return left >= right, nil
case tsl.LikeOp:
valid, err := regexp.Compile(likeToRegExp(right))
if err != nil {
return false, tsl.UnexpectedLiteralError{Literal: right}
}
return valid.MatchString(left), nil
case tsl.NotLikeOp:
valid, err := regexp.Compile(likeToRegExp(right))
if err != nil {
return false, tsl.UnexpectedLiteralError{Literal: right}
}
return !valid.MatchString(left), nil
case tsl.RegexOp:
valid, err := regexp.Compile(right)
if err != nil {
Expand Down Expand Up @@ -339,3 +352,12 @@ func handleLogicalOp(n tsl.Node, eval EvalFunc) (bool, error) {

return false, tsl.UnexpectedLiteralError{Literal: n.Func}
}

func likeToRegExp(l string) string {
e := regexp.QuoteMeta(l)
e = strings.Replace(e, "%", ".*", -1)
e = strings.Replace(e, "_", ".", -1)
e = fmt.Sprintf("^%s$", e)

return e
}

0 comments on commit 36e7e9f

Please sign in to comment.