filter parse - ts

This commit is contained in:
Jan Prochazka
2020-03-12 09:29:07 +01:00
parent 8f6b211b1b
commit e2c5c8163c
8 changed files with 102 additions and 1 deletions

View File

@@ -0,0 +1,44 @@
import P from 'parsimmon';
import { FilterType } from './types';
const whitespace = P.regexp(/\s*/m);
function token(parser) {
return parser.skip(whitespace);
}
function interpretEscapes(str) {
let escapes = {
b: '\b',
f: '\f',
n: '\n',
r: '\r',
t: '\t',
};
return str.replace(/\\(u[0-9a-fA-F]{4}|[^u])/, (_, escape) => {
let type = escape.charAt(0);
let hex = escape.slice(1);
if (type === 'u') {
return String.fromCharCode(parseInt(hex, 16));
}
if (escapes.hasOwnProperty(type)) {
return escapes[type];
}
return type;
});
}
const parser = P.createLanguage({
expr: r => P.alt(r.string),
string: () =>
token(P.regexp(/"((?:\\.|.)*?)"/, 1))
.map(interpretEscapes)
.desc('string'),
});
export function parseFilter(value: string, filterType: FilterType) {
const ast = parser.expr.tryParse(value);
console.log(ast);
return ast;
}

View File

@@ -0,0 +1,3 @@
import parserFilter, { parseFilter } from './parseFilter';
test('parse string', parseFilter('"123"', 'string'));

View File

@@ -0,0 +1,3 @@
// import types from '@dbgate/types';
export type FilterType = 'number' | 'string' | 'datetime' | 'logical';