@freelog/freelog-cg-collection 1.0.0

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.
Files changed (73) hide show
  1. package/CollectionPolicyCustomVisitor.js +544 -0
  2. package/CollectionPolicyDecompiler.js +131 -0
  3. package/CollectionPolicyErrorLexerListener.js +23 -0
  4. package/CollectionPolicyErrorListener.js +22 -0
  5. package/README.md +398 -0
  6. package/TransitionEventArgsMatchUtil.js +79 -0
  7. package/gen/CollectionPolicyLexer.js +705 -0
  8. package/gen/CollectionPolicyListener.js +672 -0
  9. package/gen/CollectionPolicyParser.js +7301 -0
  10. package/gen/CollectionPolicyVisitor.js +454 -0
  11. package/gen/LexToken.js +680 -0
  12. package/index.js +53 -0
  13. package/indexTest.js +23 -0
  14. package/package.json +32 -0
  15. package/resources/event_definition.json +169 -0
  16. package/resources/function_definite.json +79 -0
  17. package/resources/test/CalculatorTest.json +26 -0
  18. package/resources/test/FunctionHelperTest.json +21 -0
  19. package/resources/variable_definite.json +10 -0
  20. package/resources/zhaojn.json +68 -0
  21. package/resources/zhaojn.sc +8 -0
  22. package/src/calculator/ExpArgsGroupCalculator.ts +34 -0
  23. package/src/calculator/ExpBooleanCalculator.ts +77 -0
  24. package/src/calculator/ExpCalculator.ts +68 -0
  25. package/src/calculator/ExpCollectionCalculator.ts +0 -0
  26. package/src/calculator/ExpConditionCalculator.ts +43 -0
  27. package/src/function-executor/FunctionHandle.ts +45 -0
  28. package/src/function-executor/api/FunctionApiExecutor.ts +11 -0
  29. package/src/function-executor/fun/FunctionFunExecutor.ts +31 -0
  30. package/src/function-executor/kit/FunctionKitExecutor.ts +11 -0
  31. package/src/function-executor/query/FunctionQueryExecutor.ts +11 -0
  32. package/src/function-executor/time/FunctionTimeExecutor.ts +11 -0
  33. package/src/helper/ExpHelper.ts +66 -0
  34. package/src/helper/FunctionHelper.ts +97 -0
  35. package/src/helper/PolicyHelper.ts +99 -0
  36. package/src/interface/Calculator.ts +6 -0
  37. package/src/interface/DefaultFunctionInvoke.ts +12 -0
  38. package/src/interface/FunctionInvoke.ts +6 -0
  39. package/src/interface/VarContextCapable.ts +3 -0
  40. package/src/model/AssignmentClauseInfo.ts +3 -0
  41. package/src/model/exp/EntityInfo.ts +7 -0
  42. package/src/model/exp/Exp.ts +5 -0
  43. package/src/model/exp/ExpAtom.ts +5 -0
  44. package/src/model/exp/ExpOp.ts +8 -0
  45. package/src/model/exp/ExpParen.ts +6 -0
  46. package/src/model/exp/ExpSigned.ts +7 -0
  47. package/src/model/exp/FunctionCallInfo.ts +13 -0
  48. package/src/model/exp/NumberInfo.ts +6 -0
  49. package/src/model/exp/StringInfo.ts +6 -0
  50. package/src/model/exp/VariableChainInfo.ts +7 -0
  51. package/src/model/exp-args-group/ExpArgsGroup.ts +10 -0
  52. package/src/model/exp-boolean/BooleanInfo.ts +6 -0
  53. package/src/model/exp-boolean/ExpBoolean.ts +5 -0
  54. package/src/model/exp-boolean/ExpBooleanAtom.ts +5 -0
  55. package/src/model/exp-boolean/ExpBooleanOpCollection.ts +8 -0
  56. package/src/model/exp-boolean/ExpBooleanOpCompare.ts +9 -0
  57. package/src/model/exp-boolean/ExpBooleanOpLogic.ts +8 -0
  58. package/src/model/exp-boolean/ExpBooleanParen.ts +6 -0
  59. package/src/model/exp-collection/ExpCollection.ts +10 -0
  60. package/src/model/exp-condition/ExpCondition.ts +12 -0
  61. package/src/model/policy/ActionInfo.ts +23 -0
  62. package/src/model/policy/EventInfo.ts +13 -0
  63. package/src/model/policy/PolicyInfo.ts +20 -0
  64. package/src/model/policy/VarDeclarationInfo.ts +8 -0
  65. package/test/calculator/CalculatorTest.test.ts +13 -0
  66. package/test/function/FunAddElements.ts +9 -0
  67. package/test/function/FunctionFunExecutorTest.test.ts +11 -0
  68. package/test/function/FunctionHandleTest.test.ts +9 -0
  69. package/test/helper/ExpHelperTest.test.ts +7 -0
  70. package/test/helper/FunctionHelperTest.test.ts +16 -0
  71. package/test/helper/PolicyHelperTest.test.ts +14 -0
  72. package/test/tsconfig.test.json +6 -0
  73. package/tsconfig.json +24 -0
@@ -0,0 +1,544 @@
1
+ const {CollectionPolicyVisitor} = require("./gen/CollectionPolicyVisitor");
2
+
3
+ class CollectionPolicyCustomVisitor extends CollectionPolicyVisitor {
4
+ constructor() {
5
+ super();
6
+ this.audienceSection = {};
7
+ this.varDeclarationList = [];
8
+ this.stateMachine = [];
9
+
10
+ this.currentState = null;
11
+ this.currentStateObj = null;
12
+ this.currentEventElem = null;
13
+ }
14
+
15
+ visitAudience_clause(ctx) {
16
+ this.audienceSection["audiences"] = ctx.audience().map(elem => elem.getText());
17
+
18
+ return super.visitAudience_clause(ctx);
19
+ }
20
+
21
+ visitAudience_condition(ctx) {
22
+ if (ctx.expression_param_list() != null) {
23
+ this.audienceSection["conditions"] = this.visitExpression_param_list(ctx.expression_param_list());
24
+ } else {
25
+ this.audienceSection["conditions"] = [];
26
+ }
27
+
28
+ return super.visitAudience_condition(ctx);
29
+ }
30
+
31
+ visitGlobal_variable_definition(ctx) {
32
+ let varElem = {
33
+ type: "varGlobal",
34
+ content: ctx.getText(),
35
+ varName: ctx.global_variable_name().getText(),
36
+ varValue: ctx.assignment_clause().getText(),
37
+ varValueObj: this.visitAssignment_clause(ctx.assignment_clause())
38
+ };
39
+
40
+ this.varDeclarationList.push(varElem);
41
+
42
+ return varElem;
43
+ }
44
+
45
+ visitMacro_variable_definition(ctx) {
46
+ let varElem = {
47
+ type: "varMacro",
48
+ content: ctx.getText(),
49
+ varName: ctx.macro_variable_name().getText(),
50
+ varValue: ctx.assignment_clause().getText(),
51
+ varValueObj: this.visitAssignment_clause(ctx.assignment_clause())
52
+ };
53
+
54
+ this.varDeclarationList.push(varElem);
55
+
56
+ return varElem;
57
+ }
58
+
59
+ visitExpression_definition(ctx) {
60
+ let builtInFunctionResult = this.visitBuilt_in_function(ctx.expression_def_declaration().built_in_function());
61
+
62
+ let varElem = {
63
+ type: "expCustom",
64
+ content: ctx.getText(),
65
+ varName: builtInFunctionResult["funcName"],
66
+ varValue: ctx.condition_expression().getText(),
67
+ varValueObj: this.visitCondition_expression(ctx.condition_expression())
68
+ };
69
+ let paramList = [];
70
+ for (let i = 0; i < builtInFunctionResult["paramList"].length; i++) {
71
+ let paramElem = builtInFunctionResult["paramList"][i];
72
+ paramList.push(paramElem["paramValue"]);
73
+ }
74
+ varElem["paramList"] = paramList;
75
+
76
+ this.varDeclarationList.push(varElem);
77
+
78
+ return varElem;
79
+ }
80
+
81
+ visitState_definition(ctx) {
82
+ this.currentState = ctx.state_name().getText();
83
+ this.currentStateObj = {stateName: this.currentState, assignments: [], events: []};
84
+ this.stateMachine.push(this.currentStateObj);
85
+
86
+ return super.visitState_definition(ctx);
87
+ }
88
+
89
+ visitFsmVarAssignment(ctx) {
90
+ let assignmentElem = {
91
+ type: "varAssigment",
92
+ content: ctx.getText(),
93
+ varName: ctx.variable_assignment().variable_chain().getText(),
94
+ varValue: ctx.variable_assignment().assignment_clause().getText(),
95
+ varValueObj: super.visitAssignment_clause(ctx.variable_assignment().assignment_clause())
96
+ };
97
+
98
+ this.currentStateObj["assignments"].push(assignmentElem);
99
+
100
+ return assignmentElem;
101
+ }
102
+
103
+ visitFsmVarInitial(ctx) {
104
+ let assignmentElem = {
105
+ type: "varInitial",
106
+ content: ctx.getText(),
107
+ varName: ctx.local_variable_name().getText(),
108
+ varValue: ctx.assignment_clause().getText(),
109
+ varValueObj: super.visitAssignment_clause(ctx.assignment_clause())
110
+ };
111
+
112
+ this.currentStateObj["assignments"].push(assignmentElem);
113
+
114
+ return assignmentElem;
115
+ }
116
+
117
+ visitEventNormal(ctx) {
118
+ let builtInFunctionResult = this.visitBuilt_in_function(ctx.event().built_in_function());
119
+
120
+ this.currentEventElem = {
121
+ type: "normal",
122
+ content: ctx.getText(),
123
+ eventName: builtInFunctionResult["funcName"],
124
+ eventParamList: builtInFunctionResult["paramList"],
125
+ toState: ctx.state_name().getText(),
126
+ actions: []
127
+ };
128
+ if (ctx.event().event_path() != null) {
129
+ this.currentEventElem["eventPath"] = ctx.event().event_path().getText();
130
+ }
131
+ if (ctx.action_section() != null) {
132
+ this.visitAction_section(ctx.action_section());
133
+ }
134
+
135
+ this.currentStateObj["events"].push(this.currentEventElem);
136
+
137
+ return this.currentEventElem;
138
+ }
139
+
140
+ visitEventTerminate(ctx) {
141
+ this.currentEventElem = {
142
+ type: "terminate",
143
+ content: ctx.getText(),
144
+ eventName: ctx.TERMINATE().getText()
145
+ };
146
+
147
+ this.currentStateObj["events"].push(this.currentEventElem);
148
+
149
+ return this.currentEventElem;
150
+ }
151
+
152
+ visitAction_section(ctx) {
153
+ this.currentEventElem["actions"] = [];
154
+
155
+ return super.visitAction_section(ctx);
156
+ }
157
+
158
+ visitFsmActionFunctionCall(ctx) {
159
+ let actionElem = {
160
+ type: "functionCall",
161
+ content: ctx.getText(),
162
+ actionObj: this.visitFunction_call(ctx.function_call())
163
+ };
164
+
165
+ this.currentEventElem["actions"].push(actionElem);
166
+
167
+ return actionElem;
168
+ }
169
+
170
+ visitFsmActionVarAssignment(ctx) {
171
+ let actionElem = {
172
+ type: "varAssignment",
173
+ content: "ctx.getText()",
174
+ actionObj: {
175
+ varName: ctx.variable_assignment().variable_chain().getText(),
176
+ varValue: ctx.variable_assignment().assignment_clause().getText(),
177
+ varValueObj: this.visitAssignment_clause(ctx.variable_assignment().assignment_clause())
178
+ }
179
+ };
180
+
181
+ this.currentEventElem["actions"].push(actionElem);
182
+
183
+ return actionElem;
184
+ }
185
+
186
+ visitAssignment_clause(ctx) {
187
+ if (ctx.expression() != null) {
188
+ return this.visitExpression(ctx.expression());
189
+ } else if (ctx.boolean_expression() != null) {
190
+ return this.visitBoolean_expression(ctx.boolean_expression());
191
+ } else if (ctx.condition_expression() != null) {
192
+ return this.visitCondition_expression(ctx.condition_expression());
193
+ } else if (ctx.args_group_expression() != null) {
194
+ return this.visitArgs_group_expression(ctx.args_group_expression());
195
+ } else if (ctx.collection_expression() != null) {
196
+ return this.visitCollection_expression(ctx.collection_expression());
197
+ } else {
198
+ return super.visitAssignment_clause(ctx);
199
+ }
200
+ }
201
+
202
+ visitExpression(ctx) {
203
+ if (ctx.expression_op() != null) {
204
+ return {
205
+ expType: "expOp",
206
+ var1: this.visitExpression(ctx.expression(0)),
207
+ var2: this.visitExpression(ctx.expression(1)),
208
+ op: ctx.expression_op().getText()
209
+ };
210
+ } else {
211
+ return this.visitMultiplying_expression(ctx.multiplying_expression());
212
+ }
213
+ }
214
+
215
+ visitMultiplying_expression(ctx) {
216
+ if (ctx.multiplying_expression_op() != null) {
217
+ return {
218
+ expType: "expOp",
219
+ var1: this.visitMultiplying_expression(ctx.multiplying_expression(0)),
220
+ var2: this.visitMultiplying_expression(ctx.multiplying_expression(1)),
221
+ op: ctx.multiplying_expression_op().getText()
222
+ };
223
+ } else {
224
+ return this.visitPow_expression(ctx.pow_expression());
225
+ }
226
+ }
227
+
228
+ visitPow_expression(ctx) {
229
+ if (ctx.POW() != null) {
230
+ return {
231
+ expType: "expOp",
232
+ var1: this.visitPow_expression(ctx.pow_expression(0)),
233
+ var2: this.visitPow_expression(ctx.pow_expression(1)),
234
+ op: ctx.POW().getText()
235
+ };
236
+ } else {
237
+ return this.visitSigned_atom(ctx.signed_atom());
238
+ }
239
+ }
240
+
241
+ visitSigned_atom(ctx) {
242
+ if (ctx.function_call() != null) {
243
+ return this.visitFunction_call(ctx.function_call());
244
+ } else if (ctx.atom() != null) {
245
+ return this.visitAtom(ctx.atom());
246
+ } else if (ctx.signedSymbol != null) {
247
+ return {
248
+ expType: "expSigned",
249
+ var1: this.visitSigned_atom(ctx.signed_atom()),
250
+ symbol: ctx.signedSymbol.text
251
+ };
252
+ } else {
253
+ return super.visitSigned_atom(ctx);
254
+ }
255
+ }
256
+
257
+ visitAtom(ctx) {
258
+ if (ctx.constant() != null) {
259
+ return super.visitConstant(ctx.constant());
260
+ } else if (ctx.scientific() != null) {
261
+ return this.visitScientific(ctx.scientific());
262
+ } else if (ctx.LPAREN() != null) {
263
+ return {expType: "expParen", var1: this.visitExpression(ctx.expression())};
264
+ } else if (ctx.atom_string() != null) {
265
+ return this.visitAtom_string(ctx.atom_string());
266
+ } else if (ctx.entity_variable() != null) {
267
+ return this.visitEntity_variable(ctx.entity_variable());
268
+ } else if (ctx.variable_chain() != null) {
269
+ return this.visitVariable_chain(ctx.variable_chain());
270
+ } else {
271
+ return super.visitAtom(ctx);
272
+ }
273
+ }
274
+
275
+ visitBoolean_expression(ctx) {
276
+ if (ctx.boolean_expression_clause() != null) {
277
+ return this.visitBoolean_expression_clause(ctx.boolean_expression_clause());
278
+ }
279
+
280
+ if (ctx.LPAREN() != null) {
281
+ return {
282
+ expType: "expBooleanParen",
283
+ var1: this.visitBoolean_expression(ctx.boolean_expression(0))
284
+ };
285
+ }
286
+
287
+ if (ctx.NOT() != null) {
288
+ return {
289
+ expType: "expBooleanOpLogic",
290
+ var1: this.visitBoolean_expression(ctx.boolean_expression(0)),
291
+ op: ctx.NOT().getText()
292
+ };
293
+ } else if (ctx.AND() != null) {
294
+ return {
295
+ expType: "expBooleanOpLogic",
296
+ var1: this.visitBoolean_expression(ctx.boolean_expression(0)),
297
+ var2: this.visitBoolean_expression(ctx.boolean_expression(1)),
298
+ op: ctx.AND().getText()
299
+ };
300
+ } else if (ctx.OR() != null) {
301
+ return {
302
+ expType: "expBooleanOpLogic",
303
+ var1: this.visitBoolean_expression(ctx.boolean_expression(0)),
304
+ var2: this.visitBoolean_expression(ctx.boolean_expression(1)),
305
+ op: ctx.OR().getText()
306
+ };
307
+ }
308
+
309
+ return super.visitBoolean_expression(ctx);
310
+ }
311
+
312
+ visitBoolean_expression_clause(ctx) {
313
+ let result = null;
314
+ if (ctx.boolean_atom() != null) {
315
+ result = this.visitExpBooleanSingle(ctx);
316
+ } else if (ctx.boolean_op_collection() != null) {
317
+ result = this.visitExpBooleanOpCollection(ctx);
318
+ } else if (ctx.boolean_op_compare() != null) {
319
+ result = this.visitExpBooleanOpCompare(ctx);
320
+ } else {
321
+ result = super.visitBoolean_expression_clause(ctx);
322
+ }
323
+
324
+ return result;
325
+ }
326
+
327
+ visitExpBooleanSingle(ctx) {
328
+ return this.visitBoolean_atom(ctx.boolean_atom());
329
+ }
330
+
331
+ visitExpBooleanOpCollection(ctx) {
332
+ return {
333
+ expType: "expBooleanOpCollection",
334
+ var1: this.visitBoolean_atom_or_expression(ctx.boolean_atom_or_expression()),
335
+ var2: super.visitExpression_collection_clause(ctx.expression_collection_clause()),
336
+ op: ctx.boolean_op_collection().getText()
337
+ };
338
+ }
339
+
340
+ visitExpBooleanOpCompare(ctx) {
341
+ return {
342
+ expType: "expBooleanOpCompare",
343
+ var1: this.visitBoolean_atom_or_expression(ctx.boolean_atom_or_expression(0)),
344
+ var2: this.visitBoolean_atom_or_expression(ctx.boolean_atom_or_expression(1)),
345
+ op: ctx.boolean_op_compare().getText()
346
+ };
347
+ }
348
+
349
+ visitBoolean_atom(ctx) {
350
+ if (ctx.boolean_constant() != null) {
351
+ return this.visitBoolean_constant(ctx.boolean_constant());
352
+ }
353
+ if (ctx.function_call() != null) {
354
+ return this.visitFunction_call(ctx.function_call());
355
+ }
356
+ if (ctx.variable_chain() != null) {
357
+ return this.visitVariable_chain(ctx.variable_chain());
358
+ }
359
+ return super.visitBoolean_atom(ctx);
360
+ }
361
+
362
+ visitBoolean_constant(ctx) {
363
+ return {expType: "boolean", value: ctx.getText()};
364
+ }
365
+
366
+ visitCondition_expression(ctx) {
367
+ return {
368
+ expType: "expCondition",
369
+ paramList: this.visitCondition_expression_param_list(ctx.condition_expression_param_list())
370
+ };
371
+ }
372
+
373
+ visitBoolean_atom_or_expression(ctx) {
374
+ if (ctx.boolean_atom() != null) {
375
+ return this.visitBoolean_atom(ctx.boolean_atom());
376
+ }
377
+ if (ctx.expression() != null) {
378
+ return this.visitExpression(ctx.expression());
379
+ }
380
+ }
381
+
382
+ visitCondition_expression_param_list(ctx) {
383
+ let result = [];
384
+ if (ctx != null) {
385
+ ctx.condition_expression_param().forEach(param => {
386
+ result.push(this.visitCondition_expression_param(param));
387
+ });
388
+ }
389
+
390
+ return result;
391
+ }
392
+
393
+ visitCondition_expression_param(ctx) {
394
+ let result = {
395
+ paramValue: ctx.condition_expression_param_value().getText(),
396
+ paramValueObj: this.visitCondition_expression_param_value(ctx.condition_expression_param_value())
397
+ };
398
+ if (ctx.condition_expression_param_condition() != null) {
399
+ result["paramCondition"] = ctx.condition_expression_param_condition().getText();
400
+ result["paramConditionObj"] = this.visitCondition_expression_param_condition(ctx.condition_expression_param_condition());
401
+ }
402
+
403
+ return result;
404
+ }
405
+
406
+ visitCondition_expression_param_condition(ctx) {
407
+ return this.visitBoolean_expression(ctx.boolean_expression());
408
+ }
409
+
410
+ visitCondition_expression_param_value(ctx) {
411
+ if (ctx.expression() != null) {
412
+ return this.visitExpression(ctx.expression());
413
+ }
414
+ if (ctx.boolean_expression() != null) {
415
+ return this.visitBoolean_expression(ctx.boolean_expression());
416
+ }
417
+ if (ctx.args_group_expression() != null) {
418
+ return this.visitArgs_group_expression(ctx.args_group_expression());
419
+ }
420
+ if (ctx.collection_expression() != null) {
421
+ return this.visitCollection_expression(ctx.collection_expression());
422
+ }
423
+
424
+ return super.visitCondition_expression_param_value(ctx);
425
+ }
426
+
427
+ visitArgs_group_expression(ctx) {
428
+ return {
429
+ expType: "expArgsGroup",
430
+ paramList: this.visitArgs_group_param_list(ctx.args_group_param_list())
431
+ };
432
+ }
433
+
434
+ visitArgs_group_param_list(ctx) {
435
+ let paramList = [];
436
+ ctx.args_group_param().forEach(param => {
437
+ paramList.push({
438
+ paramName: param.args_group_param_name().getText().substring(1, param.args_group_param_name().getText().length - 1),
439
+ paramValue: param.args_group_param_value().getText(),
440
+ paramValueObj: this.visitArgs_group_param_value(param.args_group_param_value())
441
+ });
442
+ });
443
+
444
+ return paramList;
445
+ }
446
+
447
+ visitArgs_group_param_value(ctx) {
448
+ if (ctx.expression() != null) {
449
+ return this.visitExpression(ctx.expression());
450
+ }
451
+ if (ctx.boolean_expression() != null) {
452
+ return this.visitBoolean_expression(ctx.boolean_expression());
453
+ }
454
+ if (ctx.condition_expression() != null) {
455
+ return this.visitCondition_expression(ctx.condition_expression());
456
+ }
457
+ if (ctx.collection_expression() != null) {
458
+ return this.visitCollection_expression(ctx.collection_expression());
459
+ }
460
+ return super.visitArgs_group_param_value(ctx);
461
+ }
462
+
463
+ visitCollection_expression(ctx) {
464
+ let result = this.visitCollection_expression_content(ctx.collection_expression_content());
465
+ result["expType"] = "expCollection";
466
+
467
+ return result;
468
+ }
469
+
470
+ visitCollection_expression_content(ctx) {
471
+ return {
472
+ lSymbol: ctx.lSymbol.text,
473
+ rSymbol: ctx.rSymbol.text,
474
+ var1: this.visitExpression(ctx.expression(0)),
475
+ var2: this.visitExpression(ctx.expression(1))
476
+ };
477
+ }
478
+
479
+ visitExpression_param_list(ctx) {
480
+ let paramList = [];
481
+ ctx.expression_param().forEach(expParam => {
482
+ let paramElem = {
483
+ paramValue: expParam.assignment_clause().getText(),
484
+ paramValueObj: this.visitAssignment_clause(expParam.assignment_clause())
485
+ };
486
+ if (expParam.expression_param_name() != null) {
487
+ paramElem["paramName"] = expParam.expression_param_name().getText();
488
+ }
489
+ paramList.push(paramElem);
490
+ });
491
+
492
+ return paramList;
493
+ }
494
+
495
+ visitFunction_call(ctx) {
496
+ let result = this.visitBuilt_in_function(ctx.built_in_function());
497
+ result["expType"] = "functionCall";
498
+ if (ctx.variable_chain() != null) {
499
+ result["varChain"] = ctx.variable_chain().getText();
500
+ }
501
+
502
+ return result;
503
+ }
504
+
505
+ visitBuilt_in_function(ctx) {
506
+ let result = {funcName: ctx.funcname().getText()};
507
+
508
+ if (ctx.expression_param_list() != null) {
509
+ result["paramList"] = this.visitExpression_param_list(ctx.expression_param_list());
510
+ }
511
+
512
+ return result;
513
+ }
514
+
515
+ visitScientific(ctx) {
516
+ return {expType: "number", value: ctx.getText()};
517
+ }
518
+
519
+ visitAtom_string(ctx) {
520
+ return {expType: "string", value: ctx.getText().substring(1, ctx.getText().length - 1)};
521
+ }
522
+
523
+ visitEntity_variable(ctx) {
524
+ return {
525
+ expType: "entity",
526
+ entityType: ctx.ID().getText(),
527
+ entityName: ctx.ENTITY_NAME().getText().substring(1, ctx.ENTITY_NAME().getText().length - 1)
528
+ };
529
+ }
530
+
531
+ visitVariable_chain(ctx) {
532
+ let varType = "normal";
533
+ if (ctx.variable().environment_context() != null) {
534
+ varType = "context";
535
+ }
536
+ if (ctx.variable().environment_event() != null) {
537
+ varType = "event";
538
+ }
539
+
540
+ return {expType: "variableChain", varType: varType, content: ctx.getText()};
541
+ }
542
+ }
543
+
544
+ exports.CollectionPolicyCustomVisitor = CollectionPolicyCustomVisitor;
@@ -0,0 +1,131 @@
1
+ class CollectionPolicyDecompiler {
2
+
3
+ decompile(result) {
4
+ let content = "";
5
+ content += `\n${this.decompileAudiences(result["audiences"])}`;
6
+ content += `\n${this.decompileServiceStates(result["serviceStates"])}`;
7
+ content += `\n${this.decompileStateMachine(result["stateMachine"])}`;
8
+ return content;
9
+ }
10
+
11
+ decompileAudiences(audiences) {
12
+ return `for ${audiences.map(audience => audience.name).join(", ")}`;
13
+ }
14
+
15
+ decompileServiceStates(serviceStates) {
16
+ let content = "";
17
+ serviceStates.forEach(serviceState => {
18
+ content += `\n${this.decompileServiceState(serviceState)}`;
19
+ });
20
+
21
+ return content;
22
+ }
23
+
24
+ decompileServiceState(serviceState) {
25
+ if (serviceState["caller"] == null) {
26
+ return serviceState["name"];
27
+ } else {
28
+ if (serviceState["functions"] == null || serviceState["functions"].length === 0) {
29
+ return `${serviceState["name"]} = ${serviceState["caller"]["name"]}`;
30
+ } else {
31
+ return `${serviceState["name"]} = ${serviceState["caller"]["name"]}.${this.decompileFunctions(serviceState["functions"])}`;
32
+ }
33
+ }
34
+ }
35
+
36
+ decompileStateMachine(stateMachine) {
37
+ let content = "";
38
+ for (let state in stateMachine) {
39
+ content += `\n${this.decompileMachineState(stateMachine[state], state)}`;
40
+ }
41
+
42
+ return content;
43
+ }
44
+
45
+ decompileMachineState(machineState, state) {
46
+ let serviceStates = machineState["serviceStates"];
47
+ let serviceStatesStr = "";
48
+ if (serviceStates != null && serviceStates.length !== 0) {
49
+ serviceStatesStr = `[${serviceStates.join(", ")}]`
50
+ }
51
+
52
+ return `${state}${serviceStatesStr}:${this.decompileTransitions(machineState["transitions"])}`;
53
+ }
54
+
55
+ decompileTransitions(transitions) {
56
+ if (transitions === null || transitions.length === 0) {
57
+ return "\n\tterminate";
58
+ }
59
+
60
+ let content = "";
61
+ transitions.forEach(transition => {
62
+ content += `\n${this.decompileTransition(transition)}`;
63
+ });
64
+
65
+ return content;
66
+ }
67
+
68
+ decompileTransition(transition) {
69
+ let condition = transition["condition"];
70
+ let conditionStr = "";
71
+ if (condition != null) {
72
+ switch (condition["type"]) {
73
+ case "case":
74
+ conditionStr = `case in ${condition["serviceState"]}`;
75
+ break;
76
+ }
77
+ }
78
+
79
+ let path = transition["path"];
80
+ let pathStr = path != null ? `${path}.` : "";
81
+ let args = transition["args"];
82
+ let argsStr = "";
83
+ if (args != null) {
84
+ let argArray = [];
85
+ for (let key in args) {
86
+ argArray.push(`"${args[key]}"`);
87
+ }
88
+ argsStr = argArray.join(", ");
89
+ }
90
+ let eventStr = `~${transition["service"]}.${pathStr}${transition["name"]}(${argsStr}) => ${transition["toState"]}`;
91
+
92
+ let actions = transition["actions"];
93
+ let actionsStr = "";
94
+ if (actions != null && actions.length !== 0) {
95
+ actions.forEach(action => {
96
+ let caller = action["caller"];
97
+ let callerStr = "";
98
+ if (caller != null) {
99
+ callerStr = `${caller["name"]}.`;
100
+ }
101
+ actionsStr += `${callerStr}${this.decompileFunctions(action["functions"])}; `;
102
+ })
103
+ actionsStr = `do ${actionsStr}`;
104
+ }
105
+
106
+ return `\t${conditionStr} ${eventStr} ${actionsStr}`;
107
+ }
108
+
109
+ decompileFunctions(functions) {
110
+ if (functions == null || functions.length === 0) {
111
+ return "";
112
+ } else {
113
+ return functions.map(this.decompileFunction).join(".");
114
+ }
115
+ }
116
+
117
+ decompileFunction(func) {
118
+ if (func["functionArgs"] == null || func["functionArgs"].length === 0) {
119
+ return `${func["functionName"]}()`;
120
+ } else {
121
+ let functionArgs = func["functionArgs"];
122
+ let functionArgArray = [];
123
+ for (let name in functionArgs) {
124
+ functionArgArray.push(functionArgs[name]);
125
+ }
126
+ return `${func["functionName"]}(${functionArgArray.join(", ")})`;
127
+ }
128
+ }
129
+ }
130
+
131
+ exports.CollectionPolicyDecompiler = CollectionPolicyDecompiler;
@@ -0,0 +1,23 @@
1
+ const antlr4 = require("antlr4");
2
+
3
+ class CollectionPolicyErrorLexerListener extends antlr4.error.ErrorListener {
4
+
5
+ constructor() {
6
+ super();
7
+ this.errors = [];
8
+ this.errorObjects = [];
9
+ }
10
+
11
+ syntaxError(recognizer, offendingSymbol, line, charPositionInLine, msg, e) {
12
+ if (e instanceof antlr4.error.LexerNoViableAltException) {
13
+ this.errors.push(`line ${line}:${charPositionInLine} ${msg}`);
14
+ this.errorObjects.push({
15
+ line: line,
16
+ charPositionInLine: charPositionInLine,
17
+ msg: msg
18
+ });
19
+ }
20
+ }
21
+ }
22
+
23
+ exports.CollectionPolicyErrorLexerListener = CollectionPolicyErrorLexerListener;