Debian Codegraph @

ly/lex/_token.py

raw
1 # This file is part of python-ly, https://pypi.python.org/pypi/python-ly
2 #
3 # Copyright (c) 2008 - 2015 by Wilbert Berendsen
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 # See http://www.gnu.org/licenses/ for more information.
19
20 """
21 Token base classes.
22
23 Don't use this module directly!
24 All important classes are imported in the ly.lex namespace.
25
26 You can, however, import _token in mode modules that also are in this directory.
27
28 This module starts with an underscore so that its name does not interfere with
29 the mode modules.
30
31 """
32
33 from __future__ import unicode_literals
34
35
36 from .. import slexer
37
38
39 class Token(slexer.Token):
40 def __repr__(self):
41 c = self.__class__
42 module = c.__module__.rsplit('.', 1)[-1]
43 name = c.__name__
44 contents = slexer.Token.__repr__(self)
45 where = self.pos
46 return '<{0}.{1} {2} at {3}>'.format(module, name, contents, where)
47
48
49 class patternproperty(object):
50 """Property that caches the return value of its function and returns that next time.
51
52 Use this if the rx attribute (the pattern string to match tokens for) of a Token subclass
53 is already costly to create and you want it created lazily (i.e. only when parsing starts):
54
55 @patternproperty
56 def rx():
57 ...complicated function returning the regular expression string...
58
59 """
60 def __init__(self, func):
61 self.func = func
62
63 def __get__(self, instance, owner):
64 try:
65 return self.rx
66 except AttributeError:
67 self.rx = self.func()
68 return self.rx
69
70
71 class Unparsed(Token):
72 """Represents an unparsed piece of input text."""
73
74
75 # some token types with special behaviour:
76 class Item(Token):
77 """A token that decreases the argument count of the current parser."""
78 def update_state(self, state):
79 state.endArgument()
80
81
82 class Leaver(Token):
83 """A token that leaves the current parser."""
84 def update_state(self, state):
85 state.leave()
86
87
88 # some generic types:
89 class Space(Token):
90 """A token containing whitespace."""
91 rx = r'\s+'
92
93
94 class Newline(Space):
95 """A token that is a single newline."""
96 rx = r'\n'
97
98
99 # some base types that should be inherited:
100 class Comment(Token):
101 """Base class for tokens that belong to a comment."""
102
103
104 class LineComment(Comment):
105 """Base class for items that are a whole line comment."""
106
107
108 class BlockComment(Comment):
109 """Base class for tokens that belong to a block/multiline comment."""
110
111
112 class BlockCommentStart(BlockComment):
113 """Base class for tokens that start a block/multiline comment."""
114
115
116 class BlockCommentEnd(BlockComment):
117 """Base class for tokens that end a block/multiline comment."""
118
119
120 class String(Token):
121 """Base class for tokens that belong to a quote-delimited string."""
122
123
124 class StringStart(String):
125 """Base class for tokens that start a quote-delimited string."""
126
127
128 class StringEnd(String):
129 """Base class for tokens that end a quote-delimited string."""
130
131
132 class Character(Token):
133 """Base class for tokens that are an (escaped) character."""
134
135
136 class Numeric(Token):
137 """Base class for tokens that are a numerical value."""
138
139
140 class Error(Token):
141 """Base class for tokens that represent erroneous input."""
142
143
144 # some mixin classes that make special handling of tokens possible besides correct parsing:
145
146 # MatchEnd and MatchStart can be used by parenthesis/brace matcher.
147 # the matchname class attribute defines the type, so that it is independent
148 # of the way other types could be nested.
149 class MatchStart(object):
150 """Mixin class for tokens that have a matching token forward in the text.
151
152 The matchname attribute should give a unique name.
153
154 """
155 matchname = ""
156
157
158 class MatchEnd(object):
159 """Mixin class for tokens that have a matching token backward in the text.
160
161 The matchname attribute should give a unique name.
162
163 """
164 matchname = ""
165
166
167 # Indent and Dedent can be used by an (auto) indenter.
168 class Indent(object):
169 """Mixin class for tokens that have the text on the next line indent more."""
170
171
172 class Dedent(object):
173 """Mixin class for tokens that have the text on the next line indent less."""
174
175