<s> """<STR_LIT>""" <EOL> import cStringIO <EOL> import sys <EOL> import dns . exception <EOL> import dns . name <EOL> import dns . ttl <EOL> _DELIMITERS = { <EOL> '<STR_LIT:U+0020>' : True , <EOL> '<STR_LIT:\t>' : True , <EOL> '<STR_LIT:\n>' : True , <EOL> '<STR_LIT:;>' : True , <EOL> '<STR_LIT:(>' : True , <EOL> '<STR_LIT:)>' : True , <EOL> '<STR_LIT:">' : True } <EOL> _QUOTING_DELIMITERS = { '<STR_LIT:">' : True } <EOL> EOF = <NUM_LIT:0> <EOL> EOL = <NUM_LIT:1> <EOL> WHITESPACE = <NUM_LIT:2> <EOL> IDENTIFIER = <NUM_LIT:3> <EOL> QUOTED_STRING = <NUM_LIT:4> <EOL> COMMENT = <NUM_LIT:5> <EOL> DELIMITER = <NUM_LIT:6> <EOL> class UngetBufferFull ( dns . exception . DNSException ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class Token ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , ttype , value = '<STR_LIT>' , has_escape = False ) : <EOL> """<STR_LIT>""" <EOL> self . ttype = ttype <EOL> self . value = value <EOL> self . has_escape = has_escape <EOL> def is_eof ( self ) : <EOL> return self . ttype == EOF <EOL> def is_eol ( self ) : <EOL> return self . ttype == EOL <EOL> def is_whitespace ( self ) : <EOL> return self . ttype == WHITESPACE <EOL> def is_identifier ( self ) : <EOL> return self . ttype == IDENTIFIER <EOL> def is_quoted_string ( self ) : <EOL> return self . ttype == QUOTED_STRING <EOL> def is_comment ( self ) : <EOL> return self . ttype == COMMENT <EOL> def is_delimiter ( self ) : <EOL> return self . ttype == DELIMITER <EOL> def is_eol_or_eof ( self ) : <EOL> return ( self . ttype == EOL or self . ttype == EOF ) <EOL> def __eq__ ( self , other ) : <EOL> if not isinstance ( other , Token ) : <EOL> return False <EOL> return ( self . ttype == other . ttype and <EOL> self . value == other . value ) <EOL> def __ne__ ( self , other ) : <EOL> if not isinstance ( other , Token ) : <EOL> return True <EOL> return ( self . ttype != other . ttype or <EOL> self . value != other . value ) <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % ( self . ttype , self . value ) <EOL> def unescape ( self ) : <EOL> if not self . has_escape : <EOL> return self <EOL> unescaped = '<STR_LIT>' <EOL> l = len ( self . value ) <EOL> i = <NUM_LIT:0> <EOL> while i < l : <EOL> c = self . value [ i ] <EOL> i += <NUM_LIT:1> <EOL> if c == '<STR_LIT:\\>' : <EOL> if i >= l : <EOL> raise dns . exception . UnexpectedEnd <EOL> c = self . value [ i ] <EOL> i += <NUM_LIT:1> <EOL> if c . isdigit ( ) : <EOL> if i >= l : <EOL> raise dns . exception . UnexpectedEnd <EOL> c2 = self . value [ i ] <EOL> i += <NUM_LIT:1> <EOL> if i >= l : <EOL> raise dns . exception . UnexpectedEnd <EOL> c3 = self . value [ i ] <EOL> i += <NUM_LIT:1> <EOL> if not ( c2 . isdigit ( ) and c3 . isdigit ( ) ) : <EOL> raise dns . exception . SyntaxError <EOL> c = chr ( int ( c ) * <NUM_LIT:100> + int ( c2 ) * <NUM_LIT:10> + int ( c3 ) ) <EOL> unescaped += c <EOL> return Token ( self . ttype , unescaped ) <EOL> def __len__ ( self ) : <EOL> return <NUM_LIT:2> <EOL> def __iter__ ( self ) : <EOL> return iter ( ( self . ttype , self . value ) ) <EOL> def __getitem__ ( self , i ) : <EOL> if i == <NUM_LIT:0> : <EOL> return self . ttype <EOL> elif i == <NUM_LIT:1> : <EOL> return self . value <EOL> else : <EOL> raise IndexError <EOL> class Tokenizer ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , f = sys . stdin , filename = None ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( f , str ) : <EOL> f = cStringIO . StringIO ( f ) <EOL> if filename is None : <EOL> filename = '<STR_LIT>' <EOL> else : <EOL> if filename is None : <EOL> if f is sys . stdin : <EOL> filename = '<STR_LIT>' <EOL> else : <EOL> filename = '<STR_LIT>' <EOL> self . file = f <EOL> self . ungotten_char = None <EOL> self . ungotten_token = None <EOL> self . multiline = <NUM_LIT:0> <EOL> self . quoting = False <EOL> self . eof = False <EOL> self . delimiters = _DELIMITERS <EOL> self . line_number = <NUM_LIT:1> <EOL> self . filename = filename <EOL> def _get_char ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . ungotten_char is None : <EOL> if self . eof : <EOL> c = '<STR_LIT>' <EOL> else : <EOL> c = self . file . read ( <NUM_LIT:1> ) <EOL> if c == '<STR_LIT>' : <EOL> self . eof = True <EOL> elif c == '<STR_LIT:\n>' : <EOL> self . line_number += <NUM_LIT:1> <EOL> else : <EOL> c = self . ungotten_char <EOL> self . ungotten_char = None <EOL> return c <EOL> def where ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( self . filename , self . line_number ) <EOL> def _unget_char ( self , c ) : <EOL> """<STR_LIT>""" <EOL> if not self . ungotten_char is None : <EOL> raise UngetBufferFull <EOL> self . ungotten_char = c <EOL> def skip_whitespace ( self ) : <EOL> """<STR_LIT>""" <EOL> skipped = <NUM_LIT:0> <EOL> while True : <EOL> c = self . _get_char ( ) <EOL> if c != '<STR_LIT:U+0020>' and c != '<STR_LIT:\t>' : <EOL> if ( c != '<STR_LIT:\n>' ) or not self . multiline : <EOL> self . _unget_char ( c ) <EOL> return skipped <EOL> skipped += <NUM_LIT:1> <EOL> def get ( self , want_leading = False , want_comment = False ) : <EOL> """<STR_LIT>""" <EOL> if not self . ungotten_token is None : <EOL> token = self . ungotten_token <EOL> self . ungotten_token = None <EOL> if token . is_whitespace ( ) : <EOL> if want_leading : <EOL> return token <EOL> elif token . is_comment ( ) : <EOL> if want_comment : <EOL> return token <EOL> else : <EOL> return token <EOL> skipped = self . skip_whitespace ( ) <EOL> if want_leading and skipped > <NUM_LIT:0> : <EOL> return Token ( WHITESPACE , '<STR_LIT:U+0020>' ) <EOL> token = '<STR_LIT>' <EOL> ttype = IDENTIFIER <EOL> has_escape = False <EOL> while True : <EOL> c = self . _get_char ( ) <EOL> if c == '<STR_LIT>' or c in self . delimiters : <EOL> if c == '<STR_LIT>' and self . quoting : <EOL> raise dns . exception . UnexpectedEnd <EOL> if token == '<STR_LIT>' and ttype != QUOTED_STRING : <EOL> if c == '<STR_LIT:(>' : <EOL> self . multiline += <NUM_LIT:1> <EOL> self . skip_whitespace ( ) <EOL> continue <EOL> elif c == '<STR_LIT:)>' : <EOL> if not self . multiline > <NUM_LIT:0> : <EOL> raise dns . exception . SyntaxError <EOL> self . multiline -= <NUM_LIT:1> <EOL> self . skip_whitespace ( ) <EOL> continue <EOL> elif c == '<STR_LIT:">' : <EOL> if not self . quoting : <EOL> self . quoting = True <EOL> self . delimiters = _QUOTING_DELIMITERS <EOL> ttype = QUOTED_STRING <EOL> continue <EOL> else : <EOL> self . quoting = False <EOL> self . delimiters = _DELIMITERS <EOL> self . skip_whitespace ( ) <EOL> continue <EOL> elif c == '<STR_LIT:\n>' : <EOL> return Token ( EOL , '<STR_LIT:\n>' ) <EOL> elif c == '<STR_LIT:;>' : <EOL> while <NUM_LIT:1> : <EOL> c = self . _get_char ( ) <EOL> if c == '<STR_LIT:\n>' or c == '<STR_LIT>' : <EOL> break <EOL> token += c <EOL> if want_comment : <EOL> self . _unget_char ( c ) <EOL> return Token ( COMMENT , token ) <EOL> elif c == '<STR_LIT>' : <EOL> if self . multiline : <EOL> raise dns . exception . SyntaxError ( '<STR_LIT>' ) <EOL> return Token ( EOF ) <EOL> elif self . multiline : <EOL> self . skip_whitespace ( ) <EOL> token = '<STR_LIT>' <EOL> continue <EOL> else : <EOL> return Token ( EOL , '<STR_LIT:\n>' ) <EOL> else : <EOL> token = c <EOL> ttype = DELIMITER <EOL> else : <EOL> self . _unget_char ( c ) <EOL> break <EOL> elif self . quoting : <EOL> if c == '<STR_LIT:\\>' : <EOL> c = self . _get_char ( ) <EOL> if c == '<STR_LIT>' : <EOL> raise dns . exception . UnexpectedEnd <EOL> if c . isdigit ( ) : <EOL> c2 = self . _get_char ( ) <EOL> if c2 == '<STR_LIT>' : <EOL> raise dns . exception . UnexpectedEnd <EOL> c3 = self . _get_char ( ) <EOL> if c == '<STR_LIT>' : <EOL> raise dns . exception . UnexpectedEnd <EOL> if not ( c2 . isdigit ( ) and c3 . isdigit ( ) ) : <EOL> raise dns . exception . SyntaxError <EOL> c = chr ( int ( c ) * <NUM_LIT:100> + int ( c2 ) * <NUM_LIT:10> + int ( c3 ) ) <EOL> elif c == '<STR_LIT:\n>' : <EOL> raise dns . exception . SyntaxError ( '<STR_LIT>' ) <EOL> elif c == '<STR_LIT:\\>' : <EOL> token += c <EOL> has_escape = True <EOL> c = self . _get_char ( ) <EOL> if c == '<STR_LIT>' or c == '<STR_LIT:\n>' : <EOL> raise dns . exception . UnexpectedEnd <EOL> token += c <EOL> if token == '<STR_LIT>' and ttype != QUOTED_STRING : <EOL> if self . multiline : <EOL> raise dns . exception . SyntaxError ( '<STR_LIT>' ) <EOL> ttype = EOF <EOL> return Token ( ttype , token , has_escape ) <EOL> def unget ( self , token ) : <EOL> """<STR_LIT>""" <EOL> if not self . ungotten_token is None : <EOL> raise UngetBufferFull <EOL> self . ungotten_token = token <EOL> def next ( self ) : <EOL> """<STR_LIT>""" <EOL> token = self . get ( ) <EOL> if token . is_eof ( ) : <EOL> raise StopIteration <EOL> return token <EOL> def __iter__ ( self ) : <EOL> return self <EOL> def get_int ( self ) : <EOL> """<STR_LIT>""" <EOL> token = self . get ( ) . unescape ( ) <EOL> if not token . is_identifier ( ) : <EOL> raise dns . exception . SyntaxError ( '<STR_LIT>' ) <EOL> if not token . value . isdigit ( ) : <EOL> raise dns . exception . SyntaxError ( '<STR_LIT>' ) <EOL> return int ( token . value ) <EOL> def get_uint8 ( self ) : <EOL> """<STR_LIT>""" <EOL> value = self . get_int ( ) <EOL> if value < <NUM_LIT:0> or value > <NUM_LIT:255> : <EOL> raise dns . exception . SyntaxError ( '<STR_LIT>' % value ) <EOL> return value <EOL> def get_uint16 ( self ) : <EOL> """<STR_LIT>""" <EOL> value = self . get_int ( ) <EOL> if value < <NUM_LIT:0> or value > <NUM_LIT> : <EOL> raise dns . exception . SyntaxError ( '<STR_LIT>' % value ) <EOL> return value <EOL> def get_uint32 ( self ) : <EOL> """<STR_LIT>""" <EOL> token = self . get ( ) . unescape ( ) <EOL> if not token . is_identifier ( ) : <EOL> raise dns . exception . SyntaxError ( '<STR_LIT>' ) <EOL> if not token . value . isdigit ( ) : <EOL> raise dns . exception . SyntaxError ( '<STR_LIT>' ) <EOL> value = long ( token . value ) <EOL> if value < <NUM_LIT:0> or value > <NUM_LIT> L : <EOL> raise dns . exception . SyntaxError ( '<STR_LIT>' % value ) <EOL> return value <EOL> def get_string ( self , origin = None ) : <EOL> """<STR_LIT>""" <EOL> token = self . get ( ) . unescape ( ) <EOL> if not ( token . is_identifier ( ) or token . is_quoted_string ( ) ) : <EOL> raise dns . exception . SyntaxError ( '<STR_LIT>' ) <EOL> return token . value <EOL> def get_identifier ( self , origin = None ) : <EOL> """<STR_LIT>""" <EOL> token = self . get ( ) . unescape ( ) <EOL> if not token . is_identifier ( ) : <EOL> raise dns . exception . SyntaxError ( '<STR_LIT>' ) <EOL> return token . value <EOL> def get_name ( self , origin = None ) : <EOL> """<STR_LIT>""" <EOL> token = self . get ( ) <EOL> if not token . is_identifier ( ) : <EOL> raise dns . exception . SyntaxError ( '<STR_LIT>' ) <EOL> return dns . name . from_text ( token . value , origin ) <EOL> def get_eol ( self ) : <EOL> """<STR_LIT>""" <EOL> token = self . get ( ) <EOL> if not token . is_eol_or_eof ( ) : <EOL> raise dns . exception . SyntaxError ( '<STR_LIT>' % ( token . ttype , token . value ) ) <EOL> return token . value <EOL> def get_ttl ( self ) : <EOL> token = self . get ( ) . unescape ( ) <EOL> if not token . is_identifier ( ) : <EOL> raise dns . exception . SyntaxError ( '<STR_LIT>' ) <EOL> return dns . ttl . from_text ( token . value ) </s>
