B
    `a[Ny                 @   s  d Z ddlmZ ddlZddlZddlZddlmZmZ ddl	m
Z
 ddlmZmZmZmZ ddlmZmZmZmZmZmZmZmZmZ ddlmZ d	d
ddddddddddgZdddddgZedd ZG dd de Z!ee!G dd	 d	e"Z#G dd de#Z$G d d de%Z&G d!d" d"e"Z'e' Z(G d#d$ d$e)Z*G d%d& d&e"Z+d'd Z,G d(d) d)e"Z-e- Z.d*d Z/G d+d dZ0G d,d deZ1G d-d. d.e!Z2ee2G d/d
 d
e#Z3G d0d de"Z4G d1d de3Z5d2d3 Z6G d4d5 d5e2Z7ee7G d6d7 d7e3Z8dS )8z
    pygments.lexer
    ~~~~~~~~~~~~~~

    Base lexer classes.

    :copyright: Copyright 2006-2017 by the Pygments team, see AUTHORS.
    :license: BSD, see LICENSE for details.
    )print_functionN)apply_filtersFilter)get_filter_by_name)ErrorTextOther
_TokenType)	get_bool_optget_int_optget_list_optmake_analysator	text_typeadd_metaclass	iteritemsFutureguess_decode)	regex_optLexer
RegexLexerExtendedRegexLexerDelegatingLexerLexerContextincludeinheritbygroupsusingthisdefaultwords)s   ﻿zutf-8)s     zutf-32)s     zutf-32be)s   zutf-16)s   zutf-16bec             C   s   dS )Ng         )xr    r    -lib/python3.7/site-packages/pygments/lexer.py<lambda>$   s    r#   c               @   s   e Zd ZdZdd ZdS )	LexerMetaz
    This metaclass automagically converts ``analyse_text`` methods into
    static methods which always return float values.
    c             C   s(   d|krt |d |d< t| |||S )Nanalyse_text)r   type__new__)Zmcsnamebasesdr    r    r"   r'   -   s    zLexerMeta.__new__N)__name__
__module____qualname____doc__r'   r    r    r    r"   r$   '   s   r$   c               @   sZ   e Zd ZdZdZg Zg Zg Zg ZdZ	dd Z
dd Zdd	 Zd
d ZdddZdd ZdS )r   a  
    Lexer for a specific language.

    Basic options recognized:
    ``stripnl``
        Strip leading and trailing newlines from the input (default: True).
    ``stripall``
        Strip all leading and trailing whitespace from the input
        (default: False).
    ``ensurenl``
        Make sure that the input ends with a newline (default: True).  This
        is required for some lexers that consume input linewise.

        .. versionadded:: 1.3

    ``tabsize``
        If given and greater than 0, expand tabs in the input (default: 0).
    ``encoding``
        If given, must be an encoding name. This encoding will be used to
        convert the input string to Unicode, if it is not already a Unicode
        string (default: ``'guess'``, which uses a simple UTF-8 / Locale /
        Latin1 detection.  Can also be ``'chardet'`` to use the chardet
        library, if it is installed.
    ``inencoding``
        Overrides the ``encoding`` if given.
    Nr   c             K   s   || _ t|dd| _t|dd| _t|dd| _t|dd| _|dd	| _|d
pZ| j| _g | _	xt
|ddD ]}| | qrW d S )NstripnlTstripallFensurenltabsizer   encodingguessZ
inencodingfiltersr    )optionsr
   r/   r0   r1   r   r2   getr3   r5   r   
add_filter)selfr6   filter_r    r    r"   __init__b   s    zLexer.__init__c             C   s(   | j rd| jj| j f S d| jj S d S )Nz<pygments.lexers.%s with %r>z<pygments.lexers.%s>)r6   	__class__r+   )r9   r    r    r"   __repr__n   s    
zLexer.__repr__c             K   s&   t |tst|f|}| j| dS )z8
        Add a new stream filter to this lexer.
        N)
isinstancer   r   r5   append)r9   r:   r6   r    r    r"   r8   u   s    
zLexer.add_filterc             C   s   dS )a~  
        Has to return a float between ``0`` and ``1`` that indicates
        if a lexer wants to highlight this text. Used by ``guess_lexer``.
        If this method returns ``0`` it won't highlight it in any case, if
        it returns ``1`` highlighting with this lexer is guaranteed.

        The `LexerMeta` metaclass automatically wraps this function so
        that it works like a static method (no ``self`` or ``cls``
        parameter) and the return value is automatically converted to
        `float`. If the return value is an object that is boolean `False`
        it's the same as if the return values was ``0.0``.
        Nr    )textr    r    r"   r%   }   s    zLexer.analyse_textFc                s  t ts jdkr"t\}nʈ jdkryddl}W n tk
rT   tdY nX d}x4tD ],\}}|r`t|d 	|d}P q`W |dkr|
dd }	|dpd	d}|n&	 jd
rtd
d nd
r
td
d dddd jr4 n jrFd jdkr^ j jrzdszd7  fdd}	|	 }
|st|
 j }
|
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.
        r4   chardetr   NzkTo enable chardet encoding guessing, please install the chardet library from http://chardet.feedparser.org/replacei   r3   zutf-8u   ﻿z

c              3   s(   x"  D ]\} }}||fV  qW d S )N)get_tokens_unprocessed)_tv)r9   r@   r    r"   streamer   s    z"Lexer.get_tokens.<locals>.streamer)r>   r   r3   r   rA   ImportError_encoding_map
startswithlendecodeZdetectr7   rB   r0   stripr/   r2   
expandtabsr1   endswithr   r5   )r9   r@   Z
unfilteredrF   rA   ZdecodedZbomr3   encrI   streamr    )r9   r@   r"   
get_tokens   sL    	






zLexer.get_tokensc             C   s   t dS )z
        Return an iterable of (index, tokentype, value) pairs where "index"
        is the starting position of the token within the input text.

        In subclasses, implement this method as a generator to
        maximize effectiveness.
        N)NotImplementedError)r9   r@   r    r    r"   rE      s    zLexer.get_tokens_unprocessed)F)r+   r,   r-   r.   r(   aliases	filenamesZalias_filenamesZ	mimetypesZpriorityr;   r=   r8   r%   rT   rE   r    r    r    r"   r   3   s   
;c               @   s$   e Zd ZdZefddZdd ZdS )r   a   
    This lexer takes two lexer as arguments. A root lexer and
    a language lexer. First everything is scanned using the language
    lexer, afterwards all ``Other`` tokens are lexed using the root
    lexer.

    The lexers from the ``template`` lexer package use this base lexer.
    c             K   s0   |f || _ |f || _|| _tj| f| d S )N)
root_lexerlanguage_lexerneedler   r;   )r9   Z_root_lexerZ_language_lexerZ_needler6   r    r    r"   r;      s    zDelegatingLexer.__init__c             C   s   d}g }g }xX| j |D ]H\}}}|| jkrR|rH|t||f g }||7 }q||||f qW |r||t||f t|| j|S )N )rY   rE   rZ   r?   rM   do_insertionsrX   )r9   r@   Zbuffered
insertionsZ
lng_bufferirG   rH   r    r    r"   rE      s    

z&DelegatingLexer.get_tokens_unprocessedN)r+   r,   r-   r.   r   r;   rE   r    r    r    r"   r      s   c               @   s   e Zd ZdZdS )r   zI
    Indicates that a state should include rules from another state.
    N)r+   r,   r-   r.   r    r    r    r"   r      s   c               @   s   e Zd ZdZdd ZdS )_inheritzC
    Indicates the a state should inherit from its superclass.
    c             C   s   dS )Nr   r    )r9   r    r    r"   r=     s    z_inherit.__repr__N)r+   r,   r-   r.   r=   r    r    r    r"   r_      s   r_   c               @   s    e Zd ZdZdd Zdd ZdS )combinedz:
    Indicates a state combined from multiple states.
    c             G   s   t | |S )N)tupler'   )clsargsr    r    r"   r'     s    zcombined.__new__c             G   s   d S )Nr    )r9   rc   r    r    r"   r;     s    zcombined.__init__N)r+   r,   r-   r.   r'   r;   r    r    r    r"   r`   	  s   r`   c               @   sF   e Zd ZdZdd ZdddZdddZdd	d
Zdd Zdd Z	dS )_PseudoMatchz:
    A pseudo match object constructed from a string.
    c             C   s   || _ || _d S )N)_text_start)r9   startr@   r    r    r"   r;     s    z_PseudoMatch.__init__Nc             C   s   | j S )N)rf   )r9   argr    r    r"   rg     s    z_PseudoMatch.startc             C   s   | j t| j S )N)rf   rM   re   )r9   rh   r    r    r"   end"  s    z_PseudoMatch.endc             C   s   |rt d| jS )NzNo such group)
IndexErrorre   )r9   rh   r    r    r"   group%  s    z_PseudoMatch.groupc             C   s   | j fS )N)re   )r9   r    r    r"   groups*  s    z_PseudoMatch.groupsc             C   s   i S )Nr    )r9   r    r    r"   	groupdict-  s    z_PseudoMatch.groupdict)N)N)N)
r+   r,   r-   r.   r;   rg   ri   rk   rl   rm   r    r    r    r"   rd     s   


rd   c                 s   d fdd	}|S )zL
    Callback that yields multiple actions for each group in the match.
    Nc             3   s   xt  D ]\}}|d krq
q
t|tkrT||d }|r||d ||fV  q
||d }|d k	r
|r~||d |_x.|| t||d ||D ]}|r|V  qW q
W |r| |_d S )N   )	enumerater&   r	   rk   rg   posrd   ri   )lexermatchctxr^   actiondataitem)rc   r    r"   callback5  s"    zbygroups.<locals>.callback)Nr    )rc   rw   r    )rc   r"   r   1  s    c               @   s   e Zd ZdZdS )_ThiszX
    Special singleton used for indicating the caller class.
    Used by ``using``.
    N)r+   r,   r-   r.   r    r    r    r"   rx   K  s   rx   c                sj   i dkr: d}t|ttfr.|d< nd|fd<  tkrTdfdd	}nd	 fdd	}|S )
a  
    Callback that processes the match with a different lexer.

    The keyword arguments are forwarded to the lexer, except `state` which
    is handled separately.

    `state` specifies the state that the new lexer will start in, and can
    be an enumerable such as ('root', 'inline', 'string') or a simple
    string which is assumed to be on top of the root state.

    Note: For that to work, `_other` must not be an `ExtendedRegexLexer`.
    statestackrootNc             3   sn   r | j | jf }n| }| }x0|j| f D ]\}}}|| ||fV  q>W |rj| |_d S )N)updater6   r<   rg   rE   rk   ri   rp   )rq   rr   rs   lxsr^   rG   rH   )	gt_kwargskwargsr    r"   rw   i  s    zusing.<locals>.callbackc             3   sb    | j  f }| }x0|j| fD ]\}}}|| ||fV  q2W |r^| |_d S )N)r|   r6   rg   rE   rk   ri   rp   )rq   rr   rs   r}   r~   r^   rG   rH   )_otherr   r   r    r"   rw   x  s    
)N)N)popr>   listra   r   )r   r   r~   rw   r    )r   r   r   r"   r   S  s    


c               @   s   e Zd ZdZdd ZdS )r   z
    Indicates a state or state action (e.g. #pop) to apply.
    For example default('#pop') is equivalent to ('', Token, '#pop')
    Note that state tuples may be used as well.

    .. versionadded:: 2.0
    c             C   s
   || _ d S )N)ry   )r9   ry   r    r    r"   r;     s    zdefault.__init__N)r+   r,   r-   r.   r;   r    r    r    r"   r     s   c               @   s"   e Zd ZdZdddZdd ZdS )	r   z
    Indicates a list of literal words that is transformed into an optimized
    regex that matches any of the words.

    .. versionadded:: 2.0
    r[   c             C   s   || _ || _|| _d S )N)r   prefixsuffix)r9   r   r   r   r    r    r"   r;     s    zwords.__init__c             C   s   t | j| j| jdS )N)r   r   )r   r   r   r   )r9   r    r    r"   r7     s    z	words.getN)r[   r[   )r+   r,   r-   r.   r;   r7   r    r    r    r"   r     s   
c               @   sJ   e Zd ZdZdd Zdd Zdd Zdd	 ZdddZdd Z	dd Z
d
S )RegexLexerMetazw
    Metaclass for RegexLexer, creates the self._tokens attribute from
    self.tokens on the first instantiation.
    c             C   s    t |tr| }t||jS )zBPreprocess the regular expression component of a token definition.)r>   r   r7   recompilerr   )rb   regexrflagsry   r    r    r"   _process_regex  s    
zRegexLexerMeta._process_regexc             C   s&   t |tks"t|s"td|f |S )z5Preprocess the token component of a token definition.z2token type must be simple type or callable, not %r)r&   r	   callableAssertionError)rb   tokenr    r    r"   _process_token  s    zRegexLexerMeta._process_tokenc             C   s  t |trd|dkrdS ||kr$|fS |dkr0|S |dd dkrRt|dd  S dsbtd| nt |trd	| j }|  jd
7  _g }x4|D ],}||kstd| || ||| qW |||< |fS t |trx(|D ] }||ks|dkstd| qW |S dstd| dS )z=Preprocess the state transition action of a token definition.z#popz#pushN   z#pop:Fzunknown new state %rz_tmp_%drn   zcircular state ref %r)z#popz#pushzunknown new state zunknown new state def %r)	r>   strintr   r`   _tmpnameextend_process_statera   )rb   	new_stateunprocessed	processedZ	tmp_stateitokensZistater    r    r"   _process_new_state  s6    






z!RegexLexerMeta._process_new_statec             C   s  t |tkstd| |d dks0td| ||kr@|| S g  }||< | j}x:|| D ],}t|tr||kstd| || ||t| q^t|trq^t|t	r| 
|j||}|tdjd|f q^t |tkstd| y| |d ||}W n< tk
rF }	 ztd	|d || |	f W dd}	~	X Y nX | |d
 }
t|dkrjd}n| 
|d ||}|||
|f q^W |S )z%Preprocess a single state definition.zwrong state name %rr   #zinvalid state name %rzcircular state reference %rr[   Nzwrong rule def %rz+uncompilable regex %r in state %r of %r: %srn      )r&   r   r   flagsr>   r   r   r   r_   r   r   ry   r?   r   r   rr   ra   r   	Exception
ValueErrorr   rM   )rb   r   r   ry   tokensr   Ztdefr   rexerrr   r    r    r"   r     s>    


&
zRegexLexerMeta._process_stateNc             C   s@   i  }| j |< |p| j| }xt|D ]}| ||| q&W |S )z-Preprocess a dictionary of token definitions.)_all_tokensr   r   r   )rb   r(   	tokendefsr   ry   r    r    r"   process_tokendef  s
    zRegexLexerMeta.process_tokendefc       
   
   C   s   i }i }x| j D ]}|jdi }xt|D ]\}}||}|dkr~|||< y|t}W n tk
rr   w,Y nX |||< q,||d}|dkrq,||||d < y|t}	W n tk
r   Y q,X ||	 ||< q,W qW |S )a  
        Merge tokens from superclasses in MRO order, returning a single tokendef
        dictionary.

        Any state that is not defined by a subclass will be inherited
        automatically.  States that *are* defined by subclasses will, by
        default, override that state in the superclass.  If a subclass wishes to
        inherit definitions from a superclass, it can use the special value
        "inherit", which will cause the superclass' state definition to be
        included at that point in the state.
        r   Nrn   )__mro____dict__r7   r   indexr   r   r   )
rb   r   ZinheritablecZtoksry   itemsZcuritemsZinherit_ndxZnew_inh_ndxr    r    r"   get_tokendefs
  s0    
zRegexLexerMeta.get_tokendefsc             O   sL   d| j kr:i | _d| _t| dr(| jr(n| d|  | _tj	| f||S )z:Instantiate cls after preprocessing its token definitions._tokensr   token_variantsr[   )
r   r   r   hasattrr   r   r   r   r&   __call__)rb   rc   kwdsr    r    r"   r   ;  s    
zRegexLexerMeta.__call__)N)r+   r,   r-   r.   r   r   r   r   r   r   r   r    r    r    r"   r     s   #,
1r   c               @   s$   e Zd ZdZejZi ZdddZdS )r   z
    Base for simple stateful regular expression-based lexers.
    Simplifies the lexing process so that you need only
    provide a list of states and regular expressions.
    r{   c             c   s  d}| j }t|}||d  }xxz|D ]\}}}	|||}
|
r*|dk	rt|tkrj|||
 fV  nx|| |
D ]
}|V  qvW |
 }|	dk	r2t|	trx|	D ]8}|dkr|  q|dkr|	|d  q|	| qW nBt|	t
r||	d= n,|	dkr|	|d  nds&td|	 ||d  }P q*W yN|| dkrld	g}|d	 }|tdfV  |d
7 }w"|t|| fV  |d
7 }W q" tk
r   P Y q"X q"W dS )z}
        Split ``text`` into (tokentype, text) pairs.

        ``stack`` is the inital stack (default: ``['root']``)
        r   r   Nz#popz#pushFzwrong state def: %rrC   r{   rn   )r   r   r&   r	   rk   ri   r>   ra   r   r?   r   r   r   r   rj   )r9   r@   rz   rp   r   Z
statestackstatetokensrexmatchrt   r   mrv   ry   r    r    r"   rE   h  sP    







z!RegexLexer.get_tokens_unprocessedN)r   )	r+   r,   r-   r.   r   	MULTILINEr   r   rE   r    r    r    r"   r   I  s   c               @   s"   e Zd ZdZdddZdd ZdS )r   z9
    A helper object that holds lexer position data.
    Nc             C   s*   || _ || _|pt|| _|p"dg| _d S )Nr{   )r@   rp   rM   ri   rz   )r9   r@   rp   rz   ri   r    r    r"   r;     s    zLexerContext.__init__c             C   s   d| j | j| jf S )NzLexerContext(%r, %r, %r))r@   rp   rz   )r9   r    r    r"   r=     s    zLexerContext.__repr__)NN)r+   r,   r-   r.   r;   r=   r    r    r    r"   r     s   
c               @   s   e Zd ZdZdddZdS )r   zE
    A RegexLexer that uses a context object to store its state.
    Nc             c   s  | j }|st|d}|d }n|}||jd  }|j}xܐx|D ]<\}}}|||j|j}	|	rB|dk	rt|tkr|j||	 fV  |	 |_n.x|| |	|D ]
}
|
V  qW |s||jd  }|dk	r~t	|t
r&x|D ]B}|dkr|j  q|dkr|j|jd  q|j| qW nJt	|tr@|j|d= n0|dkr^|j|jd  ndsptd| ||jd  }P qBW yt|j|jkrP ||j d	krdg|_|d }|jtd	fV  | jd
7  _w:|jt||j fV  | jd
7  _W q: tk
r   P Y q:X q:W dS )z
        Split ``text`` into (tokentype, text) pairs.
        If ``context`` is given, use this lexer context instead.
        r   r{   r   Nz#popz#pushFzwrong state def: %rrC   rn   )r   r   rz   r@   rp   ri   r&   r	   rk   r>   ra   r   r?   r   r   r   r   rj   )r9   r@   contextr   rs   r   r   rt   r   r   rv   ry   r    r    r"   rE     s^    






z)ExtendedRegexLexer.get_tokens_unprocessed)NN)r+   r,   r-   r.   rE   r    r    r    r"   r     s   c          	   c   s  t | } yt| \}}W n( tk
r@   x|D ]
}|V  q.W dS X d}d}x|D ]\}}}	|dkrf|}d}
x|r|t|	 |kr|	|
||  }|||fV  |t|7 }x*|D ]"\}}}|||fV  |t|7 }qW || }
yt| \}}W ql tk
r   d}P Y qlX qlW |||	|
d fV  |t|	|
 7 }qPW xr|r|pJd}x,|D ]$\}}}	|||	fV  |t|	7 }qRW yt| \}}W n tk
r   d}P Y nX q<W dS )ag  
    Helper for lexers which must combine the results of several
    sublexers.

    ``insertions`` is a list of ``(index, itokens)`` pairs.
    Each ``itokens`` iterable should be inserted at position
    ``index`` into the token stream given by the ``tokens``
    argument.

    The result is a combined token stream.

    TODO: clean up the code here.
    NTr   F)iternextStopIterationrM   )r]   r   r   r   rv   ZrealposZinsleftr^   rG   rH   ZoldiZtmpvalZit_indexZit_tokenZit_valuepr    r    r"   r\     sL    


r\   c               @   s   e Zd ZdZdd ZdS )ProfilingRegexLexerMetaz>Metaclass for ProfilingRegexLexer, collects regex timing info.c                sL   t |tr t|j|j|jdn|t|tjf fdd	}|S )N)r   r   c                s`    j d fddg}t }| ||}t }|d  d7  < |d  || 7  < |S )Nr   r   g        rn   )
_prof_data
setdefaulttimerr   )r@   rp   endposinfoZt0ZresZt1)rb   compiledr   ry   r    r"   
match_func@  s    z:ProfilingRegexLexerMeta._process_regex.<locals>.match_func)	r>   r   r   r   r   r   r   sysmaxsize)rb   r   r   ry   r   r    )rb   r   r   ry   r"   r   8  s    

z&ProfilingRegexLexerMeta._process_regexN)r+   r,   r-   r.   r   r    r    r    r"   r   5  s   r   c               @   s"   e Zd ZdZg ZdZdddZdS )ProfilingRegexLexerzFDrop-in replacement for RegexLexer that does profiling of its regexes.   r{   c             #   s    j ji  xt ||D ]
}|V  qW  j j }tdd | D  fdddd}tdd |D }t	  t	d j j
t||f  t	d	 t	d
d  t	d x|D ]}t	d|  qW t	d	 d S )Nc             s   sN   | ]F\\}}\}}|t |d dddd |d| d| | fV  qdS )zu'z\\\NA   i  )reprrO   rB   ).0r~   rnrG   r    r    r"   	<genexpr>X  s   z=ProfilingRegexLexer.get_tokens_unprocessed.<locals>.<genexpr>c                s
   |  j  S )N)_prof_sort_index)r!   )r9   r    r"   r#   [  s    z<ProfilingRegexLexer.get_tokens_unprocessed.<locals>.<lambda>T)keyreversec             s   s   | ]}|d  V  qdS )   Nr    )r   r!   r    r    r"   r   ]  s    z2Profiling result for %s lexing %d chars in %.3f mszn==============================================================================================================z$%-20s %-64s ncalls  tottime  percall)ry   r   zn--------------------------------------------------------------------------------------------------------------z%-20s %-65s %5d %8.4f %8.4f)r<   r   r?   r   rE   r   sortedr   sumprintr+   rM   )r9   r@   rz   tokZrawdataru   Z	sum_totalr*   r    )r9   r"   rE   R  s$    



z*ProfilingRegexLexer.get_tokens_unprocessedN)r   )r+   r,   r-   r.   r   r   rE   r    r    r    r"   r   K  s   r   )9r.   Z
__future__r   r   r   r   Zpygments.filterr   r   Zpygments.filtersr   Zpygments.tokenr   r   r   r	   Zpygments.utilr
   r   r   r   r   r   r   r   r   Zpygments.regexoptr   __all__rK   staticmethodZ_default_analyser&   r$   objectr   r   r   r   r_   r   ra   r`   rd   r   rx   r   r   r   r   r   r   r   r   r\   r   r   r    r    r    r"   <module>
   sX   , '2 )WE?