Skip to content

Commit

Permalink
Merge pull request PaesslerAG#67 from lafriks-fork/fix/compact_operat…
Browse files Browse the repository at this point in the history
…ors_test

Fix to accept operator and extension without space between
  • Loading branch information
generikvault committed Oct 21, 2021
2 parents 2be8504 + 770a9b8 commit ab23661
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
5 changes: 5 additions & 0 deletions gval_noparameter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ func TestNoParameter(t *testing.T) {
expression: "2 > 1",
want: true,
},
{
name: "Equal test minus numbers and no spaces",
expression: "-1==-1",
want: true,
},
{

name: "Compound boolean",
Expand Down
6 changes: 3 additions & 3 deletions gval_parsingFailure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ func TestParsingFailure(t *testing.T) {
{
name: "Invalid equality comparator",
expression: "1 === 1",
wantErr: unknownOp("==="),
wantErr: unexpected(`"="`, "extension"),
},
{
name: "Too many characters for logical operator",
expression: "true &&& false",
wantErr: unknownOp("&&&"),
wantErr: unexpected(`"&"`, "extension"),
},
{

name: "Too many characters for logical operator",
expression: "true ||| false",
wantErr: unknownOp("|||"),
wantErr: unexpected(`"|"`, "extension"),
},
{

Expand Down
10 changes: 10 additions & 0 deletions operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"reflect"
"strconv"
"strings"
)

type stage struct {
Expand Down Expand Up @@ -53,6 +54,15 @@ func (l Language) isSymbolOperation(r rune) bool {
return in
}

func (l Language) isOperatorPrefix(op string) bool {
for k := range l.operators {
if strings.HasPrefix(k, op) {
return true
}
}
return false
}

func (op *infix) initiate(name string) {
f := func(a, b interface{}) (interface{}, error) {
return nil, fmt.Errorf("invalid operation (%T) %s (%T)", a, name, b)
Expand Down
2 changes: 1 addition & 1 deletion parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (p *Parser) parseOperator(c context.Context, stack *stageStack, eval Evalua
mustOp := false
if p.isSymbolOperation(scan) {
scan = p.Peek()
for p.isSymbolOperation(scan) {
for p.isSymbolOperation(scan) && p.isOperatorPrefix(op+string(scan)) {
mustOp = true
op += string(scan)
p.Next()
Expand Down

0 comments on commit ab23661

Please sign in to comment.