glsl-token-functions
Extract function definitions from an array of GLSL tokens from glsl-tokenizer.
You can use this to grab all of the functions in a shader, along with their arguments, return value and name.
Usage
functions = tokenFunctions(tokens)
Given an array of tokens
produced by glsl-tokenizer, return an array of function data in the following format:
name: 'functionName'type: 'vec4'body: 100 110args: 98 99outer: 80 111
fn.name
The function name. For example, the following function would be main
:
void
fn.type
The function return type. For example, the following function would be vec2
:
vec2
Note that this supports structs too! The following function's type
would be Ray
:
struct RayRay
fn.body
The start and end index in the original tokens array of the function body, i.e. the part between the curly braces. If the function doesn't have a body, this will be false
.
For example:
const tokenFunctions =const toString =const tokenize =const src = `void main();void main() { gl_FragColor = vec4(1); }`const tokens =const functions =console // falseconsole // " gl_FragColor = vec4(1); "{return}
fn.args
The start and end index in the original tokens array of the function arguments, i.e. the part including the parenthesis in the function definition.
For example:
const tokenFunctions =const toString =const tokenize =const src = `void main();void drawImage(vec3 ro, vec3 rd);`const tokens =const functions =console // "()"console // "(vec3 ro, vec3 rd)"{return}
fn.outer
The start and end index in the original tokens array of the entire function.
For example:
const tokenFunctions =const toString =const tokenize =const src = `void main();void main() { gl_FragColor = vec4(1); }`const tokens =const functions =console // "void main();"console // "void main() { gl_FragColor = vec4(1); }"{return}
Contributing
See stackgl/contributing for details.
License
MIT. See LICENSE.md for details.