summaryrefslogtreecommitdiff
path: root/src/lib_syntax.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib_syntax.zig')
-rw-r--r--src/lib_syntax.zig90
1 files changed, 90 insertions, 0 deletions
diff --git a/src/lib_syntax.zig b/src/lib_syntax.zig
new file mode 100644
index 00000000..395a02f0
--- /dev/null
+++ b/src/lib_syntax.zig
@@ -0,0 +1,90 @@
+//! 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;
+
+/// 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;