FunctionNode.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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.getShared = 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. var matches = [];
  49. while ( match = propertiesRegexp.exec( this.src ) ) matches.push( match );
  50. for ( var i = 0; i < matches.length; i++ ) {
  51. var match = matches[i];
  52. var prop = match[ 0 ],
  53. isGlobal = this.isMethod ? ! this.getInputByName( prop ) : true,
  54. reference = prop;
  55. if ( this.keywords[ prop ] || ( this.useKeywords && isGlobal && NodeLib.containsKeyword( prop ) ) ) {
  56. var node = this.keywords[ prop ];
  57. if ( ! node ) {
  58. var keyword = NodeLib.getKeywordData( prop );
  59. if ( keyword.cache ) node = builder.keywords[ prop ];
  60. node = node || NodeLib.getKeyword( prop, builder );
  61. if ( keyword.cache ) builder.keywords[ prop ] = node;
  62. }
  63. reference = node.build( builder );
  64. }
  65. if ( prop != reference ) {
  66. src = src.substring( 0, match.index + offset ) + reference + src.substring( match.index + prop.length + offset );
  67. offset += reference.length - prop.length;
  68. }
  69. if ( this.getIncludeByName( reference ) === undefined && NodeLib.contains( reference ) ) {
  70. builder.include( NodeLib.get( reference ) );
  71. }
  72. }
  73. if ( output === 'source' ) {
  74. return src;
  75. } else if ( this.isMethod ) {
  76. builder.include( this, false, src );
  77. return this.name;
  78. } else {
  79. return builder.format( '( ' + src + ' )', this.getType( builder ), output );
  80. }
  81. };
  82. FunctionNode.prototype.eval = function ( src, includes, extensions, keywords ) {
  83. this.src = src || '';
  84. this.includes = includes || [];
  85. this.extensions = extensions || {};
  86. this.keywords = keywords || {};
  87. if ( this.isMethod ) {
  88. var match = this.src.match( declarationRegexp );
  89. this.inputs = [];
  90. if ( match && match.length == 4 ) {
  91. this.type = match[ 1 ];
  92. this.name = match[ 2 ];
  93. var inputs = match[ 3 ].match( propertiesRegexp );
  94. if ( inputs ) {
  95. var i = 0;
  96. while ( i < inputs.length ) {
  97. var qualifier = inputs[ i ++ ];
  98. var type, name;
  99. if ( qualifier == 'in' || qualifier == 'out' || qualifier == 'inout' ) {
  100. type = inputs[ i ++ ];
  101. } else {
  102. type = qualifier;
  103. qualifier = '';
  104. }
  105. name = inputs[ i ++ ];
  106. this.inputs.push( {
  107. name: name,
  108. type: type,
  109. qualifier: qualifier
  110. } );
  111. }
  112. }
  113. } else {
  114. this.type = '';
  115. this.name = '';
  116. }
  117. }
  118. };
  119. FunctionNode.prototype.copy = function ( source ) {
  120. TempNode.prototype.copy.call( this, source );
  121. this.isMethod = source.isMethod;
  122. this.useKeywords = source.useKeywords;
  123. this.eval( source.src, source.includes, source.extensions, source.keywords );
  124. if ( source.type !== undefined ) this.type = source.type;
  125. };
  126. FunctionNode.prototype.toJSON = function ( meta ) {
  127. var data = this.getJSONNode( meta );
  128. if ( ! data ) {
  129. data = this.createJSONNode( meta );
  130. data.src = this.src;
  131. data.isMethod = this.isMethod;
  132. data.useKeywords = this.useKeywords;
  133. if ( ! this.isMethod ) data.type = this.type;
  134. data.extensions = JSON.parse( JSON.stringify( this.extensions ) );
  135. data.keywords = {};
  136. for ( var keyword in this.keywords ) {
  137. data.keywords[ keyword ] = this.keywords[ keyword ].toJSON( meta ).uuid;
  138. }
  139. if ( this.includes.length ) {
  140. data.includes = [];
  141. for ( var i = 0; i < this.includes.length; i ++ ) {
  142. data.includes.push( this.includes[ i ].toJSON( meta ).uuid );
  143. }
  144. }
  145. }
  146. return data;
  147. };
  148. export { FunctionNode };