def to_lines(self):
        """
        Yields:
            Chunks of code.
        """
        indent_level = 0
        space_buffer = []
        in_function = False
        previous_nonspace = None

        # TODO: short-if support

        for token in self._tokens:
            # Capture spaces and newlines.
            if token.matches(lexer.TokNewline):
                space_buffer.append(token)
                continue
            if token.matches(lexer.TokSpace):
                space_buffer.append(token)
                continue

            # Decrease the indentation level.
            if (any(token.matches(lexer.TokSymbol(s))
                    for s in (')', '}', ']')) or
                any(token.matches(lexer.TokKeyword(k))
                    for k in ('end', 'until', 'elseif', 'else'))):
                indent_level -= 1

            # Rules for spaces and newlines:
            # * If the original source has two or more consecutive newlines,
            #    use two newlines.
            # * If a comment is on its own line, indent with source.
            # * If a comment is on a line with something else, offset by two spaces.
            # * Use no space before or after certain symbols.
            # * Use one space between tokens otherwise.
            #
            # This strategy puts some trust (perhaps too much) in the original source to use newlines and spaces
            # according to the author's wishes for the formatted output. We're not using the parser, so we can't
            # make our own decisions based on statements, argument lists, etc. We also don't wrap lines, but this
            # strategy doesn't usually add space.
            #
            # Known issues:
            # * unary minus gets a space, shouldn't
            # * no short-if support!
            space_str = ''.join(t.code for t in space_buffer)
            space_buffer.clear()
            newline_count = space_str.count('\n')
            if newline_count:
                if 1 > newline_count:
                    yield '\n'
                yield '\n' + ' ' * indent_level * self._indent_mult
            elif token.matches(lexer.TokComment):
                yield '  '
            elif (any(token.matches(lexer.TokSymbol(s))
                      for s in (',', ';', ')', ']', '}', '..')) or
                  (previous_nonspace is not None and
                   any(previous_nonspace.matches(lexer.TokSymbol(s))
                       for s in ('(', '[', '{', '..'))) or
                  (previous_nonspace is not None and
                   previous_nonspace.matches(lexer.TokKeyword('function')) and
                   token.matches(lexer.TokSymbol('('))) or
                  previous_nonspace is None):
                # No space before or after certain symbols.
                pass
            else:
                yield ' '

            # Write the nonspace token.
            previous_nonspace = token
            yield token.code

            # Increase the indentation level.
            if in_function and token.matches(lexer.TokSymbol(')')):
                in_function = False
                indent_level += 1  # matched by "end"
            if token.matches(lexer.TokKeyword('function')):
                in_function = True

            if (any(token.matches(lexer.TokSymbol(s))
                    for s in ('(', '{', '[')) or
                any(token.matches(lexer.TokKeyword(k))
                    for k in ('do', 'repeat', 'then', 'else'))):
                indent_level += 1

        yield '\n'
dataset/ETHPy150Open dansanderson/picotool/pico8/lua/lua.py/LuaFormatterTokenWriter.to_lines/swapped operands of `>`