a
    còfg  ã                   @   s\   d Z ddlZddlmZ ddlmZ ddlmZmZ ddl	m
Z
 G dd„ dƒZdd	d
„ZdS )z	SQL Lexeré    N)ÚLock)Ú
TextIOBase)ÚtokensÚkeywords)Úconsumec                   @   sX   e Zd ZdZdZeƒ Zedd„ ƒZdd„ Z	dd„ Z
d	d
„ Zdd„ Zdd„ Zddd„ZdS )ÚLexerzrThe Lexer supports configurable syntax.
    To add support for additional keywords, use the `add_keywords` method.Nc                 C   sH   | j �, | jdu r$| ƒ | _| j ¡  W d  ƒ n1 s80    Y  | jS )zRReturns the lexer instance used internally
        by the sqlparse core functions.N)Ú_lockÚ_default_instanceÚdefault_initialization)Úcls© r   úK/home/httpd/docs/test/DocsMgr/lib/python3.9/site-packages/sqlparse/lexer.pyÚget_default_instance0   s
    
(zLexer.get_default_instancec                 C   s„   |   ¡  |  tj¡ |  tj¡ |  tj¡ |  tj¡ |  tj¡ |  tj	¡ |  tj
¡ |  tj¡ |  tj¡ |  tj¡ dS )zlInitialize the lexer with default dictionaries.
        Useful if you need to revert custom syntax settings.N)ÚclearÚset_SQL_REGEXr   Ú	SQL_REGEXÚadd_keywordsZKEYWORDS_COMMONZKEYWORDS_ORACLEZKEYWORDS_MYSQLZKEYWORDS_PLPGSQLZKEYWORDS_HQLZKEYWORDS_MSACCESSZKEYWORDS_SNOWFLAKEZKEYWORDS_BIGQUERYÚKEYWORDS©Úselfr   r   r   r
   :   s    zLexer.default_initializationc                 C   s   g | _ g | _dS )zãClear all syntax configurations.
        Useful if you want to load a reduced set of syntax configurations.
        After this call, regexps and keyword dictionaries need to be loaded
        to make the lexer functional again.N)Ú
_SQL_REGEXÚ	_keywordsr   r   r   r   r   I   s    zLexer.clearc                    s$   t jt jB ‰ ‡ fdd„|D ƒ| _dS )z.Set the list of regex that will parse the SQL.c                    s"   g | ]\}}t  |ˆ ¡j|f‘qS r   )ÚreÚcompileÚmatch)Ú.0ÚrxÚtt©ÚFLAGSr   r   Ú
<listcomp>T   s   ÿz'Lexer.set_SQL_REGEX.<locals>.<listcomp>N)r   Ú
IGNORECASEÚUNICODEr   )r   r   r   r   r   r   Q   s    
þzLexer.set_SQL_REGEXc                 C   s   | j  |¡ dS )zhAdd keyword dictionaries. Keywords are looked up in the same order
        that dictionaries were added.N)r   Úappend)r   r   r   r   r   r   Y   s    zLexer.add_keywordsc                 C   s6   |  ¡ }| jD ]}||v r|| |f  S qtj|fS )z¨Checks for a keyword.

        If the given value is in one of the KEYWORDS_* dictionary
        it's considered a keyword. Otherwise, tokens.Name is returned.
        N)Úupperr   r   ÚName)r   ÚvalueÚvalZkwdictr   r   r   Ú
is_keyword^   s
    
zLexer.is_keywordc           	      c   s  t |tƒr| ¡ }t |tƒrnZt |tƒrf|r8| |¡}qxz| d¡}W qx tyb   | d¡}Y qx0 ntd t	|ƒ¡ƒ‚t
|ƒ}|D ]†\}}| jD ]j\}}|||ƒ}|s¬q’n6t |tjƒrÈ|| ¡ fV  n|tju râ|  | ¡ ¡V  t|| ¡ | d ƒ  q„q’tj|fV  q„dS )a³  
        Return an iterable of (tokentype, value) pairs generated from
        `text`. If `unfiltered` is set to `True`, the filtering mechanism
        is bypassed even if filters are defined.

        Also preprocess the text, i.e. expand tabs and strip it if
        wanted and applies registered filters.

        Split ``text`` into (tokentype, text) pairs.

        ``stack`` is the initial stack (default: ``['root']``)
        zutf-8zunicode-escapez+Expected text or file-like object, got {!r}é   N)Ú
isinstancer   ÚreadÚstrÚbytesÚdecodeÚUnicodeDecodeErrorÚ	TypeErrorÚformatÚtypeÚ	enumerater   r   Ú
_TokenTypeÚgroupr   ZPROCESS_AS_KEYWORDr(   r   ÚendÚError)	r   ÚtextÚencodingÚiterableÚposÚcharÚrexmatchÚactionÚmr   r   r   Ú
get_tokensk   s6    


ÿ

zLexer.get_tokens)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r	   r   r   Úclassmethodr   r
   r   r   r   r(   r@   r   r   r   r   r      s   
	r   c                 C   s   t  ¡  | |¡S )z�Tokenize sql.

    Tokenize *sql* using the :class:`Lexer` and return a 2-tuple stream
    of ``(token type, value)`` items.
    )r   r   r@   )Úsqlr9   r   r   r   Útokenize›   s    rG   )N)rD   r   Ú	threadingr   Úior   Zsqlparser   r   Zsqlparse.utilsr   r   rG   r   r   r   r   Ú<module>   s    