FunctionNode.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. * @thanks bhouston / https://clara.io/
  4. */
  5. import { TempNode } from './TempNode.js';
  6. import { NodeLib } from './NodeLib.js';
  7. function FunctionNode( src, includesOrType, extensionsOrKeywords, keywordsOrExtensions, includes ) {
  8. this.isMethod = typeof includesOrType !== "string";
  9. this.useKeywords = true;
  10. TempNode.call( this, this.isMethod ? null : includesOrType );
  11. if ( this.isMethod ) {
  12. this.eval( src, includesOrType, extensionsOrKeywords, keywordsOrExtensions );
  13. } else {
  14. this.eval( src, includes, keywordsOrExtensions, extensionsOrKeywords );
  15. }
  16. };
  17. FunctionNode.rDeclaration = /^([a-z_0-9]+)\s([a-z_0-9]+)\s*\((.*?)\)/i;
  18. FunctionNode.rProperties = /[a-z_0-9]+/ig;
  19. FunctionNode.prototype = Object.create( TempNode.prototype );
  20. FunctionNode.prototype.constructor = FunctionNode;
  21. FunctionNode.prototype.nodeType = "Function";
  22. FunctionNode.prototype.isShared = function ( builder, output ) {
  23. return ! this.isMethod;
  24. };
  25. FunctionNode.prototype.getType = function ( builder ) {
  26. return builder.getTypeByFormat( this.type );
  27. };
  28. FunctionNode.prototype.getInputByName = function ( name ) {
  29. var i = this.inputs.length;
  30. while ( i -- ) {
  31. if ( this.inputs[ i ].name === name )
  32. return this.inputs[ i ];
  33. }
  34. };
  35. FunctionNode.prototype.getIncludeByName = function ( name ) {
  36. var i = this.includes.length;
  37. while ( i -- ) {
  38. if ( this.includes[ i ].name === name )
  39. return this.includes[ i ];
  40. }
  41. };
  42. FunctionNode.prototype.generate = function ( builder, output ) {
  43. var match, offset = 0, src = this.src;
  44. for ( var i = 0; i < this.includes.length; i ++ ) {
  45. builder.include( this.includes[ i ], this );
  46. }
  47. for ( var ext in this.extensions ) {
  48. builder.extensions[ ext ] = true;
  49. }
  50. while ( match = FunctionNode.rProperties.exec( this.src ) ) {
  51. var prop = match[ 0 ],
  52. isGlobal = this.isMethod ? ! this.getInputByName( prop ) : true,
  53. reference = prop;
  54. if ( this.keywords[ prop ] || ( this.useKeywords && isGlobal && NodeLib.containsKeyword( prop ) ) ) {
  55. var node = this.keywords[ prop ];
  56. if ( ! node ) {
  57. var keyword = NodeLib.getKeywordData( prop );
  58. if ( keyword.cache ) node = builder.keywords[ prop ];
  59. node = node || NodeLib.getKeyword( prop, builder );
  60. if ( keyword.cache ) builder.keywords[ prop ] = node;
  61. }
  62. reference = node.build( builder );
  63. }
  64. if ( prop != reference ) {
  65. src = src.substring( 0, match.index + offset ) + reference + src.substring( match.index + prop.length + offset );
  66. offset += reference.length - prop.length;
  67. }
  68. if ( this.getIncludeByName( reference ) === undefined && NodeLib.contains( reference ) ) {
  69. builder.include( NodeLib.get( reference ) );
  70. }
  71. }
  72. if ( output === 'source' ) {
  73. return src;
  74. } else if ( this.isMethod ) {
  75. builder.include( this, false, src );
  76. return this.name;
  77. } else {
  78. return builder.format( '( ' + src + ' )', this.getType( builder ), output );
  79. }
  80. };
  81. FunctionNode.prototype.eval = function ( src, includes, extensions, keywords ) {
  82. this.src = src || '';
  83. this.includes = includes || [];
  84. this.extensions = extensions || {};
  85. this.keywords = keywords || {};
  86. if ( this.isMethod ) {
  87. var match = this.src.match( FunctionNode.rDeclaration );
  88. this.inputs = [];
  89. if ( match && match.length == 4 ) {
  90. this.type = match[ 1 ];
  91. this.name = match[ 2 ];
  92. var inputs = match[ 3 ].match( FunctionNode.rProperties );
  93. if ( inputs ) {
  94. var i = 0;
  95. while ( i < inputs.length ) {
  96. var qualifier = inputs[ i ++ ];
  97. var type, name;
  98. if ( qualifier == 'in' || qualifier == 'out' || qualifier == 'inout' ) {
  99. type = inputs[ i ++ ];
  100. } else {
  101. type = qualifier;
  102. qualifier = '';
  103. }
  104. name = inputs[ i ++ ];
  105. this.inputs.push( {
  106. name: name,
  107. type: type,
  108. qualifier: qualifier
  109. } );
  110. }
  111. }
  112. } else {
  113. this.type = '';
  114. this.name = '';
  115. }
  116. }
  117. };
  118. FunctionNode.prototype.copy = function ( source ) {
  119. TempNode.prototype.copy.call( this, source );
  120. this.isMethod = source.isMethod;
  121. this.useKeywords = source.useKeywords;
  122. this.eval( source.src, source.includes, source.extensions, source.keywords );
  123. if ( source.type !== undefined ) this.type = source.type;
  124. };
  125. FunctionNode.prototype.toJSON = function ( meta ) {
  126. var data = this.getJSONNode( meta );
  127. if ( ! data ) {
  128. data = this.createJSONNode( meta );
  129. data.src = this.src;
  130. data.isMethod = this.isMethod;
  131. data.useKeywords = this.useKeywords;
  132. if ( ! this.isMethod ) data.type = this.type;
  133. data.extensions = JSON.parse( JSON.stringify( this.extensions ) );
  134. data.keywords = {};
  135. for ( var keyword in this.keywords ) {
  136. data.keywords[ keyword ] = this.keywords[ keyword ].toJSON( meta ).uuid;
  137. }
  138. if ( this.includes.length ) {
  139. data.includes = [];
  140. for ( var i = 0; i < this.includes.length; i ++ ) {
  141. data.includes.push( this.includes[ i ].toJSON( meta ).uuid );
  142. }
  143. }
  144. }
  145. return data;
  146. };
  147. export { FunctionNode };