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:
- Assert: 0 ≤ cp ≤ 0x10FFFF.
- If cp ≤ 0xFFFF, return the String value consisting of the code unit whose numeric value is cp.
- Let cu1 be the code unit whose numeric value is floor((cp - 0x10000) / 0x400) + 0xD800.
- Let cu2 be the code unit whose numeric value is ((cp - 0x10000) modulo 0x400) + 0xDC00.
- 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. text を 6.1.4 で述べるように String 値へ変換する。 It performs the following steps when called:
- Let result be the empty String.
- For each code point cp of text, do
- Set result to the string-concatenation of result and UTF16EncodeCodePoint(cp).
- 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:
- Assert: lead is a leading surrogate and trail is a trailing surrogate.
- Let cp be (lead - 0xD800) × 0x400 + (trail - 0xDC00) + 0x10000.
- 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. string を 6.1.4 に記述される UTF-16 エンコードされたコードポイント列として解釈し、インデックス position のコードユニットから始まる 1 つのコードポイントを読み取る。 It performs the following steps when called:
- Let size be the length of string.
- Assert: position ≥ 0 and position < size.
- Let first be the code unit at index position within string.
- Let cp be the code point whose numeric value is the numeric value of first.
- If first is neither a leading surrogate nor a trailing surrogate, then
- Return the Record { [[CodePoint]]: cp, [[CodeUnitCount]]: 1, [[IsUnpairedSurrogate]]: false }.
- If first is a trailing surrogate or position + 1 = size, then
- Return the Record { [[CodePoint]]: cp, [[CodeUnitCount]]: 1, [[IsUnpairedSurrogate]]: true }.
- Let second be the code unit at index position + 1 within string.
- If second is not a trailing surrogate, then
- Return the Record { [[CodePoint]]: cp, [[CodeUnitCount]]: 1, [[IsUnpairedSurrogate]]: true }.
- Set cp to UTF16SurrogatePairToCodePoint(first, second).
- 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. string を 6.1.4 に記述される UTF-16 エンコードされた Unicode テキストとして解釈した結果得られる Unicode 符号位置列を返す。 It performs the following steps when called:
- Let codePoints be a new empty List.
- Let size be the length of string.
- Let position be 0.
- Repeat, while position < size,
- Let cp be CodePointAt(string, position).
- Append cp.[[CodePoint]] to codePoints.
- Set position to position + cp.[[CodeUnitCount]].
- 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:
- If sourceText is a String, set sourceText to StringToCodePoints(sourceText).
- 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.
- 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.
- 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