@gramlot/gramlot 0.2.19 → 0.2.20
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/dist/gramlot.js +960 -79
- package/dist/gramlot.min.js +21 -18
- package/package.json +1 -1
- package/src/binding/inline.js +8 -2
- package/src/binding/installation.js +3 -1
- package/src/binding/pointer.js +199 -0
- package/src/binding/providers.js +7 -4
- package/src/binding/runtime.js +10 -5
- package/src/builder/gramlot-builder.js +4 -0
- package/src/builder/source.js +25 -2
- package/src/date-parser/civil.js +61 -0
- package/src/date-parser/index.d.ts +18 -0
- package/src/date-parser/index.js +196 -0
- package/src/date-parser/locales.js +257 -0
- package/src/format.js +121 -0
- package/src/gramlot.d.ts +21 -0
- package/src/gramlot.js +8 -1
- package/src/index.d.ts +4 -1
- package/src/index.js +2 -0
- package/src/renderer/attributes.js +22 -10
- package/src/renderer/gramlot-renderer.js +4 -2
- package/src/server/gramlot-server.d.ts +2 -0
- package/src/server/gramlot-server.js +6 -3
- package/src/server/page-base.js +2 -0
- package/src/server/page.d.ts +2 -0
package/package.json
CHANGED
package/src/binding/inline.js
CHANGED
|
@@ -117,7 +117,7 @@ export class InlineCompiler {
|
|
|
117
117
|
compileExpression(node, attr, code) {
|
|
118
118
|
const expr = code.slice(2);
|
|
119
119
|
const items = sourceAttributeItems(node.getAttr()).filter(([, raw]) => !isExpression(raw));
|
|
120
|
-
const run = this.compile(node, attr, code, items.map(([name]) => name), `return ${expr}`);
|
|
120
|
+
const run = this.compile(node, attr, code, [...items.map(([name]) => name).filter(name => name !== '_F'), '_F'], `return ${expr}`);
|
|
121
121
|
return resolved => {
|
|
122
122
|
const values = {};
|
|
123
123
|
for (const [name, raw] of items) {
|
|
@@ -133,10 +133,16 @@ export class InlineCompiler {
|
|
|
133
133
|
values[name] = node.pointerType(raw) ? node.getRelativeData(raw) : raw;
|
|
134
134
|
}
|
|
135
135
|
}
|
|
136
|
-
return run(values);
|
|
136
|
+
return run({...values, _F: (value, options) => this.runtime.gramlot.format(value, options)});
|
|
137
137
|
};
|
|
138
138
|
}
|
|
139
139
|
|
|
140
|
+
/** Typed placeholder inputs, compiled under the exact declaration activated with Source. */
|
|
141
|
+
compilePointerExpression(node, attr, code, expression) {
|
|
142
|
+
const run = this.compile(node, attr, code, ['_pointerValues', '_F'], `return (${expression}\n)`);
|
|
143
|
+
return values => run({_pointerValues: values, _F: (value, options) => this.runtime.gramlot.format(value, options)});
|
|
144
|
+
}
|
|
145
|
+
|
|
140
146
|
/**
|
|
141
147
|
* Release the compiled declarations of `node`: those of the attributes `names` (Source names; `''`
|
|
142
148
|
* is the node value, `_if`/`_else`/`formula`/`script` their own), or all of them when `names` is null.
|
|
@@ -2,6 +2,7 @@ import {Bag} from '@genrojs/bag';
|
|
|
2
2
|
import {META_ATTRS, SourceBag} from '@genrojs/builders';
|
|
3
3
|
import {isExpression, withoutBindingAttributes} from '../renderer/attributes.js';
|
|
4
4
|
import {GramlotBuilder} from '../builder/gramlot-builder.js';
|
|
5
|
+
import {pointerTransform, resolvePointerAttribute} from './pointer.js';
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* Installation of the `dataSetter`s and of the defaults of one Source branch (source plan §4.6,
|
|
@@ -114,7 +115,8 @@ export class DataInstaller {
|
|
|
114
115
|
const special = {};
|
|
115
116
|
for (const [name, value] of Object.entries(attrs)) {
|
|
116
117
|
if (name.startsWith('attr_')) {
|
|
117
|
-
special[name.slice(5)] =
|
|
118
|
+
special[name.slice(5)] = pointerTransform(value) ? resolvePointerAttribute(node, name)
|
|
119
|
+
: node.pointerType(value) ? node.getRelativeData(value) : value;
|
|
118
120
|
}
|
|
119
121
|
}
|
|
120
122
|
if (!Object.keys(special).length) return;
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import {BagResolver, BagCbResolver} from '@genrojs/bag';
|
|
2
|
+
import {sourceAttributeItems} from '@genrojs/builders';
|
|
3
|
+
|
|
4
|
+
/** Split an authored attribute pointer. Names exclude colon; only the first one separates. */
|
|
5
|
+
export function pointerTransform(value) {
|
|
6
|
+
if (typeof value !== 'string' || !/^[\^=](?!=)/.test(value)) return null;
|
|
7
|
+
const index = value.indexOf(':');
|
|
8
|
+
if (index < 0) return null;
|
|
9
|
+
const expression = value[index + 1] === '=';
|
|
10
|
+
return {pointer: value.slice(0, index), expression, body: value.slice(index + (expression ? 2 : 1))};
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const SLOT = /^\{(\??[A-Za-z_][A-Za-z0-9_]*|\?)?\}/;
|
|
14
|
+
|
|
15
|
+
function failure(node, name, message) {
|
|
16
|
+
return new Error(`${node.nodeTag} '${node.label}': '${name}': ${message}`);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Templates are text. Braces delimit slots; no code or inferred expression mode. */
|
|
20
|
+
export function pointerTemplate(body, read) {
|
|
21
|
+
let result = '', blocked = false;
|
|
22
|
+
for (let i = 0; i < body.length;) {
|
|
23
|
+
if (body[i] !== '{' && body[i] !== '}') { result += body[i++]; continue; }
|
|
24
|
+
const match = body.slice(i).match(SLOT);
|
|
25
|
+
if (!match) throw new SyntaxError('Invalid pointer template placeholder');
|
|
26
|
+
const slot = match[1] ?? '';
|
|
27
|
+
const optional = slot.startsWith('?');
|
|
28
|
+
const value = read(optional ? slot.slice(1) : slot);
|
|
29
|
+
if (value == null || (value === '' && !optional)) blocked = true;
|
|
30
|
+
else result += String(value);
|
|
31
|
+
i += match[0].length;
|
|
32
|
+
}
|
|
33
|
+
return blocked ? null : result;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Replace slots only in expression code, never in quoted strings, comments or regex literals.
|
|
38
|
+
* This scans lexical boundaries; InlineCompiler remains the sole JavaScript evaluator.
|
|
39
|
+
* Empty objects and one-name shorthand objects coincide with slots: use explicit object members.
|
|
40
|
+
*/
|
|
41
|
+
export function pointerExpression(body) {
|
|
42
|
+
const slots = [], identifiers = new Set();
|
|
43
|
+
let text = '', operand = true;
|
|
44
|
+
for (let i = 0; i < body.length;) {
|
|
45
|
+
const start = i, c = body[i];
|
|
46
|
+
if (c === '"' || c === "'" || c === '`') {
|
|
47
|
+
i++;
|
|
48
|
+
while (i < body.length) {
|
|
49
|
+
if (body[i] === '\\') { i += 2; continue; }
|
|
50
|
+
if (body[i++] === c) break;
|
|
51
|
+
}
|
|
52
|
+
text += body.slice(start, i); operand = false; continue;
|
|
53
|
+
}
|
|
54
|
+
if (body.startsWith('//', i)) {
|
|
55
|
+
i = body.indexOf('\n', i); if (i < 0) i = body.length;
|
|
56
|
+
text += body.slice(start, i); continue;
|
|
57
|
+
}
|
|
58
|
+
if (body.startsWith('/*', i)) {
|
|
59
|
+
const end = body.indexOf('*/', i + 2);
|
|
60
|
+
i = end < 0 ? body.length : end + 2;
|
|
61
|
+
text += body.slice(start, i); continue;
|
|
62
|
+
}
|
|
63
|
+
if (c === '/' && operand) {
|
|
64
|
+
let bracket = false; i++;
|
|
65
|
+
while (i < body.length) {
|
|
66
|
+
const current = body[i++];
|
|
67
|
+
if (current === '\\') { i++; continue; }
|
|
68
|
+
if (current === '[') bracket = true;
|
|
69
|
+
else if (current === ']') bracket = false;
|
|
70
|
+
else if (current === '/' && !bracket) break;
|
|
71
|
+
}
|
|
72
|
+
while (/[a-z]/i.test(body[i] ?? '') && i < body.length) i++;
|
|
73
|
+
text += body.slice(start, i); operand = false; continue;
|
|
74
|
+
}
|
|
75
|
+
const slot = c === '{' ? body.slice(i).match(SLOT) : null;
|
|
76
|
+
if (slot) {
|
|
77
|
+
const name = slot[1] ?? '';
|
|
78
|
+
if (name.startsWith('?')) throw new SyntaxError('Optional slots are supported in pointer templates only');
|
|
79
|
+
text += `_pointerValues[${slots.length}]`;
|
|
80
|
+
slots.push(name); i += slot[0].length; operand = false; continue;
|
|
81
|
+
}
|
|
82
|
+
const word = body.slice(i).match(/^[A-Za-z_$][\w$]*/);
|
|
83
|
+
if (word) {
|
|
84
|
+
identifiers.add(word[0]); text += word[0]; i += word[0].length;
|
|
85
|
+
operand = ['return', 'typeof', 'void', 'delete', 'new', 'in', 'instanceof', 'yield', 'await'].includes(word[0]);
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
text += c; i++;
|
|
89
|
+
if (!/\s/.test(c)) operand = /[([{,:;=!?&|+*%~<>\/-]/.test(c);
|
|
90
|
+
}
|
|
91
|
+
return {text, slots, identifiers};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Source-attribute dependencies of a suffix, including referenced helper declarations. */
|
|
95
|
+
export function pointerDependencies(node, raw, seen = new Set()) {
|
|
96
|
+
const transform = pointerTransform(raw);
|
|
97
|
+
const names = [];
|
|
98
|
+
if (transform?.expression) names.push(...pointerExpression(transform.body).slots.filter(Boolean));
|
|
99
|
+
else if (transform) pointerTemplate(transform.body, name => { if (name) names.push(name); return ''; });
|
|
100
|
+
else if (typeof raw === 'string' && raw.startsWith('==')) names.push(...pointerExpression(raw.slice(2)).identifiers);
|
|
101
|
+
else if (typeof raw === 'string') names.push(...[...raw.matchAll(/(?<!\\)\$\{([A-Za-z_][A-Za-z0-9_]*)\}/g)].map(match => match[1]));
|
|
102
|
+
const attrs = new Map(sourceAttributeItems(node.getAttr()));
|
|
103
|
+
for (const name of names) {
|
|
104
|
+
if (seen.has(name) || !attrs.has(name)) continue;
|
|
105
|
+
seen.add(name); pointerDependencies(node, attrs.get(name), seen);
|
|
106
|
+
}
|
|
107
|
+
return seen;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** A per-resolution dependency graph; Source itself remains authored and unchanged. */
|
|
111
|
+
function attributeReader(node, items, context = node) {
|
|
112
|
+
const values = new Map(), active = new Set(), consumed = new Set();
|
|
113
|
+
const compiler = node.rootBuilder.binding?.inlineCompiler;
|
|
114
|
+
// Resolve the complete Source attribute set before applying any suffix. Projection
|
|
115
|
+
// and named references share these typed values; the DOM is never an input.
|
|
116
|
+
const current = new Map();
|
|
117
|
+
for (const [name, raw] of items) {
|
|
118
|
+
if (typeof name !== 'string') continue;
|
|
119
|
+
const pointer = pointerTransform(raw)?.pointer ?? raw;
|
|
120
|
+
current.set(name, pointer instanceof BagResolver ? pointer.resolve()
|
|
121
|
+
: node.pointerType(pointer) ? context.getRelativeData(pointer) : pointer);
|
|
122
|
+
}
|
|
123
|
+
const resolve = name => {
|
|
124
|
+
if (values.has(name)) return values.get(name);
|
|
125
|
+
if (!items.has(name)) throw failure(node, name, 'referenced Source attribute does not exist');
|
|
126
|
+
if (active.has(name)) throw failure(node, name, `cyclic attribute dependency: ${[...active, name].join(' -> ')}`);
|
|
127
|
+
active.add(name);
|
|
128
|
+
const raw = items.get(name), transform = pointerTransform(raw);
|
|
129
|
+
const leading = transform ? current.get(name) : null;
|
|
130
|
+
let result;
|
|
131
|
+
const read = key => {
|
|
132
|
+
if (!key) return leading;
|
|
133
|
+
return resolve(key);
|
|
134
|
+
};
|
|
135
|
+
try {
|
|
136
|
+
if (transform) {
|
|
137
|
+
if (transform.expression) {
|
|
138
|
+
const {text, slots} = pointerExpression(transform.body);
|
|
139
|
+
// Validate references even in static output; JavaScript only executes in the browser.
|
|
140
|
+
const inputs = slots.map(read);
|
|
141
|
+
result = compiler ? compiler.compilePointerExpression(node, name, raw, text)(inputs) : null;
|
|
142
|
+
} else {
|
|
143
|
+
result = pointerTemplate(transform.body, read);
|
|
144
|
+
// A null leading pointer blocks even a constant template.
|
|
145
|
+
if (leading == null) result = null;
|
|
146
|
+
}
|
|
147
|
+
} else if (typeof raw === 'string' && raw.startsWith('==')) {
|
|
148
|
+
const names = pointerExpression(raw.slice(2)).identifiers;
|
|
149
|
+
const args = {};
|
|
150
|
+
for (const key of names) if (key !== name && key !== '_F' && items.has(key)) args[key] = resolve(key);
|
|
151
|
+
result = compiler ? compiler.compile(node, name, raw, [...Object.keys(args), '_F'],
|
|
152
|
+
`return (${raw.slice(2)}\n)` )({...args, _F: (value, options) => node.rootBuilder.binding.gramlot.format(value, options)}) : null;
|
|
153
|
+
} else {
|
|
154
|
+
result = current.get(name);
|
|
155
|
+
if (typeof result === 'string' && !node._getMeta('data_element')) {
|
|
156
|
+
result = result.replace(/(\\)?\$\{([A-Za-z_][A-Za-z0-9_]*)\}/g, (token, escaped, key) => {
|
|
157
|
+
if (escaped) return token.slice(1);
|
|
158
|
+
consumed.add(key); return String(resolve(key) ?? '');
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
values.set(name, result);
|
|
163
|
+
return result;
|
|
164
|
+
} finally { active.delete(name); }
|
|
165
|
+
};
|
|
166
|
+
return {resolve, consumed, current};
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Builder's runtimeToEvaluate hook accepts Bag resolvers as already typed values. Use that
|
|
171
|
+
* existing hook so a result beginning with ^ or = is not read a second time as a pointer.
|
|
172
|
+
* These ephemeral callbacks are never installed on Source/Data or serialized.
|
|
173
|
+
*/
|
|
174
|
+
export function preparePointerItems(node, items) {
|
|
175
|
+
const transformed = [...items].filter(([name, raw]) => typeof name === 'string' && pointerTransform(raw));
|
|
176
|
+
if (!transformed.length) return items;
|
|
177
|
+
const reader = attributeReader(node, items);
|
|
178
|
+
// Keep the original declarations available until every transform has resolved.
|
|
179
|
+
const results = new Map(transformed.map(([name]) => [name, reader.resolve(name)]));
|
|
180
|
+
for (const [name, raw] of items) {
|
|
181
|
+
if (typeof name !== 'string' || pointerTransform(raw)) continue;
|
|
182
|
+
if (raw instanceof BagResolver || node.pointerType(raw)) {
|
|
183
|
+
const result = reader.current.get(name);
|
|
184
|
+
items.set(name, new BagCbResolver(() => result));
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
for (const [name] of transformed) {
|
|
188
|
+
const result = results.get(name);
|
|
189
|
+
items.set(name, new BagCbResolver(() => result));
|
|
190
|
+
}
|
|
191
|
+
if (!node._getMeta('data_element')) for (const name of reader.consumed) items.delete(name);
|
|
192
|
+
return items;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** Resolve a control/structural attribute with the same rules as visual projection. */
|
|
196
|
+
export function resolvePointerAttribute(node, name, context = node) {
|
|
197
|
+
const items = new Map([...Object.entries(node.getAttr()), ...sourceAttributeItems(node.getAttr())]);
|
|
198
|
+
return attributeReader(node, items, context).resolve(name);
|
|
199
|
+
}
|
package/src/binding/providers.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import {AskDialog} from './ask-dialog.js';
|
|
2
2
|
import {Bag} from '@genrojs/bag';
|
|
3
3
|
import {RpcResolver} from './rpc-resolver.js';
|
|
4
|
+
import {pointerTransform, pointerDependencies, resolvePointerAttribute} from './pointer.js';
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* Providers of one Gramlot page (source plan §4.8): the semantic runtime of a `dataFormula`, a
|
|
@@ -25,7 +26,7 @@ const CONTROL_ATTRIBUTES = new Set([
|
|
|
25
26
|
export class Provider {
|
|
26
27
|
#binding;
|
|
27
28
|
#registrations = [];
|
|
28
|
-
#
|
|
29
|
+
#timingRegistrations = new Set();
|
|
29
30
|
#stopTiming = null;
|
|
30
31
|
#cancelDelay = null;
|
|
31
32
|
// The AbortController of the in-flight call of a `dataRpc`: a new call aborts it (latest wins).
|
|
@@ -63,6 +64,7 @@ export class Provider {
|
|
|
63
64
|
}
|
|
64
65
|
this.#closeRegistrations();
|
|
65
66
|
const router = this.binding.runtime.router;
|
|
67
|
+
const timingDependencies = pointerDependencies(node, node.getAttr('_timing'));
|
|
66
68
|
for (const [name, pointer] of node.pointers()) {
|
|
67
69
|
if (name === 'datapath') continue;
|
|
68
70
|
const absolute = node.absDatapath(pointer);
|
|
@@ -70,7 +72,7 @@ export class Provider {
|
|
|
70
72
|
const [path, attr = null] = absolute.split('?', 2);
|
|
71
73
|
const registration = router.register({path, attr, recipient: this});
|
|
72
74
|
this.#registrations.push(registration);
|
|
73
|
-
if (name === '_timing') this.#
|
|
75
|
+
if (name === '_timing' || timingDependencies.has(name)) this.#timingRegistrations.add(registration);
|
|
74
76
|
}
|
|
75
77
|
const subscriptions = this.binding.runtime.gramlot._subscriptions;
|
|
76
78
|
for (const name of Object.keys(node.getAttr())) {
|
|
@@ -98,7 +100,7 @@ export class Provider {
|
|
|
98
100
|
* debounces, the last call wins, and the arguments are read when the body runs.
|
|
99
101
|
*/
|
|
100
102
|
receive(change) {
|
|
101
|
-
if (change.registration
|
|
103
|
+
if (this.#timingRegistrations.has(change.registration)) {
|
|
102
104
|
this.#startTiming();
|
|
103
105
|
return;
|
|
104
106
|
}
|
|
@@ -236,6 +238,7 @@ export class Provider {
|
|
|
236
238
|
#resolved(name) {
|
|
237
239
|
const node = this.node;
|
|
238
240
|
const value = node.getAttr(name);
|
|
241
|
+
if (pointerTransform(value)) return resolvePointerAttribute(node, name);
|
|
239
242
|
return node.pointerType(value) ? node.getRelativeData(value) : value ?? null;
|
|
240
243
|
}
|
|
241
244
|
|
|
@@ -340,7 +343,7 @@ export class Provider {
|
|
|
340
343
|
|
|
341
344
|
#closeRegistrations() {
|
|
342
345
|
for (const registration of this.#registrations.splice(0)) registration.close();
|
|
343
|
-
this.#
|
|
346
|
+
this.#timingRegistrations.clear();
|
|
344
347
|
}
|
|
345
348
|
}
|
|
346
349
|
|
package/src/binding/runtime.js
CHANGED
|
@@ -4,6 +4,7 @@ import {DataInstaller} from './installation.js';
|
|
|
4
4
|
import {ControllerProvider, FormulaProvider, RemoteProvider, RpcProvider} from './providers.js';
|
|
5
5
|
import {InlineCompiler} from './inline.js';
|
|
6
6
|
import {RemoteBinding} from './remote.js';
|
|
7
|
+
import {pointerDependencies} from './pointer.js';
|
|
7
8
|
|
|
8
9
|
// The Provider class of each provider data-element (§4.8).
|
|
9
10
|
const PROVIDERS = {
|
|
@@ -345,7 +346,7 @@ export class NodeBinding {
|
|
|
345
346
|
// The clicks of a button, counted for its whole semantic life: a rebuild keeps them (P9, S12).
|
|
346
347
|
#clickCount = 0;
|
|
347
348
|
// The registration of the node's own variable datapath, read in the parent's context.
|
|
348
|
-
#
|
|
349
|
+
#datapathRegistrations = new Set();
|
|
349
350
|
|
|
350
351
|
constructor(runtime, node) {
|
|
351
352
|
this.#runtime = runtime;
|
|
@@ -397,6 +398,10 @@ export class NodeBinding {
|
|
|
397
398
|
const dataElement = node._getMeta('data_element');
|
|
398
399
|
if (dataElement && !PROVIDERS[node.nodeTag]) return;
|
|
399
400
|
if (!dataElement && typeof node.getAttr('remote') === 'string') this.#remote ??= new RemoteBinding(this);
|
|
401
|
+
const remoteDependencies = new Set();
|
|
402
|
+
if (this.remote) for (const [name, raw] of Object.entries(node.getAttr())) {
|
|
403
|
+
if (name.startsWith('remote_')) for (const dependency of pointerDependencies(node, raw)) remoteDependencies.add(dependency);
|
|
404
|
+
}
|
|
400
405
|
for (const [name, pointer] of node.pointers()) {
|
|
401
406
|
if (dataElement && name !== 'datapath') continue;
|
|
402
407
|
const context = name === 'datapath' ? node.parentNode : node;
|
|
@@ -405,8 +410,8 @@ export class NodeBinding {
|
|
|
405
410
|
const [path, attr = null] = absolute.split('?', 2);
|
|
406
411
|
const registration = this.runtime.router.register({path, attr, recipient: this});
|
|
407
412
|
this.#registrations.push(registration);
|
|
408
|
-
if (name === 'datapath') this.#
|
|
409
|
-
if (this.remote && (name === 'remote' || name.startsWith('remote_'))) this.#remoteRegistrations.add(registration);
|
|
413
|
+
if (name === 'datapath') this.#datapathRegistrations.add(registration);
|
|
414
|
+
if (this.remote && (name === 'remote' || name.startsWith('remote_') || remoteDependencies.has(name))) this.#remoteRegistrations.add(registration);
|
|
410
415
|
}
|
|
411
416
|
if (!dataElement) {
|
|
412
417
|
this.#registerTopics();
|
|
@@ -453,7 +458,7 @@ export class NodeBinding {
|
|
|
453
458
|
* after a registration of `remote` or of a `remote_<k>` parameter requested the fragment again.
|
|
454
459
|
*/
|
|
455
460
|
receive(change) {
|
|
456
|
-
if (change.registration
|
|
461
|
+
if (this.#datapathRegistrations.has(change.registration)) {
|
|
457
462
|
this.runtime.rebindBranch(this.node);
|
|
458
463
|
return;
|
|
459
464
|
}
|
|
@@ -486,7 +491,7 @@ export class NodeBinding {
|
|
|
486
491
|
|
|
487
492
|
#closeRegistrations() {
|
|
488
493
|
for (const registration of this.#registrations.splice(0)) registration.close();
|
|
489
|
-
this.#
|
|
494
|
+
this.#datapathRegistrations.clear();
|
|
490
495
|
this.#remoteRegistrations.clear();
|
|
491
496
|
}
|
|
492
497
|
}
|
|
@@ -15,6 +15,7 @@ import {GramlotHtmlRenderer} from '../renderer/gramlot-html-renderer.js';
|
|
|
15
15
|
import BINDING from './binding.json' with {type: 'json'};
|
|
16
16
|
import WEBCOMPONENT from './webcomponent.json' with {type: 'json'};
|
|
17
17
|
import {webcomponentCode} from '../webcomponent.js';
|
|
18
|
+
import {pointerTransform} from '../binding/pointer.js';
|
|
18
19
|
|
|
19
20
|
// P19: `data` is ambiguous between the legacy data-element and the HTML5 element.
|
|
20
21
|
const DATA_FORBIDDEN = 'data is forbidden: write dataSetter for a Data value or html_data for the HTML5 <data> element';
|
|
@@ -31,6 +32,9 @@ const DATA_INLINE_ATTRIBUTES = {dataFormula: 'formula', dataController: 'script'
|
|
|
31
32
|
|
|
32
33
|
/** The P18 violation of a data-element's attributes, or null. */
|
|
33
34
|
function dataElementError(tag, attrs) {
|
|
35
|
+
if (['input', 'textarea', 'select', 'webcomponent'].includes(tag) && pointerTransform(attrs.value)) {
|
|
36
|
+
return "'value' does not accept pointer suffixes; use an ordinary value pointer";
|
|
37
|
+
}
|
|
34
38
|
if (tag === 'html_button' && Object.keys(attrs).some(name =>
|
|
35
39
|
['action', 'ask', '_delay', 'fire'].includes(name) || name.startsWith('fire_'))) {
|
|
36
40
|
return 'plain HTML button: use button for action, ask, fire or _delay';
|
package/src/builder/source.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import {SourceBag, SourceBagNode, sourceAttributeItems, wrapSource} from '@genrojs/builders';
|
|
2
2
|
import {getSubtypeDict, setSubtypeDict} from '@genrojs/tytx';
|
|
3
|
+
import {pointerTransform, pointerDependencies, preparePointerItems, resolvePointerAttribute} from '../binding/pointer.js';
|
|
3
4
|
|
|
4
5
|
// The inline code attributes of each tag, besides `connect_on<event>` and the `==` expressions of any tag.
|
|
5
6
|
const CODE_ATTRIBUTES = {
|
|
@@ -78,6 +79,9 @@ export class GramlotBuilderBagNode extends SourceBagNode {
|
|
|
78
79
|
}
|
|
79
80
|
code.set(name, text);
|
|
80
81
|
}
|
|
82
|
+
for (const [name, text] of [...sourceAttributeItems(this.getAttr()), ['datapath', this.getAttr('datapath')]]) {
|
|
83
|
+
if (pointerTransform(text)?.expression) code.set(name, text);
|
|
84
|
+
}
|
|
81
85
|
this.#activatedCode = code;
|
|
82
86
|
}
|
|
83
87
|
|
|
@@ -122,7 +126,15 @@ export class GramlotBuilderBagNode extends SourceBagNode {
|
|
|
122
126
|
|
|
123
127
|
/** Builder's `^` pointers, except the attributes ending in `_path`, which hold a bare path. */
|
|
124
128
|
pointers() {
|
|
125
|
-
|
|
129
|
+
const datapath = this.getAttr('datapath');
|
|
130
|
+
const dependencies = pointerTransform(datapath) ? pointerDependencies(this, datapath) : new Set();
|
|
131
|
+
const result = super.pointers().filter(([name, pointer]) => !dependencies.has(name) && !this.#isPathAttribute(name, pointer))
|
|
132
|
+
.map(([name, pointer]) => [name, pointerTransform(pointer)?.pointer ?? pointer]);
|
|
133
|
+
for (const name of dependencies) {
|
|
134
|
+
const value = this.getAttr(name);
|
|
135
|
+
if (this.pointerType(value) === '^') result.push(['datapath', pointerTransform(value)?.pointer ?? value]);
|
|
136
|
+
}
|
|
137
|
+
return result;
|
|
126
138
|
}
|
|
127
139
|
|
|
128
140
|
/**
|
|
@@ -134,10 +146,14 @@ export class GramlotBuilderBagNode extends SourceBagNode {
|
|
|
134
146
|
const items = super.runtimeToEvaluate();
|
|
135
147
|
for (const [name, value] of items) {
|
|
136
148
|
if (!this.pointerType(value)) continue;
|
|
149
|
+
if (pointerTransform(value)) {
|
|
150
|
+
if (typeof name !== 'string') throw new Error(`${this.nodeTag} '${this.label}': pointer suffixes belong to attributes; use format/mask for node text`);
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
137
153
|
if (this.#isPathAttribute(name, value)) items.set(name, value.slice(1));
|
|
138
154
|
else if (this.absDatapath(value) === null) items.set(name, null);
|
|
139
155
|
}
|
|
140
|
-
return items;
|
|
156
|
+
return preparePointerItems(this, items);
|
|
141
157
|
}
|
|
142
158
|
|
|
143
159
|
/**
|
|
@@ -147,6 +163,7 @@ export class GramlotBuilderBagNode extends SourceBagNode {
|
|
|
147
163
|
*/
|
|
148
164
|
pathAttribute(name) {
|
|
149
165
|
const value = this.getAttr(name);
|
|
166
|
+
if (pointerTransform(value)) return resolvePointerAttribute(this, name);
|
|
150
167
|
return this.pointerType(value) && this.#isPathAttribute(name, value) ? value.slice(1) : value;
|
|
151
168
|
}
|
|
152
169
|
|
|
@@ -232,6 +249,7 @@ export class GramlotBuilderBagNode extends SourceBagNode {
|
|
|
232
249
|
|
|
233
250
|
/** Builder's absDatapath, keeping `?attr` on symbolic paths. */
|
|
234
251
|
absDatapath(path) {
|
|
252
|
+
path = pointerTransform(path)?.pointer ?? path;
|
|
235
253
|
const bare = this.pointerType(path) ? path.slice(1) : path;
|
|
236
254
|
if (bare.startsWith('#') && bare.includes('?')) {
|
|
237
255
|
const [target, attr] = bare.split('?', 2);
|
|
@@ -288,6 +306,10 @@ export class GramlotBuilderBagNode extends SourceBagNode {
|
|
|
288
306
|
* the context of the carrier's parent; the value counts as if written in place of the pointer.
|
|
289
307
|
*/
|
|
290
308
|
#variableDatapath(carrier, pointer) {
|
|
309
|
+
if (pointerTransform(pointer)) {
|
|
310
|
+
const value = resolvePointerAttribute(carrier, 'datapath', carrier.parentNode);
|
|
311
|
+
return value == null || value === '' ? null : value;
|
|
312
|
+
}
|
|
291
313
|
const path = carrier.parentNode.absDatapath(pointer);
|
|
292
314
|
if (path === null) return null;
|
|
293
315
|
const value = this.data.getItem(path);
|
|
@@ -296,6 +318,7 @@ export class GramlotBuilderBagNode extends SourceBagNode {
|
|
|
296
318
|
|
|
297
319
|
/** Whether `name` ends in `_path`; a pointer there loses its symbol, with one warning per attribute. */
|
|
298
320
|
#isPathAttribute(name, value) {
|
|
321
|
+
if (pointerTransform(value)) return false;
|
|
299
322
|
if (typeof name !== 'string' || !name.endsWith('_path')) return false;
|
|
300
323
|
if (!this.#reportedPathPointers.has(name)) {
|
|
301
324
|
this.#reportedPathPointers.add(name);
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// Copyright 2026 Softwell S.r.l. - SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// Gregorian civil calendar arithmetic. No clock, timezone, or Date object.
|
|
3
|
+
export function daysInMonth(year, month) {
|
|
4
|
+
if (month === 2) return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0) ? 29 : 28;
|
|
5
|
+
return [4, 6, 9, 11].includes(month) ? 30 : 31;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function civil(year, month, day) {
|
|
9
|
+
if (![year, month, day].every(Number.isInteger) || year < 1 || year > 9999 ||
|
|
10
|
+
month < 1 || month > 12 || day < 1 || day > daysInMonth(year, month)) {
|
|
11
|
+
throw new RangeError('Date must exist in Gregorian years 0001–9999.');
|
|
12
|
+
}
|
|
13
|
+
return `${String(year).padStart(4, '0')}-${String(month).padStart(2, '0')}-${String(day).padStart(2, '0')}`;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function parts(date) {
|
|
17
|
+
if (typeof date !== 'string' || !/^\d{4}-\d{2}-\d{2}$/.test(date)) {
|
|
18
|
+
throw new RangeError('Expected a civil date in YYYY-MM-DD format.');
|
|
19
|
+
}
|
|
20
|
+
const result = date.split('-').map(Number);
|
|
21
|
+
civil(...result);
|
|
22
|
+
return result;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function ordinal(year, month, day) {
|
|
26
|
+
const previous = year - 1;
|
|
27
|
+
let result = 365 * previous + Math.floor(previous / 4) - Math.floor(previous / 100) +
|
|
28
|
+
Math.floor(previous / 400) + day - 1;
|
|
29
|
+
for (let m = 1; m < month; m++) result += daysInMonth(year, m);
|
|
30
|
+
return result;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function addDays(date, offset) {
|
|
34
|
+
let target = ordinal(...parts(date)) + offset;
|
|
35
|
+
if (!Number.isSafeInteger(target) || target < 0 || target > ordinal(9999, 12, 31)) {
|
|
36
|
+
throw new RangeError('Date offset exceeds Gregorian years 0001–9999.');
|
|
37
|
+
}
|
|
38
|
+
// Binary search keeps arbitrarily large supported offsets bounded.
|
|
39
|
+
let low = 1, high = 10000;
|
|
40
|
+
while (high - low > 1) {
|
|
41
|
+
const middle = Math.floor((low + high) / 2);
|
|
42
|
+
if (ordinal(middle, 1, 1) <= target) low = middle;
|
|
43
|
+
else high = middle;
|
|
44
|
+
}
|
|
45
|
+
target -= ordinal(low, 1, 1);
|
|
46
|
+
let month = 1;
|
|
47
|
+
while (target >= daysInMonth(low, month)) target -= daysInMonth(low, month++);
|
|
48
|
+
return civil(low, month, target + 1);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function monday(date) {
|
|
52
|
+
return addDays(date, -(ordinal(...parts(date)) % 7));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function monthPeriod(year, month, offset = 0, count = 1) {
|
|
56
|
+
const index = year * 12 + month - 1 + offset;
|
|
57
|
+
const y = Math.floor(index / 12), m = ((index % 12) + 12) % 12 + 1;
|
|
58
|
+
const endIndex = index + count - 1;
|
|
59
|
+
const ey = Math.floor(endIndex / 12), em = ((endIndex % 12) + 12) % 12 + 1;
|
|
60
|
+
return {start: civil(y, m, 1), end: civil(ey, em, daysInMonth(ey, em))};
|
|
61
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/** Natural-language civil dates and periods, independent of DOM, clock and timezone. */
|
|
2
|
+
export interface DateExpressionOptions {
|
|
3
|
+
/** Required Gregorian civil ISO date, YYYY-MM-DD (0001–9999). */
|
|
4
|
+
workdate: string;
|
|
5
|
+
/** Supported Italian/English locale; defaults to it-IT. */
|
|
6
|
+
locale?: string;
|
|
7
|
+
/** Integer 0–99, default 20. Inclusive window [work year + ahead - 100, work year + ahead - 1]. */
|
|
8
|
+
twoDigitYearAhead?: number;
|
|
9
|
+
}
|
|
10
|
+
export type DateExpressionErrorCode = 'INVALID_CONTEXT' | 'INVALID_INPUT' |
|
|
11
|
+
'UNRECOGNIZED_EXPRESSION' | 'INVALID_DATE' |
|
|
12
|
+
'INVALID_RANGE';
|
|
13
|
+
export type DateExpressionResult =
|
|
14
|
+
{ok: true; kind: 'date'; date: string} |
|
|
15
|
+
{ok: true; kind: 'period'; start: string | null; end: string | null} |
|
|
16
|
+
{ok: false; error: {code: DateExpressionErrorCode; message: string}};
|
|
17
|
+
/** Parse a complete expression without reading or modifying page state. */
|
|
18
|
+
export function parseDateExpression(text: string, options: DateExpressionOptions): DateExpressionResult;
|