FunctionNode.js 4.7 KB

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