ion-docs icon indicating copy to clipboard operation
ion-docs copied to clipboard

Clarify the associativity of `+` and `-` in s-expressions

Open popematt opened this issue 2 years ago • 2 comments

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'?

popematt avatar Sep 09 '22 00:09 popematt

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)

rmarrowstone avatar Sep 13 '22 00:09 rmarrowstone

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.

popematt avatar Sep 16 '22 20:09 popematt