@fincity/kirun-js 1.3.0 → 1.4.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.
Files changed (40) hide show
  1. package/__tests__/engine/function/system/math/RandomIntTest.ts +0 -1
  2. package/__tests__/engine/json/schema/validator/SchemaValidatorTest.ts +51 -1
  3. package/__tests__/engine/runtime/KIRuntimeTest.ts +75 -26
  4. package/__tests__/engine/runtime/KIRuntimeWithDefinitionTest.ts +47 -9
  5. package/__tests__/engine/runtime/expression/ExpressionEvaluatorTernaryOperatorTest.ts +42 -0
  6. package/dist/index.js +1 -1
  7. package/dist/index.js.map +1 -1
  8. package/dist/module.js +1 -1
  9. package/dist/module.js.map +1 -1
  10. package/dist/types.d.ts +44 -26
  11. package/dist/types.d.ts.map +1 -1
  12. package/package.json +1 -1
  13. package/src/engine/function/AbstractFunction.ts +12 -5
  14. package/src/engine/function/system/GenerateEvent.ts +1 -2
  15. package/src/engine/function/system/string/Reverse.ts +1 -1
  16. package/src/engine/json/schema/Schema.ts +119 -2
  17. package/src/engine/json/schema/SchemaUtil.ts +1 -1
  18. package/src/engine/json/schema/array/ArraySchemaType.ts +6 -0
  19. package/src/engine/json/schema/type/MultipleType.ts +5 -2
  20. package/src/engine/json/schema/type/SingleType.ts +4 -2
  21. package/src/engine/json/schema/validator/ObjectValidator.ts +2 -3
  22. package/src/engine/model/AbstractStatement.ts +10 -0
  23. package/src/engine/model/Event.ts +12 -5
  24. package/src/engine/model/FunctionDefinition.ts +1 -2
  25. package/src/engine/model/FunctionSignature.ts +14 -4
  26. package/src/engine/model/Parameter.ts +13 -3
  27. package/src/engine/model/ParameterReference.ts +38 -11
  28. package/src/engine/model/Position.ts +1 -1
  29. package/src/engine/model/Statement.ts +58 -20
  30. package/src/engine/model/StatementGroup.ts +22 -2
  31. package/src/engine/runtime/KIRuntime.ts +7 -6
  32. package/src/engine/runtime/expression/Expression.ts +120 -0
  33. package/src/engine/runtime/expression/ExpressionEvaluator.ts +26 -0
  34. package/src/engine/runtime/expression/Operation.ts +10 -0
  35. package/src/engine/runtime/expression/operators/ternary/ConditionalTernaryOperator.ts +7 -0
  36. package/src/engine/runtime/expression/operators/ternary/TernaryOperator.ts +15 -0
  37. package/src/engine/runtime/expression/operators/ternary/index.ts +1 -0
  38. package/src/engine/util/MapUtil.ts +10 -0
  39. package/src/index.ts +1 -1
  40. package/src/engine/json/schema/object/AdditionalPropertiesType.ts +0 -32
@@ -120,7 +120,7 @@ export class SchemaUtil {
120
120
  ): Tuple2<Schema, string> | undefined {
121
121
  if (!sRepository) return undefined;
122
122
 
123
- let nms = StringUtil.splitAtFirstOccurance(inSchem?.getRef() ?? '', '/');
123
+ let nms = StringUtil.splitAtFirstOccurance(ref ?? '', '/');
124
124
  if (!nms[0]) return undefined;
125
125
 
126
126
  let nmspnm = StringUtil.splitAtFirstOccurance(nms[0], '.');
@@ -5,6 +5,12 @@ export class ArraySchemaType {
5
5
  private singleSchema: Schema | undefined;
6
6
  private tupleSchema: Schema[] | undefined;
7
7
 
8
+ public constructor(ast?: ArraySchemaType) {
9
+ if (!ast) return;
10
+ this.singleSchema = ast.singleSchema ? new Schema(ast.singleSchema) : undefined;
11
+ this.tupleSchema = ast.tupleSchema ? ast.tupleSchema.map((e) => new Schema(e)) : undefined;
12
+ }
13
+
8
14
  public setSingleSchema(schema: Schema): ArraySchemaType {
9
15
  this.singleSchema = schema;
10
16
  return this;
@@ -4,9 +4,12 @@ import { Type } from './Type';
4
4
  export class MultipleType extends Type {
5
5
  private type: Set<SchemaType>;
6
6
 
7
- constructor(type: Set<SchemaType>) {
7
+ public constructor(type: Set<SchemaType> | MultipleType) {
8
8
  super();
9
- this.type = type;
9
+
10
+ if (type instanceof MultipleType)
11
+ this.type = new Set(Array.from((type as MultipleType).type));
12
+ else this.type = new Set(Array.from(type));
10
13
  }
11
14
 
12
15
  public getType(): Set<SchemaType> {
@@ -4,9 +4,11 @@ import { Type } from './Type';
4
4
  export class SingleType extends Type {
5
5
  private type: SchemaType;
6
6
 
7
- constructor(type: SchemaType) {
7
+ public constructor(type: SchemaType | SingleType) {
8
8
  super();
9
- this.type = type;
9
+
10
+ if (type instanceof SingleType) this.type = (type as SingleType).type;
11
+ else this.type = type as SchemaType;
10
12
  }
11
13
 
12
14
  public getType(): SchemaType {
@@ -1,7 +1,6 @@
1
1
  import { Repository } from '../../../Repository';
2
2
  import { isNullValue } from '../../../util/NullCheck';
3
- import { AdditionalPropertiesType } from '../object/AdditionalPropertiesType';
4
- import { Schema } from '../Schema';
3
+ import { AdditionalPropertiesType, Schema } from '../Schema';
5
4
  import { SchemaValidationException } from './exception/SchemaValidationException';
6
5
  import { SchemaValidator } from './SchemaValidator';
7
6
 
@@ -100,7 +99,7 @@ export class ObjectValidator {
100
99
  newParents,
101
100
  apt.getSchemaValue(),
102
101
  repository,
103
- jsonObject.get(key),
102
+ jsonObject[key],
104
103
  );
105
104
  jsonObject[key] = element;
106
105
  }
@@ -6,6 +6,16 @@ export class AbstractStatement {
6
6
  private position?: Position;
7
7
  private override: boolean = false;
8
8
 
9
+ public constructor(ast?: AbstractStatement) {
10
+ if (!ast) return;
11
+ this.comment = ast.comment;
12
+ this.description = ast.description;
13
+ this.position = ast.position
14
+ ? new Position(ast.position.getLeft(), ast.position.getTop())
15
+ : undefined;
16
+ this.override = ast.override;
17
+ }
18
+
9
19
  public getComment(): string | undefined {
10
20
  return this.comment;
11
21
  }
@@ -1,5 +1,4 @@
1
- import { AdditionalPropertiesType } from '../json/schema/object/AdditionalPropertiesType';
2
- import { Schema } from '../json/schema/Schema';
1
+ import { AdditionalPropertiesType, Schema } from '../json/schema/Schema';
3
2
  import { SchemaType } from '../json/schema/type/SchemaType';
4
3
  import { TypeUtil } from '../json/schema/type/TypeUtil';
5
4
  import { SchemaReferenceException } from '../json/schema/validator/exception/SchemaReferenceException';
@@ -30,9 +29,17 @@ export class Event {
30
29
  private name: string;
31
30
  private parameters: Map<string, Schema>;
32
31
 
33
- constructor(name: string, parameters: Map<string, Schema>) {
34
- this.name = name;
35
- this.parameters = parameters;
32
+ constructor(evn: string | Event, parameters?: Map<string, Schema>) {
33
+ if (evn instanceof Event) {
34
+ this.name = evn.name;
35
+ this.parameters = new Map(
36
+ Array.from(evn.parameters.entries()).map((e) => [e[0], new Schema(e[1])]),
37
+ );
38
+ } else {
39
+ this.name = evn;
40
+ if (!parameters) throw new Error('Unknown constructor format');
41
+ this.parameters = parameters;
42
+ }
36
43
  }
37
44
 
38
45
  public getName(): string {
@@ -1,5 +1,4 @@
1
- import { AdditionalPropertiesType } from '../json/schema/object/AdditionalPropertiesType';
2
- import { Schema } from '../json/schema/Schema';
1
+ import { AdditionalPropertiesType, Schema } from '../json/schema/Schema';
3
2
  import { Namespaces } from '../namespaces/Namespaces';
4
3
  import { Event } from './Event';
5
4
  import { FunctionSignature } from './FunctionSignature';
@@ -1,5 +1,4 @@
1
- import { AdditionalPropertiesType } from '../json/schema/object/AdditionalPropertiesType';
2
- import { Schema } from '../json/schema/Schema';
1
+ import { AdditionalPropertiesType, Schema } from '../json/schema/Schema';
3
2
  import { Namespaces } from '../namespaces/Namespaces';
4
3
  import { Event } from './Event';
5
4
  import { Parameter } from './Parameter';
@@ -32,8 +31,19 @@ export class FunctionSignature {
32
31
  private parameters: Map<string, Parameter> = new Map();
33
32
  private events: Map<string, Event> = new Map();
34
33
 
35
- constructor(name: string) {
36
- this.name = name;
34
+ constructor(value: string | FunctionSignature) {
35
+ if (value instanceof FunctionSignature) {
36
+ this.name = value.name;
37
+ this.namespace = value.namespace;
38
+ this.parameters = new Map(
39
+ Array.from(value.parameters.entries()).map((e) => [e[0], new Parameter(e[1])]),
40
+ );
41
+ this.events = new Map(
42
+ Array.from(value.events.entries()).map((e) => [e[0], new Event(e[1])]),
43
+ );
44
+ } else {
45
+ this.name = value;
46
+ }
37
47
  }
38
48
 
39
49
  public getNamespace(): string {
@@ -43,9 +43,19 @@ export class Parameter {
43
43
  private variableArgument: boolean = false;
44
44
  private type: ParameterType = ParameterType.EXPRESSION;
45
45
 
46
- constructor(parameterName: string, schema: Schema) {
47
- this.schema = schema;
48
- this.parameterName = parameterName;
46
+ constructor(pn: string | Parameter, schema?: Schema) {
47
+ if (pn instanceof Parameter) {
48
+ this.schema = new Schema(pn.schema);
49
+ this.parameterName = pn.parameterName;
50
+ this.variableArgument = pn.variableArgument;
51
+ this.type = pn.type;
52
+ } else {
53
+ if (!schema) {
54
+ throw new Error('Unknown constructor signature');
55
+ }
56
+ this.schema = schema;
57
+ this.parameterName = pn;
58
+ }
49
59
  }
50
60
 
51
61
  public getSchema(): Schema {
@@ -2,6 +2,8 @@ import { Schema } from '../json/schema/Schema';
2
2
  import { SchemaType } from '../json/schema/type/SchemaType';
3
3
  import { TypeUtil } from '../json/schema/type/TypeUtil';
4
4
  import { Namespaces } from '../namespaces/Namespaces';
5
+ import { isNullValue } from '../util/NullCheck';
6
+ import UUID from '../util/UUID';
5
7
  import { ParameterReferenceType } from './ParameterReferenceType';
6
8
 
7
9
  export class ParameterReference {
@@ -12,17 +14,29 @@ export class ParameterReference {
12
14
  .setType(TypeUtil.of(SchemaType.OBJECT))
13
15
  .setProperties(
14
16
  new Map([
17
+ ['key', Schema.ofString('key')],
15
18
  ['value', Schema.ofAny('value')],
16
19
  ['expression', Schema.ofString('expression')],
17
20
  ['type', Schema.ofString('type').setEnums(['EXPRESSION', 'VALUE'])],
18
21
  ]),
19
22
  );
23
+
24
+ private key: string;
20
25
  private type: ParameterReferenceType;
21
26
  private value: any;
22
27
  private expression?: string;
23
28
 
24
- constructor(type: ParameterReferenceType) {
25
- this.type = type;
29
+ constructor(type: ParameterReferenceType | ParameterReference) {
30
+ if (type instanceof ParameterReference) {
31
+ let pv = type as ParameterReference;
32
+ this.key = pv.key;
33
+ this.type = pv.type;
34
+ this.value = isNullValue(pv.value) ? undefined : JSON.parse(JSON.stringify(pv.value));
35
+ this.expression = pv.expression;
36
+ } else {
37
+ this.type = type as ParameterReferenceType;
38
+ this.key = UUID();
39
+ }
26
40
  }
27
41
 
28
42
  public getType(): ParameterReferenceType {
@@ -32,6 +46,15 @@ export class ParameterReference {
32
46
  this.type = type;
33
47
  return this;
34
48
  }
49
+
50
+ public getKey(): string {
51
+ return this.key;
52
+ }
53
+ public setKey(key: string): ParameterReference {
54
+ this.key = key;
55
+ return this;
56
+ }
57
+
35
58
  public getValue(): any {
36
59
  return this.value;
37
60
  }
@@ -47,18 +70,22 @@ export class ParameterReference {
47
70
  return this;
48
71
  }
49
72
 
50
- public static ofExpression(value: any): ParameterReference {
51
- return new ParameterReference(ParameterReferenceType.EXPRESSION).setExpression(value);
73
+ public static ofExpression(value: any): [string, ParameterReference] {
74
+ const param = new ParameterReference(ParameterReferenceType.EXPRESSION).setExpression(
75
+ value,
76
+ );
77
+ return [param.getKey(), param];
52
78
  }
53
79
 
54
- public static ofValue(value: any): ParameterReference {
55
- return new ParameterReference(ParameterReferenceType.VALUE).setValue(value);
80
+ public static ofValue(value: any): [string, ParameterReference] {
81
+ const param = new ParameterReference(ParameterReferenceType.VALUE).setValue(value);
82
+ return [param.getKey(), param];
56
83
  }
57
84
 
58
- public static from(json: any): ParameterReference[] {
59
- if (!json) return [];
60
- return Array.from(json).map((e: any) =>
61
- new ParameterReference(e.type).setValue(e.value).setExpression(e.expression),
62
- );
85
+ public static from(e: any): ParameterReference {
86
+ return new ParameterReference(e.type)
87
+ .setValue(e.value)
88
+ .setExpression(e.expression)
89
+ .setKey(e.key);
63
90
  }
64
91
  }
@@ -18,7 +18,7 @@ export class Position {
18
18
  private left: number;
19
19
  private top: number;
20
20
 
21
- constructor(left: number, top: number) {
21
+ public constructor(left: number, top: number) {
22
22
  this.left = left;
23
23
  this.top = top;
24
24
  }
@@ -1,5 +1,4 @@
1
- import { AdditionalPropertiesType } from '../json/schema/object/AdditionalPropertiesType';
2
- import { Schema } from '../json/schema/Schema';
1
+ import { AdditionalPropertiesType, Schema } from '../json/schema/Schema';
3
2
  import { SchemaType } from '../json/schema/type/SchemaType';
4
3
  import { TypeUtil } from '../json/schema/type/TypeUtil';
5
4
  import { Namespaces } from '../namespaces/Namespaces';
@@ -22,7 +21,9 @@ export class Statement extends AbstractStatement {
22
21
  ['name', Schema.ofString('name')],
23
22
  [
24
23
  'dependentStatements',
25
- Schema.ofArray('dependentstatement', Schema.ofString('dependentstatement')),
24
+ Schema.ofObject('dependentstatement').setAdditionalProperties(
25
+ new AdditionalPropertiesType().setSchemaValue(Schema.ofBoolean('exists')),
26
+ ),
26
27
  ],
27
28
  [
28
29
  'parameterMap',
@@ -30,7 +31,11 @@ export class Statement extends AbstractStatement {
30
31
  .setName('parameterMap')
31
32
  .setAdditionalProperties(
32
33
  new AdditionalPropertiesType().setSchemaValue(
33
- Schema.ofArray('parameterReference', ParameterReference.SCHEMA),
34
+ Schema.ofObject('parameterReference').setAdditionalProperties(
35
+ new AdditionalPropertiesType().setSchemaValue(
36
+ ParameterReference.SCHEMA,
37
+ ),
38
+ ),
34
39
  ),
35
40
  ),
36
41
  ],
@@ -40,14 +45,41 @@ export class Statement extends AbstractStatement {
40
45
  private statementName: string;
41
46
  private namespace: string;
42
47
  private name: string;
43
- private parameterMap?: Map<string, ParameterReference[]>;
44
- private dependentStatements?: string[];
48
+ private parameterMap?: Map<string, Map<string, ParameterReference>>;
49
+ private dependentStatements?: Map<string, boolean>;
45
50
 
46
- public constructor(statementName: string, namespace: string, name: string) {
47
- super();
48
- this.statementName = statementName;
49
- this.namespace = namespace;
50
- this.name = name;
51
+ public constructor(sn: string | Statement, namespace?: string, name?: string) {
52
+ super(sn instanceof Statement ? (sn as Statement) : undefined);
53
+
54
+ if (sn instanceof Statement) {
55
+ let x = sn as Statement;
56
+ this.statementName = x.statementName;
57
+ this.name = x.name;
58
+ this.namespace = x.namespace;
59
+ if (x.parameterMap)
60
+ this.parameterMap = new Map(
61
+ Array.from(x.parameterMap.entries()).map((f) => [
62
+ f[0],
63
+ new Map(
64
+ Array.from(f[1].entries()).map((e) => [
65
+ e[0],
66
+ new ParameterReference(e[1]),
67
+ ]),
68
+ ),
69
+ ]),
70
+ );
71
+
72
+ if (x.dependentStatements) {
73
+ this.dependentStatements = new Map(Array.from(x.dependentStatements.entries()));
74
+ }
75
+ } else {
76
+ this.statementName = sn;
77
+ if (!name || !namespace) {
78
+ throw new Error('Unknown constructor');
79
+ }
80
+ this.namespace = namespace;
81
+ this.name = name;
82
+ }
51
83
  }
52
84
 
53
85
  public getStatementName(): string {
@@ -71,20 +103,20 @@ export class Statement extends AbstractStatement {
71
103
  this.name = name;
72
104
  return this;
73
105
  }
74
- public getParameterMap(): Map<string, ParameterReference[]> {
106
+ public getParameterMap(): Map<string, Map<string, ParameterReference>> {
75
107
  if (!this.parameterMap) {
76
108
  this.parameterMap = new Map();
77
109
  }
78
110
  return this.parameterMap;
79
111
  }
80
- public setParameterMap(parameterMap: Map<string, ParameterReference[]>): Statement {
112
+ public setParameterMap(parameterMap: Map<string, Map<string, ParameterReference>>): Statement {
81
113
  this.parameterMap = parameterMap;
82
114
  return this;
83
115
  }
84
- public getDependentStatements(): string[] {
85
- return this.dependentStatements ?? [];
116
+ public getDependentStatements(): Map<string, boolean> {
117
+ return this.dependentStatements ?? new Map();
86
118
  }
87
- public setDependentStatements(dependentStatements: string[]): Statement {
119
+ public setDependentStatements(dependentStatements: Map<string, boolean>): Statement {
88
120
  this.dependentStatements = dependentStatements;
89
121
  return this;
90
122
  }
@@ -102,14 +134,20 @@ export class Statement extends AbstractStatement {
102
134
  public static from(json: any): Statement {
103
135
  return new Statement(json.statementName, json.namespace, json.name)
104
136
  .setParameterMap(
105
- new Map<string, ParameterReference[]>(
106
- Object.entries(json.parameterMap ?? {}).map(([k, v]) => [
137
+ new Map<string, Map<string, ParameterReference>>(
138
+ Object.entries(json.parameterMap ?? {}).map(([k, v]: [string, any]) => [
107
139
  k,
108
- ParameterReference.from(v),
140
+ new Map<string, ParameterReference>(
141
+ Object.entries(v ?? {})
142
+ .map(([_, iv]) => ParameterReference.from(iv))
143
+ .map((e) => [e.getKey(), e]),
144
+ ),
109
145
  ]),
110
146
  ),
111
147
  )
112
- .setDependentStatements(json.dependentStatements)
148
+ .setDependentStatements(
149
+ new Map<string, boolean>(Object.entries(json.dependentStatements ?? {})),
150
+ )
113
151
  .setPosition(Position.from(json.position))
114
152
  .setComment(json.comment)
115
153
  .setDescription(json.description) as Statement;
@@ -4,6 +4,7 @@ import { TypeUtil } from '../json/schema/type/TypeUtil';
4
4
  import { Namespaces } from '../namespaces/Namespaces';
5
5
  import { AbstractStatement } from './AbstractStatement';
6
6
  import { Position } from './Position';
7
+ import { Statement } from './Statement';
7
8
 
8
9
  export class StatementGroup extends AbstractStatement {
9
10
  private static readonly SCHEMA_NAME: string = 'StatementGroup';
@@ -21,10 +22,12 @@ export class StatementGroup extends AbstractStatement {
21
22
  );
22
23
 
23
24
  private statementGroupName: string;
25
+ private statements: Map<string, boolean>;
24
26
 
25
- constructor(statementGroupName: string) {
27
+ constructor(statementGroupName: string, statements: Map<string, boolean> = new Map()) {
26
28
  super();
27
29
  this.statementGroupName = statementGroupName;
30
+ this.statements = statements;
28
31
  }
29
32
 
30
33
  public getStatementGroupName(): string {
@@ -35,8 +38,25 @@ export class StatementGroup extends AbstractStatement {
35
38
  return this;
36
39
  }
37
40
 
41
+ public getStatements(): Map<string, boolean> {
42
+ return this.statements;
43
+ }
44
+
45
+ public setStatements(statements: Map<string, boolean>): StatementGroup {
46
+ this.statements = statements;
47
+ return this;
48
+ }
49
+
38
50
  public static from(json: any): StatementGroup {
39
- return new StatementGroup(json.statementGroupName)
51
+ return new StatementGroup(
52
+ json.statementGroupName,
53
+ new Map(
54
+ Object.entries(json.statements || {}).map(([k, v]) => [
55
+ k,
56
+ ('' + v)?.toLowerCase() == 'true',
57
+ ]),
58
+ ),
59
+ )
40
60
  .setPosition(Position.from(json.position))
41
61
  .setComment(json.comment)
42
62
  .setDescription(json.description) as StatementGroup;
@@ -417,7 +417,7 @@ export class KIRuntime extends AbstractFunction {
417
417
  ): Map<string, any> {
418
418
  return Array.from(s.getParameterMap().entries())
419
419
  .map((e) => {
420
- let prList: ParameterReference[] = e[1];
420
+ let prList: ParameterReference[] = Array.from(e[1]?.values() ?? []);
421
421
 
422
422
  let ret: any = undefined;
423
423
 
@@ -483,9 +483,9 @@ export class KIRuntime extends AbstractFunction {
483
483
  let p: Parameter | undefined = paramSet.get(param[0]);
484
484
  if (!p) continue;
485
485
 
486
- let refList: ParameterReference[] = param[1];
486
+ let refList: ParameterReference[] = Array.from(param[1]?.values() ?? []);
487
487
 
488
- if (!refList.length) {
488
+ if (!refList.length && !p.isVariableArgument()) {
489
489
  if (isNullValue(SchemaUtil.getDefaultValue(p.getSchema(), sRepo)))
490
490
  se.addMessage(
491
491
  StatementMessageType.ERROR,
@@ -500,7 +500,7 @@ export class KIRuntime extends AbstractFunction {
500
500
 
501
501
  if (p.isVariableArgument()) {
502
502
  for (let ref of refList) this.parameterReferenceValidation(se, p, ref, sRepo);
503
- } else {
503
+ } else if (refList.length) {
504
504
  let ref: ParameterReference = refList[0];
505
505
  this.parameterReferenceValidation(se, p, ref, sRepo);
506
506
  }
@@ -509,12 +509,13 @@ export class KIRuntime extends AbstractFunction {
509
509
  }
510
510
 
511
511
  if (!isNullValue(se.getStatement().getDependentStatements())) {
512
- for (let statement of se.getStatement().getDependentStatements())
513
- se.addDependency(statement);
512
+ for (let statement of se.getStatement().getDependentStatements().entries())
513
+ if (statement[1]) se.addDependency(statement[0]);
514
514
  }
515
515
 
516
516
  if (paramSet.size) {
517
517
  for (let param of Array.from(paramSet.values())) {
518
+ if (param.isVariableArgument()) continue;
518
519
  if (isNullValue(SchemaUtil.getDefaultValue(param.getSchema(), sRepo)))
519
520
  se.addMessage(
520
521
  StatementMessageType.ERROR,
@@ -77,6 +77,33 @@ export class Expression extends ExpressionToken {
77
77
  isPrevOp = result.getT2();
78
78
  break;
79
79
  }
80
+ case '?': {
81
+ if (
82
+ i + 1 < length &&
83
+ this.expression.charAt(i + 1) != '?' &&
84
+ i != 0 &&
85
+ this.expression.charAt(i - 1) != '?'
86
+ ) {
87
+ i = this.processTernaryOperator(length, sb, buff, i, isPrevOp);
88
+ } else {
89
+ let result: Tuple2<number, boolean> = this.processOthers(
90
+ chr,
91
+ length,
92
+ sb,
93
+ buff,
94
+ i,
95
+ isPrevOp,
96
+ );
97
+ i = result.getT1();
98
+ isPrevOp = result.getT2();
99
+ if (isPrevOp && this.ops.peek() == Operation.ARRAY_OPERATOR) {
100
+ result = this.process(length, sb, i);
101
+ i = result.getT1();
102
+ isPrevOp = result.getT2();
103
+ }
104
+ }
105
+ break;
106
+ }
80
107
  default:
81
108
  let result: Tuple2<number, boolean> = this.processOthers(
82
109
  chr,
@@ -188,6 +215,80 @@ export class Expression extends ExpressionToken {
188
215
  return new Tuple2(i, false);
189
216
  }
190
217
 
218
+ private processTernaryOperator(
219
+ length: number,
220
+ sb: StringBuilder,
221
+ buff: string,
222
+ i: number,
223
+ isPrevOp: boolean,
224
+ ): number {
225
+ if (isPrevOp) {
226
+ throw new ExpressionEvaluationException(
227
+ this.expression,
228
+ 'Ternary operator is followed by an operator',
229
+ );
230
+ }
231
+
232
+ if (buff.trim() != '') {
233
+ this.tokens.push(new Expression(buff));
234
+ sb.setLength(0);
235
+ }
236
+
237
+ ++i;
238
+ let cnt: number = 1;
239
+ let inChr = '';
240
+ const start = i;
241
+ while (i < length && cnt > 0) {
242
+ inChr = this.expression.charAt(i);
243
+ if (inChr == '?') ++cnt;
244
+ else if (inChr == ':') --cnt;
245
+ ++i;
246
+ }
247
+
248
+ if (inChr != ':') {
249
+ throw new ExpressionEvaluationException(this.expression, "':' operater is missing");
250
+ }
251
+
252
+ if (i >= length) {
253
+ throw new ExpressionEvaluationException(
254
+ this.expression,
255
+ 'Third part of the ternary expression is missing',
256
+ );
257
+ }
258
+
259
+ while (
260
+ !this.ops.isEmpty() &&
261
+ this.hasPrecedence(Operation.CONDITIONAL_TERNARY_OPERATOR, this.ops.peek())
262
+ ) {
263
+ let prev: Operation = this.ops.pop();
264
+
265
+ if (Operation.UNARY_OPERATORS.has(prev)) {
266
+ const l: ExpressionToken = this.tokens.pop();
267
+ this.tokens.push(new Expression('', l, undefined, prev));
268
+ } else {
269
+ let r = this.tokens.pop();
270
+ let l = this.tokens.pop();
271
+
272
+ this.tokens.push(new Expression('', l, r, prev));
273
+ }
274
+ }
275
+
276
+ this.ops.push(Operation.CONDITIONAL_TERNARY_OPERATOR);
277
+ this.tokens.push(new Expression(this.expression.substring(start, i - 1)));
278
+
279
+ const secondExp: string = this.expression.substring(i);
280
+ if (secondExp.trim() === '') {
281
+ throw new ExpressionEvaluationException(
282
+ this.expression,
283
+ 'Third part of the ternary expression is missing',
284
+ );
285
+ }
286
+
287
+ this.tokens.push(new Expression(secondExp));
288
+
289
+ return length - 1;
290
+ }
291
+
191
292
  private processSubExpression(
192
293
  length: number,
193
294
  sb: StringBuilder,
@@ -329,6 +430,25 @@ export class Expression extends ExpressionToken {
329
430
  )
330
431
  .append(')');
331
432
  ind++;
433
+ } else if (this.ops.get(i) == Operation.CONDITIONAL_TERNARY_OPERATOR) {
434
+ let temp: ExpressionToken = tokens[ind++];
435
+ sb.insert(
436
+ 0,
437
+ temp instanceof Expression ? (temp as Expression).toString() : temp.toString(),
438
+ );
439
+ sb.insert(0, ':');
440
+ temp = tokens[ind++];
441
+ sb.insert(
442
+ 0,
443
+ temp instanceof Expression ? (temp as Expression).toString() : temp.toString(),
444
+ );
445
+ sb.insert(0, '?');
446
+ temp = tokens[ind++];
447
+ sb.insert(
448
+ 0,
449
+ temp instanceof Expression ? (temp as Expression).toString() : temp.toString(),
450
+ ).append(')');
451
+ sb.insert(0, '(');
332
452
  } else {
333
453
  if (ind == 0) {
334
454
  const temp: ExpressionToken = tokens[ind++];