@dancrumb/cant-shared 0.0.0-stage → 0.10.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.
@@ -0,0 +1,12 @@
1
+ export declare function cant(): string;
2
+ export { KEYWORDS, tokenize } from './tokenize.ts';
3
+ export type { Token, TokenKind } from './tokenize.ts';
4
+ export { parse, ParseError } from './parse.ts';
5
+ export type * from './ast.ts';
6
+ export { evaluate } from './evaluate.ts';
7
+ export type { Evaluation, EvaluationScope, RaisedError } from './evaluate.ts';
8
+ export { compare, DATE_TIME_LIMIT, dateTimeMilliseconds, durationMilliseconds, durationToText, equal, numberToText, toText, } from './values.ts';
9
+ export type { BaseValue, EnumValue, ObjectInstance, Value } from './values.ts';
10
+ export { COMPILED_FORMAT } from './compiled.ts';
11
+ export type * from './compiled.ts';
12
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,wBAAgB,IAAI,IAAI,MAAM,CAE7B;AAED,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACnD,YAAY,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAEtD,OAAO,EAAE,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAC/C,mBAAmB,UAAU,CAAC;AAE9B,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,YAAY,EAAE,UAAU,EAAE,eAAe,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAE9E,OAAO,EACL,OAAO,EACP,eAAe,EACf,oBAAoB,EACpB,oBAAoB,EACpB,cAAc,EACd,KAAK,EACL,YAAY,EACZ,MAAM,GACP,MAAM,aAAa,CAAC;AACrB,YAAY,EAAE,SAAS,EAAE,SAAS,EAAE,cAAc,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AAE/E,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAChD,mBAAmB,eAAe,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,9 @@
1
+ export function cant() {
2
+ return 'cant';
3
+ }
4
+ export { KEYWORDS, tokenize } from './tokenize.js';
5
+ export { parse, ParseError } from './parse.js';
6
+ export { evaluate } from './evaluate.js';
7
+ export { compare, DATE_TIME_LIMIT, dateTimeMilliseconds, durationMilliseconds, durationToText, equal, numberToText, toText, } from './values.js';
8
+ export { COMPILED_FORMAT } from './compiled.js';
9
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,IAAI;IAClB,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAGnD,OAAO,EAAE,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAG/C,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAGzC,OAAO,EACL,OAAO,EACP,eAAe,EACf,oBAAoB,EACpB,oBAAoB,EACpB,cAAc,EACd,KAAK,EACL,YAAY,EACZ,MAAM,GACP,MAAM,aAAa,CAAC;AAGrB,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC"}
@@ -0,0 +1,30 @@
1
+ import type { Point } from 'unist';
2
+ import type { Program } from './ast.ts';
3
+ /**
4
+ * The error `parse` throws when the source is not valid Cant. It is Peggy's `SyntaxError`, typed
5
+ * here because the package's declarations are built from the generated JavaScript, which has no
6
+ * types of its own.
7
+ */
8
+ export interface ParseError extends globalThis.SyntaxError {
9
+ /** Where the problem is. Lines and columns are 1-based and offsets are 0-based, as in unist. */
10
+ location: {
11
+ start: Point & {
12
+ offset: number;
13
+ };
14
+ end: Point & {
15
+ offset: number;
16
+ };
17
+ };
18
+ /** What the parser could have accepted where it stopped, if it stopped on a syntax error. */
19
+ expected: unknown[] | null;
20
+ /** The text it found there instead, or `null` at the end of the source. */
21
+ found: string | null | undefined;
22
+ }
23
+ export declare const ParseError: new (message: string, expected: unknown[] | null, found: string | null, location: ParseError['location']) => ParseError;
24
+ /**
25
+ * Parse Cant source into a syntax tree.
26
+ *
27
+ * @throws {ParseError} if the source is not valid Cant; its `location` says where.
28
+ */
29
+ export declare function parse(source: string): Program;
30
+ //# sourceMappingURL=parse.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parse.d.ts","sourceRoot":"","sources":["../src/parse.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,OAAO,CAAC;AACnC,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AAGxC;;;;GAIG;AACH,MAAM,WAAW,UAAW,SAAQ,UAAU,CAAC,WAAW;IACxD,gGAAgG;IAChG,QAAQ,EAAE;QAAE,KAAK,EAAE,KAAK,GAAG;YAAE,MAAM,EAAE,MAAM,CAAA;SAAE,CAAC;QAAC,GAAG,EAAE,KAAK,GAAG;YAAE,MAAM,EAAE,MAAM,CAAA;SAAE,CAAA;KAAE,CAAC;IACjF,6FAA6F;IAC7F,QAAQ,EAAE,OAAO,EAAE,GAAG,IAAI,CAAC;IAC3B,2EAA2E;IAC3E,KAAK,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC;CAClC;AAED,eAAO,MAAM,UAAU,EAA6B,KAClD,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,OAAO,EAAE,GAAG,IAAI,EAC1B,KAAK,EAAE,MAAM,GAAG,IAAI,EACpB,QAAQ,EAAE,UAAU,CAAC,UAAU,CAAC,KAC7B,UAAU,CAAC;AAEhB;;;;GAIG;AACH,wBAAgB,KAAK,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAE7C"}
package/dist/parse.js ADDED
@@ -0,0 +1,11 @@
1
+ import { parse as parseGenerated, SyntaxError } from './generated/parser.js';
2
+ export const ParseError = SyntaxError;
3
+ /**
4
+ * Parse Cant source into a syntax tree.
5
+ *
6
+ * @throws {ParseError} if the source is not valid Cant; its `location` says where.
7
+ */
8
+ export function parse(source) {
9
+ return parseGenerated(source);
10
+ }
11
+ //# sourceMappingURL=parse.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parse.js","sourceRoot":"","sources":["../src/parse.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,IAAI,cAAc,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAgB7E,MAAM,CAAC,MAAM,UAAU,GAAG,WAKX,CAAC;AAEhB;;;;GAIG;AACH,MAAM,UAAU,KAAK,CAAC,MAAc;IAClC,OAAO,cAAc,CAAC,MAAM,CAAY,CAAC;AAC3C,CAAC"}
@@ -0,0 +1,26 @@
1
+ import type { Position } from 'unist';
2
+ export type TokenKind = 'keyword' | 'identifier' | 'operator' | 'string' | 'number' | 'builtin' | 'punctuation';
3
+ export interface Token {
4
+ kind: TokenKind;
5
+ /**
6
+ * Where the token is in the source. Lines and columns are 1-based and offsets are 0-based,
7
+ * as in unist; `end` is just past the token's last character.
8
+ */
9
+ position: Position;
10
+ }
11
+ /**
12
+ * Words reserved everywhere. A program cannot use them as names. Each comes from the ADR that
13
+ * introduced it, in docs/adr.
14
+ */
15
+ export declare const KEYWORDS: ReadonlySet<string>;
16
+ /**
17
+ * Split Cant source into tokens. Whitespace and comments are skipped, and no token spans a line break.
18
+ *
19
+ * A template string is split around its interpolations: `"a ${b} c"` is the string `"a `, the
20
+ * punctuation `${`, the identifier `b`, the punctuation `}` and the string ` c"`.
21
+ *
22
+ * Words that are keywords only in one place, such as `auth` in a trigger's block, are keywords
23
+ * there and identifiers elsewhere.
24
+ */
25
+ export declare function tokenize(source: string): Token[];
26
+ //# sourceMappingURL=tokenize.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tokenize.d.ts","sourceRoot":"","sources":["../src/tokenize.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAEtC,MAAM,MAAM,SAAS,GACjB,SAAS,GACT,YAAY,GACZ,UAAU,GACV,QAAQ,GACR,QAAQ,GACR,SAAS,GACT,aAAa,CAAC;AAElB,MAAM,WAAW,KAAK;IACpB,IAAI,EAAE,SAAS,CAAC;IAChB;;;OAGG;IACH,QAAQ,EAAE,QAAQ,CAAC;CACpB;AAED;;;GAGG;AACH,eAAO,MAAM,QAAQ,EAAE,WAAW,CAAC,MAAM,CAqDvC,CAAC;AAqOH;;;;;;;;GAQG;AACH,wBAAgB,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,KAAK,EAAE,CAwBhD"}
@@ -0,0 +1,311 @@
1
+ import { Tokenizr } from 'tokenizr';
2
+ /**
3
+ * Words reserved everywhere. A program cannot use them as names. Each comes from the ADR that
4
+ * introduced it, in docs/adr.
5
+ */
6
+ export const KEYWORDS = new Set([
7
+ // 0005, 0006
8
+ 'import',
9
+ 'namespace',
10
+ 'export',
11
+ // 0007
12
+ 'action',
13
+ 'body',
14
+ 'return',
15
+ 'break',
16
+ 'continue',
17
+ 'for',
18
+ 'in',
19
+ 'while',
20
+ 'if',
21
+ 'else',
22
+ // 0009
23
+ 'trigger',
24
+ 'on',
25
+ // 0010
26
+ 'entity',
27
+ 'permissions',
28
+ // 0012, 0019
29
+ 'TEXT',
30
+ 'NUMBER',
31
+ 'BOOLEAN',
32
+ 'DATE_TIME',
33
+ 'FILE',
34
+ // 0026
35
+ 'DURATION',
36
+ 'VOID',
37
+ 'NOTHING',
38
+ 'true',
39
+ 'false',
40
+ // 0013
41
+ 'enum',
42
+ // 0021
43
+ 'SET',
44
+ 'delete',
45
+ // 0023
46
+ 'error',
47
+ 'ERROR',
48
+ 'raise',
49
+ 'pass',
50
+ 'assert',
51
+ // 0027
52
+ 'model',
53
+ // 0033
54
+ 'UNKNOWN',
55
+ // 0034
56
+ 'const',
57
+ // 0035
58
+ 'relation',
59
+ ]);
60
+ /** Operators spelled as words; they are matched as whole words only (0016, 0019, 0023, 0033). */
61
+ const WORD_OPERATORS = new Set(['and', 'or', 'orElse', 'isEmpty', 'instanceOf', 'ignore']);
62
+ /** Operators spelled with symbols. Multi-character operators come first so they win over their prefixes. */
63
+ const SYMBOL_OPERATORS = /---|::|:=|<=|>=|==|!=|\*\*|[<>+*\-/%!.]/;
64
+ /** A JavaScript number literal (docs/adr/0019-operators.md). It never starts with a sign. */
65
+ const NUMBER = new RegExp([
66
+ String.raw `0[xX][0-9A-Fa-f](?:_?[0-9A-Fa-f])*`,
67
+ String.raw `0[oO][0-7](?:_?[0-7])*`,
68
+ String.raw `0[bB][01](?:_?[01])*`,
69
+ String.raw `(?:(?:0|[1-9](?:_?[0-9])*)(?:\.(?:[0-9](?:_?[0-9])*)?)?|\.[0-9](?:_?[0-9])*)(?:[eE][+-]?[0-9](?:_?[0-9])*)?`,
70
+ ]
71
+ .map((alternative) => `(?:${alternative})`)
72
+ .join('|') + '(?![A-Za-z0-9_])');
73
+ /**
74
+ * One piece of a string's text (docs/adr/0008-strings.md): an ordinary character, an escape, or
75
+ * a `$` that does not start an interpolation. A `\` at the end of a line is allowed so that an
76
+ * unfinished escape still belongs to the string; anywhere else it takes the next character with it.
77
+ */
78
+ const TEXT = String.raw `(?:[^"\\$\r\n]|\\(?:[^\r\n]|(?=[\r\n]|$))|\$(?!\{))`;
79
+ const wordKind = (word) => {
80
+ if (KEYWORDS.has(word))
81
+ return 'keyword';
82
+ if (WORD_OPERATORS.has(word))
83
+ return 'operator';
84
+ return 'identifier';
85
+ };
86
+ /** Abandon any strings and interpolations that a line break has left unfinished. */
87
+ const resetAtLineBreak = (ctx) => {
88
+ while (ctx.state() !== 'default')
89
+ ctx.pop();
90
+ };
91
+ // Tokenizing uses a stack of states:
92
+ // - `default` is ordinary code;
93
+ // - `string` is the text of a template string after an interpolation has started;
94
+ // - `interpolation` is the code inside `${ ... }`, which may itself contain strings;
95
+ // - `braces` is the code inside a `{ ... }` within an interpolation, such as an object value,
96
+ // whose `}` closes the braces rather than the interpolation.
97
+ // Rules are tried in order, and the first one that matches wins.
98
+ const CODE = 'default, interpolation, braces';
99
+ const lexer = new Tokenizr()
100
+ .rule(CODE, /[ \t]+/, (ctx) => {
101
+ ctx.ignore();
102
+ })
103
+ .rule(CODE, /\s+/, (ctx) => {
104
+ resetAtLineBreak(ctx);
105
+ ctx.ignore();
106
+ })
107
+ // A comment runs from `//` to the end of the line (docs/adr/0007-actions.md). It comes before
108
+ // the operators so that `//` is not read as two `/` operators.
109
+ .rule(CODE, /\/\/[^\r\n]*/, (ctx) => {
110
+ ctx.ignore();
111
+ })
112
+ .rule(CODE, /[A-Za-z_][A-Za-z0-9_]*/, (ctx, match) => {
113
+ ctx.accept(wordKind(match[0]));
114
+ })
115
+ // A built-in value, such as `@subject` (docs/adr/0009-triggers.md).
116
+ .rule(CODE, /@[A-Za-z_][A-Za-z0-9_]*/, (ctx) => {
117
+ ctx.accept('builtin');
118
+ })
119
+ // A relation's cardinality, such as `0..*` (docs/adr/0035-entity-relationships.md). It comes
120
+ // before numbers so that `0..1` is not read as the number `0.` and then `.1`.
121
+ .rule(CODE, /[01]\.\.[1*]/, (ctx) => {
122
+ ctx.accept('number');
123
+ })
124
+ .rule(CODE, NUMBER, (ctx) => {
125
+ ctx.accept('number');
126
+ })
127
+ // A string that is interrupted by an interpolation continues in the `string` state.
128
+ .rule(CODE, new RegExp(`"${TEXT}*(?=\\$\\{)`), (ctx) => {
129
+ ctx.accept('string');
130
+ ctx.push('string');
131
+ })
132
+ // A string ends at its closing quote or, if that is missing, at the end of the line.
133
+ .rule(CODE, new RegExp(`"${TEXT}*"?`), (ctx) => {
134
+ ctx.accept('string');
135
+ })
136
+ // Inside an interpolation, braces nest, and only the `}` that matches `${` ends it.
137
+ .rule('interpolation, braces', /\{/, (ctx) => {
138
+ ctx.accept('punctuation');
139
+ ctx.push('braces');
140
+ })
141
+ .rule('interpolation, braces', /\}/, (ctx) => {
142
+ ctx.accept('punctuation');
143
+ ctx.pop();
144
+ })
145
+ .rule(CODE, SYMBOL_OPERATORS, (ctx) => {
146
+ ctx.accept('operator');
147
+ })
148
+ // Anything else is a single character of punctuation, so tokenizing never fails.
149
+ .rule(CODE, /./, (ctx) => {
150
+ ctx.accept('punctuation');
151
+ })
152
+ .rule('string', /\$\{/, (ctx) => {
153
+ ctx.accept('punctuation');
154
+ ctx.push('interpolation');
155
+ })
156
+ .rule('string', new RegExp(`${TEXT}+(?=\\$\\{)`), (ctx) => {
157
+ ctx.accept('string');
158
+ })
159
+ .rule('string', new RegExp(`${TEXT}*"|${TEXT}+`), (ctx) => {
160
+ ctx.accept('string');
161
+ ctx.pop();
162
+ })
163
+ .rule('string', /\r?\n/, (ctx) => {
164
+ resetAtLineBreak(ctx);
165
+ ctx.ignore();
166
+ });
167
+ /** Block headers that stand alone on their line, and the declaration whose block they are in. */
168
+ const BLOCK_HEADERS = {
169
+ trigger: new Set(['arguments', 'auth', 'parameters', 'assertions']), // 0009, 0025, 0037
170
+ entity: new Set(['subject', 'models', 'expose', 'hide']), // 0010, 0022
171
+ error: new Set(['data']), // 0023
172
+ };
173
+ /** Lines inside a declaration's block that start with a contextual keyword followed by more. */
174
+ const LINE_STARTS = {
175
+ action: new Set(['native']), // 0036
176
+ entity: new Set(['identity', 'group', 'role', 'membership']), // 0010, 0032
177
+ error: new Set(['data']), // 0023
178
+ };
179
+ const isWord = (token, ...words) => token?.kind === 'identifier' && words.includes(token.text);
180
+ const markKeyword = (token) => {
181
+ if (token !== undefined)
182
+ token.kind = 'keyword';
183
+ };
184
+ /**
185
+ * Mark the words that are keywords only in one place, such as `into` in an import or `auth` in
186
+ * a trigger's block. Elsewhere they stay identifiers. Each line is looked at with the
187
+ * declaration it belongs to: the one started by the last line that begins in the first column.
188
+ */
189
+ const markContextualKeywords = (tokens) => {
190
+ let declaration = '';
191
+ // The column of the declaration's block: the indentation of its first indented line.
192
+ let blockColumn;
193
+ const lines = new Map();
194
+ for (const token of tokens) {
195
+ const line = lines.get(token.line);
196
+ if (line === undefined)
197
+ lines.set(token.line, [token]);
198
+ else
199
+ line.push(token);
200
+ }
201
+ for (const line of lines.values()) {
202
+ const [first] = line;
203
+ if (first === undefined)
204
+ continue;
205
+ if (first.column === 1) {
206
+ const head = first.text === 'export' ? line[1] : first;
207
+ declaration = head?.kind === 'keyword' ? head.text : '';
208
+ blockColumn = undefined;
209
+ }
210
+ else {
211
+ blockColumn ??= first.column;
212
+ }
213
+ // 0006: `into` and `as` on an import line.
214
+ if (first.text === 'import' && first.kind === 'keyword') {
215
+ for (const token of line.slice(1))
216
+ if (isWord(token, 'into', 'as'))
217
+ markKeyword(token);
218
+ }
219
+ // 0006: `as` in a re-export, `export accounts::FindUser as LookupUser`.
220
+ if (first.text === 'export' && first.kind === 'keyword') {
221
+ for (const token of line.slice(2))
222
+ if (isWord(token, 'as'))
223
+ markKeyword(token);
224
+ }
225
+ if (first.column === 1) {
226
+ // 0023: `raises` and `passes` after an action's parameter list.
227
+ if (declaration === 'action') {
228
+ let depth = 0;
229
+ let closed = false;
230
+ for (const token of line) {
231
+ if (token.text === '(')
232
+ depth += 1;
233
+ if (token.text === ')') {
234
+ depth -= 1;
235
+ if (depth === 0)
236
+ closed = true;
237
+ }
238
+ if (closed && depth === 0 && isWord(token, 'raises', 'passes'))
239
+ markKeyword(token);
240
+ }
241
+ }
242
+ // 0010: `permissions <group> has <role>`.
243
+ if (declaration === 'permissions' && isWord(line[2], 'has'))
244
+ markKeyword(line[2]);
245
+ }
246
+ else if (first.column === blockColumn) {
247
+ if (line.length === 1 && BLOCK_HEADERS[declaration]?.has(first.text) === true) {
248
+ markKeyword(first);
249
+ }
250
+ if (line.length > 1 && LINE_STARTS[declaration]?.has(first.text) === true) {
251
+ markKeyword(first);
252
+ // 0032: `with` in `membership user in organization with roles`.
253
+ if (first.text === 'membership' && isWord(line[4], 'with'))
254
+ markKeyword(line[4]);
255
+ }
256
+ }
257
+ else if (declaration === 'entity' && line.length === 3 && isWord(line[1], 'as')) {
258
+ // 0022: `field as name` in an entity's `expose` block.
259
+ markKeyword(line[1]);
260
+ }
261
+ for (const [index, token] of line.entries()) {
262
+ // 0023: `raise <error> with <value>`.
263
+ if (token.text === 'raise' && token.kind === 'keyword') {
264
+ const name = line[index + 1]?.text === '::' ? index + 3 : index + 1;
265
+ if (isWord(line[name + 1], 'with'))
266
+ markKeyword(line[name + 1]);
267
+ }
268
+ // 0010: `@subject can ...` and `@subject is ...`, each with an optional `within`.
269
+ if (token.text === '@subject' && isWord(line[index + 1], 'can', 'is')) {
270
+ markKeyword(line[index + 1]);
271
+ for (const later of line.slice(index + 2, index + 5)) {
272
+ if (isWord(later, 'within'))
273
+ markKeyword(later);
274
+ }
275
+ }
276
+ }
277
+ }
278
+ };
279
+ /**
280
+ * Split Cant source into tokens. Whitespace and comments are skipped, and no token spans a line break.
281
+ *
282
+ * A template string is split around its interpolations: `"a ${b} c"` is the string `"a `, the
283
+ * punctuation `${`, the identifier `b`, the punctuation `}` and the string ` c"`.
284
+ *
285
+ * Words that are keywords only in one place, such as `auth` in a trigger's block, are keywords
286
+ * there and identifiers elsewhere.
287
+ */
288
+ export function tokenize(source) {
289
+ const tokens = lexer
290
+ .reset()
291
+ .input(source)
292
+ .tokens()
293
+ .filter((token) => token.type !== 'EOF')
294
+ .map((token) => ({
295
+ kind: token.type,
296
+ text: token.text,
297
+ line: token.line,
298
+ column: token.column,
299
+ offset: token.pos,
300
+ }));
301
+ markContextualKeywords(tokens);
302
+ return tokens.map(({ kind, text, line, column, offset }) => ({
303
+ kind,
304
+ // No token spans a line break, so the token ends on the line it starts on.
305
+ position: {
306
+ start: { line, column, offset },
307
+ end: { line, column: column + text.length, offset: offset + text.length },
308
+ },
309
+ }));
310
+ }
311
+ //# sourceMappingURL=tokenize.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tokenize.js","sourceRoot":"","sources":["../src/tokenize.ts"],"names":[],"mappings":"AAA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AE,CAAC;oBACrD,IAAI,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC;wBAAE,WAAW,CAAC,KAAK,CAAC,CAAC;gBAClD,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC;AACH,CAAC,CAAC;AAEF;;;;;;;;GAQG;AACH,MAAM,UAAU,QAAQ,CAAC,MAAc;IACrC,MAAM,MAAM,GAAe,KAAK;SAC7B,KAAK,EAAE;SACP,KAAK,CAAC,MAAM,CAAC;SACb,MAAM,EAAE;SACR,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,KAAK,KAAK,CAAC;SACvC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;QACf,IAAI,EAAE,KAAK,CAAC,IAAiB;QAC7B,IAAI,EAAE,KAAK,CAAC,IAAI;QAChB,IAAI,EAAE,KAAK,CAAC,IAAI;QAChB,MAAM,EAAE,KAAK,CAAC,MAAM;QACpB,MAAM,EAAE,KAAK,CAAC,GAAG;KAClB,CAAC,CAAC,CAAC;IAEN,sBAAsB,CAAC,MAAM,CAAC,CAAC;IAE/B,OAAO,MAAM,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,EAAE,EAAE,CAAC,CAAC;QAC3D,IAAI;QACJ,2EAA2E;QAC3E,QAAQ,EAAE;YACR,KAAK,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE;YAC/B,GAAG,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC,MAAM,EAAE;SAC1E;KACF,CAAC,CAAC,CAAC;AACN,CAAC"}
@@ -0,0 +1,83 @@
1
+ /**
2
+ * Values, as the compiler works them out for constant expressions and the interpreter holds them
3
+ * at runtime (docs/adr/0039-compiled-programs.md). For now, only the values that constant
4
+ * expressions can have: collections, files and entities are still to come.
5
+ *
6
+ * A value is plain data, so it can be written to and read from JSON.
7
+ */
8
+ export type Value = BaseValue | EnumValue | ObjectInstance;
9
+ /** A value of one of the base types (docs/adr/0012-base-types.md, 0026). */
10
+ export type BaseValue = {
11
+ kind: 'text';
12
+ value: string;
13
+ } | {
14
+ kind: 'number';
15
+ value: number;
16
+ } | {
17
+ kind: 'boolean';
18
+ value: boolean;
19
+ }
20
+ /** An instant, as milliseconds since 1970-01-01T00:00:00Z (docs/adr/0026-dates-and-times.md). */
21
+ | {
22
+ kind: 'dateTime';
23
+ value: number;
24
+ }
25
+ /** An exact length of time, as a whole number of milliseconds, which can be negative. */
26
+ | {
27
+ kind: 'duration';
28
+ value: number;
29
+ };
30
+ /** A member of an enum: the enum it belongs to, and the member's value (docs/adr/0013-enums.md). */
31
+ export interface EnumValue {
32
+ kind: 'enum';
33
+ /** The enum's symbol, `<module>#<name>` (docs/adr/0039-compiled-programs.md). */
34
+ enum: string;
35
+ value: BaseValue;
36
+ }
37
+ /**
38
+ * An object (docs/adr/0027-object-shapes.md): of a named model, or of an anonymous one when
39
+ * `model` is missing. Its members are in order, which converting it to text follows.
40
+ */
41
+ export interface ObjectInstance {
42
+ kind: 'object';
43
+ /** The model's symbol, `<module>#<name>`. */
44
+ model?: string;
45
+ members: [name: string, value: Value][];
46
+ }
47
+ export declare const text: (value: string) => BaseValue;
48
+ export declare const number: (value: number) => BaseValue;
49
+ export declare const boolean: (value: boolean) => BaseValue;
50
+ /** The range of a `DATE_TIME`, in milliseconds either side of 1970, which JavaScript's `Date` can hold. */
51
+ export declare const DATE_TIME_LIMIT = 8640000000000000;
52
+ /** The instant a `DATE_TIME` literal's string names (docs/adr/0026-dates-and-times.md). */
53
+ export declare const dateTimeMilliseconds: (text: string) => number;
54
+ /**
55
+ * A `DURATION` literal's length in milliseconds, so that equal durations written differently,
56
+ * such as `PT60M` and `PT1H`, are the same value (docs/adr/0026-dates-and-times.md).
57
+ */
58
+ export declare function durationMilliseconds(text: string): number;
59
+ /**
60
+ * Whether two values are equal under `==` (docs/adr/0019-operators.md): base values by value,
61
+ * enum values by their members' values, whatever enum they belong to, and objects only when they
62
+ * are the same object.
63
+ */
64
+ export declare function equal(a: Value, b: Value): boolean;
65
+ /**
66
+ * How two values are ordered, for `<`, `<=`, `>` and `>=`, or `undefined` if they can't be
67
+ * (docs/adr/0019-operators.md): numbers, text by UTF-16 code unit, instants and durations, and
68
+ * enum values by their members' values.
69
+ */
70
+ export declare function compare(a: Value, b: Value): number | undefined;
71
+ /** Convert a value to text (docs/adr/0011-converting-values-to-text.md). */
72
+ export declare function toText(value: Value): string;
73
+ /**
74
+ * A number in plain decimal notation, as short as it can be without losing any of its value, and
75
+ * never with an exponent (docs/adr/0011-converting-values-to-text.md).
76
+ */
77
+ export declare function numberToText(value: number): string;
78
+ /**
79
+ * A duration in ISO 8601 form, in days, hours, minutes and seconds, each as large as it can be
80
+ * and left out when it is zero (docs/adr/0026-dates-and-times.md).
81
+ */
82
+ export declare function durationToText(milliseconds: number): string;
83
+ //# sourceMappingURL=values.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"values.d.ts","sourceRoot":"","sources":["../src/values.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,MAAM,MAAM,KAAK,GAAG,SAAS,GAAG,SAAS,GAAG,cAAc,CAAC;AAE3D,4EAA4E;AAC5E,MAAM,MAAM,SAAS,GACjB;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GAC/B;IAAE,IAAI,EAAE,QAAQ,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GACjC;IAAE,IAAI,EAAE,SAAS,CAAC;IAAC,KAAK,EAAE,OAAO,CAAA;CAAE;AACrC,iGAAiG;GAC/F;IAAE,IAAI,EAAE,UAAU,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE;AACrC,yFAAyF;GACvF;IAAE,IAAI,EAAE,UA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
package/dist/values.js ADDED
@@ -0,0 +1,119 @@
1
+ export const text = (value) => ({ kind: 'text', value });
2
+ export const number = (value) => ({ kind: 'number', value });
3
+ export const boolean = (value) => ({ kind: 'boolean', value });
4
+ /** The range of a `DATE_TIME`, in milliseconds either side of 1970, which JavaScript's `Date` can hold. */
5
+ export const DATE_TIME_LIMIT = 8.64e15;
6
+ /** The instant a `DATE_TIME` literal's string names (docs/adr/0026-dates-and-times.md). */
7
+ export const dateTimeMilliseconds = (text) => Date.parse(text);
8
+ /**
9
+ * A `DURATION` literal's length in milliseconds, so that equal durations written differently,
10
+ * such as `PT60M` and `PT1H`, are the same value (docs/adr/0026-dates-and-times.md).
11
+ */
12
+ export function durationMilliseconds(text) {
13
+ const match = /^P(?:(\d+)D)?(?:T(?:(\d+)H)?(?:(\d+)M)?(?:(\d+(?:\.\d+)?)S)?)?$/.exec(text);
14
+ if (match === null)
15
+ return Number.NaN;
16
+ const [days = '0', hours = '0', minutes = '0', seconds = '0'] = match.slice(1);
17
+ return Math.round(((Number(days) * 24 + Number(hours)) * 60 + Number(minutes)) * 60_000 + Number(seconds) * 1000);
18
+ }
19
+ /**
20
+ * Whether two values are equal under `==` (docs/adr/0019-operators.md): base values by value,
21
+ * enum values by their members' values, whatever enum they belong to, and objects only when they
22
+ * are the same object.
23
+ */
24
+ export function equal(a, b) {
25
+ if (a.kind === 'object' || b.kind === 'object')
26
+ return a === b;
27
+ const left = a.kind === 'enum' ? a.value : a;
28
+ const right = b.kind === 'enum' ? b.value : b;
29
+ return left.kind === right.kind && left.value === right.value;
30
+ }
31
+ /**
32
+ * How two values are ordered, for `<`, `<=`, `>` and `>=`, or `undefined` if they can't be
33
+ * (docs/adr/0019-operators.md): numbers, text by UTF-16 code unit, instants and durations, and
34
+ * enum values by their members' values.
35
+ */
36
+ export function compare(a, b) {
37
+ if (a.kind === 'object' || b.kind === 'object')
38
+ return undefined;
39
+ const left = a.kind === 'enum' ? a.value : a;
40
+ const right = b.kind === 'enum' ? b.value : b;
41
+ if (left.kind !== right.kind || left.kind === 'boolean')
42
+ return undefined;
43
+ return left.value < right.value ? -1 : left.value > right.value ? 1 : 0;
44
+ }
45
+ /** Convert a value to text (docs/adr/0011-converting-values-to-text.md). */
46
+ export function toText(value) {
47
+ switch (value.kind) {
48
+ case 'text':
49
+ return value.value;
50
+ case 'number':
51
+ return numberToText(value.value);
52
+ case 'boolean':
53
+ return String(value.value);
54
+ case 'dateTime':
55
+ return new Date(value.value).toISOString();
56
+ case 'duration':
57
+ return durationToText(value.value);
58
+ case 'enum':
59
+ return toText(value.value);
60
+ case 'object':
61
+ // One layer deep: an object inside is written as its model's name, or `model`.
62
+ return value.members
63
+ .map(([name, member]) => `${name}: ${member.kind === 'object' ? modelName(member) : toText(member)}`)
64
+ .join(', ');
65
+ }
66
+ }
67
+ const modelName = (object) => object.model === undefined ? 'model' : object.model.slice(object.model.lastIndexOf('#') + 1);
68
+ /**
69
+ * A number in plain decimal notation, as short as it can be without losing any of its value, and
70
+ * never with an exponent (docs/adr/0011-converting-values-to-text.md).
71
+ */
72
+ export function numberToText(value) {
73
+ const written = String(value);
74
+ const exponentAt = written.indexOf('e');
75
+ if (exponentAt === -1)
76
+ return written;
77
+ const negative = written.startsWith('-');
78
+ const mantissa = written.slice(negative ? 1 : 0, exponentAt);
79
+ const exponent = Number(written.slice(exponentAt + 1));
80
+ const pointAt = mantissa.indexOf('.');
81
+ const digits = mantissa.replace('.', '');
82
+ // Where the decimal point falls in `digits` once the exponent is applied.
83
+ const point = (pointAt === -1 ? mantissa.length : pointAt) + exponent;
84
+ const plain = point <= 0
85
+ ? `0.${'0'.repeat(-point)}${digits}`
86
+ : point >= digits.length
87
+ ? `${digits}${'0'.repeat(point - digits.length)}`
88
+ : `${digits.slice(0, point)}.${digits.slice(point)}`;
89
+ return negative ? `-${plain}` : plain;
90
+ }
91
+ /**
92
+ * A duration in ISO 8601 form, in days, hours, minutes and seconds, each as large as it can be
93
+ * and left out when it is zero (docs/adr/0026-dates-and-times.md).
94
+ */
95
+ export function durationToText(milliseconds) {
96
+ if (milliseconds === 0)
97
+ return 'PT0S';
98
+ const sign = milliseconds < 0 ? '-' : '';
99
+ let rest = Math.abs(milliseconds);
100
+ const days = Math.floor(rest / 86_400_000);
101
+ rest -= days * 86_400_000;
102
+ const hours = Math.floor(rest / 3_600_000);
103
+ rest -= hours * 3_600_000;
104
+ const minutes = Math.floor(rest / 60_000);
105
+ rest -= minutes * 60_000;
106
+ const seconds = Math.floor(rest / 1000);
107
+ const fraction = rest - seconds * 1000;
108
+ let time = '';
109
+ if (hours > 0)
110
+ time += `${String(hours)}H`;
111
+ if (minutes > 0)
112
+ time += `${String(minutes)}M`;
113
+ if (fraction > 0)
114
+ time += `${String(seconds)}.${String(fraction).padStart(3, '0')}S`;
115
+ else if (seconds > 0)
116
+ time += `${String(seconds)}S`;
117
+ return `${sign}P${days > 0 ? `${String(days)}D` : ''}${time === '' ? '' : `T${time}`}`;
118
+ }
119
+ //# sourceMappingURL=values.js.map