Actions.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # cython: auto_pickle=False
  2. #=======================================================================
  3. #
  4. # Python Lexical Analyser
  5. #
  6. # Actions for use in token specifications
  7. #
  8. #=======================================================================
  9. class Action(object):
  10. def perform(self, token_stream, text):
  11. pass # abstract
  12. def same_as(self, other):
  13. return self is other
  14. class Return(Action):
  15. """
  16. Internal Plex action which causes |value| to
  17. be returned as the value of the associated token
  18. """
  19. def __init__(self, value):
  20. self.value = value
  21. def perform(self, token_stream, text):
  22. return self.value
  23. def same_as(self, other):
  24. return isinstance(other, Return) and self.value == other.value
  25. def __repr__(self):
  26. return "Return(%s)" % repr(self.value)
  27. class Call(Action):
  28. """
  29. Internal Plex action which causes a function to be called.
  30. """
  31. def __init__(self, function):
  32. self.function = function
  33. def perform(self, token_stream, text):
  34. return self.function(token_stream, text)
  35. def __repr__(self):
  36. return "Call(%s)" % self.function.__name__
  37. def same_as(self, other):
  38. return isinstance(other, Call) and self.function is other.function
  39. class Begin(Action):
  40. """
  41. Begin(state_name) is a Plex action which causes the Scanner to
  42. enter the state |state_name|. See the docstring of Plex.Lexicon
  43. for more information.
  44. """
  45. def __init__(self, state_name):
  46. self.state_name = state_name
  47. def perform(self, token_stream, text):
  48. token_stream.begin(self.state_name)
  49. def __repr__(self):
  50. return "Begin(%s)" % self.state_name
  51. def same_as(self, other):
  52. return isinstance(other, Begin) and self.state_name == other.state_name
  53. class Ignore(Action):
  54. """
  55. IGNORE is a Plex action which causes its associated token
  56. to be ignored. See the docstring of Plex.Lexicon for more
  57. information.
  58. """
  59. def perform(self, token_stream, text):
  60. return None
  61. def __repr__(self):
  62. return "IGNORE"
  63. IGNORE = Ignore()
  64. #IGNORE.__doc__ = Ignore.__doc__
  65. class Text(Action):
  66. """
  67. TEXT is a Plex action which causes the text of a token to
  68. be returned as the value of the token. See the docstring of
  69. Plex.Lexicon for more information.
  70. """
  71. def perform(self, token_stream, text):
  72. return text
  73. def __repr__(self):
  74. return "TEXT"
  75. TEXT = Text()
  76. #TEXT.__doc__ = Text.__doc__