parse function

dynamic parse(
  1. dynamic source
)

Implementation

parse(source) {
  source = source.trim();

  var declaration = source.match(declarationRegexp);

  if (declaration != null && declaration.length == 4) {
    // tokenizer

    var inputsCode = declaration[2];
    var propsMatches = [];

    var matches = propertiesRegexp.allMatches(inputsCode);
    for (var match in matches) {
      propsMatches.add(match.group(0));
    }

    // parser

    var inputs = [];

    var i = 0;

    while (i < propsMatches.length) {
      var name = propsMatches[i++][0];
      var type = propsMatches[i++][0];

      propsMatches[i++][0]; // precision

      inputs.add(NodeFunctionInput(type, name));
    }

    //

    var blockCode = source.substring(declaration[0].length);

    var name = declaration[1] != undefined ? declaration[1] : '';
    var type = declaration[3];

    return {type, inputs, name, inputsCode, blockCode};
  } else {
    throw ('FunctionNode: Function is not a WGSL code.');
  }
}