EricSmekens / jsep

JavaScript Expression Parser
http://ericsmekens.github.io/jsep/
MIT License
836 stars 136 forks source link

Discussion: left/right-associativity #195

Closed 6utt3rfly closed 3 years ago

6utt3rfly commented 3 years ago

jsep's gobbleBinaryExpression method will stack all binary expressions using left-associativity (left-to-right) using a recursive decent algorithm. This becomes meaningful only when an expression does not use parentheses, like a ? b : c or a + b * c or a ** b ** c.

However, there are several expressions that operate right-to-left, notably: ternaries, assignment operators, and exponentiation.

The current ternary plugin release does some juggling to flip : binary expressions (from the object plugin) to reverse the left-to-right ordering, but still has some issues, as reported in #189 , when there are operators of lower priority, like assignments.

I started seeing if the ternary plugin could use ? and : binary operators,, but then it has to flip everything after the expression, which "felt" wrong. It's also tricky to properly reassign both left and right branches correctly all the way through.

Since there are other cases that need right-associativity, I'm starting to wonder if jsep should add a left/right-associativity option to binary operators?

zcappp commented 3 years ago

I recalled that this ternary case described in #189 was working fine before plugins system was introduced (with simple jsep.addBinaryOp("=", 0)). see if moving ternary back to jsep core can help.

github-actions[bot] commented 3 years ago

:tada: This issue has been resolved in version 1.2.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket: