StructNode.js 1.4 KB

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