FunctionNode.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. import { TempNode } from './TempNode.js';
  2. import { NodeLib } from './NodeLib.js';
  3. var declarationRegexp = /^\s*([a-z_0-9]+)\s+([a-z_0-9]+)\s*\(([\s\S]*?)\)/i,
  4. propertiesRegexp = /[a-z_0-9]+/ig;
  5. function FunctionNode( src, includes, extensions, keywords, type ) {
  6. this.isMethod = type === undefined;
  7. this.isInterface = false;
  8. TempNode.call( this, type );
  9. this.parse( src, includes, extensions, keywords );
  10. }
  11. FunctionNode.prototype = Object.create( TempNode.prototype );
  12. FunctionNode.prototype.constructor = FunctionNode;
  13. FunctionNode.prototype.nodeType = 'Function';
  14. FunctionNode.prototype.useKeywords = true;
  15. FunctionNode.prototype.getShared = function ( /* builder, output */ ) {
  16. return ! this.isMethod;
  17. };
  18. FunctionNode.prototype.getType = function ( builder ) {
  19. return builder.getTypeByFormat( this.type );
  20. };
  21. FunctionNode.prototype.getInputByName = function ( name ) {
  22. var i = this.inputs.length;
  23. while ( i -- ) {
  24. if ( this.inputs[ i ].name === name ) {
  25. return this.inputs[ i ];
  26. }
  27. }
  28. };
  29. FunctionNode.prototype.getIncludeByName = function ( name ) {
  30. var i = this.includes.length;
  31. while ( i -- ) {
  32. if ( this.includes[ i ].name === name ) {
  33. return this.includes[ i ];
  34. }
  35. }
  36. };
  37. FunctionNode.prototype.generate = function ( builder, output ) {
  38. var match, offset = 0, src = this.src;
  39. for ( var i = 0; i < this.includes.length; i ++ ) {
  40. builder.include( this.includes[ i ], this );
  41. }
  42. for ( var ext in this.extensions ) {
  43. builder.extensions[ ext ] = true;
  44. }
  45. var matches = [];
  46. while ( match = propertiesRegexp.exec( this.src ) ) matches.push( match );
  47. for ( var i = 0; i < matches.length; i ++ ) {
  48. var match = matches[ i ];
  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. if ( ! this.isInterface ) {
  74. builder.include( this, false, src );
  75. }
  76. return this.name;
  77. } else {
  78. return builder.format( '( ' + src + ' )', this.getType( builder ), output );
  79. }
  80. };
  81. FunctionNode.prototype.parse = 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( declarationRegexp );
  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( propertiesRegexp );
  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. this.isInterface = this.src.indexOf( '{' ) === - 1;
  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.parse( source.src, source.includes, source.extensions, source.keywords );
  124. if ( source.type !== undefined ) this.type = source.type;
  125. return this;
  126. };
  127. FunctionNode.prototype.toJSON = function ( meta ) {
  128. var data = this.getJSONNode( meta );
  129. if ( ! data ) {
  130. data = this.createJSONNode( meta );
  131. data.src = this.src;
  132. data.isMethod = this.isMethod;
  133. data.useKeywords = this.useKeywords;
  134. if ( ! this.isMethod ) data.type = this.type;
  135. data.extensions = JSON.parse( JSON.stringify( this.extensions ) );
  136. data.keywords = {};
  137. for ( var keyword in this.keywords ) {
  138. data.keywords[ keyword ] = this.keywords[ keyword ].toJSON( meta ).uuid;
  139. }
  140. if ( this.includes.length ) {
  141. data.includes = [];
  142. for ( var i = 0; i < this.includes.length; i ++ ) {
  143. data.includes.push( this.includes[ i ].toJSON( meta ).uuid );
  144. }
  145. }
  146. }
  147. return data;
  148. };
  149. export { FunctionNode };