Skip to content

Commit

Permalink
improve parser to allow binary operators as object values
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny committed May 21, 2024
1 parent a41a5f8 commit 01355e9
Show file tree
Hide file tree
Showing 7 changed files with 507 additions and 527 deletions.
16 changes: 8 additions & 8 deletions builtin.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion builtin.jq
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def sub($re; str; $flags):
else
.matches[-1] as $r |
{
string: (($r | _capture | str) + $str[$r.offset+$r.length:.offset] + .string),
string: ($r | _capture | str) + $str[$r.offset+$r.length:.offset] + .string,
offset: $r.offset,
matches: .matches[:-1],
} |
Expand Down
57 changes: 48 additions & 9 deletions cli/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,17 @@
"x": 1
}
- name: object construction with binary operators
args:
- '{x: 1 + 2, y: false or true, z: null // 3}'
input: 'null'
expected: |
{
"x": 3,
"y": true,
"z": 3
}
- name: object construction with condition
args:
- '{x: if true then -. end}'
Expand Down Expand Up @@ -3330,6 +3341,20 @@
6
]
- name: reduce with binary operator
args:
- 'reduce .[] / .[] as $i (0; . + $i)'
input: '[1,2]'
expected: |
4.5
- name: reduce with variable binding
args:
- 'reduce .[] as $x (0; . + $x) as $x | $x'
input: '[1,2,3]'
expected: |
6
- name: reduce variable scope
args:
- -c
Expand Down Expand Up @@ -3393,6 +3418,29 @@
6
]
- name: foreach with binary operator
args:
- '[foreach .[] / .[] as $i (0; . + $i)]'
input: '[1,2]'
expected: |
[
1,
3,
3.5,
4.5
]
- name: foreach with variable binding
args:
- '[foreach .[] as $x (0; . + $x) as $x | $x]'
input: '[1,2,3]'
expected: |
[
1,
3,
6
]
- name: foreach variable scope
args:
- -c
Expand Down Expand Up @@ -3803,15 +3851,6 @@
[1,{"foo":"bar"}]
[2,{"foo":"bar"}]
- name: binding variable scope in try catch
args:
- -c
- '. as $x | try .foo as $x | $x | error catch $x'
input: |
{"foo":42}
expected: |
{"foo":42}
- name: binding variable scope in reduce
args:
- '[range(.)] | 3 as $x | reduce .[] as $i (.[5] as $x | $x; . + $x + $i)'
Expand Down
10 changes: 4 additions & 6 deletions compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,7 @@ func (c *compiler) compileReduce(e *Reduce) error {
}
f()
c.append(&code{op: opstore, v: v})
if err := c.compileTerm(e.Term); err != nil {
if err := c.compileQuery(e.Query); err != nil {
return err
}
if _, err := c.compilePattern(nil, e.Pattern); err != nil {
Expand Down Expand Up @@ -768,7 +768,7 @@ func (c *compiler) compileForeach(e *Foreach) error {
}
f()
c.append(&code{op: opstore, v: v})
if err := c.compileTerm(e.Term); err != nil {
if err := c.compileQuery(e.Query); err != nil {
return err
}
if _, err := c.compilePattern(nil, e.Pattern); err != nil {
Expand Down Expand Up @@ -1347,10 +1347,8 @@ func (c *compiler) compileObjectKeyVal(v [2]int, kv *ObjectKeyVal) error {
}
if kv.Val != nil {
c.append(&code{op: opload, v: v})
for _, e := range kv.Val.Queries {
if err := c.compileQuery(e); err != nil {
return err
}
if err := c.compileQuery(kv.Val); err != nil {
return err
}
}
return nil
Expand Down
Loading

0 comments on commit 01355e9

Please sign in to comment.