@@ -36,30 +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' , 'is_newline' )
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 .is_newline = self .ttype in T .Newline
59- self .normalized = value .upper () if self .is_keyword else value
60-
61- def __str__ (self ):
62- return self .value
6350
6451 # Pending tokenlist __len__ bug fix
6552 # def __len__(self):
@@ -73,19 +60,12 @@ def __repr__(self):
7360 return "<{cls} {q}{value}{q} at 0x{id:2X}>" .format (
7461 id = id (self ), ** locals ())
7562
76- def _get_repr_name (self ):
77- return str (self .ttype ).split ('.' )[- 1 ]
78-
7963 def _get_repr_value (self ):
8064 raw = str (self )
8165 if len (raw ) > 7 :
8266 raw = raw [:6 ] + '...'
8367 return re .sub (r'\s+' , ' ' , raw )
8468
85- def flatten (self ):
86- """Resolve subgroups."""
87- yield self
88-
8969 def match (self , ttype , values , regex = False ):
9070 """Checks whether the token matches the given arguments.
9171
@@ -147,24 +127,73 @@ def has_ancestor(self, other):
147127 return False
148128
149129
150- class TokenList (Token ):
130+ class Token (TokenBase ):
131+ """"A single token.
132+
133+ It has some 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+ ``is_newline`` is a boolean indicating if the token is a newline
141+ character
142+ """
143+ __slots__ = ('value' , 'ttype' , 'normalized' , 'is_keyword' , 'is_whitespace' ,
144+ 'is_newline' )
145+
146+ is_group = False
147+
148+ def __init__ (self , ttype , value ):
149+ super ().__init__ ()
150+ value = str (value )
151+ self .value = value
152+ self .ttype = ttype
153+ self .is_keyword = ttype in T .Keyword
154+ self .is_whitespace = ttype in T .Whitespace
155+ self .is_newline = self .ttype in T .Newline
156+ self .normalized = value .upper () if self .is_keyword else value
157+
158+ def __str__ (self ):
159+ return self .value
160+
161+ def _get_repr_name (self ):
162+ return str (self .ttype ).split ('.' )[- 1 ]
163+
164+ def flatten (self ):
165+ """Resolve subgroups."""
166+ yield self
167+
168+
169+ class TokenList (TokenBase ):
151170 """A group of tokens.
152171
153- It has an additional instance attribute ``tokens`` which holds a
154- list of child-tokens.
172+ It has two additional instance attributes, ``value``, which is the value of
173+ the token list, and ``tokens``, which holds a list of child-tokens.
155174 """
156175
157- __slots__ = 'tokens'
176+ __slots__ = ('tokens' , 'value' )
177+
178+ is_group = True
179+ ttype = None
180+ is_keyword = False
181+ is_whitespace = False
182+ is_newline = False
158183
159184 def __init__ (self , tokens = None ):
185+ super ().__init__ ()
160186 self .tokens = tokens or []
187+ self .value = str (self )
161188 [setattr (token , 'parent' , self ) for token in self .tokens ]
162- super ().__init__ (None , str (self ))
163- self .is_group = True
164189
165190 def __str__ (self ):
166191 return '' .join (token .value for token in self .flatten ())
167192
193+ @property
194+ def normalized (self ):
195+ return self .value
196+
168197 # weird bug
169198 # def __len__(self):
170199 # return len(self.tokens)
0 commit comments