Stefan Ram wrote:
> In a recent Firefox:
>
> -2 ** 2
>
> SyntaxError: unparenthesized unary expression can't appear on
> the left-hand side of '**'
>
> . Can't seem to find the corresponding rule in a recent copy
> of ECMAScript (12.6), though.
Next time, *cite* your sources, OK?
<
http://www.ecma-international.org/ecma-262/7.0/#sec-exp-operator>
| 12.6 Exponentiation Operator
|
| Syntax
|
| ExponentiationExpression[Yield] :
| UnaryExpression[?Yield]
| UpdateExpression[?Yield] ** ExponentiationExpression[?Yield]
(via /Statement/ → /ExpressionStatement/ → /Expression/ →
/AssignmentExpression/ → … → /MultiplicativeExpression/)
*The longest match wins*, so check the second (longer) alternative first:
“-2” must be producible by UpdateExpression. Check by looking at the
possible productions for that:
| 12.4 Update Expressions
|
| Syntax
|
| UpdateExpression :
| LeftHandSideExpression[?Yield]
| LeftHandSideExpression[?Yield][no LineTerminator here]++
| LeftHandSideExpression[?Yield][no LineTerminator here]--
| ++ UnaryExpression[?Yield]
| -- UnaryExpression[?Yield]
Conclusion: “-2” cannot be produced by the /UpdateExpression/ production’s
results.
The only remaining alternative is /UnaryExpression/:
| 12.5 Unary Operators
|
| Syntax
|
| UnaryExpression :
| UpdateExpression[?Yield]
| delete UnaryExpression
| void UnaryExpression
| typeof UnaryExpression
| + UnaryExpression
| - UnaryExpression
| ~ UnaryExpression
| ! UnaryExpression
And therein lies the problem.
“-2” cannot be produced by /UpdateExpression/, so it must be producible by
“- UnaryExpression” for “-2 ** 2” to be syntactically valid.
But “2” cannot be produced by /UnaryExpression/, because it can neither be
produced by /UpdateExpression/ nor any other /UnaryExpression/ production
result.
Conclusion: “-2 ** 2” is *syntactically invalid*.
[This has nothing to do with operator precedence.]
Finding out why “(-2) ** 2” is valid syntax, and works as intended, is left
as an exercise to the reader.
--
PointedEars
FAQ: <
http://PointedEars.de/faq> | <
http://PointedEars.de/es-matrix>
<
https://github.com/PointedEars> | <
http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.