| Crates.io | oak-gsgl |
| lib.rs | oak-gsgl |
| version | 0.0.1 |
| created_at | 2025-10-21 00:14:10.997698+00 |
| updated_at | 2026-01-23 04:28:44.96178+00 |
| description | GSGL language parser with support for modern GSGL syntax and features. |
| homepage | https://github.com/ygg-lang/oaks |
| repository | https://github.com/ygg-lang/oaks |
| max_upload_size | |
| id | 1892936 |
| size | 84,499 |
A high-performance GLSL (OpenGL Shading Language) parser for Rust, built with the Oak parser combinator framework. Parse shader programs with comprehensive AST generation and error handling.
Oak GLSL provides robust parsing capabilities for GLSL shader files, supporting vertex, fragment, geometry, and compute shaders. Built on the Oak parser combinator framework, it delivers excellent performance and detailed error messages.
Add Oak GLSL to your Cargo.toml:
use oak::{Parser, Language};
use oak_gsgl::GLSLLanguage;
fn main() {
let source = r#"
#version 330 core
layout(location = 0) in vec3 aPos;
layout(location = 1) in vec2 aTexCoord;
out vec2 TexCoord;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main() {
gl_Position = projection * view * model * vec4(aPos, 1.0);
TexCoord = aTexCoord;
}
"#;
let mut parser = Parser::<GLSLLanguage>::new();
match parser.parse(&source) {
Ok(ast) => {
println!("Parsed AST: {:#?}", ast);
}
Err(error) => {
eprintln!("Parse error: {}", error);
}
}
}
use oak::{Parser, Language};
use oak_gsgl::GLSLLanguage;
fn main() {
let source = r#"
#version 450 core
in vec2 TexCoord;
in vec3 Normal;
in vec3 FragPos;
out vec4 FragColor;
struct Material {
vec3 ambient;
vec3 diffuse;
vec3 specular;
float shininess;
};
struct Light {
vec3 position;
vec3 ambient;
vec3 diffuse;
vec3 specular;
};
uniform Material material;
uniform Light light;
uniform vec3 viewPos;
uniform sampler2D texture1;
vec3 calculatePhong(vec3 normal, vec3 fragPos, vec3 viewDir) {
vec3 lightDir = normalize(light.position - fragPos);
// Ambient
vec3 ambient = light.ambient * material.ambient;
// Diffuse
float diff = max(dot(normal, lightDir), 0.0);
vec3 diffuse = light.diffuse * (diff * material.diffuse);
// Specular
vec3 reflectDir = reflect(-lightDir, normal);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
vec3 specular = light.specular * (spec * material.specular);
return ambient + diffuse + specular;
}
void main() {
vec3 norm = normalize(Normal);
vec3 viewDir = normalize(viewPos - FragPos);
vec3 result = calculatePhong(norm, FragPos, viewDir);
vec3 texColor = texture(texture1, TexCoord).rgb;
FragColor = vec4(result * texColor, 1.0);
}
"#;
let mut parser = Parser::<GLSLLanguage>::new();
match parser.parse(&source) {
Ok(ast) => {
println!("Advanced shader parsed successfully!");
}
Err(error) => {
eprintln!("Parse error: {}", error);
}
}
}
Oak GLSL supports parsing compute shaders:
let source = r#"
#version 430
layout(local_size_x = 64, local_size_y = 1, local_size_z = 1) in;
layout(std430, binding = 0) buffer InputBuffer {
float data[];
} inputBuffer;
layout(std430, binding = 1) buffer OutputBuffer {
float result[];
} outputBuffer;
uniform float multiplier;
void main() {
uint index = gl_GlobalInvocationID.x;
if (index < inputBuffer.data.length()) {
outputBuffer.result[index] = inputBuffer.data[index] * multiplier;
}
}
"#;
Parse geometry shaders with input/output layout qualifiers:
let source = r#"
#version 330 core
layout(points) in;
layout(line_strip, max_vertices = 2) out;
uniform mat4 projection;
uniform float time;
void main() {
vec4 pos = gl_in[0].gl_Position;
gl_Position = projection * (pos + vec4(-0.1, 0.0, 0.0, 0.0));
EmitVertex();
gl_Position = projection * (pos + vec4(0.1, 0.0, 0.0, 0.0));
EmitVertex();
EndPrimitive();
}
"#;
Handle GLSL preprocessor directives:
let source = r#"
#version 450 core
#define MAX_LIGHTS 16
#define PI 3.14159265359
#ifdef USE_NORMAL_MAPPING
#extension GL_OES_standard_derivatives : enable
#endif
#if defined(USE_SHADOWS) && defined(USE_PCF)
#define SHADOW_FILTER_SIZE 3
#endif
// Shader code continues...
"#;
The parser generates a rich AST with the following main node types:
GLSLFile - Root node containing the entire shaderVersionDirective - GLSL version specificationExtensionDirective - Extension directivesPreprocessorDirective - Preprocessor commandsFunctionDefinition - Function definitionsStructDefinition - Struct type definitionsVariableDeclaration - Variable declarationsTypeQualifier - Type qualifiers (in, out, uniform, etc.)Expression - Mathematical and logical expressionsStatement - Control flow statementsOak GLSL is designed for high performance:
Oak GLSL integrates seamlessly with the Oak ecosystem:
use oak::{Parser, Language};
use oak_gsgl::GLSLLanguage;
// Use with other Oak parsers
let mut parser = Parser::<GLSLLanguage>::new();
let result = parser.parse(glsl_source);
More examples can be found in the examples directory:
We welcome contributions! Please see our Contributing Guide for details.