(self)
Yields:
            Chunks of code.
Yields:
            Chunks of code.
Yields : Chunks of code .
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 (b')', b'}', b']')) or
                any(token.matches(lexer.TokKeyword(k))
                    for k in (b'end', b'until', b'elseif', b'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 = b''.join(t.code for t in space_buffer)
            space_buffer.clear()
            newline_count = space_str.count(b'\n')
            if newline_count:
                if newline_count > 1:
                    yield b'\n'
                yield b'\n' + b' ' * indent_level * self._indent_mult
            elif token.matches(lexer.TokComment):
                yield b'  '
            elif (any(token.matches(lexer.TokSymbol(s))
                      for s in (b',', b';', b')', b']', b'}', b'..')) or
                  (previous_nonspace is not None and
                   any(previous_nonspace.matches(lexer.TokSymbol(s))
                       for s in (b'(', b'[', b'{', b'..'))) or
                  (previous_nonspace is not None and
                   previous_nonspace.matches(lexer.TokKeyword(b'function')) and
                   token.matches(lexer.TokSymbol(b'('))) or
                  previous_nonspace is None):
                # No space before or after certain symbols.
                pass
            else:
                yield b' '

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

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

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

        yield b'\n'
def to_lines ( self ) : 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 ( b')' , b'}' , b']' ) ) or any ( token . matches ( lexer . TokKeyword ( k ) ) for k in ( b'end' , b'until' , b'elseif' , b'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 = b'' . join ( t . code for t in space_buffer ) space_buffer . clear ( ) newline_count = space_str . count ( b'\n' ) if newline_count : if newline_count > 1 : yield b'\n' yield b'\n' + b' ' * indent_level * self . _indent_mult elif token . matches ( lexer . TokComment ) : yield b'  ' elif ( any ( token . matches ( lexer . TokSymbol ( s ) ) for s in ( b',' , b';' , b')' , b']' , b'}' , b'..' ) ) or ( previous_nonspace is not None and any ( previous_nonspace . matches ( lexer . TokSymbol ( s ) ) for s in ( b'(' , b'[' , b'{' , b'..' ) ) ) or ( previous_nonspace is not None and previous_nonspace . matches ( lexer . TokKeyword ( b'function' ) ) and token . matches ( lexer . TokSymbol ( b'(' ) ) ) or previous_nonspace is None ) : # No space before or after certain symbols. pass else : yield b' ' # Write the nonspace token. previous_nonspace = token yield token . code # Increase the indentation level. if in_function and token . matches ( lexer . TokSymbol ( b')' ) ) : in_function = False indent_level += 1 # matched by "end" if token . matches ( lexer . TokKeyword ( b'function' ) ) : in_function = True if ( any ( token . matches ( lexer . TokSymbol ( s ) ) for s in ( b'(' , b'{' , b'[' ) ) or any ( token . matches ( lexer . TokKeyword ( k ) ) for k in ( b'do' , b'repeat' , b'then' , b'else' ) ) ) : indent_level += 1 yield b'\n'