B
    `a[|.                 @   s  d Z ddlZddlZedZedejejB ejB Zedej	ej
B ejB ejB ZedejZG dd deZG d	d
 d
eZd9ddZd:ddZd;ddZd<ddZdd Zdd Zdd Zdd Zdd Zi Zdd Zd d! Zd"d# Zd=d$d%Zd>d'd(Z G d)d* d*e!Z"d+d, Z#d-d. Z$d/d0 Z%ej&d1k r|e'Z'e(Z(e)e*fZ+e*Z,d2Z-e.j/Z/e.j0Z0ddl1Z1ddl2Z2e1j1Z1e2j1Z3nFe4Z'e5Z(e)fZ+e)Z,d3Z-e.j6Z/e.j7Z0dd4l8m1Z1m3Z3m9Z9 G d5d6 d6e9Z:d7d8 Z;dS )?z
    pygments.util
    ~~~~~~~~~~~~~

    Utility functions.

    :copyright: Copyright 2006-2017 by the Pygments team, see AUTHORS.
    :license: BSD, see LICENSE for details.
    Nz[/\\ ]z
    (<\?.*?\?>)?\s*
    <!DOCTYPE\s+(
     [a-zA-Z_][a-zA-Z0-9]*
     (?: \s+      # optional in HTML5
     [a-zA-Z_][a-zA-Z0-9]*\s+
     "[^"]*")?
     )
     [^>]*>
z<(.+?)(\s.*?)?>.*?</.+?>z\s*<\?xml[^>]*\?>c               @   s   e Zd ZdZdS )ClassNotFoundzCRaised if one of the lookup functions didn't find a matching class.N)__name__
__module____qualname____doc__ r   r   ,lib/python3.7/site-packages/pygments/util.pyr       s   r   c               @   s   e Zd ZdS )OptionErrorN)r   r   r   r   r   r   r   r	   $   s   r	   Fc             C   s@   |  ||}|r| }||kr<td|dtt|f |S )Nz%Value for option %s must be one of %sz, )getlowerr	   joinmapstr)optionsoptnameZalloweddefaultnormcasestringr   r   r   get_choice_opt(   s    r   c             C   s|   |  ||}t|tr|S t|tr,t|S t|tsHtd||f n0| dkrXdS | dkrhdS td||f d S )NzBInvalid type %r for option %s; use 1/0, yes/no, true/false, on/off)1ZyestrueZonT)0ZnoZfalseZoffFzCInvalid value %r for option %s; use 1/0, yes/no, true/false, on/off)r
   
isinstanceboolintstring_typesr	   r   )r   r   r   r   r   r   r   get_bool_opt2   s    


r   c             C   s`   |  ||}yt|S  tk
r8   td||f Y n$ tk
rZ   td||f Y nX d S )Nz=Invalid type %r for option %s; you must give an integer valuez>Invalid value %r for option %s; you must give an integer value)r
   r   	TypeErrorr	   
ValueError)r   r   r   r   r   r   r   get_int_optF   s    r   c             C   sH   |  ||}t|tr| S t|ttfr4t|S td||f d S )Nz9Invalid type %r for option %s; you must give a list value)r
   r   r   splitlisttupler	   )r   r   r   valr   r   r   get_list_optT   s    
r$   c             C   sR   | j s
dS g }x4| j   D ]"}| r>|d|   qP qW d| S )N  )r   strip
splitlinesappendr   lstrip)objZresliner   r   r   docstring_headline`   s    r-   c                s    fdd} j |_ t|S )zAReturn a static text analyser function that returns float values.c          	      s\   y | }W n t k
r    dS X |s*dS ytdtdt|S  ttfk
rV   dS X d S )Ng        g      ?)	Exceptionminmaxfloatr   r   )textrv)fr   r   text_analysen   s    z%make_analysator.<locals>.text_analyse)r   staticmethod)r4   r5   r   )r4   r   make_analysatorl   s    r7   c             C   s   |  d}|dkr$| d|  }n|  }|dry(dd t|dd  D d }W n tk
rr   d	S X td
| tj	}|
|dk	rdS d	S )a  Check if the given regular expression matches the last part of the
    shebang if one exists.

        >>> from pygments.util import shebang_matches
        >>> shebang_matches('#!/usr/bin/env python', r'python(2\.\d)?')
        True
        >>> shebang_matches('#!/usr/bin/python2.4', r'python(2\.\d)?')
        True
        >>> shebang_matches('#!/usr/bin/python-ruby', r'python(2\.\d)?')
        False
        >>> shebang_matches('#!/usr/bin/python/ruby', r'python(2\.\d)?')
        False
        >>> shebang_matches('#!/usr/bin/startsomethingwith python',
        ...                 r'python(2\.\d)?')
        True

    It also checks for common windows executable file extensions::

        >>> shebang_matches('#!C:\\Python2.4\\Python.exe', r'python(2\.\d)?')
        True

    Parameters (``'-f'`` or ``'--foo'`` are ignored so ``'perl'`` does
    the same as ``'perl -e'``)

    Note that this method automatically searches the whole string (eg:
    the regular expression is wrapped in ``'^$'``)
    
r   Nz#!c             S   s   g | ]}|r| d s|qS )-)
startswith).0xr   r   r   
<listcomp>   s    z#shebang_matches.<locals>.<listcomp>   Fz^%s(\.(exe|cmd|bat|bin))?$T)findr   r:   split_path_rer    r'   
IndexErrorrecompile
IGNORECASEsearch)r2   regexindexZ
first_linefoundr   r   r   shebang_matches}   s    


rJ   c             C   s<   t | }|dkrdS |d}t|tj| dk	S )zCheck if the doctype matches a regular expression (if present).

    Note that this method only checks the first part of a DOCTYPE.
    eg: 'html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"'
    NFr>   )doctype_lookup_rematchgrouprC   rD   Ir'   )r2   rG   mZdoctyper   r   r   doctype_matches   s
    

rP   c             C   s
   t | dS )z3Check if the file looks like it has a html doctype.Zhtml)rP   )r2   r   r   r   html_doctype_matches   s    rQ   c             C   sl   t | rdS t| }yt| S  tk
rf   t| }|dk	rDdS t| dd dk	}|t|< |S X dS )z2Check if a doctype exists or if we have some tags.TNi  )xml_decl_rerL   hash_looks_like_xml_cacheKeyErrorrK   tag_rerF   )r2   keyrO   r3   r   r   r   looks_like_xml   s    

rX   c             C   s   d| d?  d| d@  fS )Ni  
   i   i  r   )cr   r   r   _surrogatepair   s    r[   c             C   s,  || k rt d| dk s |dk r(t dtjdkrFdt| t|f S t| \}}t|\}}||krdt|t|t|f S g }|dt|t|||krt|ptdf  || d	kr|d
t|d	  t|d	 tdtd ||kr|dt|tdt|f  dd| d S dS )zEReturns a regular expression string to match the given non-BMP range.zBad character rangei   z+unirange is only defined for non-BMP rangesi  z[%s-%s]z(?:%s[%s-%s])z	%s[%s-%s]i     z[%s-%s][%s-%s]i   z(?:|)N)r   sys
maxunicodeunichrr[   r)   r   )abZahZalZbhZblZbufr   r   r   unirange   s*    
&
rd   c       	      C   s   g }d| d }d|d  d }| ||  d  |rXx\|D ]}| || d  q<W n<x:|D ]2}t|d }| ||dd  |d	  d  q^W | |d
  d|S )z)Formats a sequence of strings for output.r&      r\   z = (,"Nr?   r^   r8   )r)   reprr   )	Zvar_nameseqrawZindent_levellinesZbase_indentZinner_indentirr   r   r   format_lines  s    

&ro   r   c             C   sB   g }t  }x2| D ]*}||ks||kr&q|| || qW |S )za
    Returns a list with duplicates removed from the iterable `it`.

    Order is preserved.
    )setr)   add)itZalready_seenZlstseenrm   r   r   r   duplicates_removed  s    

rt   c               @   s   e Zd ZdZdd ZdS )FuturezGeneric class to defer some work.

    Handled specially in RegexLexerMeta, to support regex string construction at
    first use.
    c             C   s   t d S )N)NotImplementedError)selfr   r   r   r
   *  s    z
Future.getN)r   r   r   r   r
   r   r   r   r   ru   $  s   ru   c             C   st   y|  d} | dfS  tk
rn   y ddl}| }|   } | |fS  ttfk
rh   |  d} | dfS X Y nX dS )zDecode *text* with guessed encoding.

    First try UTF-8; this should fail for non-UTF-8 encodings.
    Then try the preferred locale encoding.
    Fall back to latin-1, which always works.
    zutf-8r   Nlatin1)decodeUnicodeDecodeErrorlocalegetpreferredencodingLookupError)r2   r{   Zprefencodingr   r   r   guess_decode.  s    

r~   c             C   sD   t |ddr<y| |j} W n tk
r0   Y nX | |jfS t| S )zDecode *text* coming from terminal *term*.

    First try the terminal encoding, if given.
    Then try UTF-8.  Then try the preferred locale encoding.
    Fall back to latin-1, which always works.
    encodingN)getattrry   r   rz   r~   )r2   termr   r   r   guess_decode_from_terminalC  s    
r   c             C   s"   t | ddr| jS ddl}| S )z7Return our best guess of encoding for the given *term*.r   Nr   )r   r   r{   r|   )r   r{   r   r   r   terminal_encodingT  s    r   )   r   ur%   )StringIOBytesIOTextIOWrapperc               @   s   e Zd Zdd ZdS )UnclosingTextIOWrapperc             C   s   |    d S )N)flush)rw   r   r   r   closew  s    zUnclosingTextIOWrapper.closeN)r   r   r   r   r   r   r   r   r   u  s   r   c                s    fdd}|S )z6Class decorator for creating a class with a metaclass.c                sR   | j  }|dd  |dd  x|ddD ]}|| q0W  | j| j|S )N__dict____weakref__	__slots__r   )r   copypopr
   r   	__bases__)clsZ	orig_varsZ	slots_var)	metaclassr   r   wrapper}  s    
zadd_metaclass.<locals>.wrapperr   )r   r   r   )r   r   add_metaclass{  s    r   )NF)N)N)N)Fr   )r   )<r   rC   r_   rD   rA   DOTALL	MULTILINEVERBOSErK   UNICODErE   rV   rN   rR   r   r   r.   r	   r   r   r   r$   r-   r7   rJ   rP   rQ   rT   rX   r[   rd   ro   rt   objectru   r~   r   r   version_infora   Zxranger   Zunicoder   Z	text_typeZu_prefixdictZ	iteritemsZ
itervaluesr   Z	cStringIOr   chrrangeitemsvaluesior   r   r   r   r   r   r   <module>
   sf   
	




-	'



