tla-ts 0.0.3-dev.a85c0bd → 0.0.3-dev.a91da46

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 (81) hide show
  1. package/dist/cli.cjs +1 -1
  2. package/dist/cli.js +1 -1
  3. package/dist/index.cjs +1 -1
  4. package/dist/index.d.cts +167 -27
  5. package/dist/index.d.ts +167 -27
  6. package/dist/index.js +1 -1
  7. package/dist/parallel-worker.cjs +1 -1
  8. package/dist/parallel-worker.js +2 -2
  9. package/dist/parallel.cjs +1 -1
  10. package/dist/parallel.js +2 -2
  11. package/dist/run-cli.cjs +1351 -20
  12. package/dist/run-cli.js +1351 -20
  13. package/dist/transfer.cjs +83 -28
  14. package/dist/transfer.js +83 -28
  15. package/package.json +5 -1
  16. package/src/ast.ts +52 -6
  17. package/src/bags.ts +4 -7
  18. package/src/builtins.ts +29 -11
  19. package/src/bundled-modules.ts +3 -3
  20. package/src/canonical.ts +4 -3
  21. package/src/check.ts +1332 -262
  22. package/src/cli.ts +8 -1
  23. package/src/codegen.ts +68 -21
  24. package/src/config.ts +252 -179
  25. package/src/environment.ts +431 -120
  26. package/src/errors.ts +195 -7
  27. package/src/evaluate.ts +718 -146
  28. package/src/exit-codes.ts +6 -24
  29. package/src/index.ts +1 -0
  30. package/src/instances.ts +4 -2
  31. package/src/java-overrides.ts +104 -25
  32. package/src/levels.ts +183 -37
  33. package/src/lexer.ts +32 -4
  34. package/src/live-check.ts +204 -148
  35. package/src/liveness.ts +190 -117
  36. package/src/obligation-expression.ts +2 -2
  37. package/src/obligation-source.ts +2 -0
  38. package/src/parallel-worker.ts +6 -1
  39. package/src/parallel.ts +57 -6
  40. package/src/parser.ts +436 -128
  41. package/src/pcal-ast.ts +770 -0
  42. package/src/pcal-parse.ts +1523 -0
  43. package/src/pcal-tokenize.ts +516 -0
  44. package/src/pcal-translate.ts +1230 -0
  45. package/src/pcal-validate.ts +454 -0
  46. package/src/randomization.ts +24 -7
  47. package/src/resolve.ts +1364 -405
  48. package/src/run-cli.ts +1167 -155
  49. package/src/runtime.ts +16 -0
  50. package/src/sany-builtins.ts +147 -0
  51. package/src/sany-generator.ts +3112 -0
  52. package/src/sany-levels.ts +1208 -0
  53. package/src/sany-lexer.ts +545 -0
  54. package/src/sany-linter.ts +122 -0
  55. package/src/sany-modules.ts +1398 -0
  56. package/src/sany-nodes.ts +463 -0
  57. package/src/sany-parse-error.ts +2161 -0
  58. package/src/sany-report.ts +354 -0
  59. package/src/sany-selectors.ts +738 -0
  60. package/src/sany-source.ts +248 -0
  61. package/src/sany-xml.ts +1017 -0
  62. package/src/spec-description.ts +26 -7
  63. package/src/standard-modules.ts +203 -146
  64. package/src/standard-ranges.ts +116 -0
  65. package/src/state-graph.ts +29 -6
  66. package/src/subexpressions.ts +221 -161
  67. package/src/substitution-levels.ts +38 -17
  68. package/src/successors.ts +158 -120
  69. package/src/symmetry.ts +168 -50
  70. package/src/tableau.ts +3 -1
  71. package/src/tlc-ext.ts +13 -17
  72. package/src/tlc-messages.ts +329 -0
  73. package/src/tlc-module.ts +49 -21
  74. package/src/tlc-modules.ts +0 -4
  75. package/src/tlc-order.ts +55 -9
  76. package/src/tlc-random.ts +28 -3
  77. package/src/tlc-replay.ts +655 -0
  78. package/src/tlc-version.ts +18 -0
  79. package/src/transfer.ts +15 -3
  80. package/src/value-sources.ts +29 -0
  81. package/src/values.ts +683 -256
@@ -0,0 +1,1017 @@
1
+ import { TlaJavaException, TlaUnsupportedError } from "./errors";
2
+ import { setExitCode } from "./exit-codes";
3
+ import type { TlcVersion } from "./tlc-version";
4
+ import type { SanySpecification } from "./sany-generator";
5
+ import {
6
+ CONSTANT_DECL_KIND,
7
+ VARIABLE_DECL_KIND,
8
+ getDeclarations,
9
+ getOpDefs,
10
+ formatSanyLocation,
11
+ isAbsent,
12
+ type AssumeNode,
13
+ type FactNode,
14
+ type LevelNode,
15
+ type ModuleNode,
16
+ type OpDeclNode,
17
+ type OpDefNode,
18
+ type OpApplNode,
19
+ type SanyLocation,
20
+ type Subst,
21
+ type SymbolNode,
22
+ type TheoremNode,
23
+ type ProofPartNode,
24
+ type InstanceNode,
25
+ type UseOrHideNode,
26
+ } from "./sany-nodes";
27
+
28
+ // XMLExporterExitCode, and the status Java exits with on an exception that nothing catches.
29
+ export const XML_EXPORT_EXIT_CODE = {
30
+ uncaughtException: 1,
31
+ argsParsingFailure: 1,
32
+ specParsingFailure: 2,
33
+ unrepresentableCharacter: 7,
34
+ } as const;
35
+
36
+ interface XmlElement {
37
+ tag: string;
38
+ text?: string;
39
+ cdata?: string;
40
+ children: XmlElement[];
41
+ }
42
+
43
+ // The exception that SANY's XMLExporter throws for a node of a kind it can't export.
44
+ const getUnsupportedKind = (kind: number) =>
45
+ `java.lang.IllegalArgumentException: unsupported kind: ${kind} in xml export`;
46
+
47
+ const createElement = (tag: string, children: XmlElement[] = []): XmlElement => ({
48
+ tag,
49
+ children,
50
+ });
51
+
52
+ const createText = (tag: string, text: string | number): XmlElement => ({
53
+ tag,
54
+ text: String(text),
55
+ children: [],
56
+ });
57
+
58
+ const XML_ENTITIES: Record<string, string> = {
59
+ "&": "&amp;",
60
+ "<": "&lt;",
61
+ ">": "&gt;",
62
+ "\r": "&#13;",
63
+ };
64
+
65
+ // Java's serializer also writes a character outside the Basic Multilingual Plane, which a Java
66
+ // string holds as two chars, as a character reference.
67
+ const escapeXml = (text: string) =>
68
+ text.replace(/[&<>\r]|[\u{10000}-\u{10FFFF}]/gu, (character) =>
69
+ character.length === 2 ? `&#${character.codePointAt(0)};` : XML_ENTITIES[character],
70
+ );
71
+
72
+ // A character outside XML 1.0's Char production.
73
+ const UNREPRESENTABLE_CHARACTER = /[^\t\n\r\u0020-\uD7FF\uE000-\uFFFD\u{10000}-\u{10FFFF}]/u;
74
+
75
+ interface UnrepresentableCharacter {
76
+ tag: string;
77
+ character: string;
78
+ }
79
+
80
+ // XMLExporter.findUnrepresentableCharacterData: the first element, in document order, whose
81
+ // character data XML 1.0 cannot represent, with the character.
82
+ const findUnrepresentable = (element: XmlElement): UnrepresentableCharacter | undefined => {
83
+ const character = (element.cdata ?? element.text)?.match(UNREPRESENTABLE_CHARACTER)?.[0];
84
+ if (character !== undefined) return { tag: element.tag, character };
85
+ for (const child of element.children) {
86
+ const found = findUnrepresentable(child);
87
+ if (found) return found;
88
+ }
89
+ return undefined;
90
+ };
91
+
92
+ // The lines of Java's identity Transformer, which pretty prints with an indent amount of 2.
93
+ const printElement = (
94
+ element: XmlElement,
95
+ depth: number,
96
+ indentAmount: number,
97
+ lines: string[],
98
+ ): void => {
99
+ const indent = " ".repeat(depth * indentAmount);
100
+ // An element whose text or character data is empty has no content, like an element without
101
+ // children.
102
+ if (element.cdata) {
103
+ const data = element.cdata.replaceAll("]]>", "]]]]><![CDATA[>");
104
+ lines.push(`${indent}<${element.tag}><![CDATA[${data}]]></${element.tag}>`);
105
+ return;
106
+ }
107
+ if (element.text) {
108
+ lines.push(`${indent}<${element.tag}>${escapeXml(element.text)}</${element.tag}>`);
109
+ return;
110
+ }
111
+ if (element.children.length === 0) {
112
+ lines.push(`${indent}<${element.tag}/>`);
113
+ return;
114
+ }
115
+ lines.push(`${indent}<${element.tag}>`);
116
+ for (const child of element.children) printElement(child, depth + 1, indentAmount, lines);
117
+ lines.push(`${indent}</${element.tag}>`);
118
+ };
119
+
120
+ const getLocationElement = (location: SanyLocation) =>
121
+ createElement("location", [
122
+ createElement("column", [
123
+ createText("begin", location.beginColumn),
124
+ createText("end", location.endColumn),
125
+ ]),
126
+ createElement("line", [
127
+ createText("begin", location.beginLine),
128
+ createText("end", location.endLine),
129
+ ]),
130
+ createText("filename", location.file),
131
+ ]);
132
+
133
+ type ContextNode = SymbolNode | TheoremNode | AssumeNode;
134
+
135
+ const getReferenceTag = (node: ContextNode) => {
136
+ switch (node.kind) {
137
+ case "FormalParam":
138
+ return "FormalParamNodeRef";
139
+ case "OpDecl":
140
+ return "OpDeclNodeRef";
141
+ case "Module":
142
+ return "ModuleNodeRef";
143
+ case "Theorem":
144
+ return "TheoremNodeRef";
145
+ case "Assume":
146
+ return "AssumeNodeRef";
147
+ case "ThmOrAssumpDef":
148
+ return node.isTheorem ? "TheoremDefRef" : "AssumeDefRef";
149
+ case "OpDef":
150
+ return node.defKind === "UserDefined"
151
+ ? "UserDefinedOpKindRef"
152
+ : node.defKind === "BuiltIn"
153
+ ? "BuiltInKindRef"
154
+ : "ModuleInstanceKindRef";
155
+ }
156
+ };
157
+
158
+ // A frame of the Java stack of TLC 1.7.4's XMLExporter, in package tla2sany, with the node that
159
+ // the frame reports when an exception unwinds it.
160
+ interface JavaFrame {
161
+ at: string;
162
+ failedFor?: string;
163
+ }
164
+
165
+ // A call by which TLC 1.7.4's XMLExporter exports an expression that may be the null SANY generates
166
+ // for an operator it can't resolve, and how the JVM names that null.
167
+ interface JavaExpressionCall extends JavaFrame {
168
+ nullType: string;
169
+ nullValue: string;
170
+ }
171
+
172
+ const JAVA_CALLS = {
173
+ main: { at: "xml.XMLExporter.main(XMLExporter.java:145)" },
174
+ constant: { at: "semantic.ModuleNode.getSymbolElement(ModuleNode.java:1228)" },
175
+ variable: { at: "semantic.ModuleNode.getSymbolElement(ModuleNode.java:1236)" },
176
+ definition: { at: "semantic.ModuleNode.getSymbolElement(ModuleNode.java:1244)" },
177
+ topLevel: { at: "semantic.ModuleNode.getSymbolElement(ModuleNode.java:1268)" },
178
+ body: {
179
+ at: "semantic.OpDefNode.getSymbolElement(OpDefNode.java:1392)",
180
+ nullType: "ExprNode",
181
+ nullValue: '"this.body"',
182
+ },
183
+ param: { at: "semantic.OpDefNode.getSymbolElement(OpDefNode.java:1397)" },
184
+ builtInParam: { at: "semantic.OpDefNode.getSymbolElement(OpDefNode.java:1413)" },
185
+ theoremDefinitionBody: {
186
+ at: "semantic.ThmOrAssumpDefNode.getSymbolElement(ThmOrAssumpDefNode.java:663)",
187
+ nullType: "LevelNode",
188
+ nullValue: '"this.body"',
189
+ },
190
+ theoremDefinition: { at: "semantic.TheoremNode.getLevelElement(TheoremNode.java:439)" },
191
+ theorem: {
192
+ at: "semantic.TheoremNode.getLevelElement(TheoremNode.java:444)",
193
+ nullType: "LevelNode",
194
+ nullValue: 'the return value of "tla2sany.semantic.TheoremNode.getTheorem()"',
195
+ },
196
+ proof: { at: "semantic.TheoremNode.getLevelElement(TheoremNode.java:447)" },
197
+ assumeDefinition: { at: "semantic.AssumeNode.getLevelElement(AssumeNode.java:249)" },
198
+ assume: {
199
+ at: "semantic.AssumeNode.getLevelElement(AssumeNode.java:254)",
200
+ nullType: "ExprNode",
201
+ nullValue: 'the return value of "tla2sany.semantic.AssumeNode.getAssume()"',
202
+ },
203
+ operator: { at: "semantic.OpApplNode.getLevelElement(OpApplNode.java:1315)" },
204
+ operand: {
205
+ at: "semantic.OpApplNode.getLevelElement(OpApplNode.java:1326)",
206
+ nullType: "ExprOrOpArgNode",
207
+ nullValue: '"this.operands[i]"',
208
+ },
209
+ unbound: { at: "semantic.OpApplNode.getLevelElement(OpApplNode.java:1337)" },
210
+ bound: { at: "semantic.OpApplNode.getLevelElement(OpApplNode.java:1347)" },
211
+ range: {
212
+ at: "semantic.OpApplNode.getLevelElement(OpApplNode.java:1349)",
213
+ nullType: "ExprNode",
214
+ nullValue: '"this.ranges[i]"',
215
+ },
216
+ letBody: {
217
+ at: "semantic.LetInNode.getLevelElement(LetInNode.java:301)",
218
+ nullType: "ExprNode",
219
+ nullValue: '"this.body"',
220
+ },
221
+ letDefinition: { at: "semantic.LetInNode.getLevelElement(LetInNode.java:303)" },
222
+ labelBody: {
223
+ at: "semantic.LabelNode.getLevelElement(LabelNode.java:341)",
224
+ nullType: "LevelNode",
225
+ nullValue: '"this.body"',
226
+ },
227
+ labelParam: { at: "semantic.LabelNode.getLevelElement(LabelNode.java:343)" },
228
+ argument: { at: "semantic.OpArgNode.getLevelElement(OpArgNode.java:150)" },
229
+ exceptObject: { at: "semantic.AtNode.getLevelElement(AtNode.java:180)" },
230
+ exceptComponents: { at: "semantic.AtNode.getLevelElement(AtNode.java:181)" },
231
+ substOperator: { at: "semantic.Subst.export(Subst.java:258)" },
232
+ substExpression: {
233
+ at: "semantic.Subst.export(Subst.java:259)",
234
+ nullType: "ExprOrOpArgNode",
235
+ nullValue: '"this.expr"',
236
+ },
237
+ instanceSubst: { at: "semantic.InstanceNode.getLevelElement(InstanceNode.java:447)" },
238
+ instanceParam: { at: "semantic.InstanceNode.getLevelElement(InstanceNode.java:452)" },
239
+ useFact: {
240
+ at: "semantic.UseOrHideNode.getLevelElement(UseOrHideNode.java:175)",
241
+ nullType: "LevelNode",
242
+ nullValue: '"this.facts[i]"',
243
+ },
244
+ useDefinition: { at: "semantic.UseOrHideNode.getLevelElement(UseOrHideNode.java:176)" },
245
+ newDeclaration: { at: "semantic.NewSymbNode.getLevelElement(NewSymbNode.java:202)" },
246
+ newSet: { at: "semantic.NewSymbNode.getLevelElement(NewSymbNode.java:204)" },
247
+ assumption: {
248
+ at: "semantic.AssumeProveNode.getLevelElement(AssumeProveNode.java:404)",
249
+ nullType: "SemanticNode",
250
+ nullValue: '"assumes[i]"',
251
+ },
252
+ goal: {
253
+ at: "semantic.AssumeProveNode.getLevelElement(AssumeProveNode.java:406)",
254
+ nullType: "ExprNode",
255
+ nullValue: 'the return value of "tla2sany.semantic.AssumeProveNode.getProve()"',
256
+ },
257
+ byFact: {
258
+ at: "semantic.LeafProofNode.getLevelElement(LeafProofNode.java:154)",
259
+ nullType: "LevelNode",
260
+ nullValue: '"this.facts[i]"',
261
+ },
262
+ byDefinition: { at: "semantic.LeafProofNode.getLevelElement(LeafProofNode.java:155)" },
263
+ step: {
264
+ at: "semantic.NonLeafProofNode.getLevelElement(NonLeafProofNode.java:228)",
265
+ nullType: "LevelNode",
266
+ nullValue: '"this.steps[i]"',
267
+ },
268
+ defStep: { at: "semantic.DefStepNode.getLevelElement(DefStepNode.java:99)" },
269
+ } satisfies Record<string, JavaFrame | JavaExpressionCall>;
270
+
271
+ // The calls by which TLC 1.7.4's XMLExporter exports the parts of a SubstInNode or APSubstInNode.
272
+ const SUBST_IN_CALLS = {
273
+ SubstIn: {
274
+ subst: { at: "semantic.SubstInNode.getLevelElement(SubstInNode.java:530)" },
275
+ body: {
276
+ at: "semantic.SubstInNode.getLevelElement(SubstInNode.java:533)",
277
+ nullType: "ExprNode",
278
+ nullValue: '"this.body"',
279
+ },
280
+ from: { at: "semantic.SubstInNode.getLevelElement(SubstInNode.java:536)" },
281
+ to: { at: "semantic.SubstInNode.getLevelElement(SubstInNode.java:540)" },
282
+ },
283
+ APSubstIn: {
284
+ subst: { at: "semantic.APSubstInNode.getLevelElement(APSubstInNode.java:460)" },
285
+ body: {
286
+ at: "semantic.APSubstInNode.getLevelElement(APSubstInNode.java:463)",
287
+ nullType: "LevelNode",
288
+ nullValue: '"this.body"',
289
+ },
290
+ from: { at: "semantic.APSubstInNode.getLevelElement(APSubstInNode.java:466)" },
291
+ to: { at: "semantic.APSubstInNode.getLevelElement(APSubstInNode.java:470)" },
292
+ },
293
+ };
294
+
295
+ const SEMANTIC_EXPORT = "semantic.SemanticNode.export(SemanticNode.java:379)";
296
+ const LEVEL_EXPORT = "semantic.LevelNode.getSemanticElement(LevelNode.java:333)";
297
+ const UNSUPPORTED_KIND_FRAME = { at: "semantic.OpDefNode.getSymbolElement(OpDefNode.java:1424)" };
298
+ // Where SANY's builtin operators, such as nullODN, are.
299
+ const IN_BUILTINS = formatSanyLocation("--TLA+ BUILTINS--");
300
+
301
+ // The frames of the export of a node by reference in TLC 1.7.4's XMLExporter, down to the export
302
+ // of its definition, which reports the node.
303
+ const getDefinitionFrames = (kind: ContextNode["kind"], location: string): JavaFrame[] => {
304
+ const [exportAt, putLine, definitionAt] =
305
+ kind === "Theorem"
306
+ ? [
307
+ "TheoremNode.export(TheoremNode.java:456)",
308
+ 70,
309
+ "TheoremNode.exportDefinition(TheoremNode.java:404)",
310
+ ]
311
+ : kind === "Assume"
312
+ ? [
313
+ "AssumeNode.export(AssumeNode.java:263)",
314
+ 80,
315
+ "AssumeNode.exportDefinition(AssumeNode.java:207)",
316
+ ]
317
+ : [
318
+ kind === "ThmOrAssumpDef"
319
+ ? "ThmOrAssumpDefNode.export(ThmOrAssumpDefNode.java:671)"
320
+ : "SymbolNode.export(SymbolNode.java:177)",
321
+ 60,
322
+ "SymbolNode.exportDefinition(SymbolNode.java:138)",
323
+ ];
324
+ return [
325
+ { at: `semantic.${exportAt}` },
326
+ { at: `xml.SymbolContext.put(SymbolContext.java:${putLine})` },
327
+ { at: `semantic.${definitionAt}`, failedFor: location },
328
+ ];
329
+ };
330
+
331
+ // java.util.HashMap, which holds SymbolContext's entries, and lists them by the bucket of their
332
+ // Integer key, and the entries of a bucket in the order they were added.
333
+ const getHashMapCapacity = (size: number, capacity = 16): number =>
334
+ size > capacity * 0.75 ? getHashMapCapacity(size, capacity * 2) : capacity;
335
+
336
+ const getHashMapBucket = (key: number, capacity: number) => (key ^ (key >>> 16)) & (capacity - 1);
337
+
338
+ // Java's String.split, which drops the empty strings at the end.
339
+ const splitLines = (text: string) => {
340
+ const lines = text.split(/\r?\n/);
341
+ while (lines.at(-1) === "") lines.pop();
342
+ return lines;
343
+ };
344
+
345
+ // Java's String.trim, which removes the control characters and spaces at either end.
346
+ const trimJava = (text: string) => text.replace(/^[\0-\x20]+|[\0-\x20]+$/g, "");
347
+
348
+ // The characters of Java's Character.isWhitespace, which include four control characters.
349
+ const TRAILING_WHITESPACE =
350
+ // eslint-disable-next-line no-control-regex
351
+ /[\t-\r\x1C-\x20\u1680\u2000-\u2006\u2008-\u200A\u2028\u2029\u205F\u3000]+$/;
352
+
353
+ // SyntaxTreeNode.unboxComment: the lines of a comment boxed in (** and **), without the box.
354
+ const unboxComment = (text: string) => {
355
+ const start = text.indexOf("(**");
356
+ const end = text.lastIndexOf("**)");
357
+ if (start < 0 || end <= start) return text;
358
+ const lines = splitLines(text);
359
+ const beginLine = lines.findIndex((line) => line.includes("(**"));
360
+ const endLine = lines.findLastIndex((line) => line.includes("**)"));
361
+ if (beginLine < 0 || beginLine >= endLine) return text;
362
+ return lines
363
+ .slice(beginLine + 1, endLine)
364
+ .map((line) => {
365
+ const begin = line.indexOf("(*");
366
+ const finish = line.indexOf("*)");
367
+ if (begin < 0 || finish <= begin) return line;
368
+ return line.slice(begin + 3, Math.max(begin + 3, finish)).replace(TRAILING_WHITESPACE, "");
369
+ })
370
+ .join("\n");
371
+ };
372
+
373
+ // SyntaxTreeNode.unboxBackslashStarComment: the lines of a comment, without the \* that starts
374
+ // them, when at least half of them start with one.
375
+ const unboxBackslashStarComment = (text: string) => {
376
+ const lines = splitLines(text);
377
+ const isCommentLine = (line: string) => trimJava(line).startsWith("\\*");
378
+ if (lines.filter(isCommentLine).length < Math.floor(lines.length / 2)) return text;
379
+ return lines
380
+ .map((line) => {
381
+ if (!isCommentLine(line)) return line;
382
+ const start = line.indexOf("\\*") + 2;
383
+ return line.slice(line[start] === " " ? start + 1 : start);
384
+ })
385
+ .join("\n");
386
+ };
387
+
388
+ export interface XmlExportOptions {
389
+ // The TLC whose XMLExporter to reproduce.
390
+ version: TlcVersion;
391
+ // -r: only the root module, and only the declarations and definitions a module makes itself.
392
+ isRestricted: boolean;
393
+ // -u: the pre-comments without the boxes and \* that format them.
394
+ isUncommented: boolean;
395
+ // -t: the XML on one line.
396
+ isTerse: boolean;
397
+ }
398
+
399
+ // The XML of SANY's XMLExporter: every module of the specification, and a context with an entry
400
+ // for each symbol, theorem and assumption that the modules refer to.
401
+ const exportSpecification = (
402
+ modules: ModuleNode[],
403
+ root: ModuleNode,
404
+ options: XmlExportOptions,
405
+ ) => {
406
+ // TLC 1.7.4's XMLExporter writes fewer elements, which keeps their nodes out of the context.
407
+ const isTlc174 = options.version === "1.7.4";
408
+ // SymbolContext: SANY marks a node's UID as exported before it exports the node's definition,
409
+ // which can export other definitions, and adds the definition to its entries after them.
410
+ const keys = new Set<number>();
411
+ const entries = new Map<number, XmlElement>();
412
+ // SymbolContext's OTHER_BUG flag, which every copy of the context shares, so that from the first
413
+ // CASE with an OTHER arm on, a node missing its first operand exports it as $Other.
414
+ const otherBug = { isSet: false };
415
+ // The Java stack of TLC 1.7.4's XMLExporter as it exports the current node, outermost first.
416
+ const javaStack: JavaFrame[] = [];
417
+
418
+ const withFrames = <Result>(frames: JavaFrame[], run: () => Result) => {
419
+ if (!isTlc174) return run();
420
+ javaStack.push(...frames);
421
+ const result = run();
422
+ javaStack.length -= frames.length;
423
+ return result;
424
+ };
425
+
426
+ // TLC 1.7.4's XMLExporter reports the exception at each node it unwinds before the JVM reports
427
+ // it uncaught.
428
+ const getJavaException = (exception: string, frames: JavaFrame[]) => {
429
+ const trace = [...javaStack, ...frames].reverse();
430
+ const stackTrace = [exception, ...trace.map((frame) => `\tat tla2sany.${frame.at}`)].join("\n");
431
+ const unwound = trace
432
+ .filter((frame) => frame.failedFor !== undefined)
433
+ .map(
434
+ (frame) => `failed for node.toString(): ${frame.failedFor}\n with error \n${stackTrace}\n`,
435
+ )
436
+ .join("");
437
+ return setExitCode(
438
+ new TlaJavaException(stackTrace, unwound),
439
+ XML_EXPORT_EXIT_CODE.uncaughtException,
440
+ );
441
+ };
442
+
443
+ const getNullPointerException = (call: JavaExpressionCall) =>
444
+ getJavaException(
445
+ `java.lang.NullPointerException: Cannot invoke "tla2sany.semantic.${call.nullType}.export(org.w3c.dom.Document, tla2sany.xml.SymbolContext)" because ${call.nullValue} is null`,
446
+ [call],
447
+ );
448
+
449
+ const getReference = (node: ContextNode, call?: JavaFrame): XmlElement => {
450
+ if (!keys.has(node.id)) {
451
+ keys.add(node.id);
452
+ entries.set(
453
+ node.id,
454
+ withFrames(
455
+ [
456
+ ...(call ? [call] : []),
457
+ ...getDefinitionFrames(node.kind, formatSanyLocation(node.location)),
458
+ ],
459
+ () => exportDefinition(node),
460
+ ),
461
+ );
462
+ }
463
+ return createElement(getReferenceTag(node), [createText("UID", node.id)]);
464
+ };
465
+
466
+ const withLevel = (
467
+ element: XmlElement,
468
+ node: { levelChecked: number; level: number },
469
+ location: SanyLocation | undefined,
470
+ ) => {
471
+ const level = node.levelChecked === 0 ? [] : [createText("level", node.level)];
472
+ const head = location ? [getLocationElement(location), ...level] : level;
473
+ return { ...element, children: [...head, ...element.children] };
474
+ };
475
+
476
+ const getParamsElement = (
477
+ params: SymbolNode[],
478
+ isLeibnizArg: boolean[] | undefined,
479
+ call: JavaFrame,
480
+ ) =>
481
+ createElement(
482
+ "params",
483
+ params.map((param, index) =>
484
+ createElement("leibnizparam", [
485
+ getReference(param, call),
486
+ ...(isLeibnizArg?.[index] ? [createElement("leibniz")] : []),
487
+ ]),
488
+ ),
489
+ );
490
+
491
+ const getPreCommentsElement = (comments: string[]) => {
492
+ const text = comments
493
+ .map((comment) => comment.trimEnd())
494
+ .filter((comment) => comment.length > 0)
495
+ .join("\n");
496
+ const cdata = options.isUncommented ? unboxBackslashStarComment(unboxComment(text)) : text;
497
+ return comments.length > 0 ? [{ tag: "pre-comments", cdata, children: [] }] : [];
498
+ };
499
+
500
+ const exportDefinition = (node: ContextNode): XmlElement => {
501
+ switch (node.kind) {
502
+ case "FormalParam":
503
+ return withLevel(
504
+ createElement("FormalParamNode", [
505
+ createText("uniquename", node.name),
506
+ createText("arity", node.arity),
507
+ ]),
508
+ node,
509
+ node.location,
510
+ );
511
+ case "OpDecl":
512
+ return withLevel(
513
+ createElement("OpDeclNode", [
514
+ createText("uniquename", node.name),
515
+ createText("arity", node.arity),
516
+ createText("kind", node.declKind),
517
+ ]),
518
+ node,
519
+ node.location,
520
+ );
521
+ case "OpDef": {
522
+ // SANY's XMLExporter fails on the definition of a proof step that a DEF clause names.
523
+ if (node.defKind === "NumberedProofStep") {
524
+ throw isTlc174
525
+ ? getJavaException(getUnsupportedKind(37), [UNSUPPORTED_KIND_FRAME])
526
+ : setExitCode(
527
+ new TlaUnsupportedError(getUnsupportedKind(37)),
528
+ XML_EXPORT_EXIT_CODE.uncaughtException,
529
+ );
530
+ }
531
+ if (node.defKind === "ModuleInstance") {
532
+ return withLevel(
533
+ createElement("ModuleInstanceKind", [
534
+ createText("uniquename", node.name),
535
+ ...(node.isLocal && !isTlc174 ? [createElement("local")] : []),
536
+ ]),
537
+ node,
538
+ node.location,
539
+ );
540
+ }
541
+ const params = node.params ?? [];
542
+ if (node.defKind === "BuiltIn") {
543
+ return withLevel(
544
+ createElement("BuiltInKind", [
545
+ createText("uniquename", node.name),
546
+ createText("arity", node.arity),
547
+ ...(node.params
548
+ ? [getParamsElement(params, node.isLeibnizArg, JAVA_CALLS.builtInParam)]
549
+ : []),
550
+ ]),
551
+ node,
552
+ node.location,
553
+ );
554
+ }
555
+ const body = node.body;
556
+ const module = node.module;
557
+ // SANY leaves the body of an operator a RECURSIVE statement declares but nothing defines null.
558
+ if (body === undefined && isTlc174) throw getNullPointerException(JAVA_CALLS.body);
559
+ if (body === undefined || module === undefined) {
560
+ throw new Error(`The definition of ${node.name} has no body`);
561
+ }
562
+ if (isTlc174) {
563
+ return withLevel(
564
+ createElement("UserDefinedOpKind", [
565
+ createText("uniquename", node.name),
566
+ createText("arity", node.arity),
567
+ createElement("body", [exportExpression(body, JAVA_CALLS.body)]),
568
+ ...(node.params
569
+ ? [getParamsElement(params, node.isLeibnizArg, JAVA_CALLS.param)]
570
+ : []),
571
+ ...(node.inRecursive ? [createElement("recursive")] : []),
572
+ ]),
573
+ node,
574
+ node.location,
575
+ );
576
+ }
577
+ return withLevel(
578
+ createElement("UserDefinedOpKind", [
579
+ createText("uniquename", node.name),
580
+ createText("arity", node.arity),
581
+ createElement("originalOperator", [getReference(node.source ?? node)]),
582
+ createElement("originallyDefinedInModule", [getReference(module)]),
583
+ createElement("body", [exportExpression(body, JAVA_CALLS.body)]),
584
+ getParamsElement(params, node.isLeibnizArg, JAVA_CALLS.param),
585
+ ...getPreCommentsElement(node.preComments),
586
+ ...(node.inRecursive ? [createElement("recursive")] : []),
587
+ ...(node.recursiveSection >= 0
588
+ ? [createText("recursiveSection", node.recursiveSection)]
589
+ : []),
590
+ ...(node.isLocal ? [createElement("local")] : []),
591
+ ]),
592
+ node,
593
+ node.location,
594
+ );
595
+ }
596
+ case "ThmOrAssumpDef":
597
+ return withLevel(
598
+ createElement(node.isTheorem ? "TheoremDefNode" : "AssumeDef", [
599
+ createText("uniquename", node.name),
600
+ exportExpression(node.body, JAVA_CALLS.theoremDefinitionBody),
601
+ ]),
602
+ node,
603
+ node.location,
604
+ );
605
+ case "Module":
606
+ return createElement("ModuleNode", [
607
+ getLocationElement(node.location),
608
+ ...exportModuleChildren(node),
609
+ ]);
610
+ case "Theorem":
611
+ return withLevel(
612
+ createElement("TheoremNode", [
613
+ ...(node.definition
614
+ ? [
615
+ createElement("definition", [
616
+ getReference(node.definition, JAVA_CALLS.theoremDefinition),
617
+ ]),
618
+ ]
619
+ : []),
620
+ createElement("body", [exportExpression(node.body, JAVA_CALLS.theorem)]),
621
+ ...(node.proof ? [exportExpression(node.proof, JAVA_CALLS.proof)] : []),
622
+ ...(node.isSuffices ? [createElement("suffices")] : []),
623
+ ]),
624
+ node,
625
+ node.location,
626
+ );
627
+ case "Assume":
628
+ // AssumeNode#getLevel is its body's, which fails before the body is level-checked.
629
+ return createElement("AssumeNode", [
630
+ getLocationElement(node.location),
631
+ ...(node.body.levelChecked === 0 ? [] : [createText("level", node.body.level)]),
632
+ ...(node.definition
633
+ ? [
634
+ createElement("definition", [
635
+ getReference(node.definition, JAVA_CALLS.assumeDefinition),
636
+ ]),
637
+ ]
638
+ : []),
639
+ createElement("body", [exportExpression(node.body, JAVA_CALLS.assume)]),
640
+ ]);
641
+ }
642
+ };
643
+
644
+ // The children of a ModuleNode element after its name, built in the order SANY builds them so
645
+ // that the context entries they add appear in the same order.
646
+ const exportModuleChildren = (module: ModuleNode): XmlElement[] => {
647
+ // XMLExporter's filter for -r: a declaration or definition that the module makes itself.
648
+ const isExported = (node: OpDeclNode | OpDefNode) =>
649
+ !options.isRestricted || node.module === module;
650
+ return [
651
+ createText("uniquename", module.name),
652
+ ...(isTlc174
653
+ ? []
654
+ : [
655
+ createElement(
656
+ "extends",
657
+ [...new Set(module.extendees.map((extendee) => extendee.name))]
658
+ .toSorted()
659
+ .map((name) => createText("uniquename", name)),
660
+ ),
661
+ ]),
662
+ ...getDeclarations(module, CONSTANT_DECL_KIND)
663
+ .filter(isExported)
664
+ .map((declaration) => getReference(declaration, JAVA_CALLS.constant)),
665
+ ...getDeclarations(module, VARIABLE_DECL_KIND)
666
+ .filter(isExported)
667
+ .map((declaration) => getReference(declaration, JAVA_CALLS.variable)),
668
+ ...getOpDefs(module)
669
+ .filter(isExported)
670
+ .map((definition) => getReference(definition, JAVA_CALLS.definition)),
671
+ ...module.topLevel.map((topLevel) =>
672
+ topLevel.kind === "Instance" || topLevel.kind === "UseOrHide"
673
+ ? exportExpression(topLevel, JAVA_CALLS.topLevel)
674
+ : getReference(topLevel, JAVA_CALLS.topLevel),
675
+ ),
676
+ ...module.definitions
677
+ .filter((definition): definition is ModuleNode => !isTlc174 && definition.kind === "Module")
678
+ .map((inner) => getReference(inner)),
679
+ ];
680
+ };
681
+
682
+ // A Subst is no SemanticNode, so it reports no exception that unwinds it.
683
+ const exportSubst = (subst: Subst, call: JavaFrame) =>
684
+ withFrames([call], () =>
685
+ createElement("Subst", [
686
+ getReference(subst.op, JAVA_CALLS.substOperator),
687
+ exportExpression(subst.expr, JAVA_CALLS.substExpression),
688
+ ]),
689
+ );
690
+
691
+ const exportApplication = (node: OpApplNode) => {
692
+ // TLC 1.7.4's XMLExporter exports a specification with semantic errors, and fails on the
693
+ // operator of the node that SANY puts where it found one.
694
+ if (node.isNull && isTlc174) {
695
+ throw getJavaException(getUnsupportedKind(0), [
696
+ JAVA_CALLS.operator,
697
+ ...getDefinitionFrames("OpDef", IN_BUILTINS),
698
+ UNSUPPORTED_KIND_FRAME,
699
+ ]);
700
+ }
701
+ const last = node.operands[node.operands.length - 1];
702
+ if (
703
+ node.operator.name === "$Case" &&
704
+ node.operands.length > 1 &&
705
+ last?.kind === "OpAppl" &&
706
+ last.operator.name === "$Pair" &&
707
+ last.operands[0] === undefined
708
+ ) {
709
+ otherBug.isSet = true;
710
+ }
711
+ const operator = getReference(node.operator, JAVA_CALLS.operator);
712
+ const operands = node.operands.map((operand, index) =>
713
+ operand === undefined || (index === 0 && otherBug.isSet && isAbsent(operand))
714
+ ? isTlc174
715
+ ? createText("StringNode", "$Other")
716
+ : createElement("StringNode", [createText("StringValue", "$Other")])
717
+ : exportExpression(operand, JAVA_CALLS.operand),
718
+ );
719
+ const tupleOrs = node.tupleOrs;
720
+ const boundSymbols =
721
+ node.unbounded === undefined && node.bounded === undefined
722
+ ? []
723
+ : [
724
+ createElement("boundSymbols", [
725
+ ...(node.unbounded ?? []).map((symbol, index) =>
726
+ createElement("unbound", [
727
+ getReference(symbol, JAVA_CALLS.unbound),
728
+ ...(tupleOrs?.[index] ? [createElement("tuple")] : []),
729
+ ]),
730
+ ),
731
+ ...(node.bounded ?? []).map((symbols, index) =>
732
+ createElement("bound", [
733
+ ...symbols.map((symbol) => getReference(symbol, JAVA_CALLS.bound)),
734
+ ...(tupleOrs?.[index] ? [createElement("tuple")] : []),
735
+ exportExpression(node.ranges[index], JAVA_CALLS.range),
736
+ ]),
737
+ ),
738
+ ]),
739
+ ];
740
+ return createElement("OpApplNode", [
741
+ createElement("operator", [operator]),
742
+ createElement("operands", operands),
743
+ ...boundSymbols,
744
+ ]);
745
+ };
746
+
747
+ const exportFact = (fact: FactNode, call: JavaExpressionCall) =>
748
+ fact.kind === "Module" || fact.kind === "OpDef"
749
+ ? getReference(fact, call)
750
+ : exportExpression(fact, call);
751
+
752
+ // SemanticNode.export reports an exception that unwinds the export of a node.
753
+ const exportExpression = (
754
+ node: ExportedNode,
755
+ call: JavaFrame | JavaExpressionCall,
756
+ ): XmlElement => {
757
+ if (!isTlc174) return exportLevelNode(node);
758
+ if (node.kind === "OpAppl" && node.isAbsent && "nullType" in call) {
759
+ throw getNullPointerException(call);
760
+ }
761
+ const frames = [
762
+ call,
763
+ { at: SEMANTIC_EXPORT, failedFor: formatSanyLocation(node.location) },
764
+ { at: LEVEL_EXPORT },
765
+ ];
766
+ return withFrames(frames, () => exportLevelNode(node));
767
+ };
768
+
769
+ const exportLevelNode = (node: ExportedNode): XmlElement => {
770
+ switch (node.kind) {
771
+ case "OpAppl":
772
+ return withLevel(exportApplication(node), node, node.location);
773
+ case "Numeral":
774
+ return withLevel(
775
+ createElement("NumeralNode", [createText("IntValue", node.value)]),
776
+ node,
777
+ node.location,
778
+ );
779
+ case "String":
780
+ return withLevel(
781
+ createElement("StringNode", [createText("StringValue", node.value)]),
782
+ node,
783
+ node.location,
784
+ );
785
+ case "Decimal":
786
+ return withLevel(
787
+ createElement("DecimalNode", [
788
+ createText("mantissa", node.mantissa),
789
+ createText("exponent", node.exponent),
790
+ ...(isTlc174
791
+ ? []
792
+ : [
793
+ createText("integralPart", node.integralPart),
794
+ createText("fractionalPart", node.fractionalPart),
795
+ ]),
796
+ ]),
797
+ node,
798
+ node.location,
799
+ );
800
+ case "LetIn": {
801
+ const body = exportExpression(node.body, JAVA_CALLS.letBody);
802
+ return withLevel(
803
+ createElement("LetInNode", [
804
+ createElement("body", [body]),
805
+ createElement(
806
+ "opDefs",
807
+ node.opDefs.map((definition) => getReference(definition, JAVA_CALLS.letDefinition)),
808
+ ),
809
+ ]),
810
+ node,
811
+ node.location,
812
+ );
813
+ }
814
+ case "Label":
815
+ return withLevel(
816
+ createElement("LabelNode", [
817
+ createText("uniquename", node.name),
818
+ createText("arity", node.params.length),
819
+ createElement("body", [exportExpression(node.body, JAVA_CALLS.labelBody)]),
820
+ createElement(
821
+ "params",
822
+ node.params.map((param) => getReference(param, JAVA_CALLS.labelParam)),
823
+ ),
824
+ ]),
825
+ node,
826
+ node.location,
827
+ );
828
+ case "OpArg":
829
+ return withLevel(
830
+ createElement("OpArgNode", [
831
+ createElement("argument", [getReference(node.op, JAVA_CALLS.argument)]),
832
+ ]),
833
+ node,
834
+ node.location,
835
+ );
836
+ case "At": {
837
+ const object = node.except.operands[0];
838
+ const components = node.component.operands[0];
839
+ if (object === undefined || components === undefined) {
840
+ throw new Error("An @ refers to an incomplete EXCEPT");
841
+ }
842
+ return withLevel(
843
+ createElement("AtNode", [
844
+ exportExpression(object, JAVA_CALLS.exceptObject),
845
+ exportExpression(components, JAVA_CALLS.exceptComponents),
846
+ ]),
847
+ node,
848
+ node.location,
849
+ );
850
+ }
851
+ case "SubstIn":
852
+ case "APSubstIn": {
853
+ const calls = SUBST_IN_CALLS[node.kind];
854
+ const substs = node.substs.map((subst) => exportSubst(subst, calls.subst));
855
+ const body = exportExpression(node.body, calls.body);
856
+ return withLevel(
857
+ createElement(node.kind === "SubstIn" ? "SubstInNode" : "APSubstInNode", [
858
+ createElement("substs", substs),
859
+ createElement("body", [body]),
860
+ createElement("instFrom", [getReference(node.instantiatingModule, calls.from)]),
861
+ createElement("instTo", [getReference(node.instantiatedModule, calls.to)]),
862
+ ]),
863
+ node,
864
+ node.location,
865
+ );
866
+ }
867
+ case "Instance": {
868
+ const substs = node.substs.map((subst) => exportSubst(subst, JAVA_CALLS.instanceSubst));
869
+ const params = node.params.map((param) => getReference(param, JAVA_CALLS.instanceParam));
870
+ return createElement("InstanceNode", [
871
+ getLocationElement(node.location),
872
+ createText("level", 0),
873
+ ...(node.name === undefined ? [] : [createText("uniquename", node.name)]),
874
+ createText("module", node.module.name),
875
+ createElement("substs", substs),
876
+ createElement("params", params),
877
+ ...(node.isLocal && !isTlc174 ? [createElement("local")] : []),
878
+ ]);
879
+ }
880
+ case "UseOrHide":
881
+ return withLevel(
882
+ createElement("UseOrHideNode", [
883
+ createElement(
884
+ "facts",
885
+ node.facts.map((fact) => exportFact(fact, JAVA_CALLS.useFact)),
886
+ ),
887
+ createElement(
888
+ "defs",
889
+ node.defs.map((definition) => getReference(definition, JAVA_CALLS.useDefinition)),
890
+ ),
891
+ ...(node.isOnly ? [createElement("only")] : []),
892
+ ...(node.isHide ? [createElement("hide")] : []),
893
+ ]),
894
+ node,
895
+ node.location,
896
+ );
897
+ case "NewSymb":
898
+ return withLevel(
899
+ createElement("NewSymbNode", [
900
+ getReference(node.declaration, JAVA_CALLS.newDeclaration),
901
+ // TLC 1.7.4's NewSymbNode leaves out a set that is null.
902
+ ...(node.set && !(isTlc174 && isAbsent(node.set))
903
+ ? [exportExpression(node.set, JAVA_CALLS.newSet)]
904
+ : []),
905
+ ]),
906
+ node,
907
+ node.location,
908
+ );
909
+ case "AssumeProve":
910
+ return withLevel(
911
+ createElement("AssumeProveNode", [
912
+ createElement(
913
+ "assumes",
914
+ node.assumptions.map((assumption) =>
915
+ exportExpression(assumption, JAVA_CALLS.assumption),
916
+ ),
917
+ ),
918
+ createElement("prove", [exportExpression(node.goal, JAVA_CALLS.goal)]),
919
+ ...(node.isSuffices ? [createElement("suffices")] : []),
920
+ ...(node.isBoxed ? [createElement("boxed")] : []),
921
+ ]),
922
+ node,
923
+ node.location,
924
+ );
925
+ case "LeafProof": {
926
+ const isObvious = node.facts.length === 0 && node.defs.length === 0;
927
+ const element = node.isOmitted
928
+ ? createElement("omitted")
929
+ : isObvious
930
+ ? createElement("obvious")
931
+ : createElement("by", [
932
+ createElement(
933
+ "facts",
934
+ node.facts.map((fact) => exportFact(fact, JAVA_CALLS.byFact)),
935
+ ),
936
+ createElement(
937
+ "defs",
938
+ node.defs.map((definition) => getReference(definition, JAVA_CALLS.byDefinition)),
939
+ ),
940
+ ...(node.isOnly ? [createElement("only")] : []),
941
+ ]);
942
+ return withLevel(element, node, node.location);
943
+ }
944
+ case "NonLeafProof":
945
+ return withLevel(
946
+ createElement("steps", [
947
+ ...(isTlc174 ? [] : [createText("proofLevel", node.proofLevel)]),
948
+ ...node.steps.map((step) =>
949
+ step.kind === "Theorem"
950
+ ? getReference(step, JAVA_CALLS.step)
951
+ : exportExpression(step, JAVA_CALLS.step),
952
+ ),
953
+ ]),
954
+ node,
955
+ node.location,
956
+ );
957
+ case "DefStep":
958
+ return withLevel(
959
+ createElement(
960
+ "DefStepNode",
961
+ node.defs.map((definition) => getReference(definition, JAVA_CALLS.defStep)),
962
+ ),
963
+ node,
964
+ node.location,
965
+ );
966
+ }
967
+ };
968
+
969
+ const moduleReferences = (options.isRestricted ? [root] : modules).map((module) =>
970
+ getReference(module, JAVA_CALLS.main),
971
+ );
972
+ const capacity = getHashMapCapacity(entries.size);
973
+ const context = createElement(
974
+ "context",
975
+ [...entries.entries()]
976
+ .toSorted(
977
+ ([first], [second]) =>
978
+ getHashMapBucket(first, capacity) - getHashMapBucket(second, capacity),
979
+ )
980
+ .map(([id, element]) => createElement("entry", [createText("UID", id), element])),
981
+ );
982
+ return createElement("modules", [
983
+ createText("RootModule", root.name),
984
+ context,
985
+ ...moduleReferences,
986
+ ]);
987
+ };
988
+
989
+ type ExportedNode = LevelNode | InstanceNode | UseOrHideNode | ProofPartNode;
990
+
991
+ // The specification SANY accepts, level-checked, as SANY's tla2sany.xml.XMLExporter prints it
992
+ // with -o and the given options: every module the root module depends on, the standard modules
993
+ // among them, with the level of every expression.
994
+ export const exportSanyXml = (
995
+ specification: SanySpecification,
996
+ options: XmlExportOptions = {
997
+ version: "master",
998
+ isRestricted: false,
999
+ isUncommented: false,
1000
+ isTerse: false,
1001
+ },
1002
+ ) => {
1003
+ const document = exportSpecification(specification.modules, specification.root, options);
1004
+ const unrepresentable = findUnrepresentable(document);
1005
+ if (unrepresentable) {
1006
+ const codePoint = unrepresentable.character.codePointAt(0) ?? 0;
1007
+ throw setExitCode(
1008
+ new TlaUnsupportedError(
1009
+ `The spec contains the character U+${codePoint.toString(16).toUpperCase().padStart(4, "0")} in the ${unrepresentable.tag} element. XML 1.0 cannot represent that character in character data, not even as a numeric character reference, so the spec cannot be exported until the character is removed from it.`,
1010
+ ),
1011
+ XML_EXPORT_EXIT_CODE.unrepresentableCharacter,
1012
+ );
1013
+ }
1014
+ const lines = ['<?xml version="1.0" encoding="UTF-8" standalone="no"?>'];
1015
+ printElement(document, 0, options.isTerse ? 0 : 2, lines);
1016
+ return options.isTerse ? lines.join("") : `${lines.join("\n")}\n`;
1017
+ };