StructNode.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { TempNode } from './TempNode.js';
  2. var declarationRegexp = /^struct\s*([a-z_0-9]+)\s*{\s*((.|\n)*?)}/img,
  3. propertiesRegexp = /\s*(\w*?)\s*(\w*?)(\=|\;)/img;
  4. function StructNode( src ) {
  5. TempNode.call( this );
  6. this.parse( src );
  7. }
  8. StructNode.prototype = Object.create( TempNode.prototype );
  9. StructNode.prototype.constructor = StructNode;
  10. StructNode.prototype.nodeType = 'Struct';
  11. StructNode.prototype.getType = function ( builder ) {
  12. return builder.getTypeByFormat( this.name );
  13. };
  14. StructNode.prototype.getInputByName = function ( name ) {
  15. var i = this.inputs.length;
  16. while ( i -- ) {
  17. if ( this.inputs[ i ].name === name ) {
  18. return this.inputs[ i ];
  19. }
  20. }
  21. };
  22. StructNode.prototype.generate = function ( builder, output ) {
  23. if ( output === 'source' ) {
  24. return this.src + ';';
  25. } else {
  26. return builder.format( '( ' + this.src + ' )', this.getType( builder ), output );
  27. }
  28. };
  29. StructNode.prototype.parse = function ( src ) {
  30. this.src = src || '';
  31. this.inputs = [];
  32. var declaration = declarationRegexp.exec( this.src );
  33. if ( declaration ) {
  34. var properties = declaration[ 2 ], match;
  35. while ( match = propertiesRegexp.exec( properties ) ) {
  36. this.inputs.push( {
  37. type: match[ 1 ],
  38. name: match[ 2 ]
  39. } );
  40. }
  41. this.name = declaration[ 1 ];
  42. } else {
  43. this.name = '';
  44. }
  45. this.type = this.name;
  46. };
  47. StructNode.prototype.toJSON = function ( meta ) {
  48. var data = this.getJSONNode( meta );
  49. if ( ! data ) {
  50. data = this.createJSONNode( meta );
  51. data.src = this.src;
  52. }
  53. return data;
  54. };
  55. export { StructNode };