Parse::GLSL - extract an Abstract Syntax Tree from GLSL text
version 0.002
use Parse::GLSL; use Data::Dumper; my $parser = Parse::GLSL->new; my $txt = ''; while(my $line = <DATA>) { if($line =~ /^\s*#\s*(?:(.*))$/) { my $cmd = $1; warn "Had directive [$cmd]\n"; } else { $txt .= $line; } } print Dumper $parser->from_string($txt);
Warning: This is a preview release, and the entire API is subject to change several times over the next few releases.
This module provides basic parsing for the OpenGL Shading Language (currently dealing with fragment and vertex shaders, eventually will be expanded to cover geometry, tesselation and compute shaders as well).
The GLSL document is parsed using Parser::MGC, rather than the reference 3D Labs GLSL parser implementation, and a Perl data structure is generated as output.
Currently very basic variable checking is performed - vars must be declared before use, so this would raise an error:
void main(void) { vec2 tex_coord; vec2 c = unknown_var * tex_coord.st; }
Further checks are planned in future versions.
The exact nature of the data structure returned is subject to change, so it's not currently documented.
Constructor - see Parser::MGC for full details. This is defined here to override the standard string delimiter to " since C strings don't use the ' delimiter. Not that strings have much place in GLSL per se...
Reports current position in line if $self->{debug} is set to 2 or above.
Parse the given GLSL string.
Parse an "entry" in the GLSL text. Currently this consists of top-level variable declarations and function declarations.
Parse a variable declaration.
Parse a "definition" (x = y), currently this also includes bare identifiers so that it works with "parse_declaration" but this behaviour is subject to change.
Parse the variable type.
Parse the parameters for a function definition.
Parse a function definition and code block.
Parse a block of statements (including the { ... } delimiters).
Parse a single statement. This includes the trailing ; since it's a terminator not a separator in GLSL.
Handle control statements like if, while, that sort of thing. Any comments about the name of this method should consider the first line in the "DESCRIPTION".
Parse a 'control keyword' (or 'loopy_thing' if you've been reading the other methods). That means if, while, and anything else that does(expression) { block }.
does(expression) { block }
Parse an expression, such as fract(c) or 1 + 3 / 12 * vec3(1.0).
Parse the bit inside an expression ... so not really a nested expression, more like an expression atom or component maybe?
hey look it's a binary operator
Known built-in functions. Eventually these will be extracted to a separate definition block so that parameter types + counts can be verified.
Parse a preprocessor directive.
Note that '#' is perfectly valid as a directive.
Pick up on #defined (macro) values.
Attempt to expand the given macro.
A GLSL identifier. Somewhat vague term, currently includes variables and qualified pieces (colour.r).
Parse more than one statement.
http://www.opengl.org - particularly the official "Orange Book"
OpenGL::Shader - if you want to use shaders with Perl
reference 3d Labs GLSL compiler
Tom Molesworth <cpan@entitymodel.com>
Copyright Tom Molesworth 2011-2012. Licensed under the same terms as Perl itself.
To install Parse::GLSL, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Parse::GLSL
CPAN shell
perl -MCPAN -e shell install Parse::GLSL
For more information on module installation, please visit the detailed CPAN module installation guide.