cql-antlr-parser
Version:
Antlr Parsing of CQL in typescript
131 lines (130 loc) • 4.36 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const AntlrUtils_1 = require("./AntlrUtils");
const cqlLexer_1 = require("./generated/cqlLexer");
class CqlAntlrListener {
constructor(cqlResult) {
this.cqlResult = cqlResult;
}
enterLibrary(ctx) {
console.log(`Entering antlr root at line number ${ctx._start.line}`);
}
enterLibraryDefinition(ctx) {
this.cqlResult.library = new CqlVersionCreator(ctx).buildDao();
}
enterUsingDefinition(ctx) {
this.cqlResult.using = new CqlVersionCreator(ctx).buildDao();
}
enterIncludeDefinition(ctx) {
const cqlInclude = new CqlIncludeCreator(ctx).buildDao();
if (cqlInclude) {
this.cqlResult.includes.push(cqlInclude);
}
}
enterCodesystemDefinition(ctx) {
const cqlCodeSystem = new CqlCodeSystemCreator(ctx).buildDao();
if (cqlCodeSystem) {
this.cqlResult.codeSystems.push(cqlCodeSystem);
}
}
enterValuesetDefinition(ctx) {
const cqlValueSet = new CqlValueSystemCreator(ctx).buildDao();
if (cqlValueSet) {
this.cqlResult.valueSets.push(cqlValueSet);
}
}
enterCodeDefinition(ctx) {
const cqlCode = new CqlCodeCreator(ctx).buildDao();
if (cqlCode) {
this.cqlResult.codes.push(cqlCode);
}
}
}
class CreatorBase {
constructor(ctx, cqlDao) {
this.ctx = ctx;
this.cqlDao = cqlDao;
}
buildDao() {
this.cqlDao.text = AntlrUtils_1.AntlrUtils.findText(this.ctx);
this.cqlDao.start = this.buildLineInfo(this.ctx.start);
this.cqlDao.stop = this.buildLineInfo(this.ctx.stop);
if (typeof this.cqlDao.text === "string") {
return this.build();
}
else {
return undefined;
}
}
buildLineInfo(token) {
if (!token) {
return undefined;
}
const lineInfo = {};
lineInfo.line = token.line;
lineInfo.position = token.charPositionInLine;
if (token.charPositionInLine !== 0) {
lineInfo.position += token.stopIndex - token.startIndex;
}
return lineInfo;
}
findChildText(cqlLexerId, occurrence = 1) {
return AntlrUtils_1.AntlrUtils.findChildText(this.ctx.children, cqlLexerId, occurrence);
}
}
class CqlVersionCreator extends CreatorBase {
constructor(ctx, cqlVersion) {
super(ctx, !cqlVersion ? {} : cqlVersion);
}
static setNameVersion(children, cqlDao) {
cqlDao.name = AntlrUtils_1.AntlrUtils.findChildText(children, cqlLexer_1.cqlLexer.IDENTIFIER);
cqlDao.version = AntlrUtils_1.AntlrUtils.findChildText(children, cqlLexer_1.cqlLexer.STRING);
}
build() {
CqlVersionCreator.setNameVersion(this.ctx.children, this.cqlDao);
return this.cqlDao;
}
}
class CqlIncludeCreator extends CreatorBase {
constructor(ctx) {
super(ctx, {});
}
build() {
CqlVersionCreator.setNameVersion(this.ctx.children, this.cqlDao);
this.cqlDao.called = this.findChildText(cqlLexer_1.cqlLexer.IDENTIFIER, 2);
return this.cqlDao;
}
}
class CqlCodeSystemCreator extends CreatorBase {
constructor(ctx) {
super(ctx, {});
}
build() {
this.cqlDao.oid = this.findChildText(cqlLexer_1.cqlLexer.STRING);
this.cqlDao.name = this.findChildText(cqlLexer_1.cqlLexer.QUOTEDIDENTIFIER);
this.cqlDao.version = this.findChildText(cqlLexer_1.cqlLexer.STRING, 2);
return this.cqlDao;
}
}
class CqlValueSystemCreator extends CreatorBase {
constructor(ctx) {
super(ctx, {});
}
build() {
this.cqlDao.name = this.findChildText(cqlLexer_1.cqlLexer.QUOTEDIDENTIFIER);
this.cqlDao.url = this.findChildText(cqlLexer_1.cqlLexer.STRING);
return this.cqlDao;
}
}
class CqlCodeCreator extends CreatorBase {
constructor(ctx) {
super(ctx, {});
}
build() {
this.cqlDao.name = this.findChildText(cqlLexer_1.cqlLexer.QUOTEDIDENTIFIER);
this.cqlDao.codeId = this.findChildText(cqlLexer_1.cqlLexer.STRING);
this.cqlDao.codeSystem = this.findChildText(cqlLexer_1.cqlLexer.QUOTEDIDENTIFIER, 2);
return this.cqlDao;
}
}
exports.default = CqlAntlrListener;