def do_insertions(insertions, tokens):
insertions = iter(insertions)<EOL>try:<EOL><INDENT>index, itokens = next(insertions)<EOL><DEDENT>except StopIteration:<EOL><INDENT>for item in tokens:<EOL><INDENT>yield item<EOL><DEDENT>return<EOL><DEDENT>realpos = None<EOL>insleft = True<EOL>for i, t, v in tokens:<EOL><INDENT>if realpos is None:<EOL><INDENT>realpos = i<EOL><DEDENT>oldi = <NUM_LIT:0><EOL>while insleft and i + len(v) >= index:<EOL><INDENT>tmpval = v[oldi:index - i]<EOL>yield realpos, t, tmpval<EOL>realpos += len(tmpval)<EOL>for it_index, it_token, it_value in itokens:<EOL><INDENT>yield realpos, it_token, it_value<EOL>realpos += len(it_value)<EOL><DEDENT>oldi = index - i<EOL>try:<EOL><INDENT>index, itokens = next(insertions)<EOL><DEDENT>except StopIteration:<EOL><INDENT>insleft = False<EOL>break  <EOL><DEDENT><DEDENT>yield realpos, t, v[oldi:]<EOL>realpos += len(v) - oldi<EOL><DEDENT>while insleft:<EOL><INDENT>realpos = realpos or <NUM_LIT:0><EOL>for p, t, v in itokens:<EOL><INDENT>yield realpos, t, v<EOL>realpos += len(v)<EOL><DEDENT>try:<EOL><INDENT>index, itokens = next(insertions)<EOL><DEDENT>except StopIteration:<EOL><INDENT>insleft = False<EOL>break<EOL><DEDENT><DEDENT>
Helper for lexers which must combine the results of several
sublexers.

``insertions`` is a list of ``(index, itokens)`` pairs.
Each ``itokens`` iterable should be inserted at position
``index`` into the token stream given by the ``tokens``
argument.

The result is a combined token stream.

TODO: clean up the code here.