home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_609 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  2.2 KB  |  77 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import re
  5. import sre_parse
  6. import sre_compile
  7. import sre_constants
  8. from re import VERBOSE, MULTILINE, DOTALL
  9. from sre_constants import BRANCH, SUBPATTERN
  10. __all__ = [
  11.     'Scanner',
  12.     'pattern']
  13. FLAGS = VERBOSE | MULTILINE | DOTALL
  14.  
  15. class Scanner(object):
  16.     
  17.     def __init__(self, lexicon, flags = FLAGS):
  18.         self.actions = [
  19.             None]
  20.         s = sre_parse.Pattern()
  21.         s.flags = flags
  22.         p = []
  23.         for idx, token in enumerate(lexicon):
  24.             phrase = token.pattern
  25.             
  26.             try:
  27.                 subpattern = sre_parse.SubPattern(s, [
  28.                     (SUBPATTERN, (idx + 1, sre_parse.parse(phrase, flags)))])
  29.             except sre_constants.error:
  30.                 raise 
  31.  
  32.             p.append(subpattern)
  33.             self.actions.append(token)
  34.         
  35.         s.groups = len(p) + 1
  36.         p = sre_parse.SubPattern(s, [
  37.             (BRANCH, (None, p))])
  38.         self.scanner = sre_compile.compile(p)
  39.  
  40.     
  41.     def iterscan(self, string, idx = 0, context = None):
  42.         match = self.scanner.scanner(string, idx).match
  43.         actions = self.actions
  44.         lastend = idx
  45.         end = len(string)
  46.         while True:
  47.             m = match()
  48.             if m is None:
  49.                 break
  50.             
  51.             (matchbegin, matchend) = m.span()
  52.             if lastend == matchend:
  53.                 break
  54.             
  55.             action = actions[m.lastindex]
  56.             if action is not None:
  57.                 (rval, next_pos) = action(m, context)
  58.                 if next_pos is not None and next_pos != matchend:
  59.                     matchend = next_pos
  60.                     match = self.scanner.scanner(string, matchend).match
  61.                 
  62.                 yield (rval, matchend)
  63.             
  64.             lastend = matchend
  65.  
  66.  
  67.  
  68. def pattern(pattern, flags = FLAGS):
  69.     
  70.     def decorator(fn):
  71.         fn.pattern = pattern
  72.         fn.regex = re.compile(pattern, flags)
  73.         return fn
  74.  
  75.     return decorator
  76.  
  77.