Skip to content

Commit

Permalink
Group eval fix (#162)
Browse files Browse the repository at this point in the history
Fix group eval and printing of groups.
  • Loading branch information
ohler55 committed Feb 21, 2024
1 parent e193e47 commit a175459
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

The structure and content of this file follows [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [1.21.3] - 2024-02-20
### Fixed
- Evaluation of a not group such as `!(@.x == 2)` is now correct.

## [1.21.2] - 2024-02-14
### Fixed
- Reworked the jp equation parser to eliminate some parsing issues.
Expand Down
16 changes: 12 additions & 4 deletions jp/equation.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ type Equation struct {
right *Equation
}

// MustParseEquation parses the string argument and returns an Equation or panics.
func MustParseEquation(str string) (eq *Equation) {
p := &parser{buf: []byte(str)}
eq = precedentCorrect(p.readEq())

return reduceGroups(eq, nil)
}

// Script creates and returns a Script that implements the equation.
func (e *Equation) Script() *Script {
if e.o == nil {
Expand Down Expand Up @@ -260,10 +268,10 @@ func (e *Equation) Append(buf []byte, parens bool) []byte {
buf = append(buf, ',', ' ')
buf = e.right.Append(buf, false)
buf = append(buf, ')')
// case group.code:
// if e.left != nil {
// buf = e.left.Append(buf, e.left.o != nil && e.left.o.prec >= e.o.prec)
// }
case group.code:
if e.left != nil {
buf = e.left.Append(buf, e.left.o != nil && e.left.o.prec >= e.o.prec)
}
default:
if e.left != nil {
buf = e.left.Append(buf, e.left.o != nil && e.left.o.prec >= e.o.prec)
Expand Down
5 changes: 5 additions & 0 deletions jp/equation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,8 @@ func TestEquationScript(t *testing.T) {
eq = jp.Not(nil)
tt.Equal(t, "(!null)", eq.Script().String())
}

func TestParseEquation(t *testing.T) {
eq := jp.MustParseEquation("!(@.text ~= /(?i)notexpected/)")
tt.Equal(t, "!(@.text ~= /(?i)notexpected/)", eq.String())
}
9 changes: 3 additions & 6 deletions jp/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,7 @@ func NewScript(str string) (s *Script, err error) {

// MustNewScript parses the string argument and returns a script or an error.
func MustNewScript(str string) (s *Script) {
p := &parser{buf: []byte(str)}

eq := precedentCorrect(p.readEq())
eq = reduceGroups(eq, nil)

return eq.Script()
return MustParseEquation(str).Script()
}

// Append a string representation of the fragment to the buffer and then
Expand Down Expand Up @@ -463,6 +458,8 @@ func evalStack(sstack []any) []any {
right = sstack[i+2]
}
switch o.code {
case group.code:
sstack[i] = left
case eq.code:
if left == right {
sstack[i] = true
Expand Down
8 changes: 8 additions & 0 deletions jp/script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,3 +392,11 @@ func BenchmarkOjScriptDev(b *testing.B) {
// result := s.Eval([]any{}, []any{5})
// fmt.Printf("*** %s\n", result)
// }

func TestScriptMatchWithNotGroup(t *testing.T) {
data := map[string]any{
"text": "my Expected text NotExpected",
}
expr := jp.MustNewScript("!(@.text ~= /(?i)notexpected/)")
tt.Equal(t, false, expr.Match(data))
}

0 comments on commit a175459

Please sign in to comment.