12 ECMAScript 言語: 字句文法

ECMAScript Script または Module のソーステキス トは、まず入力要素の列へ変換される。これらは token、 line terminator、comment、または white space である。ソーステキストは左から右へ走査され、繰り返し、次 の入力要素として可能な限り最長の code point 列を取 る。

字句入力要素の識別が、それらの入力要素を消費する構文文 法コンテキストに依存する状況がいくつか存在する。そのた め、字句文法には複数の目標記号が必要となる。 InputElementHashbangOrRegExp 目標は Script または Module の開始時に用いられる。 InputElementRegExpOrTemplateTail 目標は、 RegularExpressionLiteralTemplateMiddle、 または TemplateTail が許可される構文文法コンテキス トで用いられる。InputElementRegExp 目標記号は、 RegularExpressionLiteral が許可されるが、 TemplateMiddleTemplateTail も許可されない すべての構文文法コンテキストで用いられる。 InputElementTemplateTail 目標は、 TemplateMiddle または TemplateTail が許可さ れるが、RegularExpressionLiteral は許可されない すべての構文文法コンテキストで用いられる。その他すべての コンテキストでは、InputElementDiv が字句目標記号と して用いられる。

Note

複数の字句目標を用いることにより、自動セミコロン挿入 に影響を与える字句上の曖昧性が存在しないことが保証され る。たとえば、先頭の除算または除算代入と、先頭の RegularExpressionLiteral の両方が許可される構文 文法コンテキストは存在しない。これはセミコロン挿入 (12.10 を参照)によって影響されない; 次のような例では:

a = b
/hi/g.exec(c).map(d);

ここで、LineTerminator の後にある最初の non-whitespace かつ non-comment code point が U+002F (SOLIDUS) であり、かつ構文コンテキストが除 算または除算代入を許可している場合、LineTerminator においてセミコロンは挿入されない。すなわち、上の例は次と 同じように解釈される:

a = b / hi / g.exec(c).map(d);

構文

InputElementDiv :: WhiteSpace LineTerminator Comment CommonToken DivPunctuator RightBracePunctuator InputElementRegExp :: WhiteSpace LineTerminator Comment CommonToken RightBracePunctuator RegularExpressionLiteral InputElementRegExpOrTemplateTail :: WhiteSpace LineTerminator Comment CommonToken RegularExpressionLiteral TemplateSubstitutionTail InputElementTemplateTail :: WhiteSpace LineTerminator Comment CommonToken DivPunctuator TemplateSubstitutionTail InputElementHashbangOrRegExp :: WhiteSpace LineTerminator Comment CommonToken HashbangComment RegularExpressionLiteral

12.1 Unicode 書式制御文字

Unicode の書式制御文字(すなわち、Unicode Character Database における “Cf” カテゴリの文 字。たとえば LEFT-TO-RIGHT MARK や RIGHT-TO-LEFT MARK)は、より高水準のプロトコル (markup language など)が存在しない場合に、ある範 囲のテキストの書式を制御するために用いられる制御 code である。

編集や表示を容易にするため、ソーステキストに書式制御文 字を許可することは有用である。すべての書式制御文字は comment の内部、および string literal、template literal、regular expression literal の内部で使 用してよい。

U+FEFF (ZERO WIDTH NO-BREAK SPACE) は、主 にテキストの先頭において、それが Unicode であること を示し、かつテキストのエンコーディングおよび byte 順 序を検出可能にするために用いられる書式制御文字である。こ の目的のための <ZWNBSP> 文字は、たとえば file の連 結の結果として、テキストの先頭以外にも現れることがある。 ECMAScript ソーステキストにおいて、<ZWNBSP> code point は、comment、string literal、 template literal、および regular expression literal の外側では white space 文字として扱われる (12.2 を参照)。

12.2 White Space

White space code point は、ソーステキストの可 読性を向上させ、token(分割不能な字句単位)同士を区切 るために用いられるが、それ以外では意味を持たない。 White space code point は、任意の 2 つの token の間、および入力の先頭または末尾に現れ得る。White space code point は、StringLiteralRegularExpressionLiteralTemplate、または TemplateSubstitutionTail の内部に現れ得る。そ こでは、それらは literal 値の一部を構成する意味のある code point と見なされる。また、Comment の内部に も現れ得るが、それ以外のいかなる種類の token の内部に も現れてはならない。

ECMAScript の white space code point は Table 30 に列挙されている。

Table 30: White Space Code Points
Code Points 名称 略称
U+0009 CHARACTER TABULATION <TAB>
U+000B LINE TABULATION <VT>
U+000C FORM FEED (FF) <FF>
U+FEFF ZERO WIDTH NO-BREAK SPACE <ZWNBSP>
一般カテゴリ “Space_Separator” に属する任意の code point <USP>
Note 1

U+0020 (SPACE) および U+00A0 (NO-BREAK SPACE) code point は <USP> の一部である。

Note 2

Table 30 に列挙された code point を除き、ECMAScript の WhiteSpace は、Unicode の “White_Space” property を持つが一般カテゴリ “Space_Separator” (“Zs”) に分類されないすべての code point を意図的 に除外している。

構文

WhiteSpace :: <TAB> <VT> <FF> <ZWNBSP> <USP>

12.3 Line Terminator

White space code point と同様に、line terminator code point もソーステキストの可読性を向 上させ、token(分割不能な字句単位)同士を区切るために 用いられる。しかし、white space code point とは異 なり、line terminator は構文文法の振る舞いにいくら か影響を与える。一般に、line terminator は任意の 2 つの token の間に現れ得るが、構文文法により禁止され ている箇所がいくつかある。line terminator は、自動 セミコロン挿入の処理にも影響する (12.10)。 line terminator は、StringLiteralTemplate、または TemplateSubstitutionTail を除くいかなる token の内部にも現れてはならない。 <LF> および <CR> line terminator は、 LineContinuation の一部である場合を除き、 StringLiteral token の内部に現れてはならない。

line terminator は MultiLineComment の内部 には現れ得るが、SingleLineComment の内部には現れて はならない。

line terminator は、正規表現における \s class で一致される white space code point の集合 に含まれる。

ECMAScript の line terminator code point は Table 31 に列挙されている。

Table 31: Line Terminator Code Points
Code Point Unicode 名 略称
U+000A LINE FEED (LF) <LF>
U+000D CARRIAGE RETURN (CR) <CR>
U+2028 LINE SEPARATOR <LS>
U+2029 PARAGRAPH SEPARATOR <PS>

Table 31 にある Unicode code point だけが line terminator として扱われる。その他の改行または改行区切り Unicode code point は line terminator としては扱われず、 Table 30 に列挙された要件を満たすなら white space として扱わ れる。<CR><LF> の並びは一般に line terminator として用いられる。これは、行番号を報告する 目的においては単一の SourceCharacter と見なされるべ きである。

構文

LineTerminator :: <LF> <CR> <LS> <PS> LineTerminatorSequence :: <LF> <CR> [lookahead ≠ <LF>] <LS> <PS> <CR> <LF>

12.4 Comment

Comment は、単一行でも複数行でもよい。複数行 comment は入れ子にできない。

単一行 comment は LineTerminator code point を除く任意の Unicode code point を含み得るこ と、そして token は常に可能な限り長く取られるという一 般規則のため、単一行 comment は常に // マーカーか ら行末までのすべての code point から構成される。しか し、行末の LineTerminator は単一行 comment の一部 とは見なされない; それは字句文法により別個に認識され、 構文文法のための入力要素列の一部となる。この点は非常に重 要である。なぜなら、単一行 comment の有無が自動セミコ ロン挿入の処理に影響を与えないことを意味するからである (12.10 を参照)。

Comment は white space のように振る舞い、捨てら れる。ただし、MultiLineComment が line terminator code point を含む場合、その comment 全体は、構文文法による parse の目的においては LineTerminator と見なされる。

構文

Comment :: MultiLineComment SingleLineComment MultiLineComment :: /* MultiLineCommentCharsopt */ MultiLineCommentChars :: MultiLineNotAsteriskChar MultiLineCommentCharsopt * PostAsteriskCommentCharsopt PostAsteriskCommentChars :: MultiLineNotForwardSlashOrAsteriskChar MultiLineCommentCharsopt * PostAsteriskCommentCharsopt MultiLineNotAsteriskChar :: SourceCharacter but not * MultiLineNotForwardSlashOrAsteriskChar :: SourceCharacter but not one of / or * SingleLineComment :: // SingleLineCommentCharsopt SingleLineCommentChars :: SingleLineCommentChar SingleLineCommentCharsopt SingleLineCommentChar :: SourceCharacter but not LineTerminator

この節のいくつかの生成規則には、 B.1.1 で別の定義が与えられている。

12.5 Hashbang Comment

Hashbang Comment は位置依存であり、他の種類の comment と同様、構文文法のための入力要素列から捨てられ る。

構文

HashbangComment :: #! SingleLineCommentCharsopt

12.6 Token

構文

CommonToken :: IdentifierName PrivateIdentifier Punctuator NumericLiteral StringLiteral Template Note

DivPunctuatorRegularExpressionLiteralRightBracePunctuator、および TemplateSubstitutionTail の生成規則は、 CommonToken の生成規則には含まれない追加の token を導出する。

12.7 名前と予約語

IdentifierName および ReservedWord は、 Unicode Standard Annex #31, Identifier and Pattern Syntax で与えられる Default Identifier Syntax に従って解釈される token であり、ただし少数 の修正がある。ReservedWordIdentifierName の列挙された部分集合である。構文文法は、IdentifierReservedWord ではない IdentifierName とし て定義する。Unicode の識別子文法は、Unicode Standard により指定される文字 property に基づいてい る。Unicode Standard の最新版において指定されたカテ ゴリに含まれる Unicode code point は、すべての適合 ECMAScript 実装により、そのカテゴリに属するものとして 扱われなければならない。ECMAScript 実装は、Unicode Standard の後続版で定義された識別子 code point を 認識してもよい。

Note 1

この標準は特定の code point の追加を規定する: U+0024 (DOLLAR SIGN) および U+005F (LOW LINE) は IdentifierName の任意の位置で許可され る。

構文

PrivateIdentifier :: # IdentifierName IdentifierName :: IdentifierStart IdentifierName IdentifierPart IdentifierStart :: IdentifierStartChar \ UnicodeEscapeSequence IdentifierPart :: IdentifierPartChar \ UnicodeEscapeSequence IdentifierStartChar :: UnicodeIDStart $ _ IdentifierPartChar :: UnicodeIDContinue $ AsciiLetter :: one of a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z UnicodeIDStart :: Unicode property “ID_Start” を持つ任意の Unicode code point UnicodeIDContinue :: Unicode property “ID_Continue” を持つ任意の Unicode code point

非終端記号 UnicodeEscapeSequence の定義は 12.9.4 で与えられる。

Note 2

非終端記号 IdentifierPartUnicodeIDContinue を介して _ を導出する。

Note 3

Unicode property “ID_Start” および “ID_Continue” を持つ code point の集合は、それ ぞれ、Unicode property “Other_ID_Start” および “Other_ID_Continue” を持つ code point を含 む。

12.7.1 識別子名

Unicode エスケープシーケンスは IdentifierName 内で許可され、そこでは UnicodeEscapeSequenceIdentifierCodePoint に等しい単一の Unicode code point を与える。 UnicodeEscapeSequence に先行する \ はいかな る code point も与えない。UnicodeEscapeSequence は、そうでなければ不正となる code point を IdentifierName に与えるためには使用できない。言い換 えると、\ UnicodeEscapeSequence 列を、それが 与える SourceCharacter に置き換えた場合、その結果 は依然として妥当な IdentifierName でなければなら ず、かつ元の IdentifierName とまったく同じ SourceCharacter 要素列を持たなければならない。この 仕様における IdentifierName のすべての解釈は、特定 の code point を与えるために escape sequence が使 用されたかどうかに関係なく、その実際の code point に基 づいている。

Unicode Standard に従って正準同値である 2 つの IdentifierName は、それぞれの UnicodeEscapeSequence を置き換えた後で、まったく 同じ code point 列によって表現されていない限り、等し くない

12.7.1.1 Static Semantics: Early Errors

IdentifierStart :: \ UnicodeEscapeSequence IdentifierPart :: \ UnicodeEscapeSequence

12.7.1.2 Static Semantics: IdentifierCodePoints

The syntax-directed operation IdentifierCodePoints takes no arguments and returns a List of code points. It is defined piecewise over the following productions:

IdentifierName :: IdentifierStart
  1. cpIdentifierStartIdentifierCodePoint とする。
  2. « cp » を返す。
IdentifierName :: IdentifierName IdentifierPart
  1. cps を、導出された IdentifierNameIdentifierCodePoints とする。
  2. cpIdentifierPartIdentifierCodePoint とする。
  3. cps と « cp » の list-concatenation を返す。

12.7.1.3 Static Semantics: IdentifierCodePoint

The syntax-directed operation IdentifierCodePoint takes no arguments and returns a code point. It is defined piecewise over the following productions:

IdentifierStart :: IdentifierStartChar
  1. IdentifierStartChar に一致した code point を返す。
IdentifierPart :: IdentifierPartChar
  1. IdentifierPartChar に一致した code point を返す。
UnicodeEscapeSequence :: u Hex4Digits
  1. 数値が Hex4Digits の MV である code point を返す。
UnicodeEscapeSequence :: u{ CodePoint }
  1. 数値が CodePoint の MV である code point を返す。

12.7.2 キーワードと予約語

keyword と は、IdentifierName に一致する token であり、同時 に構文上の用法も持つものをいう; すなわち、それは何らかの 構文生成規則の中で、fixed width font により、文 字どおりに現れる。ECMAScript の keyword には ifwhileasyncawait など多数が含まれる。

reserved word とは、識別子として使用できない IdentifierName である。多くの keyword は reserved word だが、そうでないものもあり、また特定の 文脈でのみ予約されるものもある。if および while は reserved word である。await は async function および module の内部でのみ予約される。 async は予約されない; それは変数名または文ラベルと して制限なく使用できる。

この仕様は、文法生成規則と early error 規則を組み 合わせて、どの名前が妥当な識別子であり、どの名前が reserved word であるかを規定する。以下の ReservedWord list にある token のうち、await および yield を除くすべては、無条件に予約される。 await および yield に対する例外は、 13.1 において、parameterized された構文生成規則を用いて 規定される。さらに、いくつかの early error 規則が、 妥当な識別子の集合を制限する。 13.1.114.3.1.114.7.5.1、 および 15.7.1 を参照する。要約すると、識別子名には 5 つのカテゴリがある:

  • MathwindowtoString、および _ のように、常に識別子として許可され、keyword ではな いもの;

  • await および yield を除く、以下に列挙さ れる ReservedWord、すなわち、決して識別子として許 可されないもの;

  • await および yield、すなわち、文脈に応じ て識別子として許可されるもの;

  • strict mode code において文脈的に識別子とし て不許可となるもの: letstaticimplementsinterfacepackageprivateprotected、および public;

  • 常に識別子として許可されるが、Identifier が許 可されない特定の構文生成規則内では keyword としても 現れるもの: asasyncfromgetmetaofset、および target

conditional keyword または contextual keyword という用語は、最後の 3 つのカテゴリに 属する keyword、すなわち、ある文脈では識別子として使 用でき、別の文脈では keyword として使用されるものを指 して用いられることがある。

構文

ReservedWord :: one of await break case catch class const continue debugger default delete do else enum export extends false finally for function if import in instanceof new null return super switch this throw true try typeof var void while with yield Note 1

5.1.5 によれば、文法中の keyword は、特定の SourceCharacter 要素の文字どおりの列に一致する。 keyword 中の code point は、\ UnicodeEscapeSequence により表現することはできな い。

IdentifierName\ UnicodeEscapeSequence を含み得るが、 els\u{65} と綴って "else" という名前の変数を宣言 することはできない。 13.1.1 における early error 規則が、reserved word と同じ StringValue を持つ識別子を排除する。

Note 2

enum は現在この仕様において keyword として 使用されていない。これは将来予約語であり、将来の言 語拡張における keyword としての使用のために確保されて いる。

同様に、implementsinterfacepackageprivateprotected、および publicstrict mode code における将来予約語 である。

Note 3

arguments および eval という名前は keyword ではないが、strict mode code においてはい くつかの制限を受ける。 13.1.18.6.415.2.115.5.115.6.1、 および 15.8.1 を参照。

12.8 区切り記号

構文

Punctuator :: OptionalChainingPunctuator OtherPunctuator OptionalChainingPunctuator :: ?. [lookahead ∉ DecimalDigit] OtherPunctuator :: one of { ( ) [ ] . ... ; , < > <= >= == != === !== + - * % ** ++ -- << >> >>> & | ^ ! ~ && || ?? ? : = += -= *= %= **= <<= >>= >>>= &= |= ^= &&= ||= ??= => DivPunctuator :: / /= RightBracePunctuator :: }

12.9 リテラル

12.9.1 Null リテラル

構文

NullLiteral :: null

12.9.2 Boolean リテラル

構文

BooleanLiteral :: true false

12.9.3 数値リテラル

構文

NumericLiteralSeparator :: _ NumericLiteral :: DecimalLiteral DecimalBigIntegerLiteral NonDecimalIntegerLiteral[+Sep] NonDecimalIntegerLiteral[+Sep] BigIntLiteralSuffix LegacyOctalIntegerLiteral DecimalBigIntegerLiteral :: 0 BigIntLiteralSuffix NonZeroDigit DecimalDigits[+Sep]opt BigIntLiteralSuffix NonZeroDigit NumericLiteralSeparator DecimalDigits[+Sep] BigIntLiteralSuffix NonDecimalIntegerLiteral[Sep] :: BinaryIntegerLiteral[?Sep] OctalIntegerLiteral[?Sep] HexIntegerLiteral[?Sep] BigIntLiteralSuffix :: n DecimalLiteral :: DecimalIntegerLiteral . DecimalDigits[+Sep]opt ExponentPart[+Sep]opt . DecimalDigits[+Sep] ExponentPart[+Sep]opt DecimalIntegerLiteral ExponentPart[+Sep]opt DecimalIntegerLiteral :: 0 NonZeroDigit NonZeroDigit NumericLiteralSeparatoropt DecimalDigits[+Sep] NonOctalDecimalIntegerLiteral DecimalDigits[Sep] :: DecimalDigit DecimalDigits[?Sep] DecimalDigit [+Sep] DecimalDigits[+Sep] NumericLiteralSeparator DecimalDigit DecimalDigit :: one of 0 1 2 3 4 5 6 7 8 9 NonZeroDigit :: one of 1 2 3 4 5 6 7 8 9 ExponentPart[Sep] :: ExponentIndicator SignedInteger[?Sep] ExponentIndicator :: one of e E SignedInteger[Sep] :: DecimalDigits[?Sep] + DecimalDigits[?Sep] - DecimalDigits[?Sep] BinaryIntegerLiteral[Sep] :: 0b BinaryDigits[?Sep] 0B BinaryDigits[?Sep] BinaryDigits[Sep] :: BinaryDigit BinaryDigits[?Sep] BinaryDigit [+Sep] BinaryDigits[+Sep] NumericLiteralSeparator BinaryDigit BinaryDigit :: one of 0 1 OctalIntegerLiteral[Sep] :: 0o OctalDigits[?Sep] 0O OctalDigits[?Sep] OctalDigits[Sep] :: OctalDigit OctalDigits[?Sep] OctalDigit [+Sep] OctalDigits[+Sep] NumericLiteralSeparator OctalDigit LegacyOctalIntegerLiteral :: 0 OctalDigit LegacyOctalIntegerLiteral OctalDigit NonOctalDecimalIntegerLiteral :: 0 NonOctalDigit LegacyOctalLikeDecimalIntegerLiteral NonOctalDigit NonOctalDecimalIntegerLiteral DecimalDigit LegacyOctalLikeDecimalIntegerLiteral :: 0 OctalDigit LegacyOctalLikeDecimalIntegerLiteral OctalDigit OctalDigit :: one of 0 1 2 3 4 5 6 7 NonOctalDigit :: one of 8 9 HexIntegerLiteral[Sep] :: 0x HexDigits[?Sep] 0X HexDigits[?Sep] HexDigits[Sep] :: HexDigit HexDigits[?Sep] HexDigit [+Sep] HexDigits[+Sep] NumericLiteralSeparator HexDigit HexDigit :: one of 0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F

NumericLiteral の直後に続く SourceCharacter は、IdentifierStart または DecimalDigit であ ってはならない。

Note

たとえば: 3in は error であり、2 つの入 力要素 3in ではない。

12.9.3.1 Static Semantics: Early Errors

NumericLiteral :: LegacyOctalIntegerLiteral DecimalIntegerLiteral :: NonOctalDecimalIntegerLiteral
  • IsStrict(this production) が true なら、それは Syntax Error である。
Note
non-strict code において、この構文は Legacy である。

12.9.3.2 Static Semantics: MV

数値リテラルは Number 型または BigInt 型の値を表 す。

12.9.3.3 Static Semantics: NumericValue

The syntax-directed operation NumericValue takes no arguments and returns a Number or a BigInt. It is defined piecewise over the following productions:

NumericLiteral :: DecimalLiteral
  1. RoundMVResult(MV of DecimalLiteral) を返す。
NumericLiteral :: NonDecimalIntegerLiteral
  1. 𝔽(MV of NonDecimalIntegerLiteral) を返す。
NumericLiteral :: LegacyOctalIntegerLiteral
  1. 𝔽(MV of LegacyOctalIntegerLiteral) を返す。
NumericLiteral :: NonDecimalIntegerLiteral BigIntLiteralSuffix
  1. NonDecimalIntegerLiteral の MV に対応する BigInt 値を返す。
DecimalBigIntegerLiteral :: 0 BigIntLiteralSuffix
  1. 0 を返す。
DecimalBigIntegerLiteral :: NonZeroDigit BigIntLiteralSuffix
  1. NonZeroDigit の MV に対応する BigInt 値を返 す。
DecimalBigIntegerLiteral :: NonZeroDigit DecimalDigits BigIntLiteralSuffix NonZeroDigit NumericLiteralSeparator DecimalDigits BigIntLiteralSuffix
  1. n を、DecimalDigits に含まれる code point 数から NumericLiteralSeparator の すべての出現を除いたものとする。
  2. mv を、(NonZeroDigit の MV × 10n) に DecimalDigits の MV を加えたものとす る。
  3. (mv) を返す。

12.9.4 文字列リテラル

Note 1

文字列リテラルは、単引用符または二重引用符で囲まれた 0 個以上の Unicode code point である。Unicode code point は escape sequence を用いて表現するこ ともできる。文字列リテラルには、閉じ引用符の code point、U+005C (REVERSE SOLIDUS)、U+000D (CARRIAGE RETURN)、および U+000A (LINE FEED) を除くすべての code point を文字どおりに現すことが できる。任意の code point は escape sequence の 形で現すことができる。文字列リテラルは ECMAScript String 値へ評価される。これらの String 値を生成する 際、Unicode code point は 11.1.1 で定義されるように UTF-16 エンコードされる。基本多言 語面に属する code point は、文字列の単一の code unit 要素としてエンコードされる。それ以外のすべての code point は、文字列の 2 個の code unit 要素 としてエンコードされる。

構文

StringLiteral :: " DoubleStringCharactersopt " ' SingleStringCharactersopt ' DoubleStringCharacters :: DoubleStringCharacter DoubleStringCharactersopt SingleStringCharacters :: SingleStringCharacter SingleStringCharactersopt DoubleStringCharacter :: SourceCharacter but not one of " or \ or LineTerminator <LS> <PS> \ EscapeSequence LineContinuation SingleStringCharacter :: SourceCharacter but not one of ' or \ or LineTerminator <LS> <PS> \ EscapeSequence LineContinuation LineContinuation :: \ LineTerminatorSequence EscapeSequence :: CharacterEscapeSequence 0 [lookahead ∉ DecimalDigit] LegacyOctalEscapeSequence NonOctalDecimalEscapeSequence HexEscapeSequence UnicodeEscapeSequence CharacterEscapeSequence :: SingleEscapeCharacter NonEscapeCharacter SingleEscapeCharacter :: one of ' " \ b f n r t v NonEscapeCharacter :: SourceCharacter but not one of EscapeCharacter or LineTerminator EscapeCharacter :: SingleEscapeCharacter DecimalDigit x u LegacyOctalEscapeSequence :: 0 [lookahead ∈ { 8, 9 }] NonZeroOctalDigit [lookahead ∉ OctalDigit] ZeroToThree OctalDigit [lookahead ∉ OctalDigit] FourToSeven OctalDigit ZeroToThree OctalDigit OctalDigit NonZeroOctalDigit :: OctalDigit but not 0 ZeroToThree :: one of 0 1 2 3 FourToSeven :: one of 4 5 6 7 NonOctalDecimalEscapeSequence :: one of 8 9 HexEscapeSequence :: x HexDigit HexDigit UnicodeEscapeSequence :: u Hex4Digits u{ CodePoint } Hex4Digits :: HexDigit HexDigit HexDigit HexDigit

非終端記号 HexDigit の定義は 12.9.3 に与えられる。SourceCharacter11.1 で定義される。

Note 2

<LF> および <CR> は、空の code point 列を生成する LineContinuation の一部である 場合を除き、文字列リテラル中に現れてはならない。これら のいずれかを文字列リテラルの String 値に含める正しい 方法は、\n\u000A のような escape sequence を用いることである。

12.9.4.1 Static Semantics: Early Errors

EscapeSequence :: LegacyOctalEscapeSequence NonOctalDecimalEscapeSequence
  • IsStrict(this production) が true なら、それは Syntax Error である。
Note 1
non-strict code において、この構文は Legacy である。
Note 2

文字列リテラルが、その後に続く Use Strict Directive に先行し、それによって外側の code が strict mode になることはあり得る。実装は、そのような literal に対 して上記の規則を確実に適用しなければならない。たとえ ば、次のソーステキストは Syntax Error を含む:

function invalid() { "\7"; "use strict"; }

12.9.4.2 Static Semantics: SV

The syntax-directed operation SV takes no arguments and returns a String.

文字列リテラルは String 型の値を表す。SV は、文字 列リテラルのさまざまな部分に対して再帰的に適用することに より、文字列リテラルの String 値を生成する。この処理 の一部として、文字列リテラル内のいくつかの Unicode code point は、以下または 12.9.3 に記述されるように、数学的値を持つものとして解釈される。

Table 32: String Single Character Escape Sequences
Escape Sequence Code Unit Value Unicode Character Name Symbol
\b 0x0008 BACKSPACE <BS>
\t 0x0009 CHARACTER TABULATION <HT>
\n 0x000A LINE FEED (LF) <LF>
\v 0x000B LINE TABULATION <VT>
\f 0x000C FORM FEED (FF) <FF>
\r 0x000D CARRIAGE RETURN (CR) <CR>
\" 0x0022 QUOTATION MARK "
\' 0x0027 APOSTROPHE '
\\ 0x005C REVERSE SOLIDUS \

12.9.4.3 Static Semantics: MV

12.9.5 正規表現リテラル

Note 1

正規表現リテラルは、その literal が評価されるたび に RegExp object へ変換される入力要素である (22.2 を参照)。プログラム中の 2 つの正規表現リテラルは、たと えその内容が同一であっても、互いに === と比較して真 になることのない正規表現オブジェクトへ評価される。 RegExp object は、new RegExp によって、または RegExp constructor を関数として呼び出すことによって も、実行時に作成され得る (22.2.4 を参照)。

以下の生成規則は、正規表現リテラルの構文を記述し、入 力要素 scanner により正規表現リテラルの終端を見つけ るために用いられる。RegularExpressionBody およ び RegularExpressionFlags から成るソーステキスト は、その後、より厳密な ECMAScript 正規表現文法 (22.2.1) を用いて再び parse される。

実装は、 22.2.1 で定義 される ECMAScript 正規表現文法を拡張してよいが、以 下で定義される RegularExpressionBody および RegularExpressionFlags の生成規則や、それらによ って用いられる生成規則を拡張してはならない。

構文

RegularExpressionLiteral :: / RegularExpressionBody / RegularExpressionFlags RegularExpressionBody :: RegularExpressionFirstChar RegularExpressionChars RegularExpressionChars :: [empty] RegularExpressionChars RegularExpressionChar RegularExpressionFirstChar :: RegularExpressionNonTerminator but not one of * or \ or / or [ RegularExpressionBackslashSequence RegularExpressionClass RegularExpressionChar :: RegularExpressionNonTerminator but not one of \ or / or [ RegularExpressionBackslashSequence RegularExpressionClass RegularExpressionBackslashSequence :: \ RegularExpressionNonTerminator RegularExpressionNonTerminator :: SourceCharacter but not LineTerminator RegularExpressionClass :: [ RegularExpressionClassChars ] RegularExpressionClassChars :: [empty] RegularExpressionClassChars RegularExpressionClassChar RegularExpressionClassChar :: RegularExpressionNonTerminator but not one of ] or \ RegularExpressionBackslashSequence RegularExpressionFlags :: [empty] RegularExpressionFlags IdentifierPartChar Note 2

正規表現リテラルは空であってはならない; 空の正規表現 リテラルを表す代わりに、code unit 列 // は単一行 comment を開始する。空の正規表現を指定するには、 /(?:)/ を用いる。

12.9.5.1 Static Semantics: BodyText

The syntax-directed operation BodyText takes no arguments and returns source text. It is defined piecewise over the following productions:

RegularExpressionLiteral :: / RegularExpressionBody / RegularExpressionFlags
  1. RegularExpressionBody として認識された source text を返す。

12.9.5.2 Static Semantics: FlagText

The syntax-directed operation FlagText takes no arguments and returns source text. It is defined piecewise over the following productions:

RegularExpressionLiteral :: / RegularExpressionBody / RegularExpressionFlags
  1. RegularExpressionFlags として認識された source text を返す。

12.9.6 Template Literal の字句構成要素

構文

Template :: NoSubstitutionTemplate TemplateHead NoSubstitutionTemplate :: ` TemplateCharactersopt ` TemplateHead :: ` TemplateCharactersopt ${ TemplateSubstitutionTail :: TemplateMiddle TemplateTail TemplateMiddle :: } TemplateCharactersopt ${ TemplateTail :: } TemplateCharactersopt ` TemplateCharacters :: TemplateCharacter TemplateCharactersopt TemplateCharacter :: $ [lookahead ≠ {] \ TemplateEscapeSequence \ NotEscapeSequence LineContinuation LineTerminatorSequence SourceCharacter but not one of ` or \ or $ or LineTerminator TemplateEscapeSequence :: CharacterEscapeSequence 0 [lookahead ∉ DecimalDigit] HexEscapeSequence UnicodeEscapeSequence NotEscapeSequence :: 0 DecimalDigit DecimalDigit but not 0 x [lookahead ∉ HexDigit] x HexDigit [lookahead ∉ HexDigit] u [lookahead ∉ HexDigit] [lookahead ≠ {] u HexDigit [lookahead ∉ HexDigit] u HexDigit HexDigit [lookahead ∉ HexDigit] u HexDigit HexDigit HexDigit [lookahead ∉ HexDigit] u { [lookahead ∉ HexDigit] u { NotCodePoint [lookahead ∉ HexDigit] u { CodePoint [lookahead ∉ HexDigit] [lookahead ≠ }] NotCodePoint :: HexDigits[~Sep] but only if the MV of HexDigits > 0x10FFFF CodePoint :: HexDigits[~Sep] but only if the MV of HexDigits ≤ 0x10FFFF Note

TemplateSubstitutionTail は、 InputElementTemplateTail という代替字句目標によっ て用いられる。

12.9.6.1 Static Semantics: TV

The syntax-directed operation TV takes no arguments and returns a String or undefined. A template literal component は、TV に より String 型の値として解釈される。TV は template object の index 付き構成要素(通称 template value)を構築するために用いられる。TV では、escape sequence は、その escape sequence が表す Unicode code point の UTF-16 code unit へ置き 換えられる。

12.9.6.2 Static Semantics: TRV

The syntax-directed operation TRV takes no arguments and returns a String. A template literal component は、TRV に より String 型の値として解釈される。TRV は template object の raw 構成要素(通称 template raw value) を構築するために用いられる。TRV は TV に似ているが、 TRV では escape sequence が literal 中に現れた ままの形で解釈される点が異なる。

Note

TVLineContinuation の code unit を除 外するが、TRV はそれを含む。<CR><LF> および <CR> の LineTerminatorSequence は、TV と TRV の両方において <LF> に正規化される。<CR> または <CR><LF> 列を含めるには、明示的な TemplateEscapeSequence が必要である。

12.10 自動セミコロン挿入

ほとんどの ECMAScript の文および宣言は、セミコロ ンで終端しなければならない。そのようなセミコロンは、ソー ステキスト中に常に明示的に現れてよい。しかし便宜のため、 特定の状況では、そのようなセミコロンはソーステキストから 省略してよい。これらの状況は、そのような状況においてセミ コロンがソースコード token 列に自動的に挿入される、と 述べることにより記述される。

12.10.1 自動セミコロン挿入の規則

以下の規則において、“token” とは、現在の字句目標 記号を用いて 12 に記述されるように決定される、実際に認識された字句 token を意味する。

セミコロン挿入には 3 つの基本規則がある:

  1. ソーステキストを左から右へ parse していると き、文法のいかなる生成規則によっても許可されない token (offending token と呼ぶ)に遭遇した場 合、次の条件の 1 つ以上が真であれば、その offending token の前にセミコロンが自動的に挿入される:

    • offending token が、直前の token から 少なくとも 1 個の LineTerminator によっ て分離されている。
    • offending token が } である。
    • 直前の token が ) であり、かつ挿入され たセミコロンが do-while 文 (14.7.2) の終端セミコロンとして parse される。
  2. ソーステキストを左から右へ parse していると き、token 列入力の末尾に到達し、parser が入 力 token 列を目標非終端記号の単一 instance として parse できない場合、入力列の末尾にセミ コロンが自動的に挿入される。
  3. ソーステキストを左から右へ parse していると き、ある token が文法上は何らかの生成規則に よって許可されるが、その生成規則が制限付き生成 規則であり、かつその token が、その制限付き 生成規則内の注記 “[no LineTerminator here]” の直後に現れる terminal または nonterminal の最初の token になる場合 (したがってその token は restricted token と呼ばれる)、そして restricted token が直 前の token から少なくとも 1 個の LineTerminator によって分離されている場 合、その restricted token の前にセミコロン が自動的に挿入される。

ただし、これらの規則には追加の最優先条件がある: そ のセミコロンが empty statement として parse され てしまう場合、またはそのセミコロンが for 文 (14.7.4) の header にある 2 つのセミコロンの 1 つになってしま う場合、セミコロンは決して自動挿入されない。

Note

文法における制限付き生成規則は、次のものだけである:

UpdateExpression[Yield, Await] : LeftHandSideExpression[?Yield, ?Await] [no LineTerminator here] ++ LeftHandSideExpression[?Yield, ?Await] [no LineTerminator here] -- ContinueStatement[Yield, Await] : continue ; continue [no LineTerminator here] LabelIdentifier[?Yield, ?Await] ; BreakStatement[Yield, Await] : break ; break [no LineTerminator here] LabelIdentifier[?Yield, ?Await] ; ReturnStatement[Yield, Await] : return ; return [no LineTerminator here] Expression[+In, ?Yield, ?Await] ; ThrowStatement[Yield, Await] : throw [no LineTerminator here] Expression[+In, ?Yield, ?Await] ; YieldExpression[In, Await] : yield yield [no LineTerminator here] AssignmentExpression[?In, +Yield, ?Await] yield [no LineTerminator here] * AssignmentExpression[?In, +Yield, ?Await] ArrowFunction[In, Yield, Await] : ArrowParameters[?Yield, ?Await] [no LineTerminator here] => ConciseBody[?In] AsyncFunctionDeclaration[Yield, Await, Default] : async [no LineTerminator here] function BindingIdentifier[?Yield, ?Await] ( FormalParameters[~Yield, +Await] ) { AsyncFunctionBody } [+Default] async [no LineTerminator here] function ( FormalParameters[~Yield, +Await] ) { AsyncFunctionBody } AsyncFunctionExpression : async [no LineTerminator here] function BindingIdentifier[~Yield, +Await]opt ( FormalParameters[~Yield, +Await] ) { AsyncFunctionBody } AsyncMethod[Yield, Await] : async [no LineTerminator here] ClassElementName[?Yield, ?Await] ( UniqueFormalParameters[~Yield, +Await] ) { AsyncFunctionBody } AsyncGeneratorDeclaration[Yield, Await, Default] : async [no LineTerminator here] function * BindingIdentifier[?Yield, ?Await] ( FormalParameters[+Yield, +Await] ) { AsyncGeneratorBody } [+Default] async [no LineTerminator here] function * ( FormalParameters[+Yield, +Await] ) { AsyncGeneratorBody } AsyncGeneratorExpression : async [no LineTerminator here] function * BindingIdentifier[+Yield, +Await]opt ( FormalParameters[+Yield, +Await] ) { AsyncGeneratorBody } AsyncGeneratorMethod[Yield, Await] : async [no LineTerminator here] * ClassElementName[?Yield, ?Await] ( UniqueFormalParameters[+Yield, +Await] ) { AsyncGeneratorBody } AsyncArrowFunction[In, Yield, Await] : async [no LineTerminator here] AsyncArrowBindingIdentifier[?Yield] [no LineTerminator here] => AsyncConciseBody[?In] CoverCallExpressionAndAsyncArrowHead[?Yield, ?Await] [no LineTerminator here] => AsyncConciseBody[?In] AsyncArrowHead : async [no LineTerminator here] ArrowFormalParameters[~Yield, +Await]

これらの制限付き生成規則の実際上の効果は次のとおりで ある:

  • parser が ++ または -- token を後置 演算子として扱う位置で、それに遭遇し、かつ直前の token と ++ または -- token の間 に少なくとも 1 個の LineTerminator がある 場合、++ または -- token の前にセミ コロンが自動的に挿入される。
  • continuebreakreturnthrow、または yield token に遭遇 し、次の token より前に LineTerminator に遭遇した場合、 continuebreakreturnthrow、または yield token の後 にセミコロンが自動的に挿入される。
  • arrow function の parameter(s) の後、 => token より前に LineTerminator がある場合、セミコロンが自動的に挿入され、その punctuator は syntax error を引き起こ す。
  • async token の後、functionIdentifierName、または ( token よ り前に LineTerminator がある場合、セミ コロンが自動的に挿入され、async token は後続 token と同じ式または class element の一部としては扱われない。
  • async token の後、* token より前に LineTerminator がある場合、セミコロンが 自動的に挿入され、その punctuator は syntax error を引き起こす。

ECMAScript プログラマへの実際上の助言は次のとお りである:

  • 後置 ++ または -- 演算子は、その被 演算子と同じ行に置くべきである。
  • return または throw 文における Expression、または yield 式におけ る AssignmentExpression は、 returnthrow、または yield token と同じ行で始めるべきである。
  • break または continue 文の LabelIdentifier は、break または continue token と同じ行に置くべきで ある。
  • arrow function の parameter(s) の終 わりとその => は同じ行に置くべきである。
  • async な function または method に先 行する async token は、その直後の token と同じ行に置くべきである。

12.10.2 自動セミコロン挿入の例

この節は非規範的である。

次のソース

{ 1 2 } 3

は、自動セミコロン挿入規則を考慮しても、 ECMAScript 文法における妥当な文ではない。これに対し、 次のソース

{ 1
2 } 3

もまた妥当な ECMAScript 文ではないが、自動セミコ ロン挿入によって次のように変換される:

{ 1
;2 ;} 3;

これは妥当な ECMAScript 文である。

次のソース

for (a; b
)

は妥当な ECMAScript 文ではなく、for 文の header に必要なセミコロンであるため、自動セミコロン挿 入によって変更もされない。自動セミコロン挿入は、for 文の header にある 2 つのセミコロンのいずれかを挿入 することは決してない。

次のソース

return
a + b

は、自動セミコロン挿入によって次のように変換される:

return;
a + b;
Note 1

a + breturn 文によって返される値 としては扱われない。なぜなら LineTerminator が token return とそれを分離しているからである。

次のソース

a = b
++c

は、自動セミコロン挿入によって次のように変換される:

a = b;
++c;
Note 2

token ++ は、変数 b に適用される後置演 算子としては扱われない。なぜなら b++ の間 に LineTerminator が存在するからである。

次のソース

if (a > b)
else c = d

は妥当な ECMAScript 文ではなく、else token の前で自動セミコロン挿入によって変更もされな い。たとえその地点で文法のいかなる生成規則も適用できな くても、自動挿入されたセミコロンは empty statement として parse されてしまうからである。

次のソース

a = b + c
(d + e).print()

は、自動セミコロン挿入によって変換されない。な ぜなら 2 行目の先頭にある括弧付き式は、関数呼出しの引数 list として解釈できるからである:

a = b + c(d + e).print()

代入文が左丸括弧で始まらなければならない状況では、プ ログラマは自動セミコロン挿入に頼るのではなく、先行する文 の末尾に明示的なセミコロンを与えるのがよい考えである。

12.10.3 自動セミコロン挿入の興味深い事例

この節は非規範的である。

ECMAScript プログラムは、自動セミコロン挿入に依存 することで、非常に少ないセミコロンを用いるスタイルでも記 述できる。上で述べたように、セミコロンはすべての改行位置 に挿入されるわけではなく、自動セミコロン挿入は line terminator をまたぐ複数 token に依存し得る。

新しい構文機能が ECMAScript に追加されると、その前 にある自動セミコロン挿入に依存する行について、parse 時 に文法生成規則が変化するような追加の文法生成規則が加えら れ得る。

この節の目的において、自動セミコロン挿入の事例が興味 深いと見なされるのは、それが、先行するソーステキストに応 じてセミコロンが挿入される場合とされない場合がある場所で ある場合である。この節の残りでは、このバージョンの ECMAScript における自動セミコロン挿入の興味深い事例を いくつか記述する。

12.10.3.1 Statement List における自動セミコロン挿入の興味深い事例

StatementList において、多くの StatementListItem はセミコロンで終わり、それは自 動セミコロン挿入によって省略できる。上記規則の結果、式 を終える行の末尾では、続く行が次のいずれかで始まる場合、 セミコロンが必要となる:

  • 左丸括弧 (()。セミコ ロンがない場合、2 行は together で CallExpression として扱われる。
  • 左角括弧 ([)。セミコ ロンがない場合、2 行は ArrayLiteral または ArrayAssignmentPattern ではなく、 property access として扱われる。
  • template literal (`)。 セミコロンがない場合、2 行は together でタグ付き Template(13.3.11) として解釈され、前の式が MemberExpression にな る。
  • 単項 + または -。 セミコロンがない場合、2 行は together で対応する二項 演算子の使用として解釈される。
  • RegExp literal。セミコロ ンがない場合、2 行は代わりに / MultiplicativeOperator として parse され得 る。たとえば RegExp が flag を持つ場合である。

12.10.3.2 自動セミコロン挿入と “[no LineTerminator here]” の事例

この節は非規範的である。

ECMAScript は “[no LineTerminator here]” を含む文法生成規則を持つ。これらの生成規則 は、文法に optional operand を持たせる手段であるこ とがある。これらの位置に LineTerminator を導入す ると、optional operand を持たない文法生成規則を使 用することで、ソーステキストの文法生成規則が変化すること になる。

この節の残りでは、このバージョンの ECMAScript に おいて “[no LineTerminator here]” を用いる生 成規則をいくつか記述する。

12.10.3.2.1 optional operand と “[no LineTerminator here]” を持つ文法生成規則の一覧