11.8 字面量

11.8.1 Null字面量

语法
NullLiteral::
    null

11.8.2 Bollean 字面量

语法
BooleanLiteral::
    true
    false

11.8.3 数字字面量

语法
NumericLiteral::
    DecimalLiteral
    BinaryIntegerLiteral
    OctalIntegerLiteral
    HexIntegerLiteral
DecimalLiteral::
    DecimalIntegerLiteral.DecimalDigits(opt)ExponentPart(opt)
    .DecimalDigitsExponentPart(opt)
    DecimalIntegerLiteralExponentPart(opt)
DecimalIntegerLiteral::
    0
    NonZeroDigitDecimalDigits(opt)
DecimalDigits::
    DecimalDigit
    DecimalDigitsDecimalDigit
DecimalDigit::one of
    0123456789
NonZeroDigit::one of
    123456789
ExponentPart::
    ExponentIndicatorSignedInteger
ExponentIndicator::one of
    eE
SignedInteger::
    DecimalDigits
    +DecimalDigits
    -DecimalDigits
BinaryIntegerLiteral::
    0bBinaryDigits
    0BBinaryDigits
BinaryDigits::
    BinaryDigit
    BinaryDigitsBinaryDigit
BinaryDigit::one of
    01
OctalIntegerLiteral::
    0oOctalDigits
    0OOctalDigits
OctalDigits::
    OctalDigit
    OctalDigitsOctalDigit
OctalDigit::one of
    01234567
HexIntegerLiteral::
    0xHexDigits
    0XHexDigits
HexDigits::
    HexDigit
    HexDigitsHexDigit
HexDigit::one of
    0123456789abcdefABCDEF

源代码中的 NumericLiteral 后面不允许紧跟着 IdentifierStart 或 DecimalDigit。

注意:比如 3in 就会报错,并且不可能会被识别为两个输入元素3和in

一致的实现中,当处理严格模式代码时,如B.1.1中所描述, NumericLiteral 的语法包括 LegacyOctalIntegerLiteral, 不是扩展DecimalIntegerLiteral 的语法去包含 NonOctalDecimalIntegerLiteral.

11.8.3.1 静态语法:MV

一个数字字面量代表着一个Number类型的值。其值取决于两个步骤:第一,直接由字面量得到其MV(mathematical value;第二,按照下面的步骤舍入

The MV of NumericLiteral::DecimalLiteral is the MV of DecimalLiteral.
The MV of NumericLiteral::BinaryIntegerLiteral is the MV of BinaryIntegerLiteral.
The MV of NumericLiteral::OctalIntegerLiteral is the MV of OctalIntegerLiteral.
The MV of NumericLiteral::HexIntegerLiteral is the MV of HexIntegerLiteral.
The MV of DecimalLiteral::DecimalIntegerLiteral. is the MV of DecimalIntegerLiteral.
The MV of DecimalLiteral::DecimalIntegerLiteral.DecimalDigits is the MV of DecimalIntegerLiteral plus (the MV of DecimalDigits × 10-n), where n is the number of code points in DecimalDigits.
The MV of DecimalLiteral::DecimalIntegerLiteral.ExponentPart is the MV of DecimalIntegerLiteral × 10e, where e is the MV of ExponentPart.
The MV of DecimalLiteral::DecimalIntegerLiteral.DecimalDigitsExponentPart is (the MV of DecimalIntegerLiteral plus (the MV of DecimalDigits × 10-n)) × 10e, where n is the number of code points in DecimalDigits and e is the MV of ExponentPart.
The MV of DecimalLiteral::.DecimalDigits is the MV of DecimalDigits × 10-n, where n is the number of code points in DecimalDigits.
The MV of DecimalLiteral::.DecimalDigitsExponentPart is the MV of DecimalDigits × 10e-n, where n is the number of code points in DecimalDigits and e is the MV of ExponentPart.
The MV of DecimalLiteral::DecimalIntegerLiteral is the MV of DecimalIntegerLiteral.
The MV of DecimalLiteral::DecimalIntegerLiteralExponentPart is the MV of DecimalIntegerLiteral × 10e, where e is the MV of ExponentPart.
The MV of DecimalIntegerLiteral::0 is 0.
The MV of DecimalIntegerLiteral::NonZeroDigit is the MV of NonZeroDigit.
The MV of DecimalIntegerLiteral::NonZeroDigitDecimalDigits is (the MV of NonZeroDigit × 10n) plus the MV of DecimalDigits, where n is the number of code points in DecimalDigits.
The MV of DecimalDigits::DecimalDigit is the MV of DecimalDigit.
The MV of DecimalDigits::DecimalDigitsDecimalDigit is (the MV of DecimalDigits × 10) plus the MV of DecimalDigit.
The MV of ExponentPart::ExponentIndicatorSignedInteger is the MV of SignedInteger.
The MV of SignedInteger::DecimalDigits is the MV of DecimalDigits.
The MV of SignedInteger::+DecimalDigits is the MV of DecimalDigits.
The MV of SignedInteger::-DecimalDigits is the negative of the MV of DecimalDigits.
The MV of DecimalDigit::0 or of HexDigit::0 or of OctalDigit::0 or of BinaryDigit::0 is 0.
The MV of DecimalDigit::1 or of NonZeroDigit::1 or of HexDigit::1 or of OctalDigit::1 or of BinaryDigit::1 is 1.
The MV of DecimalDigit::2 or of NonZeroDigit::2 or of HexDigit::2 or of OctalDigit::2 is 2.
The MV of DecimalDigit::3 or of NonZeroDigit::3 or of HexDigit::3 or of OctalDigit::3 is 3.
The MV of DecimalDigit::4 or of NonZeroDigit::4 or of HexDigit::4 or of OctalDigit::4 is 4.
The MV of DecimalDigit::5 or of NonZeroDigit::5 or of HexDigit::5 or of OctalDigit::5 is 5.
The MV of DecimalDigit::6 or of NonZeroDigit::6 or of HexDigit::6 or of OctalDigit::6 is 6.
The MV of DecimalDigit::7 or of NonZeroDigit::7 or of HexDigit::7 or of OctalDigit::7 is 7.
The MV of DecimalDigit::8 or of NonZeroDigit::8 or of HexDigit::8 is 8.
The MV of DecimalDigit::9 or of NonZeroDigit::9 or of HexDigit::9 is 9.
The MV of HexDigit::a or of HexDigit::A is 10.
The MV of HexDigit::b or of HexDigit::B is 11.
The MV of HexDigit::c or of HexDigit::C is 12.
The MV of HexDigit::d or of HexDigit::D is 13.
The MV of HexDigit::e or of HexDigit::E is 14.
The MV of HexDigit::f or of HexDigit::F is 15.
The MV of BinaryIntegerLiteral::0bBinaryDigits is the MV of BinaryDigits.
The MV of BinaryIntegerLiteral::0BBinaryDigits is the MV of BinaryDigits.
The MV of BinaryDigits::BinaryDigit is the MV of BinaryDigit.
The MV of BinaryDigits::BinaryDigitsBinaryDigit is (the MV of BinaryDigits × 2) plus the MV of BinaryDigit.
The MV of OctalIntegerLiteral::0oOctalDigits is the MV of OctalDigits.
The MV of OctalIntegerLiteral::0OOctalDigits is the MV of OctalDigits.
The MV of OctalDigits::OctalDigit is the MV of OctalDigit.
The MV of OctalDigits::OctalDigitsOctalDigit is (the MV of OctalDigits × 8) plus the MV of OctalDigit.
The MV of HexIntegerLiteral::0xHexDigits is the MV of HexDigits.
The MV of HexIntegerLiteral::0XHexDigits is the MV of HexDigits.
The MV of HexDigits::HexDigit is the MV of HexDigit.
The MV of HexDigits::HexDigitsHexDigit is (the MV of HexDigits × 16) plus the MV of HexDigit.

字面量的数字值一旦被确定,将会被舍入为Number类型。如果NV是0,其将会被舍入为 +0;否则,其舍入值就是MV值(如6.1.6)。除非此字面量是有效数字超过 20 位的 DecimalLiteral,这种情况下,数字值可以用下面两种方式产生的 MV 值确定:一,将 20 位后的每个有效数字用 0 替换后产生的 MV,二,将 20 位后的每个有效数字用 0 替换,并且递增第 20 位有效数字位置的字面量值,产生的 MV。如果一个数字是有效数字,要满足其不是 ExponentPart 的一部分,并且: 不是0,或者其左边有一个非0数字,右边有一个非0非ExponentPart的数字

11.8.4 字符串字面量

注意:一个字符串字面量是关闭着的单引号或者双引号里的零个或多个字符,当然,其中的字符也可以是由转义字符转义而来。除了闭合的引号、反斜杠、回车、行分隔符、段落分隔符、换行符之外的所有字符都可以直接出现的字符串字面量里。任何字符都可以以转义序列的形式出现在字面量里。字符串字面量会转换为ECMAScript字符串类型。这些字符都会以10.1.1中定义的方式编码为UTF-16编码。字符单元属于Basic Multilingual Plane 的会以单字符的形式编码,其他的都以双字符的形式编码

语法
StringLiteral::
    "DoubleStringCharacters(opt)"
    'SingleStringCharacters(opt)'
DoubleStringCharacters::
    DoubleStringCharacter DoubleStringCharacters(opt)
SingleStringCharacters::
    SingleStringCharacter SingleStringCharacters(opt)
DoubleStringCharacter::
    SourceCharacter but not one of " or \ or LineTerminator
    \EscapeSequence
    LineContinuation
SingleStringCharacter::
    SourceCharacter but not one of ' or \ or LineTerminator
    \EscapeSequence
    LineContinuation
LineContinuation::
    \LineTerminatorSequence
EscapeSequence::
    CharacterEscapeSequence
    0 [lookahead ∉ DecimalDigit]
    HexEscapeSequence
    UnicodeEscapeSequence

一个合格的实现中,当在严格模式下,不能把 EscapeSequence 的语法扩展到包括B.1.2中的 LegacyOctalEscapeSequence 语法

CharacterEscapeSequence::
    SingleEscapeCharacter
    NonEscapeCharacter
SingleEscapeCharacter::one of
    '"\bfnrtv
NonEscapeCharacter::
    SourceCharacter but not one of EscapeCharacter or LineTerminator
EscapeCharacter::
    SingleEscapeCharacter
    DecimalDigit
    x
    u
HexEscapeSequence::
    xHexDigitHexDigit
UnicodeEscapeSequence::
    uHex4Digits
    u{HexDigits}
Hex4Digits::
    HexDigit HexDigit HexDigit HexDigit

非终结符HexDigit的定义在11.8.3中,SourceCharacter的定义在10.1中

注意:行终结符一般不能出现在字符串字面量中,除非行终结符是LineContinuation的一部分,并且其产生一个空的字符序列,把行终结符放在字符字面量中的合适的做法是用转义序列,比如 '\n' 或者 '\u000A'

11.8.4.1 静态语法:早期错误

UnicodeEscapeSequence::u{HexDigits}
  • 如果MV的16进制数大于1114111,会是一个语法错误

11.8.4.2 静态语法:字符串值

StringLiteral::
    "DoubleStringCharacters(opt)"
    'SingleStringCharacters(opt)'
  • 返回的字符串值的元素是字符串字面量的SV

11.8.4.3 静态语法:SV

一个字符串字面量代表一个 String 类型的值。字面量的字符串值 (SV) 由字符串字面量各部分贡献的字符值 (CV) 描述。作为这一过程的一部分,字符字面量里的某些字符字符会被解释成包含数学值 (MV),如 11.8.3 和下面描述的

  • The SV of StringLiteral::"" is the empty code unit sequence.
  • The SV of StringLiteral::'' is the empty code unit sequence.
  • The SV of StringLiteral::"DoubleStringCharacters" is the SV of DoubleStringCharacters.
  • The SV of StringLiteral::'SingleStringCharacters' is the SV of SingleStringCharacters.
  • The SV of DoubleStringCharacters::DoubleStringCharacter is a sequence of one or two code units that is the SV of DoubleStringCharacter.
  • The SV of DoubleStringCharacters::DoubleStringCharacterDoubleStringCharacters is a sequence of one or two code units that is the SV of DoubleStringCharacter followed by all the code units in the SV of DoubleStringCharacters in order.
  • The SV of SingleStringCharacters::SingleStringCharacter is a sequence of one or two code units that is the SV of SingleStringCharacter.
  • The SV of SingleStringCharacters::SingleStringCharacterSingleStringCharacters is a sequence of one or two code units that is the SV of SingleStringCharacter followed by all the code units in the SV of SingleStringCharacters in order.
  • The SV of DoubleStringCharacter::SourceCharacterbut not one of " or \ or LineTerminator is the UTF16Encoding of the code point value of SourceCharacter.
  • The SV of DoubleStringCharacter::\EscapeSequence is the SV of the EscapeSequence.
  • The SV of DoubleStringCharacter::LineContinuation is the empty code unit sequence.
  • The SV of SingleStringCharacter::SourceCharacterbut not one of ' or \ or LineTerminator is the UTF16Encoding of the code point value of SourceCharacter.
  • The SV of SingleStringCharacter::\EscapeSequence is the SV of the EscapeSequence.
  • The SV of SingleStringCharacter::LineContinuation is the empty code unit sequence.
  • The SV of EscapeSequence::CharacterEscapeSequence is the SV of the CharacterEscapeSequence.
  • The SV of EscapeSequence::0 is the code unit value 0.
  • The SV of EscapeSequence::HexEscapeSequence is the SV of the HexEscapeSequence.
  • The SV of EscapeSequence::UnicodeEscapeSequence is the SV of the UnicodeEscapeSequence.
  • The SV of CharacterEscapeSequence::SingleEscapeCharacter is the code unit whose value is determined by the SingleEscapeCharacter according to Table 34.
  • The SV of CharacterEscapeSequence::NonEscapeCharacter is the SV of the NonEscapeCharacter.
  • The SV of NonEscapeCharacter::SourceCharacterbut not one of EscapeCharacter or LineTerminator is the UTF16Encoding of the code point value of SourceCharacter.
  • The SV of HexEscapeSequence::xHexDigitHexDigit is the code unit value that is (16 times the MV of the first HexDigit) plus the MV of the second HexDigit.
  • The SV of UnicodeEscapeSequence::uHex4Digits is the SV of Hex4Digits.
  • The SV of Hex4Digits::HexDigit HexDigit HexDigit HexDigit is the code unit value that is (4096 times the MV of the first HexDigit) plus (256 times the MV of the second HexDigit) plus (16 times the MV of the third HexDigit) plus the MV of the fourth HexDigit.
  • The SV of UnicodeEscapeSequence::u{HexDigits} is the UTF16Encoding of the MV of HexDigits.
表34 单字符转义序列
转义序列 字符编码值 名称 符号
\b \u0008 回格 < BS >
\t \u0009 水平制表符 < HT >
\n \u000A 进行(新行) < LF >
\v \u000B 竖直制表符 < VT >
\f \u000C 进纸 < FF >
\r \u000D 回车 < CR >
\" \u0022 双引号 "
\' \u0027 单引号 '
\\ \u005C 反斜杠 \

11.8.5 正则表达式字面量

注意1:正则表达式是一个输入元素,每当字面量被解析时,都会被转换为 RegExp 对象。两个正则表达式字面量,即使字符全部相等,也不能用 === 来比较他们是否相等。RegExp对象也可以在运行时用new RegExp 创建或者像调用函数一样,调用 RegExp 构造函数。

下面的产生式描述了正则表达式字面量的语法,输入元素扫描器还用它搜索正则表达式字面量的结束位置。RegularExpressionBody 和 RegularExpressionFlags 包含的字符组成的字符串后续会传递给正则表达式构造器,在那里用更严格文法进行解析。
一个实现可以扩展正则表达式构造器的文法。但它不能扩展 RegularExpressionBody 和 RegularExpressionFlags 产生式或使用这些产生式的产生式

语法
RegularExpressionLiteral::
    /RegularExpressionBody/RegularExpressionFlags
RegularExpressionBody::
    RegularExpressionFirstCharRegularExpressionChars
RegularExpressionChars::
    [empty]
    RegularExpressionCharsRegularExpressionChar
RegularExpressionFirstChar::
    RegularExpressionNonTerminatorbut not one of * or \ or / or [
    RegularExpressionBackslashSequence
    RegularExpressionClass
RegularExpressionChar::
    RegularExpressionNonTerminatorbut not one of \ or / or [
    RegularExpressionBackslashSequence
    RegularExpressionClass
RegularExpressionBackslashSequence::
    \ RegularExpressionNonTerminator
RegularExpressionNonTerminator::
    SourceCharacterbut not LineTerminator
RegularExpressionClass::
    [RegularExpressionClassChars]
RegularExpressionClassChars::
    [empty]
    RegularExpressionClassCharsRegularExpressionClassChar
RegularExpressionClassChar::
    RegularExpressionNonTerminatorbut not one of ] or \
    RegularExpressionBackslashSequence
RegularExpressionFlags::
    [empty]
    RegularExpressionFlagsIdentifierPart

11.8.5.1 静态语法:早期错误

RegularExpressionFlags::RegularExpressionFlags IdentifierPart
  • 如果包含Unicode转义字符会报错.

11.8.5.2 静态语法: 正文

RegularExpressionLiteral::/ RegularExpressionBody / RegularExpressionFlags
  • 返回被识别为正则表达式正文的源代码

11.8.5.3 静态语法: 标记

RegularExpressionLiteral::/ RegularExpressionBody / RegularExpressionFlags
  • 返回被识别为正则表达式标记的源代码.

11.8.6 模板字面量和词法组件

语法
Template::
    NoSubstitutionTemplate
    TemplateHead
NoSubstitutionTemplate::
    `TemplateCharacters(opt)`
TemplateHead::
    `TemplateCharacters(opt)${
TemplateSubstitutionTail::
    TemplateMiddle
    TemplateTail
TemplateMiddle::
    }TemplateCharacters(opt)${
TemplateTail::
    }TemplateCharacters(opt)`
TemplateCharacters::
    TemplateCharacterTemplateCharacters(opt)
TemplateCharacter::
    $[lookahead ≠ {]
    \EscapeSequence
    LineContinuation
    LineTerminatorSequence
    SourceCharacterbut not one of ` or \ or $ or LineTerminator

一个合格的实现在解析 TemplateCharacter 时不会扩展如B.1.2中定义的转义序列语法

注意:TemplateSubstitutionTail 用来作为InputElementTemplateTail 另一个词法目标.

11.8.6.1 静态语法:TV和TRV

一个模板字面量组件是被解释为一个Unicode字符单元序列。一个字面量组件的模板值(TV)是由其多部分的模板字面量的值组成的。作为这个处理过程的一部分,一些模板中的Unicode字符会被解释为MV,在得到TV时,转义序列会被UTF-16字符单元替换。TRV和TV很像,区别是TRV是逐个逐个解释的。

  • The TV and TRV of NoSubstitutionTemplate::`` is the empty code unit sequence.
  • The TV and TRV of TemplateHead::`${ is the empty code unit sequence.
  • The TV and TRV of TemplateMiddle::}${ is the empty code unit sequence.
  • The TV and TRV of TemplateTail::}` is the empty code unit sequence.
  • The TV of NoSubstitutionTemplate::TemplateCharacters is the TV of TemplateCharacters.
  • The TV of TemplateHead::`TemplateCharacters${ is the TV of TemplateCharacters.
  • The TV of TemplateMiddle::}TemplateCharacters${ is the TV of TemplateCharacters.
  • The TV of TemplateTail::}TemplateCharacters` is the TV of TemplateCharacters.
  • The TV of TemplateCharacters::TemplateCharacter is the TV of TemplateCharacter.
  • The TV of TemplateCharacters::TemplateCharacterTemplateCharacters is a sequence consisting of the code units in the TV of TemplateCharacter followed by all the code units in the TV of TemplateCharacters in order.
  • The TV of TemplateCharacter::SourceCharacterbut not one of ` or \ or $ or LineTerminator is the UTF16Encoding of the code point value of SourceCharacter.
  • The TV of TemplateCharacter::$ is the code unit value 0x0024.
  • The TV of TemplateCharacter::\EscapeSequence is the SV of EscapeSequence. The TV of TemplateCharacter::LineContinuation is the TV of LineContinuation.
  • The TV of TemplateCharacter::LineTerminatorSequence is the TRV of LineTerminatorSequence.
  • The TV of LineContinuation::\LineTerminatorSequence is the empty code unit sequence.
  • The TRV of NoSubstitutionTemplate::TemplateCharacters is the TRV of TemplateCharacters.
  • The TRV of TemplateHead::`TemplateCharacters${ is the TRV of TemplateCharacters.
  • The TRV of TemplateMiddle::}TemplateCharacters${ is the TRV of TemplateCharacters.
  • The TRV of TemplateTail::}TemplateCharacters` is the TRV of TemplateCharacters.
  • The TRV of TemplateCharacters::TemplateCharacter is the TRV of TemplateCharacter.
  • The TRV of TemplateCharacters::TemplateCharacterTemplateCharacters is a sequence consisting of the code units in the TRV of TemplateCharacter followed by all the code units in the TRV of TemplateCharacters, in order.
  • The TRV of TemplateCharacter::SourceCharacterbut not one of ` or \ or $ or LineTerminator is the UTF16Encoding of the code point value of SourceCharacter.
  • The TRV of TemplateCharacter::$ is the code unit value 0x0024.
  • The TRV of TemplateCharacter::\EscapeSequence is the sequence consisting of the code unit value 0x005C followed by the code units of TRV of EscapeSequence.
  • The TRV of TemplateCharacter::LineContinuation is the TRV of LineContinuation.
  • The TRV of TemplateCharacter::LineTerminatorSequence is the TRV of LineTerminatorSequence.
  • The TRV of EscapeSequence::CharacterEscapeSequence is the TRV of the CharacterEscapeSequence.
  • The TRV of EscapeSequence::0 is the code unit value 0x0030 (DIGIT ZERO).
  • The TRV of EscapeSequence::HexEscapeSequence is the TRV of the HexEscapeSequence.
  • The TRV of EscapeSequence::UnicodeEscapeSequence is the TRV of the UnicodeEscapeSequence.
  • The TRV of CharacterEscapeSequence::SingleEscapeCharacter is the TRV of the SingleEscapeCharacter.
  • The TRV of CharacterEscapeSequence::NonEscapeCharacter is the SV of the NonEscapeCharacter.
  • The TRV of SingleEscapeCharacter::one of'"\bfnrtv is the SV of the SourceCharacter that is that single code point.
  • The TRV of HexEscapeSequence::xHexDigitHexDigit is the sequence consisting of code unit value 0x0078 followed by TRV of the first HexDigit followed by the TRV of the second HexDigit.
  • The TRV of UnicodeEscapeSequence::uHex4Digits is the sequence consisting of code unit value 0x0075 followed by TRV of Hex4Digits.
  • The TRV of UnicodeEscapeSequence::u{HexDigits} is the sequence consisting of code unit value 0x0075 followed by code unit value 0x007B followed by TRV of HexDigits followed by code unit value 0x007D.
  • The TRV of Hex4Digits::HexDigitHexDigitHexDigitHexDigit is the sequence consisting of the TRV of the first HexDigit followed by the TRV of the second HexDigit followed by the TRV of the third HexDigit followed by the TRV of the fourth HexDigit.
  • The TRV of HexDigits::HexDigit is the TRV of HexDigit.
  • The TRV of HexDigits::HexDigitsHexDigit is the sequence consisting of TRV of HexDigits followed by TRV of HexDigit.
  • The TRV of a HexDigit is the SV of the SourceCharacter that is that HexDigit.
  • The TRV of LineContinuation::\LineTerminatorSequence is the sequence consisting of the code unit value 0x005C followed by the code units of TRV of LineTerminatorSequence.
  • The TRV of LineTerminatorSequence::< LF > is the code unit value 0x000A.
  • The TRV of LineTerminatorSequence::< CR > is the code unit value 0x000A.
  • The TRV of LineTerminatorSequence::< LS > is the code unit value 0x2028.
  • The TRV of LineTerminatorSequence::< PS > is the code unit value 0x2029.
  • The TRV of LineTerminatorSequence::< CR >< LF > is the sequence consisting of the code unit value 0x000A.

注意:TV不包含 LineContinuation ,TRV包含。TV和TRV中 < CR >< LF > 和 LineTerminatorSequences 会被归一化为 < LF >,包含< CR > or < CR >< LF > 序列需要明确的 EscapeSequence

results matching ""

    No results matching ""