11 ECMAScript 言語: ソーステキスト

11.1 ソーステキスト

構文

SourceCharacter :: 任意の Unicode 符号位置

ECMAScript ソーステキストは、Unicode 符号位置の並びである。ECMAScript の文法によって許可される箇所では、サロゲート符号位置を含め、U+0000 から U+10FFFF までのすべての Unicode 符号位置の値が ECMAScript ソーステキスト中に現れ得る。ECMAScript ソーステキストの保存および交換に用いられる実際のエンコーディングは、この仕様においては関係しない。外部のソーステキストのエンコーディングに関係なく、適合する ECMAScript 実装は、ソーステキストを、各 SourceCharacter が Unicode 符号位置である等価な SourceCharacter 値の並びであるかのように処理する。適合する ECMAScript 実装は、ソーステキストに対していかなる正規化も行う必要はなく、またソーステキストの正規化を行っているかのように振る舞う必要もない。

結合文字列の各構成要素は、利用者がその列全体を単一の文字と見なすかもしれない場合であっても、個別の Unicode 符号位置として扱われる。

Note

文字列リテラル、正規表現リテラル、テンプレートリテラル、および識別子においては、任意の Unicode 符号位置は、その符号位置の数値を明示的に表す Unicode エスケープシーケンスを用いて表すこともできる。コメントの内部では、そのようなエスケープシーケンスは実質的にコメントの一部として無視される。

ECMAScript は、Unicode エスケープシーケンスの振る舞いにおいて Java プログラミング言語と異なる。たとえば Java プログラムでは、Unicode エスケープシーケンス \u000A が単一行コメントの中に現れると、それは行終端子(Unicode 符号位置 U+000A は LINE FEED (LF) である)として解釈され、その結果、次の符号位置はコメントの一部ではなくなる。同様に、Unicode エスケープシーケンス \u000A が Java プログラム中の文字列リテラルに現れると、それもまた行終端子として解釈されるが、これは文字列リテラル内では許されない—文字列リテラルの値の一部として LINE FEED (LF) を含めるには、\u000A の代わりに \n と書かなければならない。ECMAScript プログラムでは、コメント中に現れる Unicode エスケープシーケンスは決して解釈されず、したがってコメントの終端に寄与することはない。同様に、ECMAScript プログラムの文字列リテラル中に現れる Unicode エスケープシーケンスは常にそのリテラルに寄与し、行終端子として、あるいは文字列リテラルを終端し得る符号位置として解釈されることは決してない。

11.1.1 Static Semantics: UTF16EncodeCodePoint ( cp )

The abstract operation UTF16EncodeCodePoint takes argument cp (a Unicode code point) and returns a String. It performs the following steps when called:

  1. Assert: 0 ≤ cp ≤ 0x10FFFF.
  2. cp ≤ 0xFFFF なら、数値が cp である code unit からなる String 値を返す。
  3. cu1 を、数値が floor((cp - 0x10000) / 0x400) + 0xD800 である code unit とする。
  4. cu2 を、数値が ((cp - 0x10000) modulo 0x400) + 0xDC00 である code unit とする。
  5. cu1cu2string-concatenation を返す。

11.1.2 Static Semantics: CodePointsToString ( text )

The abstract operation CodePointsToString takes argument text (a sequence of Unicode code points) and returns a String. これは、6.1.4 に記述されるように、text を String 値へ変換する。 It performs the following steps when called:

  1. result を空の String とする。
  2. text の各 code point cp について、次を行う
    1. resultresultUTF16EncodeCodePoint(cp) の string-concatenation に設定する。
  3. result を返す。

11.1.3 Static Semantics: UTF16SurrogatePairToCodePoint ( lead, trail )

The abstract operation UTF16SurrogatePairToCodePoint takes arguments lead (a code unit) and trail (a code unit) and returns a code point. UTF-16 のサロゲート対を形成する 2 つの code unit は、1 つの code point に変換される。 It performs the following steps when called:

  1. Assert: lead は上位サロゲートであり、trail は下位サロゲートである。
  2. cp を (lead - 0xD800) × 0x400 + (trail - 0xDC00) + 0x10000 とする。
  3. code point cp を返す。

11.1.4 Static Semantics: CodePointAt ( string, position )

The abstract operation CodePointAt takes arguments string (a String) and position (a non-negative integer) and returns a Record with fields [[CodePoint]] (a code point), [[CodeUnitCount]] (a positive integer), and [[IsUnpairedSurrogate]] (a Boolean). これは、6.1.4 に記述されるように string を UTF-16 で符号化された code point の並びとして解釈し、その index position にある code unit から始まる 1 個の code point を読み取る。 It performs the following steps when called:

  1. sizestring の長さとする。
  2. Assert: position ≥ 0 and position < size.
  3. firststring 内の index position にある code unit とする。
  4. cp を、数値が first の数値である code point とする。
  5. first が上位サロゲートでも下位サロゲートでもないなら、
    1. Record { [[CodePoint]]: cp, [[CodeUnitCount]]: 1, [[IsUnpairedSurrogate]]: false } を返す。
  6. first が下位サロゲートであるか、または position + 1 = size なら、
    1. Record { [[CodePoint]]: cp, [[CodeUnitCount]]: 1, [[IsUnpairedSurrogate]]: true } を返す。
  7. secondstring 内の index position + 1 にある code unit とする。
  8. second が下位サロゲートでないなら、
    1. Record { [[CodePoint]]: cp, [[CodeUnitCount]]: 1, [[IsUnpairedSurrogate]]: true } を返す。
  9. cpUTF16SurrogatePairToCodePoint(first, second) に設定する。
  10. Record { [[CodePoint]]: cp, [[CodeUnitCount]]: 2, [[IsUnpairedSurrogate]]: false } を返す。

11.1.5 Static Semantics: StringToCodePoints ( string )

The abstract operation StringToCodePoints takes argument string (a String) and returns a List of code points. これは、6.1.4 に記述されるように、string を UTF-16 で符号化された Unicode テキストとして解釈した結果得られる Unicode code point の並びを返す。 It performs the following steps when called:

  1. codePoints を新しい空の List とする。
  2. sizestring の長さとする。
  3. position を 0 とする。
  4. position < size の間、繰り返す
    1. cpCodePointAt(string, position) とする。
    2. cp.[[CodePoint]]codePoints に append する。
    3. positionposition + cp.[[CodeUnitCount]] に設定する。
  5. codePoints を返す。

11.1.6 Static Semantics: ParseText ( sourceText, goalSymbol )

The abstract operation ParseText takes arguments sourceText (a String or a sequence of Unicode code points) and goalSymbol (a nonterminal in one of the ECMAScript grammars) and returns a Parse Node or a non-empty List of SyntaxError objects. It performs the following steps when called:

  1. sourceText が String なら、sourceTextStringToCodePoints(sourceText) に設定する。
  2. goalSymbol を目標記号として sourceText の parse を試み、その parse 結果について early error 条件を解析する。parse と early error 検出は、implementation-defined な方法で交互に行われてもよい。
  3. parse が成功し、かつ early error が見つからなかったなら、parse の結果得られた parse tree の根にある Parse NodegoalSymbol の instance)を返す。
  4. parse error および / または early error を表す 1 個以上の SyntaxError object の List を返す。複数の parse error または early error が存在する場合、List 中の error object の数および順序は implementation-defined であるが、少なくとも 1 個は存在しなければならない。
Note 1

あるテキストにおいて、ある地点に early error があり、さらに後の地点に syntax error もある場合を考える。parse の pass の後に early error の pass を行う実装は、syntax error を報告して early error の pass には進まないかもしれない。2 つの処理を交互に行う実装は、early error を報告して syntax error を見つける処理には進まないかもしれない。第三の実装は、両方の error を報告するかもしれない。これらの挙動はいずれも適合している。

Note 2

17 も参照。

11.2 ソースコードの種類

ECMAScript コードには 4 種類ある:

Note 1

関数コードは通常、関数定義(15.2)、Arrow 関数定義(15.3)、メソッド定義(15.4)、Generator 関数定義(15.5)、Async 関数定義(15.8)、Async Generator 関数定義(15.6)、および Async Arrow 関数(15.9)の本体として与えられる。関数コードはまた、Function constructor20.2.1.1)、GeneratorFunction constructor27.3.1.1)、AsyncFunction constructor27.7.1.1)、および AsyncGeneratorFunction constructor27.4.1.1)への引数からも導出される。

Note 2

BindingIdentifier を関数コードに含めることの実際上の効果は、strict mode code に対する Early Errors が、その本体に "use strict" directive を含む関数の名前である BindingIdentifier にも適用されるということであり、たとえ周囲のコードが strict mode code でなくてもそうである。

11.2.1 Directive Prologue と Use Strict Directive

Directive Prologue とは、FunctionBodyScriptBody、または ModuleBody の先頭の StatementListItem または ModuleItem として現れる ExpressionStatement のうち最長の列であって、その列中の各 ExpressionStatementStringLiteral token とそれに続くセミコロンだけから構成されるものをいう。セミコロンは明示的に現れてもよいし、自動セミコロン挿入(12.10)によって挿入されてもよい。Directive Prologue は空の列であってもよい。

Use Strict Directive とは、Directive Prologue 内の ExpressionStatement であって、その StringLiteral が正確に "use strict" または 'use strict' のいずれかの code point 列であるものをいう。Use Strict DirectiveEscapeSequence または LineContinuation を含んではならない。

Directive Prologue は複数の Use Strict Directive を含んでもよい。ただし、その場合、実装は警告を出してもよい。

Note

Directive PrologueExpressionStatement は、それを含む生成規則の評価中に通常どおり評価される。実装は、Use Strict Directive ではなく、かつ Directive Prologue 内に現れる ExpressionStatement に対して、実装固有の意味を定義してもよい。適切な通知機構が存在するなら、Directive Prologue 内で、Use Strict Directive ではなく、かつ実装によって意味が定義されていない ExpressionStatement に遭遇した場合、実装は警告を出すべきである。

11.2.2 Strict Mode Code

ECMAScript の構文単位は、制限なしまたは strict mode の構文および意味論(4.3.2)のいずれかを用いて処理され得る。コードは、次の状況では strict mode code として解釈される:

strict mode code でない ECMAScript code は、non-strict code と呼ばれる。

11.2.2.1 Static Semantics: IsStrict ( node )

The abstract operation IsStrict takes argument node (a Parse Node) and returns a Boolean. It performs the following steps when called:

  1. node に対応するソーステキストが strict mode code なら、true を返す。
  2. false を返す。

11.2.3 非 ECMAScript 関数

ECMAScript 実装は、その評価動作が ECMAScript ソーステキスト以外の、何らかの host-defined な実行可能コード形式で表現される関数特殊オブジェクトの評価をサポートしてもよい。関数オブジェクトが ECMAScript code 内で定義されたものであるか、あるいは組込み関数であるかは、そのような関数オブジェクトを呼び出す、またはそのような関数オブジェクトから呼び出される ECMAScript code の観点からは観測不能である。