What is the exact syntactic ambiguity that requires parentheses around the yield expression in an OR assignment?

The following code ...
Suppose the output is inside a generator function and that something and else defined, etc.

 const value = something || yield else(); 

... produces the following in V8 (Chrome or Nodejs):

  const start = initial || yield wait(); ^^^^^ SyntaxError: Unexpected strict mode reserved word 

... and this is in Firefox:

 SyntaxError: yield is a reserved identifier 

I first noticed this in the bluebird coroutine I wrote. The fix is ​​to wrap yield wait() in parentheses.

This error occurs during parsing, not at runtime; therefore, my first assumption is that this is because there is syntactic ambiguity. I looked at the yield keyword , which defines it as:

 [rv] = yield [expression]; 

yield takes an expression and returns a value. This does not happen without the || (OR) as const value = yield else(); ; so I looked at the priority of the operator . Operator || (OR) evaluates to 5 before the yield of 2 . Priority looks fine.

It appears that the operator || (OR) requires an expression on both sides, and although I assume yield [expression] is an expression, maybe it is not? Why do I need to wrap this part in parentheses to make it an explicit expression? What could be || yield || yield ambiguous? I'm sure I just missed it; or is there any hidden / deeper reason for this?

This question was also hard to find, so I hope I will not cheat here.

(You can use this Plunker https://plnkr.co/edit/rNidnFuyIOFkRkkcyWRV to make a mistake if you want to see it.)

Thanks!

+5
source share
1 answer

If you want to find out the syntax rules, you should look at the specification .

A LogicalOrExpression is defined as

 LogicalANDExpression: BitwiseORExpression LogicalANDExpression && BitwiseORExpression LogicalORExpression: LogicalANDExpression LogicalORExpression || LogicalANDExpression 

Boolean expression An expression can only contain BitwiseORExpressions. But YieldExpression is not BitwiseORExpression, it is AssignmentExpression :

 AssignmentExpression: ConditionalExpression [+Yield] YieldExpression ArrowFunction LeftHandSideExpression = AssignmentExpression LeftHandSideExpression AssignmentOperatorAssignmentExpression 

This is basically higher in the expression hierarchy:

  +--------------------------+ | AssignmentExpression | +--------------------------+ ^ | +----------------+---------------+ | | | | +--------------------------+ +--------------------------+ | ConditionalExpression | | YieldExpression | +--------------------------+ +--------------------------+ ^ | | +--------------------------+ | LogicalORExpression | +--------------------------+ ^ | | +--------------------------+ | LogicalANDExpression | +--------------------------+ ^ | | +--------------------------+ | BitwiseORExpression | +--------------------------+ 

Perhaps the explanation of why the syntax is structured this way is that yield also a valid identifier name (outside of generators or yield expressions). But for more details, you probably want to ask https://esdiscuss.org/ .

+7
source

Source: https://habr.com/ru/post/1266832/


All Articles