Ecma262: What String does it says in Notational Convention (section 5.1.4)?

Created on 17 Mar 2019  Â·  18Comments  Â·  Source: tc39/ecma262

The specification says the following:

NOTE 1
Parsing the same String multiple times will lead to different Parse Nodes, e.g., as occurs in:

eval(str); eval(str);

It is not entirely clear what about String it says.

  1. The String is eval(str);
  2. The String is an argument str to the eval function.

If this is the first option, then instead of this function there can be anything even a variable declaration (for example: let a;), right?

question

Most helpful comment

"strings" aren't parsed, rather "source texts" are parsed and str is simply a variable that has some value so it is not exactly clear how this example is illustrating the the point that two parses of the same _source text_ produces two distinct parse trees that share no parse nodes.

Perhaps it would be clearer this way:

let str =  "something;"; //a string representation of ECMAScript source text
eval(str); eval(str);

Each eval converts the value of str into an ECMAScript source test and performs an independent parse that creates its own separate tree of Parse Nodes.
The trees are distinct even though each parse operates upon a source text that was derived from the same String value.

All 18 comments

I believe it’s the second option.

@ljharb

I believe it’s the second option.

I think we need to clarify this issue.


the only example i can think of is:

eval('tag``'); eval('tag``');

edit: this is wrong, the parse nodes for those templates would be different even if the strings were the same parse nodes

In the example referenced in the OP, the only thing happening multiple times is a call to eval, with the same string argument. I’m not clear on how it could be read any differently.

How would you suggest it be clarified?

@ljharb

It is necessary to clarify what is a String. Since I do not know what alternative examples can be given except eval(str); eval(str);. Regarding this example, I would write this:

NOTE 1
Parsing the same String multiple times will lead to different Parse Nodes, e.g., as occurs in (String is the function argument):

eval(str); eval(str);

As for the other interpretation: This can be understood as var a; var a; or ("foo" + "bar"); ("foo" + "bar");. In this case, a string is a line of code separated by a ";"

By using the capital S, i believe it’s unambiguosly referring to an ECMAScript String value, ie, the contents of str. The sentence is indicating that the same string contents won’t produce the same parse node - that it’s the call to eval, not the string contents, that determines the creation of new parse nodes.

(linking to https://tc39.github.io/ecma262/#sec-syntactic-grammar )

@ljharb I understand you, could you give a couple of examples besides eval(str);?

We could maybe change the wording to something like:

Parsing the same String multiple times will lead to different Parse Nodes.
For example, consider:
    eval(str); eval(str);
Each call to `eval` will create its own separate set of Parse Nodes,
even though the same String value is passed to both calls.

@jmdyck Good change. But I will ask you too what I ljharb asked. Could you give a couple of examples except eval(str);?

Other than eval and Function, i’m not sure how you’d demonstrate parsing twice with the same String value.

"strings" aren't parsed, rather "source texts" are parsed and str is simply a variable that has some value so it is not exactly clear how this example is illustrating the the point that two parses of the same _source text_ produces two distinct parse trees that share no parse nodes.

Perhaps it would be clearer this way:

let str =  "something;"; //a string representation of ECMAScript source text
eval(str); eval(str);

Each eval converts the value of str into an ECMAScript source test and performs an independent parse that creates its own separate tree of Parse Nodes.
The trees are distinct even though each parse operates upon a source text that was derived from the same String value.

@allenwb Thanks, now it became clear. I asked above about examples other than eval(str); to me, ljharb answered that this could be a function. But I honestly do not understand how the function may be in place of eval(str);. Do not explain it?

I said Function, not “a function” - referring explicitly to the function constructor.

@ljharb Sorry, did not attach any importance to this. If you are talking about the constructor function, then you mean the creation of a function through the constructor: new Function (str)?

@dSalieri yes, although the new there is optional.

@ljharb clear, thank you for your answers.

@dSalieri have your questions all been answered?

@ljharb if you talk about this issue, definitely yes.

Was this page helpful?
0 / 5 - 0 ratings