~ubuntu-branches/ubuntu/hardy/gnue-common/hardy

« back to all changes in this revision

Viewing changes to src/external/plex/Actions.py

  • Committer: Bazaar Package Importer
  • Author(s): Andrew Mitchell
  • Date: 2005-03-09 11:06:31 UTC
  • Revision ID: james.westby@ubuntu.com-20050309110631-8gvvn39q7tjz1kj6
Tags: upstream-0.5.14
ImportĀ upstreamĀ versionĀ 0.5.14

Show diffs side-by-side

added added

removed removed

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