Ecma262: What does [Yield, Await] mean in ECMA262 documentation?

Created on 17 Feb 2020  ·  3Comments  ·  Source: tc39/ecma262

The documentation is clear and easy to understand, mostly. However, some sections are quite hard to understand for me because of some signs that I can't guess their meaning.

For example,
image

GeneratorMethod[Yield, Await]:
  * PropertyName[?Yield, ?Await] (UniqueFormalParameters[+Yield, ~Await] ) {
    GeneartorBody
  }

In this example, what does Yield, Await or Default mean and what are those + and ~ signs for?

Most helpful comment

To summarize, these are called parameters (or sometimes, in other documents, flags), and they can appear in three places:

  1. When they appear to the right of the LHS nonterminal of a grammar production, that indicates this production takes those parameters, and they will be unprefixed
  2. When they appear to the right of one of the RHS nonterminals, they will be prefixed with ?, +, or ~:

    1. a ? prefix means that this should be provided the same variant of the parameter as the LHS was (e.g. the first ?Yield in the first production in your screenshot indicates that the PropertyName should be parsed using the same variant of the Yield parameter as the GeneratorMethod is being parsed with)

    2. a + prefix means that this nonterminal should be provided the positive variant of the parameter (e.g. the first +Yield in your screenshot indicates that UniqueFormalParameters should be parsed with the positive variant of that parameter)

    3. a ~ prefix means that this terminal should should be provided the negative variant of the parameter (e.g. the first -Await in your screenshot indicates that UniqueFormalParameters should be parsed with the negative variant of that parameter)

    4. When they appear to the left of one of the RHS productions, they will be prefixed with + or ~:

    5. a + prefix indicates that this production alternative is only available when the positive variant of the parameter was provided to the LHS (e.g. the [+Yield] to the left of YieldExpression in this section indicates that the YieldExpression production is only available when parsing AssignmentExpression using the positive variant of the Yield flag)

    6. a ~ prefix indicates that this production alternative is only available when the negative variant of the parameter was provided to the LHS (e.g. the [-Yield] to the left of the yield in this section indicates that the yield production is only available when parsing IdentifierReference using the negative variant of the Yield flag)

For a concrete example, a regular FunctionExpression has a FunctionBody which is parsed using the negative variant (or absence) of the yield parameter per 2.iii, which is threaded through the parsing of Statement, ExpressionStatement and ultimately AssignmentExpression per 1 and 2.i, which means that AssignmentExpression is parsed using the negative variant of the yield parameter, which means that per 3.ii you cannot have a YieldExpression in a regular FunctionExpression. (But you can have a yield as an IdentifierReference per 3.i.)

By contrast, a regular GeneratorExpression has a FunctionBody (via GeneratorBody) which is parsed using the positive variant (or presence) of the yield parameter per 2.ii, which means that you can have a YieldExpression in a GeneratorExpression, but you cannot have a yield as an IdentifierReference.

All 3 comments

The meaning of these is specified in 5.1.5 Grammar Notation, part way down where it states

A production may be parameterized by a subscripted annotation of the form “[parameters]”

and further down it talks about the +, ~ and ? annotations.

Thank you, I'll read that part and try to grasp what it explains

To summarize, these are called parameters (or sometimes, in other documents, flags), and they can appear in three places:

  1. When they appear to the right of the LHS nonterminal of a grammar production, that indicates this production takes those parameters, and they will be unprefixed
  2. When they appear to the right of one of the RHS nonterminals, they will be prefixed with ?, +, or ~:

    1. a ? prefix means that this should be provided the same variant of the parameter as the LHS was (e.g. the first ?Yield in the first production in your screenshot indicates that the PropertyName should be parsed using the same variant of the Yield parameter as the GeneratorMethod is being parsed with)

    2. a + prefix means that this nonterminal should be provided the positive variant of the parameter (e.g. the first +Yield in your screenshot indicates that UniqueFormalParameters should be parsed with the positive variant of that parameter)

    3. a ~ prefix means that this terminal should should be provided the negative variant of the parameter (e.g. the first -Await in your screenshot indicates that UniqueFormalParameters should be parsed with the negative variant of that parameter)

    4. When they appear to the left of one of the RHS productions, they will be prefixed with + or ~:

    5. a + prefix indicates that this production alternative is only available when the positive variant of the parameter was provided to the LHS (e.g. the [+Yield] to the left of YieldExpression in this section indicates that the YieldExpression production is only available when parsing AssignmentExpression using the positive variant of the Yield flag)

    6. a ~ prefix indicates that this production alternative is only available when the negative variant of the parameter was provided to the LHS (e.g. the [-Yield] to the left of the yield in this section indicates that the yield production is only available when parsing IdentifierReference using the negative variant of the Yield flag)

For a concrete example, a regular FunctionExpression has a FunctionBody which is parsed using the negative variant (or absence) of the yield parameter per 2.iii, which is threaded through the parsing of Statement, ExpressionStatement and ultimately AssignmentExpression per 1 and 2.i, which means that AssignmentExpression is parsed using the negative variant of the yield parameter, which means that per 3.ii you cannot have a YieldExpression in a regular FunctionExpression. (But you can have a yield as an IdentifierReference per 3.i.)

By contrast, a regular GeneratorExpression has a FunctionBody (via GeneratorBody) which is parsed using the positive variant (or presence) of the yield parameter per 2.ii, which means that you can have a YieldExpression in a GeneratorExpression, but you cannot have a yield as an IdentifierReference.

Was this page helpful?
0 / 5 - 0 ratings