3
[_ð]xF  ã               @   s´   d Z ddlmZ ddlZddlZddlZddlZddlm	Z	m
Z
mZ ddddddddœZddiZdZdZd	Zd
ZdZdZdZG dd„ dejjƒZG dd„ deƒZG dd„ deƒZdS )zTokenize DNS master file formaté    )ÚStringIONé   )ÚlongÚ	text_typeÚbinary_typeT)ú ú	Ú
ú;ú(ú)ú"r   é   é   é   é   é   c               @   s   e Zd ZdZdS )ÚUngetBufferFullzDAn attempt was made to unget a token when the unget buffer was full.N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   ú2/tmp/pip-build-yz_zf6az/dnspython/dns/tokenizer.pyr   0   s   r   c               @   s’   e Zd ZdZd%dd„Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zd"d#„ Zd$S )&ÚTokenz�A DNS master file format token.

    ttype: The token type
    value: The token value
    has_escape: Does the token value contain escapes?
    Ú Fc             C   s   || _ || _|| _dS )zInitialize a token instance.N)ÚttypeÚvalueÚ
has_escape)Úselfr   r   r   r   r   r   Ú__init__<   s    zToken.__init__c             C   s
   | j tkS )N)r   ÚEOF)r   r   r   r   Úis_eofC   s    zToken.is_eofc             C   s
   | j tkS )N)r   ÚEOL)r   r   r   r   Úis_eolF   s    zToken.is_eolc             C   s
   | j tkS )N)r   Ú
WHITESPACE)r   r   r   r   Úis_whitespaceI   s    zToken.is_whitespacec             C   s
   | j tkS )N)r   Ú
IDENTIFIER)r   r   r   r   Úis_identifierL   s    zToken.is_identifierc             C   s
   | j tkS )N)r   ÚQUOTED_STRING)r   r   r   r   Úis_quoted_stringO   s    zToken.is_quoted_stringc             C   s
   | j tkS )N)r   ÚCOMMENT)r   r   r   r   Ú
is_commentR   s    zToken.is_commentc             C   s
   | j tkS )N)r   Ú	DELIMITER)r   r   r   r   Úis_delimiterU   s    zToken.is_delimiterc             C   s   | j tkp| j tkS )N)r   r#   r!   )r   r   r   r   Úis_eol_or_eofX   s    zToken.is_eol_or_eofc             C   s&   t |tƒsdS | j|jko$| j|jkS )NF)Ú
isinstancer   r   r   )r   Úotherr   r   r   Ú__eq__[   s    
zToken.__eq__c             C   s&   t |tƒsdS | j|jkp$| j|jkS )NT)r0   r   r   r   )r   r1   r   r   r   Ú__ne__a   s    
zToken.__ne__c             C   s   d| j | jf S )Nz%d "%s")r   r   )r   r   r   r   Ú__str__g   s    zToken.__str__c             C   s  | j s
| S d}t| jƒ}d}xØ||k rô| j| }|d7 }|dkrê||krPtjj‚| j| }|d7 }|jƒ rê||krztjj‚| j| }|d7 }||krœtjj‚| j| }|d7 }|jƒ o¼|jƒ sÆtjj‚tt	|ƒd t	|ƒd  t	|ƒ ƒ}||7 }qW t
| j|ƒS )Nr   r   r   ú\éd   é
   )r   Úlenr   ÚdnsÚ	exceptionÚUnexpectedEndÚisdigitÚSyntaxErrorÚchrÚintr   r   )r   Z	unescapedÚlÚiÚcÚc2Úc3r   r   r   Úunescapej   s6    





$zToken.unescapec             C   s   dS )Nr   r   )r   r   r   r   Ú__len__‰   s    zToken.__len__c             C   s   t | j| jfƒS )N)Úiterr   r   )r   r   r   r   Ú__iter__Œ   s    zToken.__iter__c             C   s$   |dkr| j S |dkr| jS t‚d S )Nr   r   )r   r   Ú
IndexError)r   rA   r   r   r   Ú__getitem__�   s
    zToken.__getitem__N)r   F)r   r   r   r   r    r"   r$   r&   r(   r*   r,   r.   r/   r2   r3   r4   rE   rF   rH   rJ   r   r   r   r   r   4   s"   
r   c               @   s¸   e Zd ZdZejdfdd„Zdd„ Zdd„ Zd	d
„ Z	dd„ Z
d)dd„Zdd„ Zdd„ ZeZdd„ Zd*dd„Zdd„ Zd+d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 )/Ú	Tokenizerad  A DNS master file format tokenizer.

    A token object is basically a (type, value) tuple.  The valid
    types are EOF, EOL, WHITESPACE, IDENTIFIER, QUOTED_STRING,
    COMMENT, and DELIMITER.

    file: The file to tokenize

    ungotten_char: The most recently ungotten character, or None.

    ungotten_token: The most recently ungotten token, or None.

    multiline: The current multiline level.  This value is increased
    by one every time a '(' delimiter is read, and decreased by one every time
    a ')' delimiter is read.

    quoting: This variable is true if the tokenizer is currently
    reading a quoted string.

    eof: This variable is true if the tokenizer has encountered EOF.

    delimiters: The current delimiter dictionary.

    line_number: The current line number

    filename: A filename that will be returned by the where() method.
    Nc             C   sš   t |tƒr t|ƒ}|dkr`d}n@t |tƒrDt|jƒ ƒ}|dkr`d}n|dkr`|tjkr\d}nd}|| _d| _d| _	d| _
d| _d| _t| _d| _|| _dS )aE  Initialize a tokenizer instance.

        f: The file to tokenize.  The default is sys.stdin.
        This parameter may also be a string, in which case the tokenizer
        will take its input from the contents of the string.

        filename: the name of the filename that the where() method
        will return.
        Nz<string>z<stdin>z<file>r   Fr   )r0   r   r   r   ÚdecodeÚsysÚstdinÚfileÚungotten_charÚungotten_tokenÚ	multilineÚquotingÚeofÚ_DELIMITERSÚ
delimitersÚline_numberÚfilename)r   ÚfrX   r   r   r   r    µ   s*    


zTokenizer.__init__c             C   sZ   | j dkrJ| jrd}qV| jjdƒ}|dkr2d| _qV|dkrV|  jd7  _n| j }d| _ |S )z%Read a character from input.
        Nr   r   Tr	   )rP   rT   rO   ÚreadrW   )r   rB   r   r   r   Ú	_get_charØ   s    
zTokenizer._get_charc             C   s   | j | jfS )z·Return the current location in the input.

        Returns a (string, int) tuple.  The first item is the filename of
        the input, the second is the current line number.
        )rX   rW   )r   r   r   r   Úwhereê   s    zTokenizer.wherec             C   s   | j dk	rt‚|| _ dS )a%  Unget a character.

        The unget buffer for characters is only one character large; it is
        an error to try to unget a character when the unget buffer is not
        empty.

        c: the character to unget
        raises UngetBufferFull: there is already an ungotten char
        N)rP   r   )r   rB   r   r   r   Ú_unget_charó   s    
zTokenizer._unget_charc             C   sL   d}xB| j ƒ }|dkr<|dkr<|dks.| j r<| j|ƒ |S |d7 }qW dS )aF  Consume input until a non-whitespace character is encountered.

        The non-whitespace character is then ungotten, and the number of
        whitespace characters consumed is returned.

        If the tokenizer is in multiline mode, then newlines are whitespace.

        Returns the number of characters skipped.
        r   r   r   r	   r   N)r[   rR   r]   )r   ÚskippedrB   r   r   r   Úskip_whitespace  s    
zTokenizer.skip_whitespaceFc       
      C   sT  | j dk	r>| j }d| _ |jƒ r(|r>|S n|jƒ r:|r>|S n|S | jƒ }|r\|dkr\ttdƒS d}t}d}�x°| jƒ }|dksˆ|| jk�r|dkrž| j	ržt
jj‚|dko¬|tk�r|dkrÔ|  jd7  _| jƒ  ql�q|dk�r
| jdkrðt
jj‚|  jd8  _| jƒ  qlnö|d	k�rH| j	�s0d
| _	t| _t}qlnd| _	t| _| jƒ  qln¸|dk�r\ttdƒS |dk�røx,| jƒ }|dk�s„|dk�r†P ||7 }�qhW |�r®| j|ƒ tt|ƒS |dk�rÔ| j�rÌt
jjdƒ‚ttƒS | j�rì| jƒ  d}qln
ttdƒS n|}t}n
| j|ƒ P � nþ| j	�rÖ|dk�r¾| jƒ }|dk�r>t
jj‚|jƒ �rÔ| jƒ }|dk�rbt
jj‚| jƒ }	|dk�r|t
jj‚|jƒ �oŒ|	jƒ �s˜t
jj‚tt|ƒd t|ƒd  t|	ƒ ƒ}n|dk�rt
jjdƒ‚n:|dk�r||7 }d
}| jƒ }|dk�s|dk�rt
jj‚||7 }qlW |dk�rH|tk�rH| j�rDt
jjdƒ‚t}t|||ƒS )a½  Get the next token.

        want_leading: If True, return a WHITESPACE token if the
        first character read is whitespace.  The default is False.

        want_comment: If True, return a COMMENT token if the
        first token read is a comment.  The default is False.

        Raises dns.exception.UnexpectedEnd: input ended prematurely

        Raises dns.exception.SyntaxError: input was badly formed

        Returns a Token.
        Nr   r   r   Fr   r   r   r   Tr	   r
   zunbalanced parenthesesr5   r6   r7   znewline in quoted string)rQ   r&   r,   r_   r   r%   r'   r[   rV   rS   r9   r:   r;   r)   rR   r=   Ú_QUOTING_DELIMITERSrU   r#   r]   r+   r!   r-   r<   r>   r?   )
r   Zwant_leadingZwant_commentÚtokenr^   r   r   rB   rC   rD   r   r   r   Úget  sÀ    
















&

zTokenizer.getc             C   s   | j dk	rt‚|| _ dS )a  Unget a token.

        The unget buffer for tokens is only one token large; it is
        an error to try to unget a token when the unget buffer is not
        empty.

        token: the token to unget

        Raises UngetBufferFull: there is already an ungotten token
        N)rQ   r   )r   ra   r   r   r   Úunget“  s    
zTokenizer.ungetc             C   s   | j ƒ }|jƒ rt‚|S )zHReturn the next item in an iteration.

        Returns a Token.
        )rb   r"   ÚStopIteration)r   ra   r   r   r   Únext£  s    zTokenizer.nextc             C   s   | S )Nr   )r   r   r   r   rH   °  s    zTokenizer.__iter__r7   c             C   sB   | j ƒ jƒ }|jƒ s tjjdƒ‚|jjƒ s6tjjdƒ‚t|j|ƒS )z’Read the next token and interpret it as an integer.

        Raises dns.exception.SyntaxError if not an integer.

        Returns an int.
        zexpecting an identifierzexpecting an integer)	rb   rE   r(   r9   r:   r=   r   r<   r?   )r   Úbasera   r   r   r   Úget_intµ  s    
zTokenizer.get_intc             C   s,   | j ƒ }|dk s|dkr(tjjd| ƒ‚|S )z¸Read the next token and interpret it as an 8-bit unsigned
        integer.

        Raises dns.exception.SyntaxError if not an 8-bit unsigned integer.

        Returns an int.
        r   éÿ   z#%d is not an unsigned 8-bit integer)rg   r9   r:   r=   )r   r   r   r   r   Ú	get_uint8Ä  s
    	
zTokenizer.get_uint8c             C   sJ   | j |d�}|dk s|dkrF|dkr6tjjd| ƒ‚ntjjd| ƒ‚|S )z¸Read the next token and interpret it as a 16-bit unsigned
        integer.

        Raises dns.exception.SyntaxError if not a 16-bit unsigned integer.

        Returns an int.
        )rf   r   iÿÿ  é   z*%o is not an octal unsigned 16-bit integerz$%d is not an unsigned 16-bit integer)rg   r9   r:   r=   )r   rf   r   r   r   r   Ú
get_uint16Ó  s    	
zTokenizer.get_uint16c             C   sh   | j ƒ jƒ }|jƒ s tjjdƒ‚|jjƒ s6tjjdƒ‚t|jƒ}|dk sT|tdƒkrdtjjd| ƒ‚|S )z¸Read the next token and interpret it as a 32-bit unsigned
        integer.

        Raises dns.exception.SyntaxError if not a 32-bit unsigned integer.

        Returns an int.
        zexpecting an identifierzexpecting an integerr   l        z$%d is not an unsigned 32-bit integer)	rb   rE   r(   r9   r:   r=   r   r<   r   )r   ra   r   r   r   r   Ú
get_uint32æ  s    	


zTokenizer.get_uint32c             C   s.   | j ƒ jƒ }|jƒ p|jƒ s(tjjdƒ‚|jS )z�Read the next token and interpret it as a string.

        Raises dns.exception.SyntaxError if not a string.

        Returns a string.
        zexpecting a string)rb   rE   r(   r*   r9   r:   r=   r   )r   Úoriginra   r   r   r   Ú
get_stringú  s    zTokenizer.get_stringc             C   s&   | j ƒ jƒ }|jƒ s tjjdƒ‚|jS )z—Read the next token, which should be an identifier.

        Raises dns.exception.SyntaxError if not an identifier.

        Returns a string.
        zexpecting an identifier)rb   rE   r(   r9   r:   r=   r   )r   rm   ra   r   r   r   Úget_identifier  s    zTokenizer.get_identifierc             C   s,   | j ƒ }|jƒ stjjdƒ‚tjj|j|ƒS )z—Read the next token and interpret it as a DNS name.

        Raises dns.exception.SyntaxError if not a name.

        Returns a dns.name.Name.
        zexpecting an identifier)rb   r(   r9   r:   r=   ÚnameÚ	from_textr   )r   rm   ra   r   r   r   Úget_name  s    zTokenizer.get_namec             C   s.   | j ƒ }|jƒ s(tjjd|j|jf ƒ‚|jS )znRead the next token and raise an exception if it isn't EOL or
        EOF.

        Returns a string.
        z expected EOL or EOF, got %d "%s")rb   r/   r9   r:   r=   r   r   )r   ra   r   r   r   Úget_eol!  s    zTokenizer.get_eolc             C   s.   | j ƒ jƒ }|jƒ s tjjdƒ‚tjj|jƒS )z¾Read the next token and interpret it as a DNS TTL.

        Raises dns.exception.SyntaxError or dns.ttl.BadTTL if not an
        identifier or badly formed.

        Returns an int.
        zexpecting an identifier)	rb   rE   r(   r9   r:   r=   Zttlrq   r   )r   ra   r   r   r   Úget_ttl/  s    	zTokenizer.get_ttl)FF)r7   )r7   )N)N)N)r   r   r   r   rM   rN   r    r[   r\   r]   r_   rb   rc   re   Ú__next__rH   rg   ri   rk   rl   rn   ro   rr   rs   rt   r   r   r   r   rK   ˜   s(   #	
}




rK   )r   Úior   rM   Zdns.exceptionr9   Zdns.nameZdns.ttlÚ_compatr   r   r   rU   r`   r!   r#   r%   r'   r)   r+   r-   r:   ZDNSExceptionr   Úobjectr   rK   r   r   r   r   Ú<module>   s0   d