@qrvey/formula-lang 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -31,6 +31,33 @@ describe('Transpiler tests suit', () => {
|
|
|
31
31
|
expect(result.expression).toBe('(1 + 2)');
|
|
32
32
|
});
|
|
33
33
|
|
|
34
|
+
test('Should resolve basic logic operation', () => {
|
|
35
|
+
const AST: ProgramAST = {
|
|
36
|
+
type: 'Program',
|
|
37
|
+
exp: '1 <= 1',
|
|
38
|
+
lang: 'QrveyLang',
|
|
39
|
+
version: '0.0.0',
|
|
40
|
+
body: {
|
|
41
|
+
operator: '<=',
|
|
42
|
+
type: 'BinaryExpression',
|
|
43
|
+
left: {
|
|
44
|
+
type: 'Literal',
|
|
45
|
+
dataType: 'number',
|
|
46
|
+
value: 1,
|
|
47
|
+
},
|
|
48
|
+
right: {
|
|
49
|
+
type: 'Literal',
|
|
50
|
+
dataType: 'number',
|
|
51
|
+
value: 2,
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
} as ProgramAST;
|
|
55
|
+
|
|
56
|
+
const transpiler = new Transpiler(AST, ENGINES.ELASTICSEARCH);
|
|
57
|
+
const result = transpiler.get();
|
|
58
|
+
expect(result.expression).toBe('(1 <= 2)');
|
|
59
|
+
});
|
|
60
|
+
|
|
34
61
|
test('Should resolve MID function', () => {
|
|
35
62
|
const AST: ProgramAST = {
|
|
36
63
|
type: 'Program',
|
|
@@ -52,7 +52,7 @@ export interface FunctionCallAST extends CommonAST {
|
|
|
52
52
|
arguments: Array<CommonAST>;
|
|
53
53
|
}
|
|
54
54
|
export interface VariableAST extends CommonAST {
|
|
55
|
-
value: string | boolean | number | CommonAST[];
|
|
55
|
+
value: string | boolean | number | CommonAST[] | null;
|
|
56
56
|
}
|
|
57
57
|
export interface LiteralAST extends VariableAST {
|
|
58
58
|
dataType: string;
|
|
@@ -2,21 +2,21 @@ import { LRParser } from '@lezer/lr';
|
|
|
2
2
|
import { LRLanguage, LanguageSupport } from '@codemirror/language';
|
|
3
3
|
import { styleTags, tags } from '@lezer/highlight';
|
|
4
4
|
// This file was generated by lezer-generator. You probably shouldn't edit it.
|
|
5
|
-
const spec_identifier = { __proto__: null, true: 14, false: 14 };
|
|
5
|
+
const spec_identifier = { __proto__: null, true: 14, false: 14, null: 16 };
|
|
6
6
|
const parser = LRParser.deserialize({
|
|
7
7
|
version: 14,
|
|
8
|
-
states: "%
|
|
9
|
-
stateData: "
|
|
10
|
-
goto: "#
|
|
11
|
-
nodeNames: "⚠ LineComment Program Expression Value Number String Boolean Array Function FunctionIdentifier FunctionArguments Variable UnaryExpression ArithOp BinaryExpression ArithOp ArithOp ParenthesizedExpression",
|
|
12
|
-
maxTerm:
|
|
8
|
+
states: "%vO]QPOOO!]QPO'#CeOOQO'#C`'#C`OOQO'#Cg'#CgO!dQPO'#CfO!iQQO'#CiO]QPO'#CjQ!nQSOOO]QPO'#CpOOQO'#C_'#C_O!|QPO'#CzO#UQPO,59PO#ZQPO,59QO#bQPO,59TOOQO,59U,59UO]QPO,59WO]QPO,59WO]QPO,59WO#gQSO,59[O#xQPO'#CqO$SQPO,59fOOQO1G.k1G.kO$[QSO'#DOOOQO'#Ch'#ChO$pQPO1G.lOOQO1G.o1G.oOOQO1G.r1G.rO%WQSO1G.rO%bQSO1G.rOOQO1G.v1G.vOOQO,59],59]OOQO-E6o-E6oO%lQPO'#CrO%vQPO,59jOOQO7+$W7+$WO&OQSO,59^OOQO-E6p-E6p",
|
|
9
|
+
stateData: "&k~OiOSjOSPOS~OTQOUQOVQOWQO_UOkROmPOqWOtTO~OTQOUQOVQOWQOmPO~OlnP~PzOq[O~Ou]O~O_aOa_Ob`Oc`O~OocOlnX~OleO~OprP~P]OsiO~O_aOa_Ob`Oc`OpmO~OleXoeX~PzOocOlna~O_aOa_Ob`Oc`OopOprX~OprO~Oa_O_`ig`ip`io`i~Ob`ic`i~P$uOb`Oc`O~P$uOofXpfX~P]OopOpra~O_aOa_Ob`Oc`Oofapfa~OPbijki~",
|
|
10
|
+
goto: "#usPPPt!^PPPP!n!z#U#`!z!zP!zPPP!z#c#iPPPPPPP#oPPP#rQVOQ^UQbWQf[Qj_Qk`QlaRsp`XOUW[_`apQYPRnceQOPUW[_`acpaXOUW[_`apaSOUW[_`apRh[QdYRodQqfRtqRZPRg[",
|
|
11
|
+
nodeNames: "⚠ LineComment Program Expression Value Number String Boolean Null Array Function FunctionIdentifier FunctionArguments Variable UnaryExpression ArithOp BinaryExpression LogicOp ArithOp ArithOp ParenthesizedExpression",
|
|
12
|
+
maxTerm: 37,
|
|
13
13
|
skippedNodes: [0, 1],
|
|
14
14
|
repeatNodeCount: 2,
|
|
15
|
-
tokenData: "
|
|
16
|
-
tokenizers: [0, 1],
|
|
15
|
+
tokenData: "*U~RpXY#VYZ#zZ[#V]^#zpq#Vrs$Pxy%myz%rz{%w{|%||}'O}!O%|!O!P'T!P!Q'f!Q![(V!^!_(s!_!`)Q!`!a)V!c!})a!}#O)z#P#Q*P#R#S&g#T#o)a$f$g#V#BY#BZ#V$IS$I_#V$I|$I}#z$I}$JO#z$JT$JU#V$KV$KW#V&FU&FV#V?HT?HU#V~#[Yi~XY#VZ[#Vpq#V$f$g#V#BY#BZ#V$IS$I_#V$JT$JU#V$KV$KW#V&FU&FV#V?HT?HU#V~$POj~~$SVOr$Prs$is#O$P#O#P$n#P;'S$P;'S;=`%g<%lO$P~$nOU~~$qRO;'S$P;'S;=`$z;=`O$P~$}WOr$Prs$is#O$P#O#P$n#P;'S$P;'S;=`%g;=`<%l$P<%lO$P~%jP;=`<%l$P~%rOq~~%wOp~~%|Oc~V&TU_TuQ{|&g}!O&g!Q![&g!c!}&g#R#S&g#T#o&gQ&lUuQ{|&g}!O&g!Q![&g!c!}&g#R#S&g#T#o&g~'TOo~P'WP!Q!['ZP'`QTP!Q!['Z#R#S'Z~'kPb~!P!Q'n~'sSP~OY'nZ;'S'n;'S;=`(P<%lO'n~(SP;=`<%l'nR(^VTPuQ{|&g}!O&g!O!P'Z!Q![(V!c!}&g#R#S(V#T#o&gT(zQmPaS!_!`)Q!`!a)QS)VOaST)^PlPaS!_!`)QR)hUuQkP{|&g}!O&g!Q![)a!c!})a#R#S)a#T#o)a~*POt~~*UOs~",
|
|
16
|
+
tokenizers: [0, 1, 2],
|
|
17
17
|
topRules: { "Program": [0, 2] },
|
|
18
|
-
specialized: [{ term:
|
|
19
|
-
tokenPrec:
|
|
18
|
+
specialized: [{ term: 27, get: value => spec_identifier[value] || -1 }],
|
|
19
|
+
tokenPrec: 250
|
|
20
20
|
});
|
|
21
21
|
const QFormulaLang = LRLanguage.define({
|
|
22
22
|
parser: parser.configure({
|
|
@@ -28,13 +28,13 @@ const QFormulaLang = LRLanguage.define({
|
|
|
28
28
|
Variable: tags.keyword,
|
|
29
29
|
LineComment: tags.lineComment,
|
|
30
30
|
FunctionIdentifier: tags.lineComment,
|
|
31
|
-
|
|
32
|
-
})
|
|
33
|
-
]
|
|
31
|
+
'( )': tags.paren,
|
|
32
|
+
}),
|
|
33
|
+
],
|
|
34
34
|
}),
|
|
35
35
|
languageData: {
|
|
36
|
-
commentTokens: { line:
|
|
37
|
-
}
|
|
36
|
+
commentTokens: { line: '//' },
|
|
37
|
+
},
|
|
38
38
|
});
|
|
39
39
|
function QFormula() {
|
|
40
40
|
return new LanguageSupport(QFormulaLang);
|
|
@@ -39,6 +39,12 @@ function transformNode(program, node) {
|
|
|
39
39
|
dataType: 'boolean',
|
|
40
40
|
value: getNodeValue(program, node) === 'true',
|
|
41
41
|
};
|
|
42
|
+
case 'Null':
|
|
43
|
+
return {
|
|
44
|
+
type: 'Literal',
|
|
45
|
+
dataType: 'boolean',
|
|
46
|
+
value: null,
|
|
47
|
+
};
|
|
42
48
|
case 'Array':
|
|
43
49
|
return {
|
|
44
50
|
type: 'Literal',
|
|
@@ -60,9 +66,10 @@ function transformNode(program, node) {
|
|
|
60
66
|
}
|
|
61
67
|
function transformBinaryExpression(program, node) {
|
|
62
68
|
const [left, right] = node.getChildren('Expression');
|
|
63
|
-
const
|
|
69
|
+
const logicOperator = node.getChild('LogicOp');
|
|
70
|
+
const arithOperator = node.getChild('ArithOp');
|
|
64
71
|
return {
|
|
65
|
-
operator: getNodeValue(program,
|
|
72
|
+
operator: getNodeValue(program, logicOperator !== null && logicOperator !== void 0 ? logicOperator : arithOperator),
|
|
66
73
|
type: AST_TYPES.binaryExpression,
|
|
67
74
|
left: transformNode(program, left),
|
|
68
75
|
right: transformNode(program, right),
|
|
@@ -6,7 +6,7 @@ export declare class Transpiler {
|
|
|
6
6
|
constructor(ast: ProgramAST, engine: ENGINES);
|
|
7
7
|
get(): TranspilationResponse;
|
|
8
8
|
processNode(expression: CommonAST): string;
|
|
9
|
-
literal({ value, dataType }: LiteralAST): string | number | boolean | CommonAST[];
|
|
9
|
+
literal({ value, dataType }: LiteralAST): string | number | boolean | CommonAST[] | null;
|
|
10
10
|
unaryExpression({ operator, right }: UnaryExpressionAST): string;
|
|
11
11
|
binaryExpression({ left, operator, right }: BinaryExpressionAST): string;
|
|
12
12
|
functionCall(node: FunctionCallAST): string;
|