1# cython: auto_pickle=False
2#=======================================================================
3#
4#   Python Lexical Analyser
5#
6#   Actions for use in token specifications
7#
8#=======================================================================
9
10class Action(object):
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
82    def perform(self, token_stream, text):
83        return None
84
85    def __repr__(self):
86        return "IGNORE"
87
88
89IGNORE = Ignore()
90#IGNORE.__doc__ = Ignore.__doc__
91
92
93class Text(Action):
94    """
95    TEXT is a Plex action which causes the text of a token to
96    be returned as the value of the token. See the docstring of
97    Plex.Lexicon  for more information.
98    """
99
100    def perform(self, token_stream, text):
101        return text
102
103    def __repr__(self):
104        return "TEXT"
105
106
107TEXT = Text()
108#TEXT.__doc__ = Text.__doc__
109
110
111