treesitter-types-go 0.1.1

Pre-generated strongly-typed AST types for Go (tree-sitter-go)
Documentation

Strongly-typed AST types for Go, auto-generated from tree-sitter-go's node-types.json.

This crate is generated by treesitter-types and is automatically kept up to date when a new version of the grammar crate is released.

These types have been tested by parsing the Go compiler source code.

See the Tree-sitter project for more information about the underlying parser framework.

Example

use treesitter_types_go::*;

// A minimal Go hello-world program.
let src = b"\
package main

import \"fmt\"

func main() {
    fmt.Println(\"Hello, World!\")
}
";

// Parse the source with tree-sitter and convert into typed AST.
let mut parser = tree_sitter::Parser::new();
parser.set_language(&tree_sitter_go::LANGUAGE.into()).unwrap();
let tree = parser.parse(src, None).unwrap();
let source_file = SourceFile::from_node(tree.root_node(), src).unwrap();

// The source file has three top-level children.
assert_eq!(source_file.children.len(), 3);

// 1) The package clause — `package main`.
let SourceFileChildren::PackageClause(pkg) = &source_file.children[0] else {
    panic!("expected a package clause");
};
assert_eq!(pkg.children.text(), "main");

// 2) The import declaration — `import "fmt"`.
let SourceFileChildren::ImportDeclaration(import) = &source_file.children[1] else {
    panic!("expected an import declaration");
};
let ImportDeclarationChildren::ImportSpec(spec) = &import.children else {
    panic!("expected a single import spec");
};
assert!(spec.name.is_none()); // no alias

// 3) The function declaration — `func main() { ... }`.
let SourceFileChildren::FunctionDeclaration(func) = &source_file.children[2] else {
    panic!("expected a function declaration");
};
assert_eq!(func.name.text(), "main");
assert!(func.parameters.children.is_empty()); // no parameters
assert!(func.result.is_none());                // no return type
assert!(func.body.is_some());                  // has a body