StructNode.js 1.6 KB

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