antlr-ng
Version:
Next generation ANTLR Tool
126 lines (125 loc) • 4.54 kB
JavaScript
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
import { IntervalSet } from "antlr4ng";
import { Character } from "../support/Character.js";
import { CharSupport } from "./CharSupport.js";
import { getPropertyCodePoints } from "../support/Unicode.js";
var ResultType = /* @__PURE__ */ ((ResultType2) => {
ResultType2[ResultType2["Invalid"] = 0] = "Invalid";
ResultType2[ResultType2["CodePoint"] = 1] = "CodePoint";
ResultType2[ResultType2["Property"] = 2] = "Property";
return ResultType2;
})(ResultType || {});
;
class EscapeSequenceParsing {
static {
__name(this, "EscapeSequenceParsing");
}
static #emptySet = IntervalSet.of(-1, -1);
static #fullSet = IntervalSet.of(Character.MIN_CODE_POINT, Character.MAX_CODE_POINT);
/**
* Parses a single escape sequence starting at `startOff`.
*
* @returns a type of INVALID if no valid escape sequence was found, a Result otherwise.
*/
static parseEscape(s, startOff) {
let offset = startOff;
if (offset + 2 > s.length || s.codePointAt(offset) !== 92) {
return EscapeSequenceParsing.invalid(startOff, s.length - 1);
}
offset++;
const escaped = s.codePointAt(offset);
offset += Character.charCount(escaped);
if (escaped === 117) {
if (offset + 3 > s.length) {
return EscapeSequenceParsing.invalid(startOff, s.length - 1);
}
let hexStartOffset;
let hexEndOffset;
if (s.codePointAt(offset) === 123) {
hexStartOffset = offset + 1;
hexEndOffset = s.indexOf("}", hexStartOffset);
if (hexEndOffset === -1) {
return EscapeSequenceParsing.invalid(startOff, s.length - 1);
}
offset = hexEndOffset + 1;
} else {
if (offset + 4 > s.length) {
return EscapeSequenceParsing.invalid(startOff, s.length - 1);
}
hexStartOffset = offset;
hexEndOffset = offset + 4;
offset = hexEndOffset;
}
const codePointValue = CharSupport.parseHexValue(s, hexStartOffset, hexEndOffset);
if (codePointValue === -1 || codePointValue > Character.MAX_CODE_POINT) {
return EscapeSequenceParsing.invalid(startOff, startOff + 6 - 1);
}
return {
type: 1 /* CodePoint */,
codePoint: codePointValue,
propertyIntervalSet: EscapeSequenceParsing.#emptySet,
startOffset: startOff,
parseLength: offset - startOff
};
} else if (escaped === 112 || escaped === 80) {
if (offset + 3 > s.length) {
return EscapeSequenceParsing.invalid(startOff, s.length - 1);
}
if (s.codePointAt(offset) !== 123) {
return EscapeSequenceParsing.invalid(startOff, offset);
}
const openBraceOffset = offset;
const closeBraceOffset = s.indexOf("}", openBraceOffset);
if (closeBraceOffset === -1) {
return EscapeSequenceParsing.invalid(startOff, s.length - 1);
}
const propertyName = s.substring(openBraceOffset + 1, closeBraceOffset);
const lookupResult = getPropertyCodePoints(propertyName);
if (lookupResult.status !== "ok" || lookupResult.codePoints === void 0 || lookupResult.codePoints.length === 0) {
return EscapeSequenceParsing.invalid(startOff, closeBraceOffset);
}
offset = closeBraceOffset + 1;
let codePoints = lookupResult.codePoints;
if (escaped === 80) {
codePoints = codePoints.complementWithVocabulary(EscapeSequenceParsing.#fullSet);
}
return {
type: 2 /* Property */,
codePoint: -1,
propertyIntervalSet: codePoints,
startOffset: startOff,
parseLength: offset - startOff
};
} else {
let codePoint = CharSupport.ANTLRLiteralEscapedCharValue.get(s[offset - 1]);
if (codePoint === void 0) {
if (escaped !== 93 && escaped !== 45) {
return EscapeSequenceParsing.invalid(startOff, startOff + 1);
} else {
codePoint = escaped;
}
}
return {
type: 1 /* CodePoint */,
codePoint,
propertyIntervalSet: EscapeSequenceParsing.#emptySet,
startOffset: startOff,
parseLength: offset - startOff
};
}
}
static invalid(start, stop) {
return {
type: 0 /* Invalid */,
codePoint: 0,
propertyIntervalSet: EscapeSequenceParsing.#emptySet,
startOffset: start,
parseLength: stop - start + 1
};
}
}
export {
EscapeSequenceParsing,
ResultType
};