StructNode.js 1.7 KB

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