xast
Version:
AST parsing library
105 lines • 4.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Source = void 0;
const utils_1 = require("./utils");
const characterClasses_1 = require("./characterClasses");
const TokenKind_1 = require("./TokenKind");
class Source {
static isSource(source) {
return source instanceof Source;
}
constructor(body, name = 'Schema', locationOffset = { line: 1, column: 1 }) {
this.body = body;
this.name = name;
this.locationOffset = locationOffset;
}
get [Symbol.toStringTag]() {
return 'Source';
}
readDigits(start, firstCode) {
if (!(0, characterClasses_1.isDigit)(firstCode)) {
throw this.syntaxError(start, `Invalid number, got: ${this.printCodePointAt(start)}.`);
}
const body = this.body;
let position = start + 1;
while ((0, characterClasses_1.isDigit)(body.charCodeAt(position))) {
++position;
}
return position;
}
readEscapedCharacter(position) {
const body = this.body;
const code = body.charCodeAt(position + 1);
switch (code) {
case 0x0022:
return { value: '\u0022', size: 2 };
case 0x005c:
return { value: '\u005c', size: 2 };
case 0x002f:
return { value: '\u002f', size: 2 };
case 0x0062:
return { value: '\u0008', size: 2 };
case 0x0066:
return { value: '\u000c', size: 2 };
case 0x006e:
return { value: '\u000a', size: 2 };
case 0x0072:
return { value: '\u000d', size: 2 };
case 0x0074:
return { value: '\u0009', size: 2 };
}
throw this.syntaxError(position, `Invalid character escape sequence: "${body.slice(position, position + 2)}".`);
}
readEscapedUnicodeFixedWidth(position) {
const body = this.body;
const code = (0, utils_1.read16BitHexCode)(body, position + 2);
if ((0, characterClasses_1.isUnicodeScalarValue)(code)) {
return { value: String.fromCodePoint(code), size: 6 };
}
if ((0, characterClasses_1.isLeadingSurrogate)(code)) {
if (body.charCodeAt(position + 6) === 0x005c &&
body.charCodeAt(position + 7) === 0x0075) {
const trailingCode = (0, utils_1.read16BitHexCode)(body, position + 8);
if ((0, characterClasses_1.isTrailingSurrogate)(trailingCode)) {
return { value: String.fromCodePoint(code, trailingCode), size: 12 };
}
}
}
throw this.syntaxError(position, `Invalid Unicode escape sequence: "${body.slice(position, position + 6)}".`);
}
readEscapedUnicodeVariableWidth(position) {
const body = this.body;
let point = 0;
let size = 3;
while (size < 12) {
const code = body.charCodeAt(position + size++);
if (code === 0x007d) {
if (size < 5 || !(0, characterClasses_1.isUnicodeScalarValue)(point)) {
break;
}
return { value: String.fromCodePoint(point), size };
}
point = (point << 4) | (0, utils_1.readHexDigit)(code);
if (point < 0) {
break;
}
}
throw this.syntaxError(position, `Invalid Unicode escape sequence: "${body.slice(position, position + size)}".`);
}
printCodePointAt(position) {
const code = this.body.codePointAt(position);
if (code === undefined) {
return TokenKind_1.TokenKind.EOF;
}
else if (code >= 0x0020 && code <= 0x007e) {
const char = String.fromCodePoint(code);
return char === '"' ? "'\"'" : `"${char}"`;
}
return 'U+' + code.toString(16).toUpperCase().padStart(4, '0');
}
syntaxError(position, description) {
return new Error(`Syntax Error: ${description}\n${position}\n${this.body.slice(position - 10, position + 10)}`);
}
}
exports.Source = Source;
//# sourceMappingURL=Source.js.map