StructNode.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. import { TempNode } from './TempNode.js';
  5. import { FunctionNode } from './FunctionNode.js';
  6. function StructNode( src ) {
  7. TempNode.call( this);
  8. this.eval( src );
  9. };
  10. StructNode.rDeclaration = /^struct\s*([a-z_0-9]+)\s*{\s*((.|\n)*?)}/img;
  11. StructNode.rProperties = /\s*(\w*?)\s*(\w*?)(\=|\;)/img;
  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. StructNode.prototype.generate = function ( builder, output ) {
  26. if ( output === 'source' ) {
  27. return this.src + ';';
  28. } else {
  29. return builder.format( "(" + src + ")", this.getType( builder ), output );
  30. }
  31. };
  32. StructNode.prototype.eval = function ( src ) {
  33. this.src = src || '';
  34. this.inputs = [];
  35. var declaration = StructNode.rDeclaration.exec( this.src );
  36. if (declaration) {
  37. var properties = declaration[2], matchType, matchName;
  38. while ( matchType = FunctionNode.rProperties.exec( properties ) ) {
  39. matchName = FunctionNode.rProperties.exec( properties )[0];
  40. this.inputs.push( {
  41. name: matchName,
  42. type: matchType
  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 };