Skip to content

Commit

Permalink
Fixed bug where functions with no parameters wouldn't play nice with …
Browse files Browse the repository at this point in the history
…modifiers during stage planning
  • Loading branch information
Knetic committed Sep 17, 2016
1 parent 3fa1fa4 commit 17f6df8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
25 changes: 25 additions & 0 deletions parsing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,31 @@ func TestConstantParsing(test *testing.T) {
},
},
},
TokenParsingTest{
Name: "Function with modifier afterwards (#28)",
Input: "foo() + 1",
Functions: map[string]ExpressionFunction{"foo": noop},
Expected: []ExpressionToken{
ExpressionToken{
Kind: FUNCTION,
Value: noop,
},
ExpressionToken{
Kind: CLAUSE,
},
ExpressionToken{
Kind: CLAUSE_CLOSE,
},
ExpressionToken{
Kind: MODIFIER,
Value: "+",
},
ExpressionToken{
Kind: NUMERIC,
Value: 1.0,
},
},
},
}

tokenParsingTests = combineWhitespaceExpressions(tokenParsingTests)
Expand Down
10 changes: 7 additions & 3 deletions stagePlanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package govaluate
import (
"errors"
"time"
"fmt"
)

var stageSymbolMap = map[OperatorSymbol]evaluationOperator{
Expand Down Expand Up @@ -337,8 +338,10 @@ func planValue(stream *tokenStream) (*evaluationStage, error) {

case CLAUSE_CLOSE:

// when functions do not have anything within the parens, the CLAUSE_CLOSE is not consumed. This consumes it.
return planTokens(stream)
// when functions have empty params, this will be hit. In this case, we don't have any evaluation stage to do,
// so we just return nil so that the stage planner continues on its way.
stream.rewind()
return nil, nil

case VARIABLE:
operator = makeParameterStage(token.Value.(string))
Expand All @@ -360,7 +363,8 @@ func planValue(stream *tokenStream) (*evaluationStage, error) {
}

if operator == nil {
return nil, errors.New("Unable to plan token kind: " + GetTokenKindString(token.Kind))
errorMsg := fmt.Sprintf("Unable to plan token kind: '%s', value: '%v'", GetTokenKindString(token.Kind), token.Value)
return nil, errors.New(errorMsg)
}

return &evaluationStage{
Expand Down

0 comments on commit 17f6df8

Please sign in to comment.