diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-17 06:00:14 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-17 12:15:01 -0300 |
| commit | 82d998af568caebd7fe28c4f6fb7feb1bfb4572f (patch) | |
| tree | 8cf486f1590f65322fe18c2d91f0c8573bb5ae85 /src/syntax.zig | |
| parent | b06543725a84ac5ce8cc48810b526c00032247cf (diff) | |
| download | pardes-82d998af568caebd7fe28c4f6fb7feb1bfb4572f.tar.gz pardes-82d998af568caebd7fe28c4f6fb7feb1bfb4572f.zip | |
syntax: a grammar handed in at run time, beside the compiled-in table (docs/next-editor.md, R4): `Language` is a program's own (a sketch's DSL: its tree-sitter language, its highlights, textobjects and indents sources, told by an extension), `addLanguage` puts it among the grammars a path's extension finds and `forgetLanguages` drops it with its parser and queries (before the code it points into is unloaded); one `byExt` finds a grammar for forExt, supportsPath and specFor where each looped the table itself, a Spec's language is its function or the pointer handed in (`langOf`), and stop drops each spec through the same dropSpec; test: a grammar handed in colours and walks as the compiled-in one; core-test 811 of 950, the syntax snapshots byte-identical
Diffstat (limited to 'src/syntax.zig')
| -rw-r--r-- | src/syntax.zig | 149 |
1 files changed, 116 insertions, 33 deletions
diff --git a/src/syntax.zig b/src/syntax.zig index 70399bf7..3f75c4be 100644 --- a/src/syntax.zig +++ b/src/syntax.zig @@ -30,7 +30,9 @@ const Lazy = struct { src: []const u8 = "", query: ?*ts.Query = null, failed: bo const Spec = struct { name: []const u8, exts: []const []const u8, - language: *const fn () callconv(.c) *const ts.Language, + /// A compiled-in grammar's, else `lang`, one handed in at run time. + language: ?LanguageFn = null, + lang: ?*const ts.Language = null, query_src: []const u8, /// helix's textobjects.scm and indents.scm for the grammar /// (vendor/queries), compiled the first time they are asked for @@ -102,18 +104,8 @@ pub fn start(gpa: std.mem.Allocator) void { pub fn stop() void { if (comptime enabled) { - for (&specs) |*spec| { - if (spec.selected) |selected| { - selected.cursor.destroy(); - selected.parser.destroy(); - selected.query.destroy(); - } - spec.selected = null; - for ([_]*Lazy{ &spec.objects, &spec.indents }) |lazy| { - if (lazy.query) |q| q.destroy(); - lazy.* = .{ .src = lazy.src }; - } - } + for (&specs) |*spec| dropSpec(spec); + forgetLanguages(); for (&diff_pieces) |*slot| { if (slot.*) |piece| syntax_allocator.free(piece.styles); slot.* = null; @@ -134,9 +126,89 @@ const Selected = struct { capture_styles: []u8, }; +fn langOf(spec: *const Spec) *const ts.Language { + return spec.lang orelse spec.language.?(); +} + +/// A grammar handed in at run time, beside the compiled-in ones: a +/// program's own (a sketch's language), its tree-sitter language and its +/// queries' sources, told by its extension (`.dsl`) as theirs are by +/// theirs. What it points at is the caller's: `forgetLanguages` before +/// the code it is in goes (a reload). +pub const Language = struct { + name: []const u8, + ext: []const u8, + ts: *const ts.Language, + highlights: []const u8 = "", + textobjects: []const u8 = "", + indents: []const u8 = "", + + /// A path its extension tells, in `buf`. + pub fn path(l: Language, buf: []u8) []const u8 { + return std.fmt.bufPrint(buf, "a{s}", .{l.ext}) catch ""; + } +}; + +var handed: [8]Spec = undefined; +var handed_exts: [8][1][]const u8 = undefined; +var handed_n: usize = 0; + +/// `l` told by its extension from now on; one already told by it is +/// replaced (its parser and queries made again) unless it is the same. +/// False when eight are told already. +pub fn addLanguage(l: Language) bool { + const at = for (handed[0..handed_n], 0..) |*spec, i| { + if (std.mem.eql(u8, spec.exts[0], l.ext)) { + if (spec.lang == l.ts and spec.query_src.ptr == l.highlights.ptr and spec.objects.src.ptr == l.textobjects.ptr) return true; + dropSpec(spec); + break i; + } + } else if (handed_n == handed.len) return false else handed_n; + handed_exts[at] = .{l.ext}; + handed[at] = .{ + .name = l.name, + .exts = &handed_exts[at], + .lang = l.ts, + .query_src = l.highlights, + .objects = .{ .src = l.textobjects }, + .indents = .{ .src = l.indents }, + }; + handed_n = @max(handed_n, at + 1); + return true; +} + +/// Every grammar handed in at run time gone, with the parsers and queries +/// made for it. +pub fn forgetLanguages() void { + for (handed[0..handed_n]) |*spec| dropSpec(spec); + handed_n = 0; +} + +fn dropSpec(spec: *Spec) void { + if (spec.selected) |selected| { + selected.cursor.destroy(); + selected.parser.destroy(); + selected.query.destroy(); + } + spec.selected = null; + for ([_]*Lazy{ &spec.objects, &spec.indents }) |lazy| { + if (lazy.query) |q| q.destroy(); + lazy.* = .{ .src = lazy.src }; + } +} + +/// The grammar a file's extension names: compiled in, else handed in. +fn byExt(ext: []const u8) ?*Spec { + for (&specs) |*spec| for (spec.exts) |choice| { + if (std.ascii.eqlIgnoreCase(ext, choice)) return spec; + }; + for (handed[0..handed_n]) |*spec| if (std.ascii.eqlIgnoreCase(ext, spec.exts[0])) return spec; + return null; +} + fn ensure(spec: *Spec) !Selected { if (spec.selected) |selected| return selected; - const lang = spec.language(); + const lang = langOf(spec); var error_offset: u32 = 0; const query = try ts.Query.create(lang, spec.query_src, &error_offset); errdefer query.destroy(); @@ -163,12 +235,7 @@ fn ensure(spec: *Spec) !Selected { } fn forExt(ext: []const u8) !?Selected { - for (&specs) |*spec| { - for (spec.exts) |choice| { - if (std.ascii.eqlIgnoreCase(ext, choice)) return try ensure(spec); - } - } - return null; + return try ensure(byExt(ext) orelse return null); } fn runQuery(styles: []u8, sel: Selected, tree: *ts.Tree, base: usize) void { @@ -270,11 +337,7 @@ pub const ContextDeclaration = struct { pub fn supportsPath(path: []const u8) bool { if (!enabled) return false; - const ext = std.Io.Dir.path.extension(path); - for (specs) |spec| for (spec.exts) |choice| { - if (std.ascii.eqlIgnoreCase(ext, choice)) return true; - }; - return false; + return byExt(std.Io.Dir.path.extension(path)) != null; } /// A whole file's parse, kept by the file for the keys that walk its nodes @@ -441,18 +504,14 @@ pub fn indentQuery(path: []const u8) ?struct { *ts.Query, []const u8 } { } fn specFor(path: []const u8) ?*Spec { - const ext = std.Io.Dir.path.extension(path); - for (&specs) |*spec| for (spec.exts) |choice| { - if (std.ascii.eqlIgnoreCase(ext, choice)) return spec; - }; - return null; + return byExt(std.Io.Dir.path.extension(path)); } fn lazyQuery(spec: *Spec, lazy: *Lazy) ?*ts.Query { if (lazy.query) |q| return q; if (lazy.src.len == 0 or lazy.failed) return null; var error_offset: u32 = 0; - lazy.query = ts.Query.create(spec.language(), lazy.src, &error_offset) catch { + lazy.query = ts.Query.create(langOf(spec), lazy.src, &error_offset) catch { lazy.failed = true; return null; }; @@ -600,7 +659,7 @@ pub fn declarationsApart(gpa: std.mem.Allocator, path: []const u8, content: []co const spec = specs: for (&specs) |*spec| { for (spec.exts) |choice| if (std.ascii.eqlIgnoreCase(ext, choice)) break :specs spec; } else return &.{}; - const tree = try parseApart(spec.language(), content, halt); + const tree = try parseApart(langOf(spec), content, halt); defer tree.destroy(); return declarationsFromTree(gpa, content, tree, halt); } @@ -1216,7 +1275,7 @@ test "every vendored textobject and indent query compiles against its grammar" { for (&specs) |*spec| for ([_]Lazy{ spec.objects, spec.indents }, [_][]const u8{ "textobjects", "indents" }) |lazy, what| { if (lazy.src.len == 0) continue; var error_offset: u32 = 0; - const q = ts.Query.create(spec.language(), lazy.src, &error_offset) catch { + const q = ts.Query.create(langOf(spec), lazy.src, &error_offset) catch { std.debug.print("{s} for {s} do not compile (offset {d})\n", .{ what, spec.name, error_offset }); failed += 1; continue; @@ -2108,3 +2167,27 @@ test "a kept tree told its edits parses to the fresh parse's tree, places and al try std.testing.expectEqual(fn_.endPoint(), rn.endPoint()); try std.testing.expectEqual(fn_.startByte(), rn.startByte()); } + +test "syntax: a grammar handed in at run time colours and walks as the one compiled in" { + if (!zig_grammar) return error.SkipZigTest; + const gpa = std.testing.allocator; + start(gpa); + defer stop(); + const zig_lang = @extern(LanguageFn, .{ .name = "tree_sitter_zig" })(); + try std.testing.expect(addLanguage(.{ .name = "mine", .ext = ".mine", .ts = zig_lang, .highlights = ts_queries.zig_highlights })); + // the same again is kept; told again with other queries, replaced + try std.testing.expect(addLanguage(.{ .name = "mine", .ext = ".mine", .ts = zig_lang, .highlights = ts_queries.zig_highlights })); + try std.testing.expect(supportsPath("x.mine")); + const src = "const x = 1; // one\nfn f() void {}\n"; + const want = try highlightFileRange(gpa, "a.zig", src, 0, src.len); + defer gpa.free(want); + const got = try highlightFileRange(gpa, "a.mine", src, 0, src.len); + defer gpa.free(got); + try std.testing.expectEqualSlices(u8, want, got); + const tree = parseTree("a.mine", src, null) orelse return error.NoTree; + defer freeTree(tree); + var spans: [4]Span = undefined; + try std.testing.expect(walkNodes(tree, .expand_node, 6, 7, 6, &spans) > 0); + forgetLanguages(); + try std.testing.expect(!supportsPath("x.mine")); +} |
