NAME

Parse::GLSL - extract an Abstract Syntax Tree from GLSL text

VERSION

version 0.002

SYNOPSIS

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);

DESCRIPTION

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.

METHODS

new

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...

where_am_i

Reports current position in line if $self->{debug} is set to 2 or above.

parse

Parse the given GLSL string.

parse_item

Parse an "entry" in the GLSL text. Currently this consists of top-level variable declarations and function declarations.

parse_declaration

Parse a variable declaration.

parse_definition

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_type

Parse the variable type.

parse_parameter

Parse the parameters for a function definition.

parse_function

Parse a function definition and code block.

parse_block

Parse a block of statements (including the { ... } delimiters).

parse_statement

Parse a single statement. This includes the trailing ; since it's a terminator not a separator in GLSL.

parse_loopy_thing

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".

token_control_keyword

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 }.

parse_expression

Parse an expression, such as fract(c) or 1 + 3 / 12 * vec3(1.0).

parse_nested_expression

Parse the bit inside an expression ... so not really a nested expression, more like an expression atom or component maybe?

token_operator

hey look it's a binary operator

token_function

Known built-in functions. Eventually these will be extracted to a separate definition block so that parameter types + counts can be verified.

token_preprocessor_directive

Parse a preprocessor directive.

Note that '#' is perfectly valid as a directive.

token_macro

Pick up on #defined (macro) values.

expand_macro

Attempt to expand the given macro.

token_glsl_ident

A GLSL identifier. Somewhat vague term, currently includes variables and qualified pieces (colour.r).

parse_statements

Parse more than one statement.

SEE ALSO

AUTHOR

Tom Molesworth <cpan@entitymodel.com>

LICENSE

Copyright Tom Molesworth 2011-2012. Licensed under the same terms as Perl itself.