GLSL manipulator
GLSL parser and code generator based on Google's glsl-unit grammar.
Install
npm install glsl-man
Usage
Parsing
var glsl = require('glsl-man');
var ast = glsl.parse(source);
Deparsing
var glsl = require('glsl-man');
var ast = glsl.parse(source);
var generated = glsl.string(ast);
Querying
var glsl = require('glsl-man');
var ast = glsl.parse(source);
var uniforms = glsl.query.all(
ast,
glsl.query.selector('declarator[typeAttribute] > type[qualifier=uniform]'));
API
Parsing
glsl.parse(string)
- Generates AST from GLSL
Deparsing
glsl.string(ASTNode, options)
- Generates GLSL from AST
parse(string)
- options - The default options are described below:
{
tab: '\t', // Character used for tab
space: ' ', // Character used for space
newline: '\n', // Character used for newlines
// The following should not be altered to produce valid GLSL
terminator: ';', // Character used to terminate a statement
comma: ',' // Character used for comma
}
glsl.wrap(ASTNode)
- Wraps the given node in a 'root' scope. Useful for
parse(string)
Querying
glsl.query.selector(string)
- Returns a selector
glsl.query.all(node, selector, matches)
- Searches the tree depth first and returns all nodes that match the selector
glsl.query.first(node, selector)
- Searches the tree depth first and returns the first node that matches the selector
glsl.query.children(node, selector, matches)
- Searches only the immediate subnodes of the given node and returns all children that match the selector
glsl.query.firstChild(node, selector)
- Searches only the immediate subnodes of the given node and returns the first node that matches the selector
glsl.query.subnodes(node)
- Returns a list of all subnodes of the given node that can be further traversed
Modifying
glsl.mod.find(node)
- Returns an object withindex
andstatements
keys.
glsl.mod.remove(node)
- Removes the given node from it's AST.
glsl.mod.replace(node, newNode)
- Replaces the given node withnewNode
.
glsl.mod.add(node, newNode, after)
- InsertsnewNode
before or afternode
.
glsl.mod.addBefore(node, newNode)
- Shortcut toglsl.mod.add
.
glsl.mod.addAfter(node, newNode)
- Shortcut toglsl.mod.add
.