build: add the options to build entirely without tree-sitter

This commit is contained in:
CJ van den Berg 2024-08-16 21:59:22 +02:00
parent 78489e31f6
commit 3de13b1354
5 changed files with 231 additions and 91 deletions

View file

@ -2,19 +2,18 @@ const std = @import("std");
const builtin = @import("builtin"); const builtin = @import("builtin");
pub fn build(b: *std.Build) void { pub fn build(b: *std.Build) void {
const enable_tracy_option = b.option(bool, "enable_tracy", "Enable tracy client library (default: no)"); const tracy_enabled = b.option(bool, "enable_tracy", "Enable tracy client library (default: no)") orelse false;
const optimize_deps_option = b.option(bool, "optimize_deps", "Enable optimization for dependecies (default: yes)"); const optimize_deps = b.option(bool, "optimize_deps", "Enable optimization for dependecies (default: yes)") orelse true;
const use_llvm_option = b.option(bool, "use_llvm", "Enable llvm backend (default: yes)"); const use_llvm_option = b.option(bool, "use_llvm", "Enable llvm backend (default: yes)");
const use_lld_option = b.option(bool, "use_lld", "Enable lld backend (default: yes)"); const use_lld_option = b.option(bool, "use_lld", "Enable lld backend (default: yes)");
const use_tree_sitter = b.option(bool, "use_tree_sitter", "Enable tree-sitter (default: yes)") orelse true;
const tracy_enabled = if (enable_tracy_option) |enabled| enabled else false;
const optimize_deps_enabled = if (optimize_deps_option) |enabled| enabled else true;
const options = b.addOptions(); const options = b.addOptions();
options.addOption(bool, "enable_tracy", tracy_enabled); options.addOption(bool, "enable_tracy", tracy_enabled);
options.addOption(bool, "optimize_deps", optimize_deps_enabled); options.addOption(bool, "optimize_deps", optimize_deps);
options.addOption(bool, "use_llvm", use_llvm_option orelse false); options.addOption(bool, "use_tree_sitter", use_tree_sitter);
options.addOption(bool, "use_lld", use_lld_option orelse false); options.addOption(bool, "use_llvm", use_llvm_option orelse true);
options.addOption(bool, "use_lld", use_lld_option orelse true);
const options_mod = options.createModule(); const options_mod = options.createModule();
@ -22,7 +21,7 @@ pub fn build(b: *std.Build) void {
// std.debug.print("target abi: {s}\n", .{@tagName(target.result.abi)}); // std.debug.print("target abi: {s}\n", .{@tagName(target.result.abi)});
const optimize = b.standardOptimizeOption(.{}); const optimize = b.standardOptimizeOption(.{});
const dependency_optimize = if (optimize_deps_enabled) .ReleaseFast else optimize; const dependency_optimize = if (optimize_deps) .ReleaseFast else optimize;
std.fs.cwd().makeDir(".cache") catch |e| switch (e) { std.fs.cwd().makeDir(".cache") catch |e| switch (e) {
error.PathAlreadyExists => {}, error.PathAlreadyExists => {},
@ -82,6 +81,7 @@ pub fn build(b: *std.Build) void {
const syntax_dep = b.dependency("syntax", .{ const syntax_dep = b.dependency("syntax", .{
.target = target, .target = target,
.optimize = dependency_optimize, .optimize = dependency_optimize,
.use_tree_sitter = use_tree_sitter,
}); });
const syntax_mod = syntax_dep.module("syntax"); const syntax_mod = syntax_dep.module("syntax");

View file

@ -1,6 +1,11 @@
const std = @import("std"); const std = @import("std");
pub fn build(b: *std.Build) void { pub fn build(b: *std.Build) void {
const use_tree_sitter = b.option(bool, "use_tree_sitter", "Enable tree-sitter (default: yes)") orelse true;
const options = b.addOptions();
options.addOption(bool, "use_tree_sitter", use_tree_sitter);
const options_mod = options.createModule();
const target = b.standardTargetOptions(.{}); const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{}); const optimize = b.standardOptimizeOption(.{});
@ -9,9 +14,8 @@ pub fn build(b: *std.Build) void {
.optimize = optimize, .optimize = optimize,
}); });
_ = b.addModule("syntax", .{ const imports: []const std.Build.Module.Import = if (use_tree_sitter) &.{
.root_source_file = b.path("src/syntax.zig"), .{ .name = "build_options", .module = options_mod },
.imports = &.{
.{ .name = "treez", .module = tree_sitter_dep.module("treez") }, .{ .name = "treez", .module = tree_sitter_dep.module("treez") },
ts_queryfile(b, tree_sitter_dep, "tree-sitter-agda/queries/highlights.scm"), ts_queryfile(b, tree_sitter_dep, "tree-sitter-agda/queries/highlights.scm"),
ts_queryfile(b, tree_sitter_dep, "tree-sitter-bash/queries/highlights.scm"), ts_queryfile(b, tree_sitter_dep, "tree-sitter-bash/queries/highlights.scm"),
@ -81,9 +85,14 @@ pub fn build(b: *std.Build) void {
ts_queryfile(b, tree_sitter_dep, "tree-sitter-rust/queries/injections.scm"), ts_queryfile(b, tree_sitter_dep, "tree-sitter-rust/queries/injections.scm"),
ts_queryfile(b, tree_sitter_dep, "tree-sitter-superhtml/tree-sitter-superhtml/queries/injections.scm"), ts_queryfile(b, tree_sitter_dep, "tree-sitter-superhtml/tree-sitter-superhtml/queries/injections.scm"),
ts_queryfile(b, tree_sitter_dep, "tree-sitter-zig/queries/injections.scm"), ts_queryfile(b, tree_sitter_dep, "tree-sitter-zig/queries/injections.scm"),
}, } else &.{
}); .{ .name = "build_options", .module = options_mod },
};
_ = b.addModule("syntax", .{
.root_source_file = b.path("src/syntax.zig"),
.imports = imports,
});
} }
fn ts_queryfile(b: *std.Build, dep: *std.Build.Dependency, comptime sub_path: []const u8) std.Build.Module.Import { fn ts_queryfile(b: *std.Build, dep: *std.Build.Dependency, comptime sub_path: []const u8) std.Build.Module.Import {

View file

@ -1,5 +1,11 @@
const std = @import("std"); const std = @import("std");
const treez = @import("treez"); const build_options = @import("build_options");
const treez = if (build_options.use_tree_sitter)
@import("treez")
else
@import("treez_dummy.zig");
pub const FileType = @This(); pub const FileType = @This();
color: u24, color: u24,
@ -63,8 +69,12 @@ pub fn Parser(comptime lang: []const u8) LangFn {
} }
fn get_parser(comptime lang: []const u8) LangFn { fn get_parser(comptime lang: []const u8) LangFn {
if (build_options.use_tree_sitter) {
const language_name = ft_func_name(lang); const language_name = ft_func_name(lang);
return @extern(?LangFn, .{ .name = "tree_sitter_" ++ language_name }) orelse @compileError(std.fmt.comptimePrint("Cannot find extern tree_sitter_{s}", .{language_name})); return @extern(?LangFn, .{ .name = "tree_sitter_" ++ language_name }) orelse @compileError(std.fmt.comptimePrint("Cannot find extern tree_sitter_{s}", .{language_name}));
} else {
return treez.Language.LangFn;
}
} }
fn ft_func_name(comptime lang: []const u8) []const u8 { fn ft_func_name(comptime lang: []const u8) []const u8 {
@ -112,8 +122,20 @@ fn load_file_types(comptime Namespace: type) []const FileType {
.lang_fn = if (@hasField(@TypeOf(args), "parser")) args.parser else get_parser(lang), .lang_fn = if (@hasField(@TypeOf(args), "parser")) args.parser else get_parser(lang),
.extensions = vec(args.extensions), .extensions = vec(args.extensions),
.comment = args.comment, .comment = args.comment,
.highlights = if (@hasField(@TypeOf(args), "highlights")) @embedFile(args.highlights) else @embedFile("tree-sitter-" ++ lang ++ "/queries/highlights.scm"), .highlights = if (build_options.use_tree_sitter)
.injections = if (@hasField(@TypeOf(args), "injections")) @embedFile(args.injections) else null, if (@hasField(@TypeOf(args), "highlights"))
@embedFile(args.highlights)
else
@embedFile("tree-sitter-" ++ lang ++ "/queries/highlights.scm")
else
"",
.injections = if (build_options.use_tree_sitter)
if (@hasField(@TypeOf(args), "injections"))
@embedFile(args.injections)
else
null
else
null,
.first_line_matches = if (@hasField(@TypeOf(args), "first_line_matches")) args.first_line_matches else null, .first_line_matches = if (@hasField(@TypeOf(args), "first_line_matches")) args.first_line_matches else null,
.formatter = if (@hasField(@TypeOf(args), "formatter")) vec(args.formatter) else null, .formatter = if (@hasField(@TypeOf(args), "formatter")) vec(args.formatter) else null,
.language_server = if (@hasField(@TypeOf(args), "language_server")) vec(args.language_server) else null, .language_server = if (@hasField(@TypeOf(args), "language_server")) vec(args.language_server) else null,

View file

@ -1,5 +1,10 @@
const std = @import("std"); const std = @import("std");
const treez = @import("treez"); const build_options = @import("build_options");
const treez = if (build_options.use_tree_sitter)
@import("treez")
else
@import("treez_dummy.zig");
const Self = @This(); const Self = @This();
@ -7,10 +12,10 @@ pub const Edit = treez.InputEdit;
pub const FileType = @import("file_type.zig"); pub const FileType = @import("file_type.zig");
pub const Range = treez.Range; pub const Range = treez.Range;
pub const Point = treez.Point; pub const Point = treez.Point;
const Input = treez.Input;
const Language = treez.Language; const Language = treez.Language;
const Parser = treez.Parser; const Parser = treez.Parser;
const Query = treez.Query; const Query = treez.Query;
const Tree = treez.Tree;
pub const Node = treez.Node; pub const Node = treez.Node;
a: std.mem.Allocator, a: std.mem.Allocator,
@ -19,13 +24,13 @@ file_type: *const FileType,
parser: *Parser, parser: *Parser,
query: *Query, query: *Query,
injections: *Query, injections: *Query,
tree: ?*Tree = null, tree: ?*treez.Tree = null,
pub fn create(file_type: *const FileType, a: std.mem.Allocator, content: []const u8) !*Self { pub fn create(file_type: *const FileType, a: std.mem.Allocator, content: []const u8) !*Self {
const self = try a.create(Self); const self = try a.create(Self);
self.* = .{ self.* = .{
.a = a, .a = a,
.lang = file_type.lang_fn() orelse std.debug.panic("tree-sitter parser function failed for language: {d}", .{file_type.name}), .lang = file_type.lang_fn() orelse std.debug.panic("tree-sitter parser function failed for language: {s}", .{file_type.name}),
.file_type = file_type, .file_type = file_type,
.parser = try Parser.create(), .parser = try Parser.create(),
.query = try Query.create(self.lang, file_type.highlights), .query = try Query.create(self.lang, file_type.highlights),
@ -85,7 +90,7 @@ pub fn refresh_from_buffer(self: *Self, buffer: anytype, metrics: anytype) !void
.syntax = self, .syntax = self,
}; };
const input: treez.Input = .{ const input: Input = .{
.payload = &state, .payload = &state,
.read = struct { .read = struct {
fn read(payload: ?*anyopaque, _: u32, position: treez.Point, bytes_read: *u32) callconv(.C) [*:0]const u8 { fn read(payload: ?*anyopaque, _: u32, position: treez.Point, bytes_read: *u32) callconv(.C) [*:0]const u8 {

View file

@ -0,0 +1,104 @@
pub const InputEdit = extern struct {
start_byte: u32,
old_end_byte: u32,
new_end_byte: u32,
start_point: Point,
old_end_point: Point,
new_end_point: Point,
};
pub const Range = extern struct {
start_point: Point = .{},
end_point: Point = .{},
start_byte: u32 = 0,
end_byte: u32 = 0,
};
pub const Point = extern struct {
row: u32 = 0,
column: u32 = 0,
};
pub const InputEncoding = enum(c_uint) {
utf_8,
utf_16,
};
pub const Input = extern struct {
payload: ?*anyopaque,
read: ?*const fn (payload: ?*anyopaque, byte_index: u32, position: Point, bytes_read: *u32) callconv(.C) [*:0]const u8,
encoding: InputEncoding,
};
pub const Language = struct {
var dummy: @This() = .{};
pub fn LangFn() callconv(.C) ?*const Language {
return &dummy;
}
};
pub const Parser = struct {
var dummy: @This() = .{};
pub fn create() !*@This() {
return &dummy;
}
pub fn parse(_: *Parser, _: ?*Tree, _: Input) !*Tree {
return &Tree.dummy;
}
pub fn parseString(_: *@This(), _: ?[]const u8, _: []const u8) !?*Tree {
return null;
}
pub fn destroy(_: *@This()) void {}
pub fn setLanguage(_: *Parser, _: *const Language) !void {}
};
pub const Query = struct {
var dummy: @This() = .{};
pub fn create(_: *const Language, _: []const u8) !*Query {
return &dummy;
}
pub const Cursor = struct {
var dummy_: @This() = .{};
pub fn create() !*@This() {
return &dummy_;
}
pub fn execute(_: *@This(), _: *Query, _: *Node) void {}
pub fn setPointRange(_: *@This(), _: Point, _: Point) void {}
pub fn nextMatch(_: *@This()) ?*Match {
return null;
}
pub fn destroy(_: *@This()) void {}
pub const Match = struct {
pub fn captures(_: *@This()) []Capture {
return &[_]Capture{};
}
};
pub const Capture = struct {
id: u32,
node: Node,
};
};
pub fn getCaptureNameForId(_: *@This(), _: u32) []const u8 {
return "";
}
pub fn destroy(_: *@This()) void {}
};
pub const Tree = struct {
var dummy: @This() = .{};
pub fn getRootNode(_: *@This()) *Node {
return &Node.dummy;
}
pub fn destroy(_: *@This()) void {}
pub fn edit(_: *Tree, _: *const InputEdit) void {}
};
pub const Node = struct {
var dummy: @This() = .{};
pub fn getRange(_: *const @This()) Range {
return .{};
}
pub fn asSExpressionString(_: *const @This()) []const u8 {
return "";
}
pub fn freeSExpressionString(_: []const u8) void {}
pub fn getParent(_: *const @This()) *Node {
return &dummy;
}
pub fn isNull(_: *const @This()) bool {
return true;
}
};