def beautify(self, s, opts=None):
    if (opts != None):
        self.opts = opts
    if (self.opts.brace_style not in ['expand', 'collapse', 'end-expand']):
        raise Exception('opts.brace_style must be "expand", "collapse" or "end-expand".')
    self.blank_state()
    while (s and (s[0] in [' ', '\t'])):
        self.preindent_string += s[0]
        s = s[1:]
    self.input = self.unpack(s, self.opts.eval_code)
    self.parser_pos = 0
    handlers = {'TK_START_EXPR': self.handle_start_expr, 'TK_END_EXPR': self.handle_end_expr, 'TK_START_BLOCK': self.handle_start_block, 'TK_END_BLOCK': self.handle_end_block, 'TK_WORD': self.handle_word, 'TK_RESERVED': self.handle_word, 'TK_SEMICOLON': self.handle_semicolon, 'TK_STRING': self.handle_string, 'TK_EQUALS': self.handle_equals, 'TK_OPERATOR': self.handle_operator, 'TK_COMMA': self.handle_comma, 'TK_BLOCK_COMMENT': self.handle_block_comment, 'TK_INLINE_COMMENT': self.handle_inline_comment, 'TK_COMMENT': self.handle_comment, 'TK_DOT': self.handle_dot, 'TK_UNKNOWN': self.handle_unknown}
    while True:
        (self.token_text, self.token_type) = self.get_next_token()
        if (self.token_type == 'TK_EOF'):
            while (self.flags.mode == MODE.Statement):
                self.restore_mode()
            break
        keep_whitespace = (self.opts.keep_array_indentation and self.is_array(self.flags.mode))
        self.input_wanted_newline = (self.n_newlines > 0)
        if keep_whitespace:
            for i in range(self.n_newlines):
                self.append_newline((i > 0))
        else:
            if ((self.opts.max_preserve_newlines != 0) and (self.n_newlines > self.opts.max_preserve_newlines)):
                self.n_newlines = self.opts.max_preserve_newlines
            if (self.opts.preserve_newlines and (self.n_newlines > 1)):
                for i in range(self.n_newlines):
                    self.append_newline((i != 0))
        handlers[self.token_type](self.token_text)
        if ((self.token_type != 'TK_INLINE_COMMENT') and (self.token_type != 'TK_COMMENT') and (self.token_type != 'TK_BLOCK_COMMENT') and (self.token_type != 'TK_UNKNOWN')):
            self.last_last_text = self.flags.last_text
            self.last_type = self.token_type
            self.flags.last_text = self.token_text
        self.flags.had_comment = (self.token_type in ['TK_COMMENT', 'TK_INLINE_COMMENT', 'TK_BLOCK_COMMENT'])
    sweet_code = ''.join(self.output_lines[0].text)
    if (len(self.output_lines) > 1):
        for line_index in range(1, len(self.output_lines)):
            sweet_code += ('\n' + ''.join(self.output_lines[line_index].text))
    sweet_code = re.sub('[\n ]+$', '', sweet_code)
    return sweet_code
def beautify(self, s, opts=None):
    if (opts != None):
        self.opts = opts
    if (self.opts.brace_style not in ['expand', 'collapse', 'end-expand']):
        raise Exception('opts.brace_style must be "expand", "collapse" or "end-expand".')
    self.blank_state()
    while (s and (s[0] in [' ', '\t'])):
        self.preindent_string += s[0]
        s = s[1:]
    self.input = self.unpack(s, self.opts.eval_code)
    self.parser_pos = 0
    handlers = {'TK_START_EXPR': self.handle_start_expr, 'TK_END_EXPR': self.handle_end_expr, 'TK_START_BLOCK': self.handle_start_block, 'TK_END_BLOCK': self.handle_end_block, 'TK_WORD': self.handle_word, 'TK_RESERVED': self.handle_word, 'TK_SEMICOLON': self.handle_semicolon, 'TK_STRING': self.handle_string, 'TK_EQUALS': self.handle_equals, 'TK_OPERATOR': self.handle_operator, 'TK_COMMA': self.handle_comma, 'TK_BLOCK_COMMENT': self.handle_block_comment, 'TK_INLINE_COMMENT': self.handle_inline_comment, 'TK_COMMENT': self.handle_comment, 'TK_DOT': self.handle_dot, 'TK_UNKNOWN': self.handle_unknown}
    while True:
        (self.token_text, self.token_type) = self.get_next_token()
        if (self.token_type == 'TK_EOF'):
            while (self.flags.mode == MODE.Statement):
                self.restore_mode()
            break
        keep_whitespace = (self.opts.keep_array_indentation and self.is_array(self.flags.mode))
        self.input_wanted_newline = (self.n_newlines > 0)
        if keep_whitespace:
            for i in range(self.n_newlines):
                self.append_newline((i > 0))
        else:
            if ((self.opts.max_preserve_newlines != 0) and (self.n_newlines > self.opts.max_preserve_newlines)):
                self.n_newlines = self.opts.max_preserve_newlines
            if (self.opts.preserve_newlines and (self.n_newlines > 1)):
                for i in range(self.n_newlines):
                    self.append_newline((i != 0))
        handlers[self.token_type](self.token_text)
        if ((self.token_type != 'TK_INLINE_COMMENT') and (self.token_type != 'TK_COMMENT') and (self.token_type != 'TK_BLOCK_COMMENT') and (self.token_type != 'TK_UNKNOWN')):
            self.last_last_text = self.flags.last_text
            self.last_type = self.token_type
            self.flags.last_text = self.token_text
        self.flags.had_comment = (self.token_type in ['TK_COMMENT', 'TK_INLINE_COMMENT', 'TK_BLOCK_COMMENT'])
    sweet_code = .join(self.output_lines[0].text)
    if (len(self.output_lines) > 1):
        for line_index in range(1, len(self.output_lines)):
            sweet_code += ('\n' + .join(self.output_lines[line_index].text))
    sweet_code = re.sub('[\n ]+$', , sweet_code)
    return sweet_code