diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-17 07:51:24 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-17 12:15:01 -0300 |
| commit | f37732448a2a374281c9610d05a10315e97dcbbf (patch) | |
| tree | 8778dba261ce8dcddcfea894d838e0b51b0255cb /examples/live/test | |
| parent | fad609ddb13cbc0f33150fc3a9dfc25051641cd8 (diff) | |
| download | sketchbook-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/test')
| -rw-r--r-- | examples/live/test/grammar.zig | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/examples/live/test/grammar.zig b/examples/live/test/grammar.zig new file mode 100644 index 0000000..497c05d --- /dev/null +++ b/examples/live/test/grammar.zig @@ -0,0 +1,54 @@ +//! The language's grammar (grammar/grammar.js, its parser.c compiled into +//! the sketch) against src/Lang.zig, which compiles the text: they must +//! agree on what is well formed, or what is coloured and walked would not +//! be the program that plays. +const std = @import("std"); +const app = @import("app"); +const Lang = @import("Lang"); +const pe = @import("pardes-editor"); + +test "the grammar and Lang.zig agree: both take a program, both refuse a broken line; what only Lang.zig refuses is its meaning" { + const parser = pe.syntax.ts.Parser.create(); + defer parser.destroy(); + try parser.setLanguage(app.tree_sitter_live()); + const Case = struct { text: []const u8, grammar: bool, lang: bool }; + for ([_]Case{ + .{ .text = Lang.survey, .grammar = true, .lang = true }, + .{ .text = "out = sin(tau * 220 * t) * 0.3\ncolor = hsv(x, 0.5, y)", .grammar = true, .lang = true }, + .{ .text = "\n # nothing\n a = 1 + 2 * -t / 4 - (5 - -6) # trailing\n\nout = a + 1e-3\n", .grammar = true, .lang = true }, + // broken lines: both refuse + .{ .text = "out = sin(tau * 220 * t * 0.3", .grammar = false, .lang = false }, + .{ .text = "out = 1 +", .grammar = false, .lang = false }, + .{ .text = "= 1", .grammar = false, .lang = false }, + .{ .text = "out 1", .grammar = false, .lang = false }, + .{ .text = "out", .grammar = false, .lang = false }, + .{ .text = "out = * 2", .grammar = false, .lang = false }, + .{ .text = "out = 1 2", .grammar = false, .lang = false }, + .{ .text = "out = sin(1 2)", .grammar = false, .lang = false }, + .{ .text = "out = 1.2.3", .grammar = false, .lang = false }, + .{ .text = "out = 1 $ 2", .grammar = false, .lang = false }, + .{ .text = "out = (1 + 2))", .grammar = false, .lang = false }, + // well formed, but meaning nothing: only Lang.zig knows its names, + // builtins and arities, and the problem it shows is the one drawn + .{ .text = "out = kick\nkick = 1", .grammar = true, .lang = false }, + .{ .text = "a = 1\na = 2", .grammar = true, .lang = false }, + .{ .text = "sin = 1", .grammar = true, .lang = false }, + .{ .text = "out = sin", .grammar = true, .lang = false }, + .{ .text = "out = foo(2)", .grammar = true, .lang = false }, + .{ .text = "out = mix(1, 2)", .grammar = true, .lang = false }, + .{ .text = "out = rgb(1, 1, 1)", .grammar = true, .lang = false }, + .{ .text = "a = 1\nout = a(2)", .grammar = true, .lang = false }, + }) |case| { + const tree = parser.parseString(case.text, null) orelse return error.NoTree; + defer tree.destroy(); + const lang = Lang.compile(case.text) == .program; + std.testing.expectEqual(case.grammar, !tree.rootNode().hasError()) catch |e| { + std.debug.print("the grammar on {s}\n", .{case.text}); + return e; + }; + std.testing.expectEqual(case.lang, lang) catch |e| { + std.debug.print("Lang.zig on {s}\n", .{case.text}); + return e; + }; + } +} |
