home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 October / maximum-cd-2011-10.iso / DiscContents / digsby_setup.exe / lib / dns / tokenizer.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-06-22  |  10.7 KB  |  430 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. import cStringIO
  5. import sys
  6. import dns.exception as dns
  7. import dns.name as dns
  8. import dns.ttl as dns
  9. _DELIMITERS = {
  10.     ' ': True,
  11.     '\t': True,
  12.     '\n': True,
  13.     ';': True,
  14.     '(': True,
  15.     ')': True,
  16.     '"': True }
  17. _QUOTING_DELIMITERS = {
  18.     '"': True }
  19. EOF = 0
  20. EOL = 1
  21. WHITESPACE = 2
  22. IDENTIFIER = 3
  23. QUOTED_STRING = 4
  24. COMMENT = 5
  25. DELIMITER = 6
  26.  
  27. class UngetBufferFull(dns.exception.DNSException):
  28.     pass
  29.  
  30.  
  31. class Token(object):
  32.     
  33.     def __init__(self, ttype, value = '', has_escape = False):
  34.         self.ttype = ttype
  35.         self.value = value
  36.         self.has_escape = has_escape
  37.  
  38.     
  39.     def is_eof(self):
  40.         return self.ttype == EOF
  41.  
  42.     
  43.     def is_eol(self):
  44.         return self.ttype == EOL
  45.  
  46.     
  47.     def is_whitespace(self):
  48.         return self.ttype == WHITESPACE
  49.  
  50.     
  51.     def is_identifier(self):
  52.         return self.ttype == IDENTIFIER
  53.  
  54.     
  55.     def is_quoted_string(self):
  56.         return self.ttype == QUOTED_STRING
  57.  
  58.     
  59.     def is_comment(self):
  60.         return self.ttype == COMMENT
  61.  
  62.     
  63.     def is_delimiter(self):
  64.         return self.ttype == DELIMITER
  65.  
  66.     
  67.     def is_eol_or_eof(self):
  68.         if not self.ttype == EOL:
  69.             pass
  70.         return self.ttype == EOF
  71.  
  72.     
  73.     def __eq__(self, other):
  74.         if not isinstance(other, Token):
  75.             return False
  76.         if self.ttype == other.ttype:
  77.             pass
  78.         return self.value == other.value
  79.  
  80.     
  81.     def __ne__(self, other):
  82.         if not isinstance(other, Token):
  83.             return True
  84.         if not self.ttype != other.ttype:
  85.             pass
  86.         return self.value != other.value
  87.  
  88.     
  89.     def __str__(self):
  90.         return '%d "%s"' % (self.ttype, self.value)
  91.  
  92.     
  93.     def unescape(self):
  94.         if not self.has_escape:
  95.             return self
  96.         unescaped = ''
  97.         l = len(self.value)
  98.         i = 0
  99.         while i < l:
  100.             c = self.value[i]
  101.             i += 1
  102.             if c == '\\':
  103.                 if i >= l:
  104.                     raise dns.exception.UnexpectedEnd
  105.                 i >= l
  106.                 c = self.value[i]
  107.                 i += 1
  108.                 if c.isdigit():
  109.                     if i >= l:
  110.                         raise dns.exception.UnexpectedEnd
  111.                     i >= l
  112.                     c2 = self.value[i]
  113.                     i += 1
  114.                     if i >= l:
  115.                         raise dns.exception.UnexpectedEnd
  116.                     i >= l
  117.                     c3 = self.value[i]
  118.                     i += 1
  119.                     if not c2.isdigit() and c3.isdigit():
  120.                         raise dns.exception.SyntaxError
  121.                     c3.isdigit()
  122.                     c = chr(int(c) * 100 + int(c2) * 10 + int(c3))
  123.                 
  124.             
  125.             unescaped += c
  126.         return Token(self.ttype, unescaped)
  127.  
  128.     
  129.     def __len__(self):
  130.         return 2
  131.  
  132.     
  133.     def __iter__(self):
  134.         return iter((self.ttype, self.value))
  135.  
  136.     
  137.     def __getitem__(self, i):
  138.         if i == 0:
  139.             return self.ttype
  140.         if i == 1:
  141.             return self.value
  142.         raise IndexError
  143.  
  144.  
  145.  
  146. class Tokenizer(object):
  147.     
  148.     def __init__(self, f = sys.stdin, filename = None):
  149.         if isinstance(f, str):
  150.             f = cStringIO.StringIO(f)
  151.             if filename is None:
  152.                 filename = '<string>'
  153.             
  154.         elif filename is None:
  155.             if f is sys.stdin:
  156.                 filename = '<stdin>'
  157.             else:
  158.                 filename = '<file>'
  159.         
  160.         self.file = f
  161.         self.ungotten_char = None
  162.         self.ungotten_token = None
  163.         self.multiline = 0
  164.         self.quoting = False
  165.         self.eof = False
  166.         self.delimiters = _DELIMITERS
  167.         self.line_number = 1
  168.         self.filename = filename
  169.  
  170.     
  171.     def _get_char(self):
  172.         if self.ungotten_char is None:
  173.             if self.eof:
  174.                 c = ''
  175.             else:
  176.                 c = self.file.read(1)
  177.                 if c == '':
  178.                     self.eof = True
  179.                 elif c == '\n':
  180.                     self.line_number += 1
  181.                 
  182.         else:
  183.             c = self.ungotten_char
  184.             self.ungotten_char = None
  185.         return c
  186.  
  187.     
  188.     def where(self):
  189.         return (self.filename, self.line_number)
  190.  
  191.     
  192.     def _unget_char(self, c):
  193.         if self.ungotten_char is not None:
  194.             raise UngetBufferFull
  195.         self.ungotten_char is not None
  196.         self.ungotten_char = c
  197.  
  198.     
  199.     def skip_whitespace(self):
  200.         skipped = 0
  201.         while True:
  202.             c = self._get_char()
  203.             if c != ' ' and c != '\t':
  204.                 if c != '\n' or not (self.multiline):
  205.                     self._unget_char(c)
  206.                     return skipped
  207.             
  208.             skipped += 1
  209.  
  210.     
  211.     def get(self, want_leading = False, want_comment = False):
  212.         if self.ungotten_token is not None:
  213.             token = self.ungotten_token
  214.             self.ungotten_token = None
  215.             if token.is_whitespace():
  216.                 if want_leading:
  217.                     return token
  218.             elif token.is_comment():
  219.                 if want_comment:
  220.                     return token
  221.             else:
  222.                 return token
  223.         token.is_whitespace()
  224.         skipped = self.skip_whitespace()
  225.         if want_leading and skipped > 0:
  226.             return Token(WHITESPACE, ' ')
  227.         token = ''
  228.         ttype = IDENTIFIER
  229.         has_escape = False
  230.         while True:
  231.             c = self._get_char()
  232.             if c == '' or c in self.delimiters:
  233.                 if c == '' and self.quoting:
  234.                     raise dns.exception.UnexpectedEnd
  235.                 self.quoting
  236.                 if token == '' and ttype != QUOTED_STRING:
  237.                     if c == '(':
  238.                         self.multiline += 1
  239.                         self.skip_whitespace()
  240.                         continue
  241.                     elif c == ')':
  242.                         if not self.multiline > 0:
  243.                             raise dns.exception.SyntaxError
  244.                         self.multiline > 0
  245.                         self.multiline -= 1
  246.                         self.skip_whitespace()
  247.                         continue
  248.                     elif c == '"':
  249.                         if not self.quoting:
  250.                             self.quoting = True
  251.                             self.delimiters = _QUOTING_DELIMITERS
  252.                             ttype = QUOTED_STRING
  253.                             continue
  254.                         else:
  255.                             self.quoting = False
  256.                             self.delimiters = _DELIMITERS
  257.                             self.skip_whitespace()
  258.                     elif c == '\n':
  259.                         return Token(EOL, '\n')
  260.                     skipped > 0
  261.                     if c == ';':
  262.                         while None:
  263.                             c = self._get_char()
  264.                             if c == '\n' or c == '':
  265.                                 break
  266.                             
  267.                             token += c
  268.                             continue
  269.                             if want_comment:
  270.                                 self._unget_char(c)
  271.                                 return Token(COMMENT, token)
  272.                             if c == '':
  273.                                 if self.multiline:
  274.                                     raise dns.exception.SyntaxError('unbalanced parentheses')
  275.                                 self.multiline
  276.                                 return Token(EOF)
  277.                             if self.multiline:
  278.                                 self.skip_whitespace()
  279.                                 token = ''
  280.                                 continue
  281.                             else:
  282.                                 return Token(EOL, '\n')
  283.                             token = c
  284.                             ttype = DELIMITER
  285.                         c == ''
  286.                         self._unget_char(c)
  287.                         break
  288.                     elif self.quoting:
  289.                         if c == '\\':
  290.                             c = self._get_char()
  291.                             if c == '':
  292.                                 raise dns.exception.UnexpectedEnd
  293.                             c == ''
  294.                             if c.isdigit():
  295.                                 c2 = self._get_char()
  296.                                 if c2 == '':
  297.                                     raise dns.exception.UnexpectedEnd
  298.                                 c2 == ''
  299.                                 c3 = self._get_char()
  300.                                 if c == '':
  301.                                     raise dns.exception.UnexpectedEnd
  302.                                 c == ''
  303.                                 if not c2.isdigit() and c3.isdigit():
  304.                                     raise dns.exception.SyntaxError
  305.                                 c3.isdigit()
  306.                                 c = chr(int(c) * 100 + int(c2) * 10 + int(c3))
  307.                             
  308.                         elif c == '\n':
  309.                             raise dns.exception.SyntaxError('newline in quoted string')
  310.                         
  311.                     elif c == '\\':
  312.                         token += c
  313.                         has_escape = True
  314.                         c = self._get_char()
  315.                         if c == '' or c == '\n':
  316.                             raise dns.exception.UnexpectedEnd
  317.                         c == '\n'
  318.                     
  319.             token += c
  320.         if token == '' and ttype != QUOTED_STRING:
  321.             if self.multiline:
  322.                 raise dns.exception.SyntaxError('unbalanced parentheses')
  323.             self.multiline
  324.             ttype = EOF
  325.         
  326.         return Token(ttype, token, has_escape)
  327.  
  328.     
  329.     def unget(self, token):
  330.         if self.ungotten_token is not None:
  331.             raise UngetBufferFull
  332.         self.ungotten_token is not None
  333.         self.ungotten_token = token
  334.  
  335.     
  336.     def next(self):
  337.         token = self.get()
  338.         if token.is_eof():
  339.             raise StopIteration
  340.         token.is_eof()
  341.         return token
  342.  
  343.     
  344.     def __iter__(self):
  345.         return self
  346.  
  347.     
  348.     def get_int(self):
  349.         token = self.get().unescape()
  350.         if not token.is_identifier():
  351.             raise dns.exception.SyntaxError('expecting an identifier')
  352.         token.is_identifier()
  353.         if not token.value.isdigit():
  354.             raise dns.exception.SyntaxError('expecting an integer')
  355.         token.value.isdigit()
  356.         return int(token.value)
  357.  
  358.     
  359.     def get_uint8(self):
  360.         value = self.get_int()
  361.         if value < 0 or value > 255:
  362.             raise dns.exception.SyntaxError('%d is not an unsigned 8-bit integer' % value)
  363.         value > 255
  364.         return value
  365.  
  366.     
  367.     def get_uint16(self):
  368.         value = self.get_int()
  369.         if value < 0 or value > 65535:
  370.             raise dns.exception.SyntaxError('%d is not an unsigned 16-bit integer' % value)
  371.         value > 65535
  372.         return value
  373.  
  374.     
  375.     def get_uint32(self):
  376.         token = self.get().unescape()
  377.         if not token.is_identifier():
  378.             raise dns.exception.SyntaxError('expecting an identifier')
  379.         token.is_identifier()
  380.         if not token.value.isdigit():
  381.             raise dns.exception.SyntaxError('expecting an integer')
  382.         token.value.isdigit()
  383.         value = long(token.value)
  384.         if value < 0 or value > 0x100000000L:
  385.             raise dns.exception.SyntaxError('%d is not an unsigned 32-bit integer' % value)
  386.         value > 0x100000000L
  387.         return value
  388.  
  389.     
  390.     def get_string(self, origin = None):
  391.         token = self.get().unescape()
  392.         if not token.is_identifier() or token.is_quoted_string():
  393.             raise dns.exception.SyntaxError('expecting a string')
  394.         token.is_quoted_string()
  395.         return token.value
  396.  
  397.     
  398.     def get_identifier(self, origin = None):
  399.         token = self.get().unescape()
  400.         if not token.is_identifier():
  401.             raise dns.exception.SyntaxError('expecting an identifier')
  402.         token.is_identifier()
  403.         return token.value
  404.  
  405.     
  406.     def get_name(self, origin = None):
  407.         token = self.get()
  408.         if not token.is_identifier():
  409.             raise dns.exception.SyntaxError('expecting an identifier')
  410.         token.is_identifier()
  411.         return dns.name.from_text(token.value, origin)
  412.  
  413.     
  414.     def get_eol(self):
  415.         token = self.get()
  416.         if not token.is_eol_or_eof():
  417.             raise dns.exception.SyntaxError('expected EOL or EOF, got %d "%s"' % (token.ttype, token.value))
  418.         token.is_eol_or_eof()
  419.         return token.value
  420.  
  421.     
  422.     def get_ttl(self):
  423.         token = self.get().unescape()
  424.         if not token.is_identifier():
  425.             raise dns.exception.SyntaxError('expecting an identifier')
  426.         token.is_identifier()
  427.         return dns.ttl.from_text(token.value)
  428.  
  429.  
  430.