Spaces:
Sleeping
Sleeping
File size: 5,905 Bytes
443c22e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | /**
* @fileoverview Utility functions to locate the source text of each code unit in the value of a string literal or template token.
* @author Francesco Trotta
*/
"use strict";
/**
* Represents a code unit produced by the evaluation of a JavaScript common token like a string
* literal or template token.
*/
class CodeUnit {
constructor(start, source) {
this.start = start;
this.source = source;
}
get end() {
return this.start + this.length;
}
get length() {
return this.source.length;
}
}
/**
* An object used to keep track of the position in a source text where the next characters will be read.
*/
class TextReader {
constructor(source) {
this.source = source;
this.pos = 0;
}
/**
* Advances the reading position of the specified number of characters.
* @param {number} length Number of characters to advance.
* @returns {void}
*/
advance(length) {
this.pos += length;
}
/**
* Reads characters from the source.
* @param {number} [offset=0] The offset where reading starts, relative to the current position.
* @param {number} [length=1] Number of characters to read.
* @returns {string} A substring of source characters.
*/
read(offset = 0, length = 1) {
const start = offset + this.pos;
return this.source.slice(start, start + length);
}
}
const SIMPLE_ESCAPE_SEQUENCES = {
__proto__: null,
b: "\b",
f: "\f",
n: "\n",
r: "\r",
t: "\t",
v: "\v",
};
/**
* Reads a hex escape sequence.
* @param {TextReader} reader The reader should be positioned on the first hexadecimal digit.
* @param {number} length The number of hexadecimal digits.
* @returns {string} A code unit.
*/
function readHexSequence(reader, length) {
const str = reader.read(0, length);
const charCode = parseInt(str, 16);
reader.advance(length);
return String.fromCharCode(charCode);
}
/**
* Reads a Unicode escape sequence.
* @param {TextReader} reader The reader should be positioned after the "u".
* @returns {string} A code unit.
*/
function readUnicodeSequence(reader) {
const regExp = /\{(?<hexDigits>[\dA-F]+)\}/iuy;
regExp.lastIndex = reader.pos;
const match = regExp.exec(reader.source);
if (match) {
const codePoint = parseInt(match.groups.hexDigits, 16);
reader.pos = regExp.lastIndex;
return String.fromCodePoint(codePoint);
}
return readHexSequence(reader, 4);
}
/**
* Reads an octal escape sequence.
* @param {TextReader} reader The reader should be positioned after the first octal digit.
* @param {number} maxLength The maximum number of octal digits.
* @returns {string} A code unit.
*/
function readOctalSequence(reader, maxLength) {
const [octalStr] = reader.read(-1, maxLength).match(/^[0-7]+/u);
reader.advance(octalStr.length - 1);
const octal = parseInt(octalStr, 8);
return String.fromCharCode(octal);
}
/**
* Reads an escape sequence or line continuation.
* @param {TextReader} reader The reader should be positioned on the backslash.
* @returns {string} A string of zero, one or two code units.
*/
function readEscapeSequenceOrLineContinuation(reader) {
const char = reader.read(1);
reader.advance(2);
const unitChar = SIMPLE_ESCAPE_SEQUENCES[char];
if (unitChar) {
return unitChar;
}
switch (char) {
case "x":
return readHexSequence(reader, 2);
case "u":
return readUnicodeSequence(reader);
case "\r":
if (reader.read() === "\n") {
reader.advance(1);
}
// fallthrough
case "\n":
case "\u2028":
case "\u2029":
return "";
case "0":
case "1":
case "2":
case "3":
return readOctalSequence(reader, 3);
case "4":
case "5":
case "6":
case "7":
return readOctalSequence(reader, 2);
default:
return char;
}
}
/**
* Reads an escape sequence or line continuation and generates the respective `CodeUnit` elements.
* @param {TextReader} reader The reader should be positioned on the backslash.
* @returns {Generator<CodeUnit>} Zero, one or two `CodeUnit` elements.
*/
function* mapEscapeSequenceOrLineContinuation(reader) {
const start = reader.pos;
const str = readEscapeSequenceOrLineContinuation(reader);
const end = reader.pos;
const source = reader.source.slice(start, end);
switch (str.length) {
case 0:
break;
case 1:
yield new CodeUnit(start, source);
break;
default:
yield new CodeUnit(start, source);
yield new CodeUnit(start, source);
break;
}
}
/**
* Parses a string literal.
* @param {string} source The string literal to parse, including the delimiting quotes.
* @returns {CodeUnit[]} A list of code units produced by the string literal.
*/
function parseStringLiteral(source) {
const reader = new TextReader(source);
const quote = reader.read();
reader.advance(1);
const codeUnits = [];
for (;;) {
const char = reader.read();
if (char === quote) {
break;
}
if (char === "\\") {
codeUnits.push(...mapEscapeSequenceOrLineContinuation(reader));
} else {
codeUnits.push(new CodeUnit(reader.pos, char));
reader.advance(1);
}
}
return codeUnits;
}
/**
* Parses a template token.
* @param {string} source The template token to parse, including the delimiting sequences `` ` ``, `${` and `}`.
* @returns {CodeUnit[]} A list of code units produced by the template token.
*/
function parseTemplateToken(source) {
const reader = new TextReader(source);
reader.advance(1);
const codeUnits = [];
for (;;) {
const char = reader.read();
if (char === "`" || (char === "$" && reader.read(1) === "{")) {
break;
}
if (char === "\\") {
codeUnits.push(...mapEscapeSequenceOrLineContinuation(reader));
} else {
let unitSource;
if (char === "\r" && reader.read(1) === "\n") {
unitSource = "\r\n";
} else {
unitSource = char;
}
codeUnits.push(new CodeUnit(reader.pos, unitSource));
reader.advance(unitSource.length);
}
}
return codeUnits;
}
module.exports = { parseStringLiteral, parseTemplateToken };
|