Index: trunk/extensions/VisualEditor/modules/parser/ext.util.TokenCollector.js |
— | — | @@ -0,0 +1,87 @@ |
| 2 | +/** |
| 3 | + * Small utility class that encapsulates the common 'collect all tokens |
| 4 | + * starting from a token of type x until token of type y or (optionally) the |
| 5 | + * end-of-input'. Only supported for synchronous in-order transformation |
| 6 | + * stages (SyncTokenTransformManager), as async out-of-order expansions |
| 7 | + * would wreak havoc with this kind of collector. |
| 8 | + * |
| 9 | + * Calls the passed-in callback with the collected tokens. |
| 10 | + * |
| 11 | + * @class |
| 12 | + * @constructor |
| 13 | + * @param {Object} SyncTokenTransformManager to register with |
| 14 | + * @param {Function} Transform function, called like this: |
| 15 | + * transform( tokens, cb, manager ) with |
| 16 | + * tokens: chunk of tokens |
| 17 | + * cb: function, returnTokens ( tokens, notYetDone ) with notYetDone |
| 18 | + * indicating the last chunk of an async return. |
| 19 | + * manager: TokenTransformManager, provides the args etc. |
| 20 | + * @param {Boolean} Match the 'end' tokens as closing tag as well (accept |
| 21 | + * unclosed sections). |
| 22 | + * @param {Nummber} Numerical rank of the tranform |
| 23 | + * @param {String} Token type to register for ('tag', 'text' etc) |
| 24 | + * @param {String} (optional, only for token type 'tag'): tag name. |
| 25 | + */ |
| 26 | + |
| 27 | +function TokenCollector ( manager, transformer, toEnd, rank, type, name ) { |
| 28 | + this.transformer = transformer; |
| 29 | + this.manager = manager; |
| 30 | + this.rank = rank; |
| 31 | + this.type = type; |
| 32 | + this.name = name; |
| 33 | + this.toEnd = toEnd; |
| 34 | + this.tokens = []; |
| 35 | + this.isActive = false; |
| 36 | + manager.addTransform( this._onDelimiterToken.bind( this ), rank, type, name ); |
| 37 | + manager.addTransform( this._onDelimiterToken.bind( this ), rank, 'end' ); |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Register any collector with slightly lower priority than the start/end token type |
| 42 | + * XXX: This feels a bit hackish, a list-of-registrations per rank might be |
| 43 | + * better. |
| 44 | + */ |
| 45 | +TokenCollector.prototype._anyDelta = 0.00001; |
| 46 | + |
| 47 | + |
| 48 | +/** |
| 49 | + * Handle the delimiter token. |
| 50 | + * XXX: Adjust to sync phase callback when that is modified! |
| 51 | + */ |
| 52 | +TokenCollector.prototype._onDelimiterToken ( token, cb, frame ) { |
| 53 | + this.manager.addTransform( this._anyToken.bind ( this ), rank + this._anyDelta, 'any' ); |
| 54 | + this.tokens.push ( token ); |
| 55 | + if ( ! this.isActive ) { |
| 56 | + this.isActive = true; |
| 57 | + this.cb = cb; |
| 58 | + return { async: true }; |
| 59 | + } else if ( token.type !== 'end' || this.toEnd ) { |
| 60 | + // end token |
| 61 | + this.tokens = []; |
| 62 | + this.manager.removeTransform( this.rank + this._anyDelta, 'any' ); |
| 63 | + this.isActive = false; |
| 64 | + // Transformer can be either sync or async, but receives all collected |
| 65 | + // tokens instead of a single token. |
| 66 | + return this.transformer ( this.tokens, this.cb, this.manager ); |
| 67 | + // XXX sync version: return tokens |
| 68 | + } else if ( token.type === 'end' && ! this.toEnd ) { |
| 69 | + // Did not encounter a matching end token before the end, and are not |
| 70 | + // supposed to collect to the end. So just return the tokens verbatim. |
| 71 | + this.isActive = false; |
| 72 | + return { tokens: this.tokens }; |
| 73 | + } |
| 74 | +}; |
| 75 | + |
| 76 | + |
| 77 | +/** |
| 78 | + * Handle 'any' token in between delimiter tokens. Activated when |
| 79 | + * encountering the delimiter token, and collects all tokens until the end |
| 80 | + * token is reached. |
| 81 | + */ |
| 82 | +TokenCollector.prototype._onAnyToken ( token, cb, frame ) { |
| 83 | + // Simply collect anything ordinary in between |
| 84 | + this.tokens.push( token ); |
| 85 | + return { }; |
| 86 | +} |
| 87 | + |
| 88 | + |
Property changes on: trunk/extensions/VisualEditor/modules/parser/ext.util.TokenCollector.js |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 89 | + native |