# -*- coding: utf-8 -*-
"""
    PostgreSQL SQL lexer
"""

from pygments.lexers.sql import SqlLexer
from pygments.token import Name, Keyword

__all__ = ['PostgreSQLLexer']

class PostgreSQLLexer(SqlLexer):
    name = 'PostgreSQL'
    aliases = ['postgresql', 'postgres']
    filenames = ['*.sql'] # just to have one if you want to use

    EXTRA_KEYWORDS = ['partition', 'over', 'rank', 'ntile',
                      'first_value', 'last_value', 'row_number', 'nulls',
                      'range', 'unbounded', 'preceding', 'following', 'trunc',
                      'nth_value', 'dense_rank', 'lag', 'lead',
                      'round', 'make_interval', 'date_trunc']

    def get_tokens_unprocessed(self, text):
        for index, token, value in SqlLexer.get_tokens_unprocessed(self, text):
            if token is Name and value in self.EXTRA_KEYWORDS:
                yield index, Keyword, value
            else:
                yield index, token, value
