-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix unary operator precedence, allow some expressions as object values #3053
base: master
Are you sure you want to change the base?
Conversation
Seems like an improvement and fair compromise. Also nice bonus to allow more things as object literal values, have annoyed me a few times. The parser change looks fine to me but i'm that experience with yacc. |
@itchyny would it be possible to allow most binops also (except |
@wader We'll need to duplicate yacc rules for |
We could also add a special rule to allow |
If not too messy to implement i think it would be nice "quality of life" and consistency improvement. I guessing new users find it confusing that |
Now I improved the parser rules to allow binary operator expressions as object values. It wasn't messy than I thought. $ ./jq -n '{x: 1 + 2, y: false or true, z: null // 3}'
{
"x": 3,
"y": true,
"z": 3
} |
I have just noticed that this patch also makes |
Also the parens previously required for For example:
|
@itchyny nice work! really like this. coming to gojq soon? :) |
dc61166
to
6ce98d6
Compare
Also fixes:
Add regression test for it? |
@wader, just for reference, in jaq, the predence of negation (
On the other hand, jaq currently cannot handle |
Currently, the precedence of the unary operator
-
is too low so it causes some unintuitive results. For example, as I pointed out in #2704 (comment),[-1 as $x | 1,$x]
results in[-1,-1]
in jq 1.7 (and all previous versions), while most of us will expect to see[1,-1]
. When I noticed the behavior a few years ago, I was thinking we should keep the behavior for compatibility, but the issue #2704 was created and convinced me that we should fix the behavior. Actually, We can fix by simply changing the unary operator to be parsed as a part ofTerm
. However, this fix disallows to use the unary operator beforereduce
,foreach
,if
, andtry
expressions. So I would like to make these expression toTerm
as well, to keep supporting unary operator before them. This change allows these expression as object values; something like{ x: if . then 1 else 2 end, y: try error catch ., z: reduce range(3) as $x (0; . + $x) }
. Also allows us to use the unary operator inreduce
expressions;reduce -.[] as $x (...)
. I think the changes are unlikely to break existing scripts, but let me clarify what I intentionally dropped supporting; unary operator before function declaration, and labels;[-def f: 1; f]
,[-label $x | 1]
.