11 ECMAScript 言語: ソーステキスト (ECMAScript Language: Source Text)

11.1 ソーステキスト (Source Text)

構文 (Syntax)

SourceCharacter :: any Unicode code point

ECMAScript ソーステキスト は Unicode 符号位置の列である。ECMAScript 文法で許容される箇所であれば、U+0000 から U+10FFFF までのすべての Unicode 符号位置(サロゲートコードポイントを含む)が ECMAScript ソーステキスト中に現れうる。ECMAScript ソーステキストを格納または交換するために実際に用いられるエンコーディングは本仕様にとって本質的ではない。外部的なソーステキストのエンコーディングに関わらず、適合する ECMAScript 実装はソーステキストを、各 SourceCharacter が 1 つの Unicode 符号位置であるような同値の SourceCharacter 値列として処理する。適合実装はソーステキストの正規化を行うこと、または正規化を行っているかのように振る舞うことを要求されない。

結合文字シーケンスの構成要素は、利用者がその全体を 1 文字と捉える可能性があっても、個々の独立した Unicode 符号位置として扱われる。

Note

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

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

11.1.1 静的セマンティクス: UTF16EncodeCodePoint ( cp: a Unicode code point, ): a String

The abstract operation UNKNOWN takes UNPARSEABLE ARGUMENTS. It performs the following steps when called:

  1. Assert: 0 ≤ cp ≤ 0x10FFFF.
  2. If cp ≤ 0xFFFF, return the String value consisting of the code unit whose numeric value is cp.
  3. Let cu1 be the code unit whose numeric value is floor((cp - 0x10000) / 0x400) + 0xD800.
  4. Let cu2 be the code unit whose numeric value is ((cp - 0x10000) modulo 0x400) + 0xDC00.
  5. Return the string-concatenation of cu1 and cu2.

11.1.2 静的セマンティクス: CodePointsToString ( text: a sequence of Unicode code points, ): a String

The abstract operation UNKNOWN takes UNPARSEABLE ARGUMENTS. text6.1.4 で述べるように String 値へ変換する。 It performs the following steps when called:

  1. Let result be the empty String.
  2. For each code point cp of text, do
    1. Set result to the string-concatenation of result and UTF16EncodeCodePoint(cp).
  3. Return result.

11.1.3 静的セマンティクス: UTF16SurrogatePairToCodePoint ( lead: a code unit, trail: a code unit, ): a code point

The abstract operation UNKNOWN takes UNPARSEABLE ARGUMENTS. UTF-16 サロゲートペアを形成する 2 つのコードユニットを 1 つのコードポイントへ変換する。 It performs the following steps when called:

  1. Assert: lead is a leading surrogate and trail is a trailing surrogate.
  2. Let cp be (lead - 0xD800) × 0x400 + (trail - 0xDC00) + 0x10000.
  3. Return the code point cp.

11.1.4 静的セマンティクス: CodePointAt ( string: a String, position: a non-negative integer, ): a Record with fields [[CodePoint]] (a code point), [[CodeUnitCount]] (a positive integer), and [[IsUnpairedSurrogate]] (a Boolean)

The abstract operation UNKNOWN takes UNPARSEABLE ARGUMENTS. string6.1.4 に記述される UTF-16 エンコードされたコードポイント列として解釈し、インデックス position のコードユニットから始まる 1 つのコードポイントを読み取る。 It performs the following steps when called:

  1. Let size be the length of string.
  2. Assert: position ≥ 0 and position < size.
  3. Let first be the code unit at index position within string.
  4. Let cp be the code point whose numeric value is the numeric value of first.
  5. If first is neither a leading surrogate nor a trailing surrogate, then
    1. Return the Record { [[CodePoint]]: cp, [[CodeUnitCount]]: 1, [[IsUnpairedSurrogate]]: false }.
  6. If first is a trailing surrogate or position + 1 = size, then
    1. Return the Record { [[CodePoint]]: cp, [[CodeUnitCount]]: 1, [[IsUnpairedSurrogate]]: true }.
  7. Let second be the code unit at index position + 1 within string.
  8. If second is not a trailing surrogate, then
    1. Return the Record { [[CodePoint]]: cp, [[CodeUnitCount]]: 1, [[IsUnpairedSurrogate]]: true }.
  9. Set cp to UTF16SurrogatePairToCodePoint(first, second).
  10. Return the Record { [[CodePoint]]: cp, [[CodeUnitCount]]: 2, [[IsUnpairedSurrogate]]: false }.

11.1.5 静的セマンティクス: StringToCodePoints ( string: a String, ): a List of code points

The abstract operation UNKNOWN takes UNPARSEABLE ARGUMENTS. string6.1.4 に記述される UTF-16 エンコードされた Unicode テキストとして解釈した結果得られる Unicode 符号位置列を返す。 It performs the following steps when called:

  1. Let codePoints be a new empty List.
  2. Let size be the length of string.
  3. Let position be 0.
  4. Repeat, while position < size,
    1. Let cp be CodePointAt(string, position).
    2. Append cp.[[CodePoint]] to codePoints.
    3. Set position to position + cp.[[CodeUnitCount]].
  5. Return codePoints.

11.1.6 静的セマンティクス: ParseText ( sourceText: a String or a sequence of Unicode code points, goalSymbol: a nonterminal in one of the ECMAScript grammars, ): a Parse Node or a non-empty List of SyntaxError objects

The abstract operation UNKNOWN takes UNPARSEABLE ARGUMENTS. It performs the following steps when called:

  1. If sourceText is a String, set sourceText to StringToCodePoints(sourceText).
  2. Attempt to parse sourceText using goalSymbol as the goal symbol, and analyse the parse result for any early error conditions. Parsing and early error detection may be interleaved in an implementation-defined manner.
  3. If the parse succeeded and no early errors were found, return the Parse Node (an instance of goalSymbol) at the root of the parse tree resulting from the parse.
  4. Otherwise, return a List of one or more SyntaxError objects representing the parsing errors and/or early errors. If more than one parsing error or early error is present, the number and ordering of error objects in the list is implementation-defined, but at least one must be present.
Note 1

ある箇所に早期エラーが存在し、後続位置に構文エラーが存在するテキストを考える。パース後に早期エラー検査を行う実装は構文エラーを報告し、早期エラー検査へ進まないかもしれない。両者をインターリーブする実装は早期エラーを報告し構文エラー検出を行わないかもしれない。第三の実装は両方のエラーを報告するかもしれない。これらの挙動はいずれも適合である。

Note 2

17 も参照のこと。

11.2 ソースコードの種類 (Types of Source Code)

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

Note 1

関数コードは一般に Function 定義(15.2)、Arrow Function 定義(15.3)、Method 定義(15.4)、Generator Function 定義(15.5)、Async Function 定義(15.8)、Async Generator Function 定義(15.6)、および Async Arrow Function(15.9)の本体として提供される。関数コードはまた Function コンストラクタ20.2.1.1)、GeneratorFunction コンストラクタ27.3.1.1)、AsyncFunction コンストラクタ27.7.1.1)、AsyncGeneratorFunction コンストラクタ27.4.1.1)への引数から導出される。

Note 2

BindingIdentifier を関数コードに含める実際上の効果は、その関数本体が "use strict" ディレクティブを含む関数の名前である BindingIdentifier に対して、周囲のコードが strict でない場合でも strict mode の Early Error が適用されることである。

11.2.1 ディレクティブプロローグと Use Strict ディレクティブ (Directive Prologues and the Use Strict Directive)

ディレクティブプロローグ (Directive Prologue) とは、FunctionBody, ScriptBody, または ModuleBody の先頭に現れる StatementListItem もしくは ModuleItem のうち、連続して並ぶ最長の ExpressionStatement 列であって、その列中の各 ExpressionStatement がセミコロンにより終端される(明示または自動セミコロン挿入 (12.10) により)純粋な StringLiteral トークンのみから構成されるものである。ディレクティブプロローグは空列であってもよい。

Use Strict ディレクティブ (Use Strict Directive) は、その StringLiteral が正確に "use strict" または 'use strict' のいずれかの符号位置列であるディレクティブプロローグ中の ExpressionStatement である。Use Strict ディレクティブは EscapeSequenceLineContinuation を含んではならない。

ディレクティブプロローグは複数の Use Strict ディレクティブを含んでもよい。ただし実装はこれが発生した場合に警告を出してよい。

Note

ディレクティブプロローグの ExpressionStatement は包含している生成規則の評価中に通常どおり評価される。実装は、Use Strict ディレクティブではないがディレクティブプロローグ中に現れる ExpressionStatement に対し実装独自の意味を定義してよい。適切な通知手段が存在する場合、実装はディレクティブプロローグ内で Use Strict ディレクティブではなく、かつ実装により意味が定義されていない ExpressionStatement に遭遇した際、警告を発するべきである。

11.2.2 Strict Mode コード (Strict Mode Code)

ECMAScript の構文単位は制限のない(非 strict)または strict モードの構文とセマンティクス(4.3.2)で処理されうる。コードは次の状況で strict mode code として解釈される:

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

11.2.2.1 静的セマンティクス: IsStrict ( node: a Parse Node, ): a Boolean

The abstract operation UNKNOWN takes UNPARSEABLE ARGUMENTS. It performs the following steps when called:

  1. If the source text matched by node is strict mode code, return true; else return false.

11.2.3 非 ECMAScript 関数 (Non-ECMAScript Functions)

ECMAScript 実装は、評価挙動が ECMAScript ソーステキスト以外のホスト定義実行可能コード形式で記述される関数エキゾチックオブジェクトの評価をサポートしてよい。ある関数オブジェクトが ECMAScript コード内で定義されたか、組み込み関数であるかは、その関数オブジェクトが呼び出す/呼び出される ECMAScript コードの観点からは観測できない。