Open mikez opened 2 years ago
Good idea i think. Had a look at the wiki and found this https://github.com/stedolan/jq/wiki/jq-Language-Description#operators-priority, maybe it can be extended?
@wader Good find!
It seems @fadado added this in 2019 with the comment "Added operators priority table. Please, check the table validity, specially the new added operator ?//
" and seemingly it hasn't been revised since. So maybe there was some uncertainty there too? :) Or maybe the concept of operator precedence is less applicable in the world of jq?
Some proposals:
Is it asc or desc? I'd argue that usually it's desc. Anyway the direction should be specified.
Order: from highest precedence (most binding) to lowest precedence (least binding).
Oh, indeed. I probably had the wiki in mind. Order is not specified there. And it's apparently from lowest to highest. Which I think is rather unusual. At least from my experience...
Thank you for this.
Yes, the wiki is currently the best place for this work, and you can go ahead and edit it. Eventually we'd like to come up with a more formal specification of the language and put it up as a separate repository in the jqlang
org.
Does anyone here think operator precedence tables are helpful? I do. :) (Examples: Python and JavaScript.)
Is there already such a table for jq? If not, would it make sense to include it in the Wiki or elsewhere?
Example table (feel free to copy and change)
Here's a first draft based on Rosetta Code, parser.y, some simplifying assumptions, and guesses. Somebody who knows the codebase better than me, would be a good candidate to correct and extend this. :)
Order: from highest precedence (most binding) to lowest precedence (least binding).
x?
-x
*
,/
,%
+
,-
==
,!=
,<
,>
,<=
,>=
and
or
=
,|=
,+=
,-=
,*=
,/=
,%=
//
,
|
label $variable
try … catch …
if … then … end
foreach … as … (…)
reduce … as … (…)
… as $variable
def … ; …