Crates.io | glsl-lang-quote |
lib.rs | glsl-lang-quote |
version | 0.6.1 |
source | src |
created_at | 2021-04-04 23:13:07.43048 |
updated_at | 2024-10-01 20:19:40.350641 |
description | Quoting proc-macro for glsl-lang |
homepage | https://github.com/alixinne/glsl-lang |
repository | https://github.com/alixinne/glsl-lang |
max_upload_size | |
id | 379048 |
size | 84,629 |
glsl-lang-quote
offers proc-macros to quote GLSL syntax in Rust, using the [glsl_lang] crate.
use glsl_lang_quote::glsl;
// Parse a translation unit at compile time
let ast = glsl! {
void main() {
gl_FragColor = vec4(1., 0., 0., 1.);
}
};
// There is exactly one external declaration
assert_eq!(ast.0.len(), 1);
This crate offers a set of features quote-expr
, and quote-statement
. Enabling those
features enable the respective parser-expr
, and parser-statement
in [glsl_lang], which
creates dedicated parsers for those types of GLSL fragments of the language grammar.
This is the most efficient option for parsing lots of expressions and statements at
compile-time, however this will slow down the initial compilation of glsl-lang-quote
since
the generated parser file in [glsl_lang] will be much larger.
This is why by default, this crate enables the quote-parsable
feature, which uses
[glsl_lang]'s [glsl_lang::parse::Parsable] trait, whose limitations apply. Whichever method you
chose, the following code will work:
use glsl_lang_quote::glsl_expr;
// Parse an expression
let ast = glsl_expr! {
a = vec4(1., 0., 0., 1.)
};
Since [glsl_lang]'s parser has a stateful lexer (to handle the fact that GLSL's grammar is not
context-free), declaring a type (a struct
for example) and using it must happen in the same
macro invocation, otherwise the parser will forget about the previously declared types. The
best is to only parse whole translation units ([glsl!] macro), or parse unambiguous fragments
(such as expressions, but not statements).
use glsl_lang_quote::{glsl, glsl_statement};
// This is ok:
let ast = glsl! {
struct PointLight {
vec3 pos;
vec3 color;
}
PointLight p;
};
// This will not compile, PointLight can't be parsed as a type name without extra state
let statement = glsl_expr! {
PointLight p;
};
## Author
Alixinne <alixinne@pm.me>
## License
BSD-3-Clause