Implicitly escaped $ (or not) in quasis?

Allen Wirfs-Brock allen at wirfs-brock.com
Tue Jun 26 09:19:52 PDT 2012


I'm working on incorporating quasis into the ES6 draft and there is an issue I want to discuss:

In the wiki proposal[1]  $  is used as the prefix for substitutions that may be of two forms:
   `xyz$foo 1234`      //$foo substitues the value of the variable foo
   `xyz${foo} 1234`    ${expr} generally substitues the result of evaluating expr, so ${foo} substitutes the value of foo

Both of the above examples will produce the same result.

The wiki grammar has this production for dealing with $:

   LiteralCharacter :: $ [lookahead ∉ {, IdentifierStart ]

In other words, a $ is a literal part of the quasi text if it that is not immediately followed by a { or an IdentifierStart character. 

IdentifierStart includes lots of different things including $  itself and many non-roman characters.

For example, under these rules can you (our human readers) identify which of the following is intended to be literal text and which contains a variable substitution:
   `I say: $ᐭ`     // ᐭ is U+142B
   `I say: $⏅`     // ⏅ is U+23C5

Or, perhaps more routinely:

   `$1234`   // this is all literal text
   `$$1234`  //this is a substitution for the variable $1234

I can think of two alternative way to eliminate these potentially confusing situations:

1)  Eliminate the literal use of $ entirely.  The valid uses then are either ${expr} or $IndentifierName.  Any other occurrence of $ within the literal part of a Quasi would have to be escaped.  eg:
     `$99.95`   //syntax error
     `\$99.95`   // same as: "$99.95"

2) Eliminate the $IdentifierName form entirely.  Use ${IdentifierName} instead.  Any occurrence of $ not followed by { is a literal $.   eg:
    `$99.95`   // same as: "$99.95"
    `$$1234`  //same as "$$1234"
    `$foo`        //same as "$foo"
    `${foo}`     //substitutes the value of the variable foo
    `${`            //syntax error

Of these two alternatives,  I favor the second over the first and over the current wiki specification.  I think alternative 2 makes the overall  language simpler for users to learn and to read as it has only one clearly delimitated form of substitution.  It eliminates the need to learn and recognize two different forms and the possibility of confusion when non ascii characters are being used.  It also allows $ to be used literally, which I suspect is quite common in some locales. This alternative also is simpler to specify and requires fewer parsing irregularities.

There are a couple down sides I see for alternative 2, relative to alternative one.  It means that the most common form for substitution expression ( a single identifier) requires two more characters.  It also may cause some confusion for people who are used to languages that support $identifier substitution syntax in strings.   Personally, I think the advantages of this approach out weight these disadvantages. Other my disagree.

So, I propose that we go with alternative 2.  Thoughts?

Allen

[1] http://wiki.ecmascript.org/doku.php?id=harmony:quasis#literalportion 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120626/bd4b0b61/attachment.html>


More information about the es-discuss mailing list