ECMAScript 2017: Why does EscapeSequence include NonEscapeCharacter?

旧巷老猫 提交于 2019-12-12 18:20:37

问题


The below excerpts refer to ECMAScript 2017.

11.8.4 String Literals, Note 1

A string literal is zero or more Unicode code points enclosed in single or double quotes. Unicode code points may also be represented by an escape sequence. .... Any code points may appear in the form of an escape sequence.

11.8.4 String Literals, Syntax

Nonterminal symbol EscapeSequence has the following lexical grammar production:

EscapeSequence ::
    CharacterEscapeSequence
    0 [lookahead ∉ DecimalDigit]
    HexEscapeSequence
    UnicodeEscapeSequence

Nonterminal symbol CharacterEscapeSequence has the following lexical grammar production:

CharacterEscapeSequence ::
    SingleEscapeCharacter
    NonEscapeCharacter

11.8.4.3 Static Semantics: SV

Contains descriptions such as:

The SV of DoubleStringCharacter :: \ EscapeSequence is the SV of the EscapeSequence

Questions

  1. What is meant by escape sequence in Note 1? Trying to understand what an escape sequence actually does, rather than just the lexical grammar for it
  2. Why does CharacterEscapeSequence include NonEscapeCharacter?
  3. The descriptions in 11.8.4.3 Static Semantics: SV do not seem to follow the normal ECMAScript convention for lexical grammar productions. What is meant by those descriptions?
  4. Added question: Does Note 1 state that code points can be within quotes or alternatively after an escape sequence (such as backslash)? Is that what is meant by Any code points may appear in the form of an escape sequence?

回答1:


  1. What is meant by escape sequence in Note 1?

    The EscapeSequence from your next question.

  2. Why does CharacterEscapeSequence include NonEscapeCharacter?

    Because invalid escapes just have their backslash ignored – for example, '\c' === 'c'. Backwards compatibility can't be broken.

  3. 11.8.4.3 Static Semantics: SV contains descriptions such as “The SV of DoubleStringCharacter :: \ EscapeSequence is the SV of the EscapeSequence”. Those lines do not follow the normal ECMAScript convention for lexical grammar productions. What is meant by those descriptions?

    It means that you should refer to the rule in the same section corresponding to the EscapeSequence. For example, if you had "\x20", the \x20 would be a DoubleStringCharacter consisting of \ and the EscapeSequence x20, which in turn is a HexEscapeSequence x HexDigit HexDigit, whose SV is given by

    The SV of HexEscapeSequence :: x HexDigit HexDigit is the code unit value that is (16 times the MV of the first HexDigit) plus the MV of the second HexDigit.



来源:https://stackoverflow.com/questions/49604170/ecmascript-2017-why-does-escapesequence-include-nonescapecharacter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!