aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/tools/cython/Cython/Plex/Actions.py
blob: c88176e71615ca4ee5454a49a8b25ae18471b292 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# cython: auto_pickle=False
#=======================================================================
#
#   Python Lexical Analyser
#
#   Actions for use in token specifications
#
#=======================================================================

class Action(object):
    def perform(self, token_stream, text):
        pass  # abstract

    def same_as(self, other):
        return self is other


class Return(Action):
    """
    Internal Plex action which causes |value| to
    be returned as the value of the associated token
    """

    def __init__(self, value):
        self.value = value

    def perform(self, token_stream, text):
        return self.value

    def same_as(self, other):
        return isinstance(other, Return) and self.value == other.value

    def __repr__(self):
        return "Return(%s)" % repr(self.value)


class Call(Action):
    """
    Internal Plex action which causes a function to be called.
    """

    def __init__(self, function):
        self.function = function

    def perform(self, token_stream, text):
        return self.function(token_stream, text)

    def __repr__(self):
        return "Call(%s)" % self.function.__name__

    def same_as(self, other):
        return isinstance(other, Call) and self.function is other.function


class Begin(Action):
    """
    Begin(state_name) is a Plex action which causes the Scanner to
    enter the state |state_name|. See the docstring of Plex.Lexicon
    for more information.
    """

    def __init__(self, state_name):
        self.state_name = state_name

    def perform(self, token_stream, text):
        token_stream.begin(self.state_name)

    def __repr__(self):
        return "Begin(%s)" % self.state_name

    def same_as(self, other):
        return isinstance(other, Begin) and self.state_name == other.state_name


class Ignore(Action):
    """
    IGNORE is a Plex action which causes its associated token
    to be ignored. See the docstring of Plex.Lexicon  for more
    information.
    """

    def perform(self, token_stream, text):
        return None

    def __repr__(self):
        return "IGNORE"


IGNORE = Ignore()
#IGNORE.__doc__ = Ignore.__doc__


class Text(Action):
    """
    TEXT is a Plex action which causes the text of a token to
    be returned as the value of the token. See the docstring of
    Plex.Lexicon  for more information.
    """

    def perform(self, token_stream, text):
        return text

    def __repr__(self):
        return "TEXT"


TEXT = Text()
#TEXT.__doc__ = Text.__doc__