@nasa-jpl/aerie-sequence-languages 0.0.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 (123) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +2 -0
  3. package/dist/cjs/converters/satf-sasf-utils.d.ts +48 -0
  4. package/dist/cjs/converters/satf-sasf-utils.d.ts.map +1 -0
  5. package/dist/cjs/converters/satf-sasf-utils.js +939 -0
  6. package/dist/cjs/converters/satf-sasf-utils.js.map +1 -0
  7. package/dist/cjs/converters/seqJsonToSeqn.d.ts +6 -0
  8. package/dist/cjs/converters/seqJsonToSeqn.d.ts.map +1 -0
  9. package/dist/cjs/converters/seqJsonToSeqn.js +310 -0
  10. package/dist/cjs/converters/seqJsonToSeqn.js.map +1 -0
  11. package/dist/cjs/converters/seqnToSeqJson.d.ts +11 -0
  12. package/dist/cjs/converters/seqnToSeqJson.d.ts.map +1 -0
  13. package/dist/cjs/converters/seqnToSeqJson.js +713 -0
  14. package/dist/cjs/converters/seqnToSeqJson.js.map +1 -0
  15. package/dist/cjs/index.d.ts +9 -0
  16. package/dist/cjs/index.d.ts.map +1 -0
  17. package/dist/cjs/index.js +22 -0
  18. package/dist/cjs/index.js.map +1 -0
  19. package/dist/cjs/languages/satf/constants/satf-sasf-constants.d.ts +139 -0
  20. package/dist/cjs/languages/satf/constants/satf-sasf-constants.d.ts.map +1 -0
  21. package/dist/cjs/languages/satf/constants/satf-sasf-constants.js +143 -0
  22. package/dist/cjs/languages/satf/constants/satf-sasf-constants.js.map +1 -0
  23. package/dist/cjs/languages/satf/grammar/satf-sasf.d.ts +4 -0
  24. package/dist/cjs/languages/satf/grammar/satf-sasf.d.ts.map +1 -0
  25. package/dist/cjs/languages/satf/grammar/satf-sasf.grammar.d.ts +3 -0
  26. package/dist/cjs/languages/satf/grammar/satf-sasf.grammar.d.ts.map +1 -0
  27. package/dist/cjs/languages/satf/grammar/satf-sasf.grammar.js +20 -0
  28. package/dist/cjs/languages/satf/grammar/satf-sasf.grammar.js.map +1 -0
  29. package/dist/cjs/languages/satf/grammar/satf-sasf.grammar.terms.d.ts +69 -0
  30. package/dist/cjs/languages/satf/grammar/satf-sasf.grammar.terms.d.ts.map +1 -0
  31. package/dist/cjs/languages/satf/grammar/satf-sasf.grammar.terms.js +7 -0
  32. package/dist/cjs/languages/satf/grammar/satf-sasf.grammar.terms.js.map +1 -0
  33. package/dist/cjs/languages/satf/grammar/satf-sasf.js +56 -0
  34. package/dist/cjs/languages/satf/grammar/satf-sasf.js.map +1 -0
  35. package/dist/cjs/languages/satf/types/types.d.ts +23 -0
  36. package/dist/cjs/languages/satf/types/types.d.ts.map +1 -0
  37. package/dist/cjs/languages/satf/types/types.js +3 -0
  38. package/dist/cjs/languages/satf/types/types.js.map +1 -0
  39. package/dist/cjs/languages/seq-n/seq-n.d.ts +2 -0
  40. package/dist/cjs/languages/seq-n/seq-n.d.ts.map +1 -0
  41. package/dist/cjs/languages/seq-n/seq-n.grammar.d.ts +3 -0
  42. package/dist/cjs/languages/seq-n/seq-n.grammar.d.ts.map +1 -0
  43. package/dist/cjs/languages/seq-n/seq-n.grammar.js +24 -0
  44. package/dist/cjs/languages/seq-n/seq-n.grammar.js.map +1 -0
  45. package/dist/cjs/languages/seq-n/seq-n.grammar.terms.d.ts +39 -0
  46. package/dist/cjs/languages/seq-n/seq-n.grammar.terms.d.ts.map +1 -0
  47. package/dist/cjs/languages/seq-n/seq-n.grammar.terms.js +6 -0
  48. package/dist/cjs/languages/seq-n/seq-n.grammar.terms.js.map +1 -0
  49. package/dist/cjs/languages/seq-n/seq-n.js +6 -0
  50. package/dist/cjs/languages/seq-n/seq-n.js.map +1 -0
  51. package/dist/cjs/languages/seq-n/seqn-grammar-constants.d.ts +45 -0
  52. package/dist/cjs/languages/seq-n/seqn-grammar-constants.d.ts.map +1 -0
  53. package/dist/cjs/languages/seq-n/seqn-grammar-constants.js +48 -0
  54. package/dist/cjs/languages/seq-n/seqn-grammar-constants.js.map +1 -0
  55. package/dist/cjs/logger.d.ts +3 -0
  56. package/dist/cjs/logger.d.ts.map +1 -0
  57. package/dist/cjs/logger.js +11 -0
  58. package/dist/cjs/logger.js.map +1 -0
  59. package/dist/cjs/utils/string.d.ts +8 -0
  60. package/dist/cjs/utils/string.d.ts.map +1 -0
  61. package/dist/cjs/utils/string.js +32 -0
  62. package/dist/cjs/utils/string.js.map +1 -0
  63. package/dist/esm/converters/satf-sasf-utils.d.ts +48 -0
  64. package/dist/esm/converters/satf-sasf-utils.d.ts.map +1 -0
  65. package/dist/esm/converters/satf-sasf-utils.js +927 -0
  66. package/dist/esm/converters/satf-sasf-utils.js.map +1 -0
  67. package/dist/esm/converters/seqJsonToSeqn.d.ts +6 -0
  68. package/dist/esm/converters/seqJsonToSeqn.d.ts.map +1 -0
  69. package/dist/esm/converters/seqJsonToSeqn.js +305 -0
  70. package/dist/esm/converters/seqJsonToSeqn.js.map +1 -0
  71. package/dist/esm/converters/seqnToSeqJson.d.ts +11 -0
  72. package/dist/esm/converters/seqnToSeqJson.d.ts.map +1 -0
  73. package/dist/esm/converters/seqnToSeqJson.js +705 -0
  74. package/dist/esm/converters/seqnToSeqJson.js.map +1 -0
  75. package/dist/esm/index.d.ts +9 -0
  76. package/dist/esm/index.d.ts.map +1 -0
  77. package/dist/esm/index.js +8 -0
  78. package/dist/esm/index.js.map +1 -0
  79. package/dist/esm/languages/satf/constants/satf-sasf-constants.d.ts +139 -0
  80. package/dist/esm/languages/satf/constants/satf-sasf-constants.d.ts.map +1 -0
  81. package/dist/esm/languages/satf/constants/satf-sasf-constants.js +139 -0
  82. package/dist/esm/languages/satf/constants/satf-sasf-constants.js.map +1 -0
  83. package/dist/esm/languages/satf/grammar/satf-sasf.d.ts +4 -0
  84. package/dist/esm/languages/satf/grammar/satf-sasf.d.ts.map +1 -0
  85. package/dist/esm/languages/satf/grammar/satf-sasf.grammar.d.ts +3 -0
  86. package/dist/esm/languages/satf/grammar/satf-sasf.grammar.d.ts.map +1 -0
  87. package/dist/esm/languages/satf/grammar/satf-sasf.grammar.js +17 -0
  88. package/dist/esm/languages/satf/grammar/satf-sasf.grammar.js.map +1 -0
  89. package/dist/esm/languages/satf/grammar/satf-sasf.grammar.terms.d.ts +69 -0
  90. package/dist/esm/languages/satf/grammar/satf-sasf.grammar.terms.d.ts.map +1 -0
  91. package/dist/esm/languages/satf/grammar/satf-sasf.grammar.terms.js +3 -0
  92. package/dist/esm/languages/satf/grammar/satf-sasf.grammar.terms.js.map +1 -0
  93. package/dist/esm/languages/satf/grammar/satf-sasf.js +53 -0
  94. package/dist/esm/languages/satf/grammar/satf-sasf.js.map +1 -0
  95. package/dist/esm/languages/satf/types/types.d.ts +23 -0
  96. package/dist/esm/languages/satf/types/types.d.ts.map +1 -0
  97. package/dist/esm/languages/satf/types/types.js +2 -0
  98. package/dist/esm/languages/satf/types/types.js.map +1 -0
  99. package/dist/esm/languages/seq-n/seq-n.d.ts +2 -0
  100. package/dist/esm/languages/seq-n/seq-n.d.ts.map +1 -0
  101. package/dist/esm/languages/seq-n/seq-n.grammar.d.ts +3 -0
  102. package/dist/esm/languages/seq-n/seq-n.grammar.d.ts.map +1 -0
  103. package/dist/esm/languages/seq-n/seq-n.grammar.js +21 -0
  104. package/dist/esm/languages/seq-n/seq-n.grammar.js.map +1 -0
  105. package/dist/esm/languages/seq-n/seq-n.grammar.terms.d.ts +39 -0
  106. package/dist/esm/languages/seq-n/seq-n.grammar.terms.d.ts.map +1 -0
  107. package/dist/esm/languages/seq-n/seq-n.grammar.terms.js +3 -0
  108. package/dist/esm/languages/seq-n/seq-n.grammar.terms.js.map +1 -0
  109. package/dist/esm/languages/seq-n/seq-n.js +3 -0
  110. package/dist/esm/languages/seq-n/seq-n.js.map +1 -0
  111. package/dist/esm/languages/seq-n/seqn-grammar-constants.d.ts +45 -0
  112. package/dist/esm/languages/seq-n/seqn-grammar-constants.d.ts.map +1 -0
  113. package/dist/esm/languages/seq-n/seqn-grammar-constants.js +45 -0
  114. package/dist/esm/languages/seq-n/seqn-grammar-constants.js.map +1 -0
  115. package/dist/esm/logger.d.ts +3 -0
  116. package/dist/esm/logger.d.ts.map +1 -0
  117. package/dist/esm/logger.js +7 -0
  118. package/dist/esm/logger.js.map +1 -0
  119. package/dist/esm/utils/string.d.ts +8 -0
  120. package/dist/esm/utils/string.d.ts.map +1 -0
  121. package/dist/esm/utils/string.js +25 -0
  122. package/dist/esm/utils/string.js.map +1 -0
  123. package/package.json +74 -0
@@ -0,0 +1,939 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.seqnToSATF = seqnToSATF;
4
+ exports.seqnToSASF = seqnToSASF;
5
+ exports.satfToSeqn = satfToSeqn;
6
+ exports.sasfToSeqn = sasfToSeqn;
7
+ const aerie_time_utils_1 = require("@nasa-jpl/aerie-time-utils");
8
+ const string_1 = require("../utils/string");
9
+ const satf_sasf_js_1 = require("../languages/satf/grammar/satf-sasf.js");
10
+ const satf_sasf_constants_js_1 = require("../languages/satf/constants/satf-sasf-constants.js");
11
+ const seq_n_1 = require("../languages/seq-n/seq-n");
12
+ const seqn_grammar_constants_1 = require("../languages/seq-n/seqn-grammar-constants");
13
+ /**
14
+ * Asynchronously converts a parsed SeqN tree via lezer into a structured SATF representation.
15
+ *
16
+ * Parse different sections (metadata, variables, steps) of the SeqN tree and
17
+ * assemble them into a `ParsedSatf` object containing the corresponding SATF sections.
18
+ *
19
+ * @async
20
+ * @param {string} sequence - The original SeqN source text corresponding to the `seqnTree`.
21
+ * @param {string[]} [globalVariables] - Optional. A list of predefined global variable names to be used
22
+ * @param {CommandDictionary} [commandDictionary] - Optional. A dictionary containing command definitions.
23
+ * @returns {Promise<ParsedSatf>} A Promise that resolves to an object containing the generated
24
+ * SATF `header`, `parameters , `variables`, and `steps` block strings (or undefined if a section is empty/not generated).
25
+ */
26
+ async function seqnToSATF(seqn, globalVariables, commandDictionary) {
27
+ const seqnTree = seq_n_1.SeqnParser.parse(seqn);
28
+ const header = parseHeaderfromSeqn(seqnTree, seqn);
29
+ const parameters = satfVariablesFromSeqn(seqnTree, seqn);
30
+ const variables = satfVariablesFromSeqn(seqnTree, seqn, 'Variables');
31
+ const steps = satfStepsFromSeqn(seqnTree, seqn, [...(globalVariables ? globalVariables : []), ...getSatfVariableNames(seqnTree, seqn)], commandDictionary);
32
+ return {
33
+ header,
34
+ ...(parameters ? { parameters } : {}),
35
+ ...(variables ? { variables } : {}),
36
+ ...(steps ? { steps } : {}),
37
+ };
38
+ }
39
+ /**
40
+ * Asynchronously converts a parsed SeqN tree via lezer into a structured SASF representation.
41
+ *
42
+ * Parse the metadata section and generate SASF request blocks from the SeqN tree.
43
+ * It then combines these into a `ParsedSasf` object.
44
+ *
45
+ * @async
46
+ * @param {string} sequence - The original SeqN source text corresponding to the `seqnTree`.
47
+ * @param {string[]} [globalVariables] - Optional. A list of predefined global variable names to be used
48
+ *
49
+ * @param {CommandDictionary} [commandDictionary] - Optional. A dictionary containing command definitions,
50
+ * @returns {Promise<ParsedSasf>} A Promise that resolves to an object containing the generated
51
+ * SASF `metadata` string and the concatenated `requests` string (or undefined if a section is empty/not generated).
52
+ */
53
+ async function seqnToSASF(seqn, globalVariables, commandDictionary) {
54
+ const seqnTree = seq_n_1.SeqnParser.parse(seqn);
55
+ const header = parseHeaderfromSeqn(seqnTree, seqn);
56
+ // TODO: I don't think sasf have varaibles or parameters:
57
+ // const parameters = satfVariablesFromSeqn(seqnTree, seqn);
58
+ // const variables = satfVariablesFromSeqn(seqnTree, seqn, 'Variables');
59
+ const requests = sasfRequestFromSeqN(seqnTree, seqn, globalVariables, commandDictionary);
60
+ return {
61
+ header,
62
+ requests,
63
+ };
64
+ }
65
+ function parseHeaderfromSeqn(seqnTree, sequence) {
66
+ var _a, _b, _c, _d;
67
+ const metadata = {};
68
+ const metadataEntries = (_b = (_a = seqnTree.topNode.getChild(seqn_grammar_constants_1.SEQN_NODES.METADATA)) === null || _a === void 0 ? void 0 : _a.getChildren(seqn_grammar_constants_1.SEQN_NODES.METADATA_ENTRY)) !== null && _b !== void 0 ? _b : [];
69
+ for (const entry of metadataEntries) {
70
+ const keyNode = entry.getChild(seqn_grammar_constants_1.SEQN_NODES.KEY);
71
+ const valueNode = entry.getChild(seqn_grammar_constants_1.SEQN_NODES.VALUE);
72
+ if (keyNode === null || valueNode === null) {
73
+ continue;
74
+ }
75
+ const key = (0, string_1.unquoteUnescape)(sequence.slice(keyNode.from, keyNode.to));
76
+ const value = sequence.slice(valueNode.from, valueNode.to);
77
+ metadata[key] = value;
78
+ }
79
+ let commentMetadata = seqnTree.topNode.getChildren(seqn_grammar_constants_1.SEQN_NODES.LINE_COMMENT);
80
+ if (commentMetadata.length === 0) {
81
+ commentMetadata = (_d = (_c = seqnTree.topNode.getChild(seqn_grammar_constants_1.SEQN_NODES.COMMANDS)) === null || _c === void 0 ? void 0 : _c.getChildren(seqn_grammar_constants_1.SEQN_NODES.LINE_COMMENT)) !== null && _d !== void 0 ? _d : [];
82
+ }
83
+ for (const comment of commentMetadata) {
84
+ const text = sequence.slice(comment.from, comment.to);
85
+ const [key, value] = text.split('=').map(string_1.unquoteUnescape);
86
+ if (key && value) {
87
+ metadata[key.slice(1).trim()] = value;
88
+ }
89
+ }
90
+ return metadata;
91
+ }
92
+ function satfStepsFromSeqn(seqnTree, sequence, variables, commandDictionary) {
93
+ var _a;
94
+ let stepNode = (_a = seqnTree.topNode.getChild(satf_sasf_constants_js_1.SATF_SASF_NODES.COMMANDS)) === null || _a === void 0 ? void 0 : _a.firstChild;
95
+ let steps = [];
96
+ while (stepNode) {
97
+ steps.push(stepNode);
98
+ stepNode = stepNode.nextSibling;
99
+ }
100
+ steps = steps.filter((step) => step.name !== satf_sasf_constants_js_1.SATF_SASF_NODES.LINE_COMMENT);
101
+ if (steps === null || steps.length === 0) {
102
+ return undefined;
103
+ }
104
+ return `STEPS,\n${steps
105
+ .map((step, index) => {
106
+ return parseSeqNStep(step, sequence, commandDictionary, variables, 1 + index++);
107
+ })
108
+ .filter((step) => step)
109
+ .join('\n')}\nend`;
110
+ }
111
+ function parseSeqNStep(child, text, commandDictionary, variables, order) {
112
+ switch (child === null || child === void 0 ? void 0 : child.name) {
113
+ case seqn_grammar_constants_1.SEQN_NODES.COMMAND:
114
+ return parseSeqNCommand(child, text, commandDictionary, variables, order);
115
+ case seqn_grammar_constants_1.SEQN_NODES.ACTIVATE:
116
+ return parseSeqNActivate(child, text, commandDictionary, variables, order);
117
+ case 'Load':
118
+ case 'GroundBlock':
119
+ case 'GroundEvent':
120
+ default:
121
+ return undefined;
122
+ }
123
+ }
124
+ function parseSeqNCommand(commandNode, sequence, commandDictionary, variables, order) {
125
+ const time = parseSeqNTime(commandNode, sequence);
126
+ const stemNode = commandNode.getChild(seqn_grammar_constants_1.SEQN_NODES.STEM);
127
+ const stem = stemNode ? sequence.slice(stemNode.from, stemNode.to) : 'UNKNOWN';
128
+ const argsNode = commandNode.getChild(seqn_grammar_constants_1.SEQN_NODES.ARGS);
129
+ const args = argsNode ? parseSeqNArgs(argsNode, sequence, commandDictionary, variables, stem) : [];
130
+ const description = parseSeqNDescription(commandNode, sequence);
131
+ const metadata = parsSeqNMetadata(commandNode, sequence, [seqn_grammar_constants_1.SEQN_NODES.NTEXT]);
132
+ const models = parseSeqNModel(commandNode, sequence);
133
+ return (`${'\t'}command(${order},` +
134
+ `\n${'\t'.repeat(2)}SCHEDULED_TIME,\\${time.tag}\\,${time.type},` +
135
+ `${metadata ? `\n${'\t'.repeat(2)}${metadata},` : ''}` +
136
+ `${description ? `\n${'\t'.repeat(2)}COMMENT,\\${description}\\,` : ''}` +
137
+ `${models ? `\n${'\t'.repeat(2)}ASSUMED_MODEL_VALUES,\\${models}\\,` : ''}` +
138
+ `\n${'\t'.repeat(2)}${stem}${args.length !== 0 ? `(${serializeSeqNArgs(args)})` : ''}` +
139
+ `\n${'\t'}),`);
140
+ }
141
+ function parseSeqNActivate(stepNode, sequence, commandDictionary, variables, order) {
142
+ const nameNode = stepNode.getChild(seqn_grammar_constants_1.SEQN_NODES.SEQUENCE_NAME);
143
+ const sequenceName = nameNode ? (0, string_1.unquoteUnescape)(sequence.slice(nameNode.from, nameNode.to)) : 'UNKNOWN';
144
+ const time = parseSeqNTime(stepNode, sequence);
145
+ const argsNode = stepNode.getChild(seqn_grammar_constants_1.SEQN_NODES.ARGS);
146
+ const args = argsNode ? parseSeqNArgs(argsNode, sequence, commandDictionary, variables, sequenceName) : [];
147
+ const engine = parseSeqNEngine(stepNode, sequence);
148
+ const epoch = parseSeqNEpoch(stepNode, sequence);
149
+ return `${'\t'}SPAWN(${order},
150
+ ${'\t'.repeat(2)}SCHEDULED_TIME,\\${time.tag}\\,${time.type},${engine !== undefined
151
+ ? `
152
+ ${'\t'.repeat(2)}RETURN_ENGINE_ID_TO,\\${engine}\\,`
153
+ : ''}${epoch !== undefined
154
+ ? `
155
+ ${'\t'.repeat(2)}EPOCH,${epoch},`
156
+ : ''}
157
+ ${'\t'.repeat(2)}RT_on_board_block(${sequenceName},${serializeSeqNArgs(args)})
158
+ ${'\t'}),`;
159
+ }
160
+ function parseSeqNEngine(stepNode, text) {
161
+ var _a;
162
+ const engineNode = (_a = stepNode.getChild(satf_sasf_constants_js_1.SATF_SASF_NODES.ENGINE)) === null || _a === void 0 ? void 0 : _a.getChild(satf_sasf_constants_js_1.SATF_SASF_NODES.NUMBER);
163
+ return engineNode ? parseInt(text.slice(engineNode.from, engineNode.to), 10) : undefined;
164
+ }
165
+ function parseSeqNEpoch(stepNode, text) {
166
+ var _a;
167
+ const epochNode = (_a = stepNode.getChild(satf_sasf_constants_js_1.SATF_SASF_NODES.EPOCH)) === null || _a === void 0 ? void 0 : _a.getChild(satf_sasf_constants_js_1.SATF_SASF_NODES.STRING);
168
+ return epochNode ? (0, string_1.unquoteUnescape)(text.slice(epochNode.from, epochNode.to)) : undefined;
169
+ }
170
+ function parseSeqNTime(commandNode, sequence) {
171
+ const tag = '00:00:01';
172
+ const timeTagNode = commandNode.getChild('TimeTag');
173
+ if (timeTagNode === null) {
174
+ return { tag: '00:00:00', type: 'UNKNOWN' };
175
+ }
176
+ const time = timeTagNode.firstChild;
177
+ if (time === null) {
178
+ return { tag, type: 'UNKNOWN' };
179
+ }
180
+ const timeValue = sequence.slice(time.from + 1, time.to).trim();
181
+ if (time.name === 'TimeComplete') {
182
+ return { tag, type: 'WAIT_PREVIOUS_END' };
183
+ }
184
+ else if (time.name === 'TimeGroundEpoch') {
185
+ const parentNode = time.parent;
186
+ if (parentNode) {
187
+ // ex: G+3:00 "GroundEpochName"
188
+ const parentNodeText = sequence.slice(parentNode.from, parentNode.to);
189
+ const splitParentNodeText = parentNodeText.slice(1, parentNodeText.length).split(' ');
190
+ if (splitParentNodeText.length > 0) {
191
+ const epochTag = splitParentNodeText[0];
192
+ const epochName = (0, string_1.unquoteUnescape)(splitParentNodeText[1]);
193
+ return { tag: `${epochName}${epochTag}`, type: 'GROUND_EPOCH' };
194
+ }
195
+ }
196
+ }
197
+ else {
198
+ if ((0, aerie_time_utils_1.validateTime)(timeValue, aerie_time_utils_1.TimeTypes.ISO_ORDINAL_TIME)) {
199
+ return { tag: timeValue, type: 'ABSOLUTE' };
200
+ }
201
+ else if ((0, aerie_time_utils_1.validateTime)(timeValue, aerie_time_utils_1.TimeTypes.DOY_TIME)) {
202
+ const { isNegative, days, hours, minutes, seconds, milliseconds } = (0, aerie_time_utils_1.getDurationTimeComponents)((0, aerie_time_utils_1.parseDurationString)(timeValue, 'seconds'));
203
+ const signed = timeValue.charAt(0) === '-' || timeValue.charAt(0) === '+';
204
+ return {
205
+ tag: `${isNegative}${days}${hours}:${minutes}:${seconds}${milliseconds}`,
206
+ type: signed ? 'EPOCH' : 'FROM_PREVIOUS_START',
207
+ };
208
+ }
209
+ else if ((0, aerie_time_utils_1.validateTime)(timeValue, aerie_time_utils_1.TimeTypes.SECOND_TIME)) {
210
+ const signed = timeValue.charAt(0) === '-' || timeValue.charAt(0) === '+';
211
+ let balancedTime = (0, aerie_time_utils_1.getBalancedDuration)(timeValue);
212
+ if ((0, aerie_time_utils_1.parseDurationString)(balancedTime, 'seconds').milliseconds === 0) {
213
+ balancedTime = balancedTime.slice(0, -4);
214
+ }
215
+ return { tag: balancedTime, type: signed ? 'EPOCH' : 'FROM_PREVIOUS_START' };
216
+ }
217
+ }
218
+ return { tag, type: 'UNKNOWN' };
219
+ }
220
+ function parseSeqNDescription(node, text) {
221
+ const descriptionNode = node.getChild(seqn_grammar_constants_1.SEQN_NODES.LINE_COMMENT);
222
+ if (!descriptionNode) {
223
+ return undefined;
224
+ }
225
+ // +1 offset to drop '#' prefix
226
+ const description = text.slice(descriptionNode.from + 1, descriptionNode.to).trim();
227
+ return (0, string_1.removeEscapedQuotes)(description);
228
+ }
229
+ function parseSeqNArgs(argsNode, sequence, commandDictionary, variables, stem) {
230
+ var _a, _b, _c;
231
+ const args = [];
232
+ let argNode = argsNode.firstChild;
233
+ const dictArguments = (_b = (_a = commandDictionary === null || commandDictionary === void 0 ? void 0 : commandDictionary.fswCommandMap[stem]) === null || _a === void 0 ? void 0 : _a.arguments) !== null && _b !== void 0 ? _b : [];
234
+ let i = 0;
235
+ while (argNode) {
236
+ const dictionaryArg = (_c = dictArguments[i]) !== null && _c !== void 0 ? _c : null;
237
+ const arg = parseSeqNArg(argNode, sequence, dictionaryArg, variables);
238
+ if (arg !== undefined) {
239
+ args.push(arg);
240
+ }
241
+ argNode = argNode === null || argNode === void 0 ? void 0 : argNode.nextSibling;
242
+ i++;
243
+ }
244
+ return args;
245
+ }
246
+ function parseSeqNArg(argNode, sequence, dictionaryArg, variables) {
247
+ const nodeValue = sequence.slice(argNode.from, argNode.to);
248
+ if (variables.includes(nodeValue)) {
249
+ return {
250
+ name: undefined,
251
+ type: 'string',
252
+ value: `"${nodeValue}"`,
253
+ };
254
+ }
255
+ switch (argNode.name) {
256
+ case seqn_grammar_constants_1.SEQN_NODES.BOOLEAN: {
257
+ return {
258
+ name: dictionaryArg ? dictionaryArg.name : undefined,
259
+ type: 'boolean',
260
+ value: nodeValue === 'true' ? 'TRUE' : 'FALSE',
261
+ };
262
+ }
263
+ case seqn_grammar_constants_1.SEQN_NODES.ENUM: {
264
+ return {
265
+ name: dictionaryArg ? dictionaryArg.name : undefined,
266
+ type: 'enum',
267
+ value: nodeValue,
268
+ };
269
+ }
270
+ case seqn_grammar_constants_1.SEQN_NODES.NUMBER: {
271
+ const decimalCount = nodeValue.slice(nodeValue.indexOf('.') + 1).length;
272
+ return {
273
+ name: dictionaryArg ? dictionaryArg.name : undefined,
274
+ type: 'number',
275
+ value: parseFloat(nodeValue).toFixed(decimalCount),
276
+ };
277
+ }
278
+ case seqn_grammar_constants_1.SEQN_NODES.STRING: {
279
+ return {
280
+ name: dictionaryArg ? dictionaryArg.name : undefined,
281
+ type: 'string',
282
+ value: nodeValue,
283
+ };
284
+ }
285
+ default: {
286
+ break;
287
+ }
288
+ }
289
+ }
290
+ function serializeSeqNArgs(args) {
291
+ return args
292
+ .map(arg => {
293
+ return `${arg.value}`;
294
+ })
295
+ .join(', ');
296
+ }
297
+ function getSatfVariableNames(seqnTree, text) {
298
+ let types = [seqn_grammar_constants_1.SEQN_NODES.PARAMETER_DECLARATION, seqn_grammar_constants_1.SEQN_NODES.LOCAL_DECLARATION];
299
+ let names = [];
300
+ for (let i = 0; i < types.length; i++) {
301
+ let variableContainer = seqnTree.topNode.getChild(types[i]);
302
+ if (!variableContainer) {
303
+ continue;
304
+ }
305
+ const variables = variableContainer.getChildren(seqn_grammar_constants_1.SEQN_NODES.VARIABLE);
306
+ if (!variables || variables.length === 0) {
307
+ continue;
308
+ }
309
+ variables.forEach((variableNode) => {
310
+ const nameNode = variableNode.getChild(seqn_grammar_constants_1.SEQN_NODES.ENUM);
311
+ if (nameNode) {
312
+ names.push(text.slice(nameNode.from, nameNode.to));
313
+ }
314
+ });
315
+ }
316
+ return names;
317
+ }
318
+ function satfVariablesFromSeqn(seqnTree, text, type = 'Parameters') {
319
+ let nType = 'ParameterDeclaration';
320
+ if (type === 'Variables') {
321
+ nType = 'LocalDeclaration';
322
+ }
323
+ const variableContainer = seqnTree.topNode.getChild(nType);
324
+ if (!variableContainer) {
325
+ return undefined;
326
+ }
327
+ const variables = variableContainer.getChildren(seqn_grammar_constants_1.SEQN_NODES.VARIABLE);
328
+ if (!variables || variables.length === 0) {
329
+ return undefined;
330
+ }
331
+ const serializedVariables = variables
332
+ .map((variableNode) => {
333
+ const nameNode = variableNode.getChild(seqn_grammar_constants_1.SEQN_NODES.ENUM);
334
+ const typeNode = variableNode.getChild(seqn_grammar_constants_1.SEQN_NODES.TYPE);
335
+ const enumNode = variableNode.getChild(seqn_grammar_constants_1.SEQN_NODES.ENUM_NAME);
336
+ const rangeNode = variableNode.getChild(seqn_grammar_constants_1.SEQN_NODES.RANGE);
337
+ const allowableValuesNode = variableNode.getChild(seqn_grammar_constants_1.SEQN_NODES.VALUES);
338
+ const objects = variableNode.getChildren(seqn_grammar_constants_1.SEQN_NODES.OBJECT);
339
+ const variableText = nameNode ? text.slice(nameNode.from, nameNode.to) : 'UNKNOWN';
340
+ const variable = { name: variableText, type: 'INT' };
341
+ if (typeNode) {
342
+ variable.type = text.slice(typeNode.from, typeNode.to);
343
+ if (enumNode) {
344
+ variable.enum_name = text.slice(enumNode.from, enumNode.to);
345
+ }
346
+ if (rangeNode) {
347
+ const allowableRanges = parseAllowableRanges(text, rangeNode);
348
+ if (allowableRanges && allowableRanges.length > 0) {
349
+ variable.allowable_ranges = allowableRanges;
350
+ }
351
+ }
352
+ if (allowableValuesNode) {
353
+ const allowableValues = parseAllowableValues(text, allowableValuesNode);
354
+ if (allowableValues && allowableValues.length > 0) {
355
+ variable.allowable_values = allowableValues;
356
+ }
357
+ }
358
+ // old style for parameters and variables
359
+ }
360
+ else {
361
+ for (const object of objects) {
362
+ const properties = object.getChildren(seqn_grammar_constants_1.SEQN_NODES.PROPERTY);
363
+ properties.forEach(property => {
364
+ const propertyName = property.getChild(seqn_grammar_constants_1.SEQN_NODES.PROPERTY_NAME);
365
+ const propertyValue = propertyName === null || propertyName === void 0 ? void 0 : propertyName.nextSibling;
366
+ if (!propertyName || !propertyValue) {
367
+ return;
368
+ }
369
+ const propertyNameString = text.slice(propertyName === null || propertyName === void 0 ? void 0 : propertyName.from, propertyName === null || propertyName === void 0 ? void 0 : propertyName.to).replaceAll('"', '');
370
+ const propertyValueString = text.slice(propertyValue === null || propertyValue === void 0 ? void 0 : propertyValue.from, propertyValue === null || propertyValue === void 0 ? void 0 : propertyValue.to).replaceAll('"', '');
371
+ switch (propertyNameString.toLowerCase()) {
372
+ case 'allowable_ranges': {
373
+ if (!propertyValue) {
374
+ break;
375
+ }
376
+ const allowableRanges = parseAllowableRanges(text, propertyValue);
377
+ if (allowableRanges && allowableRanges.length > 0) {
378
+ variable.allowable_ranges = allowableRanges;
379
+ }
380
+ break;
381
+ }
382
+ case 'allowable_values':
383
+ {
384
+ if (!propertyValue) {
385
+ break;
386
+ }
387
+ const allowableValues = parseAllowableValues(text, propertyValue);
388
+ if (allowableValues && allowableValues.length > 0) {
389
+ variable.allowable_values = allowableValues;
390
+ }
391
+ }
392
+ break;
393
+ case 'enum_name':
394
+ variable.enum_name = propertyValueString;
395
+ break;
396
+ case 'sc_name':
397
+ variable.sc_name = propertyValueString;
398
+ break;
399
+ case 'type':
400
+ variable.type = propertyValueString;
401
+ break;
402
+ }
403
+ });
404
+ }
405
+ }
406
+ //convert seqn type to satf type
407
+ switch (variable.type) {
408
+ case seqn_grammar_constants_1.SEQN_NODES.VAR_UINT:
409
+ variable.type = satf_sasf_constants_js_1.SATF_SASF_NODES.PARAM_UNSIGNED_DECIMAL;
410
+ break;
411
+ case seqn_grammar_constants_1.SEQN_NODES.VAR_INT:
412
+ variable.type = satf_sasf_constants_js_1.SATF_SASF_NODES.PARAM_SIGNED_DECIMAL;
413
+ break;
414
+ case seqn_grammar_constants_1.SEQN_NODES.VAR_STRING:
415
+ variable.type = satf_sasf_constants_js_1.SATF_SASF_NODES.PARAM_QUOTED_STRING;
416
+ break;
417
+ case seqn_grammar_constants_1.SEQN_NODES.VAR_FLOAT:
418
+ break;
419
+ case seqn_grammar_constants_1.SEQN_NODES.VAR_ENUM:
420
+ variable.type = satf_sasf_constants_js_1.SATF_SASF_NODES.PARAM_STRING;
421
+ break;
422
+ }
423
+ return (`\t${variable.name}` +
424
+ `(\n\t\tTYPE,${variable.type}${variable.enum_name ? `\n\t\t\ENUM_NAME,${variable.enum_name}` : ''}` +
425
+ `${variable.allowable_ranges
426
+ ? variable.allowable_ranges
427
+ .map(range => {
428
+ return `\n\t\tRANGES,\\${range.min}...${range.max}\\`;
429
+ })
430
+ .join(',')
431
+ : ''}` +
432
+ `${variable.allowable_values ? `\n\t\t\RANGES,\\${variable.allowable_values}\\` : ''}` +
433
+ `${variable.sc_name ? `\n\t\tSC_NAME,${variable.sc_name}` : ''}` +
434
+ `\n\t)`);
435
+ })
436
+ .join(',\n');
437
+ return `${type.toUpperCase()},\n ${serializedVariables},\nend,\n`;
438
+ }
439
+ function parseAllowableRanges(text, rangeNode) {
440
+ if (!rangeNode) {
441
+ return [];
442
+ }
443
+ return text
444
+ .slice(rangeNode.from, rangeNode.to)
445
+ .split(',')
446
+ .map(range => {
447
+ const rangeMatch = /^(?<min>[-+]?\d+(\.\d*)?)?(\.\.\.)(?<max>[-+]?\d+(\.\d*)?)?$/.exec(range.replaceAll('"', '').trim());
448
+ if (rangeMatch && rangeMatch.groups) {
449
+ const { min, max } = rangeMatch.groups;
450
+ const maxNum = !isNaN(Number(max)) ? Number(max) : Infinity;
451
+ const minNum = !isNaN(Number(min)) ? Number(min) : -Infinity;
452
+ return { max: maxNum, min: minNum };
453
+ }
454
+ return undefined;
455
+ })
456
+ .filter(range => range !== undefined);
457
+ }
458
+ function parseAllowableValues(text, allowableValuesNode) {
459
+ const allowableValues = text
460
+ .slice(allowableValuesNode.from + 1, allowableValuesNode.to - 1)
461
+ .split(',')
462
+ .map(value => value.trim());
463
+ return allowableValues.length > 0 ? allowableValues : undefined;
464
+ }
465
+ function sasfRequestFromSeqN(seqnTree, sequence, variables = [], commandDictionary) {
466
+ var _a;
467
+ const requests = (_a = seqnTree.topNode.getChild(seqn_grammar_constants_1.SEQN_NODES.COMMANDS)) === null || _a === void 0 ? void 0 : _a.getChildren(seqn_grammar_constants_1.SEQN_NODES.REQUEST);
468
+ if (requests == null || requests.length === 0) {
469
+ return undefined;
470
+ }
471
+ return requests
472
+ .map((requestNode) => {
473
+ var _a, _b;
474
+ const nameNode = requestNode.getChild(seqn_grammar_constants_1.SEQN_NODES.REQUEST_NAME);
475
+ const name = nameNode ? (0, string_1.unquoteUnescape)(sequence.slice(nameNode.from, nameNode.to)) : 'UNKNOWN';
476
+ const parsedTime = parseSeqNTime(requestNode, sequence);
477
+ const requester = parsSeqNMetadata(requestNode, sequence, ['REQUESTOR']);
478
+ const processor = parsSeqNMetadata(requestNode, sequence, ['PROCESSOR']);
479
+ const key = parsSeqNMetadata(requestNode, sequence, ['KEY']);
480
+ const request = `request(${name},` +
481
+ `\n\tSTART_TIME, ${parsedTime.tag},` +
482
+ `${requester ? `\n\t${requester.replaceAll('\\', '\"')}` : ''},` +
483
+ `${processor ? `\n\t${processor.replaceAll('\\', '\"')}` : ''},` +
484
+ `${key ? `\n\t${key.replaceAll('\\', '\"')}` : ''})`;
485
+ `\n\n`;
486
+ let order = 1;
487
+ let child = (_a = requestNode === null || requestNode === void 0 ? void 0 : requestNode.getChild(seqn_grammar_constants_1.SEQN_NODES.STEPS)) === null || _a === void 0 ? void 0 : _a.firstChild;
488
+ const steps = [];
489
+ while (child) {
490
+ steps.push(`\t${(_b = parseSeqNStep(child, sequence, commandDictionary, variables, order++)) === null || _b === void 0 ? void 0 : _b.replaceAll('\n', '\n\t')}`);
491
+ child = child === null || child === void 0 ? void 0 : child.nextSibling;
492
+ }
493
+ return `${request}\n${steps.join('\n')}\nend;\n`;
494
+ })
495
+ .join(',\n');
496
+ }
497
+ /**
498
+ * Parses a SATF formatted string asynchronously to extract header information and sequence data.
499
+ * It utilizes the SatfLanguage parser to generate SeqN parts.
500
+ *
501
+ * @async
502
+ * @function satfToSeqn
503
+ * @param {string} satf - The SATF or SASF formatted string content to parse.
504
+ * @param {string[]} [globalVariables] - Optional. A list of predefined global variable names to be used
505
+ * during the parsing of steps
506
+ * @returns {Promise<ParsedSequence>} A Promise that resolves to an object containing the parsed header
507
+ * and an array of sequence objects.
508
+ * If the input string does not contain a top-level SATF structure recognized by the parser,
509
+ * it resolves with a default empty ParsedSequence object (e.g., { header: "", sequences: [] }).
510
+ */
511
+ async function satfToSeqn(satf, globalVariables) {
512
+ const base = satf_sasf_js_1.SatfLanguage.parser.parse(satf).topNode;
513
+ const satfNode = base.getChild(satf_sasf_constants_js_1.SATF_SASF_NODES.SATF);
514
+ if (!satfNode) {
515
+ return { metadata: '', sequences: [] };
516
+ }
517
+ const metadata = parseHeader(satfNode.getChild(satf_sasf_constants_js_1.SATF_SASF_NODES.HEADER), satf);
518
+ const sequences = parseBody(satfNode.getChild(satf_sasf_constants_js_1.SATF_SASF_NODES.BODY), globalVariables, satf);
519
+ return { metadata, sequences };
520
+ }
521
+ async function sasfToSeqn(sasf, globalVariables) {
522
+ const base = satf_sasf_js_1.SatfLanguage.parser.parse(sasf).topNode;
523
+ const sasfNode = base.getChild(satf_sasf_constants_js_1.SATF_SASF_NODES.SASF);
524
+ if (!sasfNode) {
525
+ return { metadata: '', sequences: [] };
526
+ }
527
+ const metadata = parseHeader(sasfNode.getChild(satf_sasf_constants_js_1.SATF_SASF_NODES.HEADER), sasf);
528
+ const sequences = parseBody(sasfNode.getChild(satf_sasf_constants_js_1.SATF_SASF_NODES.BODY), globalVariables, sasf);
529
+ return { metadata, sequences };
530
+ }
531
+ function parseHeader(headerNode, text) {
532
+ var _a, _b, _c;
533
+ const header = '';
534
+ if (!headerNode) {
535
+ return header;
536
+ }
537
+ const sfduHeader = (_c = (_b = (_a = headerNode
538
+ .getChild(satf_sasf_constants_js_1.SATF_SASF_NODES.SFDU_HEADER)) === null || _a === void 0 ? void 0 : _a.getChild(satf_sasf_constants_js_1.SATF_SASF_NODES.HEADER_PAIRS)) === null || _b === void 0 ? void 0 : _b.getChildren(satf_sasf_constants_js_1.SATF_SASF_NODES.HEADER_PAIR)) !== null && _c !== void 0 ? _c : [];
539
+ return sfduHeader
540
+ .map((pairNode) => {
541
+ const keyNode = pairNode.getChild(satf_sasf_constants_js_1.SATF_SASF_NODES.KEY);
542
+ const valueNode = pairNode.getChild(satf_sasf_constants_js_1.SATF_SASF_NODES.VALUE);
543
+ if (!keyNode || !valueNode) {
544
+ console.error(`Error processing header entry: ${text.slice(pairNode.from, pairNode.to)}`);
545
+ return '';
546
+ }
547
+ const key = text.slice(keyNode.from, keyNode.to).trim();
548
+ const value = text.slice(valueNode.from, valueNode.to).trim();
549
+ if (key.length === 0 || value.length === 0) {
550
+ return '';
551
+ }
552
+ return `@METADATA "${key}" "${value}"`;
553
+ })
554
+ .join('\n');
555
+ }
556
+ function parseBody(bodyNode, globalVariables = [], text) {
557
+ var _a, _b, _c, _d;
558
+ if (!bodyNode) {
559
+ return [];
560
+ }
561
+ //satf
562
+ if (bodyNode.getChild(satf_sasf_constants_js_1.SATF_SASF_NODES.ACTIVITY_TYPE_DEFINITIONS)) {
563
+ const activityTypeNodes = (_b = (_a = bodyNode.getChild(satf_sasf_constants_js_1.SATF_SASF_NODES.ACTIVITY_TYPE_DEFINITIONS)) === null || _a === void 0 ? void 0 : _a.getChildren(satf_sasf_constants_js_1.SATF_SASF_NODES.ACTIVITY_TYPE_GROUP)) !== null && _b !== void 0 ? _b : [];
564
+ return activityTypeNodes.map((group, i) => {
565
+ let sequenceName = 'sequence-' + i;
566
+ const sequenceNameNode = group.getChild(satf_sasf_constants_js_1.SATF_SASF_NODES.ACTIVITY_TYPE_NAME);
567
+ const seqGenNode = group.getChild(satf_sasf_constants_js_1.SATF_SASF_NODES.SEQGEN);
568
+ const vcNode = group.getChild(satf_sasf_constants_js_1.SATF_SASF_NODES.VIRTUAL_CHANNEL);
569
+ const onBoardFilenameNode = group.getChild(satf_sasf_constants_js_1.SATF_SASF_NODES.ON_BOARD_FILENAME);
570
+ const onBoardFilePathNode = group.getChild(satf_sasf_constants_js_1.SATF_SASF_NODES.ON_BOARD_PATH);
571
+ if (sequenceNameNode) {
572
+ const name = text.slice(sequenceNameNode.from, sequenceNameNode.to);
573
+ sequenceName = name.split('/').pop() || 'sequence-' + i;
574
+ }
575
+ const inputParameters = parseParameters(group.getChild(satf_sasf_constants_js_1.SATF_SASF_NODES.PARAMETERS), 'INPUT_PARAMS', text);
576
+ const localVariables = parseParameters(group.getChild(satf_sasf_constants_js_1.SATF_SASF_NODES.VARIABLES), 'LOCALS', text);
577
+ let metadata = '';
578
+ if (vcNode) {
579
+ metadata += `@METADATA "VIRTUAL_CHANNEL" "${text.slice(vcNode.from, vcNode.to)}"\n`;
580
+ }
581
+ if (onBoardFilenameNode) {
582
+ metadata += `@METADATA "ON_BOARD_FILENAME" "${text.slice(onBoardFilenameNode.from, onBoardFilenameNode.to)}"\n`;
583
+ }
584
+ if (onBoardFilePathNode) {
585
+ metadata += `@METADATA "ON_BOARD_PATH" "${text.slice(onBoardFilePathNode.from, onBoardFilePathNode.to)}"\n`;
586
+ }
587
+ if (seqGenNode) {
588
+ metadata += `@METADATA "SEQGEN" "${text.slice(seqGenNode.from, seqGenNode.to)}"\n`;
589
+ }
590
+ metadata = metadata.trimEnd();
591
+ const steps = parseSteps(group.getChild(satf_sasf_constants_js_1.SATF_SASF_NODES.STEPS), [
592
+ ...parseVariableName(group.getChild(satf_sasf_constants_js_1.SATF_SASF_NODES.PARAMETERS), text),
593
+ ...parseVariableName(group.getChild(satf_sasf_constants_js_1.SATF_SASF_NODES.VARIABLES), text),
594
+ ...globalVariables,
595
+ ], text);
596
+ return {
597
+ name: sequenceName,
598
+ metadata,
599
+ inputParameters,
600
+ localVariables,
601
+ steps,
602
+ };
603
+ });
604
+ }
605
+ //sasf
606
+ if (bodyNode.getChild(satf_sasf_constants_js_1.SATF_SASF_NODES.REQUESTS)) {
607
+ const requestNodes = (_d = (_c = bodyNode.getChild(satf_sasf_constants_js_1.SATF_SASF_NODES.REQUESTS)) === null || _c === void 0 ? void 0 : _c.getChildren(satf_sasf_constants_js_1.SATF_SASF_NODES.REQUEST)) !== null && _d !== void 0 ? _d : [];
608
+ return requestNodes.map((group, i) => {
609
+ let requests = '';
610
+ const requestNameNode = group.getChild(satf_sasf_constants_js_1.SATF_SASF_NODES.REQUEST_NAME);
611
+ const requestorNode = group.getChild(satf_sasf_constants_js_1.SATF_SASF_NODES.REQUESTOR);
612
+ const processorNode = group.getChild(satf_sasf_constants_js_1.SATF_SASF_NODES.PROCESSOR);
613
+ const keyNode = group.getChild(satf_sasf_constants_js_1.SATF_SASF_NODES.KEY);
614
+ const startTimeNode = group.getChild(satf_sasf_constants_js_1.SATF_SASF_NODES.START_TIME);
615
+ const sequenceName = requestNameNode ? text.slice(requestNameNode.from, requestNameNode.to) : 'sequence-' + i;
616
+ requests += parseTimeTagNode(startTimeNode ? startTimeNode.getChild(satf_sasf_constants_js_1.SATF_SASF_NODES.TIME) : null, startTimeNode ? startTimeNode.getChild(satf_sasf_constants_js_1.SATF_SASF_NODES.TIME_RELATION) : null, text);
617
+ requests += `@REQUEST_BEGIN("${sequenceName}")\n`;
618
+ requests += parseSteps(group.getChild(satf_sasf_constants_js_1.SATF_SASF_NODES.STEPS), globalVariables, text)
619
+ .split('\n')
620
+ .map(line => ' '.repeat(2) + line)
621
+ .join('\n');
622
+ requests += `\n@REQUEST_END\n`;
623
+ if (requestorNode) {
624
+ requests += `@METADATA "REQUESTOR" "${(0, string_1.removeQuote)(text.slice(requestorNode.from, requestorNode.to))}"\n`;
625
+ }
626
+ if (processorNode) {
627
+ requests += `@METADATA "PROCESSOR" "${(0, string_1.removeQuote)(text.slice(processorNode.from, processorNode.to))}"\n`;
628
+ }
629
+ if (keyNode) {
630
+ requests += `@METADATA "KEY" "${(0, string_1.removeQuote)(text.slice(keyNode.from, keyNode.to))}"\n`;
631
+ }
632
+ return { name: sequenceName, requests };
633
+ });
634
+ }
635
+ return [];
636
+ }
637
+ function parseVariableName(parameterNode, text) {
638
+ if (!parameterNode) {
639
+ return [];
640
+ }
641
+ const entries = parameterNode.getChildren(satf_sasf_constants_js_1.SATF_SASF_NODES.ENTRY);
642
+ if (!entries || entries.length == 0) {
643
+ return [];
644
+ }
645
+ return entries.map(param => {
646
+ const nameNode = param.getChild(satf_sasf_constants_js_1.SATF_SASF_NODES.NAME);
647
+ return nameNode ? `${text.slice(nameNode.from, nameNode.to)}` : '';
648
+ });
649
+ }
650
+ /** Mapping between SATF and SeqN from Taifun
651
+ UNSIGNED_DECIMAL -> UINT
652
+ SIGNED_DECIMAL -> INT
653
+ HEXADECIMAL -> STRING
654
+ OCTAL -> STRING
655
+ BINARY -> STRING
656
+ ENGINEERING -> FLOAT
657
+ TIME -> STRING
658
+ DURATION -> STRING
659
+ STRING -> STRING | ENUM (if enum_name is defined)
660
+ QUOATED_STRING -> STRING
661
+ NOTE: HEXADECIMAL, OCTAL, BINARY are all uint in seqgen
662
+ */
663
+ function parseParameters(parameterNode, variableType, text) {
664
+ if (!parameterNode) {
665
+ return '';
666
+ }
667
+ const entries = parameterNode.getChildren(satf_sasf_constants_js_1.SATF_SASF_NODES.ENTRY);
668
+ if (entries && entries.length > 0) {
669
+ let parameter = `@${variableType}_BEGIN\n`;
670
+ parameter += entries
671
+ .map(param => {
672
+ const nameNode = param.getChild(satf_sasf_constants_js_1.SATF_SASF_NODES.NAME);
673
+ const typeNode = param.getChild(satf_sasf_constants_js_1.SATF_SASF_NODES.TYPE);
674
+ const rangesNode = param.getChildren(satf_sasf_constants_js_1.SATF_SASF_NODES.RANGE);
675
+ const enumNameNode = param.getChild(satf_sasf_constants_js_1.SATF_SASF_NODES.ENUM_NAME);
676
+ const name = nameNode ? `${text.slice(nameNode.from, nameNode.to)}` : '';
677
+ const enumName = enumNameNode ? ` ${text.slice(enumNameNode.from, enumNameNode.to)}` : '';
678
+ let type = typeNode ? text.slice(typeNode.from, typeNode.to).trim() : '';
679
+ switch (type) {
680
+ case satf_sasf_constants_js_1.SATF_SASF_NODES.PARAM_UNSIGNED_DECIMAL:
681
+ type = seqn_grammar_constants_1.SEQN_NODES.VAR_UINT;
682
+ break;
683
+ case satf_sasf_constants_js_1.SATF_SASF_NODES.PARAM_SIGNED_DECIMAL:
684
+ type = seqn_grammar_constants_1.SEQN_NODES.VAR_INT;
685
+ break;
686
+ case satf_sasf_constants_js_1.SATF_SASF_NODES.PARAM_HEXADECIMAL:
687
+ case satf_sasf_constants_js_1.SATF_SASF_NODES.PARAM_OCTAL:
688
+ case satf_sasf_constants_js_1.SATF_SASF_NODES.PARAM_BINARY:
689
+ case satf_sasf_constants_js_1.SATF_SASF_NODES.PARAM_TIME:
690
+ case satf_sasf_constants_js_1.SATF_SASF_NODES.PARAM_DURATION:
691
+ case satf_sasf_constants_js_1.SATF_SASF_NODES.PARAM_QUOTED_STRING:
692
+ type = seqn_grammar_constants_1.SEQN_NODES.VAR_STRING;
693
+ break;
694
+ case satf_sasf_constants_js_1.SATF_SASF_NODES.PARAM_ENGINEERING:
695
+ type = seqn_grammar_constants_1.SEQN_NODES.VAR_FLOAT;
696
+ break;
697
+ case seqn_grammar_constants_1.SEQN_NODES.VAR_STRING:
698
+ {
699
+ // Always an enum matches the jpl_sequence tool
700
+ //if (enumNameNode) {
701
+ type = seqn_grammar_constants_1.SEQN_NODES.VAR_ENUM;
702
+ //} else {
703
+ // type = VAR_STRING;
704
+ //}
705
+ }
706
+ break;
707
+ default:
708
+ console.log(`type: ${type} is not supported`);
709
+ }
710
+ const allowableValues = [];
711
+ const allowableRanges = [];
712
+ rangesNode.forEach((range) => {
713
+ text
714
+ .slice(range.from, range.to)
715
+ .split(',')
716
+ .forEach(r => {
717
+ r = r.replaceAll('"', '').trim();
718
+ if (r.includes('...')) {
719
+ allowableRanges.push(r);
720
+ }
721
+ else {
722
+ allowableValues.push(r);
723
+ }
724
+ });
725
+ });
726
+ return `${name} ${type}${enumName}${allowableRanges.length === 0 ? (allowableValues.length === 0 ? '' : ' ""') : ` "${allowableRanges.join(', ')}"`}${allowableValues.length === 0 ? '' : ` "${allowableValues.join(', ')}"`}`;
727
+ })
728
+ .join('\n');
729
+ parameter += `\n@${variableType}_END`;
730
+ return parameter;
731
+ }
732
+ return '';
733
+ }
734
+ function parseSteps(stepNode, variableNames, text) {
735
+ const step = '';
736
+ if (!stepNode) {
737
+ return step;
738
+ }
739
+ const commandNodes = stepNode.getChildren(satf_sasf_constants_js_1.SATF_SASF_NODES.COMMAND);
740
+ return commandNodes
741
+ .map(command => {
742
+ const time = parseTimeNode(command.getChild(satf_sasf_constants_js_1.SATF_SASF_NODES.SCHEDULED_TIME), text);
743
+ const stem = parseStem(command.getChild(satf_sasf_constants_js_1.SATF_SASF_NODES.STEM), text);
744
+ const comment = parseComment(command.getChild(satf_sasf_constants_js_1.SATF_SASF_NODES.COMMENT), text);
745
+ const args = parseArgsNode(command.getChild(satf_sasf_constants_js_1.SATF_SASF_NODES.ARGS), variableNames, text);
746
+ const models = parseModel(command.getChild(satf_sasf_constants_js_1.SATF_SASF_NODES.ASSUMED_MODEL_VALUES), text);
747
+ const metadata = parseSatfCommandMetadata(command, text);
748
+ //metadata
749
+ return `${time}${stem}${args.length > 0 ? ` ${args}` : ''}${comment.length > 0 ? ` ${comment}` : ''}${metadata.length > 0 ? `\n${metadata}` : ''}${models.length > 0 ? `\n${models}` : ''}`;
750
+ })
751
+ .join('\n');
752
+ }
753
+ function parseTimeNode(timeNode, text) {
754
+ if (!timeNode) {
755
+ return 'C ';
756
+ }
757
+ const timeValueNode = timeNode.getChild(satf_sasf_constants_js_1.SATF_SASF_NODES.TIME);
758
+ const timeTagNode = timeNode.getChild(satf_sasf_constants_js_1.SATF_SASF_NODES.TIME_RELATION);
759
+ return parseTimeTagNode(timeValueNode, timeTagNode, text);
760
+ }
761
+ function parseTimeTagNode(timeValueNode, timeTagNode, text) {
762
+ if (timeValueNode && !timeTagNode) {
763
+ return `A${text.slice(timeValueNode.from, timeValueNode.to)} `;
764
+ }
765
+ else if (!timeValueNode || !timeTagNode) {
766
+ return `R00:00:00`;
767
+ }
768
+ const time = text.slice(timeValueNode.from, timeValueNode.to);
769
+ const timeTag = text.slice(timeTagNode.from, timeTagNode.to);
770
+ switch (timeTag.trim()) {
771
+ case 'ABSOLUTE':
772
+ return `A${time} `;
773
+ case 'EPOCH':
774
+ return `E${time} `;
775
+ case 'FROM_PREVIOUS_START':
776
+ return `R${time} `;
777
+ case 'FROM_REQUEST_START':
778
+ case 'FROM_ACTIVITY_START':
779
+ return `B${time} `;
780
+ case 'WAIT_PREVIOUS_END':
781
+ return `C `;
782
+ default:
783
+ return 'error';
784
+ }
785
+ }
786
+ function parseComment(commentNode, text) {
787
+ let comment = commentNode
788
+ ? `${text
789
+ .slice(commentNode.from, commentNode.to)
790
+ .split('\n')
791
+ .map(line => line.trim())
792
+ .join(' ')
793
+ .trim()}` // flatten comment to one line SeqN doesn't support multi-line comments on a command
794
+ : '';
795
+ if (comment.length === 0) {
796
+ return comment;
797
+ }
798
+ return `# ${(0, string_1.removeQuote)(comment)}`;
799
+ }
800
+ function parseStem(stemNode, text) {
801
+ return stemNode ? text.slice(stemNode.from, stemNode.to) : '';
802
+ }
803
+ function parseArgsNode(argsNode, variableNames, text) {
804
+ if (!argsNode) {
805
+ return '';
806
+ }
807
+ let argNode = argsNode.firstChild;
808
+ const args = [];
809
+ while (argNode) {
810
+ args.push(`${parseArgNode(argNode, variableNames, text)}`);
811
+ argNode = argNode === null || argNode === void 0 ? void 0 : argNode.nextSibling;
812
+ }
813
+ return args.join(' ');
814
+ }
815
+ function parseArgNode(argNode, variableNames, text) {
816
+ if (!argNode) {
817
+ return '';
818
+ }
819
+ const argValue = (0, string_1.removeQuote)(text.slice(argNode.from, argNode.to));
820
+ if (variableNames.includes(argValue)) {
821
+ return argValue;
822
+ }
823
+ switch (argNode.name) {
824
+ case satf_sasf_constants_js_1.SATF_SASF_NODES.STRING:
825
+ return `"${argValue}"`;
826
+ case satf_sasf_constants_js_1.SATF_SASF_NODES.NUMBER:
827
+ case satf_sasf_constants_js_1.SATF_SASF_NODES.BOOLEAN:
828
+ case satf_sasf_constants_js_1.SATF_SASF_NODES.ENUM:
829
+ case satf_sasf_constants_js_1.SATF_SASF_NODES.GLOBAL:
830
+ return `${argValue}`;
831
+ case satf_sasf_constants_js_1.SATF_SASF_NODES.ARITHMETICAL:
832
+ return `(${argValue})`;
833
+ default: {
834
+ console.log(`${argNode.name}: ${argValue} is not supported`);
835
+ return 'Error';
836
+ }
837
+ }
838
+ }
839
+ function parsSeqNMetadata(node, text, filter) {
840
+ const metadataNode = node.getChild('Metadata');
841
+ if (!metadataNode) {
842
+ return undefined;
843
+ }
844
+ const metadataEntry = metadataNode.getChildren('MetaEntry');
845
+ if (!metadataEntry || metadataEntry.length === 0) {
846
+ return undefined;
847
+ }
848
+ const obj = [];
849
+ metadataEntry.forEach(entry => {
850
+ const keyNode = entry.getChild('Key');
851
+ const valueNode = entry.getChild('Value');
852
+ if (!keyNode || !valueNode) {
853
+ return; // Skip this entry if either the key or value is missing
854
+ }
855
+ const keyText = (0, string_1.unquoteUnescape)(text.slice(keyNode.from, keyNode.to));
856
+ if (!filter.includes(keyText)) {
857
+ return;
858
+ }
859
+ let value = text.slice(valueNode.from, valueNode.to);
860
+ try {
861
+ value = JSON.parse(value);
862
+ }
863
+ catch (e) {
864
+ console.log(`Malformed metadata ${value}`);
865
+ }
866
+ obj.push(`${keyText.toUpperCase()},\\${value}\\`);
867
+ });
868
+ return obj.join(`,\n${'\t'.repeat(2)}`);
869
+ }
870
+ function parseSatfCommandMetadata(commandNode, text) {
871
+ let metadata = '';
872
+ if (!commandNode) {
873
+ return metadata;
874
+ }
875
+ const inclusionNode = commandNode.getChild(satf_sasf_constants_js_1.SATF_SASF_NODES.INCLUSION_CONDITION);
876
+ const drawNode = commandNode.getChild(satf_sasf_constants_js_1.SATF_SASF_NODES.DRAW);
877
+ const nTextNode = commandNode.getChild(satf_sasf_constants_js_1.SATF_SASF_NODES.NTEXT);
878
+ if (inclusionNode) {
879
+ metadata += `@METADATA "INCLUSION_CONDITION" "${(0, string_1.removeQuote)(text.slice(inclusionNode.from, inclusionNode.to))}"\n`;
880
+ }
881
+ if (drawNode) {
882
+ metadata += `@METADATA "DRAW" "${(0, string_1.removeQuote)(text.slice(drawNode.from, drawNode.to))}"\n`;
883
+ }
884
+ if (nTextNode) {
885
+ metadata += `@METADATA "NTEXT" "${(0, string_1.removeQuote)(text.slice(nTextNode.from, nTextNode.to))}"\n`;
886
+ }
887
+ return metadata.slice(0, -1);
888
+ }
889
+ function parseModel(modelNode, text) {
890
+ if (!modelNode) {
891
+ return '';
892
+ }
893
+ const modelsNode = modelNode.getChildren(satf_sasf_constants_js_1.SATF_SASF_NODES.MODEL);
894
+ return modelsNode
895
+ .map(model => {
896
+ const keyNode = model.getChild(satf_sasf_constants_js_1.SATF_SASF_NODES.KEY);
897
+ const valueNode = model.getChild(satf_sasf_constants_js_1.SATF_SASF_NODES.VALUE);
898
+ if (!keyNode || !valueNode) {
899
+ return null;
900
+ }
901
+ return `@MODEL "${text.slice(keyNode.from, keyNode.to)}" ${text.slice(valueNode.from, valueNode.to)} "00:00:00"`;
902
+ })
903
+ .filter(model => model !== null)
904
+ .join('\n');
905
+ }
906
+ function parseSeqNModel(node, text) {
907
+ const modelContainer = node.getChild('Models');
908
+ if (!modelContainer) {
909
+ return undefined;
910
+ }
911
+ const modelNodes = modelContainer.getChildren('Model');
912
+ if (!modelNodes || modelNodes.length === 0) {
913
+ return undefined;
914
+ }
915
+ const models = [];
916
+ for (const modelNode of modelNodes) {
917
+ const variableNode = modelNode.getChild('Variable');
918
+ const valueNode = modelNode.getChild('Value');
919
+ const variable = variableNode ? (0, string_1.unquoteUnescape)(text.slice(variableNode.from, variableNode.to)) : 'UNKNOWN';
920
+ // Value can be string, number or boolean
921
+ let value;
922
+ const valueChild = valueNode === null || valueNode === void 0 ? void 0 : valueNode.firstChild;
923
+ if (valueChild) {
924
+ const valueText = text.slice(valueChild.from, valueChild.to);
925
+ if (valueChild.name === 'String') {
926
+ value = valueText;
927
+ }
928
+ else if (valueChild.name === 'Boolean') {
929
+ value = !/^FALSE$/i.test(valueText);
930
+ }
931
+ else if (valueChild.name === 'Number') {
932
+ value = Number(valueText);
933
+ }
934
+ }
935
+ models.push(`${variable}=${value}`);
936
+ }
937
+ return models.join(',');
938
+ }
939
+ //# sourceMappingURL=satf-sasf-utils.js.map