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