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:
- Assert: 0 ≤ cp ≤ 0x10FFFF.
- cp ≤ 0xFFFF なら、数値が cp である code unit からなる String 値を返す。
- cu1 を、数値が floor((cp - 0x10000) / 0x400) + 0xD800 である code unit とする。
- cu2 を、数値が ((cp - 0x10000) modulo 0x400) + 0xDC00 である code unit とする。
- cu1 と cu2 の string-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:
- result を空の String とする。
- text の各 code point cp について、次を行う
- result を result と UTF16EncodeCodePoint(cp) の string-concatenation に設定する。
- 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:
- Assert: lead は上位サロゲートであり、trail は下位サロゲートである。
- cp を (lead - 0xD800) × 0x400 + (trail - 0xDC00) + 0x10000 とする。
- 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:
- size を string の長さとする。
- Assert: position ≥ 0 and position < size.
- first を string 内の index position にある code unit とする。
- cp を、数値が first の数値である code point とする。
- first が上位サロゲートでも下位サロゲートでもないなら、
- Record { [[CodePoint]]: cp, [[CodeUnitCount]]: 1, [[IsUnpairedSurrogate]]: false } を返す。
- first が下位サロゲートであるか、または position + 1 = size なら、
- Record { [[CodePoint]]: cp, [[CodeUnitCount]]: 1, [[IsUnpairedSurrogate]]: true } を返す。
- second を string 内の index position + 1 にある code unit とする。
- second が下位サロゲートでないなら、
- Record { [[CodePoint]]: cp, [[CodeUnitCount]]: 1, [[IsUnpairedSurrogate]]: true } を返す。
- cp を UTF16SurrogatePairToCodePoint(first, second) に設定する。
- 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:
- codePoints を新しい空の List とする。
- size を string の長さとする。
- position を 0 とする。
- position < size の間、繰り返す
- cp を CodePointAt(string, position) とする。
- cp.[[CodePoint]] を codePoints に append する。
- position を position + cp.[[CodeUnitCount]] に設定する。
- 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:
- sourceText が String なら、sourceText を StringToCodePoints(sourceText) に設定する。
- goalSymbol を目標記号として sourceText の parse を試み、その parse 結果について early error 条件を解析する。parse と early error 検出は、implementation-defined な方法で交互に行われてもよい。
- parse が成功し、かつ early error が見つからなかったなら、parse の結果得られた parse tree の根にある Parse Node(goalSymbol の instance)を返す。
- 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