TypeScript 源码详细解读(2)词法1-字符处理
<p>本节文章研究的代码位于 tsc/src/compiler/scanner.ts</p><h2> </h2>
<h2>字符</h2>
<p>任何源码都是由很多字符组成的,这些字符可以是字母、数字、空格、符号、汉字等……</p>
<p>每一个字符都有一个编码值,比如字符“a”的编码值是97,字符“林”的编码值是26519。</p>
<p>每个字符对应的编码值是多少是由编码表决定的,上面所示的编码值是全球统一的编码表 Unicode 中的编码值,如果没有特别声明,所有编码值都是以 Unicode 为准的。</p>
<p>一般地,字符的编码值都是有序的,比如字符“a”的编码值是97,字符“b”的编码值是98,字符“c”的编码值是99,汉字则是按照笔划顺序排序的。在给字符串排序时,也是根据每个字符的编码值大小进行排序的。</p>
<p>如果想要判断一个字符是不是英文字母,只需要判断这个字符的编码值是否位于字符“a”的编码值和字符“z”的编码值之间即可。</p>
<p>在 JavaScript 中,可以通过 "a".charCodeAt(0) 获取字符“a”的编码值;通过 String.fromCharCode(97) 获取指定编码值对应的字符。</p>
<p> </p>
<h2> CharacterCodes 枚举</h2>
<p>在代码中如果直接写 99,你可能不清楚这个数字的含义,但如果写成 CharacterCodes.c,你就可以很快明白。通过枚举给每个编码值定义一个名称,方便读者理解,同时我们也不需要去记忆每个字符的实际编码值。CharacterCodes 枚举位于 tsc/src/compiler/types.ts,源码如下:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> @internal </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
export const enum CharacterCodes {</span><span style="color: rgba(0, 0, 0, 1)">
_0 </span>= 0x30<span style="color: rgba(0, 0, 0, 1)">,
_1 </span>= 0x31<span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> ...(略)</span>
_9 = 0x39<span style="color: rgba(0, 0, 0, 1)">,
a </span>= 0x61<span style="color: rgba(0, 0, 0, 1)">,
b </span>= 0x62<span style="color: rgba(0, 0, 0, 1)">,</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> ...(略)</span><span style="color: rgba(0, 0, 0, 1)">
z </span>= 0x7A<span style="color: rgba(0, 0, 0, 1)">,
A </span>= 0x41<span style="color: rgba(0, 0, 0, 1)">,</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> ...(略)</span><span style="color: rgba(0, 0, 0, 1)">
Z </span>= 0x5a<span style="color: rgba(0, 0, 0, 1)">,
ampersand </span>= 0x26, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> &</span>
asterisk = 0x2A, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> *</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> ...(略)</span>
}</pre>
</div>
<h2> </h2>
<h2>字符判断</h2>
<p>要判断一个字符是不是数字字符,只需确认它的字符编码是不是在“0”和“9”的编码值之间:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">function</span> isDigit(ch: number): <span style="color: rgba(0, 0, 255, 1)">boolean</span> {<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 参数 ch 表示一个编码值</span>
<span style="color: rgba(0, 0, 255, 1)">return</span> ch >= CharacterCodes._0 && ch <=<span style="color: rgba(0, 0, 0, 1)"> CharacterCodes._9;
}</span></pre>
</div>
<p>同理,还可以判断其它字符,比如判断是不是换行符:</p>
<div class="cnblogs_code">
<pre>export <span style="color: rgba(0, 0, 255, 1)">function</span> isLineBreak(ch: number): <span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> ES5 7.3:</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> The ECMAScript line terminator characters are listed in Table 3.</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Table 3: Line Terminator Characters</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Code Unit Value Name Formal Name</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> \u000A Line Feed <LF></span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> \u000D Carriage Return <CR></span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> \u2028 Line separator <LS></span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> \u2029 Paragraph separator <PS></span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Only the characters in Table 3 are treated as line terminators. Other new line or line</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> breaking characters are treated as white space but not as line terminators.</span>
<span style="color: rgba(0, 0, 255, 1)">return</span> ch === CharacterCodes.lineFeed ||<span style="color: rgba(0, 0, 0, 1)">
ch </span>=== CharacterCodes.carriageReturn ||<span style="color: rgba(0, 0, 0, 1)">
ch </span>=== CharacterCodes.lineSeparator ||<span style="color: rgba(0, 0, 0, 1)">
ch </span>===<span style="color: rgba(0, 0, 0, 1)"> CharacterCodes.paragraphSeparator;
}</span></pre>
</div>
<p>根据 ES 规范,换行符一共有 4 个,虽然平常我们只实用前两个,但对有些语言来说,后两个也是需要的。</p>
<p>判断是不是空格:</p>
<div class="cnblogs_code">
<pre>export <span style="color: rgba(0, 0, 255, 1)">function</span> isWhiteSpaceLike(ch: number): <span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 0, 255, 1)">return</span> isWhiteSpaceSingleLine(ch) ||<span style="color: rgba(0, 0, 0, 1)"> isLineBreak(ch);
}
</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">* Does not include line breaks. For that, see isWhiteSpaceLike. </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
export </span><span style="color: rgba(0, 0, 255, 1)">function</span> isWhiteSpaceSingleLine(ch: number): <span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Note: nextLine is in the Zs space, and should be considered to be a whitespace.</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> It is explicitly not a line-break as it isn't in the exact set specified by EcmaScript.</span>
<span style="color: rgba(0, 0, 255, 1)">return</span> ch === CharacterCodes.space ||<span style="color: rgba(0, 0, 0, 1)">
ch </span>=== CharacterCodes.tab ||<span style="color: rgba(0, 0, 0, 1)">
ch </span>=== CharacterCodes.verticalTab ||<span style="color: rgba(0, 0, 0, 1)">
ch </span>=== CharacterCodes.formFeed ||<span style="color: rgba(0, 0, 0, 1)">
ch </span>=== CharacterCodes.nonBreakingSpace ||<span style="color: rgba(0, 0, 0, 1)">
ch </span>=== CharacterCodes.nextLine ||<span style="color: rgba(0, 0, 0, 1)">
ch </span>=== CharacterCodes.ogham ||<span style="color: rgba(0, 0, 0, 1)">
ch </span>>= CharacterCodes.enQuad && ch <= CharacterCodes.zeroWidthSpace ||<span style="color: rgba(0, 0, 0, 1)">
ch </span>=== CharacterCodes.narrowNoBreakSpace ||<span style="color: rgba(0, 0, 0, 1)">
ch </span>=== CharacterCodes.mathematicalSpace ||<span style="color: rgba(0, 0, 0, 1)">
ch </span>=== CharacterCodes.ideographicSpace ||<span style="color: rgba(0, 0, 0, 1)">
ch </span>===<span style="color: rgba(0, 0, 0, 1)"> CharacterCodes.byteOrderMark;
}</span></pre>
</div>
<p>有的地方需要把换行当空格处理,有的地方不需要,所以 TypeScript 拆成两个函数,一个包括换行符,一个不包括。</p>
<p> </p>
<h2>判断标识符(Identifier)</h2>
<p>标识符即俗称的变量名,我们都知道 JS 中变量名是不能随便取的,是有规则的,比如开头不能是数字。</p>
<p>在 ES 规范中,明确地点名了:哪些字符可以做标识符;哪些字符可以做标识符但不能以它开头。TypeScript 实现了 isUnicodeIdentifierStart 和 isUnicodeIdentifierPart 来分别判断。</p>
<p>哪些字符可以做标识符,其实是没有简单的规律的,这些都是在 ES 规范一个个手动指定的,规范中这个列表很长,最简单的实现就是:手动记录每个字符是否允许作标识符,然后查表。</p>
<p><img src="https://img2018.cnblogs.com/blog/158732/202001/158732-20200113144617619-575033413.png" alt="" width="628" height="239"></p>
<p> </p>
<p> </p>
<p> </p>
<p>不过字符很多,每个字符单独记录要占用很大空间,所以 TypeScript 设计了一个小算法来压缩内存,算法基于这么一个事实:一般地,允许作为标识符的字符都是连续的一段(比如“a”到“z”)。</p>
<p> <img src="https://img2018.cnblogs.com/blog/158732/202001/158732-20200113145024103-1799941839.png" alt="" width="627" height="256"></p>
<p> </p>
<p>只要记录每段的开头和结尾部分,就可以比原先的记录该段的所有字符,要更节约内存。</p>
<p>将所有开始位置和结束位置放在同一个数组,数组的奇数位即图中的蓝色段,表示每段开头,偶数位即绿色段,表示每段结尾。</p>
<p>当需要查找一个字符是不是标识符时,采用二分搜索算法,快速定位确认它是否在包含的段中。</p>
<div class="cnblogs_code">
<pre>const unicodeESNextIdentifierStart =
const unicodeESNextIdentifierPart </span>=
</span><span style="color: rgba(0, 0, 255, 1)">function</span> lookupInUnicodeMap(code: number, map: readonly number[]): <span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 由于代码中多数字符还是英文字符,如果是就不查表直接判断</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Bail out quickly if it couldn't possibly be in the map.</span>
<span style="color: rgba(0, 0, 255, 1)">if</span> (code < map) {
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 以下是标准二分搜索算法,不懂的同学请自己补课</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Perform binary search in one of the Unicode range maps</span>
let lo = 0<span style="color: rgba(0, 0, 0, 1)">;
let hi: number </span>=<span style="color: rgba(0, 0, 0, 1)"> map.length;
let mid: number;
</span><span style="color: rgba(0, 0, 255, 1)">while</span> (lo + 1 <<span style="color: rgba(0, 0, 0, 1)"> hi) {
mid </span>= lo + (hi - lo) / 2<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> mid has to be even to catch a range's beginning</span>
mid -= mid % 2<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (map <= code && code <= map) {
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;
}
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (code <<span style="color: rgba(0, 0, 0, 1)"> map) {
hi </span>=<span style="color: rgba(0, 0, 0, 1)"> mid;
} </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
lo </span>= mid + 2<span style="color: rgba(0, 0, 0, 1)">;
}
}
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;
}</span></pre>
</div>
<p>接下来就可以看明白 isUnicodeIdentifierStart 和 isUnicodeIdentifierPart 这两个函数了:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> @internal </span><span style="color: rgba(0, 128, 0, 1)">*/</span> export <span style="color: rgba(0, 0, 255, 1)">function</span> isUnicodeIdentifierStart(code: number, languageVersion: ScriptTarget |<span style="color: rgba(0, 0, 0, 1)"> undefined) {
</span><span style="color: rgba(0, 0, 255, 1)">return</span> languageVersion! >= ScriptTarget.ES2015 ?<span style="color: rgba(0, 0, 0, 1)">
lookupInUnicodeMap(code, unicodeESNextIdentifierStart) :
languageVersion</span>! === ScriptTarget.ES5 ?<span style="color: rgba(0, 0, 0, 1)"> lookupInUnicodeMap(code, unicodeES5IdentifierStart) :
lookupInUnicodeMap(code, unicodeES3IdentifierStart);
}
</span><span style="color: rgba(0, 0, 255, 1)">function</span> isUnicodeIdentifierPart(code: number, languageVersion: ScriptTarget |<span style="color: rgba(0, 0, 0, 1)"> undefined) {
</span><span style="color: rgba(0, 0, 255, 1)">return</span> languageVersion! >= ScriptTarget.ES2015 ?<span style="color: rgba(0, 0, 0, 1)">
lookupInUnicodeMap(code, unicodeESNextIdentifierPart) :
languageVersion</span>! === ScriptTarget.ES5 ?<span style="color: rgba(0, 0, 0, 1)"> lookupInUnicodeMap(code, unicodeES5IdentifierPart) :
lookupInUnicodeMap(code, unicodeES3IdentifierPart);
}</span></pre>
</div>
<p>由于 TypeScript 支持不同版本的 ES 代码,且不同版本的 ES 规范对标识符的定义有细微查表,所以 TypeScript 内部准备了不同版本的表。</p>
<p> </p>
<p>通过以上俩函数的结合,也就可以判断一个字符串是不是合法的标识符了:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> @internal </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
export </span><span style="color: rgba(0, 0, 255, 1)">function</span> isIdentifierText(name: string, languageVersion: ScriptTarget | undefined): <span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> {
let ch </span>= codePointAt(name, 0<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 0, 1)">isIdentifierStart(ch, languageVersion)) {
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;
}
</span><span style="color: rgba(0, 0, 255, 1)">for</span> (let i = charSize(ch); i < name.length; i +=<span style="color: rgba(0, 0, 0, 1)"> charSize(ch)) {
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (!isIdentifierPart(ch =<span style="color: rgba(0, 0, 0, 1)"> codePointAt(name, i), languageVersion)) {
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;
}
}
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;
}</span></pre>
</div>
<h2> </h2>
<h2>行列号和索引</h2>
<p>如果将源码看成字符串,每个字符都有一个字符串的下标索引,同时这个字符又可以理解为源码中的第几行第几列。</p>
<p>给定一个字符串的索引,可以通过扫描这个索引之前有几个换行符确定这个索引属于第几行第几列,反过来,通过行列号也可以确认这个位置对应的字符串索引。</p>
<p>在源码中如果发现一个错误,编译器需要向用户报告错误,并明确指出位置,一般地,编译器需要将错误的行列报出来(如果报的是索引那你自己慢慢数……),为了能够在报错时知道这些位置,编译器在词法扫描阶段就需要保存一切源码位置了,那编译器存的是行列号还是索引呢?</p>
<p>有的编译器选择了存行列号,因为行列号才是用户最后需要的,但行列号意味着需要两个字段存储这个信息,如果将它们分别处理,每次处理行列号的地方都需要两行代码,如果将它们合并为一个对象,这在 JavaScript 引擎中会造成大量的引用对象,影响性能。因此 TypeScript 选择:存储索引。出错的时候,再将索引换算成行列号显示出来。</p>
<p>TypeScript 用 Position(位置)这个术语表示索引,用 LineAndCharacter(行和字符)这个术语表示行列号。这三者都是从 0 开始计数的,即 line = 0 表示第一行。</p>
<p>为什么是 LineAndCharacter 而不是 LineAndColumn(行列),主要为了和 VSCode 中的 LineColumn 区分,多数情况,LineAndCharacter 和 LineAndColumn 是一样的,除非碰到制表符(TAB)缩进,一个 TAB 始终是一个字符,但它可能跨越 2 列、4 列、8列等(具体根据用户配置)。TypeScript 并不在意 TAB 这个字符,统一将它当一个字符处理可以简单许多,所以为了避免和 VSCode 的行列混淆,改用了别的称呼。</p>
<p> </p>
<p>基于索引计算行列号需要遍历这个索引之前的所有字符,为了加速计算,TypeScript 作了一个小优化:缓存每行第一个字符的索引,然后通过二分搜索查找对应的行列(又是二分?)</p>
<p>首先计算每行第一个字符的索引表:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> @internal </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
export </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> computeLineStarts(text: string): number[] {
const result: number[] </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Array();
let pos </span>= 0<span style="color: rgba(0, 0, 0, 1)">;
let lineStart </span>= 0<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">while</span> (pos <<span style="color: rgba(0, 0, 0, 1)"> text.length) {
const ch </span>=<span style="color: rgba(0, 0, 0, 1)"> text.charCodeAt(pos);
pos</span>++<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">switch</span><span style="color: rgba(0, 0, 0, 1)"> (ch) {
</span><span style="color: rgba(0, 0, 255, 1)">case</span><span style="color: rgba(0, 0, 0, 1)"> CharacterCodes.carriageReturn:
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (text.charCodeAt(pos) ===<span style="color: rgba(0, 0, 0, 1)"> CharacterCodes.lineFeed) {
pos</span>++<span style="color: rgba(0, 0, 0, 1)">;
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> falls through</span>
<span style="color: rgba(0, 0, 255, 1)">case</span><span style="color: rgba(0, 0, 0, 1)"> CharacterCodes.lineFeed:
result.push(lineStart);
lineStart </span>=<span style="color: rgba(0, 0, 0, 1)"> pos;
</span><span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">default</span><span style="color: rgba(0, 0, 0, 1)">:
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (ch > CharacterCodes.maxAsciiCharacter &&<span style="color: rgba(0, 0, 0, 1)"> isLineBreak(ch)) {
result.push(lineStart);
lineStart </span>=<span style="color: rgba(0, 0, 0, 1)"> pos;
}
</span><span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">;
}
}
result.push(lineStart);
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> result;
}</span></pre>
</div>
<p>然后检索索引表查询行列号:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> @internal </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
* We assume the first line starts at position 0 and 'position' is non-negative.
</span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
export </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> computeLineAndCharacterOfPosition(lineStarts: readonly number[], position: number): LineAndCharacter {
let lineNumber </span>=<span style="color: rgba(0, 0, 0, 1)"> binarySearch(lineStarts, position, identity, compareValues);
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (lineNumber < 0<span style="color: rgba(0, 0, 0, 1)">) {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> If the actual position was not found,</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> the binary search returns the 2's-complement of the next line start</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> e.g. if the line starts at and the position requested was 20</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> then the search will return -2.</span>
<span style="color: rgba(0, 128, 0, 1)">//
</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> We want the index of the previous line start, so we subtract 1.</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Review 2's-complement if this is confusing.</span>
lineNumber = ~lineNumber - 1<span style="color: rgba(0, 0, 0, 1)">;
Debug.assert(lineNumber </span>!== -1, "position cannot precede the beginning of the file"<span style="color: rgba(0, 0, 0, 1)">);
}
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> {
line: lineNumber,
character: position </span>-<span style="color: rgba(0, 0, 0, 1)"> lineStarts
};
}</span></pre>
</div>
<p>同时使用索引表也可以实现从行列号查询索引:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> @internal </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
export </span><span style="color: rgba(0, 0, 255, 1)">function</span> computePositionOfLineAndCharacter(lineStarts: readonly number[], line: number, character: number, debugText?: string, allowEdits?: <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">): number {
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (line < 0 || line >=<span style="color: rgba(0, 0, 0, 1)"> lineStarts.length) {
</span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (allowEdits) {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Clamp line to nearest allowable value</span>
line = line < 0 ? 0 : line >= lineStarts.length ? lineStarts.length - 1<span style="color: rgba(0, 0, 0, 1)"> : line;
}
</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
Debug.fail(`Bad line number. Line: ${line}, lineStarts.length: ${lineStarts.length} , line map is correct</span>? ${debugText !== undefined ? arraysEqual(lineStarts, computeLineStarts(debugText)) : "unknown"<span style="color: rgba(0, 0, 0, 1)">}`);
}
}
const res </span>= lineStarts +<span style="color: rgba(0, 0, 0, 1)"> character;
</span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (allowEdits) {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Clamp to nearest allowable values to allow the underlying to be edited without crashing (accuracy is lost, instead)</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> TODO: Somehow track edits between file as it was during the creation of sourcemap we have and the current file and</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> apply them to the computed position to improve accuracy</span>
<span style="color: rgba(0, 0, 255, 1)">return</span> res > lineStarts ? lineStarts : <span style="color: rgba(0, 0, 255, 1)">typeof</span> debugText === "string" && res > debugText.length ?<span style="color: rgba(0, 0, 0, 1)"> debugText.length : res;
}
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (line < lineStarts.length - 1<span style="color: rgba(0, 0, 0, 1)">) {
Debug.assert(res </span>< lineStarts);
}
</span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span> (debugText !==<span style="color: rgba(0, 0, 0, 1)"> undefined) {
Debug.assert(res </span><= debugText.length); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Allow single character overflow for trailing newline</span>
<span style="color: rgba(0, 0, 0, 1)"> }
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> res;
}</span></pre>
</div>
<h2>小结</h2>
<p>本节介绍了 scanner 中的一些独立函数,这些函数都将被词法扫描程序中调用。先独立理解了这些概念,对完全理解词法扫描会有重大帮助。</p>
<p>下节将介绍:词法扫描的实现(即 scanner.ts 中剩余的其它函数)【更新于 2020-1-18】</p>
<p>#如果你有问题可以在评论区提问#</p><br><br>
来源:https://www.cnblogs.com/xuld/p/12187519.html
頁:
[1]