3 ¢báW‰Gã@s´dZddlmZddlZddlZddlZddlZddlm Z m Z m Z ddddddddœZ ddiZ dZdZd Zd Zd Zd Zd ZGdd„dejjƒZGdd„deƒZGdd„deƒZdS)zTokenize DNS master file formaté)ÚStringIONé)ÚlongÚ text_typeÚ binary_typeT)ú ú Ú ú;ú(ú)ú"r éééééc@seZdZdZdS)ÚUngetBufferFullzDAn attempt was made to unget a token when the unget buffer was full.N)Ú__name__Ú __module__Ú __qualname__Ú__doc__©rrú/usr/lib/python3.6/tokenizer.pyr.src@s’eZdZdZd%dd„Zdd„Zdd „Zd d „Zd d „Zdd„Z dd„Z dd„Z dd„Z dd„Z dd„Zdd„Zdd„Zdd„Zd d!„Zd"d#„Zd$S)&ÚTokenzêA 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 ÚFcCs||_||_||_dS)aInitialize 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)ÚselfrrrrrrÚ__init__?s zToken.__init__cCs |jtkS)N)rÚEOF)rrrrÚis_eofMsz Token.is_eofcCs |jtkS)N)rÚEOL)rrrrÚis_eolPsz Token.is_eolcCs |jtkS)N)rÚ WHITESPACE)rrrrÚ is_whitespaceSszToken.is_whitespacecCs |jtkS)N)rÚ IDENTIFIER)rrrrÚ is_identifierVszToken.is_identifiercCs |jtkS)N)rÚ QUOTED_STRING)rrrrÚis_quoted_stringYszToken.is_quoted_stringcCs |jtkS)N)rÚCOMMENT)rrrrÚ is_comment\szToken.is_commentcCs |jtkS)N)rÚ DELIMITER)rrrrÚ is_delimiter_szToken.is_delimitercCs|jtkp|jtkS)N)rr#r!)rrrrÚ is_eol_or_eofbszToken.is_eol_or_eofcCs&t|tƒsdS|j|jko$|j|jkS)NF)Ú isinstancerrr)rÚotherrrrÚ__eq__es  z Token.__eq__cCs&t|tƒsdS|j|jkp$|j|jkS)NT)r0rrr)rr1rrrÚ__ne__ks  z Token.__ne__cCsd|j|jfS)Nz%d "%s")rr)rrrrÚ__str__qsz Token.__str__cCs|js |Sd}t|jƒ}d}xØ||krô|j|}|d7}|dkrê||krPtjj‚|j|}|d7}|jƒrê||krztjj‚|j|}|d7}||krœtjj‚|j|}|d7}|jƒo¼|jƒsÆtjj‚tt |ƒdt |ƒdt |ƒƒ}||7}qWt |j |ƒS)Nrrrú\édé ) rÚlenrÚdnsÚ exceptionÚ UnexpectedEndÚisdigitÚ SyntaxErrorÚchrÚintrr)rZ unescapedÚlÚiÚcÚc2Úc3rrrÚunescapets6      $ zToken.unescapecCsdS)Nrr)rrrrÚ__len__“sz Token.__len__cCst|j|jfƒS)N)Úiterrr)rrrrÚ__iter__–szToken.__iter__cCs$|dkr|jS|dkr|jSt‚dS)Nrr)rrÚ IndexError)rrArrrÚ __getitem__™s zToken.__getitem__N)rF)rrrrr r"r$r&r(r*r,r.r/r2r3r4rErFrHrJrrrrr3s"  rc@s´eZdZdZejdfdd„Zdd„Zdd„Zd d „Z d d „Z d(dd„Z dd„Z dd„Z e Zdd„Zdd„Zdd„Zdd„Zdd„Zd)dd„Zd*d d!„Zd+d"d#„Zd$d%„Zd&d'„ZdS),Ú 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 NcCsšt|tƒr t|ƒ}|dkr`d}n@t|tƒrDt|jƒƒ}|dkr`d}n|dkr`|tjkr\d}nd}||_d|_d|_ d|_ d|_ d|_ t |_d|_||_dS)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 NzzzrFr)r0rrrÚdecodeÚsysÚstdinÚfileÚ ungotten_charÚungotten_tokenÚ multilineÚquotingÚeofÚ _DELIMITERSÚ delimitersÚ line_numberÚfilename)rÚfrXrrrr Ás*    zTokenizer.__init__cCsZ|jdkrJ|jrd}qV|jjdƒ}|dkr2d|_qV|dkrV|jd7_n |j}d|_|S)z|j}d|_|jƒr(|r>|Sn|jƒr:|r>|Sn|S|jƒ}|r\|dkr\ttdƒSd}t}d}�x°|jƒ}|dksˆ||jk�r|dkrž|j ržt j j ‚|dko¬|t k�r|dkrÔ|jd7_|jƒql�q |dk�r |jdkrðt j j‚|jd8_|jƒqlnö|d k�rH|j �s0d |_ t|_t }qlnd|_ t|_|jƒqln¸|d k�r\ttd ƒS|d k�røx,|jƒ}|d k�s„|dk�r†P||7}�qhW|�r®|j|ƒtt|ƒS|dk�rÔ|j�rÌt j jd ƒ‚ttƒS|j�rì|jƒd}qln ttd ƒSn|}t}n |j|ƒP�nþ|j �rÖ|dk�r¾|jƒ}|dk�r>t j j ‚|jƒ�rÔ|jƒ}|dk�rbt j j ‚|jƒ} |dk�r|t j j ‚|jƒ�oŒ| jƒ�s˜t j j‚tt|ƒdt|ƒdt| ƒƒ}n|d k�rt j jdƒ‚n:|dk�r||7}d }|jƒ}|dk�s|d k�rt j j ‚||7}qlW|dk�rH|t k�rH|j�rDt j jd ƒ‚t}t|||ƒS)aGet 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 NrrrFr rr r Tr r zunbalanced parenthesesr5r6r7znewline in quoted string)rQr&r,r_rr%r'r[rVrSr9r:r;r)rRr=Ú_QUOTING_DELIMITERSrUr#r]r+r!r-r<r>r?) rZ want_leadingZ want_commentÚtokenr^rrrBrCrDrrrÚget%sÀ                   &    z Tokenizer.getcCs|jdk rt‚||_dS)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)rQr)rrarrrÚunget s zTokenizer.ungetcCs|jƒ}|jƒrt‚|S)zLReturn the next item in an iteration. @rtype: (int, string) )rbr"Ú StopIteration)rrarrrÚnext°szTokenizer.nextcCs|S)Nr)rrrrrH¼szTokenizer.__iter__cCs@|jƒjƒ}|jƒs tjjdƒ‚|jjƒs6tjjdƒ‚t|jƒS)z|Read the next token and interpret it as an integer. @raises dns.exception.SyntaxError: @rtype: int zexpecting an identifierzexpecting an integer) rbrEr(r9r:r=rr<r?)rrarrrÚget_intÁs     zTokenizer.get_intcCs,|jƒ}|dks|dkr(tjjd|ƒ‚|S)z“Read 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)rfr9r:r=)rrrrrÚ get_uint8Ïs  zTokenizer.get_uint8cCs,|jƒ}|dks|dkr(tjjd|ƒ‚|S)z“Read the next token and interpret it as a 16-bit unsigned integer. @raises dns.exception.SyntaxError: @rtype: int riÿÿz$%d is not an unsigned 16-bit integer)rfr9r:r=)rrrrrÚ get_uint16Ýs  zTokenizer.get_uint16cCsh|jƒjƒ}|jƒs tjjdƒ‚|jjƒs6tjjdƒ‚t|jƒ}|dksT|tdƒkrdtjjd|ƒ‚|S)z“Read the next token and interpret it as a 32-bit unsigned integer. @raises dns.exception.SyntaxError: @rtype: int zexpecting an identifierzexpecting an integerrlz$%d is not an unsigned 32-bit integer) rbrEr(r9r:r=rr<r)rrarrrrÚ get_uint32ës      zTokenizer.get_uint32cCs.|jƒjƒ}|jƒp|jƒs(tjjdƒ‚|jS)z}Read the next token and interpret it as a string. @raises dns.exception.SyntaxError: @rtype: string zexpecting a string)rbrEr(r*r9r:r=r)rÚoriginrarrrÚ get_stringþs  zTokenizer.get_stringcCs&|jƒjƒ}|jƒs tjjdƒ‚|jS)z’Read the next token and raise an exception if it is not an identifier. @raises dns.exception.SyntaxError: @rtype: string zexpecting an identifier)rbrEr(r9r:r=r)rrkrarrrÚget_identifier s  zTokenizer.get_identifiercCs,|jƒ}|jƒstjjdƒ‚tjj|j|ƒS)z„Read the next token and interpret it as a DNS name. @raises dns.exception.SyntaxError: @rtype: dns.name.Name objectzexpecting an identifier)rbr(r9r:r=ÚnameÚ from_textr)rrkrarrrÚget_names zTokenizer.get_namecCs.|jƒ}|jƒs(tjjd|j|jfƒ‚|jS)z–Read 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")rbr/r9r:r=rr)rrarrrÚget_eol!s  zTokenizer.get_eolcCs.|jƒjƒ}|jƒs tjjdƒ‚tjj|jƒS)Nzexpecting an identifier) rbrEr(r9r:r=Zttlror)rrarrrÚget_ttl0s  zTokenizer.get_ttl)FF)N)N)N)rrrrrMrNr r[r\r]r_rbrcreÚ__next__rHrfrhrirjrlrmrprqrrrrrrrK¢s($  {  rK)rÚiorrMZ dns.exceptionr9Zdns.nameZdns.ttlZ_compatrrrrUr`r!r#r%r'r)r+r-r:Z DNSExceptionrÚobjectrrKrrrrÚs0 o