19 releases
new 0.7.2 | Nov 22, 2024 |
---|---|
0.6.1 | Oct 1, 2024 |
0.5.3 | Jul 23, 2024 |
0.5.2 | Nov 13, 2023 |
0.1.3 | Apr 16, 2021 |
#17 in #lalr-parser
244 downloads per month
440KB
11K
SLoC
glsl-lang-quote
glsl-lang-quote
offers proc-macros to quote GLSL syntax in Rust, using the [glsl_lang] crate.
Usage
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);
Parsing expressions
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.)
};
Quoting and stateful lexers
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
Dependencies
~3.5–10MB
~108K SLoC