treesitter_types_go/lib.rs
1//! Strongly-typed AST types for Go, auto-generated from
2//! [`tree-sitter-go`](https://docs.rs/tree-sitter-go)'s `node-types.json`.
3//!
4//! This crate is generated by [`treesitter-types`](https://docs.rs/treesitter-types) and is
5//! automatically kept up to date when a new version of the grammar crate is released.
6//!
7//! These types have been tested by parsing the
8//! [Go compiler](https://github.com/golang/go) source code.
9//!
10//! See the [Tree-sitter](https://tree-sitter.github.io/tree-sitter/) project for more
11//! information about the underlying parser framework.
12//!
13//! # Example
14//!
15//! ```
16//! use treesitter_types_go::*;
17//!
18//! // A minimal Go hello-world program.
19//! let src = b"\
20//! package main
21//!
22//! import \"fmt\"
23//!
24//! func main() {
25//! fmt.Println(\"Hello, World!\")
26//! }
27//! ";
28//!
29//! // Parse the source with tree-sitter and convert into typed AST.
30//! let mut parser = tree_sitter::Parser::new();
31//! parser.set_language(&tree_sitter_go::LANGUAGE.into()).unwrap();
32//! let tree = parser.parse(src, None).unwrap();
33//! let source_file = SourceFile::from_node(tree.root_node(), src).unwrap();
34//!
35//! // The source file has three top-level children.
36//! assert_eq!(source_file.children.len(), 3);
37//!
38//! // 1) The package clause — `package main`.
39//! let SourceFileChildren::PackageClause(pkg) = &source_file.children[0] else {
40//! panic!("expected a package clause");
41//! };
42//! assert_eq!(pkg.children.text(), "main");
43//!
44//! // 2) The import declaration — `import "fmt"`.
45//! let SourceFileChildren::ImportDeclaration(import) = &source_file.children[1] else {
46//! panic!("expected an import declaration");
47//! };
48//! let ImportDeclarationChildren::ImportSpec(spec) = &import.children else {
49//! panic!("expected a single import spec");
50//! };
51//! assert!(spec.name.is_none()); // no alias
52//!
53//! // 3) The function declaration — `func main() { ... }`.
54//! let SourceFileChildren::FunctionDeclaration(func) = &source_file.children[2] else {
55//! panic!("expected a function declaration");
56//! };
57//! assert_eq!(func.name.text(), "main");
58//! assert!(func.parameters.children.is_empty()); // no parameters
59//! assert!(func.result.is_none()); // no return type
60//! assert!(func.body.is_some()); // has a body
61//! ```
62
63pub use tree_sitter_go;
64pub use treesitter_types::tree_sitter;
65pub use treesitter_types::{FromNode, LeafNode, ParseError, Span, Spanned};
66
67mod generated;
68pub use generated::*;