[6a3a178] | 1 | var tokenize = require('../tokenizer');
|
---|
| 2 | var TokenStream = require('../common/TokenStream');
|
---|
| 3 | var tokenStream = new TokenStream();
|
---|
| 4 | var astToTokens = {
|
---|
| 5 | decorator: function(handlers) {
|
---|
| 6 | var curNode = null;
|
---|
| 7 | var prev = { len: 0, node: null };
|
---|
| 8 | var nodes = [prev];
|
---|
| 9 | var buffer = '';
|
---|
| 10 |
|
---|
| 11 | return {
|
---|
| 12 | children: handlers.children,
|
---|
| 13 | node: function(node) {
|
---|
| 14 | var tmp = curNode;
|
---|
| 15 | curNode = node;
|
---|
| 16 | handlers.node.call(this, node);
|
---|
| 17 | curNode = tmp;
|
---|
| 18 | },
|
---|
| 19 | chunk: function(chunk) {
|
---|
| 20 | buffer += chunk;
|
---|
| 21 | if (prev.node !== curNode) {
|
---|
| 22 | nodes.push({
|
---|
| 23 | len: chunk.length,
|
---|
| 24 | node: curNode
|
---|
| 25 | });
|
---|
| 26 | } else {
|
---|
| 27 | prev.len += chunk.length;
|
---|
| 28 | }
|
---|
| 29 | },
|
---|
| 30 | result: function() {
|
---|
| 31 | return prepareTokens(buffer, nodes);
|
---|
| 32 | }
|
---|
| 33 | };
|
---|
| 34 | }
|
---|
| 35 | };
|
---|
| 36 |
|
---|
| 37 | function prepareTokens(str, nodes) {
|
---|
| 38 | var tokens = [];
|
---|
| 39 | var nodesOffset = 0;
|
---|
| 40 | var nodesIndex = 0;
|
---|
| 41 | var currentNode = nodes ? nodes[nodesIndex].node : null;
|
---|
| 42 |
|
---|
| 43 | tokenize(str, tokenStream);
|
---|
| 44 |
|
---|
| 45 | while (!tokenStream.eof) {
|
---|
| 46 | if (nodes) {
|
---|
| 47 | while (nodesIndex < nodes.length && nodesOffset + nodes[nodesIndex].len <= tokenStream.tokenStart) {
|
---|
| 48 | nodesOffset += nodes[nodesIndex++].len;
|
---|
| 49 | currentNode = nodes[nodesIndex].node;
|
---|
| 50 | }
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | tokens.push({
|
---|
| 54 | type: tokenStream.tokenType,
|
---|
| 55 | value: tokenStream.getTokenValue(),
|
---|
| 56 | index: tokenStream.tokenIndex, // TODO: remove it, temporary solution
|
---|
| 57 | balance: tokenStream.balance[tokenStream.tokenIndex], // TODO: remove it, temporary solution
|
---|
| 58 | node: currentNode
|
---|
| 59 | });
|
---|
| 60 | tokenStream.next();
|
---|
| 61 | // console.log({ ...tokens[tokens.length - 1], node: undefined });
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | return tokens;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | module.exports = function(value, syntax) {
|
---|
| 68 | if (typeof value === 'string') {
|
---|
| 69 | return prepareTokens(value, null);
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | return syntax.generate(value, astToTokens);
|
---|
| 73 | };
|
---|