
    xF                         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ej*                  j,                        Z G d de      Z G d de      Zy)zTokenize DNS master file format    )StringION   )long	text_typebinary_typeT) 	
;()"r                  c                       e Zd ZdZy)UngetBufferFullzDAn attempt was made to unget a token when the unget buffer was full.N)__name__
__module____qualname____doc__      lib/third_party/dns/tokenizer.pyr   r   0   s    Nr   r   c                   r    e Zd ZdZddZd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zy)TokenzA DNS master file format token.

    ttype: The token type
    value: The token value
    has_escape: Does the token value contain escapes?
    c                 .    || _         || _        || _        y)zInitialize a token instance.N)ttypevalue
has_escape)selfr    r!   r"   s       r   __init__zToken.__init__<   s     

$r   c                 (    | j                   t        k(  S N)r    EOFr#   s    r   is_eofzToken.is_eofC       zzS  r   c                 (    | j                   t        k(  S r&   )r    EOLr(   s    r   is_eolzToken.is_eolF   r*   r   c                 (    | j                   t        k(  S r&   )r    
WHITESPACEr(   s    r   is_whitespacezToken.is_whitespaceI       zzZ''r   c                 (    | j                   t        k(  S r&   )r    
IDENTIFIERr(   s    r   is_identifierzToken.is_identifierL   r1   r   c                 (    | j                   t        k(  S r&   )r    QUOTED_STRINGr(   s    r   is_quoted_stringzToken.is_quoted_stringO   s    zz]**r   c                 (    | j                   t        k(  S r&   )r    COMMENTr(   s    r   
is_commentzToken.is_commentR   s    zzW$$r   c                 (    | j                   t        k(  S r&   )r    	DELIMITERr(   s    r   is_delimiterzToken.is_delimiterU   s    zzY&&r   c                 R    | j                   t        k(  xs | j                   t        k(  S r&   )r    r,   r'   r(   s    r   is_eol_or_eofzToken.is_eol_or_eofX   s    zzS 5DJJ#$55r   c                     t        |t              sy| j                  |j                  k(  xr | j                  |j                  k(  S )NF
isinstancer   r    r!   r#   others     r   __eq__zToken.__eq__[   s9    %'

ekk) *

ekk)	+r   c                     t        |t              sy| j                  |j                  k7  xs | j                  |j                  k7  S )NTrA   rC   s     r   __ne__zToken.__ne__a   s9    %'

ekk) *

ekk)	+r   c                 8    d| j                   | j                  fz  S )Nz%d "%s")r    r!   r(   s    r   __str__zToken.__str__g   s    DJJ

333r   c                    | j                   s| S d}t        | j                        }d}||k  r;| j                  |   }|dz  }|dk(  r||k\  rt        j                  j
                  | j                  |   }|dz  }|j                         r||k\  rt        j                  j
                  | j                  |   }|dz  }||k\  rt        j                  j
                  | j                  |   }|dz  }|j                         r|j                         st        j                  j                  t        t        |      dz  t        |      dz  z   t        |      z         }||z  }||k  r;t        | j                  |      S )N r   r   \d   
   )r"   lenr!   dns	exceptionUnexpectedEndisdigitSyntaxErrorchrintr   r    )r#   	unescapedlicc2c3s          r   unescapezToken.unescapej   sG   K	

O!e

1AFADy6--555JJqMQ99;Av!mm999ABFAAv!mm999ABFAJJLRZZ\!mm777CFSL3r7R<7#b'ABANI) !e* TZZ++r   c                      y)Nr   r   r(   s    r   __len__zToken.__len__   s    r   c                 D    t        | j                  | j                  f      S r&   )iterr    r!   r(   s    r   __iter__zToken.__iter__   s    TZZ,--r   c                 R    |dk(  r| j                   S |dk(  r| j                  S t        )Nr   r   )r    r!   
IndexError)r#   rY   s     r   __getitem__zToken.__getitem__   s*    6::!V::r   N)rK   F)r   r   r   r   r$   r)   r-   r0   r4   r7   r:   r=   r?   rE   rG   rI   r]   r_   rb   re   r   r   r   r   r   4   sW    %!!((+%'6++4,>.r   r   c                       e Zd ZdZej
                  dfdZd Zd Zd Z	d Z
ddZd	 Zd
 ZeZd ZddZd ZddZd ZddZddZddZd Zd Zy)	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                 X   t        |t              rt        |      }|Jd}nGt        |t              rt        |j	                               }|d}n||t
        j                  u rd}nd}|| _        d| _        d| _	        d| _
        d| _        d| _        t        | _        d| _        || _        y)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   )rB   r   r   r   decodesysstdinfileungotten_charungotten_token	multilinequotingeof_DELIMITERS
delimitersline_numberfilename)r#   fru   s      r   r$   zTokenizer.__init__   s     a#A%;'$A%		>(H'H	!"% r   c                     | j                   U| j                  rd}|S | j                  j                  d      }|dk(  r	d| _        |S |dk(  r| xj                  dz  c_        |S | j                   }d| _         |S )z%Read a character from input.
        NrK   r   Tr
   )rm   rq   rl   readrt   r#   rZ   s     r   	_get_charzTokenizer._get_char   s     %xx  IINN1%7#DH  $Y$$)$  ""A!%Dr   c                 2    | j                   | j                  fS )zReturn 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.
        )ru   rt   r(   s    r   wherezTokenizer.where   s     t//00r   c                 6    | j                   t        || _         y)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)rm   r   ry   s     r   _unget_charzTokenizer._unget_char   s     )!!r   c                     d}	 | j                         }|dk7  r)|dk7  r$|dk7  s| j                  s| j                  |       |S |dz  }D)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   )rz   ro   r~   )r#   skippedrZ   s      r   skip_whitespacezTokenizer.skip_whitespace  sS      ACxAIIdnn$$Q'"NqLG r   c                    | j                   =| j                   }d| _         |j                         r|r|S |j                         r|r|S |S | j                         }|r|dkD  rt	        t
        d      S d}t        }d}	 | j                         }|dk(  s|| j                  v r|dk(  r&| j                  rt        j                  j                  |dk(  r|t        k7  r|dk(  r&| xj                  dz  c_        | j                          |d	k(  rO| j                  dk  rt        j                  j                  | xj                  dz  c_        | j                          |d
k(  rJ| j                  sd| _	        t         | _        t        }
d| _	        t"        | _        | j                          .|dk(  rt	        t$        d      S |dk(  r	 | j                         }|dk(  s|dk(  rn||z  }!|r!| j'                  |       t	        t(        |      S |dk(  r:| j                  rt        j                  j                  d      t	        t*              S | j                  r| j                          d}t	        t$        d      S |}t,        }n| j'                  |       n| j                  r5|dk(  r| j                         }|dk(  rt        j                  j                  |j/                         r/| j                         }|dk(  rt        j                  j                  | j                         }	|dk(  rt        j                  j                  |j/                         r|	j/                         st        j                  j                  t1        t3        |      dz  t3        |      dz  z   t3        |	      z         }nd|dk(  r_t        j                  j                  d      |dk(  r;||z  }d}| j                         }|dk(  s|dk(  rt        j                  j                  ||z  }|dk(  r:|t        k7  r1| j                  rt        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   rK   FTr   r   r   r   r
   r   zunbalanced parenthesesrL   rM   rN   znewline in quoted string)rn   r0   r:   r   r   r/   r3   rz   rs   rp   rP   rQ   rR   r6   ro   rT   _QUOTING_DELIMITERSrr   r,   r~   r9   r'   r<   rS   rU   rV   )
r#   want_leadingwant_commenttokenr   r    r"   rZ   r[   r\   s
             r   getzTokenizer.get  s     *''E"&D""$ L!!# L&&(GaKS))
 ABw!t.7t||--555B;5M#9Cx!+,,. c>>Q."%--";";;!+,,. c#||+/DL.ADO$1E$+0DL.9DO 002$d$S$//c $ 0A DyAG %!QJE	  
 ( ,,Q/#(%#88"W#~~&)mm&?&?$<'> !>#(:-!^^ 002$&E$#(d#33
 !" )$$Q'9(ABw!mm999yy{!^^-8"%--"="==!^^-7"%--"="== "

"%--";";;As2w| ;c"g EF$Y--334NOOd
 
!
NN$7a4i--555QJEk l B;5M1~~mm//0HIIEUE:..r   c                 6    | j                   t        || _         y)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)rn   r   r#   r   s     r   ungetzTokenizer.unget  s     *!!#r   c                 R    | j                         }|j                         rt        |S )zHReturn the next item in an iteration.

        Returns a Token.
        )r   r)   StopIterationr   s     r   nextzTokenizer.next  s#     
<<>r   c                     | S r&   r   r(   s    r   rb   zTokenizer.__iter__  s    r   c                 :   | j                         j                         }|j                         st        j                  j                  d      |j                  j                         st        j                  j                  d      t        |j                  |      S )zRead the next token and interpret it as an integer.

        Raises dns.exception.SyntaxError if not an integer.

        Returns an int.
        expecting an identifierexpecting an integer)	r   r]   r4   rP   rQ   rT   r!   rS   rV   )r#   baser   s      r   get_intzTokenizer.get_int  sq     
##%""$--++,EFF{{""$--++,BCC5;;%%r   c                 ~    | j                         }|dk  s|dkD  r"t        j                  j                  d|z        |S )zRead 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r   rP   rQ   rT   )r#   r!   s     r   	get_uint8zTokenizer.get_uint8  s@     19--++5=? ?r   c                     | j                  |      }|dk  s|dkD  rI|dk(  r"t        j                  j                  d|z        t        j                  j                  d|z        |S )zRead 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.
        )r   r   i     z*%o is not an octal unsigned 16-bit integerz$%d is not an unsigned 16-bit integerr   )r#   r   r!   s      r   
get_uint16zTokenizer.get_uint16  sq     $'19qymm//@5HJ J mm//:UBD Dr   c                    | j                         j                         }|j                         st        j                  j                  d      |j                  j                         st        j                  j                  d      t        |j                        }|dk  s|t        d      kD  r"t        j                  j                  d|z        |S )zRead 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.
        r   r   r   l        z$%d is not an unsigned 32-bit integer)	r   r]   r4   rP   rQ   rT   r!   rS   r   )r#   r   r!   s      r   
get_uint32zTokenizer.get_uint32  s     
##%""$--++,EFF{{""$--++,BCCU[[!19Z 00--++6>@ @r   c                     | j                         j                         }|j                         s/|j                         st        j
                  j                  d      |j                  S )zRead the next token and interpret it as a string.

        Raises dns.exception.SyntaxError if not a string.

        Returns a string.
        zexpecting a string)r   r]   r4   r7   rP   rQ   rT   r!   r#   originr   s      r   
get_stringzTokenizer.get_string  sO     
##%##%)?)?)A--++,@AA{{r   c                     | j                         j                         }|j                         st        j                  j                  d      |j                  S )zRead the next token, which should be an identifier.

        Raises dns.exception.SyntaxError if not an identifier.

        Returns a string.
        r   )r   r]   r4   rP   rQ   rT   r!   r   s      r   get_identifierzTokenizer.get_identifier  sD     
##%""$--++,EFF{{r   c                     | 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 if not a name.

        Returns a dns.name.Name.
        r   )r   r4   rP   rQ   rT   name	from_textr!   r   s      r   get_namezTokenizer.get_name  sL     
""$--++,EFFxx!!%++v66r   c                     | j                         }|j                         s8t        j                  j	                  d|j
                  |j                  fz        |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")r   r?   rP   rQ   rT   r    r!   r   s     r   get_eolzTokenizer.get_eol!  s\     
""$--++2ekk6;kk6C CD D {{r   c                     | j                         j                         }|j                         st        j                  j                  d      t        j                  j                  |j                        S )zRead 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.
        r   )	r   r]   r4   rP   rQ   rT   ttlr   r!   r   s     r   get_ttlzTokenizer.get_ttl/  sS     
##%""$--++,EFFww  --r   )FF)rN   r&   )r   r   r   r   rj   rk   r$   rz   r|   r~   r   r   r   r   __next__rb   r   r   r   r   r   r   r   r   r   r   r   r   rg   rg      sv    8 T !!F$1({/z$ 	 H
&&(7.r   rg   )r   ior   rj   dns.exceptionrP   dns.namedns.ttl_compatr   r   r   rr   r   r'   r,   r/   r3   r6   r9   r<   rQ   DNSExceptionr   objectr   rg   r   r   r   <module>r      s   $ &  
    1 1 


				 Dk 


	Ocmm00 OaF aHc. c.r   