FunctionNode.js 5.0 KB

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