@@ -36,29 +36,17 @@ def get_alias(self):
3636 return self ._get_first_name (reverse = True )
3737
3838
39- class Token :
40- """Base class for all other classes in this module .
39+ class TokenBase :
40+ """Base class for ``Token`` and ``TokenList`` .
4141
42- It represents a single token and has two instance attributes:
43- ``value`` is the unchanged value of the token and ``ttype`` is
44- the type of the token.
42+ It has a single instance attribute, ``parent``, which if not ``None``
43+ represents the ``TokenList`` that contains this token.
4544 """
4645
47- __slots__ = ('value' , 'ttype' , 'parent' , 'normalized' , 'is_keyword' ,
48- 'is_group' , 'is_whitespace' )
46+ __slots__ = 'parent'
4947
50- def __init__ (self , ttype , value ):
51- value = str (value )
52- self .value = value
53- self .ttype = ttype
48+ def __init__ (self ):
5449 self .parent = None
55- self .is_group = False
56- self .is_keyword = ttype in T .Keyword
57- self .is_whitespace = self .ttype in T .Whitespace
58- self .normalized = value .upper () if self .is_keyword else value
59-
60- def __str__ (self ):
61- return self .value
6250
6351 # Pending tokenlist __len__ bug fix
6452 # def __len__(self):
@@ -72,19 +60,12 @@ def __repr__(self):
7260 return "<{cls} {q}{value}{q} at 0x{id:2X}>" .format (
7361 id = id (self ), ** locals ())
7462
75- def _get_repr_name (self ):
76- return str (self .ttype ).split ('.' )[- 1 ]
77-
7863 def _get_repr_value (self ):
7964 raw = str (self )
8065 if len (raw ) > 7 :
8166 raw = raw [:6 ] + '...'
8267 return re .sub (r'\s+' , ' ' , raw )
8368
84- def flatten (self ):
85- """Resolve subgroups."""
86- yield self
87-
8869 def match (self , ttype , values , regex = False ):
8970 """Checks whether the token matches the given arguments.
9071
@@ -146,24 +127,68 @@ def has_ancestor(self, other):
146127 return False
147128
148129
149- class TokenList (Token ):
130+ class Token (TokenBase ):
131+ """"A single token.
132+
133+ It has five additional instance attributes:
134+ ``value`` is the unchanged value of the token
135+ ``ttype`` is the type of the token
136+ ``normalized`` is the value of the token, converted to uppercase if it
137+ is a keyword
138+ ``is_keyword`` is a boolean indicating if the token is a keyword
139+ ``is_whitespace`` is a boolean indicating if the token is whitespace
140+ """
141+ __slots__ = ('value' , 'ttype' , 'normalized' , 'is_keyword' , 'is_whitespace' )
142+
143+ is_group = False
144+
145+ def __init__ (self , ttype , value ):
146+ super ().__init__ ()
147+ value = str (value )
148+ self .value = value
149+ self .ttype = ttype
150+ self .is_keyword = ttype in T .Keyword
151+ self .is_whitespace = ttype in T .Whitespace
152+ self .normalized = value .upper () if self .is_keyword else value
153+
154+ def __str__ (self ):
155+ return self .value
156+
157+ def _get_repr_name (self ):
158+ return str (self .ttype ).split ('.' )[- 1 ]
159+
160+ def flatten (self ):
161+ """Resolve subgroups."""
162+ yield self
163+
164+
165+ class TokenList (TokenBase ):
150166 """A group of tokens.
151167
152- It has an additional instance attribute ``tokens`` which holds a
153- list of child-tokens.
168+ It has two additional instance attributes, ``value``, which is the value of
169+ the token list, and ``tokens``, which holds a list of child-tokens.
154170 """
155171
156- __slots__ = 'tokens'
172+ __slots__ = ('tokens' , 'value' )
173+
174+ is_group = True
175+ ttype = None
176+ is_keyword = False
177+ is_whitespace = False
157178
158179 def __init__ (self , tokens = None ):
180+ super ().__init__ ()
159181 self .tokens = tokens or []
182+ self .value = str (self )
160183 [setattr (token , 'parent' , self ) for token in self .tokens ]
161- super ().__init__ (None , str (self ))
162- self .is_group = True
163184
164185 def __str__ (self ):
165186 return '' .join (token .value for token in self .flatten ())
166187
188+ @property
189+ def normalized (self ):
190+ return self .value
191+
167192 # weird bug
168193 # def __len__(self):
169194 # return len(self.tokens)
0 commit comments