o
    /h-                     @   sj   d Z ddgZddlZddlZddlZddlmZmZmZ ddl	m
Z
 i ZG dd dZd	d
 Zdd ZdS )zNew CSS Tokenizer (a generator)	TokenizerCSSProductions    N   )MACROSPRODUCTIONSr   )	normalizec                   @   s~   e Zd ZdZejejejejej	ej
dZdZedjZedjZddd	Zd
d Zdd Zdd Zdd ZdddZdS )r   z^
    generates a list of Token tuples:
        (Tokenname, value, startline, startcolumn)
    )z
@font-facez@importz@mediaz
@namespacez@pagez
@variables
z*\\[0-9a-fA-F]{1,6}(?:\r\n|[\t\r\n\f\x20])?z\\((\r\n)|[\n\r\f])NTc           	      C   s   t |trt| }n|}t||f}|tv r t| \}}}n*|s$t}|s(t}| | 	||}dd |D d }dd |D d }|||ft|< || _
|| _|| _|| _g | _dS )z
        inits tokenizer with given macros and productions which default to
        cssutils own macros and productions
        c                 S       g | ]}|d  dkr|d qS )r   COMMENTr    .0xr   r   F/var/www/html/myenv/lib/python3.10/site-packages/cssutils/tokenize2.py
<listcomp>5        z&Tokenizer.__init__.<locals>.<listcomp>r   c                 S   r	   )r   URIr   r   r   r   r   r   r   6   r   N)
isinstancedictsorteditemsstr_TOKENIZER_CACHEr   r   _compile_productions_expand_macrostokenmatchescommentmatcher
urimatcher_doComments_pushed)	selfmacrosproductions
doCommentsmacros_hash_keyhash_keyr   r   r   r   r   r   __init__!   s*   


zTokenizer.__init__c                    sV    fdd}g }|D ]\}}t d|r!t d||}t d|s|||f q
|S )z@returns macro expanded productions, order of productions is keptc                    s   d |   d   S )N(?:%s)macro)	groupdict)mr!   r   r   macro_valueC   s   z-Tokenizer._expand_macros.<locals>.macro_valuez{[a-zA-Z][a-zA-Z0-9-]*}z"{(?P<macro>[a-zA-Z][a-zA-Z0-9-]*)})researchsubappend)r    r!   r"   r,   expandedkeyvaluer   r+   r   r   @   s   zTokenizer._expand_macrosc                 C   s4   g }|D ]\}}| |td| tjjf q|S )z>compile productions into callable match objects, order is keptr'   )r0   r-   compileUmatch)r    expanded_productionscompiledr2   r3   r   r   r   r   O   s    zTokenizer._compile_productionsc                 G   s   t || j| _dS )z:Push back tokens which have been pulled but not processed.N)	itertoolschainr   )r    tokensr   r   r   pushV   s   zTokenizer.pushc                 C   s
   g | _ d S N)r   )r    r   r   r   clearZ   s   
zTokenizer.clearFc              	   #   s   dd   fdd}d }}d}j d j dd \}}}	|||}
|
r:|
d}||||fV  |t|7 }t||drVd}tj|||fV  |t|7 }|t|7 }t|}|}||k rjE dH  || }|d	v rd
|||fV  |d7 }|d7 }n*|	D ]&\}}|r|d
krt||drd||d  }|}
|
rjrd|||fV  |} n|||}
|
r|
d}|dkr|	 dkr|

dt|k r||

d dkrq|r!d|krt|||rd| |d  }}n.d|kr!d||kr!dD ]}||d  | }|}
|
rd|
d}} nq|dv r8 |}|dv r7d|}n5d|krkz	j|| }W n$ tyj   d|krft||t| drftj}|d7 }nd}Y nw |}jszjs|dkr||||fV  |t|7 }|j}||7 }|rt||jd }n|t|7 } nq||u sJ ||k sa|rdd||fV  dS dS )a;  Generator: Tokenize text and yield tokens, each token is a tuple
        of::

            (name, value, line, col)

        The token value will contain a normal string, meaning CSS unicode
        escapes have been resolved to normal characters. The serializer
        escapes needed characters back to unicode escapes depending on
        the stylesheet target encoding.

        text
            to be tokenized
        fullsheet
            if ``True`` appends EOF token as last one and completes incomplete
            COMMENT or INVALID (to STRING) tokens
        c                 S   s4   t | ddd d}|tjkrt|S | dS )zused by unicodesubr   r   N   )intgroupsys
maxunicodechr)r*   numr   r   r   _replo   s   

z!Tokenizer.tokenize.<locals>._replc                    s   t  | S )znormalize and do unicodesub)r   
unicodesub)r3   rF   r    r   r   
_normalizew   s   z&Tokenizer.tokenize.<locals>._normalizer   r   Nz	@charset z,:;{}>[]CHARz/*z%s*/r
   IDENTand(INVALIDSTRINGFUNCTIONzurl()z')z"))r   )		DIMENSIONrK   rO   r   HASHr
   rP   rN   zUNICODE-RANGE)rO   rN    	ATKEYWORDz@charset EOF)r   rA   lenhas_atr   CHARSET_SYMr   r   r   lowerend	suffix_eqr   rG   cleanstring_atkeywordsKeyErrorcount_lineseprfind)r    text	fullsheetrI   linecolposBOMmatcherr"   r6   found	_len_text
_orig_textcnamepossiblecommentr\   possibleurir3   nlsr   rH   r   tokenize]   s   













TozTokenizer.tokenize)NNT)F)__name__
__module____qualname____doc__r   FONT_FACE_SYM
IMPORT_SYM	MEDIA_SYMNAMESPACE_SYMPAGE_SYMVARIABLES_SYMr_   rb   r-   r4   r/   rG   r^   r&   r   r   r<   r>   rs   r   r   r   r   r      s$    
c                 C   s   | ||t |  |kS )a  Check if text has substring string as position pos.

    This is effectively equal to ``text[pos:pos+len(string)] == string``.

    If ``pos`` is 0, this is equal to ``text.startswith(string)``.

    :param str text: The text to check against.
    :param int pos: The starting position where the substring should be
                    searched.
    :param str string: The string to search.
    )rX   )rd   rh   stringr   r   r   rY     s   rY   c                    sB   s kS t  t  ko t fddtt  D S )ay  Check if the suffix of text starting at pos equals expected.

    This is effectively equivalent to ``text[pos:] == expected``, but avoids
    allocating a huge substring.

    If ``pos`` is 0, this is equal to ``text == expected``.

    :param str text: Text to compare.
    :param int pos: Starting position for the suffix.
    :param str expected: String to compare to.
    c                 3   s$    | ]}|   | kV  qd S r=   r   )r   iexpectedrh   rd   r   r   	<genexpr>"  s    
zsuffix_eq.<locals>.<genexpr>)rX   allrange)rd   rh   r   r   r   r   r]     s
   $
r]   )rw   __all__r9   r-   rB   cssproductionsr   r   r   helperr   r   r   rY   r]   r   r   r   r   <module>   s     w