@formulavize/lezer-fiz 2.1.0 → 2.1.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.
@@ -19,12 +19,12 @@ jobs:
19
19
  - name: Install pnpm
20
20
  uses: pnpm/action-setup@v4
21
21
  with:
22
- version: 10
22
+ version: 11
23
23
 
24
24
  - name: Set up Node.js
25
25
  uses: actions/setup-node@v4
26
26
  with:
27
- node-version: 24
27
+ node-version: 26
28
28
  cache: pnpm
29
29
 
30
30
  - run: pnpm install --frozen-lockfile
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@formulavize/lezer-fiz",
3
- "version": "2.1.0",
3
+ "version": "2.1.1",
4
4
  "description": "fiz grammar for lezer",
5
5
  "main": "dist/index.cjs",
6
6
  "type": "module",
@@ -12,20 +12,20 @@
12
12
  "types": "dist/index.d.ts",
13
13
  "dependencies": {
14
14
  "@lezer/highlight": "^1.2.3",
15
- "@lezer/lr": "^1.4.8"
15
+ "@lezer/lr": "^1.4.10"
16
16
  },
17
17
  "devDependencies": {
18
18
  "@lezer/generator": "^1.8.0",
19
19
  "chokidar-cli": "^3.0.0",
20
- "mocha": "^11.7.5",
20
+ "mocha": "^11.8.0",
21
21
  "npm-run-all": "^4.1.5",
22
22
  "only-allow": "^1.2.2",
23
- "prettier": "^3.8.1",
24
- "rollup": "^4.60.1"
23
+ "prettier": "^3.9.6",
24
+ "rollup": "^4.62.4"
25
25
  },
26
26
  "engines": {
27
- "node": ">=24.0",
28
- "pnpm": ">=10"
27
+ "node": ">=26.0",
28
+ "pnpm": ">=11"
29
29
  },
30
30
  "scripts": {
31
31
  "preinstall": "npx only-allow pnpm",
@@ -0,0 +1,517 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>fiz Grammar — Railroad Diagrams</title>
7
+ <style>
8
+ body {
9
+ font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
10
+ max-width: 1200px;
11
+ margin: 0 auto;
12
+ padding: 2rem;
13
+ background: #fafafa;
14
+ color: #222;
15
+ }
16
+ h1 { margin-bottom: 0.25rem; }
17
+ h1 + p { color: #666; margin-top: 0; }
18
+ .toc { margin: 1rem 0 2rem; columns: 3; column-gap: 2rem; }
19
+ .toc a { font-family: "SF Mono", "Fira Code", monospace; font-size: 0.9rem; display: block; margin-bottom: 0.3rem; }
20
+ section { margin-bottom: 2.5rem; scroll-margin-top: 1rem; }
21
+ h2 {
22
+ font-size: 1.1rem;
23
+ font-family: "SF Mono", "Fira Code", "Cascadia Code", monospace;
24
+ background: #e8e8e8;
25
+ display: inline-block;
26
+ padding: 0.2rem 0.6rem;
27
+ border-radius: 4px;
28
+ margin-bottom: 0.5rem;
29
+ }
30
+ .note {
31
+ font-size: 0.85rem;
32
+ color: #666;
33
+ margin-top: 0.25rem;
34
+ font-style: italic;
35
+ }
36
+ svg { display: block; margin: 0.5rem 0; border: 1px solid #ddd; border-radius: 6px; background: #fff; }
37
+ </style>
38
+ </head>
39
+ <body>
40
+
41
+ <h1>fiz Grammar — Railroad Diagrams</h1>
42
+ <p>Auto-generated from <code>lezer-fiz/src/fiz.grammar</code></p>
43
+
44
+ <div id="toc" class="toc"></div>
45
+ <div id="diagrams"></div>
46
+
47
+ <script>
48
+ // ── Minimal railroad diagram renderer ──────────────────────────
49
+ // Generates SVG railroad/syntax diagrams without any dependencies.
50
+
51
+ const FONT = '13px "SF Mono","Fira Code","Cascadia Code",monospace';
52
+ const PAD = 6; // text padding inside boxes
53
+ const H = 24; // box height
54
+ const R = 10; // arc radius
55
+ const GAP = 10; // horizontal gap between elements
56
+ const VGAP = 8; // vertical gap between choice branches
57
+ const ARROW = 8; // arrowhead size
58
+
59
+ // Measure text width (approximate, works offline)
60
+ const _canvas = document.createElement("canvas");
61
+ const _ctx = _canvas.getContext("2d");
62
+ _ctx.font = FONT;
63
+ function tw(text) { return _ctx.measureText(text).width; }
64
+
65
+ // SVG helpers
66
+ function svgEl(tag, attrs, ...children) {
67
+ const el = document.createElementNS("http://www.w3.org/2000/svg", tag);
68
+ for (const [k, v] of Object.entries(attrs || {})) el.setAttribute(k, v);
69
+ for (const c of children) {
70
+ if (typeof c === "string") el.appendChild(document.createTextNode(c));
71
+ else if (c) el.appendChild(c);
72
+ }
73
+ return el;
74
+ }
75
+
76
+ function path(d) { return svgEl("path", { d, fill: "none", stroke: "#333", "stroke-width": "2" }); }
77
+
78
+ // ── Node types ──────────────────────────────────────────────────
79
+
80
+ class Terminal {
81
+ constructor(text) { this.text = text; this.w = tw(text) + PAD * 2 + 8; this.h = H; this.label = null; }
82
+ render(x, y) {
83
+ const g = svgEl("g");
84
+ const r = 6;
85
+ g.appendChild(svgEl("rect", { x, y: y - this.h / 2, width: this.w, height: this.h, rx: r, ry: r, fill: "#ddf4ff", stroke: "#333", "stroke-width": "1.5" }));
86
+ g.appendChild(svgEl("text", { x: x + this.w / 2, y: y + 4.5, "text-anchor": "middle", fill: "#222", style: `font: ${FONT}` }, this.text));
87
+ return g;
88
+ }
89
+ }
90
+
91
+ class NonTerminal {
92
+ constructor(text) { this.text = text; this.w = tw(text) + PAD * 2 + 8; this.h = H; this.label = null; }
93
+ render(x, y) {
94
+ const g = svgEl("g");
95
+ g.appendChild(svgEl("rect", { x, y: y - this.h / 2, width: this.w, height: this.h, fill: "#fff3cd", stroke: "#333", "stroke-width": "1.5" }));
96
+ g.appendChild(svgEl("text", { x: x + this.w / 2, y: y + 4.5, "text-anchor": "middle", fill: "#222", style: `font: ${FONT}` }, this.text));
97
+ return g;
98
+ }
99
+ }
100
+
101
+ class Seq {
102
+ constructor(items) {
103
+ this.items = items;
104
+ this.h = Math.max(...items.map(i => i.h));
105
+ this.w = items.reduce((s, i) => s + i.w, 0) + GAP * (items.length - 1);
106
+ this.label = null;
107
+ }
108
+ render(x, y) {
109
+ const g = svgEl("g");
110
+ let cx = x;
111
+ for (let i = 0; i < this.items.length; i++) {
112
+ const item = this.items[i];
113
+ g.appendChild(item.render(cx, y));
114
+ cx += item.w;
115
+ if (i < this.items.length - 1) {
116
+ g.appendChild(path(`M${cx},${y} h${GAP}`));
117
+ cx += GAP;
118
+ }
119
+ }
120
+ return g;
121
+ }
122
+ }
123
+
124
+ function measureBranches(items) {
125
+ // For each item, measure height above and below center
126
+ const above = [];
127
+ const below = [];
128
+ for (const item of items) {
129
+ above.push(item.h / 2);
130
+ below.push(item.h / 2);
131
+ }
132
+ return { above, below };
133
+ }
134
+
135
+ class ChoiceNode {
136
+ constructor(defaultIdx, items) {
137
+ this.defaultIdx = defaultIdx;
138
+ this.items = items;
139
+ const maxW = Math.max(...items.map(i => i.w));
140
+ this.innerW = maxW;
141
+ this.w = maxW + R * 4 + GAP * 2;
142
+
143
+ // Calculate total height
144
+ let totalH = 0;
145
+ for (let i = 0; i < items.length; i++) {
146
+ totalH += items[i].h;
147
+ if (i < items.length - 1) totalH += VGAP;
148
+ }
149
+ this.h = totalH;
150
+ this.label = null;
151
+
152
+ // Pre-calculate y offsets for each branch (relative to center of default branch)
153
+ this.branchY = [];
154
+ let accum = 0;
155
+ for (let i = 0; i < items.length; i++) {
156
+ this.branchY.push(accum + items[i].h / 2);
157
+ accum += items[i].h + VGAP;
158
+ }
159
+ const defaultCenter = this.branchY[defaultIdx];
160
+ for (let i = 0; i < this.branchY.length; i++) this.branchY[i] -= defaultCenter;
161
+ }
162
+ render(x, y) {
163
+ const g = svgEl("g");
164
+ const midX = x + this.w / 2;
165
+ for (let i = 0; i < this.items.length; i++) {
166
+ const item = this.items[i];
167
+ const by = y + this.branchY[i];
168
+ const itemX = midX - item.w / 2;
169
+ // Left connector
170
+ if (i === this.defaultIdx) {
171
+ g.appendChild(path(`M${x},${y} H${itemX}`));
172
+ } else {
173
+ const goDown = by > y;
174
+ if (goDown) {
175
+ g.appendChild(path(`M${x},${y} v${by - y - R} a${R},${R} 0 0,0 ${R},${R} H${itemX}`));
176
+ } else {
177
+ g.appendChild(path(`M${x},${y} v${by - y + R} a${R},${R} 0 0,1 ${R},-${R} H${itemX}`));
178
+ }
179
+ }
180
+ g.appendChild(item.render(itemX, by));
181
+ // Right connector
182
+ const rightEnd = x + this.w;
183
+ const itemRight = itemX + item.w;
184
+ if (i === this.defaultIdx) {
185
+ g.appendChild(path(`M${itemRight},${by} H${rightEnd}`));
186
+ } else {
187
+ const goUp = by > y;
188
+ if (goUp) {
189
+ g.appendChild(path(`M${itemRight},${by} H${rightEnd - R} a${R},${R} 0 0,0 ${R},-${R} V${y}`));
190
+ } else {
191
+ g.appendChild(path(`M${itemRight},${by} H${rightEnd - R} a${R},${R} 0 0,1 ${R},${R} V${y}`));
192
+ }
193
+ }
194
+ }
195
+ return g;
196
+ }
197
+ }
198
+
199
+ class LoopNode {
200
+ // item going forward, rep going backward (optional)
201
+ constructor(item, rep, skipAllowed) {
202
+ this.item = item;
203
+ this.rep = rep;
204
+ this.skipAllowed = skipAllowed;
205
+ this.w = Math.max(item.w, rep ? rep.w : 0) + R * 4 + GAP * 2;
206
+ const repH = rep ? rep.h : H * 0.6;
207
+ this.h = item.h + repH + VGAP;
208
+ this.label = null;
209
+ }
210
+ render(x, y) {
211
+ const g = svgEl("g");
212
+ const midX = x + this.w / 2;
213
+ const itemX = midX - this.item.w / 2;
214
+ const loopY = y + this.item.h / 2 + VGAP + (this.rep ? this.rep.h / 2 : 4);
215
+
216
+ if (this.skipAllowed) {
217
+ // Draw skip path above
218
+ const skipY = y - this.item.h / 2 - VGAP;
219
+ g.appendChild(path(`M${x},${y} v${skipY - y + R} a${R},${R} 0 0,1 ${R},-${R} H${x + this.w - R} a${R},${R} 0 0,1 ${R},${R} V${y}`));
220
+ }
221
+
222
+ // Forward path
223
+ g.appendChild(path(`M${x},${y} H${itemX}`));
224
+ g.appendChild(this.item.render(itemX, y));
225
+ g.appendChild(path(`M${itemX + this.item.w},${y} H${x + this.w}`));
226
+
227
+ // Loop-back path
228
+ const repX = this.rep ? midX - this.rep.w / 2 : midX;
229
+ // Right side going down
230
+ g.appendChild(path(`M${x + this.w - R},${y} a${R},${R} 0 0,1 ${R},${R} V${loopY - R} a${R},${R} 0 0,1 -${R},${R}`));
231
+ if (this.rep) {
232
+ g.appendChild(path(`M${x + this.w - R},${loopY} H${repX + this.rep.w}`));
233
+ g.appendChild(this.rep.render(repX, loopY));
234
+ g.appendChild(path(`M${repX},${loopY} H${x + R}`));
235
+ } else {
236
+ // Just a line with arrow
237
+ g.appendChild(path(`M${x + this.w - R},${loopY} H${x + R}`));
238
+ // Arrow in middle pointing left
239
+ const arrowX = midX;
240
+ g.appendChild(svgEl("polygon", { points: `${arrowX + 4},${loopY - 4} ${arrowX - 4},${loopY} ${arrowX + 4},${loopY + 4}`, fill: "#333", stroke: "none" }));
241
+ }
242
+ // Left side going up
243
+ g.appendChild(path(`M${x + R},${loopY} a${R},${R} 0 0,1 -${R},-${R} V${y + R} a${R},${R} 0 0,1 ${R},-${R}`));
244
+
245
+ return g;
246
+ }
247
+ }
248
+
249
+ class OptNode {
250
+ constructor(item) {
251
+ this.item = item;
252
+ this.w = item.w + R * 4 + GAP * 2;
253
+ this.h = item.h + VGAP + R * 2;
254
+ this.label = null;
255
+ }
256
+ render(x, y) {
257
+ const g = svgEl("g");
258
+ const midX = x + this.w / 2;
259
+ const itemX = midX - this.item.w / 2;
260
+
261
+ // Main path through item
262
+ g.appendChild(path(`M${x},${y} H${itemX}`));
263
+ g.appendChild(this.item.render(itemX, y));
264
+ g.appendChild(path(`M${itemX + this.item.w},${y} H${x + this.w}`));
265
+
266
+ // Skip path (below)
267
+ const skipY = y + this.item.h / 2 + VGAP;
268
+ g.appendChild(path(`M${x},${y} v${skipY - y - R} a${R},${R} 0 0,0 ${R},${R} H${x + this.w - R} a${R},${R} 0 0,0 ${R},-${R} V${y}`));
269
+
270
+ return g;
271
+ }
272
+ }
273
+
274
+ // ── Builder functions ───────────────────────────────────────────
275
+
276
+ function T(text) { return new Terminal(text); }
277
+ function NT(text) { return new NonTerminal(text); }
278
+ function Seq_(...items) { return items.length === 1 ? items[0] : new Seq(items); }
279
+ function Choice(defaultIdx, ...items) { return items.length === 1 ? items[0] : new ChoiceNode(defaultIdx, items); }
280
+ function Opt(item) { return new OptNode(item); }
281
+ function ZeroOrMore(item, rep) { return new LoopNode(item, rep || null, true); }
282
+ function OneOrMore(item, rep) { return new LoopNode(item, rep || null, false); }
283
+
284
+ function renderDiagram(node) {
285
+ const padX = 20;
286
+ const padY = 20;
287
+ const totalW = node.w + padX * 2 + 30; // extra for entry/exit lines
288
+ const totalH = node.h + padY * 2;
289
+
290
+ const svg = svgEl("svg", {
291
+ width: totalW,
292
+ height: totalH,
293
+ viewBox: `0 0 ${totalW} ${totalH}`,
294
+ class: "railroad-diagram",
295
+ });
296
+
297
+ const centerY = totalH / 2;
298
+ const startX = padX;
299
+ const nodeX = padX + 15;
300
+
301
+ // Entry line
302
+ svg.appendChild(path(`M${startX},${centerY} H${nodeX}`));
303
+ // Entry circle
304
+ svg.appendChild(svgEl("circle", { cx: startX, cy: centerY, r: 3, fill: "#333" }));
305
+
306
+ // Main content
307
+ svg.appendChild(node.render(nodeX, centerY));
308
+
309
+ // Exit line
310
+ const exitStart = nodeX + node.w;
311
+ const exitEnd = exitStart + 15;
312
+ svg.appendChild(path(`M${exitStart},${centerY} H${exitEnd}`));
313
+ // Exit double line
314
+ svg.appendChild(svgEl("circle", { cx: exitEnd, cy: centerY, r: 3, fill: "#333" }));
315
+ svg.appendChild(svgEl("circle", { cx: exitEnd, cy: centerY, r: 6, fill: "none", stroke: "#333", "stroke-width": "1.5" }));
316
+
317
+ return svg;
318
+ }
319
+
320
+ // ── Grammar definitions ─────────────────────────────────────────
321
+
322
+ const rules = [];
323
+ function def(name, node, note) { rules.push({ name, node, note }); }
324
+
325
+ // Recipe (top-level)
326
+ def("Recipe",
327
+ ZeroOrMore(NT("statement"))
328
+ );
329
+
330
+ // statement
331
+ def("statement",
332
+ Seq_(
333
+ Choice(0,
334
+ NT("Call"),
335
+ NT("RhsVariable"),
336
+ NT("Assignment"),
337
+ NT("StyleTagDeclaration"),
338
+ NT("StyleBinding"),
339
+ NT("GlobalStyleBinding"),
340
+ NT("Namespace"),
341
+ NT("Import"),
342
+ ),
343
+ NT("semi"),
344
+ )
345
+ );
346
+
347
+ // semi
348
+ def("semi",
349
+ Choice(0, T(";"), T("\u23CE auto-insert")),
350
+ "Semicolons are automatically inserted at newlines (ASI)"
351
+ );
352
+
353
+ // QualifiableIdentifier
354
+ def("QualifiableIdentifier",
355
+ Seq_(T("Identifier"), ZeroOrMore(Seq_(T("."), T("Identifier"))))
356
+ );
357
+
358
+ // Call
359
+ def("Call",
360
+ Seq_(T("Identifier"), NT("ArgList"), Opt(NT("StyleArgList")))
361
+ );
362
+
363
+ // ArgList
364
+ def("ArgList",
365
+ Seq_(T("("), Opt(Seq_(NT("value"), ZeroOrMore(Seq_(T(","), NT("value"))))), T(")"))
366
+ );
367
+
368
+ // value
369
+ def("value",
370
+ Choice(0, NT("RhsVariable"), NT("Call")),
371
+ "Namespace and Import are disallowed inside ArgList for readability"
372
+ );
373
+
374
+ // RhsVariable
375
+ def("RhsVariable",
376
+ NT("QualifiableIdentifier")
377
+ );
378
+
379
+ // LhsVariable
380
+ def("LhsVariable",
381
+ Seq_(T("Identifier"), Opt(NT("StyleArgList")))
382
+ );
383
+
384
+ // Assignment
385
+ def("Assignment",
386
+ Seq_(
387
+ NT("LhsVariable"),
388
+ ZeroOrMore(Seq_(T(","), NT("LhsVariable"))),
389
+ T("="),
390
+ Choice(0, NT("Call"), NT("RhsVariable"), NT("Namespace"), NT("Import")),
391
+ )
392
+ );
393
+
394
+ // Namespace
395
+ def("Namespace",
396
+ Seq_(Opt(T("Identifier")), NT("StatementList"), Opt(NT("ArgList")), Opt(NT("StyleArgList")))
397
+ );
398
+
399
+ // StatementList
400
+ def("StatementList",
401
+ Seq_(T("["), ZeroOrMore(NT("statement")), T("]"))
402
+ );
403
+
404
+ // Import
405
+ def("Import",
406
+ Seq_(Opt(T("Identifier")), T("@"), T("StringLiteral")),
407
+ "StringLiteral is a path-like resource identifier"
408
+ );
409
+
410
+ // StyleTagDeclaration
411
+ def("StyleTagDeclaration",
412
+ Seq_(T("#"), T("Identifier"), NT("StyleArgList"))
413
+ );
414
+
415
+ // StyleTag
416
+ def("StyleTag",
417
+ Seq_(T("#"), NT("QualifiableIdentifier"))
418
+ );
419
+
420
+ // StyleArgList
421
+ def("StyleArgList",
422
+ Seq_(T("{"), ZeroOrMore(Choice(0, NT("StyleDeclaration"), NT("StyleTag"), T("StringLiteral"))), T("}"))
423
+ );
424
+
425
+ // StyleDeclaration
426
+ def("StyleDeclaration",
427
+ Seq_(NT("PropertyName"), T(":"), NT("styleValue"), ZeroOrMore(Seq_(T(","), NT("styleValue"))), NT("semi"))
428
+ );
429
+
430
+ // styleValue
431
+ def("styleValue",
432
+ Choice(0, T("ColorLiteral"), NT("NumberLiteral"), T("StringLiteral"))
433
+ );
434
+
435
+ // StyleBinding
436
+ def("StyleBinding",
437
+ Seq_(T("%"), T("Identifier"), NT("StyleArgList"))
438
+ );
439
+
440
+ // GlobalStyleBinding
441
+ def("GlobalStyleBinding",
442
+ Seq_(T("*"), T("Identifier"), NT("StyleArgList"))
443
+ );
444
+
445
+ // NumberLiteral
446
+ def("NumberLiteral",
447
+ Seq_(T("number"), Opt(T("Unit")))
448
+ );
449
+
450
+ // PropertyName
451
+ def("PropertyName",
452
+ T("cssIdentifier")
453
+ );
454
+
455
+ // Identifier (token)
456
+ def("Identifier",
457
+ Seq_(T("letter | _ | $"), ZeroOrMore(T("letter | digit | _ | $"))),
458
+ "Token rule — also allows Unicode \\u00A1+"
459
+ );
460
+
461
+ // ColorLiteral (token)
462
+ def("ColorLiteral",
463
+ Seq_(T("#"), T("hex\u00D7 3"), Opt(Seq_(T("hex"), Opt(Seq_(T("hex\u00D7 2"), Opt(T("hex\u00D7 2"))))))),
464
+ "Supports #RGB, #RGBA, #RRGGBB, #RRGGBBAA"
465
+ );
466
+
467
+ // StringLiteral (token)
468
+ def("StringLiteral",
469
+ Choice(0,
470
+ Seq_(T('"'), ZeroOrMore(Choice(0, T("char"), Seq_(T("\\"), T("any")))), T('"')),
471
+ Seq_(T("'"), ZeroOrMore(Choice(0, T("char"), Seq_(T("\\"), T("any")))), T("'")),
472
+ ),
473
+ "Double or single quoted with backslash escapes"
474
+ );
475
+
476
+ // ── Render all ──────────────────────────────────────────────────
477
+
478
+ const container = document.getElementById("diagrams");
479
+ const tocEl = document.getElementById("toc");
480
+
481
+ for (const { name, node, note } of rules) {
482
+ const id = "rule-" + name;
483
+
484
+ // TOC entry
485
+ const a = document.createElement("a");
486
+ a.href = "#" + id;
487
+ a.textContent = name;
488
+ tocEl.appendChild(a);
489
+
490
+ // Section
491
+ const section = document.createElement("section");
492
+ section.id = id;
493
+ const heading = document.createElement("h2");
494
+ heading.textContent = name;
495
+ section.appendChild(heading);
496
+
497
+ try {
498
+ section.appendChild(renderDiagram(node));
499
+ } catch (e) {
500
+ const err = document.createElement("pre");
501
+ err.style.color = "red";
502
+ err.textContent = "Error rendering: " + e.message;
503
+ section.appendChild(err);
504
+ }
505
+
506
+ if (note) {
507
+ const p = document.createElement("p");
508
+ p.className = "note";
509
+ p.textContent = note;
510
+ section.appendChild(p);
511
+ }
512
+
513
+ container.appendChild(section);
514
+ }
515
+ </script>
516
+ </body>
517
+ </html>