summaryrefslogtreecommitdiff
path: root/core/lib_syntax.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-18 22:03:14 -0300
committerGabriel Schneider <[email protected]>2026-11-18 22:23:51 -0300
commit99f5b10eccd95aa0066ea6fe6bb75f367ab56a47 (patch)
tree813596ea84318a7bae4fd7416ba463de8e3b7107 /core/lib_syntax.zig
parent92d553123673376adf6126ab90d49a22c5b833d2 (diff)
downloadpardes-99f5b10eccd95aa0066ea6fe6bb75f367ab56a47.tar.gz
pardes-99f5b10eccd95aa0066ea6fe6bb75f367ab56a47.zip
core: src/ becomes core/, pardes.zig its root.zig (the reorganisation's tree, step 2d), and every mention of a src/ path follows: the build's paths, the core's imports of its root, the tests that read the repository's files by path (lspbench's, look.zig's spans over core/look.zig), the comments that cite files, the documents and the manual (the typst corpus reads core/tutor.txt and core/plumbing); left as they were: the /src tree's served names (src/pardes.zig and the rest are the 9P tree's, not the repository's), the CHANGELOG's history, fixtures that are only strings; fs-unit-test runs from the core's root (a module of the core rooted at fs.zig had the runtime's and the devices' `core` be fs.zig); nothing else changes: the ledger (pardes.test.* now root.test.*), frames byte-identical, replicas and process-test the base's
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'core/lib_syntax.zig')
-rw-r--r--core/lib_syntax.zig109
1 files changed, 109 insertions, 0 deletions
diff --git a/core/lib_syntax.zig b/core/lib_syntax.zig
new file mode 100644
index 00000000..a25af884
--- /dev/null
+++ b/core/lib_syntax.zig
@@ -0,0 +1,109 @@
+//! pardes's syntax as a library: the `pardes-syntax` module a package that
+//! depends on pardes imports (build.zig's `library`, docs/typ/building.typ),
+//! for a program that draws source text itself and wants it parsed and
+//! coloured as pardes has it. Nothing is copied here: these are the editor's
+//! own grammars and highlighting (syntax.zig), themes (colors.zig) and line
+//! arithmetic (modal.zig), under the names they have there. Whatever is
+//! named here compiles with tree-sitter and the grammars alone; test/lib
+//! imports it as such a package and holds it to that.
+//!
+//! A language is told by a path's extension, as in the editor; `pathFor`
+//! gives a path for a language's name. One thread: each language's parser
+//! is kept and shared between calls.
+const syntax = @import("syntax.zig");
+const colors = @import("colors.zig");
+const modal = @import("modal.zig");
+
+/// Every grammar pardes knows (`all`), by name or alias (`byName`) and by
+/// path (`byPath`). The build's `tree-sitter` option says which of them are
+/// compiled in, and `supportsPath` answers for this build.
+pub const grammars = @import("grammar_manifest.zig");
+pub const supportsPath = syntax.supportsPath;
+
+/// A path that selects the language `name` (a grammar's name or an alias,
+/// in any case: "c", "py", "Zig"); null when no grammar goes by it.
+pub fn pathFor(name: []const u8) ?[]const u8 {
+ const i = grammars.byName(name) orelse return null;
+ inline for (grammars.all, 0..) |g, at| {
+ if (g.exts.len > 0 and at == i) return "a" ++ g.exts[0];
+ }
+ return null;
+}
+
+/// tree-sitter and the grammars' scanners allocate from `start`'s allocator
+/// from then on, and from libc's malloc without it; before any other call
+/// here, and once. `stop` frees the parsers and queries kept per language,
+/// after a `start` or without one.
+pub const start = syntax.start;
+pub const stop = syntax.stop;
+
+/// What a byte is drawn as. pardes has one colour each for keywords,
+/// strings, numbers and comments (`synInk`); none is the text's own.
+pub const Syn = syntax.Syn;
+/// One `Syn` for each byte of `content[start..end]`, that range parsed as a
+/// text of its own, the caller's to free; empty where the path has no
+/// grammar.
+pub const highlightFileRange = syntax.highlightFileRange;
+/// A whole text in one parse: its `colors`, a `Syn` a byte, and its
+/// `declarations`, the functions, types and tests of more than a line (the
+/// editor's sticky headers), in source order, an outer one before its inner.
+pub const SourceAnalysis = syntax.SourceAnalysis;
+pub const ContextDeclaration = syntax.ContextDeclaration;
+pub const analyzeSource = syntax.analyzeSource;
+
+/// A unified diff as a language: `diff` walks one a line at a time
+/// (`Walk`: a line's kind, the file its section names, the hunk's line
+/// numbers) and says what a row of it points at (`targetAt`);
+/// `highlightDiff` is a style byte for each byte of `content[start..end]`,
+/// the caller's to free: headers as comments, `@@` lines as keywords, each
+/// hunk's code in the language of the file its section names (old and new
+/// side each parsed whole), and over that an added or removed line's
+/// grounds (`ground_add`, `ground_del`; its first byte `ground_emph`, the
+/// strong row). A byte's low bits (`syn_bits`) are a `Syn`; `Grounds` are
+/// a theme's colours for the rest.
+pub const diff = @import("diff.zig");
+pub const highlightDiff = syntax.highlightDiff;
+pub const syn_bits = syntax.syn_bits;
+pub const ground_add = syntax.ground_add;
+pub const ground_del = syntax.ground_del;
+pub const ground_emph = syntax.ground_emph;
+pub const ground_line = syntax.ground_line;
+pub const Grounds = colors.Grounds;
+
+/// The tree-sitter bindings a tree is read through: `tree.rootNode()`, and
+/// a node's `kind()`, `startByte()`, `endByte()`, `child(i)`, `parent()`.
+pub const ts = syntax.ts;
+/// `content` parsed by its path's grammar, the caller's to `destroy()`; null
+/// without a grammar.
+pub fn parse(path: []const u8, content: []const u8) ?*ts.Tree {
+ return @ptrCast(@alignCast(syntax.parseTree(path, content, null)));
+}
+/// helix's walks from a byte range to the nodes about it: the node that
+/// holds it (`expand_node`), a sibling, its children.
+pub const NodeWalk = syntax.NodeWalk;
+pub const Span = syntax.Span;
+pub const walkNodes = syntax.walkNodes;
+
+pub const Theme = colors.Theme;
+/// Every theme: pardes's own, then those imported from helix and zed. The
+/// first is the default.
+pub const themes: []const Theme = &colors.themes;
+/// The theme named `name`, whatever its case.
+pub fn theme(name: []const u8) ?*const Theme {
+ return &colors.themes[colors.themeIndex(name) orelse return null];
+}
+/// A theme's page and its text's ink as a file pane has them: its own, or
+/// its tag's for the one theme with none (`dark`, a terminal's own).
+pub fn page(th: *const Theme) [3]u8 {
+ return th.bg orelse th.tag_bg;
+}
+pub fn ink(th: *const Theme) [3]u8 {
+ return th.fg orelse th.tag_fg;
+}
+pub const synInk = colors.synInk;
+
+/// A text's lines by its newlines: how many, the byte line `row` (from 0)
+/// starts at, and its bytes.
+pub const lineCount = modal.lineCount;
+pub const lineStartOffset = modal.lineStartOffset;
+pub const lineSlice = modal.lineSlice;