Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2014
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. ## PSKeyword
  2. ##
  3. class PSKeyword(PSObject):
  4.  
  5. """A class that represents a PostScript keyword.
  6.  
  7. PostScript keywords are a dozen of predefined words.
  8. Commands and directives in PostScript are expressed by keywords.
  9. They are also used to denote the content boundaries.
  10.  
  11. Note: Do not create an instance of PSKeyword directly.
  12. Always use PSKeywordTable.intern().
  13. """
  14.  
  15. def __init__(self, name):
  16. self.name = name
  17. return
  18.  
  19. def __repr__(self):
  20. return self.name
  21.  
  22.  
  23. ## PSSymbolTable
  24. ##
  25. class PSSymbolTable(object):
  26.  
  27. """A utility class for storing PSLiteral/PSKeyword objects.
  28.  
  29. Interned objects can be checked its identity with "is" operator.
  30. """
  31.  
  32. def __init__(self, klass):
  33. self.dict = {}
  34. self.klass = klass
  35. return
  36.  
  37. def intern(self, name):
  38. if name in self.dict:
  39. lit = self.dict[name]
  40. else:
  41. lit = self.klass(name)
  42. self.dict[name] = lit
  43. return lit
  44.  
  45. # These are global variables being inited before init runs
  46. PSLiteralTable = PSSymbolTable(PSLiteral)
  47. PSKeywordTable = PSSymbolTable(PSKeyword)
  48. LIT = PSLiteralTable.intern
  49. KWD = PSKeywordTable.intern
  50. KEYWORD_PROC_BEGIN = KWD(b'{')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement