123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- import { TempNode } from './TempNode.js';
- import { NodeLib } from './NodeLib.js';
- const declarationRegexp = /^\s*([a-z_0-9]+)\s+([a-z_0-9]+)\s*\(([\s\S]*?)\)/i,
- propertiesRegexp = /[a-z_0-9]+/ig;
- class FunctionNode extends TempNode {
- constructor( src, includes, extensions, keywords, type ) {
- super( type );
- this.isMethod = type === undefined;
- this.isInterface = false;
- this.parse( src, includes, extensions, keywords );
- }
- getShared( /* builder, output */ ) {
- return ! this.isMethod;
- }
- getType( builder ) {
- return builder.getTypeByFormat( this.type );
- }
- getInputByName( name ) {
- let i = this.inputs.length;
- while ( i -- ) {
- if ( this.inputs[ i ].name === name ) {
- return this.inputs[ i ];
- }
- }
- }
- getIncludeByName( name ) {
- let i = this.includes.length;
- while ( i -- ) {
- if ( this.includes[ i ].name === name ) {
- return this.includes[ i ];
- }
- }
- }
- generate( builder, output ) {
- let match, offset = 0, src = this.src;
- for ( let i = 0; i < this.includes.length; i ++ ) {
- builder.include( this.includes[ i ], this );
- }
- for ( const ext in this.extensions ) {
- builder.extensions[ ext ] = true;
- }
- const matches = [];
- while ( match = propertiesRegexp.exec( this.src ) ) matches.push( match );
- for ( let i = 0; i < matches.length; i ++ ) {
- const match = matches[ i ];
- const prop = match[ 0 ],
- isGlobal = this.isMethod ? ! this.getInputByName( prop ) : true;
- let reference = prop;
- if ( this.keywords[ prop ] || ( this.useKeywords && isGlobal && NodeLib.containsKeyword( prop ) ) ) {
- let node = this.keywords[ prop ];
- if ( ! node ) {
- const keyword = NodeLib.getKeywordData( prop );
- if ( keyword.cache ) node = builder.keywords[ prop ];
- node = node || NodeLib.getKeyword( prop, builder );
- if ( keyword.cache ) builder.keywords[ prop ] = node;
- }
- reference = node.build( builder );
- }
- if ( prop !== reference ) {
- src = src.substring( 0, match.index + offset ) + reference + src.substring( match.index + prop.length + offset );
- offset += reference.length - prop.length;
- }
- if ( this.getIncludeByName( reference ) === undefined && NodeLib.contains( reference ) ) {
- builder.include( NodeLib.get( reference ) );
- }
- }
- if ( output === 'source' ) {
- return src;
- } else if ( this.isMethod ) {
- if ( ! this.isInterface ) {
- builder.include( this, false, src );
- }
- return this.name;
- } else {
- return builder.format( '( ' + src + ' )', this.getType( builder ), output );
- }
- }
- parse( src, includes, extensions, keywords ) {
- this.src = src || '';
- this.includes = includes || [];
- this.extensions = extensions || {};
- this.keywords = keywords || {};
- if ( this.isMethod ) {
- const match = this.src.match( declarationRegexp );
- this.inputs = [];
- if ( match && match.length == 4 ) {
- this.type = match[ 1 ];
- this.name = match[ 2 ];
- const inputs = match[ 3 ].match( propertiesRegexp );
- if ( inputs ) {
- let i = 0;
- while ( i < inputs.length ) {
- let qualifier = inputs[ i ++ ];
- let type;
- if ( qualifier === 'in' || qualifier === 'out' || qualifier === 'inout' ) {
- type = inputs[ i ++ ];
- } else {
- type = qualifier;
- qualifier = '';
- }
- const name = inputs[ i ++ ];
- this.inputs.push( {
- name: name,
- type: type,
- qualifier: qualifier
- } );
- }
- }
- this.isInterface = this.src.indexOf( '{' ) === - 1;
- } else {
- this.type = '';
- this.name = '';
- }
- }
- }
- copy( source ) {
- super.copy( source );
- this.isMethod = source.isMethod;
- this.useKeywords = source.useKeywords;
- this.parse( source.src, source.includes, source.extensions, source.keywords );
- if ( source.type !== undefined ) this.type = source.type;
- return this;
- }
- toJSON( meta ) {
- let data = this.getJSONNode( meta );
- if ( ! data ) {
- data = this.createJSONNode( meta );
- data.src = this.src;
- data.isMethod = this.isMethod;
- data.useKeywords = this.useKeywords;
- if ( ! this.isMethod ) data.type = this.type;
- data.extensions = JSON.parse( JSON.stringify( this.extensions ) );
- data.keywords = {};
- for ( const keyword in this.keywords ) {
- data.keywords[ keyword ] = this.keywords[ keyword ].toJSON( meta ).uuid;
- }
- if ( this.includes.length ) {
- data.includes = [];
- for ( let i = 0; i < this.includes.length; i ++ ) {
- data.includes.push( this.includes[ i ].toJSON( meta ).uuid );
- }
- }
- }
- return data;
- }
- }
- FunctionNode.prototype.nodeType = 'Function';
- FunctionNode.prototype.useKeywords = true;
- export { FunctionNode };
|