summaryrefslogtreecommitdiff
path: root/examples/live/src
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-17 07:51:24 -0300
committerGabriel Schneider <[email protected]>2026-11-17 12:15:01 -0300
commitf37732448a2a374281c9610d05a10315e97dcbbf (patch)
tree8778dba261ce8dcddcfea894d838e0b51b0255cb /examples/live/src
parentfad609ddb13cbc0f33150fc3a9dfc25051641cd8 (diff)
downloadsketchbook-f37732448a2a374281c9610d05a10315e97dcbbf.tar.gz
sketchbook-f37732448a2a374281c9610d05a10315e97dcbbf.zip
examples/live: the language's grammar, its colours and a new line's indent
grammar/grammar.js is the language as src/Lang.zig parses it, for tree-sitter (24 lines): lines of `name = expression` parted by newlines, `+ -` below `* /` below unary `-`, calls, parentheses, numbers (Lang's digits, point and `e`), names, `#` comments as extras. `tree-sitter generate` (tree-sitter 0.26.9; ABI 14, the CLI's without a tree-sitter.json) wrote grammar/src/parser.c (918 lines) and its tree_sitter/ headers, committed: the build compiles them into the sketch as a static library and never runs the generator. The `Language` is handed to the editor at run time (`tree_sitter_live`, queries/highlights.scm and queries/indents.scm embedded). The parse is a module's `Syntax`, kept between calls so Alt-i goes back the way Alt-o came, handed to every key, forgotten at a jump (in `draw`, `in.jumped`) and started again by a reload (the tree a reload leaves is leaked: marked ponytail). `code` colours each run of bytes by `Syntax.colors` in pardes's own theme (orchard, the first): a defined name as a keyword, a called builtin as a string, numbers and comments as theirs, the rest in the theme's ink. indents.scm gives a new line no indent outside an open call or parenthesis: pardes's plain indent is a level more after `)`, so every line of this language that ends in a call pushed the next one in. The library now reads a handed-in language's indent query (pardes, its own change). Test: test/grammar.zig parses with the grammar and compiles with Lang.zig 22 texts: the survey's program and two others both take; eleven broken lines both refuse (an unclosed or extra parenthesis, a missing operand or `=`, two numbers, `1.2.3`, a stray `$`); eight both parse but only Lang.zig refuses, as only it knows names, builtins, arities and where a colour may stand (a name used before it is defined or defined twice, a builtin redefined or called with too few arguments, an unknown call, a colour outside the color line, a name called). Lang.zig's survey is public for it. zig build test: 85 of 85 steps. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'examples/live/src')
-rw-r--r--examples/live/src/Lang.zig5
-rw-r--r--examples/live/src/main.zig37
2 files changed, 37 insertions, 5 deletions
diff --git a/examples/live/src/Lang.zig b/examples/live/src/Lang.zig
index 484de54..cc6280f 100644
--- a/examples/live/src/Lang.zig
+++ b/examples/live/src/Lang.zig
@@ -519,8 +519,9 @@ fn expectOps(tape: Tape, want: []const Op) !void {
try testing.expectEqualSlices(Op, want, ops(tape)[0..tape.len]);
}
-/// The survey's program (reports/demo1.md, §1 A).
-const survey =
+/// The survey's program (reports/demo1.md, §1 A); main.zig's test of the
+/// grammar parses it too.
+pub const survey =
\\# the beat, shared by sound and picture
\\bpm = 120
\\beat = t * bpm / 60
diff --git a/examples/live/src/main.zig b/examples/live/src/main.zig
index ca4d0be..d6bc8d6 100644
--- a/examples/live/src/main.zig
+++ b/examples/live/src/main.zig
@@ -82,8 +82,24 @@ pub fn command(ss: *Session, c: *sk.Console, line: []const u8) !bool {
/// call deterministic and bounded. A key at the most text took 29 KiB of it.
var scratch: [128 * 1024]u8 = undefined;
+/// The language's grammar (grammar/, its parser.c compiled in by build.zig),
+/// handed to the editor at run time.
+pub extern fn tree_sitter_live() *const pe.syntax.ts.Language;
+pub fn language() pe.Language {
+ return .{ .name = "live", .ext = ".live", .ts = tree_sitter_live(), .highlights = @embedFile("highlights.scm"), .indents = @embedFile("indents.scm") };
+}
+
+/// The text's parse, for the editor's node walks (Alt-o, Alt-i, ...):
+/// derived, so not in State, and kept between calls, as Alt-i goes back the
+/// way Alt-o came only through its history; forgotten at a jump (`draw`).
+/// A reload starts it again, with the code its grammar is in.
+// ponytail: the tree a reload leaves behind is leaked (tree-sitter's
+// malloc, a few KiB a reload).
+var syn: pe.Syntax = .{};
+
pub fn update(s: *State, in: *const sk.Input) void {
const revision = s.ed.revision;
+ syn.lang = language();
// a chord's key_down may come with its letter as text too: not typed
var chord = false;
for (in.events) |e| {
@@ -111,7 +127,7 @@ pub fn update(s: *State, in: *const sk.Input) void {
var fba: std.heap.FixedBufferAllocator = .init(&scratch);
// ponytail: a key the text has no room for is dropped, as part 1's
// buffer dropped it; the editor leaves the value as it was.
- _ = s.ed.key(fba.allocator(), null, k);
+ _ = s.ed.key(fba.allocator(), &syn, k);
}
if (s.ed.revision == revision) return;
var text: [Editor.capacity.text]u8 = undefined;
@@ -180,6 +196,7 @@ const picture_pipeline: gpu.Pipeline = .{
};
pub fn draw(s: *const State, ss: *Session, f: *sk.Frame) void {
+ if (f.in.jumped) syn.forget();
f.clear(0x000000);
if (!f.host.gpu()) {
f.text(16, 16, 2, "live needs a GPU: the host has none.", 0xff8080);
@@ -276,8 +293,11 @@ const ink = 0xe8e8e8;
const red = 0xff5a50;
const selected: sk.Color = .{ .r = 0x3c, .g = 0x6e, .b = 0xc8, .a = 170 };
const also_selected: sk.Color = .{ .r = 0x3c, .g = 0x6e, .b = 0xc8, .a = 90 };
+/// pardes's own theme: its text's ink and a class's.
+const theme = &pe.syntax.themes[0];
-/// The editor's text over the picture on a dark band: its lines, every
+/// The editor's text over the picture on a dark band: its lines coloured
+/// by the grammar in pardes's theme, every
/// range (the primary stronger), a caret at each range's head that says
/// the mode (a block in normal and select, a bar in insert), the mode's
/// name in the corner; the problem as a red mark under its byte and its
@@ -289,6 +309,8 @@ fn code(s: *const State, f: *sk.Frame) void {
const text = s.ed.bytes(f.scratch) catch return;
const ranges = f.scratch.create([pe.max_selections]pe.Selection) catch return;
const got = s.ed.ranges(f.scratch, ranges) catch return;
+ // a class a byte, from the grammar's highlights (none if it fails)
+ const colors = pe.Syntax.colors(f.scratch, &s.ed, language(), 0, text.len) catch &.{};
const cell = f.host.textSizeIn(.code, scale, "M");
const problem = s.problem.len > 0;
var lines: usize = 1;
@@ -309,7 +331,16 @@ fn code(s: *const State, f: *sk.Frame) void {
const to = @min(@max(r.anchor, r.head), end);
if (from < to) f.fill(.{ .x = margin + @as(f32, @floatFromInt(from - start)) * cell[0], .y = y, .w = @as(f32, @floatFromInt(to - from)) * cell[0], .h = cell[1] }, if (i == got.primary) selected else also_selected);
}
- f.textIn(.code, margin, y, scale, line, ink);
+ // a run of bytes of one class at a time, in the theme's inks
+ var a: usize = 0;
+ while (a < line.len) {
+ const class = if (start + a < colors.len) colors[start + a] else .none;
+ var b = a + 1;
+ while (b < line.len and (if (start + b < colors.len) colors[start + b] else .none) == class) b += 1;
+ const c = pe.syntax.synInk(theme, class) orelse pe.syntax.ink(theme);
+ f.textIn(.code, margin + @as(f32, @floatFromInt(a)) * cell[0], y, scale, line[a..b], sk.Color{ .r = c[0], .g = c[1], .b = c[2] });
+ a = b;
+ }
for (ranges[0..got.n], 0..) |r, i| {
// the head's character: the one before it in a range that
// grows forward