cql-antlr-parser
Version:
Antlr Parsing of CQL in typescript
48 lines (47 loc) • 1.62 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AntlrUtils = void 0;
const ParserRuleContext_1 = require("antlr4ts/ParserRuleContext");
const misc_1 = require("antlr4ts/misc");
class AntlrUtils {
static findText(ctx) {
var _a, _b;
const stop = (_a = ctx.stop) === null || _a === void 0 ? void 0 : _a.stopIndex;
if (typeof stop === "number") {
const interval = new misc_1.Interval(ctx.start.startIndex, stop);
return (_b = ctx.start.inputStream) === null || _b === void 0 ? void 0 : _b.getText(interval);
}
return undefined;
}
static findChildText(children, lexerType, occurrence = 1) {
if (!children || children.length === 0) {
return undefined;
}
const foundChild = AntlrUtils.findChild(children, lexerType, occurrence);
if (!foundChild) {
return undefined;
}
return AntlrUtils.findText(foundChild);
}
static findChild(children, lexerType, occurrence) {
let found = 0;
for (let child of children) {
if (AntlrUtils.isTargetType(child, lexerType)) {
if (++found === occurrence) {
return child;
}
}
}
return undefined;
}
static isTargetType(child, targetType) {
if (child instanceof ParserRuleContext_1.ParserRuleContext) {
const rule = child;
return rule.start.type === targetType;
}
else {
return false;
}
}
}
exports.AntlrUtils = AntlrUtils;