Skip to main content

tree_sitter_glsl_spec/
lib.rs

1//! This crate provides GLSL language support for the [`tree-sitter`] parsing library.
2//!
3//! Typically, you will use the [`LANGUAGE`] constant to add this language to a
4//! tree-sitter [`Parser`], and then use the parser to parse some code:
5//!
6//! ```
7//! let code = r#"
8//!     void fragment_shader() {
9//!         gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
10//!     }
11//! "#;
12//! let mut parser = tree_sitter::Parser::new();
13//! let language = tree_sitter_glsl_spec::LANGUAGE;
14//! parser
15//!     .set_language(&language.into())
16//!     .expect("Error loading GLSL parser");
17//! let tree = parser.parse(code, None).unwrap();
18//! assert!(!tree.root_node().has_error());
19//! ```
20//!
21//! [`Parser`]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Parser.html
22//! [tree-sitter]: https://tree-sitter.github.io/
23
24use tree_sitter_language::LanguageFn;
25
26unsafe extern "C" {
27    fn tree_sitter_glsl() -> *const ();
28}
29
30/// The tree-sitter [`LanguageFn`][LanguageFn] for this grammar.
31///
32/// [LanguageFn]: https://docs.rs/tree-sitter-language/*/tree_sitter_language/struct.LanguageFn.html
33pub const LANGUAGE: LanguageFn = unsafe { LanguageFn::from_raw(tree_sitter_glsl) };
34
35/// The content of the [`node-types.json`][] file for this grammar.
36///
37/// [`node-types.json`]: https://tree-sitter.github.io/tree-sitter/using-parsers#static-node-types
38pub const NODE_TYPES: &str = include_str!("../../src/node-types.json");
39
40/// Syntax highlighting query. Maps GLSL nodes to highlight capture names
41/// (`@keyword`, `@function`, `@type`, `@operator`, `@variable`, etc.).
42/// Includes built-in function/variable/constant recognition.
43///
44/// Use with [`tree_sitter_highlight::HighlightConfiguration`].
45pub const HIGHLIGHTS_QUERY: &str = include_str!("../../queries/highlights.scm");
46
47/// Language injection query. Identifies `#ifdef __cplusplus` / `#ifdef __STDC__`
48/// guard blocks and marks their foreign-language content for injection
49/// (`cpp` or `c`).
50///
51/// Requires `OPT.MULTILINGUAL` to be enabled in grammar.js before the C
52/// sources are generated.
53pub const INJECTIONS_QUERY: &str = include_str!("../../queries/injections.scm");
54
55/// Local variable tracking query. Defines scopes (functions, blocks, loops)
56/// and tracks identifier definitions and references within them.
57///
58/// Use with [`tree_sitter_highlight::HighlightConfiguration`] for
59/// scope-aware highlighting or with code navigation tools.
60pub const LOCALS_QUERY: &str = include_str!("../../queries/locals.scm");
61
62/// Tags query for code navigation. Captures function definitions, type
63/// definitions, variable declarations, function calls, and type references.
64///
65/// Use with [`tree_sitter_tags::TagsConfiguration`] for symbol indexing.
66pub const TAGS_QUERY: &str = include_str!("../../queries/tags.scm");
67
68/// Version tags query. Captures GLSL constructs whose availability depends
69/// on a minimum `#version` (e.g., `uint` requires 130, `double` requires 400).
70/// Intended for post-parse validation, not syntax highlighting.
71pub const VERSION_TAGS_QUERY: &str = include_str!("../../queries/version-tags.scm");
72
73/// Constructor heuristic query. Identifies function calls where the callee
74/// is a user-defined type name (capitalized identifier), which are likely
75/// struct constructors. Opt-in — not included in default highlighting.
76pub const CONSTRUCTOR_HEURISTICS_QUERY: &str =
77    include_str!("../../queries/constructor-heuristics.scm");
78
79#[cfg(test)]
80mod tests {
81    #[test]
82    fn test_can_load_grammar() {
83        let mut parser = tree_sitter::Parser::new();
84        parser
85            .set_language(&super::LANGUAGE.into())
86            .expect("Error loading GLSL language");
87    }
88}