### Parse Zig Code and Query AST with Tree-sitter Source: https://github.com/tree-sitter/zig-tree-sitter/blob/master/README.md This `src/main.zig` example illustrates how to use the `tree-sitter` Zig bindings to parse a Zig source code string, obtain the abstract syntax tree (AST), and then perform a query on the AST to find specific nodes, such as identifiers. It demonstrates parser creation, language setting, buffer parsing, node assertion, query creation, and cursor execution. ```Zig const std = @import("std"); const ts = @import("tree-sitter"); extern fn tree_sitter_zig() callconv(.C) *ts.Language; pub fn main() !void { // Create a parser for the zig language const language = tree_sitter_zig(); defer language.destroy(); const parser = ts.Parser.create(); defer parser.destroy(); try parser.setLanguage(language); // Parse some source code and get the root node const tree = try parser.parseBuffer("pub fn main() !void {}", null, null); defer tree.destroy(); const node = tree.rootNode(); std.debug.assert(std.mem.eql(u8, node.type(), "source_file")); std.debug.assert(node.endPoint().cmp(.{ .row = 0, .column = 22 }) == 0); // Create a query and execute it var error_offset: u32 = 0; const query = try ts.Query.create(language, "name: (identifier) @name", &error_offset); defer query.destroy(); const cursor = ts.QueryCursor.create(); defer cursor.destroy(); cursor.exec(query, node); // Get the captured node of the first match const match = cursor.nextMatch().?; const capture = match.captures[0].node; std.debug.assert(std.mem.eql(u8, capture.type(), "identifier")); } ``` -------------------------------- ### Configure Zig Project with Tree-sitter Dependency Source: https://github.com/tree-sitter/zig-tree-sitter/blob/master/README.md This `build.zig` snippet demonstrates how to configure a Zig project to include the `tree-sitter` and `tree-sitter-zig` dependencies. It sets up an executable, defines standard build options, and links the necessary modules and artifacts for parsing Zig code. ```Zig const std = @import("std"); pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); const exe = b.addExecutable(.{ .name = "zig-tree-sitter-usage", .root_source_file = b.path("src/main.zig"), .target = target, .optimize = optimize, }); b.installArtifact(exe); const tree_sitter = b.dependency("tree_sitter", .{ .target = target, .optimize = optimize, }); exe.root_module.addImport("tree-sitter", tree_sitter.module("tree-sitter")); const tree_sitter_zig = b.dependency("tree_sitter_zig", .{ .target = target, .optimize = optimize, }); exe.linkLibrary(tree_sitter_zig.artifact("tree-sitter-zig")); const run_cmd = b.addRunArtifact(exe); run_cmd.step.dependOn(b.getInstallStep()); const run_step = b.step("run", "Run the example"); run_step.dependOn(&run_cmd.step); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.