FunctionNode.js 4.6 KB

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