2012-01-09 17:49:16 +00:00
|
|
|
/**
|
|
|
|
* Token transformation managers with a (mostly) abstract
|
|
|
|
* TokenTransformManager base class and AsyncTokenTransformManager and
|
|
|
|
* SyncTokenTransformManager implementation subclasses. Individual
|
|
|
|
* transformations register for the token types they are interested in and are
|
|
|
|
* called on each matching token.
|
|
|
|
*
|
|
|
|
* Async token transformations are supported by the TokenAccumulator class,
|
|
|
|
* that manages as-early-as-possible and in-order return of tokens including
|
|
|
|
* buffering.
|
2011-12-12 10:01:47 +00:00
|
|
|
*
|
2012-01-03 18:44:31 +00:00
|
|
|
* See
|
|
|
|
* https://www.mediawiki.org/wiki/Future/Parser_development/Token_stream_transformations
|
|
|
|
* for more documentation.
|
2011-12-14 09:33:25 +00:00
|
|
|
*
|
2012-01-03 18:44:31 +00:00
|
|
|
* @author Gabriel Wicke <gwicke@wikimedia.org>
|
|
|
|
*/
|
2011-12-08 14:37:31 +00:00
|
|
|
|
2012-01-03 18:44:31 +00:00
|
|
|
var events = require('events');
|
2011-12-28 01:37:06 +00:00
|
|
|
|
2011-12-13 20:13:09 +00:00
|
|
|
/**
|
2012-01-09 17:49:16 +00:00
|
|
|
* Base class for token transform managers
|
2011-12-13 20:13:09 +00:00
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @constructor
|
|
|
|
* @param {Function} callback, a callback function accepting a token list as
|
|
|
|
* its only argument.
|
|
|
|
*/
|
2012-01-09 17:49:16 +00:00
|
|
|
function TokenTransformManager( ) {
|
|
|
|
// Separate the constructor, so that we can call it from subclasses.
|
|
|
|
this._construct();
|
2011-12-08 14:37:31 +00:00
|
|
|
}
|
|
|
|
|
2012-01-03 18:44:31 +00:00
|
|
|
// Inherit from EventEmitter
|
2012-01-09 17:49:16 +00:00
|
|
|
TokenTransformManager.prototype = new events.EventEmitter();
|
|
|
|
TokenTransformManager.prototype.constructor = TokenTransformManager;
|
|
|
|
|
|
|
|
TokenTransformManager.prototype._construct = function () {
|
|
|
|
this.transformers = {
|
|
|
|
tag: {}, // for TAG, ENDTAG, SELFCLOSINGTAG, keyed on name
|
|
|
|
text: [],
|
|
|
|
newline: [],
|
|
|
|
comment: [],
|
|
|
|
end: [], // eof
|
|
|
|
martian: [], // none of the above (unknown token type)
|
|
|
|
any: [] // all tokens, before more specific handlers are run
|
|
|
|
};
|
|
|
|
};
|
2012-01-03 18:44:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Register to a token source, normally the tokenizer.
|
2012-01-04 11:00:54 +00:00
|
|
|
* The event emitter emits a 'chunk' event with a chunk of tokens,
|
2012-01-03 18:44:31 +00:00
|
|
|
* and signals the end of tokens by triggering the 'end' event.
|
2012-01-09 17:49:16 +00:00
|
|
|
* XXX: Perform registration directly in the constructor?
|
2012-01-03 18:44:31 +00:00
|
|
|
*
|
2012-01-09 17:49:16 +00:00
|
|
|
* @method
|
2012-01-03 18:44:31 +00:00
|
|
|
* @param {Object} EventEmitter token even emitter.
|
|
|
|
*/
|
2012-01-09 17:49:16 +00:00
|
|
|
TokenTransformManager.prototype.listenForTokensFrom = function ( tokenEmitter ) {
|
|
|
|
tokenEmitter.addListener('chunk', this.onChunk.bind( this ) );
|
2012-01-03 18:44:31 +00:00
|
|
|
tokenEmitter.addListener('end', this.onEndEvent.bind( this ) );
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-01-09 17:49:16 +00:00
|
|
|
|
2011-12-13 20:13:09 +00:00
|
|
|
/**
|
2012-01-09 17:49:16 +00:00
|
|
|
* Map a rank to a phase.
|
2011-12-13 20:13:09 +00:00
|
|
|
*
|
2012-01-09 17:49:16 +00:00
|
|
|
* XXX: Might not be needed anymore, as phases are now subclassed and
|
|
|
|
* registrations are separated.
|
2011-12-13 20:13:09 +00:00
|
|
|
*/
|
2012-01-09 17:49:16 +00:00
|
|
|
TokenTransformManager.prototype._rankToPhase = function ( rank ) {
|
2012-01-03 18:44:31 +00:00
|
|
|
if ( rank < 0 || rank > 3 ) {
|
2012-01-09 17:49:16 +00:00
|
|
|
throw "TransformManager error: Invalid transformation rank " + rank;
|
2012-01-03 18:44:31 +00:00
|
|
|
}
|
|
|
|
if ( rank <= 2 ) {
|
|
|
|
return 2;
|
|
|
|
} else {
|
|
|
|
return 3;
|
|
|
|
}
|
2011-12-12 14:03:54 +00:00
|
|
|
};
|
|
|
|
|
2011-12-13 20:13:09 +00:00
|
|
|
/**
|
2012-01-03 18:44:31 +00:00
|
|
|
* Add a transform registration.
|
2011-12-13 20:13:09 +00:00
|
|
|
*
|
|
|
|
* @method
|
2012-01-03 18:44:31 +00:00
|
|
|
* @param {Function} transform.
|
2012-01-04 09:48:24 +00:00
|
|
|
* @param {Number} rank, [0,3) with [0,1) in-order on input token stream,
|
|
|
|
* [1,2) out-of-order and [2,3) in-order on output token stream
|
2011-12-13 20:13:09 +00:00
|
|
|
* @param {String} type, one of 'tag', 'text', 'newline', 'comment', 'end',
|
|
|
|
* 'martian' (unknown token), 'any' (any token, matched before other matches).
|
|
|
|
* @param {String} tag name for tags, omitted for non-tags
|
|
|
|
*/
|
2012-01-09 17:49:16 +00:00
|
|
|
TokenTransformManager.prototype.addTransform = function ( transformation, rank, type, name ) {
|
|
|
|
var transArr,
|
2012-01-03 18:44:31 +00:00
|
|
|
transformer = {
|
|
|
|
transform: transformation,
|
|
|
|
rank: rank
|
|
|
|
};
|
2011-12-08 14:37:31 +00:00
|
|
|
if ( type === 'tag' ) {
|
2011-12-13 14:48:47 +00:00
|
|
|
name = name.toLowerCase();
|
2012-01-09 17:49:16 +00:00
|
|
|
transArr = this.transformers.tag[name];
|
2012-01-03 18:44:31 +00:00
|
|
|
if ( ! transArr ) {
|
2012-01-09 17:49:16 +00:00
|
|
|
transArr = this.transformers.tag[name] = [];
|
2011-12-08 14:37:31 +00:00
|
|
|
}
|
|
|
|
} else {
|
2012-01-09 17:49:16 +00:00
|
|
|
transArr = this.transformers[type];
|
2011-12-08 14:37:31 +00:00
|
|
|
}
|
2012-01-03 18:44:31 +00:00
|
|
|
transArr.push(transformer);
|
|
|
|
// sort ascending by rank
|
2012-01-04 14:09:05 +00:00
|
|
|
transArr.sort( this._cmpTransformations );
|
2011-12-08 14:37:31 +00:00
|
|
|
};
|
|
|
|
|
2011-12-13 20:13:09 +00:00
|
|
|
/**
|
2012-01-03 18:44:31 +00:00
|
|
|
* Remove a transform registration
|
2011-12-13 20:13:09 +00:00
|
|
|
*
|
|
|
|
* @method
|
2012-01-04 09:48:24 +00:00
|
|
|
* @param {Function} transform.
|
|
|
|
* @param {Number} rank, [0,3) with [0,1) in-order on input token stream,
|
|
|
|
* [1,2) out-of-order and [2,3) in-order on output token stream
|
2011-12-13 20:13:09 +00:00
|
|
|
* @param {String} type, one of 'tag', 'text', 'newline', 'comment', 'end',
|
|
|
|
* 'martian' (unknown token), 'any' (any token, matched before other matches).
|
|
|
|
* @param {String} tag name for tags, omitted for non-tags
|
|
|
|
*/
|
2012-01-09 17:49:16 +00:00
|
|
|
TokenTransformManager.prototype.removeTransform = function ( rank, type, name ) {
|
2012-01-03 18:44:31 +00:00
|
|
|
var i = -1,
|
|
|
|
ts;
|
|
|
|
|
|
|
|
function rankUnEqual ( i ) {
|
|
|
|
return i.rank !== rank;
|
|
|
|
}
|
|
|
|
|
2011-12-08 14:37:31 +00:00
|
|
|
if ( type === 'tag' ) {
|
2011-12-13 14:48:47 +00:00
|
|
|
name = name.toLowerCase();
|
2012-01-09 17:49:16 +00:00
|
|
|
var maybeTransArr = this.transformers.tag.name;
|
2012-01-03 18:44:31 +00:00
|
|
|
if ( maybeTransArr ) {
|
2012-01-09 17:49:16 +00:00
|
|
|
this.transformers.tag.name = maybeTransArr.filter( rankUnEqual );
|
2011-12-08 14:37:31 +00:00
|
|
|
}
|
|
|
|
} else {
|
2012-01-09 17:49:16 +00:00
|
|
|
this.transformers[type] = this.transformers[type].filter( rankUnEqual ) ;
|
2011-12-08 14:37:31 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-12-13 20:13:09 +00:00
|
|
|
/**
|
2012-01-03 18:44:31 +00:00
|
|
|
* Enforce separation between phases when token types or tag names have
|
|
|
|
* changed, or when multiple tokens were returned. Processing will restart
|
|
|
|
* with the new rank.
|
2012-01-09 17:49:16 +00:00
|
|
|
*
|
|
|
|
* XXX: This should also be moved to the subclass (actually partially implicit if
|
|
|
|
* _transformTagToken and _transformToken are subclassed and set the rank when
|
|
|
|
* fully processed). The token type change case still needs to be covered
|
|
|
|
* though.
|
2011-12-13 20:13:09 +00:00
|
|
|
*/
|
2012-01-09 17:49:16 +00:00
|
|
|
TokenTransformManager.prototype._resetTokenRank = function ( res, transformer ) {
|
2012-01-03 18:44:31 +00:00
|
|
|
if ( res.token ) {
|
|
|
|
// reset rank after type or name change
|
|
|
|
if ( transformer.rank < 1 ) {
|
|
|
|
res.token.rank = 0;
|
|
|
|
} else {
|
|
|
|
res.token.rank = 1;
|
|
|
|
}
|
|
|
|
} else if ( res.tokens && transformer.rank > 2 ) {
|
|
|
|
for ( var i = 0; i < res.tokens.length; i++ ) {
|
|
|
|
if ( res.tokens[i].rank === undefined ) {
|
|
|
|
// Do not run phase 0 on newly created tokens from
|
|
|
|
// phase 1.
|
|
|
|
res.tokens[i].rank = 2;
|
|
|
|
}
|
2011-12-08 14:37:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-01-04 14:09:05 +00:00
|
|
|
/**
|
|
|
|
* Comparison for sorting transformations by ascending rank.
|
|
|
|
*/
|
2012-01-09 17:49:16 +00:00
|
|
|
TokenTransformManager.prototype._cmpTransformations = function ( a, b ) {
|
2012-01-04 14:09:05 +00:00
|
|
|
return a.rank - b.rank;
|
|
|
|
};
|
2011-12-12 10:01:47 +00:00
|
|
|
|
|
|
|
/* Call all transformers on a tag.
|
2012-01-09 17:49:16 +00:00
|
|
|
* XXX: Move to subclasses and use a different signature?
|
2011-12-12 10:01:47 +00:00
|
|
|
*
|
2012-01-04 12:28:41 +00:00
|
|
|
* @method
|
2012-01-03 18:44:31 +00:00
|
|
|
* @param {Object} The current token.
|
|
|
|
* @param {Function} Completion callback for async processing.
|
|
|
|
* @param {Number} Rank of phase end, both key for transforms and rank for
|
|
|
|
* processed tokens.
|
|
|
|
* @returns {Object} Token(s) and async indication.
|
2011-12-12 10:01:47 +00:00
|
|
|
*/
|
2012-01-09 17:49:16 +00:00
|
|
|
TokenTransformManager.prototype._transformTagToken = function ( token, cb, phaseEndRank ) {
|
2011-12-13 14:48:47 +00:00
|
|
|
// prepend 'any' transformers
|
2012-01-09 17:49:16 +00:00
|
|
|
var ts = this.transformers.any,
|
2012-01-03 18:44:31 +00:00
|
|
|
res = { token: token },
|
|
|
|
transform,
|
|
|
|
l, i,
|
|
|
|
aborted = false,
|
|
|
|
tName = token.name.toLowerCase(),
|
2012-01-09 17:49:16 +00:00
|
|
|
tagts = this.transformers.tag[tName];
|
2012-01-03 18:44:31 +00:00
|
|
|
|
2012-01-04 14:09:05 +00:00
|
|
|
if ( tagts && tagts.length ) {
|
|
|
|
// could cache this per tag type to avoid re-sorting each time
|
2011-12-13 14:48:47 +00:00
|
|
|
ts = ts.concat(tagts);
|
2012-01-04 14:09:05 +00:00
|
|
|
ts.sort( this._cmpTransformations );
|
2011-12-13 14:48:47 +00:00
|
|
|
}
|
|
|
|
//console.log(JSON.stringify(ts, null, 2));
|
2011-12-08 14:37:31 +00:00
|
|
|
if ( ts ) {
|
2012-01-03 18:44:31 +00:00
|
|
|
for ( i = 0, l = ts.length; i < l; i++ ) {
|
|
|
|
transformer = ts[i];
|
2012-01-11 19:48:49 +00:00
|
|
|
if ( res.token.rank && transformer.rank < res.token.rank ) {
|
2012-01-13 18:48:25 +00:00
|
|
|
//console.log( 'SKIPPING' + JSON.stringify( token, null, 2 ) +
|
|
|
|
// '\ntransform:\n' + JSON.stringify( transformer, null, 2 ) );
|
2012-01-03 18:44:31 +00:00
|
|
|
// skip transformation, was already applied.
|
|
|
|
continue;
|
|
|
|
}
|
2011-12-12 10:01:47 +00:00
|
|
|
// Transform token with side effects
|
2012-01-09 17:49:16 +00:00
|
|
|
res = transformer.transform( res.token, cb, this, this.prevToken );
|
2012-01-03 18:44:31 +00:00
|
|
|
// if multiple tokens or null token: process returned tokens (in parent)
|
|
|
|
if ( !res.token || // async implies tokens instead of token, so no
|
|
|
|
// need to check explicitly
|
|
|
|
res.token.type !== token.type ||
|
|
|
|
res.token.name !== token.name ) {
|
|
|
|
this._resetTokenRank ( res, transformer );
|
|
|
|
aborted = true;
|
2011-12-08 14:37:31 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-01-03 18:44:31 +00:00
|
|
|
// track progress on token
|
|
|
|
res.token.rank = transformer.rank;
|
|
|
|
}
|
|
|
|
if ( ! aborted ) {
|
|
|
|
// Mark token as fully processed.
|
|
|
|
res.token.rank = phaseEndRank;
|
2011-12-08 14:37:31 +00:00
|
|
|
}
|
|
|
|
}
|
2012-01-03 18:44:31 +00:00
|
|
|
return res;
|
2011-12-08 14:37:31 +00:00
|
|
|
};
|
|
|
|
|
2012-01-09 17:49:16 +00:00
|
|
|
|
2011-12-22 11:43:55 +00:00
|
|
|
/* Call all transformers on non-tag token types.
|
2012-01-09 17:49:16 +00:00
|
|
|
* XXX: different signature for sync vs. async, move to subclass?
|
2011-12-12 10:01:47 +00:00
|
|
|
*
|
2012-01-04 12:28:41 +00:00
|
|
|
* @method
|
2012-01-03 18:44:31 +00:00
|
|
|
* @param {Object} The current token.
|
|
|
|
* @param {Function} Completion callback for async processing.
|
|
|
|
* @param {Number} Rank of phase end, both key for transforms and rank for
|
|
|
|
* processed tokens.
|
|
|
|
* @param {Array} ts List of token transformers for this token type.
|
|
|
|
* @returns {Object} Token(s) and async indication.
|
2011-12-12 10:01:47 +00:00
|
|
|
*/
|
2012-01-09 17:49:16 +00:00
|
|
|
TokenTransformManager.prototype._transformToken = function ( token, cb, phaseEndRank, ts ) {
|
2011-12-13 14:48:47 +00:00
|
|
|
// prepend 'any' transformers
|
2012-01-09 17:49:16 +00:00
|
|
|
var anyTrans = this.transformers.any;
|
2012-01-04 14:09:05 +00:00
|
|
|
if ( anyTrans.length ) {
|
2012-01-09 17:49:16 +00:00
|
|
|
ts = this.transformers.any.concat(ts);
|
2012-01-04 14:09:05 +00:00
|
|
|
ts.sort( this._cmpTransformations );
|
|
|
|
}
|
2012-01-03 18:44:31 +00:00
|
|
|
var transformer,
|
|
|
|
res = { token: token },
|
|
|
|
aborted = false;
|
2011-12-08 14:37:31 +00:00
|
|
|
if ( ts ) {
|
|
|
|
for (var i = 0, l = ts.length; i < l; i++ ) {
|
2012-01-03 18:44:31 +00:00
|
|
|
transformer = ts[i];
|
|
|
|
if ( res.token.rank && transformer.rank <= res.token.rank ) {
|
|
|
|
// skip transformation, was already applied.
|
|
|
|
continue;
|
|
|
|
}
|
2012-01-04 09:48:24 +00:00
|
|
|
// Transform the token.
|
|
|
|
// XXX: consider moving the rank out of the token itself to avoid
|
|
|
|
// transformations messing with it in broken ways. Not sure if
|
|
|
|
// some transformations need to manipulate it though. gwicke
|
2012-01-09 17:49:16 +00:00
|
|
|
res = transformer.transform( res.token, cb, this, this.prevToken );
|
2012-01-03 18:44:31 +00:00
|
|
|
if ( !res.token ||
|
|
|
|
res.token.type !== token.type ) {
|
|
|
|
this._resetTokenRank ( res, transformer );
|
|
|
|
aborted = true;
|
2011-12-08 14:37:31 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-01-03 18:44:31 +00:00
|
|
|
res.token.rank = transformer.rank;
|
|
|
|
}
|
|
|
|
if ( ! aborted ) {
|
|
|
|
// mark token as completely processed
|
|
|
|
res.token.rank = phaseEndRank; // need phase passed in!
|
2011-12-08 14:37:31 +00:00
|
|
|
}
|
2012-01-03 18:44:31 +00:00
|
|
|
|
2011-12-08 14:37:31 +00:00
|
|
|
}
|
2012-01-03 18:44:31 +00:00
|
|
|
return res;
|
2011-12-08 14:37:31 +00:00
|
|
|
};
|
|
|
|
|
2012-01-09 17:49:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
/******************** Async token transforms: Phase 2 **********************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Asynchronous and potentially out-of-order token transformations, used in phase 2.
|
|
|
|
*
|
|
|
|
* return protocol for individual transforms:
|
|
|
|
* { tokens: [tokens], async: true }: async expansion -> outstanding++ in parent
|
|
|
|
* { tokens: [tokens] }: fully expanded, tokens will be reprocessed
|
|
|
|
* { token: token }: single-token return
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @constructor
|
|
|
|
* @param {Function} childFactory: A function that can be used to create a
|
|
|
|
* new, nested transform manager:
|
|
|
|
* nestedAsyncTokenTransformManager = manager.newChildPipeline( inputType, args );
|
|
|
|
* @param {Object} args, the argument map for templates
|
|
|
|
* @param {Object} env, the environment.
|
|
|
|
*/
|
2012-01-10 01:09:50 +00:00
|
|
|
function AsyncTokenTransformManager ( childFactories, args, env ) {
|
2012-01-09 17:49:16 +00:00
|
|
|
// Factory function for new AsyncTokenTransformManager creation with
|
|
|
|
// default transforms enabled
|
|
|
|
// Also sets up a tokenizer and phase-1-transform depending on the input format
|
|
|
|
// nestedAsyncTokenTransformManager = manager.newChildPipeline( inputType, args );
|
2012-01-10 01:09:50 +00:00
|
|
|
this.childFactories = childFactories;
|
2012-01-09 17:49:16 +00:00
|
|
|
this._construct();
|
|
|
|
this._reset( args, env );
|
2012-01-11 19:48:49 +00:00
|
|
|
// FIXME: pass actual title?
|
2012-01-14 00:58:20 +00:00
|
|
|
this.loopCheck = new LoopCheck( null );
|
2012-01-09 17:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Inherit from TokenTransformManager, and thus also from EventEmitter.
|
|
|
|
AsyncTokenTransformManager.prototype = new TokenTransformManager();
|
|
|
|
AsyncTokenTransformManager.prototype.constructor = AsyncTokenTransformManager;
|
|
|
|
|
2011-12-13 20:13:09 +00:00
|
|
|
/**
|
2012-01-09 17:49:16 +00:00
|
|
|
* Create a new child pipeline.
|
2011-12-12 10:01:47 +00:00
|
|
|
*
|
2012-01-09 17:49:16 +00:00
|
|
|
* @method
|
|
|
|
* @param {String} Input type, currently only support 'text/wiki'.
|
|
|
|
* @param {Object} Template arguments
|
|
|
|
* @returns {Object} Pipeline, which is an object with 'first' pointing to the
|
|
|
|
* first stage of the pipeline, and 'last' pointing to the last stage.
|
2011-12-13 20:13:09 +00:00
|
|
|
*/
|
2012-01-10 01:09:50 +00:00
|
|
|
AsyncTokenTransformManager.prototype.newChildPipeline = function ( inputType, args, title ) {
|
2012-01-17 22:29:26 +00:00
|
|
|
//console.log( 'newChildPipeline: ' + JSON.stringify( args ) );
|
2012-01-10 01:09:50 +00:00
|
|
|
var pipe = this.childFactories.input( inputType, args );
|
|
|
|
|
|
|
|
// now set up a few things on the child AsyncTokenTransformManager.
|
|
|
|
var child = pipe.last;
|
|
|
|
// We assume that the title was already checked against this.loopCheck
|
|
|
|
// before!
|
2012-01-14 00:58:20 +00:00
|
|
|
child.loopCheck = new LoopCheck (
|
|
|
|
this.env.normalizeTitle( this.env.tokensToString ( title ) ),
|
|
|
|
this.loopCheck
|
|
|
|
);
|
2012-01-10 01:09:50 +00:00
|
|
|
// Same for depth!
|
|
|
|
child.depth = this.depth + 1;
|
2012-01-09 17:49:16 +00:00
|
|
|
return pipe;
|
2012-01-03 18:44:31 +00:00
|
|
|
};
|
|
|
|
|
2012-01-10 01:09:50 +00:00
|
|
|
/**
|
|
|
|
* Create a pipeline for attribute transformations.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {String} Input type, currently only support 'text/wiki'.
|
|
|
|
* @param {Object} Template arguments
|
|
|
|
* @returns {Object} Pipeline, which is an object with 'first' pointing to the
|
|
|
|
* first stage of the pipeline, and 'last' pointing to the last stage.
|
|
|
|
*/
|
2012-01-11 00:05:51 +00:00
|
|
|
AsyncTokenTransformManager.prototype.getAttributePipeline = function ( inputType, args ) {
|
2012-01-10 01:09:50 +00:00
|
|
|
return this.childFactories.attributes( inputType, args );
|
|
|
|
};
|
|
|
|
|
2012-01-03 18:44:31 +00:00
|
|
|
/**
|
2012-01-09 17:49:16 +00:00
|
|
|
* Reset the internal token and outstanding-callback state of the
|
|
|
|
* TokenTransformManager, but keep registrations untouched.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {Object} args, template arguments
|
|
|
|
* @param {Object} The environment.
|
2012-01-03 18:44:31 +00:00
|
|
|
*/
|
2012-01-09 17:49:16 +00:00
|
|
|
AsyncTokenTransformManager.prototype._reset = function ( args, env ) {
|
|
|
|
// Note: Much of this is frame-like.
|
|
|
|
this.tailAccumulator = undefined;
|
|
|
|
// eventize: bend to event emitter callback
|
|
|
|
this.tokenCB = this._returnTokens.bind( this );
|
|
|
|
this.accum = new TokenAccumulator(null);
|
|
|
|
this.firstaccum = this.accum;
|
|
|
|
this.prevToken = undefined;
|
2012-01-17 22:29:26 +00:00
|
|
|
//console.log( 'AsyncTokenTransformManager args ' + JSON.stringify( args ) );
|
2012-01-09 17:49:16 +00:00
|
|
|
if ( ! args ) {
|
|
|
|
this.args = {}; // no arguments at the top level
|
2012-01-03 18:44:31 +00:00
|
|
|
} else {
|
2012-01-09 17:49:16 +00:00
|
|
|
this.args = args;
|
|
|
|
}
|
|
|
|
if ( ! env ) {
|
|
|
|
if ( !this.env ) {
|
|
|
|
throw "AsyncTokenTransformManager: environment needed!" + env;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.env = env;
|
2011-12-12 10:01:47 +00:00
|
|
|
}
|
2012-01-03 18:44:31 +00:00
|
|
|
};
|
2011-12-12 20:53:14 +00:00
|
|
|
|
2012-01-11 19:48:49 +00:00
|
|
|
/**
|
|
|
|
* Simplified wrapper that processes all tokens passed in
|
|
|
|
*/
|
|
|
|
AsyncTokenTransformManager.prototype.process = function ( tokens ) {
|
|
|
|
if ( ! $.isArray ( tokens ) ) {
|
|
|
|
tokens = [tokens];
|
|
|
|
}
|
|
|
|
this.onChunk( tokens );
|
|
|
|
this.onEndEvent();
|
|
|
|
};
|
2012-01-04 09:48:24 +00:00
|
|
|
|
2012-01-09 17:49:16 +00:00
|
|
|
/**
|
|
|
|
* Transform and expand tokens. Transformed token chunks will be emitted in
|
|
|
|
* the 'chunk' event.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {Array} chunk of tokens
|
|
|
|
*/
|
|
|
|
AsyncTokenTransformManager.prototype.onChunk = function ( tokens ) {
|
|
|
|
// Set top-level callback to next transform phase
|
|
|
|
var res = this.transformTokens ( tokens, this.tokenCB );
|
|
|
|
this.tailAccumulator = res.async;
|
2012-01-13 18:48:25 +00:00
|
|
|
//console.log('AsyncTokenTransformManager onChunk ', tokens);
|
2012-01-09 17:49:16 +00:00
|
|
|
//this.phase2TailCB( tokens, true );
|
|
|
|
if ( res.async ) {
|
|
|
|
this.tokenCB = res.async.getParentCB ( 'sibling' );
|
|
|
|
}
|
2012-01-13 18:48:25 +00:00
|
|
|
this.emit( 'chunk', res.tokens );
|
2012-01-09 17:49:16 +00:00
|
|
|
};
|
|
|
|
|
2012-01-03 18:44:31 +00:00
|
|
|
/**
|
2012-01-04 09:48:24 +00:00
|
|
|
* Run transformations from phases 0 and 1. This includes starting and
|
|
|
|
* managing asynchronous transformations.
|
|
|
|
*
|
2012-01-03 18:44:31 +00:00
|
|
|
*/
|
2012-01-09 17:49:16 +00:00
|
|
|
AsyncTokenTransformManager.prototype.transformTokens = function ( tokens, parentCB ) {
|
2011-12-12 20:53:14 +00:00
|
|
|
|
2012-01-13 18:48:25 +00:00
|
|
|
//console.log('AsyncTokenTransformManager.transformTokens: ' + JSON.stringify(tokens) );
|
2012-01-03 18:44:31 +00:00
|
|
|
|
|
|
|
var res,
|
2012-01-13 18:48:25 +00:00
|
|
|
phaseEndRank = 2, // XXX: parametrize!
|
2012-01-03 18:44:31 +00:00
|
|
|
// Prepare a new accumulator, to be used by async children (if any)
|
|
|
|
localAccum = [],
|
|
|
|
accum = new TokenAccumulator( parentCB ),
|
|
|
|
cb = accum.getParentCB( 'child' ),
|
|
|
|
activeAccum = null,
|
|
|
|
tokensLength = tokens.length,
|
|
|
|
token,
|
2012-01-09 17:49:16 +00:00
|
|
|
ts = this.transformers;
|
2012-01-03 18:44:31 +00:00
|
|
|
|
2012-01-11 19:48:49 +00:00
|
|
|
|
2012-01-03 18:44:31 +00:00
|
|
|
for ( var i = 0; i < tokensLength; i++ ) {
|
|
|
|
token = tokens[i];
|
|
|
|
|
|
|
|
switch( token.type ) {
|
2011-12-08 14:37:31 +00:00
|
|
|
case 'TAG':
|
|
|
|
case 'ENDTAG':
|
|
|
|
case 'SELFCLOSINGTAG':
|
2012-01-09 17:49:16 +00:00
|
|
|
res = this._transformTagToken( token, cb, phaseEndRank );
|
2011-12-08 14:37:31 +00:00
|
|
|
break;
|
|
|
|
case 'TEXT':
|
2012-01-09 17:49:16 +00:00
|
|
|
res = this._transformToken( token, cb, phaseEndRank, ts.text );
|
2011-12-08 14:37:31 +00:00
|
|
|
break;
|
|
|
|
case 'COMMENT':
|
2012-01-09 17:49:16 +00:00
|
|
|
res = this._transformToken( token, cb, phaseEndRank, ts.comment);
|
2011-12-08 14:37:31 +00:00
|
|
|
break;
|
|
|
|
case 'NEWLINE':
|
2012-01-09 17:49:16 +00:00
|
|
|
res = this._transformToken( token, cb, phaseEndRank, ts.newline );
|
2011-12-08 14:37:31 +00:00
|
|
|
break;
|
|
|
|
case 'END':
|
2012-01-09 17:49:16 +00:00
|
|
|
res = this._transformToken( token, cb, phaseEndRank, ts.end );
|
2011-12-08 14:37:31 +00:00
|
|
|
break;
|
|
|
|
default:
|
2012-01-09 17:49:16 +00:00
|
|
|
res = this._transformToken( token, cb, phaseEndRank, ts.martian );
|
2011-12-08 14:37:31 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-01-03 18:44:31 +00:00
|
|
|
|
|
|
|
if( res.tokens ) {
|
2011-12-12 10:01:47 +00:00
|
|
|
// Splice in the returned tokens (while replacing the original
|
|
|
|
// token), and process them next.
|
2012-01-03 18:44:31 +00:00
|
|
|
[].splice.apply( tokens, [i, 1].concat(res.tokens) );
|
|
|
|
tokensLength = tokens.length;
|
2011-12-12 10:01:47 +00:00
|
|
|
i--; // continue at first inserted token
|
2012-01-03 18:44:31 +00:00
|
|
|
} else if ( res.token ) {
|
|
|
|
if ( res.token.rank === 2 ) {
|
|
|
|
// token is done.
|
|
|
|
if ( activeAccum ) {
|
|
|
|
// push to accumulator
|
|
|
|
activeAccum.push( res.token );
|
|
|
|
} else {
|
|
|
|
// If there is no accumulator yet, then directly return the
|
|
|
|
// token to the parent. Collect them in localAccum for this
|
|
|
|
// purpose.
|
2012-01-13 18:48:25 +00:00
|
|
|
localAccum.push( res.token );
|
2012-01-03 18:44:31 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// re-process token.
|
|
|
|
tokens[i] = res.token;
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
} else if ( res.async ) {
|
2012-01-13 18:48:25 +00:00
|
|
|
//console.log( 'tokens returned' );
|
2012-01-03 18:44:31 +00:00
|
|
|
// The child now switched to activeAccum, we have to create a new
|
|
|
|
// accumulator for the next potential child.
|
|
|
|
activeAccum = accum;
|
|
|
|
accum = new TokenAccumulator( activeAccum.getParentCB( 'sibling' ) );
|
|
|
|
cb = accum.getParentCB( 'child' );
|
2011-12-08 14:37:31 +00:00
|
|
|
}
|
2011-12-12 20:53:14 +00:00
|
|
|
}
|
|
|
|
|
2012-01-03 18:44:31 +00:00
|
|
|
// Return finished tokens directly to caller, and indicate if further
|
|
|
|
// async actions are outstanding. The caller needs to point a sibling to
|
|
|
|
// the returned accumulator, or call .siblingDone() to mark the end of a
|
|
|
|
// chain.
|
|
|
|
return { tokens: localAccum, async: activeAccum };
|
2011-12-08 14:37:31 +00:00
|
|
|
};
|
|
|
|
|
2011-12-13 20:13:09 +00:00
|
|
|
/**
|
2012-01-03 18:44:31 +00:00
|
|
|
* Callback from tokens fully processed for phase 0 and 1, which are now ready
|
|
|
|
* for synchronous and globally in-order phase 2 processing.
|
2012-01-09 17:49:16 +00:00
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {Array} chunk of tokens
|
|
|
|
* @param {Mixed} Either a falsy value if this is the last callback
|
|
|
|
* (everything is done), or a truish value if not yet done.
|
2011-12-13 20:13:09 +00:00
|
|
|
*/
|
2012-01-09 17:49:16 +00:00
|
|
|
AsyncTokenTransformManager.prototype._returnTokens = function ( tokens, notYetDone ) {
|
|
|
|
//tokens = this._transformPhase2( this.frame, tokens, this.parentCB );
|
|
|
|
//console.log('AsyncTokenTransformManager._returnTokens, after _transformPhase2.');
|
2012-01-03 18:44:31 +00:00
|
|
|
|
2012-01-13 18:48:25 +00:00
|
|
|
this.emit( 'chunk', tokens );
|
2012-01-03 18:44:31 +00:00
|
|
|
|
|
|
|
if ( ! notYetDone ) {
|
2012-01-13 18:48:25 +00:00
|
|
|
//console.log('AsyncTokenTransformManager._returnTokens done. tokens:' +
|
|
|
|
// JSON.stringify( tokens, null, 2 ) + ', listeners: ' +
|
|
|
|
// JSON.stringify( this.listeners( 'chunk' ), null, 2 ) );
|
2012-01-03 18:44:31 +00:00
|
|
|
// signal our done-ness to consumers.
|
|
|
|
this.emit( 'end' );
|
|
|
|
// and reset internal state.
|
2012-01-04 09:48:24 +00:00
|
|
|
this._reset();
|
2011-12-08 14:37:31 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-01-09 17:49:16 +00:00
|
|
|
/**
|
|
|
|
* Callback for the end event emitted from the tokenizer.
|
|
|
|
* Either signals the end of input to the tail of an ongoing asynchronous
|
|
|
|
* processing pipeline, or directly emits 'end' if the processing was fully
|
|
|
|
* synchronous.
|
|
|
|
*/
|
|
|
|
AsyncTokenTransformManager.prototype.onEndEvent = function () {
|
|
|
|
if ( this.tailAccumulator ) {
|
|
|
|
this.tailAccumulator.siblingDone();
|
|
|
|
} else {
|
|
|
|
// nothing was asynchronous, so we'll have to emit end here.
|
|
|
|
this.emit('end');
|
|
|
|
this._reset();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*************** In-order, synchronous transformer (phase 1 and 3) ***************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Subclass for phase 3, in-order and synchronous processing.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @constructor
|
|
|
|
* @param {Object} environment.
|
|
|
|
*/
|
2012-01-13 18:48:25 +00:00
|
|
|
function SyncTokenTransformManager ( env, phaseEndRank ) {
|
2012-01-09 17:49:16 +00:00
|
|
|
// both inherited
|
|
|
|
this._construct();
|
2012-01-13 18:48:25 +00:00
|
|
|
this.phaseEndRank = phaseEndRank;
|
2012-01-09 17:49:16 +00:00
|
|
|
this.args = {}; // no arguments at the top level
|
|
|
|
this.env = env;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inherit from TokenTransformManager, and thus also from EventEmitter.
|
|
|
|
SyncTokenTransformManager.prototype = new TokenTransformManager();
|
|
|
|
SyncTokenTransformManager.prototype.constructor = SyncTokenTransformManager;
|
|
|
|
|
2012-01-03 18:44:31 +00:00
|
|
|
|
2012-01-11 19:48:49 +00:00
|
|
|
SyncTokenTransformManager.prototype.process = function ( tokens ) {
|
|
|
|
if ( ! $.isArray ( tokens ) ) {
|
|
|
|
tokens = [tokens];
|
|
|
|
}
|
|
|
|
this.onChunk( tokens );
|
|
|
|
this.onEndEvent();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-12-13 20:13:09 +00:00
|
|
|
/**
|
2012-01-09 17:49:16 +00:00
|
|
|
* Global in-order and synchronous traversal on token stream. Emits
|
|
|
|
* transformed chunks of tokens in the 'chunk' event.
|
2011-12-13 20:13:09 +00:00
|
|
|
*
|
2012-01-09 17:49:16 +00:00
|
|
|
* @method
|
|
|
|
* @param {Array} Token chunk.
|
2012-01-03 18:44:31 +00:00
|
|
|
*/
|
2012-01-09 17:49:16 +00:00
|
|
|
SyncTokenTransformManager.prototype.onChunk = function ( tokens ) {
|
2012-01-13 18:48:25 +00:00
|
|
|
//console.log('SyncTokenTransformManager.onChunk: ' +
|
|
|
|
// JSON.stringify( tokens, null, 2 ) );
|
2012-01-03 18:44:31 +00:00
|
|
|
var res,
|
|
|
|
localAccum = [],
|
|
|
|
localAccumLength = 0,
|
|
|
|
tokensLength = tokens.length,
|
2012-01-10 01:09:50 +00:00
|
|
|
cb, // XXX: not meaningful for purely synchronous processing!
|
2012-01-03 18:44:31 +00:00
|
|
|
token,
|
2012-01-09 17:49:16 +00:00
|
|
|
// Top-level frame only in phase 3, as everything is already expanded.
|
|
|
|
ts = this.transformers;
|
2012-01-03 18:44:31 +00:00
|
|
|
|
|
|
|
for ( var i = 0; i < tokensLength; i++ ) {
|
|
|
|
token = tokens[i];
|
|
|
|
|
|
|
|
switch( token.type ) {
|
|
|
|
case 'TAG':
|
|
|
|
case 'ENDTAG':
|
|
|
|
case 'SELFCLOSINGTAG':
|
2012-01-13 18:48:25 +00:00
|
|
|
res = this._transformTagToken( token, cb, this.phaseEndRank );
|
2012-01-03 18:44:31 +00:00
|
|
|
break;
|
|
|
|
case 'TEXT':
|
2012-01-13 18:48:25 +00:00
|
|
|
res = this._transformToken( token, cb, this.phaseEndRank,
|
2012-01-03 18:44:31 +00:00
|
|
|
ts.text );
|
|
|
|
break;
|
|
|
|
case 'COMMENT':
|
2012-01-13 18:48:25 +00:00
|
|
|
res = this._transformToken( token, cb, this.phaseEndRank, ts.comment );
|
2012-01-03 18:44:31 +00:00
|
|
|
break;
|
|
|
|
case 'NEWLINE':
|
2012-01-13 18:48:25 +00:00
|
|
|
res = this._transformToken( token, cb, this.phaseEndRank, ts.newline );
|
2012-01-03 18:44:31 +00:00
|
|
|
break;
|
|
|
|
case 'END':
|
2012-01-13 18:48:25 +00:00
|
|
|
res = this._transformToken( token, cb, this.phaseEndRank, ts.end );
|
2012-01-03 18:44:31 +00:00
|
|
|
break;
|
|
|
|
default:
|
2012-01-13 18:48:25 +00:00
|
|
|
res = this._transformToken( token, cb, this.phaseEndRank, ts.martian );
|
2012-01-03 18:44:31 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( res.tokens ) {
|
|
|
|
// Splice in the returned tokens (while replacing the original
|
|
|
|
// token), and process them next.
|
|
|
|
[].splice.apply( tokens, [i, 1].concat(res.tokens) );
|
|
|
|
tokensLength = tokens.length;
|
|
|
|
i--; // continue at first inserted token
|
|
|
|
} else if ( res.token ) {
|
2012-01-13 18:48:25 +00:00
|
|
|
if ( res.token.rank === this.phaseEndRank ) {
|
2012-01-03 18:44:31 +00:00
|
|
|
// token is done.
|
|
|
|
localAccum.push(res.token);
|
|
|
|
this.prevToken = res.token;
|
|
|
|
} else {
|
|
|
|
// re-process token.
|
|
|
|
tokens[i] = res.token;
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
}
|
2011-12-12 20:53:14 +00:00
|
|
|
}
|
2012-01-09 17:49:16 +00:00
|
|
|
this.emit( 'chunk', localAccum );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback for the end event emitted from the tokenizer.
|
|
|
|
* Either signals the end of input to the tail of an ongoing asynchronous
|
|
|
|
* processing pipeline, or directly emits 'end' if the processing was fully
|
|
|
|
* synchronous.
|
|
|
|
*/
|
|
|
|
SyncTokenTransformManager.prototype.onEndEvent = function () {
|
|
|
|
// This phase is fully synchronous, so just pass the end along and prepare
|
|
|
|
// for the next round.
|
|
|
|
this.emit('end');
|
2011-12-08 14:37:31 +00:00
|
|
|
};
|
|
|
|
|
2012-01-03 18:44:31 +00:00
|
|
|
|
2012-01-10 01:09:50 +00:00
|
|
|
/********************** AttributeTransformManager *************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Utility transformation manager for attributes, using an attribute
|
|
|
|
* transformation pipeline (normally phase1 SyncTokenTransformManager and
|
|
|
|
* phase2 AsyncTokenTransformManager). This pipeline needs to be independent
|
|
|
|
* of the containing TokenTransformManager to isolate transforms from each
|
2012-01-11 00:05:51 +00:00
|
|
|
* other. The AttributeTransformManager returns its result by calling the
|
|
|
|
* supplied callback.
|
2012-01-10 01:09:50 +00:00
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @constructor
|
|
|
|
* @param {Object} Containing TokenTransformManager
|
|
|
|
*/
|
|
|
|
function AttributeTransformManager ( manager, callback ) {
|
2012-01-11 19:48:49 +00:00
|
|
|
this.manager = manager;
|
2012-01-10 01:09:50 +00:00
|
|
|
this.callback = callback;
|
2012-01-11 00:05:51 +00:00
|
|
|
this.outstanding = 0;
|
|
|
|
this.kvs = [];
|
2012-01-11 19:48:49 +00:00
|
|
|
this.pipe = manager.getAttributePipeline( manager.args );
|
2012-01-10 01:09:50 +00:00
|
|
|
}
|
|
|
|
|
2012-01-11 19:48:49 +00:00
|
|
|
AttributeTransformManager.prototype.process = function ( attributes ) {
|
2012-01-11 00:05:51 +00:00
|
|
|
// Potentially need to use multiple pipelines to support concurrent async expansion
|
|
|
|
//this.pipe.process(
|
|
|
|
var pipe,
|
|
|
|
ref;
|
2012-01-13 18:48:25 +00:00
|
|
|
//console.log( 'AttributeTransformManager.process: ' + JSON.stringify( attributes ) );
|
2012-01-11 00:05:51 +00:00
|
|
|
|
|
|
|
// transform each argument (key and value), and handle asynchronous returns
|
|
|
|
for ( var i = 0, l = attributes.length; i < l; i++ ) {
|
|
|
|
kv = { key: [], value: [] };
|
2012-01-11 19:48:49 +00:00
|
|
|
this.kvs.push( kv );
|
2012-01-11 00:05:51 +00:00
|
|
|
|
|
|
|
// Assume that the return is async, will be decremented in callback
|
|
|
|
this.outstanding += 2;
|
|
|
|
|
|
|
|
// transform the key
|
2012-01-11 19:48:49 +00:00
|
|
|
pipe = this.manager.getAttributePipeline( this.manager.args );
|
|
|
|
pipe.addListener( 'chunk',
|
|
|
|
this.onChunk.bind( this, this._returnAttributeKey.bind( this, i ) )
|
2012-01-11 00:05:51 +00:00
|
|
|
);
|
2012-01-11 19:48:49 +00:00
|
|
|
pipe.addListener( 'end',
|
|
|
|
this.onEnd.bind( this, this._returnAttributeKey.bind( this, i ) )
|
2012-01-11 00:05:51 +00:00
|
|
|
);
|
|
|
|
pipe.process( attributes[i][0] );
|
|
|
|
|
|
|
|
// transform the value
|
2012-01-11 19:48:49 +00:00
|
|
|
pipe = this.manager.getAttributePipeline( this.manager.args );
|
|
|
|
pipe.addListener( 'chunk',
|
|
|
|
this.onChunk.bind( this, this._returnAttributeValue.bind( this, i ) )
|
2012-01-11 00:05:51 +00:00
|
|
|
);
|
2012-01-11 19:48:49 +00:00
|
|
|
pipe.addListener( 'end',
|
|
|
|
this.onEnd.bind( this, this._returnAttributeValue.bind( this, i ) )
|
2012-01-11 00:05:51 +00:00
|
|
|
);
|
2012-01-13 18:48:25 +00:00
|
|
|
//console.log('starting attribute transform of ' + JSON.stringify( attributes[i][1] ) );
|
2012-01-11 00:05:51 +00:00
|
|
|
pipe.process( attributes[i][1] );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
AttributeTransformManager.prototype._returnAttributes = function ( ) {
|
|
|
|
// convert attributes
|
|
|
|
var out = [];
|
|
|
|
for ( var i = 0, l = this.kvs.length; i < l; i++ ) {
|
2012-01-11 19:48:49 +00:00
|
|
|
var kv = this.kvs[i];
|
|
|
|
out.push( [kv.key, kv.value] );
|
2012-01-11 00:05:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// and call the callback with the result
|
2012-01-13 18:48:25 +00:00
|
|
|
//console.log('AttributeTransformManager._returnAttributes: ' + out );
|
2012-01-11 00:05:51 +00:00
|
|
|
this.callback( out );
|
|
|
|
};
|
|
|
|
|
2012-01-10 01:09:50 +00:00
|
|
|
/**
|
|
|
|
* Collect chunks returned from the pipeline
|
|
|
|
*/
|
2012-01-13 18:48:25 +00:00
|
|
|
AttributeTransformManager.prototype.onChunk = function ( cb, chunk ) {
|
|
|
|
cb( chunk, true );
|
2012-01-10 01:09:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Empty the pipeline by returning to the parent
|
|
|
|
*/
|
2012-01-13 18:48:25 +00:00
|
|
|
AttributeTransformManager.prototype.onEnd = function ( cb ) {
|
|
|
|
cb( [], false );
|
2012-01-10 01:09:50 +00:00
|
|
|
};
|
2012-01-09 17:49:16 +00:00
|
|
|
|
|
|
|
|
2012-01-11 00:05:51 +00:00
|
|
|
/**
|
|
|
|
* Callback for async argument value expansions
|
|
|
|
*/
|
|
|
|
AttributeTransformManager.prototype._returnAttributeValue = function ( ref, tokens, notYetDone ) {
|
2012-01-13 18:48:25 +00:00
|
|
|
//console.log( 'check _returnAttributeValue: ' + JSON.stringify( tokens ) );
|
|
|
|
this.kvs[ref].value = this.kvs[ref].value.concat( tokens );
|
2012-01-11 00:05:51 +00:00
|
|
|
if ( ! notYetDone ) {
|
2012-01-11 19:48:49 +00:00
|
|
|
this.outstanding--;
|
|
|
|
if ( this.outstanding === 0 ) {
|
2012-01-11 00:05:51 +00:00
|
|
|
// this calls back to frame.cb, so no return here.
|
2012-01-11 19:48:49 +00:00
|
|
|
this._returnAttributes();
|
2012-01-11 00:05:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback for async argument key expansions
|
|
|
|
*/
|
|
|
|
AttributeTransformManager.prototype._returnAttributeKey = function ( ref, tokens, notYetDone ) {
|
2012-01-13 18:48:25 +00:00
|
|
|
this.kvs[ref].key = this.kvs[ref].key.concat( tokens );
|
2012-01-11 00:05:51 +00:00
|
|
|
if ( ! notYetDone ) {
|
2012-01-11 19:48:49 +00:00
|
|
|
this.outstanding--;
|
|
|
|
if ( this.outstanding === 0 ) {
|
2012-01-11 00:05:51 +00:00
|
|
|
// this calls back to frame.cb, so no return here.
|
2012-01-11 19:48:49 +00:00
|
|
|
this._returnAttributes();
|
2012-01-11 00:05:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2012-01-09 17:49:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
/******************************* TokenAccumulator *************************/
|
2011-12-13 20:13:09 +00:00
|
|
|
/**
|
2012-01-03 18:44:31 +00:00
|
|
|
* Token accumulators buffer tokens between asynchronous processing points,
|
|
|
|
* and return fully processed token chunks in-order and as soon as possible.
|
2012-01-09 17:49:16 +00:00
|
|
|
* They support the AsyncTokenTransformManager.
|
2011-12-13 20:13:09 +00:00
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @constructor
|
|
|
|
* @param {Object} next TokenAccumulator to link to
|
|
|
|
* @param {Array} (optional) tokens, init accumulator with tokens or []
|
|
|
|
*/
|
2012-01-03 18:44:31 +00:00
|
|
|
function TokenAccumulator ( parentCB ) {
|
|
|
|
this.parentCB = parentCB;
|
|
|
|
this.accum = [];
|
|
|
|
// Wait for child and sibling by default
|
|
|
|
// Note: Need to decrement outstanding on last accum
|
|
|
|
// in a chain.
|
|
|
|
this.outstanding = 2;
|
2011-12-08 14:37:31 +00:00
|
|
|
}
|
|
|
|
|
2011-12-13 20:13:09 +00:00
|
|
|
/**
|
2012-01-03 18:44:31 +00:00
|
|
|
* Curry a parentCB with the object and reference.
|
2011-12-13 20:13:09 +00:00
|
|
|
*
|
2012-01-04 12:28:41 +00:00
|
|
|
* @method
|
2012-01-03 18:44:31 +00:00
|
|
|
* @param {Object} TokenAccumulator
|
|
|
|
* @param {misc} Reference / key for callback
|
|
|
|
* @returns {Function}
|
2011-12-13 20:13:09 +00:00
|
|
|
*/
|
2012-01-03 18:44:31 +00:00
|
|
|
TokenAccumulator.prototype.getParentCB = function ( reference ) {
|
2012-01-09 17:49:16 +00:00
|
|
|
return this._returnTokens.bind( this, reference );
|
2011-12-08 14:37:31 +00:00
|
|
|
};
|
|
|
|
|
2011-12-13 20:13:09 +00:00
|
|
|
/**
|
2012-01-03 18:44:31 +00:00
|
|
|
* Pass tokens to an accumulator
|
2011-12-13 20:13:09 +00:00
|
|
|
*
|
|
|
|
* @method
|
2012-01-03 18:44:31 +00:00
|
|
|
* @param {Object} token
|
|
|
|
*/
|
2012-01-09 17:49:16 +00:00
|
|
|
TokenAccumulator.prototype._returnTokens = function ( reference, tokens, notYetDone ) {
|
2012-01-03 18:44:31 +00:00
|
|
|
var res,
|
|
|
|
cb,
|
|
|
|
returnTokens = [];
|
|
|
|
|
|
|
|
if ( ! notYetDone ) {
|
|
|
|
this.outstanding--;
|
|
|
|
}
|
|
|
|
|
2012-01-13 18:48:25 +00:00
|
|
|
//console.log( 'TokenAccumulator._returnTokens' );
|
2012-01-03 18:44:31 +00:00
|
|
|
if ( reference === 'child' ) {
|
2012-01-13 18:48:25 +00:00
|
|
|
tokens = tokens.concat( this.accum );
|
|
|
|
//console.log('TokenAccumulator._returnTokens: ' +
|
|
|
|
// JSON.stringify( tokens, null, 2 )
|
|
|
|
// );
|
|
|
|
this.accum = [];
|
2012-01-03 18:44:31 +00:00
|
|
|
// XXX: Use some marker to avoid re-transforming token chunks several
|
|
|
|
// times?
|
2012-01-13 18:48:25 +00:00
|
|
|
//res = this.transformTokens( tokens, this.parentCB );
|
|
|
|
|
|
|
|
//if ( res.async ) {
|
|
|
|
// // new asynchronous expansion started, chain of accumulators
|
|
|
|
// // created
|
|
|
|
// if ( this.outstanding === 0 ) {
|
|
|
|
// // Last accum in chain should only wait for child
|
|
|
|
// res.async.outstanding--;
|
|
|
|
// cb = this.parentCB;
|
|
|
|
// } else {
|
|
|
|
// cb = this.parentCB;
|
|
|
|
// // set own callback to new sibling, the end of accumulator chain
|
|
|
|
// this.parentCB = res.async.getParentCB( 'sibling' );
|
|
|
|
// }
|
|
|
|
//}
|
|
|
|
//if ( ! notYetDone ) {
|
|
|
|
// // Child is done, return accumulator from sibling. Siblings
|
|
|
|
// // process tokens themselves, so we concat those to the result of
|
|
|
|
// // processing tokens from the child.
|
|
|
|
// tokens = res.tokens.concat( this.accum );
|
|
|
|
// this.accum = [];
|
|
|
|
//}
|
|
|
|
this.parentCB( tokens, false );
|
2012-01-03 18:44:31 +00:00
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
// sibling
|
|
|
|
if ( this.outstanding === 0 ) {
|
|
|
|
tokens = this.accum.concat( tokens );
|
|
|
|
// A sibling will transform tokens, so we don't have to do this
|
|
|
|
// again.
|
|
|
|
this.parentCB( res.tokens, false );
|
|
|
|
return null;
|
|
|
|
} else if ( this.outstanding === 1 && notYetDone ) {
|
|
|
|
// Sibling is not yet done, but child is. Return own parentCB to
|
|
|
|
// allow the sibling to go direct, and call back parent with
|
|
|
|
// tokens. The internal accumulator is empty at this stage, as its
|
|
|
|
// tokens are passed to the parent when the child is done.
|
|
|
|
return this.parentCB( tokens, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mark the sibling as done (normally at the tail of a chain).
|
2011-12-13 20:13:09 +00:00
|
|
|
*/
|
2012-01-03 18:44:31 +00:00
|
|
|
TokenAccumulator.prototype.siblingDone = function () {
|
2012-01-13 18:48:25 +00:00
|
|
|
//console.log( 'TokenAccumulator.siblingDone: ' );
|
|
|
|
this._returnTokens ( 'sibling', [], false );
|
2011-12-12 10:01:47 +00:00
|
|
|
};
|
|
|
|
|
2012-01-03 18:44:31 +00:00
|
|
|
|
2011-12-13 20:13:09 +00:00
|
|
|
/**
|
2012-01-03 18:44:31 +00:00
|
|
|
* Push a token into the accumulator
|
2011-12-13 20:13:09 +00:00
|
|
|
*
|
|
|
|
* @method
|
2012-01-03 18:44:31 +00:00
|
|
|
* @param {Object} token
|
2011-12-13 20:13:09 +00:00
|
|
|
*/
|
2012-01-03 18:44:31 +00:00
|
|
|
TokenAccumulator.prototype.push = function ( token ) {
|
|
|
|
return this.accum.push(token);
|
2011-12-08 14:37:31 +00:00
|
|
|
};
|
2011-12-12 14:03:54 +00:00
|
|
|
|
2012-01-03 18:44:31 +00:00
|
|
|
|
2012-01-10 01:09:50 +00:00
|
|
|
/**
|
|
|
|
* Loop check helper class for AsyncTokenTransformManager.
|
|
|
|
*
|
|
|
|
* We use a bottom-up linked list to allow sharing of paths between async
|
|
|
|
* expansions.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @constructor
|
|
|
|
*/
|
2012-01-11 19:48:49 +00:00
|
|
|
function LoopCheck ( title, parent ) {
|
2012-01-10 01:09:50 +00:00
|
|
|
if ( parent ) {
|
|
|
|
this.parent = parent;
|
|
|
|
} else {
|
|
|
|
this.parent = null;
|
|
|
|
}
|
|
|
|
this.title = title;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if expanding <title> would lead to a loop.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {String} Title to check.
|
|
|
|
*/
|
|
|
|
LoopCheck.prototype.check = function ( title ) {
|
|
|
|
var elem = this;
|
|
|
|
do {
|
2012-01-14 00:58:20 +00:00
|
|
|
//console.log( 'loop check: ' + title + ' vs ' + elem.title );
|
2012-01-10 01:09:50 +00:00
|
|
|
if ( elem.title === title ) {
|
|
|
|
// Loop detected
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
elem = elem.parent;
|
|
|
|
} while ( elem );
|
|
|
|
// No loop detected.
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2011-12-12 14:03:54 +00:00
|
|
|
if (typeof module == "object") {
|
2012-01-09 17:49:16 +00:00
|
|
|
module.exports.AsyncTokenTransformManager = AsyncTokenTransformManager;
|
|
|
|
module.exports.SyncTokenTransformManager = SyncTokenTransformManager;
|
2012-01-11 19:48:49 +00:00
|
|
|
module.exports.AttributeTransformManager = AttributeTransformManager;
|
2011-12-12 14:03:54 +00:00
|
|
|
}
|