3 bWG@sdZddlmZddlZddlZddlZddlZddlm Z m Z m Z ddddddddZ ddiZ dZdZd Zd Zd Zd Zd ZGdddejjZGdddeZGdddeZdS)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@seZdZdZd%ddZddZdd Zd d Zd d ZddZ ddZ ddZ ddZ ddZ ddZddZddZddZd d!Zd"d#Zd$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 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)ttypevalue has_escape)selfrrrrrr__init__?s zToken.__init__cCs |jtkS)N)rEOF)rrrris_eofMsz Token.is_eofcCs |jtkS)N)rEOL)rrrris_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)rrrris_quoted_stringYszToken.is_quoted_stringcCs |jtkS)N)rCOMMENT)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|tsdS|j|jko$|j|jkS)NF) isinstancerrr)rotherrrr__eq__es  z Token.__eq__cCs&t|tsdS|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}|jr||krztjj|j|}|d7}||krtjj|j|}|d7}|jo|jstjjtt |dt |dt |}||7}qWt |j |S)Nrrr\d ) rlenrdns exception UnexpectedEndisdigit SyntaxErrorchrintrr)rZ unescapedlicc2c3rrrunescapets6      $ zToken.unescapecCsdS)Nrr)rrrr__len__sz Token.__len__cCst|j|jfS)N)iterrr)rrrr__iter__szToken.__iter__cCs$|dkr|jS|dkr|jStdS)Nrr)rr IndexError)rrArrr __getitem__s zToken.__getitem__N)rF)rrrrr r"r$r&r(r*r,r.r/r2r3r4rErFrHrJrrrrr3s"  rc@seZdZdZejdfddZddZddZd d Z d d Z d(ddZ ddZ ddZ e ZddZddZddZddZddZd)ddZd*d d!Zd+d"d#Zd$d%Zd&d'ZdS), TokenizeraA 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 NcCst|tr t|}|dkr`d}n@t|trDt|j}|dkr`d}n|dkr`|tjkr\d}nd}||_d|_d|_ d|_ d|_ d|_ t |_d|_||_dS)aInitialize 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)r0rrrdecodesysstdinfile ungotten_charungotten_token multilinequotingeof _DELIMITERS delimiters line_numberfilename)rfrXrrrr s*    zTokenizer.__init__cCsZ|jdkrJ|jrd}qV|jjd}|dkr2d|_qV|dkrV|jd7_n |j}d|_|S)z|j}d|_|jr(|r>|Sn|jr:|r>|Sn|S|j}|r\|dkr\ttdSd}t}d}x|j}|dks||jkr|dkr|j rt j j |dko|t kr|dkr|jd7_|jqlq |dkr |jdkrt j j|jd8_|jqln|d krH|j s0d |_ t|_t }qlnd|_ t|_|jqln|d kr\ttd S|d krx,|j}|d ks|dkrP||7}qhW|r|j|tt|S|dkr|jrt j jd ttS|jr|jd}qln ttd Sn|}t}n |j|Pn|j r|dkr|j}|dkr>t j j |jr|j}|dkrbt j j |j} |dkr|t j j |jo| jst j jtt|dt|dt| }n|d krt j jdn:|dkr||7}d }|j}|dks|d krt j j ||7}qlW|dkrH|t krH|jrDt 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_commenttokenr^rrrBrCrDrrrget%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)rrarrrungets zTokenizer.ungetcCs|j}|jrt|S)zLReturn the next item in an iteration. @rtype: (int, string) )rbr" StopIteration)rrarrrnextszTokenizer.nextcCs|S)Nr)rrrrrHszTokenizer.__iter__cCs@|jj}|js tjjd|jjs6tjjdt|jS)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?)rrarrrget_ints     zTokenizer.get_intcCs,|j}|dks|dkr(tjjd||S)zRead the next token and interpret it as an 8-bit unsigned integer. @raises dns.exception.SyntaxError: @rtype: int rz#%d is not an unsigned 8-bit integer)rfr9r:r=)rrrrr get_uint8s  zTokenizer.get_uint8cCs,|j}|dks|dkr(tjjd||S)zRead the next token and interpret it as a 16-bit unsigned integer. @raises dns.exception.SyntaxError: @rtype: int riz$%d is not an unsigned 16-bit integer)rfr9r:r=)rrrrr get_uint16s  zTokenizer.get_uint16cCsh|jj}|js tjjd|jjs6tjjdt|j}|dksT|tdkrdtjjd||S)zRead 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_uint32s      zTokenizer.get_uint32cCs.|jj}|jp|js(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)roriginrarrr get_strings  zTokenizer.get_stringcCs&|jj}|js tjjd|jS)zRead 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)rrkrarrrget_identifier s  zTokenizer.get_identifiercCs,|j}|jstjjdtjj|j|S)zRead 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)rrkrarrrget_names zTokenizer.get_namecCs.|j}|js(tjjd|j|jf|jS)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")rbr/r9r:r=rr)rrarrrget_eol!s  zTokenizer.get_eolcCs.|jj}|js tjjdtjj|jS)Nzexpecting an identifier) rbrEr(r9r:r=Zttlror)rrarrrget_ttl0s  zTokenizer.get_ttl)FF)N)N)N)rrrrrMrNr r[r\r]r_rbrcre__next__rHrfrhrirjrlrmrprqrrrrrrrKs($  {  rK)riorrMZ dns.exceptionr9Zdns.nameZdns.ttlZ_compatrrrrUr`r!r#r%r'r)r+r-r:Z DNSExceptionrobjectrrKrrrrs0 o