???<!-- GIF89;a -->
123123123123
.....................................................................................................................................???<!-- GIF89;a -->
123123123123
.....................................................................................................................................3
bWG                 @   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   /usr/lib/python3.6/tokenizer.pyr   .   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 )&TokenzA DNS master file format token.

    @ivar ttype: The token type
    @type ttype: int
    @ivar value: The token value
    @type value: string
    @ivar has_escape: Does the token value contain escapes?
    @type has_escape: bool
     Fc             C   s   || _ || _|| _dS )a  Initialize a token instance.

        @param ttype: The token type
        @type ttype: int
        @param value: The token value
        @type value: string
        @param has_escape: Does the token value contain escapes?
        @type has_escape: bool
        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_eofM   s    zToken.is_eofc             C   s
   | j tkS )N)r   EOL)r   r   r   r   is_eolP   s    zToken.is_eolc             C   s
   | j tkS )N)r   
WHITESPACE)r   r   r   r   is_whitespaceS   s    zToken.is_whitespacec             C   s
   | j tkS )N)r   
IDENTIFIER)r   r   r   r   is_identifierV   s    zToken.is_identifierc             C   s
   | j tkS )N)r   QUOTED_STRING)r   r   r   r   is_quoted_stringY   s    zToken.is_quoted_stringc             C   s
   | j tkS )N)r   COMMENT)r   r   r   r   
is_comment\   s    zToken.is_commentc             C   s
   | j tkS )N)r   	DELIMITER)r   r   r   r   is_delimiter_   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_eofb   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__e   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__k   s    
zToken.__ne__c             C   s   d| j | jf S )Nz%d "%s")r   r   )r   r   r   r   __str__q   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rtjj| j| }|d7 }|j o|j st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   unescapet   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   3   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 Zdd Z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 ),	Tokenizera  A DNS master file format tokenizer.

    A token is a (type, value) tuple, where I{type} is an int, and
    I{value} is a string.  The valid types are EOF, EOL, WHITESPACE,
    IDENTIFIER, QUOTED_STRING, COMMENT, and DELIMITER.

    @ivar file: The file to tokenize
    @type file: file
    @ivar ungotten_char: The most recently ungotten character, or None.
    @type ungotten_char: string
    @ivar ungotten_token: The most recently ungotten token, or None.
    @type ungotten_token: (int, string) token tuple
    @ivar 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.
    @type multiline: int
    @ivar quoting: This variable is true if the tokenizer is currently
    reading a quoted string.
    @type quoting: bool
    @ivar eof: This variable is true if the tokenizer has encountered EOF.
    @type eof: bool
    @ivar delimiters: The current delimiter dictionary.
    @type delimiters: dict
    @ivar line_number: The current line number
    @type line_number: int
    @ivar filename: A filename that will be returned by the L{where} method.
    @type filename: string
    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 )a  Initialize a tokenizer instance.

        @param 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.
        @type f: file or string
        @param filename: the name of the filename that the L{where} method
        will return.
        @type filename: string
        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.
        @rtype: string
        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 )zReturn the current location in the input.

        @rtype: (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 )aE  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.

        @param c: the character to unget
        @type c: string
        @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 )a(  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.

        @rtype: int
        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	rt
jj|dko|tkr|dkr|  jd7  _| j  qlq|dkr
| jdkrt
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rx,| j }|dks|dkrP ||7 }qhW |r| j| tt|S |dkr| jrt
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 st
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.

        @param want_leading: If True, return a WHITESPACE token if the
        first character read is whitespace.  The default is False.
        @type want_leading: bool
        @param want_comment: If True, return a COMMENT token if the
        first token read is a comment.  The default is False.
        @type want_comment: bool
        @rtype: Token object
        @raises dns.exception.UnexpectedEnd: input ended prematurely
        @raises dns.exception.SyntaxError: input was badly formed
        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.

        @param token: the token to unget
        @type token: Token object
        @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 )zLReturn the next item in an iteration.
        @rtype: (int, string)
        )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__c             C   s@   | 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:
        @rtype: int
        zexpecting an identifierzexpecting an integer)	rb   rE   r(   r9   r:   r=   r   r<   r?   )r   ra   r   r   r   get_int  s    
zTokenizer.get_intc             C   s,   | j  }|dk s|dkr(tjjd| |S )zRead the next token and interpret it as an 8-bit unsigned
        integer.

        @raises dns.exception.SyntaxError:
        @rtype: int
        r      z#%d is not an unsigned 8-bit integer)rf   r9   r:   r=   )r   r   r   r   r   	get_uint8  s
    
zTokenizer.get_uint8c             C   s,   | j  }|dk s|dkr(tjjd| |S )zRead the next token and interpret it as a 16-bit unsigned
        integer.

        @raises dns.exception.SyntaxError:
        @rtype: int
        r   i  z$%d is not an unsigned 16-bit integer)rf   r9   r:   r=   )r   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 )zRead the next token and interpret it as a 32-bit unsigned
        integer.

        @raises dns.exception.SyntaxError:
        @rtype: 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:
        @rtype: 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 )zRead the next token and raise an exception if it is not an identifier.

        @raises dns.exception.SyntaxError:
        @rtype: string
        zexpecting an identifier)rb   rE   r(   r9   r:   r=   r   )r   rk   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 )zRead the next token and interpret it as a DNS name.

        @raises dns.exception.SyntaxError:
        @rtype: dns.name.Name objectzexpecting an identifier)rb   r(   r9   r:   r=   name	from_textr   )r   rk   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 )zRead the next token and raise an exception if it isn't EOL or
        EOF.

        @raises dns.exception.SyntaxError:
        @rtype: 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 )Nzexpecting an identifier)	rb   rE   r(   r9   r:   r=   Zttlro   r   )r   ra   r   r   r   get_ttl0  s    zTokenizer.get_ttl)FF)N)N)N)r   r   r   r   rM   rN   r    r[   r\   r]   r_   rb   rc   re   __next__rH   rf   rh   ri   rj   rl   rm   rp   rq   rr   r   r   r   r   rK      s(   $	
{



rK   )r   ior   rM   Zdns.exceptionr9   Zdns.nameZdns.ttlZ_compatr   r   r   rU   r`   r!   r#   r%   r'   r)   r+   r-   r:   ZDNSExceptionr   objectr   rK   r   r   r   r   <module>   s0   o