glsl_lang/lib.rs
1//! `glsl-lang` is a crate implementing a LALR parser for the GLSL 4.x language,
2//! with partial support for preprocessor directives. Its AST and features are
3//! modeled after [Dimitri Sabadie's `glsl` crate](https://github.com/phaazon/glsl).
4//!
5//! See the [homepage](https://github.com/alixinne/glsl-lang) for more detailed comparison
6//! elements.
7//!
8//! # Examples
9//!
10//! ## Parsing GLSL
11//!
12//! ```
13//! use glsl_lang::{ast, parse::DefaultParse};
14//!
15//! // Some GLSL source to parse
16//! let source = r#"void main() {
17//! gl_FragColor = vec4(1., 0.5, 0.25, 1.);
18//! }"#;
19//!
20//! // Try parsing the source
21//! let ast = ast::TranslationUnit::parse(source);
22//! assert!(ast.is_ok());
23//! ```
24//!
25//! # Crate features
26//!
27//! This crate has the following features:
28//! - `parser-expr`: generate parser code for parsing GLSL expressions
29//! - `parser-statement`: generate parser code for parsing GLSL statements
30//!
31//! None of these features are enabled by default, as they significantly increase the compile
32//! times. As an alternative, you may use the [`Parsable`](crate::parse::Parsable) trait, which
33//! wraps grammar rules in suitable source and matches the result to extract the part of the AST
34//! we're interested in.
35//!
36//! ```
37//! // parse::Parse is not implemented for ast::Expr with the default features
38//! use glsl_lang::{ast, parse::Parsable};
39//!
40//! let source = "a = b ? 1.0 : 0.0";
41//!
42//! // Parse with Parsable::parse
43//! let ast = ast::Expr::parse(source);
44//! assert!(ast.is_ok());
45//! ```
46//!
47//! # Useful links
48//!
49//! - [The OpenGL Shading Language Version 4.60](https://www.khronos.org/registry/OpenGL/specs/gl/GLSLangSpec.4.60.pdf)
50
51#![deny(missing_docs)]
52
53use lalrpop_util::lalrpop_mod;
54
55pub use glsl_lang_lexer as lexer;
56pub use glsl_lang_types::ast;
57lalrpop_mod!(
58 #[allow(clippy::all)]
59 parser
60);
61pub mod parse;
62pub mod transpiler;
63pub mod visitor;
64
65#[cfg(test)]
66mod parse_tests;