Skip to main content

treesitter_types/codegen/
mod.rs

1pub mod emitter;
2pub mod grammar_ir;
3pub mod name_mangler;
4pub mod type_mapper;
5
6use proc_macro2::TokenStream;
7use std::path::Path;
8
9/// Generates a `TokenStream` of typed AST definitions from the contents of a `node-types.json`.
10pub fn generate(node_types_json: &str) -> Result<TokenStream, Error> {
11    let nodes = grammar_ir::parse_node_types(node_types_json)?;
12    let decisions = type_mapper::map_types(&nodes);
13    Ok(emitter::emit(&decisions))
14}
15
16/// Formats a `TokenStream` into pretty-printed Rust source code.
17pub fn format(tokens: &TokenStream) -> Result<String, Error> {
18    let file = syn::parse2(tokens.clone()).map_err(Error::Syn)?;
19    Ok(prettyplease::unparse(&file))
20}
21
22/// Generates typed AST code from `node-types.json` contents and returns it as formatted Rust source.
23pub fn generate_to_string(node_types_json: &str) -> Result<String, Error> {
24    let tokens = generate(node_types_json)?;
25    format(&tokens)
26}
27
28/// Reads a `node-types.json` file, generates typed AST code, and writes it to `$OUT_DIR`.
29///
30/// The generated code is formatted with `prettyplease` for readability.
31///
32/// Intended for use in `build.rs`:
33/// ```no_run
34/// treesitter_types::codegen::emit_to_out_dir("path/to/node-types.json").unwrap();
35/// ```
36///
37/// Then in `lib.rs`:
38/// ```ignore
39/// include!(concat!(env!("OUT_DIR"), "/treesitter_types_generated.rs"));
40/// ```
41pub fn emit_to_out_dir(node_types_path: impl AsRef<Path>) -> Result<(), Error> {
42    let node_types_path = node_types_path.as_ref();
43    let json = std::fs::read_to_string(node_types_path)
44        .map_err(|e| Error::Io(node_types_path.to_path_buf(), e))?;
45
46    emit_str_to_out_dir(&json)?;
47
48    // Tell Cargo to re-run if the input changes
49    println!("cargo:rerun-if-changed={}", node_types_path.display());
50
51    Ok(())
52}
53
54/// Generates typed AST code from a `node-types.json` string and writes it to `$OUT_DIR`.
55///
56/// This is useful when the JSON is available as a constant (e.g., from a grammar crate's
57/// `NODE_TYPES` constant) rather than a file path.
58///
59/// Intended for use in `build.rs`:
60/// ```ignore
61/// treesitter_types::codegen::emit_str_to_out_dir(tree_sitter_go::NODE_TYPES).unwrap();
62/// ```
63///
64/// Then in `lib.rs`:
65/// ```ignore
66/// include!(concat!(env!("OUT_DIR"), "/treesitter_types_generated.rs"));
67/// ```
68pub fn emit_str_to_out_dir(node_types_json: &str) -> Result<(), Error> {
69    let tokens = generate(node_types_json)?;
70    let code = format(&tokens)?;
71
72    let out_dir = std::env::var("OUT_DIR").map_err(|_| Error::NoOutDir)?;
73    let out_path = Path::new(&out_dir).join("treesitter_types_generated.rs");
74    std::fs::write(&out_path, code).map_err(|e| Error::Io(out_path, e))?;
75
76    Ok(())
77}
78
79#[derive(Debug, thiserror::Error)]
80pub enum Error {
81    #[error("failed to parse node-types.json: {0}")]
82    Json(#[from] serde_json::Error),
83
84    #[error("generated code is not valid Rust syntax: {0}")]
85    Syn(syn::Error),
86
87    #[error("I/O error on {0}: {1}")]
88    Io(std::path::PathBuf, #[source] std::io::Error),
89
90    #[error("OUT_DIR environment variable not set (are you running from build.rs?)")]
91    NoOutDir,
92}