ion-docs
ion-docs copied to clipboard
Clarify the associativity of `+` and `-` in s-expressions
These behaviors are present in ion-java
, ion-js
, and ion-rust
. (I did not check any other implementations.)
( +inf ) // parses as ( +inf )
( +inf'+' ) // parses as ( +inf '+' )
( +inf+ ) // parses as ( '+' 'inf' '+' ) <= WAT?
This is also holds if you substitute -
for any +
or if you substitute any other operator symbol in place of the +
after the inf
.
Also:
( +inf+inf ) // parses as ( '+' 'inf' +inf ) <= WAT?
So, why does the addition of an unquoted operator symbol or another infinity after +inf
cause +inf
to split into '+' 'inf'
?
Another example:
(2000T -2001T)
Is not valid per the ANTLR grammar (Ion doesn't allow negative timestamps), but it could be parsed as:
(2000T '-' 2001T)
Another potentially confusing case:
(x+1) // ( 'x' '+' 1 )
(x-1) // ( 'x' -1 )
(x*-1) // ( 'x' '*-' 1 )
The -
associates with the number, unless it is preceded by another operator symbol character.