| 35 | } |
| 36 | |
| 37 | function parse(input, matchers) { |
| 38 | const children = []; |
| 39 | const length = input.length; |
| 40 | |
| 41 | let index = 0; |
| 42 | let buffer = ''; |
| 43 | |
| 44 | function flush() { |
| 45 | if (buffer) { |
| 46 | children.push({ |
| 47 | type: 'text', |
| 48 | value: buffer, |
| 49 | source: buffer, |
| 50 | }); |
| 51 | buffer = ''; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | while (index < length) { |
| 56 | const result = match(input, index, matchers); |
| 57 | |
| 58 | if (result) { |
| 59 | flush(); |
| 60 | |
| 61 | const [node, position] = result; |
| 62 | children.push(node); |
| 63 | index = position; |
| 64 | continue; |
| 65 | } |
| 66 | |
| 67 | buffer += input[index]; |
| 68 | index++; |
| 69 | } |
| 70 | |
| 71 | flush(); |
| 72 | |
| 73 | return children; |
| 74 | } |
| 75 | |
| 76 | module.exports = function (input, matchers) { |
| 77 | try { |