# Path: format_sql/parser.py
# def parse(toks):
#     for statement, unused_count in _parse(toks):
#         yield statement
# 
# Path: format_sql/styler.py
# def style(statements, indent=0, keyword_upper=True, liner=None):
#     if not liner:
#         liner = Liner()
# 
#     structures = {
#         From: _style_from,
#         GroupBy: _style_group_by,
#         Having: _style_having,
#         Insert: _style_insert,
#         Limit: _style_limit,
#         OrderBy: _style_order_by,
#         Select: _style_select,
#         Semicolon: _style_semicolon,
#         Where: _style_where,
#     }
# 
#     for i, statement in enumerate(statements):
#         func = structures[statement.__class__]
#         try:
#             func(statement, liner=liner, indent=indent)
#         except IndexError:
#             raise InvalidSQL()
# 
#         if isinstance(statement, Semicolon) and len(statements) > i + 1:
#             # statements holds multiple separate statements
#             liner.add_empty_lines(count=2)
# 
#     liner.end_line()
#     return liner.lines
# 
# Path: format_sql/tokenizer.py
# def tokenize(s):
#     last_type = None
#     for word in cutter(s):
#         token_type = Token.get_token(word)
#         if token_type:
#             last_type = token_type
#             yield Token(token_type, word)
# 
#         elif word[0].isdigit() or word.startswith(('+', '-')):
#             yield Token(Token.NUMBER, word)
# 
#         elif word.endswith('('):
#             if last_type == Token.INSERT:
#                 token_type = Token.IDENTIFIER
#             else:
#                 token_type = Token.FUNC
#             yield Token(token_type, word[:-1].strip())
#             yield Token(Token.PARENTHESIS_OPEN, '(')
# 
#         elif word.startswith(STR_STARTERS):
#             yield Token(Token.STR, word)
# 
#         elif word in ('=', '<>', '<', '>', '!=', '>=', '<=') or word.upper() == 'LIKE':
#             yield Token(Token.COMPARE, word)
# 
#         elif word.upper() in ('AND', 'OR'):
#             yield Token(Token.LINK, word)
# 
#         else:
#             yield Token(Token.IDENTIFIER, word)
# 
# Path: format_sql/util.py
# def print_non_data(msg):
#     print(msg, file=sys.stderr)


from format_sql.parser import parse
from format_sql.styler import style
from format_sql.tokenizer import tokenize
from format_sql.util import print_non_data
# -*- coding: utf-8 -*-
"""
format-sql
Makes your SQL readable.

Copyright (c) 2014-2015, Friedrich Paetzke (paetzke@fastmail.fm)
All rights reserved.

"""


def format_sql(s, debug=False):
    tokens = list(tokenize(s))
    if debug:
        print_non_data('Tokens: %s' % tokens)
    parsed = list(parse(tokens))
    if debug:
        print_non_data('Statements: %s' % parsed)
    styled = style(parsed)
    if debug:

# Path: format_sql/parser.py
# def parse(toks):
#     for statement, unused_count in _parse(toks):
#         yield statement
# 
# Path: format_sql/styler.py
# def style(statements, indent=0, keyword_upper=True, liner=None):
#     if not liner:
#         liner = Liner()
# 
#     structures = {
#         From: _style_from,
#         GroupBy: _style_group_by,
#         Having: _style_having,
#         Insert: _style_insert,
#         Limit: _style_limit,
#         OrderBy: _style_order_by,
#         Select: _style_select,
#         Semicolon: _style_semicolon,
#         Where: _style_where,
#     }
# 
#     for i, statement in enumerate(statements):
#         func = structures[statement.__class__]
#         try:
#             func(statement, liner=liner, indent=indent)
#         except IndexError:
#             raise InvalidSQL()
# 
#         if isinstance(statement, Semicolon) and len(statements) > i + 1:
#             # statements holds multiple separate statements
#             liner.add_empty_lines(count=2)
# 
#     liner.end_line()
#     return liner.lines
# 
# Path: format_sql/tokenizer.py
# def tokenize(s):
#     last_type = None
#     for word in cutter(s):
#         token_type = Token.get_token(word)
#         if token_type:
#             last_type = token_type
#             yield Token(token_type, word)
# 
#         elif word[0].isdigit() or word.startswith(('+', '-')):
#             yield Token(Token.NUMBER, word)
# 
#         elif word.endswith('('):
#             if last_type == Token.INSERT:
#                 token_type = Token.IDENTIFIER
#             else:
#                 token_type = Token.FUNC
#             yield Token(token_type, word[:-1].strip())
#             yield Token(Token.PARENTHESIS_OPEN, '(')
# 
#         elif word.startswith(STR_STARTERS):
#             yield Token(Token.STR, word)
# 
#         elif word in ('=', '<>', '<', '>', '!=', '>=', '<=') or word.upper() == 'LIKE':
#             yield Token(Token.COMPARE, word)
# 
#         elif word.upper() in ('AND', 'OR'):
#             yield Token(Token.LINK, word)
# 
#         else:
#             yield Token(Token.IDENTIFIER, word)
# 
# Path: format_sql/util.py
# def print_non_data(msg):
#     print(msg, file=sys.stderr)


# Path: format_sql/shortcuts.py
from format_sql.parser import parse
from format_sql.styler import style
from format_sql.tokenizer import tokenize
from format_sql.util import print_non_data

# -*- coding: utf-8 -*-
"""
format-sql
Makes your SQL readable.

Copyright (c) 2014-2015, Friedrich Paetzke (paetzke@fastmail.fm)
All rights reserved.

"""


def format_sql(s, debug=False):
    tokens = list(tokenize(s))
    if debug:
        print_non_data('Tokens: %s' % tokens)
    parsed = list(parse(tokens))
    if debug:
        print_non_data('Statements: %s' % parsed)
    styled = style(parsed)
    if debug:

        print_non_data('Output: %s' % styled)