@guardian-network/policy-dsl 0.3.2

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 (121) hide show
  1. package/README.md +82 -0
  2. package/dist/antlr/generated/grammar/LacLangLexer.d.ts +53 -0
  3. package/dist/antlr/generated/grammar/LacLangLexer.d.ts.map +1 -0
  4. package/dist/antlr/generated/grammar/LacLangLexer.js +322 -0
  5. package/dist/antlr/generated/grammar/LacLangLexer.js.map +1 -0
  6. package/dist/antlr/generated/grammar/LacLangListener.d.ts +39 -0
  7. package/dist/antlr/generated/grammar/LacLangListener.d.ts.map +1 -0
  8. package/dist/antlr/generated/grammar/LacLangListener.js +3 -0
  9. package/dist/antlr/generated/grammar/LacLangListener.js.map +1 -0
  10. package/dist/antlr/generated/grammar/LacLangParser.d.ts +231 -0
  11. package/dist/antlr/generated/grammar/LacLangParser.d.ts.map +1 -0
  12. package/dist/antlr/generated/grammar/LacLangParser.js +1399 -0
  13. package/dist/antlr/generated/grammar/LacLangParser.js.map +1 -0
  14. package/dist/antlr/index.d.ts +4 -0
  15. package/dist/antlr/index.d.ts.map +1 -0
  16. package/dist/antlr/index.js +20 -0
  17. package/dist/antlr/index.js.map +1 -0
  18. package/dist/index.d.ts +4 -0
  19. package/dist/index.d.ts.map +1 -0
  20. package/dist/index.js +23 -0
  21. package/dist/index.js.map +1 -0
  22. package/dist/ir-generation/hashing.helper.d.ts +3 -0
  23. package/dist/ir-generation/hashing.helper.d.ts.map +1 -0
  24. package/dist/ir-generation/hashing.helper.js +12 -0
  25. package/dist/ir-generation/hashing.helper.js.map +1 -0
  26. package/dist/ir-generation/index.d.ts +3 -0
  27. package/dist/ir-generation/index.d.ts.map +1 -0
  28. package/dist/ir-generation/index.js +19 -0
  29. package/dist/ir-generation/index.js.map +1 -0
  30. package/dist/ir-generation/parsed-state-to-ir.transformer.d.ts +11 -0
  31. package/dist/ir-generation/parsed-state-to-ir.transformer.d.ts.map +1 -0
  32. package/dist/ir-generation/parsed-state-to-ir.transformer.js +30 -0
  33. package/dist/ir-generation/parsed-state-to-ir.transformer.js.map +1 -0
  34. package/dist/transpiler/Transpiler.d.ts +17 -0
  35. package/dist/transpiler/Transpiler.d.ts.map +1 -0
  36. package/dist/transpiler/Transpiler.js +43 -0
  37. package/dist/transpiler/Transpiler.js.map +1 -0
  38. package/dist/transpiler/errors/ErrorFactory.d.ts +22 -0
  39. package/dist/transpiler/errors/ErrorFactory.d.ts.map +1 -0
  40. package/dist/transpiler/errors/ErrorFactory.js +62 -0
  41. package/dist/transpiler/errors/ErrorFactory.js.map +1 -0
  42. package/dist/transpiler/errors/helpers.d.ts +3 -0
  43. package/dist/transpiler/errors/helpers.d.ts.map +1 -0
  44. package/dist/transpiler/errors/helpers.js +6 -0
  45. package/dist/transpiler/errors/helpers.js.map +1 -0
  46. package/dist/transpiler/errors/index.d.ts +2 -0
  47. package/dist/transpiler/errors/index.d.ts.map +1 -0
  48. package/dist/transpiler/errors/index.js +18 -0
  49. package/dist/transpiler/errors/index.js.map +1 -0
  50. package/dist/transpiler/errors/validation-errors.d.ts +57 -0
  51. package/dist/transpiler/errors/validation-errors.d.ts.map +1 -0
  52. package/dist/transpiler/errors/validation-errors.js +132 -0
  53. package/dist/transpiler/errors/validation-errors.js.map +1 -0
  54. package/dist/transpiler/helpers/formatter.d.ts +10 -0
  55. package/dist/transpiler/helpers/formatter.d.ts.map +1 -0
  56. package/dist/transpiler/helpers/formatter.js +140 -0
  57. package/dist/transpiler/helpers/formatter.js.map +1 -0
  58. package/dist/transpiler/helpers/index.d.ts +5 -0
  59. package/dist/transpiler/helpers/index.d.ts.map +1 -0
  60. package/dist/transpiler/helpers/index.js +21 -0
  61. package/dist/transpiler/helpers/index.js.map +1 -0
  62. package/dist/transpiler/helpers/resources.helper.d.ts +4 -0
  63. package/dist/transpiler/helpers/resources.helper.d.ts.map +1 -0
  64. package/dist/transpiler/helpers/resources.helper.js +50 -0
  65. package/dist/transpiler/helpers/resources.helper.js.map +1 -0
  66. package/dist/transpiler/helpers/types.d.ts +12 -0
  67. package/dist/transpiler/helpers/types.d.ts.map +1 -0
  68. package/dist/transpiler/helpers/types.js +3 -0
  69. package/dist/transpiler/helpers/types.js.map +1 -0
  70. package/dist/transpiler/helpers/validations.helper.d.ts +8 -0
  71. package/dist/transpiler/helpers/validations.helper.d.ts.map +1 -0
  72. package/dist/transpiler/helpers/validations.helper.js +41 -0
  73. package/dist/transpiler/helpers/validations.helper.js.map +1 -0
  74. package/dist/transpiler/index.d.ts +3 -0
  75. package/dist/transpiler/index.d.ts.map +1 -0
  76. package/dist/transpiler/index.js +21 -0
  77. package/dist/transpiler/index.js.map +1 -0
  78. package/dist/transpiler/listener.d.ts +17 -0
  79. package/dist/transpiler/listener.d.ts.map +1 -0
  80. package/dist/transpiler/listener.js +94 -0
  81. package/dist/transpiler/listener.js.map +1 -0
  82. package/dist/transpiler/state/LatentState.d.ts +30 -0
  83. package/dist/transpiler/state/LatentState.d.ts.map +1 -0
  84. package/dist/transpiler/state/LatentState.js +130 -0
  85. package/dist/transpiler/state/LatentState.js.map +1 -0
  86. package/dist/transpiler/state/index.d.ts +3 -0
  87. package/dist/transpiler/state/index.d.ts.map +1 -0
  88. package/dist/transpiler/state/index.js +19 -0
  89. package/dist/transpiler/state/index.js.map +1 -0
  90. package/dist/transpiler/state/types.d.ts +42 -0
  91. package/dist/transpiler/state/types.d.ts.map +1 -0
  92. package/dist/transpiler/state/types.js +3 -0
  93. package/dist/transpiler/state/types.js.map +1 -0
  94. package/package.json +79 -0
  95. package/src/antlr/generated/grammar/LacLang.interp +88 -0
  96. package/src/antlr/generated/grammar/LacLang.tokens +52 -0
  97. package/src/antlr/generated/grammar/LacLangLexer.interp +108 -0
  98. package/src/antlr/generated/grammar/LacLangLexer.tokens +52 -0
  99. package/src/antlr/generated/grammar/LacLangLexer.ts +331 -0
  100. package/src/antlr/generated/grammar/LacLangListener.ts +216 -0
  101. package/src/antlr/generated/grammar/LacLangParser.ts +1484 -0
  102. package/src/antlr/index.ts +3 -0
  103. package/src/index.ts +3 -0
  104. package/src/ir-generation/hashing.helper.ts +13 -0
  105. package/src/ir-generation/index.ts +2 -0
  106. package/src/ir-generation/parsed-state-to-ir.transformer.ts +50 -0
  107. package/src/transpiler/Transpiler.ts +58 -0
  108. package/src/transpiler/errors/ErrorFactory.ts +130 -0
  109. package/src/transpiler/errors/helpers.ts +4 -0
  110. package/src/transpiler/errors/index.ts +1 -0
  111. package/src/transpiler/errors/validation-errors.ts +157 -0
  112. package/src/transpiler/helpers/formatter.ts +173 -0
  113. package/src/transpiler/helpers/index.ts +4 -0
  114. package/src/transpiler/helpers/resources.helper.ts +83 -0
  115. package/src/transpiler/helpers/types.ts +23 -0
  116. package/src/transpiler/helpers/validations.helper.ts +69 -0
  117. package/src/transpiler/index.ts +2 -0
  118. package/src/transpiler/listener.ts +180 -0
  119. package/src/transpiler/state/LatentState.ts +202 -0
  120. package/src/transpiler/state/index.ts +2 -0
  121. package/src/transpiler/state/types.ts +50 -0
@@ -0,0 +1,23 @@
1
+ import {
2
+ InstanceConfigArgumentsOnly,
3
+ NodeTreeInitData as ParsingResult,
4
+ } from '@guardian-network/shared';
5
+
6
+ export type InnerValidationMiddleware = (
7
+ artifactAddress: string,
8
+ currentInstanceConfig: InstanceConfigArgumentsOnly,
9
+ ) => Promise<void>;
10
+
11
+ export type OuterValidationMiddlerware = (
12
+ output: ParsingResult[],
13
+ ) => Promise<void>;
14
+
15
+ export type ValidationMiddlware = {
16
+ innerValidations: InnerValidationMiddleware;
17
+ outerValidations: OuterValidationMiddlerware;
18
+ };
19
+
20
+ export type TranspilerConfig = {
21
+ partialSources?: boolean;
22
+ sourcesDir: string;
23
+ };
@@ -0,0 +1,69 @@
1
+ import { findCycle, mapToArray, TypedValue } from '@guardian-network/shared';
2
+ import { ParserRuleContext } from 'antlr4ts';
3
+ import { ErrorFactory } from '../errors/ErrorFactory';
4
+ import { extractReferenceNodeIds, formatInstanceReference } from '../helpers';
5
+ import { InstancesById, InstancesByName } from '../state/types';
6
+
7
+ export const lookupOrThrow = <T>(
8
+ key: string,
9
+ storage: Map<string, T>,
10
+ error: Error,
11
+ ) => {
12
+ const item = storage.get(key);
13
+
14
+ if (!item) throw error;
15
+
16
+ return item;
17
+ };
18
+
19
+ export const lookupAndThrow = <T>(
20
+ key: string,
21
+ storage: Map<string, T>,
22
+ errorMorph: (item: T) => Error,
23
+ ) => {
24
+ const item = storage.get(key);
25
+
26
+ if (!!item) throw errorMorph(item);
27
+ };
28
+
29
+ export const findCycleAndThrow = (
30
+ instancesByNameMap: InstancesByName,
31
+ instancesByIdMap: InstancesById,
32
+ ) => {
33
+ const instancesList = mapToArray(instancesByNameMap);
34
+
35
+ const nodesList = instancesList.map((instance) => ({
36
+ id: instance.id,
37
+ references: extractReferenceNodeIds(instance.config),
38
+ }));
39
+
40
+ const cycleFound = findCycle(nodesList);
41
+
42
+ if (!!cycleFound) {
43
+ const invokingNode = instancesByIdMap.get(cycleFound.parentNodeId)!;
44
+ const referencedNode = instancesByIdMap.get(cycleFound.nodeId)!;
45
+
46
+ throw ErrorFactory.cyclicReferenceDSL(
47
+ invokingNode.name,
48
+ referencedNode.name,
49
+ invokingNode.ctx,
50
+ referencedNode.ctx,
51
+ );
52
+ }
53
+ };
54
+
55
+ export const findSelfReferenceAndThrow = (
56
+ instanceName: string,
57
+ instanceId: string, // same as nodeId
58
+ execArguments: TypedValue[],
59
+ ctx: ParserRuleContext,
60
+ ) => {
61
+ const maybeSelfReference = execArguments.find(
62
+ ({ substitution, value }) =>
63
+ !!substitution && value == formatInstanceReference(instanceId),
64
+ );
65
+
66
+ if (!!maybeSelfReference) {
67
+ throw ErrorFactory.selfReferenceDSL(instanceName, ctx);
68
+ }
69
+ };
@@ -0,0 +1,2 @@
1
+ export * from './helpers';
2
+ export { Transpiler } from './Transpiler';
@@ -0,0 +1,180 @@
1
+ import { InstanceConfig } from '@guardian-network/shared';
2
+ import { join, normalize } from 'node:path';
3
+ import {
4
+ ArtifactDeclarationContext,
5
+ ConstantDeclarationContext,
6
+ DirectiveContext,
7
+ EvaluateStatementContext,
8
+ ImportStatementContext,
9
+ InstanceDeclarationContext,
10
+ LacLangListener,
11
+ ProgramContext,
12
+ VarDeclarationContext,
13
+ } from '../antlr';
14
+ import { nodeIdFromDeclaration as calculateNodeId } from '../ir-generation';
15
+ import { ErrorFactory } from './errors/ErrorFactory';
16
+ import {
17
+ dereferenceArtifact,
18
+ extractAndLookupExecArguments,
19
+ extractAndLookupInitArguments,
20
+ fetchContent,
21
+ isLocalRelativeUrl,
22
+ isLocalUrl,
23
+ TranspilerConfig,
24
+ } from './helpers';
25
+ import {
26
+ findCycleAndThrow,
27
+ findSelfReferenceAndThrow,
28
+ lookupAndThrow,
29
+ lookupOrThrow,
30
+ } from './helpers/validations.helper';
31
+ import { LatentState } from './state/LatentState';
32
+ import { Transpiler } from './Transpiler';
33
+
34
+ export class LacLangTranspiler implements LacLangListener {
35
+ public latentState: LatentState = new LatentState();
36
+ private config: TranspilerConfig;
37
+
38
+ constructor(options: TranspilerConfig) {
39
+ this.config = options;
40
+ }
41
+
42
+ enterDirective(ctx: DirectiveContext) {
43
+ switch (true) {
44
+ case !!ctx.directiveIndentifier().injectedOnlyDirective():
45
+ this.latentState.setInjectionConstraint(false);
46
+ break;
47
+ default:
48
+ throw ErrorFactory.unknownDirective(
49
+ ctx.directiveIndentifier().text,
50
+ ctx,
51
+ );
52
+ }
53
+ }
54
+
55
+ enterImportStatement(ctx: ImportStatementContext) {
56
+ const url = ctx.STRING_LITERAL().text.replace(/^['"]|['"]$/g, '');
57
+ const normalizedUrl =
58
+ isLocalUrl(url) || isLocalRelativeUrl(url)
59
+ ? normalize(join(this.config.sourcesDir, url))
60
+ : url;
61
+
62
+ const sources = fetchContent(normalizedUrl);
63
+
64
+ const subTranspiler = Transpiler.create(sources, {
65
+ partialSources: true,
66
+ sourcesDir: this.config.sourcesDir,
67
+ });
68
+ subTranspiler.transpile();
69
+
70
+ const subState = subTranspiler.getLatentState();
71
+ this.latentState.merge(subState);
72
+ }
73
+
74
+ enterVarDeclaration(ctx: VarDeclarationContext): void {
75
+ const name = ctx.IDENTIFIER().text;
76
+
77
+ lookupAndThrow(name, this.latentState.variables, (declared) =>
78
+ ErrorFactory.variableAlreadyDefined(name, ctx, declared.ctx),
79
+ );
80
+
81
+ this.latentState.setVariables(ctx);
82
+ }
83
+
84
+ enterConstantDeclaration(ctx: ConstantDeclarationContext) {
85
+ const name = ctx.IDENTIFIER().text;
86
+
87
+ lookupAndThrow(name, this.latentState.constants, (declared) =>
88
+ ErrorFactory.constantIsAlreadyDefined(name, ctx, declared.ctx),
89
+ );
90
+
91
+ this.latentState.setConstants(ctx);
92
+ }
93
+
94
+ enterArtifactDeclaration(ctx: ArtifactDeclarationContext) {
95
+ const name = ctx.IDENTIFIER().text;
96
+
97
+ lookupAndThrow(name, this.latentState.artifacts, (declared) =>
98
+ ErrorFactory.artifactAlreadyDefined(name, ctx, declared.ctx),
99
+ );
100
+
101
+ this.latentState.setArtifacts(ctx);
102
+ }
103
+
104
+ enterInstanceDeclaration(ctx: InstanceDeclarationContext) {
105
+ const name = ctx.IDENTIFIER().text;
106
+
107
+ lookupAndThrow(name, this.latentState.instancesByName, (declared) =>
108
+ ErrorFactory.instanceAlreadyDefined(name, ctx, declared.ctx),
109
+ );
110
+
111
+ const execArguments = extractAndLookupExecArguments(ctx, this.latentState);
112
+ const initArguments = extractAndLookupInitArguments(ctx, this.latentState);
113
+
114
+ const artifactDereferenced = dereferenceArtifact(
115
+ ctx,
116
+ this.latentState.artifacts,
117
+ );
118
+
119
+ const instanceConfig: InstanceConfig = {
120
+ artifactAddress: artifactDereferenced,
121
+ needsInitialization: !!initArguments,
122
+ execArguments,
123
+ initArguments: initArguments ?? [],
124
+ };
125
+
126
+ const instancesCount = this.latentState.instancesByName.size;
127
+ const nodeId = calculateNodeId(instanceConfig, instancesCount);
128
+
129
+ // note: may be redundant cause in this case instance will not be even defined yet
130
+ findSelfReferenceAndThrow(name, nodeId, execArguments, ctx);
131
+
132
+ this.latentState.setInstancesByName(
133
+ ctx,
134
+ nodeId,
135
+ instanceConfig,
136
+ instancesCount,
137
+ );
138
+
139
+ this.latentState.setInstancesById(
140
+ ctx,
141
+ nodeId,
142
+ instanceConfig,
143
+ instancesCount,
144
+ );
145
+
146
+ // note: may be redundant due to linear declaration by design
147
+ findCycleAndThrow(
148
+ this.latentState.instancesByName,
149
+ this.latentState.instancesById,
150
+ );
151
+ }
152
+
153
+ enterEvaluateStatement(ctx: EvaluateStatementContext) {
154
+ const previouslyDeclared = this.latentState.evaluateRelativeTo;
155
+ if (!!previouslyDeclared) {
156
+ throw ErrorFactory.evaluateAlreadyDeclared(ctx, previouslyDeclared.ctx);
157
+ }
158
+
159
+ const instanceName = ctx.IDENTIFIER().text;
160
+ const refInst = lookupOrThrow(
161
+ instanceName,
162
+ this.latentState.instancesByName,
163
+ ErrorFactory.instanceNotDefined(instanceName, ctx),
164
+ );
165
+
166
+ if (refInst.type != 'bool')
167
+ throw ErrorFactory.evaluateTypeNotBool(instanceName, ctx, refInst.ctx);
168
+
169
+ this.latentState.setEvaluateRelativeTo = {
170
+ nodeId: refInst.id,
171
+ ctx,
172
+ };
173
+ }
174
+
175
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
176
+ exitProgram(_: ProgramContext) {
177
+ if (!this.latentState.evaluateRelativeTo && !this.config.partialSources)
178
+ throw ErrorFactory.noEvaluateStatement();
179
+ }
180
+ }
@@ -0,0 +1,202 @@
1
+ import { InstanceConfig } from '@guardian-network/shared';
2
+ import {
3
+ ArtifactDeclarationContext,
4
+ ConstantDeclarationContext,
5
+ InstanceDeclarationContext,
6
+ VarDeclarationContext,
7
+ } from '../../antlr';
8
+ import { ErrorFactory } from '../errors';
9
+ import {
10
+ Artifacts,
11
+ Constants,
12
+ Evaluating,
13
+ InstancesById,
14
+ InstancesByName,
15
+ Variables,
16
+ } from './types';
17
+
18
+ export class LatentState {
19
+ private constantsMap: Constants;
20
+
21
+ private variablesMap: Variables;
22
+
23
+ private artifactsMap: Artifacts;
24
+
25
+ private instancesByNameMap: InstancesByName;
26
+
27
+ private instancesByIdMap: InstancesById;
28
+
29
+ private evaluateRelativeToInternal?: Evaluating;
30
+
31
+ private onlyInjectedVariables?: boolean;
32
+
33
+ // note: value properties (not Map) must not have defaults because :196 constraint
34
+ constructor() {
35
+ this.constantsMap = new Map();
36
+
37
+ this.variablesMap = new Map();
38
+
39
+ this.artifactsMap = new Map();
40
+
41
+ this.instancesByNameMap = new Map();
42
+
43
+ this.instancesByIdMap = new Map();
44
+ }
45
+
46
+ get constants() {
47
+ return this.constantsMap;
48
+ }
49
+
50
+ get variables() {
51
+ return this.variablesMap;
52
+ }
53
+
54
+ get artifacts() {
55
+ return this.artifactsMap;
56
+ }
57
+
58
+ get instancesByName() {
59
+ return this.instancesByNameMap;
60
+ }
61
+
62
+ get instancesById() {
63
+ return this.instancesByIdMap;
64
+ }
65
+
66
+ get evaluateRelativeTo() {
67
+ return this.evaluateRelativeToInternal;
68
+ }
69
+ set setEvaluateRelativeTo(value: Evaluating) {
70
+ this.evaluateRelativeToInternal = value;
71
+ }
72
+
73
+ setVariables(ctx: VarDeclarationContext) {
74
+ const { text: name } = ctx.IDENTIFIER();
75
+ const { text: type } = ctx.dataType();
76
+
77
+ let injection = ctx.injectionModifier()?.STRING_LITERAL().text;
78
+
79
+ if (!injection && this.onlyInjectedVariables)
80
+ throw ErrorFactory.nonInjectedVariable(name, ctx);
81
+
82
+ injection = injection ?? '';
83
+
84
+ this.variablesMap.set(name, { type, ctx, injection });
85
+ }
86
+
87
+ setConstants(ctx: ConstantDeclarationContext) {
88
+ const { text: name } = ctx.IDENTIFIER();
89
+ const { text: value } = ctx.literal();
90
+ const { text: type } = ctx.dataType();
91
+
92
+ this.constantsMap.set(name, {
93
+ value,
94
+ type,
95
+ ctx,
96
+ });
97
+ }
98
+
99
+ setArtifacts(ctx: ArtifactDeclarationContext) {
100
+ const { text: name } = ctx.IDENTIFIER();
101
+ const { text: address } = ctx.ADDRESS_LITERAL();
102
+
103
+ this.artifactsMap.set(name, {
104
+ address,
105
+ ctx,
106
+ });
107
+ }
108
+
109
+ setInstancesByName(
110
+ ctx: InstanceDeclarationContext,
111
+ nodeId: string,
112
+ instanceConfig: InstanceConfig,
113
+ instancesCount: number,
114
+ ) {
115
+ const { text: name } = ctx.IDENTIFIER();
116
+ const { text: type } = ctx.dataType();
117
+
118
+ this.instancesByNameMap.set(name, {
119
+ id: nodeId,
120
+ config: instanceConfig,
121
+ type,
122
+ index: instancesCount,
123
+ ctx,
124
+ });
125
+ }
126
+
127
+ setInstancesById(
128
+ ctx: InstanceDeclarationContext,
129
+ nodeId: string,
130
+ instanceConfig: InstanceConfig,
131
+ instancesCount: number,
132
+ ) {
133
+ const { text: name } = ctx.IDENTIFIER();
134
+ const { text: type } = ctx.dataType();
135
+
136
+ this.instancesByIdMap.set(nodeId, {
137
+ name,
138
+ config: instanceConfig,
139
+ type,
140
+ index: instancesCount,
141
+ ctx,
142
+ });
143
+ }
144
+
145
+ setInjectionConstraint(allowNonInjectedVariables: boolean) {
146
+ this.onlyInjectedVariables = !allowNonInjectedVariables;
147
+ }
148
+
149
+ merge(subState: LatentState) {
150
+ for (const property of Object.keys(this)) {
151
+ const indexingProperty = <keyof LatentState>property;
152
+
153
+ try {
154
+ if (this[indexingProperty] instanceof Map) {
155
+ this.concatMaps(
156
+ <Map<string, any>>subState[indexingProperty],
157
+ this[indexingProperty],
158
+ );
159
+ } else {
160
+ this.moveValue(subState[indexingProperty], this[indexingProperty]);
161
+ }
162
+ } catch (e) {
163
+ throw ErrorFactory.importAmbiguity(property, (<Error>e).message);
164
+ }
165
+ }
166
+ }
167
+
168
+ private concatMaps<T>(
169
+ source: Map<string, T | T[]>,
170
+ target: Map<string, T | T[]>,
171
+ ) {
172
+ for (const originalKey of source.keys()) {
173
+ const originalValue = source.get(originalKey) as T;
174
+
175
+ if (target.get(originalKey) != undefined) {
176
+ if (Array.isArray(originalValue)) {
177
+ target.set(
178
+ originalKey,
179
+ originalValue.concat(target.get(originalKey)).sort(),
180
+ );
181
+ return;
182
+ }
183
+
184
+ throw new Error(
185
+ `Ambiguity while merging maps: target already has "${originalKey}" record`,
186
+ );
187
+ }
188
+
189
+ target.set(originalKey, originalValue);
190
+ }
191
+ }
192
+
193
+ private moveValue<T>(source: T, target: T) {
194
+ if (!!target) {
195
+ throw new Error(
196
+ `Ambiguity while merging values: target already set to "${target}"`,
197
+ );
198
+ }
199
+
200
+ target = source;
201
+ }
202
+ }
@@ -0,0 +1,2 @@
1
+ export * from './LatentState';
2
+ export * from './types';
@@ -0,0 +1,50 @@
1
+ import { InstanceConfig } from '@guardian-network/shared';
2
+ import { ParserRuleContext } from 'antlr4ts';
3
+
4
+ type KeyType = string;
5
+ type IdKeyType = KeyType;
6
+ type NameKeyType = KeyType;
7
+
8
+ type DataType = { type: string };
9
+ type AddressType = {
10
+ address: string;
11
+ };
12
+ type NameType = {
13
+ name: string;
14
+ };
15
+ type NodeIdType = {
16
+ nodeId: string;
17
+ };
18
+ type IndexType = {
19
+ index: number;
20
+ };
21
+ type ConfigType = {
22
+ config: InstanceConfig;
23
+ };
24
+ type InjectionType = { injection: string };
25
+ type ValueType = { value: string };
26
+
27
+ type ContextWrapped = { ctx: ParserRuleContext };
28
+
29
+ export type Constants = Map<KeyType, ValueType & DataType & ContextWrapped>;
30
+
31
+ export type Variables = Map<KeyType, DataType & ContextWrapped & InjectionType>;
32
+
33
+ export type Artifacts = Map<KeyType, AddressType & ContextWrapped>;
34
+
35
+ export type InstancesByName = Map<
36
+ NameKeyType,
37
+ {
38
+ id: string;
39
+ } & IndexType &
40
+ ConfigType &
41
+ DataType &
42
+ ContextWrapped
43
+ >;
44
+
45
+ export type InstancesById = Map<
46
+ IdKeyType,
47
+ NameType & IndexType & ConfigType & DataType & ContextWrapped
48
+ >;
49
+
50
+ export type Evaluating = NodeIdType & ContextWrapped;