diff --git a/package.json b/package.json index fee5e03..d95ab98 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@madie/cql-antlr-parser", - "version": "1.0.3", + "version": "1.0.4", "description": "Antlr Parsing of CQL in typescript", "publishConfig": { "access": "public" diff --git a/src/AntlrUtils.ts b/src/AntlrUtils.ts index f660011..8f0c8c1 100644 --- a/src/AntlrUtils.ts +++ b/src/AntlrUtils.ts @@ -13,23 +13,31 @@ export default class AntlrUtils { return undefined; } - static findChildTextByTypes( - children: ParseTree[] | undefined, - lexerType: number[], - occurrence = 1 - ): string | undefined { - let result: string | undefined = undefined; - lexerType.forEach((lexType) => { - const foundChild: string | undefined = this.findChildText( - children, - lexType, - occurrence + static findChildName(children: ParseTree[] | undefined): string | undefined { + + if (children?.length != 4) { + console.error( + "########### Entering.. children length is ", + children?.length ); - if (foundChild) { - result = foundChild; - } - }); - return result; + throw new Error("Definition might be malformed."); + + } + return children ? children[1].text : undefined; + } + + static findChildExpression( + children: ParseTree[] | undefined + ): string | undefined { + + if (children?.length != 4) { + console.error( + "########### Entering.. children length is ", + children?.length + ) + throw new Error("Definition might be malformed."); + } + return children ? children[3].text : undefined; } static findChildText( diff --git a/src/CqlAntlrListener.ts b/src/CqlAntlrListener.ts index 113af4c..474652e 100644 --- a/src/CqlAntlrListener.ts +++ b/src/CqlAntlrListener.ts @@ -100,6 +100,7 @@ export default class CqlAntlrListener implements cqlListener { enterExpressionDefinition(ctx: ExpressionDefinitionContext): void { const cqlCode: CqlExpressionDefinition | undefined = new CqlExpressionDefinitionCreator(ctx).buildDao(); + if (cqlCode) { this.cqlResult.expressionDefinitions.push(cqlCode); } diff --git a/src/CqlExpressionDefinitionCreator.ts b/src/CqlExpressionDefinitionCreator.ts index dceff31..3fff5fd 100644 --- a/src/CqlExpressionDefinitionCreator.ts +++ b/src/CqlExpressionDefinitionCreator.ts @@ -31,13 +31,9 @@ export default class CqlExpressionDefinitionCreator extends CreatorBase { return AntlrUtils.findChildText(children, cqlLexerId, occurrence); } - protected findChildTextByTypes( - lexerIdArr: number[], - occurrence = 1, + protected findChildName( + children: ParseTree[] | undefined = this.ctx.children + ): string | undefined { + const result: string | undefined = AntlrUtils.findChildName(children); + return result; + } + + protected findChildExpression( children: ParseTree[] | undefined = this.ctx.children ): string | undefined { - const result: string | undefined = AntlrUtils.findChildTextByTypes( - children, - lexerIdArr, - occurrence - ); + const result: string | undefined = AntlrUtils.findChildExpression(children); return result; } diff --git a/test/CqlAntlr.test.ts b/test/CqlAntlr.test.ts index 2d5ed5d..d4d0db0 100644 --- a/test/CqlAntlr.test.ts +++ b/test/CqlAntlr.test.ts @@ -17,16 +17,14 @@ import CqlResult from "../src/dto/CqlResult"; describe("test antlr", () => { it("parse simple Fhir CQL Definition", () => { const cqlAntlr = new CqlAntlr(simpleDefinitionCql); - const cqlResult: CqlResult = cqlAntlr.parse(); - expect(cqlResult.codes.length).toBe(0); expect(cqlResult.valueSets.length).toBe(0); expect(cqlResult.codeSystems.length).toBe(0); expect(cqlResult.parameters.length).toBe(0); - expect(cqlResult.expressionDefinitions.length).toEqual(1); + expect(cqlResult.expressionDefinitions.length).toEqual(4); cqlResult.expressionDefinitions.forEach((def) => { expect(def.name).toBeDefined(); }); diff --git a/test/testCql.ts b/test/testCql.ts index 944fcbf..98dee62 100644 --- a/test/testCql.ts +++ b/test/testCql.ts @@ -1,11 +1,23 @@ -const simpleDefinitionCql = `library TJCOverall_FHIR4 version '4.0.000' +const simpleDefinitionCql = ` + //MAT-4844: Test Define with no quotes + library ScreeningPrediabetesFHIR version '0.0.000' + using QICore version '4.1.1' + context Patient + define "SDE Ethnicity": + SDE."SDE Ethnicity" -//MAT-4844: Test Define with no quotes -define InitialPopulation: - true - + define InitialPopulation: + "VTE Prophylaxis by Medication Administered or Device Applied" + + define "Numerator": + "VTE Prophylaxis by Medication Administered or Device Applied" + + define "VTE Prophylaxis by Medication Administered or Device Applied": + ( ["MedicationAdministration": medication in "Low Dose Unfractionated Heparin for VTE Prophylaxis"] VTEMedication + where VTEMedication.status = 'completed' + ) `; const fhirTestCql = `library TJCOverall_FHIR4 version '4.0.000'