everystate 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +111 -86
- package/bin/everystate.mjs +35 -0
- package/native/darwin-arm64/everystate +0 -0
- package/native/darwin-x64/everystate +0 -0
- package/native/linux-arm64/everystate +0 -0
- package/native/linux-x64/everystate +0 -0
- package/native/win32-x64/everystate.exe +0 -0
- package/package.json +23 -24
- package/wasm/cli.mjs +26 -0
- package/wasm/everystate.d.ts +33 -0
- package/wasm/everystate.mjs +112 -0
- package/wasm/everystate.wasm +0 -0
- package/CONFORMANCE.md +0 -64
- package/dist/bundled.d.ts +0 -1
- package/dist/bundled.js +0 -2
- package/dist/cfg.d.ts +0 -24
- package/dist/cfg.js +0 -252
- package/dist/checker.d.ts +0 -96
- package/dist/checker.js +0 -520
- package/dist/cli.d.ts +0 -2
- package/dist/cli.js +0 -4
- package/dist/community.d.ts +0 -17
- package/dist/community.js +0 -1381
- package/dist/entry.d.ts +0 -2
- package/dist/entry.js +0 -39
- package/dist/eval.d.ts +0 -119
- package/dist/eval.js +0 -1533
- package/dist/index.d.ts +0 -33
- package/dist/index.js +0 -24
- package/dist/java.d.ts +0 -2
- package/dist/java.js +0 -6
- package/dist/lexer.d.ts +0 -17
- package/dist/lexer.js +0 -255
- package/dist/liveness.d.ts +0 -80
- package/dist/liveness.js +0 -855
- package/dist/main.d.ts +0 -40
- package/dist/main.js +0 -494
- package/dist/model.d.ts +0 -56
- package/dist/model.js +0 -582
- package/dist/modules.d.ts +0 -91
- package/dist/modules.js +0 -649
- package/dist/node-host.d.ts +0 -2
- package/dist/node-host.js +0 -25
- package/dist/parser.d.ts +0 -30
- package/dist/parser.js +0 -1114
- package/dist/pprint.d.ts +0 -7
- package/dist/pprint.js +0 -647
- package/dist/stdlib.d.ts +0 -14
- package/dist/stdlib.js +0 -588
- package/dist/values.d.ts +0 -150
- package/dist/values.js +0 -822
package/dist/pprint.d.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* `tlc2.value.Values.ppr(value)` / `tlc2.pprint.PrettyPrint.mypp(value, width)`:
|
|
3
|
-
* re-wraps the one-line string form of a TLC value at `width` columns. Returns
|
|
4
|
-
* `value` unchanged when it cannot be parsed, cannot be formatted, or when the
|
|
5
|
-
* parse does not reach the last character.
|
|
6
|
-
*/
|
|
7
|
-
export declare function ppr(value: string, width?: number): string;
|
package/dist/pprint.js
DELETED
|
@@ -1,647 +0,0 @@
|
|
|
1
|
-
// Faithful port of TLC's value pretty-printer (tlc2.pprint.{PrettyPrint,Parse,
|
|
2
|
-
// Format,Node}). TLC prints every value through
|
|
3
|
-
//
|
|
4
|
-
// tlc2.value.Values.ppr(s) == tlc2.pprint.PrettyPrint.mypp(s, 80)
|
|
5
|
-
//
|
|
6
|
-
// which RE-PARSES the one-line string form of the value and re-wraps it so that
|
|
7
|
-
// it fits in `width` columns. The parser is deliberately naive (no escape
|
|
8
|
-
// handling in strings, constants are runs of letters/digits only, ...) and the
|
|
9
|
-
// port keeps every one of those quirks: whenever the Java code would throw, or
|
|
10
|
-
// would not consume the whole input, the input is returned unchanged.
|
|
11
|
-
//
|
|
12
|
-
// The structure (and most names) follow the Java source one-to-one so the two
|
|
13
|
-
// can be diffed by eye.
|
|
14
|
-
/* ------------------------------------------------------------------------- */
|
|
15
|
-
/* Exceptions */
|
|
16
|
-
/* ------------------------------------------------------------------------- */
|
|
17
|
-
class ParseException extends Error {
|
|
18
|
-
}
|
|
19
|
-
class FormatException extends Error {
|
|
20
|
-
}
|
|
21
|
-
/** Stand-in for java.lang.StringIndexOutOfBoundsException. */
|
|
22
|
-
class StringIndexOutOfBoundsException extends Error {
|
|
23
|
-
}
|
|
24
|
-
/* ------------------------------------------------------------------------- */
|
|
25
|
-
/* java.lang.String / java.lang.Character helpers */
|
|
26
|
-
/* ------------------------------------------------------------------------- */
|
|
27
|
-
/** String.charAt: returns the UTF-16 code unit, throws when out of range. */
|
|
28
|
-
function charAt(string, index) {
|
|
29
|
-
if (index < 0 || index >= string.length) {
|
|
30
|
-
throw new StringIndexOutOfBoundsException("index " + index + ", length " + string.length);
|
|
31
|
-
}
|
|
32
|
-
return string.charCodeAt(index);
|
|
33
|
-
}
|
|
34
|
-
/** String.regionMatches(index, other, 0, other.length()) / startsWith(other, index). */
|
|
35
|
-
function regionMatches(string, index, other) {
|
|
36
|
-
if (index < 0 || index + other.length > string.length)
|
|
37
|
-
return false;
|
|
38
|
-
return string.startsWith(other, index);
|
|
39
|
-
}
|
|
40
|
-
/** Character.isWhitespace(char). */
|
|
41
|
-
function isWhitespace(c) {
|
|
42
|
-
return ((c >= 0x09 && c <= 0x0d) ||
|
|
43
|
-
(c >= 0x1c && c <= 0x20) ||
|
|
44
|
-
c === 0x1680 ||
|
|
45
|
-
(c >= 0x2000 && c <= 0x2006) ||
|
|
46
|
-
(c >= 0x2008 && c <= 0x200a) ||
|
|
47
|
-
c === 0x2028 ||
|
|
48
|
-
c === 0x2029 ||
|
|
49
|
-
c === 0x205f ||
|
|
50
|
-
c === 0x3000);
|
|
51
|
-
}
|
|
52
|
-
const DIGIT_RE = /^\p{Nd}$/u;
|
|
53
|
-
const LETTER_OR_DIGIT_RE = /^[\p{L}\p{Nd}]$/u;
|
|
54
|
-
/** Character.isDigit(char) -- a single UTF-16 unit, so surrogates never match. */
|
|
55
|
-
function isDigit(c) {
|
|
56
|
-
if (c < 0x80)
|
|
57
|
-
return c >= 0x30 && c <= 0x39;
|
|
58
|
-
return DIGIT_RE.test(String.fromCharCode(c));
|
|
59
|
-
}
|
|
60
|
-
/** Character.isLetterOrDigit(char). */
|
|
61
|
-
function isLetterOrDigit(c) {
|
|
62
|
-
if (c < 0x80) {
|
|
63
|
-
return (c >= 0x30 && c <= 0x39) || (c >= 0x41 && c <= 0x5a) || (c >= 0x61 && c <= 0x7a);
|
|
64
|
-
}
|
|
65
|
-
return LETTER_OR_DIGIT_RE.test(String.fromCharCode(c));
|
|
66
|
-
}
|
|
67
|
-
/* ------------------------------------------------------------------------- */
|
|
68
|
-
/* Node */
|
|
69
|
-
/* ------------------------------------------------------------------------- */
|
|
70
|
-
const CONSTANT = 1;
|
|
71
|
-
const SET = 2;
|
|
72
|
-
const SEQUENCE = 3;
|
|
73
|
-
const RECORD = 4;
|
|
74
|
-
const RECORDPAIR = 5;
|
|
75
|
-
const FUNCTION = 6;
|
|
76
|
-
const FUNCTIONPAIR = 7;
|
|
77
|
-
const SUBSET = 8;
|
|
78
|
-
class Node {
|
|
79
|
-
type;
|
|
80
|
-
first;
|
|
81
|
-
last;
|
|
82
|
-
children;
|
|
83
|
-
next;
|
|
84
|
-
string;
|
|
85
|
-
constructor(str, s, e, t) {
|
|
86
|
-
this.string = str;
|
|
87
|
-
this.type = t;
|
|
88
|
-
this.first = s;
|
|
89
|
-
this.last = e;
|
|
90
|
-
this.children = null;
|
|
91
|
-
this.next = null;
|
|
92
|
-
}
|
|
93
|
-
/** string.substring(first, last+1), with Java's bounds checking. */
|
|
94
|
-
value() {
|
|
95
|
-
const begin = this.first;
|
|
96
|
-
const end = this.last + 1;
|
|
97
|
-
if (begin < 0 || end > this.string.length || begin > end) {
|
|
98
|
-
throw new StringIndexOutOfBoundsException("begin " + begin + ", end " + end + ", length " + this.string.length);
|
|
99
|
-
}
|
|
100
|
-
return this.string.substring(begin, end);
|
|
101
|
-
}
|
|
102
|
-
length() {
|
|
103
|
-
return this.last - this.first + 1;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
/* ------------------------------------------------------------------------- */
|
|
107
|
-
/* Parse */
|
|
108
|
-
/* ------------------------------------------------------------------------- */
|
|
109
|
-
const P_setOpen = 0x7b; // '{'
|
|
110
|
-
const P_setClose = 0x7d; // '}'
|
|
111
|
-
const P_seqOpen = 0x3c; // '<'
|
|
112
|
-
const P_seqClose = 0x3e; // '>'
|
|
113
|
-
const P_recOpen = 0x5b; // '['
|
|
114
|
-
const P_recClose = 0x5d; // ']'
|
|
115
|
-
const P_recDiv = "|->";
|
|
116
|
-
const P_funOpen = 0x28; // '('
|
|
117
|
-
const P_funClose = 0x29; // ')'
|
|
118
|
-
const P_funDiv = ":>";
|
|
119
|
-
const P_funSep = "@@";
|
|
120
|
-
const P_intDiv = "..";
|
|
121
|
-
const P_ssDiv = "SUBSET";
|
|
122
|
-
const COMMA = 0x2c;
|
|
123
|
-
const QUOTE = 0x22;
|
|
124
|
-
const MINUS = 0x2d;
|
|
125
|
-
/** Converts a run off the end of the string into a ParseException, as each Java method does. */
|
|
126
|
-
function rethrow(e, what, index, string) {
|
|
127
|
-
if (e instanceof StringIndexOutOfBoundsException) {
|
|
128
|
-
throw new ParseException("TLC Bug: while formating output, the formatter ran off the end of the string while " +
|
|
129
|
-
what + " starting at index " + index + " in the value " + string + "\n" + e.message);
|
|
130
|
-
}
|
|
131
|
-
throw e;
|
|
132
|
-
}
|
|
133
|
-
function parse(string, index) {
|
|
134
|
-
if (string.length === 0) {
|
|
135
|
-
throw new ParseException("TLC Bug: while formating output, the formatter was called with an empty string for a value");
|
|
136
|
-
}
|
|
137
|
-
if (index < 0 || index > string.length - 1) {
|
|
138
|
-
throw new ParseException("TLC Bug: while formating output, the formatter was called with a string " +
|
|
139
|
-
string + " for a value and an index " + index + " out of bounds");
|
|
140
|
-
}
|
|
141
|
-
index = skipWhitespace(string, index);
|
|
142
|
-
// NB: an uncaught StringIndexOutOfBoundsException cannot escape from here:
|
|
143
|
-
// skipWhitespace either returns a valid index or throws a ParseException.
|
|
144
|
-
if (charAt(string, index) === P_setOpen) {
|
|
145
|
-
return parseSet(string, index);
|
|
146
|
-
}
|
|
147
|
-
if (charAt(string, index) === P_seqOpen) {
|
|
148
|
-
return parseSequence(string, index);
|
|
149
|
-
}
|
|
150
|
-
if (charAt(string, index) === P_recOpen) {
|
|
151
|
-
return parseRecord(string, index);
|
|
152
|
-
}
|
|
153
|
-
if (charAt(string, index) === P_funOpen) {
|
|
154
|
-
return parseFunction(string, index);
|
|
155
|
-
}
|
|
156
|
-
if (regionMatches(string, index, P_ssDiv)) {
|
|
157
|
-
return parseSubset(string, index);
|
|
158
|
-
}
|
|
159
|
-
return parseConstant(string, index);
|
|
160
|
-
}
|
|
161
|
-
/** Parse a set. Assume that index points to the '{' character in string. */
|
|
162
|
-
function parseSet(string, index) {
|
|
163
|
-
const first = index;
|
|
164
|
-
let last = index;
|
|
165
|
-
try {
|
|
166
|
-
const node = new Node(string, first, 0, SET);
|
|
167
|
-
let firstelement = null;
|
|
168
|
-
let lastelement = null;
|
|
169
|
-
// skip the '{' character and following whitespace
|
|
170
|
-
last++;
|
|
171
|
-
last = skipWhitespace(string, last);
|
|
172
|
-
// parse comma-separated elements in set up to a '}' character
|
|
173
|
-
while (charAt(string, last) !== P_setClose) {
|
|
174
|
-
const elt = parse(string, last);
|
|
175
|
-
if (lastelement === null) {
|
|
176
|
-
firstelement = elt;
|
|
177
|
-
lastelement = elt;
|
|
178
|
-
}
|
|
179
|
-
else {
|
|
180
|
-
lastelement.next = elt;
|
|
181
|
-
lastelement = elt;
|
|
182
|
-
}
|
|
183
|
-
// point to character after this element
|
|
184
|
-
last = elt.last + 1;
|
|
185
|
-
// skip the comma between elements, and surrounding whitespace
|
|
186
|
-
last = skipWhitespace(string, last);
|
|
187
|
-
if (charAt(string, last) === COMMA)
|
|
188
|
-
last++;
|
|
189
|
-
last = skipWhitespace(string, last);
|
|
190
|
-
}
|
|
191
|
-
node.children = firstelement;
|
|
192
|
-
node.last = last;
|
|
193
|
-
return node;
|
|
194
|
-
}
|
|
195
|
-
catch (e) {
|
|
196
|
-
rethrow(e, "parsing a set", index, string);
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
/** Parse a sequence. Assume that index points to the '<' character in string. */
|
|
200
|
-
function parseSequence(string, index) {
|
|
201
|
-
const first = index;
|
|
202
|
-
let last = index;
|
|
203
|
-
try {
|
|
204
|
-
const node = new Node(string, first, 0, SEQUENCE);
|
|
205
|
-
let firstelement = null;
|
|
206
|
-
let lastelement = null;
|
|
207
|
-
// skip the '<<' token and following whitespace
|
|
208
|
-
last++;
|
|
209
|
-
last++;
|
|
210
|
-
last = skipWhitespace(string, last);
|
|
211
|
-
// parse comma-separated elements in sequence up to a '>>' token
|
|
212
|
-
while (charAt(string, last) !== P_seqClose) {
|
|
213
|
-
const elt = parse(string, last);
|
|
214
|
-
if (lastelement === null) {
|
|
215
|
-
firstelement = elt;
|
|
216
|
-
lastelement = elt;
|
|
217
|
-
}
|
|
218
|
-
else {
|
|
219
|
-
lastelement.next = elt;
|
|
220
|
-
lastelement = elt;
|
|
221
|
-
}
|
|
222
|
-
// point to character after this element
|
|
223
|
-
last = elt.last + 1;
|
|
224
|
-
// skip the comma between elements, and surrounding whitespace
|
|
225
|
-
last = skipWhitespace(string, last);
|
|
226
|
-
if (charAt(string, last) === COMMA)
|
|
227
|
-
last++;
|
|
228
|
-
last = skipWhitespace(string, last);
|
|
229
|
-
}
|
|
230
|
-
// point to the second '>' in the '>>' token (unchecked, as in Java)
|
|
231
|
-
last++;
|
|
232
|
-
node.children = firstelement;
|
|
233
|
-
node.last = last;
|
|
234
|
-
return node;
|
|
235
|
-
}
|
|
236
|
-
catch (e) {
|
|
237
|
-
rethrow(e, "parsing a sequence", index, string);
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
/** Parse a record. Assume that index points to the '[' character in string. */
|
|
241
|
-
function parseRecord(string, index) {
|
|
242
|
-
const first = index;
|
|
243
|
-
let last = index;
|
|
244
|
-
try {
|
|
245
|
-
const node = new Node(string, first, 0, RECORD);
|
|
246
|
-
let firstelement = null;
|
|
247
|
-
let lastelement = null;
|
|
248
|
-
// skip the '[' character and following whitespace
|
|
249
|
-
last++;
|
|
250
|
-
last = skipWhitespace(string, last);
|
|
251
|
-
// parse comma-separated pairs in record up to a ']' character
|
|
252
|
-
while (charAt(string, last) !== P_recClose) {
|
|
253
|
-
const elt = parseRecordPair(string, last);
|
|
254
|
-
if (lastelement === null) {
|
|
255
|
-
firstelement = elt;
|
|
256
|
-
lastelement = elt;
|
|
257
|
-
}
|
|
258
|
-
else {
|
|
259
|
-
lastelement.next = elt;
|
|
260
|
-
lastelement = elt;
|
|
261
|
-
}
|
|
262
|
-
// point to character after this element
|
|
263
|
-
last = elt.last + 1;
|
|
264
|
-
// skip the comma between elements, and surrounding whitespace
|
|
265
|
-
last = skipWhitespace(string, last);
|
|
266
|
-
if (charAt(string, last) === COMMA)
|
|
267
|
-
last++;
|
|
268
|
-
last = skipWhitespace(string, last);
|
|
269
|
-
}
|
|
270
|
-
node.children = firstelement;
|
|
271
|
-
node.last = last;
|
|
272
|
-
return node;
|
|
273
|
-
}
|
|
274
|
-
catch (e) {
|
|
275
|
-
rethrow(e, "parsing a record", index, string);
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
/** Assume that index points to the start of a "field|->value" record pair in string. */
|
|
279
|
-
function parseRecordPair(string, index) {
|
|
280
|
-
const first = index;
|
|
281
|
-
let last = index;
|
|
282
|
-
try {
|
|
283
|
-
const node = new Node(string, first, 0, RECORDPAIR);
|
|
284
|
-
// get field name
|
|
285
|
-
const field = parse(string, last);
|
|
286
|
-
// point to position just past field name in string
|
|
287
|
-
last = field.last + 1;
|
|
288
|
-
// skip the '|->' token and the surrounding whitespace
|
|
289
|
-
last = skipWhitespace(string, last);
|
|
290
|
-
if (!regionMatches(string, last, P_recDiv)) {
|
|
291
|
-
throw new ParseException("TLC Bug: while formating output, the formatter couldn't find the token " + P_recDiv +
|
|
292
|
-
" while parsing a record field/value pair starting at index " + index +
|
|
293
|
-
" in value " + string);
|
|
294
|
-
}
|
|
295
|
-
last = last + P_recDiv.length;
|
|
296
|
-
last = skipWhitespace(string, last);
|
|
297
|
-
// get field value
|
|
298
|
-
const value = parse(string, last);
|
|
299
|
-
// make field name and value children of the record pair node
|
|
300
|
-
field.next = value;
|
|
301
|
-
node.children = field;
|
|
302
|
-
node.last = value.last;
|
|
303
|
-
return node;
|
|
304
|
-
}
|
|
305
|
-
catch (e) {
|
|
306
|
-
rethrow(e, "parsing a record pair", index, string);
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
/** Parse a function. Assume that index points to the '(' character in string. */
|
|
310
|
-
function parseFunction(string, index) {
|
|
311
|
-
const first = index;
|
|
312
|
-
let last = index;
|
|
313
|
-
try {
|
|
314
|
-
const node = new Node(string, first, 0, FUNCTION);
|
|
315
|
-
let firstpair = null;
|
|
316
|
-
let lastpair = null;
|
|
317
|
-
// skip the '(' character and following whitespace
|
|
318
|
-
last++;
|
|
319
|
-
last = skipWhitespace(string, last);
|
|
320
|
-
// parse the pairs in function up to a ')' character
|
|
321
|
-
while (charAt(string, last) !== P_funClose) {
|
|
322
|
-
const pair = parseFunctionPair(string, last);
|
|
323
|
-
if (lastpair === null) {
|
|
324
|
-
firstpair = pair;
|
|
325
|
-
lastpair = pair;
|
|
326
|
-
}
|
|
327
|
-
else {
|
|
328
|
-
lastpair.next = pair;
|
|
329
|
-
lastpair = pair;
|
|
330
|
-
}
|
|
331
|
-
// point to character after this pair
|
|
332
|
-
last = pair.last + 1;
|
|
333
|
-
// skip the separator between pairs, and surrounding whitespace
|
|
334
|
-
last = skipWhitespace(string, last);
|
|
335
|
-
if (regionMatches(string, last, P_funSep)) {
|
|
336
|
-
last = last + P_funSep.length;
|
|
337
|
-
}
|
|
338
|
-
last = skipWhitespace(string, last);
|
|
339
|
-
}
|
|
340
|
-
node.children = firstpair;
|
|
341
|
-
node.last = last;
|
|
342
|
-
return node;
|
|
343
|
-
}
|
|
344
|
-
catch (e) {
|
|
345
|
-
rethrow(e, "parsing a function", index, string);
|
|
346
|
-
}
|
|
347
|
-
}
|
|
348
|
-
/** Parse a SUBSET. Assume that index points to the start of the substring "SUBSET". */
|
|
349
|
-
function parseSubset(string, index) {
|
|
350
|
-
const first = index;
|
|
351
|
-
let last = index;
|
|
352
|
-
try {
|
|
353
|
-
const node = new Node(string, first, 0, SUBSET);
|
|
354
|
-
// skip the 'SUBSET' and following whitespace
|
|
355
|
-
last += 6;
|
|
356
|
-
last = skipWhitespace(string, last);
|
|
357
|
-
const elt = parse(string, last);
|
|
358
|
-
node.children = elt;
|
|
359
|
-
node.last = elt.last;
|
|
360
|
-
return node;
|
|
361
|
-
}
|
|
362
|
-
catch (e) {
|
|
363
|
-
rethrow(e, "parsing a SUBSET", index, string);
|
|
364
|
-
}
|
|
365
|
-
}
|
|
366
|
-
/** Assume that index points to the start of a "domain:>range" pair in string. */
|
|
367
|
-
function parseFunctionPair(string, index) {
|
|
368
|
-
const first = index;
|
|
369
|
-
let last = index;
|
|
370
|
-
try {
|
|
371
|
-
const node = new Node(string, first, 0, FUNCTIONPAIR);
|
|
372
|
-
// get domain value
|
|
373
|
-
const domain = parse(string, last);
|
|
374
|
-
// point to position just past domain value in string
|
|
375
|
-
last = domain.last + 1;
|
|
376
|
-
// skip the ':>' token and the surrounding whitespace
|
|
377
|
-
last = skipWhitespace(string, last);
|
|
378
|
-
if (!regionMatches(string, last, P_funDiv)) {
|
|
379
|
-
throw new ParseException("TLC Bug: while formating output, the formatter couldn't find token " + P_funDiv +
|
|
380
|
-
" while parsing a function arg/value pair starting at index " + index +
|
|
381
|
-
" in value " + string);
|
|
382
|
-
}
|
|
383
|
-
last = last + P_funDiv.length;
|
|
384
|
-
last = skipWhitespace(string, last);
|
|
385
|
-
// get range value
|
|
386
|
-
const range = parse(string, last);
|
|
387
|
-
// make domain and range values children of the function pair node
|
|
388
|
-
domain.next = range;
|
|
389
|
-
node.children = domain;
|
|
390
|
-
node.last = range.last;
|
|
391
|
-
return node;
|
|
392
|
-
}
|
|
393
|
-
catch (e) {
|
|
394
|
-
rethrow(e, "parsing a function pair", index, string);
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
|
-
/** Parse a constant: strings, booleans, integers, intervals and model values. */
|
|
398
|
-
function parseConstant(string, index) {
|
|
399
|
-
try {
|
|
400
|
-
const first = skipWhitespace(string, index);
|
|
401
|
-
let last = first;
|
|
402
|
-
if (charAt(string, first) === QUOTE) {
|
|
403
|
-
// we are parsing a string constant (NB: no notion of escaped quotes)
|
|
404
|
-
do
|
|
405
|
-
last++;
|
|
406
|
-
while (charAt(string, last) !== QUOTE);
|
|
407
|
-
return new Node(string, first, last, CONSTANT);
|
|
408
|
-
}
|
|
409
|
-
if (isDigit(charAt(string, last))) {
|
|
410
|
-
// we might be parsing a constant interval i..j
|
|
411
|
-
const node = parseInterval(string, index);
|
|
412
|
-
if (node !== null)
|
|
413
|
-
return node;
|
|
414
|
-
}
|
|
415
|
-
if (charAt(string, last) === MINUS) {
|
|
416
|
-
// we are parsing a negative integer
|
|
417
|
-
last++;
|
|
418
|
-
while (isDigit(charAt(string, last))) {
|
|
419
|
-
last++;
|
|
420
|
-
if (last === string.length)
|
|
421
|
-
break;
|
|
422
|
-
}
|
|
423
|
-
last--;
|
|
424
|
-
return new Node(string, first, last, CONSTANT);
|
|
425
|
-
}
|
|
426
|
-
if (isLetterOrDigit(charAt(string, last))) {
|
|
427
|
-
// we are parsing an integer, boolean, or model value
|
|
428
|
-
while (isLetterOrDigit(charAt(string, last))) {
|
|
429
|
-
last++;
|
|
430
|
-
if (last === string.length)
|
|
431
|
-
break;
|
|
432
|
-
}
|
|
433
|
-
last--;
|
|
434
|
-
return new Node(string, first, last, CONSTANT);
|
|
435
|
-
}
|
|
436
|
-
// we have encountered an illegal character in a constant
|
|
437
|
-
throw new ParseException("TLC Bug: while formating output, the formatter discovered an illegal character while" +
|
|
438
|
-
" parsing a constant starting at index " + first + " in the value " + string);
|
|
439
|
-
}
|
|
440
|
-
catch (e) {
|
|
441
|
-
rethrow(e, "parsing a constant", index, string);
|
|
442
|
-
}
|
|
443
|
-
}
|
|
444
|
-
/**
|
|
445
|
-
* Parse a constant interval of the form i..j for integers i and j. Returns
|
|
446
|
-
* null if the parsing fails, rather than throwing as other routines do.
|
|
447
|
-
*/
|
|
448
|
-
function parseInterval(string, index) {
|
|
449
|
-
try {
|
|
450
|
-
const first = index;
|
|
451
|
-
let last = first;
|
|
452
|
-
// parse the first integer i
|
|
453
|
-
if (!isDigit(charAt(string, last)))
|
|
454
|
-
return null;
|
|
455
|
-
while (last < string.length && isDigit(charAt(string, last))) {
|
|
456
|
-
last++;
|
|
457
|
-
}
|
|
458
|
-
// parse the interval constructor ".."
|
|
459
|
-
if (!regionMatches(string, last, P_intDiv))
|
|
460
|
-
return null;
|
|
461
|
-
last++;
|
|
462
|
-
last++;
|
|
463
|
-
// parse the second integer j
|
|
464
|
-
if (!isDigit(charAt(string, last)))
|
|
465
|
-
return null;
|
|
466
|
-
while (isDigit(charAt(string, last))) {
|
|
467
|
-
last++;
|
|
468
|
-
if (last === string.length)
|
|
469
|
-
break;
|
|
470
|
-
}
|
|
471
|
-
last--;
|
|
472
|
-
return new Node(string, first, last, CONSTANT);
|
|
473
|
-
}
|
|
474
|
-
catch (e) {
|
|
475
|
-
// bail if we run off the string during parsing
|
|
476
|
-
if (e instanceof StringIndexOutOfBoundsException)
|
|
477
|
-
return null;
|
|
478
|
-
throw e;
|
|
479
|
-
}
|
|
480
|
-
}
|
|
481
|
-
/**
|
|
482
|
-
* Skip whitespace starting at index in string, and return the index of the
|
|
483
|
-
* first non-whitespace character, throwing if we run off the end.
|
|
484
|
-
*/
|
|
485
|
-
function skipWhitespace(string, index) {
|
|
486
|
-
try {
|
|
487
|
-
let i = index;
|
|
488
|
-
while (isWhitespace(charAt(string, i))) {
|
|
489
|
-
i++;
|
|
490
|
-
}
|
|
491
|
-
return i;
|
|
492
|
-
}
|
|
493
|
-
catch (e) {
|
|
494
|
-
rethrow(e, "skipping whitespace", index, string);
|
|
495
|
-
}
|
|
496
|
-
}
|
|
497
|
-
/* ------------------------------------------------------------------------- */
|
|
498
|
-
/* Format */
|
|
499
|
-
/* ------------------------------------------------------------------------- */
|
|
500
|
-
const setOpen = "{ ";
|
|
501
|
-
const setClose = " }";
|
|
502
|
-
const setPad = " ";
|
|
503
|
-
const setSep = ",";
|
|
504
|
-
const seqOpen = "<< ";
|
|
505
|
-
const seqClose = " >>";
|
|
506
|
-
const seqPad = " ";
|
|
507
|
-
const seqSep = ",";
|
|
508
|
-
const funOpen = "( ";
|
|
509
|
-
const funClose = " )";
|
|
510
|
-
const funPad = " ";
|
|
511
|
-
const funSep = " @@";
|
|
512
|
-
const funDiv = " :>";
|
|
513
|
-
const funDivPad = " ";
|
|
514
|
-
const recOpen = "[ ";
|
|
515
|
-
const recClose = " ]";
|
|
516
|
-
const recPad = " ";
|
|
517
|
-
const recSep = ",";
|
|
518
|
-
const recDiv = " |->";
|
|
519
|
-
const recDivPad = " ";
|
|
520
|
-
const subsetOpen = "SUBSET ";
|
|
521
|
-
const subsetClose = " ";
|
|
522
|
-
const subsetPad = " ";
|
|
523
|
-
const subsetSep = ",";
|
|
524
|
-
function format(value, columnwidth, trailerwidth, leftpad) {
|
|
525
|
-
// Use the value's original format if it will fit on the line
|
|
526
|
-
if (value.length() <= columnwidth - trailerwidth)
|
|
527
|
-
return value.value();
|
|
528
|
-
// Break the value over multiple lines since it won't fit on one line
|
|
529
|
-
switch (value.type) {
|
|
530
|
-
case CONSTANT:
|
|
531
|
-
return value.value();
|
|
532
|
-
case SET:
|
|
533
|
-
return formatSimpleValue(value, columnwidth, trailerwidth, leftpad, setOpen, setClose, setPad, setSep);
|
|
534
|
-
case SEQUENCE:
|
|
535
|
-
return formatSimpleValue(value, columnwidth, trailerwidth, leftpad, seqOpen, seqClose, seqPad, seqSep);
|
|
536
|
-
case FUNCTION:
|
|
537
|
-
return formatPairValue(value, columnwidth, trailerwidth, leftpad, funOpen, funClose, funPad, funSep, funDiv, funDivPad);
|
|
538
|
-
case RECORD:
|
|
539
|
-
return formatPairValue(value, columnwidth, trailerwidth, leftpad, recOpen, recClose, recPad, recSep, recDiv, recDivPad);
|
|
540
|
-
case SUBSET:
|
|
541
|
-
return formatSimpleValue(value, columnwidth, trailerwidth, leftpad, subsetOpen, subsetClose, subsetPad, subsetSep);
|
|
542
|
-
default:
|
|
543
|
-
throw new FormatException("TLC Bug: while formating output, the formatter called with an expression of type " +
|
|
544
|
-
value.type + " while formatting " + value.value() + " and this should never happen");
|
|
545
|
-
}
|
|
546
|
-
}
|
|
547
|
-
function formatSimpleValue(list, columnwidth, trailerwidth, leftpad, open, close, pad, sep) {
|
|
548
|
-
return (open +
|
|
549
|
-
formatValues(list.children, columnwidth - Math.max(open.length, pad.length), trailerwidth + close.length, leftpad + pad, sep) +
|
|
550
|
-
close);
|
|
551
|
-
}
|
|
552
|
-
function formatValues(values, columnwidth, trailerwidth, leftpad, sep) {
|
|
553
|
-
let pp = "";
|
|
554
|
-
for (let value = values; value !== null; value = value.next) {
|
|
555
|
-
if (value.next !== null) {
|
|
556
|
-
// this is not the last value in the list, so leave room for sep
|
|
557
|
-
pp = pp + format(value, columnwidth, sep.length, leftpad) + sep + "\n" + leftpad;
|
|
558
|
-
}
|
|
559
|
-
else {
|
|
560
|
-
// this is the last value in the list
|
|
561
|
-
pp = pp + format(value, columnwidth, trailerwidth, leftpad);
|
|
562
|
-
}
|
|
563
|
-
}
|
|
564
|
-
return pp;
|
|
565
|
-
}
|
|
566
|
-
function formatPairValue(list, columnwidth, trailerwidth, leftpad, open, close, pad, sep, div, divpad) {
|
|
567
|
-
return (open +
|
|
568
|
-
formatPairs(list.children, columnwidth - Math.max(open.length, pad.length), trailerwidth + close.length, leftpad + pad, sep, div, divpad) +
|
|
569
|
-
close);
|
|
570
|
-
}
|
|
571
|
-
function formatPairs(pairs, columnwidth, trailerwidth, leftpad, sep, div, divpad) {
|
|
572
|
-
let pp = "";
|
|
573
|
-
for (let pair = pairs; pair !== null; pair = pair.next) {
|
|
574
|
-
// Every pair node is built by parse{Record,Function}Pair with exactly two children.
|
|
575
|
-
const arg = pair.children;
|
|
576
|
-
const val = arg.next;
|
|
577
|
-
// First suppose this pair is not the last pair in the list. In this
|
|
578
|
-
// case, don't forget to append sep to the end of the value; but don't
|
|
579
|
-
// bother keeping the last trailerwidth characters open at the end of
|
|
580
|
-
// the line, go ahead and use them to format the value.
|
|
581
|
-
if (pair.next !== null) {
|
|
582
|
-
// put argument and value on one line if they will fit
|
|
583
|
-
if (arg.length() + div.length + 1 + val.length() + sep.length <= columnwidth) {
|
|
584
|
-
pp = pp + arg.value() + div + " " + val.value() + sep + "\n" + leftpad;
|
|
585
|
-
continue;
|
|
586
|
-
}
|
|
587
|
-
// put argument on one line if it will fit
|
|
588
|
-
if (arg.length() + div.length <= columnwidth) {
|
|
589
|
-
pp = pp + arg.value() + div +
|
|
590
|
-
"\n" + leftpad + divpad +
|
|
591
|
-
format(val, columnwidth - divpad.length, sep.length, leftpad + divpad) + sep +
|
|
592
|
-
"\n" + leftpad;
|
|
593
|
-
continue;
|
|
594
|
-
}
|
|
595
|
-
// format the argument on multiple lines and then the value
|
|
596
|
-
pp = pp + format(arg, columnwidth, trailerwidth + div.length, leftpad) + div +
|
|
597
|
-
"\n" + leftpad + divpad +
|
|
598
|
-
format(val, columnwidth - divpad.length, sep.length, leftpad + divpad) + sep +
|
|
599
|
-
"\n" + leftpad;
|
|
600
|
-
continue;
|
|
601
|
-
}
|
|
602
|
-
// Now suppose this pair is the last pair in the list. Remember to leave
|
|
603
|
-
// trailerwidth space at the end of the line, but don't worry about
|
|
604
|
-
// appending sep to the end of the line.
|
|
605
|
-
// put argument and value on one line if they will fit
|
|
606
|
-
if (arg.length() + div.length + 1 + val.length() <= columnwidth - trailerwidth) {
|
|
607
|
-
pp = pp + arg.value() + div + " " + val.value();
|
|
608
|
-
continue;
|
|
609
|
-
}
|
|
610
|
-
// put argument alone on one line if it will fit
|
|
611
|
-
if (arg.length() + div.length <= columnwidth) {
|
|
612
|
-
pp = pp + arg.value() + div +
|
|
613
|
-
"\n" + leftpad + divpad +
|
|
614
|
-
format(val, columnwidth - divpad.length, trailerwidth, leftpad + divpad);
|
|
615
|
-
continue;
|
|
616
|
-
}
|
|
617
|
-
// format the argument on multiple lines and then the value
|
|
618
|
-
pp = pp + format(arg, columnwidth, trailerwidth + div.length, leftpad) + div +
|
|
619
|
-
"\n" + leftpad + divpad +
|
|
620
|
-
format(val, columnwidth - divpad.length, trailerwidth, leftpad + divpad);
|
|
621
|
-
continue;
|
|
622
|
-
}
|
|
623
|
-
return pp;
|
|
624
|
-
}
|
|
625
|
-
/* ------------------------------------------------------------------------- */
|
|
626
|
-
/* PrettyPrint */
|
|
627
|
-
/* ------------------------------------------------------------------------- */
|
|
628
|
-
/**
|
|
629
|
-
* `tlc2.value.Values.ppr(value)` / `tlc2.pprint.PrettyPrint.mypp(value, width)`:
|
|
630
|
-
* re-wraps the one-line string form of a TLC value at `width` columns. Returns
|
|
631
|
-
* `value` unchanged when it cannot be parsed, cannot be formatted, or when the
|
|
632
|
-
* parse does not reach the last character.
|
|
633
|
-
*/
|
|
634
|
-
export function ppr(value, width = 80) {
|
|
635
|
-
try {
|
|
636
|
-
const tree = parse(value, 0);
|
|
637
|
-
if (tree.last < value.length - 1) {
|
|
638
|
-
return value;
|
|
639
|
-
}
|
|
640
|
-
return format(tree, width, 0, "");
|
|
641
|
-
}
|
|
642
|
-
catch {
|
|
643
|
-
// Java catches every Exception here (ParseException, FormatException and
|
|
644
|
-
// the StringIndexOutOfBoundsException a bogus Node.value() can raise).
|
|
645
|
-
return value;
|
|
646
|
-
}
|
|
647
|
-
}
|
package/dist/stdlib.d.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import type { Def, ModuleInfo } from './modules.ts';
|
|
2
|
-
import type { Evaluator } from './eval.ts';
|
|
3
|
-
import { SetBase } from './values.ts';
|
|
4
|
-
import type { Value } from './values.ts';
|
|
5
|
-
export type Fn = (ev: Evaluator, a: any[]) => Value;
|
|
6
|
-
export declare const fail: (msg: string) => never;
|
|
7
|
-
export declare function int(v: Value, op: string, i: number): number;
|
|
8
|
-
export declare function seq(v: Value, op: string, i: number): Value[];
|
|
9
|
-
export declare function set(v: Value, op: string, i: number): SetBase;
|
|
10
|
-
export declare function toStr(v: Value): string;
|
|
11
|
-
export declare function globalDefs(): Map<string, Def>;
|
|
12
|
-
export declare function buildStdlib(io: {
|
|
13
|
-
print: (s: string) => void;
|
|
14
|
-
}): Map<string, ModuleInfo>;
|