问题
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
- 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 - Why does
CharacterEscapeSequence
includeNonEscapeCharacter
? - 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?
- 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:
What is meant by escape sequence in Note 1?
The
EscapeSequence
from your next question.Why does CharacterEscapeSequence include NonEscapeCharacter?
Because invalid escapes just have their backslash ignored – for example,
'\c' === 'c'
. Backwards compatibility can't be broken.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 EscapeSequencex20
, which in turn is a HexEscapeSequencex
HexDigit HexDigit, whose SV is given byThe 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