Skip to content

Commit

Permalink
Single line comments (#71)
Browse files Browse the repository at this point in the history
* feat: added $capabilities() function

* fix: package lock

* feat: support single line comments and comments inside strings

* fix: remove console.log
  • Loading branch information
mechanik-daniel authored May 28, 2024
1 parent 0baa499 commit bf35b6a
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 31 deletions.
5 changes: 3 additions & 2 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
* © Copyright Outburn Ltd. 2022-2024 All Rights Reserved
* Project name: FUME-COMMUNITY
*/
import { getLogger } from './helpers/logger';
import { FumeServer } from './server';

const server = new FumeServer();
server.warmUp().then((res) => {
console.log('FUME is ready!');
getLogger().info('FUME is ready!');
}).catch((err) => {
console.error('FUME failed to initialize:', err);
getLogger().error({ ERROR: 'FUME failed to initialize', details: err });
});
36 changes: 18 additions & 18 deletions src/controllers/root.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/**
* © Copyright Outburn Ltd. 2022-2024 All Rights Reserved
* Project name: FUME-COMMUNITY
*/
/**
* © Copyright Outburn Ltd. 2022-2024 All Rights Reserved
* Project name: FUME-COMMUNITY
*/

import type { Request, Response } from 'express';

import config from '../config';
import { getCache } from '../helpers/cache';
import { recacheFromServer } from '../helpers/conformance';
import { v2json } from '../helpers/hl7v2';
import { transform } from '../helpers/jsonataFunctions';
import { getLogger } from '../helpers/logger';
import type { Request, Response } from 'express';

import config from '../config';
import { getCache } from '../helpers/cache';
import { recacheFromServer } from '../helpers/conformance';
import { v2json } from '../helpers/hl7v2';
import { transform } from '../helpers/jsonataFunctions';
import { getLogger } from '../helpers/logger';
import { parseCsv } from '../helpers/stringFunctions';

const get = async (req: Request, res: Response) => {
Expand All @@ -32,16 +32,16 @@ const evaluate = async (req: Request, res: Response) => {
let inputJson;

if (req.body.contentType === 'x-application/hl7-v2+er7') {
console.log('Content-Type suggests HL7 V2.x message');
console.log('Trying to parse V2 message as JSON...');
getLogger().info('Content-Type suggests HL7 V2.x message');
getLogger().info('Trying to parse V2 message as JSON...');
inputJson = await v2json(req.body.input);
console.log('Parsed V2 message');
getLogger().info('Parsed V2 message');
} else {
if (req.body.contentType === 'text/csv') {
console.log('Content-Type suggests CSV input');
console.log('Trying to parse CSV to JSON...');
getLogger().info('Content-Type suggests CSV input');
getLogger().info('Trying to parse CSV to JSON...');
inputJson = await parseCsv(req.body.input);
console.log('Parsed CSV to JSON');
getLogger().info('Parsed CSV to JSON');
} else {
inputJson = req.body.input;
}
Expand Down
4 changes: 3 additions & 1 deletion src/helpers/conformance/getCachePath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import fs from 'fs';
import os from 'os';
import path from 'path';

import { getLogger } from '../logger';

export const getCachePath = (innerFolder = '') => {
const cachePath = path.join(os.homedir(), '.fhir', innerFolder);
if (!fs.existsSync(cachePath)) {
fs.mkdirSync(cachePath, { recursive: true });
console.log(`Directory '${cachePath}' created successfully.`);
getLogger().info(`Directory '${cachePath}' created successfully.`);
}
return cachePath;
};
Expand Down
2 changes: 2 additions & 0 deletions src/helpers/jsonataFunctions/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import * as v2 from '../hl7v2';
import { getLogger } from '../logger';
import * as objectFuncs from '../objectFunctions';
import compiler from '../parser';
import { removeComments } from '../parser/removeComments';
import runtime from '../runtime';
import * as stringFuncs from '../stringFunctions';
import { getStructureDefinition } from './getStructureDefinition';
Expand Down Expand Up @@ -105,6 +106,7 @@ export const transform = async (input, expression: string, extraBindings: Record
bindings.getMandatoriesOfStructure = compiler.getMandatoriesOfStructure;
bindings.getElementDefinition = compiler.getElementDefinition;
bindings.replaceColonsWithBrackets = compiler.replaceColonsWithBrackets;
bindings.removeComments = removeComments;
// end of debug functions

// bind all aliases from cache
Expand Down
4 changes: 3 additions & 1 deletion src/helpers/parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { funcs } from '../jsonataFuncs';
import { replaceColonsWithBrackets } from '../stringFunctions';
import { getElementDefinition } from './getElementDefinition';
import { getSnapshot } from './getSnapshot';
import { removeComments } from './removeComments';
import { toJsonataString } from './toJsonataString';

export default {
Expand All @@ -15,5 +16,6 @@ export default {
getMandatoriesOfElement: funcs.getMandatoriesOfElement,
getMandatoriesOfStructure: funcs.getMandatoriesOfStructure,
getElementDefinition,
replaceColonsWithBrackets
replaceColonsWithBrackets,
removeComments
};
65 changes: 65 additions & 0 deletions src/helpers/parser/removeComments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* © Copyright Outburn Ltd. 2022-2024 All Rights Reserved
* Project name: FUME-COMMUNITY
*/

export const removeComments = (expr: string): string => {
const exprLen: number = expr.length;
if (exprLen === 0) return expr;
let accExpr: string = '';
let currentChar: string = '';
let nextChar: string = '';
let prevChar: string = '';
let prevPrevChar: string = '';
let openedQuote: string = '';
let openedComment: string = '';

for (let i = 0; i < exprLen; i++) {
currentChar = expr.charAt(i);
nextChar = i < (exprLen - 1) ? expr.charAt(i + 1) : '';
prevChar = i > 0 ? expr.charAt(i - 1) : '';
prevPrevChar = i > 1 ? expr.charAt(i - 2) : '';

if (openedComment !== '') {
// inside a comment
const twoChars: string = prevChar + currentChar;

if (openedComment === '//' && (currentChar === '\r' || currentChar === '\n')) {
// this is the end of the // comment
openedComment = '';
accExpr += '\n';
} else {
if (openedComment === '/*' && twoChars === '*/' && prevPrevChar !== '/') {
// this is the end of the /* comment
openedComment = '';
}
};
continue;
};

// not inside a comment
if ((currentChar === '"' || currentChar === '\'') && prevChar !== '\\') {
// quote sign, unescaped
if (openedQuote === '') {
// it's an opening quote
openedQuote = currentChar;
} else {
// it's a closing quote
if (openedQuote === currentChar) {
openedQuote = '';
}
}
} else {
// not a quote sign
const twoChars: string = currentChar + nextChar;
if ((twoChars === '/*' || twoChars === '//') && openedQuote === '') {
// opening comment, not inside quotes
openedComment = twoChars;
continue;
}
}
accExpr += currentChar;
};

return accExpr;
};
6 changes: 4 additions & 2 deletions src/helpers/parser/toJsonataString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
} from '../stringFunctions';
import thrower from '../thrower';
import { getElementDefinition } from './getElementDefinition';
import { removeComments } from './removeComments';

export const toJsonataString = async (inExpr: string): Promise<string | undefined> => {
console.time('Parse to JSONATA');
Expand Down Expand Up @@ -417,9 +418,10 @@ export const toJsonataString = async (inExpr: string): Promise<string | undefine
};

try {
let parsed: string = await parseFumeExpression(inExpr);
const withoutComments: string = removeComments(inExpr);
let parsed: string = await parseFumeExpression(withoutComments);
if (!expressionHasFlash) {
res = inExpr;
res = withoutComments;
} else {
// check if expression starts with "("
const isEnclosed = parsed.trimStart().startsWith('(') && parsed.trimEnd().endsWith('(');
Expand Down
2 changes: 0 additions & 2 deletions src/helpers/stringFunctions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
isNumeric,
matches,
parseCsv,
removeComments,
removeEmptyLines,
splitToLines,
startsWith,
Expand All @@ -31,7 +30,6 @@ export {
isNumeric,
matches,
parseCsv,
removeComments,
removeEmptyLines,
replaceColonsWithBrackets,
splitToLines,
Expand Down
6 changes: 1 addition & 5 deletions src/helpers/stringFunctions/stringFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,8 @@ export const removeEmptyLines = (arr: string[] | string): string[] => {
return lines.filter((line) => line.trim() !== '');
};

// takes out all comments from expression
// TODO: add support for end-of-line // type comments
export const removeComments = (expr: string): string => expr.replace(/(\/\*[^*]*\*\/)/g, '');

// clean and split an expression. returns a line array
export const splitToLines = (expr: string): string[] => removeEmptyLines(removeComments(expr).split(/\r?\n/));
export const splitToLines = (expr: string): string[] => removeEmptyLines(expr.split(/\r?\n/));

export const hashKey = (str: string): string => sha256(str + uuid(str));

Expand Down

0 comments on commit bf35b6a

Please sign in to comment.